/[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.63 by wakaba, Sun Nov 11 06:54:36 2007 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;
96        $s = \ (Encode::decode ($charset, $$bytes_s));
97        $self->{input_encoding} = lc $charset; ## TODO: normalize name ## TODO: set $doc->input_encoding
98        $self->{confident} = 1;
99      } else {
100        $s = ref $_[0] ? $_[0] : \($_[0]);
101        $self->{confident} = 0;
102      }
103    
104      $self->{change_encoding} = sub {
105        my $self = shift;
106        my $charset = lc shift;
107        ## TODO: if $charset is supported
108        ## TODO: normalize charset name
109    
110        ## "Change the encoding" algorithm:
111    
112        ## Step 1    
113        if ($charset eq 'utf-16') { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?
114          $charset = 'utf-8';
115        }
116    
117        ## Step 2
118        if (defined $self->{input_encoding} and
119            $self->{input_encoding} eq $charset) {
120          $self->{confident} = 1;
121          return;
122        }
123    
124        !!!parse-error (type => 'charset label detected', level => 'w');
125    
126        ## Step 3
127        # if (can) {
128          ## change the encoding on the fly.
129          #$self->{confident} = 1;
130          #return;
131        # }
132    
133        ## Step 4
134        throw Whatpm::HTML::RestartParser (charset => $charset);
135      }; # $self->{change_encoding}
136    
137      my @args = @_; shift @args; # $s
138      my $return;
139      try {
140        $return = $self->parse_char_string ($s, @args);  
141      } catch Whatpm::HTML::RestartParser with {
142        my $charset = shift->{charset};
143        $s = \ (Encode::decode ($charset, $$bytes_s));    
144        $self->{input_encoding} = $charset; ## TODO: $doc->input_encoding;
145        $self->{confident} = 1;
146        $return = $self->parse_char_string ($s, @args);
147      };
148      return $return;
149    } # parse_byte_string
150    
151    *parse_char_string = \&parse_string;
152    
153  sub parse_string ($$$;$) {  sub parse_string ($$$;$) {
154    my $self = shift->new;    my $self = ref $_[0] ? shift : shift->new;
155    my $s = \$_[0];    my $s = ref $_[0] ? $_[0] : \($_[0]);
156    $self->{document} = $_[1];    $self->{document} = $_[1];
157      @{$self->{document}->child_nodes} = ();
158    
159    ## NOTE: |set_inner_html| copies most of this method's code    ## NOTE: |set_inner_html| copies most of this method's code
160    
161      $self->{confident} = 1 unless exists $self->{confident};
162    
163    my $i = 0;    my $i = 0;
164    my $line = 1;    my $line = 1;
165    my $column = 0;    my $column = 0;
# Line 147  sub new ($) { Line 216  sub new ($) {
216    $self->{parse_error} = sub {    $self->{parse_error} = sub {
217      #      #
218    };    };
219      $self->{change_encoding} = sub {
220        # if ($_[0] is a supported encoding) {
221        #   run "change the encoding" algorithm;
222        #   throw Whatpm::HTML::RestartParser (charset => $new_encoding);
223        # }
224      };
225      $self->{application_cache_selection} = sub {
226        #
227      };
228    return $self;    return $self;
229  } # new  } # new
230    
# Line 159  sub CDATA_CONTENT_MODEL () { CM_LIMITED_ Line 237  sub CDATA_CONTENT_MODEL () { CM_LIMITED_
237  sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }  sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }
238  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
239    
240    sub DATA_STATE () { 0 }
241    sub ENTITY_DATA_STATE () { 1 }
242    sub TAG_OPEN_STATE () { 2 }
243    sub CLOSE_TAG_OPEN_STATE () { 3 }
244    sub TAG_NAME_STATE () { 4 }
245    sub BEFORE_ATTRIBUTE_NAME_STATE () { 5 }
246    sub ATTRIBUTE_NAME_STATE () { 6 }
247    sub AFTER_ATTRIBUTE_NAME_STATE () { 7 }
248    sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 }
249    sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
250    sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
251    sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
252    sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
253    sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
254    sub COMMENT_START_STATE () { 14 }
255    sub COMMENT_START_DASH_STATE () { 15 }
256    sub COMMENT_STATE () { 16 }
257    sub COMMENT_END_STATE () { 17 }
258    sub COMMENT_END_DASH_STATE () { 18 }
259    sub BOGUS_COMMENT_STATE () { 19 }
260    sub DOCTYPE_STATE () { 20 }
261    sub BEFORE_DOCTYPE_NAME_STATE () { 21 }
262    sub DOCTYPE_NAME_STATE () { 22 }
263    sub AFTER_DOCTYPE_NAME_STATE () { 23 }
264    sub BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 24 }
265    sub DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE () { 25 }
266    sub DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE () { 26 }
267    sub AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 27 }
268    sub BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 28 }
269    sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE () { 29 }
270    sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }
271    sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
272    sub BOGUS_DOCTYPE_STATE () { 32 }
273    
274    sub DOCTYPE_TOKEN () { 1 }
275    sub COMMENT_TOKEN () { 2 }
276    sub START_TAG_TOKEN () { 3 }
277    sub END_TAG_TOKEN () { 4 }
278    sub END_OF_FILE_TOKEN () { 5 }
279    sub CHARACTER_TOKEN () { 6 }
280    
281    sub AFTER_HTML_IMS () { 0b100 }
282    sub HEAD_IMS ()       { 0b1000 }
283    sub BODY_IMS ()       { 0b10000 }
284    sub BODY_TABLE_IMS () { 0b100000 }
285    sub TABLE_IMS ()      { 0b1000000 }
286    sub ROW_IMS ()        { 0b10000000 }
287    sub BODY_AFTER_IMS () { 0b100000000 }
288    sub FRAME_IMS ()      { 0b1000000000 }
289    
290    sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
291    sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
292    sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
293    sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
294    sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
295    sub BEFORE_HEAD_IM () { HEAD_IMS | 0b11 }
296    sub IN_BODY_IM () { BODY_IMS }
297    sub IN_CELL_IM () { BODY_IMS | BODY_TABLE_IMS | 0b01 }
298    sub IN_CAPTION_IM () { BODY_IMS | BODY_TABLE_IMS | 0b10 }
299    sub IN_ROW_IM () { TABLE_IMS | ROW_IMS | 0b01 }
300    sub IN_TABLE_BODY_IM () { TABLE_IMS | ROW_IMS | 0b10 }
301    sub IN_TABLE_IM () { TABLE_IMS }
302    sub AFTER_BODY_IM () { BODY_AFTER_IMS }
303    sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
304    sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
305    sub IN_SELECT_IM () { 0b01 }
306    sub IN_COLUMN_GROUP_IM () { 0b10 }
307    
308  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
309    
310  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
311    my $self = shift;    my $self = shift;
312    $self->{state} = 'data'; # MUST    $self->{state} = DATA_STATE; # MUST
313    $self->{content_model} = PCDATA_CONTENT_MODEL; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
314    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE
315    undef $self->{current_attribute};    undef $self->{current_attribute};
# Line 177  sub _initialize_tokenizer ($) { Line 323  sub _initialize_tokenizer ($) {
323  } # _initialize_tokenizer  } # _initialize_tokenizer
324    
325  ## A token has:  ## A token has:
326  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',  ##   ->{type} == DOCTYPE_TOKEN, START_TAG_TOKEN, END_TAG_TOKEN, COMMENT_TOKEN,
327  ##       'character', or 'end-of-file'  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN
328  ##   ->{name} (DOCTYPE, start tag (tag name), end tag (tag name))  ##   ->{name} (DOCTYPE_TOKEN)
329  ##   ->{public_identifier} (DOCTYPE)  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
330  ##   ->{system_identifier} (DOCTYPE)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
331  ##   ->{correct} == 1 or 0 (DOCTYPE)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
332  ##   ->{attributes} isa HASH (start tag, end tag)  ##   ->{correct} == 1 or 0 (DOCTYPE_TOKEN)
333  ##   ->{data} (comment, character)  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
334    ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
335    
336  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
337    
# Line 194  sub _initialize_tokenizer ($) { Line 341  sub _initialize_tokenizer ($) {
341  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
342  ## and removed from the list.  ## and removed from the list.
343    
344    ## NOTE: HTML5 "Writing HTML documents" section, applied to
345    ## documents and not to user agents and conformance checkers,
346    ## contains some requirements that are not detected by the
347    ## parsing algorithm:
348    ## - Some requirements on character encoding declarations. ## TODO
349    ## - "Elements MUST NOT contain content that their content model disallows."
350    ##   ... Some are parse error, some are not (will be reported by c.c.).
351    ## - Polytheistic slash SHOULD NOT be used. (Applied only to atheists.) ## TODO
352    ## - Text (in elements, attributes, and comments) SHOULD NOT contain
353    ##   control characters other than space characters. ## TODO: (what is control character? C0, C1 and DEL?  Unicode control character?)
354    
355    ## TODO: HTML5 poses authors two SHOULD-level requirements that cannot
356    ## be detected by the HTML5 parsing algorithm:
357    ## - Text,
358    
359  sub _get_next_token ($) {  sub _get_next_token ($) {
360    my $self = shift;    my $self = shift;
361    if (@{$self->{token}}) {    if (@{$self->{token}}) {
# Line 201  sub _get_next_token ($) { Line 363  sub _get_next_token ($) {
363    }    }
364    
365    A: {    A: {
366      if ($self->{state} eq 'data') {      if ($self->{state} == DATA_STATE) {
367        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_input_character} == 0x0026) { # &
368          if ($self->{content_model} & CM_ENTITY) { # PCDATA | RCDATA          if ($self->{content_model} & CM_ENTITY) { # PCDATA | RCDATA
369            $self->{state} = 'entity data';            $self->{state} = ENTITY_DATA_STATE;
370            !!!next-input-character;            !!!next-input-character;
371            redo A;            redo A;
372          } else {          } else {
# Line 226  sub _get_next_token ($) { Line 388  sub _get_next_token ($) {
388          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
389              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
390               not $self->{escape})) {               not $self->{escape})) {
391            $self->{state} = 'tag open';            $self->{state} = TAG_OPEN_STATE;
392            !!!next-input-character;            !!!next-input-character;
393            redo A;            redo A;
394          } else {          } else {
# Line 243  sub _get_next_token ($) { Line 405  sub _get_next_token ($) {
405                    
406          #          #
407        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
408          !!!emit ({type => 'end-of-file'});          !!!emit ({type => END_OF_FILE_TOKEN});
409          last A; ## TODO: ok?          last A; ## TODO: ok?
410        }        }
411        # Anything else        # Anything else
412        my $token = {type => 'character',        my $token = {type => CHARACTER_TOKEN,
413                     data => chr $self->{next_input_character}};                     data => chr $self->{next_input_character}};
414        ## Stay in the data state        ## Stay in the data state
415        !!!next-input-character;        !!!next-input-character;
# Line 255  sub _get_next_token ($) { Line 417  sub _get_next_token ($) {
417        !!!emit ($token);        !!!emit ($token);
418    
419        redo A;        redo A;
420      } elsif ($self->{state} eq 'entity data') {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
421        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
422                
423        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);
424    
425        $self->{state} = 'data';        $self->{state} = DATA_STATE;
426        # next-input-character is already done        # next-input-character is already done
427    
428        unless (defined $token) {        unless (defined $token) {
429          !!!emit ({type => 'character', data => '&'});          !!!emit ({type => CHARACTER_TOKEN, data => '&'});
430        } else {        } else {
431          !!!emit ($token);          !!!emit ($token);
432        }        }
433    
434        redo A;        redo A;
435      } elsif ($self->{state} eq 'tag open') {      } elsif ($self->{state} == TAG_OPEN_STATE) {
436        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
437          if ($self->{next_input_character} == 0x002F) { # /          if ($self->{next_input_character} == 0x002F) { # /
438            !!!next-input-character;            !!!next-input-character;
439            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
440            redo A;            redo A;
441          } else {          } else {
442            ## reconsume            ## reconsume
443            $self->{state} = 'data';            $self->{state} = DATA_STATE;
444    
445            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<'});
446    
447            redo A;            redo A;
448          }          }
449        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
450          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_input_character} == 0x0021) { # !
451            $self->{state} = 'markup declaration open';            $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
452            !!!next-input-character;            !!!next-input-character;
453            redo A;            redo A;
454          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{next_input_character} == 0x002F) { # /
455            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
456            !!!next-input-character;            !!!next-input-character;
457            redo A;            redo A;
458          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_input_character} and
459                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{next_input_character} <= 0x005A) { # A..Z
460            $self->{current_token}            $self->{current_token}
461              = {type => 'start tag',              = {type => START_TAG_TOKEN,
462                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_input_character} + 0x0020)};
463            $self->{state} = 'tag name';            $self->{state} = TAG_NAME_STATE;
464            !!!next-input-character;            !!!next-input-character;
465            redo A;            redo A;
466          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_input_character} and
467                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_input_character} <= 0x007A) { # a..z
468            $self->{current_token} = {type => 'start tag',            $self->{current_token} = {type => START_TAG_TOKEN,
469                              tag_name => chr ($self->{next_input_character})};                              tag_name => chr ($self->{next_input_character})};
470            $self->{state} = 'tag name';            $self->{state} = TAG_NAME_STATE;
471            !!!next-input-character;            !!!next-input-character;
472            redo A;            redo A;
473          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_input_character} == 0x003E) { # >
474            !!!parse-error (type => 'empty start tag');            !!!parse-error (type => 'empty start tag');
475            $self->{state} = 'data';            $self->{state} = DATA_STATE;
476            !!!next-input-character;            !!!next-input-character;
477    
478            !!!emit ({type => 'character', data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});
479    
480            redo A;            redo A;
481          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_input_character} == 0x003F) { # ?
482            !!!parse-error (type => 'pio');            !!!parse-error (type => 'pio');
483            $self->{state} = 'bogus comment';            $self->{state} = BOGUS_COMMENT_STATE;
484            ## $self->{next_input_character} is intentionally left as is            ## $self->{next_input_character} is intentionally left as is
485            redo A;            redo A;
486          } else {          } else {
487            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago');
488            $self->{state} = 'data';            $self->{state} = DATA_STATE;
489            ## reconsume            ## reconsume
490    
491            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<'});
492    
493            redo A;            redo A;
494          }          }
495        } else {        } else {
496          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
497        }        }
498      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
499        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
500          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
501            ## 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 510  sub _get_next_token ($) {
510              } else {              } else {
511                $self->{next_input_character} = shift @next_char; # reconsume                $self->{next_input_character} = shift @next_char; # reconsume
512                !!!back-next-input-character (@next_char);                !!!back-next-input-character (@next_char);
513                $self->{state} = 'data';                $self->{state} = DATA_STATE;
514    
515                !!!emit ({type => 'character', data => '</'});                !!!emit ({type => CHARACTER_TOKEN, data => '</'});
516        
517                redo A;                redo A;
518              }              }
# Line 367  sub _get_next_token ($) { Line 529  sub _get_next_token ($) {
529                    $self->{next_input_character} == -1) {                    $self->{next_input_character} == -1) {
530              $self->{next_input_character} = shift @next_char; # reconsume              $self->{next_input_character} = shift @next_char; # reconsume
531              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
532              $self->{state} = 'data';              $self->{state} = DATA_STATE;
533              !!!emit ({type => 'character', data => '</'});              !!!emit ({type => CHARACTER_TOKEN, data => '</'});
534              redo A;              redo A;
535            } else {            } else {
536              $self->{next_input_character} = shift @next_char;              $self->{next_input_character} = shift @next_char;
# Line 378  sub _get_next_token ($) { Line 540  sub _get_next_token ($) {
540          } else {          } else {
541            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
542            # next-input-character is already done            # next-input-character is already done
543            $self->{state} = 'data';            $self->{state} = DATA_STATE;
544            !!!emit ({type => 'character', data => '</'});            !!!emit ({type => CHARACTER_TOKEN, data => '</'});
545            redo A;            redo A;
546          }          }
547        }        }
548                
549        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_input_character} and
550            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_input_character} <= 0x005A) { # A..Z
551          $self->{current_token} = {type => 'end tag',          $self->{current_token} = {type => END_TAG_TOKEN,
552                            tag_name => chr ($self->{next_input_character} + 0x0020)};                            tag_name => chr ($self->{next_input_character} + 0x0020)};
553          $self->{state} = 'tag name';          $self->{state} = TAG_NAME_STATE;
554          !!!next-input-character;          !!!next-input-character;
555          redo A;          redo A;
556        } elsif (0x0061 <= $self->{next_input_character} and        } elsif (0x0061 <= $self->{next_input_character} and
557                 $self->{next_input_character} <= 0x007A) { # a..z                 $self->{next_input_character} <= 0x007A) { # a..z
558          $self->{current_token} = {type => 'end tag',          $self->{current_token} = {type => END_TAG_TOKEN,
559                            tag_name => chr ($self->{next_input_character})};                            tag_name => chr ($self->{next_input_character})};
560          $self->{state} = 'tag name';          $self->{state} = TAG_NAME_STATE;
561          !!!next-input-character;          !!!next-input-character;
562          redo A;          redo A;
563        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
564          !!!parse-error (type => 'empty end tag');          !!!parse-error (type => 'empty end tag');
565          $self->{state} = 'data';          $self->{state} = DATA_STATE;
566          !!!next-input-character;          !!!next-input-character;
567          redo A;          redo A;
568        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
569          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
570          $self->{state} = 'data';          $self->{state} = DATA_STATE;
571          # reconsume          # reconsume
572    
573          !!!emit ({type => 'character', data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</'});
574    
575          redo A;          redo A;
576        } else {        } else {
577          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
578          $self->{state} = 'bogus comment';          $self->{state} = BOGUS_COMMENT_STATE;
579          ## $self->{next_input_character} is intentionally left as is          ## $self->{next_input_character} is intentionally left as is
580          redo A;          redo A;
581        }        }
582      } elsif ($self->{state} eq 'tag name') {      } elsif ($self->{state} == TAG_NAME_STATE) {
583        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
584            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
585            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
586            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
587            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
588          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
589          !!!next-input-character;          !!!next-input-character;
590          redo A;          redo A;
591        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
592          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
593            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
594                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
595            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
596          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
597            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
598            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
599              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 439  sub _get_next_token ($) { Line 601  sub _get_next_token ($) {
601          } else {          } else {
602            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
603          }          }
604          $self->{state} = 'data';          $self->{state} = DATA_STATE;
605          !!!next-input-character;          !!!next-input-character;
606    
607          !!!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 616  sub _get_next_token ($) {
616          redo A;          redo A;
617        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
618          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
619          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
620            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
621                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
622            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
623          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
624            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
625            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
626              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 466  sub _get_next_token ($) { Line 628  sub _get_next_token ($) {
628          } else {          } else {
629            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
630          }          }
631          $self->{state} = 'data';          $self->{state} = DATA_STATE;
632          # reconsume          # reconsume
633    
634          !!!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 637  sub _get_next_token ($) {
637        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
638          !!!next-input-character;          !!!next-input-character;
639          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
640              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
641              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
642            # permitted slash            # permitted slash
643            #            #
644          } else {          } else {
645            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
646          }          }
647          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
648          # next-input-character is already done          # next-input-character is already done
649          redo A;          redo A;
650        } else {        } else {
# Line 492  sub _get_next_token ($) { Line 654  sub _get_next_token ($) {
654          !!!next-input-character;          !!!next-input-character;
655          redo A;          redo A;
656        }        }
657      } elsif ($self->{state} eq 'before attribute name') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
658        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
659            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
660            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 502  sub _get_next_token ($) { Line 664  sub _get_next_token ($) {
664          !!!next-input-character;          !!!next-input-character;
665          redo A;          redo A;
666        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
667          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
668            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
669                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
670            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
671          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
672            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
673            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
674              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 514  sub _get_next_token ($) { Line 676  sub _get_next_token ($) {
676          } else {          } else {
677            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
678          }          }
679          $self->{state} = 'data';          $self->{state} = DATA_STATE;
680          !!!next-input-character;          !!!next-input-character;
681    
682          !!!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 686  sub _get_next_token ($) {
686                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_input_character} <= 0x005A) { # A..Z
687          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),
688                                value => ''};                                value => ''};
689          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
690          !!!next-input-character;          !!!next-input-character;
691          redo A;          redo A;
692        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
693          !!!next-input-character;          !!!next-input-character;
694          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
695              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
696              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
697            # permitted slash            # permitted slash
698            #            #
# Line 542  sub _get_next_token ($) { Line 704  sub _get_next_token ($) {
704          redo A;          redo A;
705        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
706          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
707          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
708            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
709                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
710            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
711          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
712            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
713            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
714              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 554  sub _get_next_token ($) { Line 716  sub _get_next_token ($) {
716          } else {          } else {
717            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
718          }          }
719          $self->{state} = 'data';          $self->{state} = DATA_STATE;
720          # reconsume          # reconsume
721    
722          !!!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 725  sub _get_next_token ($) {
725        } else {        } else {
726          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          $self->{current_attribute} = {name => chr ($self->{next_input_character}),
727                                value => ''};                                value => ''};
728          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
729          !!!next-input-character;          !!!next-input-character;
730          redo A;          redo A;
731        }        }
732      } elsif ($self->{state} eq 'attribute name') {      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {
733        my $before_leave = sub {        my $before_leave = sub {
734          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
735              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
# Line 585  sub _get_next_token ($) { Line 747  sub _get_next_token ($) {
747            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
748            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
749          $before_leave->();          $before_leave->();
750          $self->{state} = 'after attribute name';          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
751          !!!next-input-character;          !!!next-input-character;
752          redo A;          redo A;
753        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_input_character} == 0x003D) { # =
754          $before_leave->();          $before_leave->();
755          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
756          !!!next-input-character;          !!!next-input-character;
757          redo A;          redo A;
758        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
759          $before_leave->();          $before_leave->();
760          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
761            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
762                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
763            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
764          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
765            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
766            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
767              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 607  sub _get_next_token ($) { Line 769  sub _get_next_token ($) {
769          } else {          } else {
770            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
771          }          }
772          $self->{state} = 'data';          $self->{state} = DATA_STATE;
773          !!!next-input-character;          !!!next-input-character;
774    
775          !!!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 785  sub _get_next_token ($) {
785          $before_leave->();          $before_leave->();
786          !!!next-input-character;          !!!next-input-character;
787          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
788              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
789              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
790            # permitted slash            # permitted slash
791            #            #
792          } else {          } else {
793            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
794          }          }
795          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
796          # next-input-character is already done          # next-input-character is already done
797          redo A;          redo A;
798        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
799          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
800          $before_leave->();          $before_leave->();
801          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
802            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
803                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
804            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
805          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
806            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
807            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
808              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 648  sub _get_next_token ($) { Line 810  sub _get_next_token ($) {
810          } else {          } else {
811            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
812          }          }
813          $self->{state} = 'data';          $self->{state} = DATA_STATE;
814          # reconsume          # reconsume
815    
816          !!!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 822  sub _get_next_token ($) {
822          !!!next-input-character;          !!!next-input-character;
823          redo A;          redo A;
824        }        }
825      } elsif ($self->{state} eq 'after attribute name') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
826        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
827            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
828            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 670  sub _get_next_token ($) { Line 832  sub _get_next_token ($) {
832          !!!next-input-character;          !!!next-input-character;
833          redo A;          redo A;
834        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_input_character} == 0x003D) { # =
835          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
836          !!!next-input-character;          !!!next-input-character;
837          redo A;          redo A;
838        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
839          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
840            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
841                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
842            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
843          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
844            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
845            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
846              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 686  sub _get_next_token ($) { Line 848  sub _get_next_token ($) {
848          } else {          } else {
849            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
850          }          }
851          $self->{state} = 'data';          $self->{state} = DATA_STATE;
852          !!!next-input-character;          !!!next-input-character;
853    
854          !!!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 858  sub _get_next_token ($) {
858                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_input_character} <= 0x005A) { # A..Z
859          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),
860                                value => ''};                                value => ''};
861          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
862          !!!next-input-character;          !!!next-input-character;
863          redo A;          redo A;
864        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
865          !!!next-input-character;          !!!next-input-character;
866          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
867              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
868              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
869            # permitted slash            # permitted slash
870            #            #
# Line 710  sub _get_next_token ($) { Line 872  sub _get_next_token ($) {
872            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
873            ## TODO: Different error type for <aa / bb> than <aa/>            ## TODO: Different error type for <aa / bb> than <aa/>
874          }          }
875          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
876          # next-input-character is already done          # next-input-character is already done
877          redo A;          redo A;
878        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
879          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
880          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
881            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
882                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
883            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
884          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
885            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
886            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
887              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 727  sub _get_next_token ($) { Line 889  sub _get_next_token ($) {
889          } else {          } else {
890            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
891          }          }
892          $self->{state} = 'data';          $self->{state} = DATA_STATE;
893          # reconsume          # reconsume
894    
895          !!!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 898  sub _get_next_token ($) {
898        } else {        } else {
899          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          $self->{current_attribute} = {name => chr ($self->{next_input_character}),
900                                value => ''};                                value => ''};
901          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
902          !!!next-input-character;          !!!next-input-character;
903          redo A;                  redo A;        
904        }        }
905      } elsif ($self->{state} eq 'before attribute value') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
906        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
907            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
908            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 750  sub _get_next_token ($) { Line 912  sub _get_next_token ($) {
912          !!!next-input-character;          !!!next-input-character;
913          redo A;          redo A;
914        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_input_character} == 0x0022) { # "
915          $self->{state} = 'attribute value (double-quoted)';          $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
916          !!!next-input-character;          !!!next-input-character;
917          redo A;          redo A;
918        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
919          $self->{state} = 'attribute value (unquoted)';          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
920          ## reconsume          ## reconsume
921          redo A;          redo A;
922        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_input_character} == 0x0027) { # '
923          $self->{state} = 'attribute value (single-quoted)';          $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
924          !!!next-input-character;          !!!next-input-character;
925          redo A;          redo A;
926        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
927          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
928            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
929                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
930            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
931          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
932            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
933            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
934              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 774  sub _get_next_token ($) { Line 936  sub _get_next_token ($) {
936          } else {          } else {
937            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
938          }          }
939          $self->{state} = 'data';          $self->{state} = DATA_STATE;
940          !!!next-input-character;          !!!next-input-character;
941    
942          !!!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 944  sub _get_next_token ($) {
944          redo A;          redo A;
945        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
946          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
947          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
948            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
949                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
950            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
951          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
952            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
953            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
954              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 794  sub _get_next_token ($) { Line 956  sub _get_next_token ($) {
956          } else {          } else {
957            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
958          }          }
959          $self->{state} = 'data';          $self->{state} = DATA_STATE;
960          ## reconsume          ## reconsume
961    
962          !!!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 964  sub _get_next_token ($) {
964          redo A;          redo A;
965        } else {        } else {
966          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});
967          $self->{state} = 'attribute value (unquoted)';          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
968          !!!next-input-character;          !!!next-input-character;
969          redo A;          redo A;
970        }        }
971      } elsif ($self->{state} eq 'attribute value (double-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
972        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_input_character} == 0x0022) { # "
973          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
974          !!!next-input-character;          !!!next-input-character;
975          redo A;          redo A;
976        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
977          $self->{last_attribute_value_state} = 'attribute value (double-quoted)';          $self->{last_attribute_value_state} = $self->{state};
978          $self->{state} = 'entity in attribute value';          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
979          !!!next-input-character;          !!!next-input-character;
980          redo A;          redo A;
981        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
982          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
983          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
984            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
985                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
986            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
987          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
988            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
989            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
990              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 830  sub _get_next_token ($) { Line 992  sub _get_next_token ($) {
992          } else {          } else {
993            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
994          }          }
995          $self->{state} = 'data';          $self->{state} = DATA_STATE;
996          ## reconsume          ## reconsume
997    
998          !!!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 1004  sub _get_next_token ($) {
1004          !!!next-input-character;          !!!next-input-character;
1005          redo A;          redo A;
1006        }        }
1007      } elsif ($self->{state} eq 'attribute value (single-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1008        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_input_character} == 0x0027) { # '
1009          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1010          !!!next-input-character;          !!!next-input-character;
1011          redo A;          redo A;
1012        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
1013          $self->{last_attribute_value_state} = 'attribute value (single-quoted)';          $self->{last_attribute_value_state} = $self->{state};
1014          $self->{state} = 'entity in attribute value';          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1015          !!!next-input-character;          !!!next-input-character;
1016          redo A;          redo A;
1017        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1018          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1019          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1020            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
1021                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
1022            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1023          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1024            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1025            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1026              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 866  sub _get_next_token ($) { Line 1028  sub _get_next_token ($) {
1028          } else {          } else {
1029            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1030          }          }
1031          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1032          ## reconsume          ## reconsume
1033    
1034          !!!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 1040  sub _get_next_token ($) {
1040          !!!next-input-character;          !!!next-input-character;
1041          redo A;          redo A;
1042        }        }
1043      } elsif ($self->{state} eq 'attribute value (unquoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1044        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1045            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1046            $self->{next_input_character} == 0x000B or # HT            $self->{next_input_character} == 0x000B or # HT
1047            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1048            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
1049          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1050          !!!next-input-character;          !!!next-input-character;
1051          redo A;          redo A;
1052        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
1053          $self->{last_attribute_value_state} = 'attribute value (unquoted)';          $self->{last_attribute_value_state} = $self->{state};
1054          $self->{state} = 'entity in attribute value';          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1055          !!!next-input-character;          !!!next-input-character;
1056          redo A;          redo A;
1057        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1058          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1059            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
1060                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
1061            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1062          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1063            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1064            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1065              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 905  sub _get_next_token ($) { Line 1067  sub _get_next_token ($) {
1067          } else {          } else {
1068            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1069          }          }
1070          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1071          !!!next-input-character;          !!!next-input-character;
1072    
1073          !!!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 1075  sub _get_next_token ($) {
1075          redo A;          redo A;
1076        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1077          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1078          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1079            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
1080                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
1081            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1082          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1083            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1084            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1085              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 925  sub _get_next_token ($) { Line 1087  sub _get_next_token ($) {
1087          } else {          } else {
1088            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1089          }          }
1090          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1091          ## reconsume          ## reconsume
1092    
1093          !!!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 1099  sub _get_next_token ($) {
1099          !!!next-input-character;          !!!next-input-character;
1100          redo A;          redo A;
1101        }        }
1102      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {
1103        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);
1104    
1105        unless (defined $token) {        unless (defined $token) {
# Line 950  sub _get_next_token ($) { Line 1112  sub _get_next_token ($) {
1112        $self->{state} = $self->{last_attribute_value_state};        $self->{state} = $self->{last_attribute_value_state};
1113        # next-input-character is already done        # next-input-character is already done
1114        redo A;        redo A;
1115      } elsif ($self->{state} eq 'bogus comment') {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1116        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1117                
1118        my $token = {type => 'comment', data => ''};        my $token = {type => COMMENT_TOKEN, data => ''};
1119    
1120        BC: {        BC: {
1121          if ($self->{next_input_character} == 0x003E) { # >          if ($self->{next_input_character} == 0x003E) { # >
1122            $self->{state} = 'data';            $self->{state} = DATA_STATE;
1123            !!!next-input-character;            !!!next-input-character;
1124    
1125            !!!emit ($token);            !!!emit ($token);
1126    
1127            redo A;            redo A;
1128          } elsif ($self->{next_input_character} == -1) {          } elsif ($self->{next_input_character} == -1) {
1129            $self->{state} = 'data';            $self->{state} = DATA_STATE;
1130            ## reconsume            ## reconsume
1131    
1132            !!!emit ($token);            !!!emit ($token);
# Line 976  sub _get_next_token ($) { Line 1138  sub _get_next_token ($) {
1138            redo BC;            redo BC;
1139          }          }
1140        } # BC        } # BC
1141      } elsif ($self->{state} eq 'markup declaration open') {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1142        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1143    
1144        my @next_char;        my @next_char;
# Line 986  sub _get_next_token ($) { Line 1148  sub _get_next_token ($) {
1148          !!!next-input-character;          !!!next-input-character;
1149          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_input_character};
1150          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_input_character} == 0x002D) { # -
1151            $self->{current_token} = {type => 'comment', data => ''};            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};
1152            $self->{state} = 'comment start';            $self->{state} = COMMENT_START_STATE;
1153            !!!next-input-character;            !!!next-input-character;
1154            redo A;            redo A;
1155          }          }
# Line 1018  sub _get_next_token ($) { Line 1180  sub _get_next_token ($) {
1180                    if ($self->{next_input_character} == 0x0045 or # E                    if ($self->{next_input_character} == 0x0045 or # E
1181                        $self->{next_input_character} == 0x0065) { # e                        $self->{next_input_character} == 0x0065) { # e
1182                      ## ISSUE: What a stupid code this is!                      ## ISSUE: What a stupid code this is!
1183                      $self->{state} = 'DOCTYPE';                      $self->{state} = DOCTYPE_STATE;
1184                      !!!next-input-character;                      !!!next-input-character;
1185                      redo A;                      redo A;
1186                    }                    }
# Line 1032  sub _get_next_token ($) { Line 1194  sub _get_next_token ($) {
1194        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'bogus comment');
1195        $self->{next_input_character} = shift @next_char;        $self->{next_input_character} = shift @next_char;
1196        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
1197        $self->{state} = 'bogus comment';        $self->{state} = BOGUS_COMMENT_STATE;
1198        redo A;        redo A;
1199                
1200        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
1201        ## 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?
1202      } elsif ($self->{state} eq 'comment start') {      } elsif ($self->{state} == COMMENT_START_STATE) {
1203        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1204          $self->{state} = 'comment start dash';          $self->{state} = COMMENT_START_DASH_STATE;
1205          !!!next-input-character;          !!!next-input-character;
1206          redo A;          redo A;
1207        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1208          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
1209          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1210          !!!next-input-character;          !!!next-input-character;
1211    
1212          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1052  sub _get_next_token ($) { Line 1214  sub _get_next_token ($) {
1214          redo A;          redo A;
1215        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1216          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1217          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1218          ## reconsume          ## reconsume
1219    
1220          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1061  sub _get_next_token ($) { Line 1223  sub _get_next_token ($) {
1223        } else {        } else {
1224          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
1225              .= chr ($self->{next_input_character});              .= chr ($self->{next_input_character});
1226          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1227          !!!next-input-character;          !!!next-input-character;
1228          redo A;          redo A;
1229        }        }
1230      } elsif ($self->{state} eq 'comment start dash') {      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
1231        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1232          $self->{state} = 'comment end';          $self->{state} = COMMENT_END_STATE;
1233          !!!next-input-character;          !!!next-input-character;
1234          redo A;          redo A;
1235        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1236          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
1237          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1238          !!!next-input-character;          !!!next-input-character;
1239    
1240          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1080  sub _get_next_token ($) { Line 1242  sub _get_next_token ($) {
1242          redo A;          redo A;
1243        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1244          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1245          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1246          ## reconsume          ## reconsume
1247    
1248          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1089  sub _get_next_token ($) { Line 1251  sub _get_next_token ($) {
1251        } else {        } else {
1252          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
1253              .= '-' . chr ($self->{next_input_character});              .= '-' . chr ($self->{next_input_character});
1254          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1255          !!!next-input-character;          !!!next-input-character;
1256          redo A;          redo A;
1257        }        }
1258      } elsif ($self->{state} eq 'comment') {      } elsif ($self->{state} == COMMENT_STATE) {
1259        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1260          $self->{state} = 'comment end dash';          $self->{state} = COMMENT_END_DASH_STATE;
1261          !!!next-input-character;          !!!next-input-character;
1262          redo A;          redo A;
1263        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1264          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1265          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1266          ## reconsume          ## reconsume
1267    
1268          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1112  sub _get_next_token ($) { Line 1274  sub _get_next_token ($) {
1274          !!!next-input-character;          !!!next-input-character;
1275          redo A;          redo A;
1276        }        }
1277      } elsif ($self->{state} eq 'comment end dash') {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
1278        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1279          $self->{state} = 'comment end';          $self->{state} = COMMENT_END_STATE;
1280          !!!next-input-character;          !!!next-input-character;
1281          redo A;          redo A;
1282        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1283          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1284          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1285          ## reconsume          ## reconsume
1286    
1287          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1127  sub _get_next_token ($) { Line 1289  sub _get_next_token ($) {
1289          redo A;          redo A;
1290        } else {        } else {
1291          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment
1292          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1293          !!!next-input-character;          !!!next-input-character;
1294          redo A;          redo A;
1295        }        }
1296      } elsif ($self->{state} eq 'comment end') {      } elsif ($self->{state} == COMMENT_END_STATE) {
1297        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_input_character} == 0x003E) { # >
1298          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1299          !!!next-input-character;          !!!next-input-character;
1300    
1301          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1147  sub _get_next_token ($) { Line 1309  sub _get_next_token ($) {
1309          redo A;          redo A;
1310        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1311          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1312          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1313          ## reconsume          ## reconsume
1314    
1315          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1156  sub _get_next_token ($) { Line 1318  sub _get_next_token ($) {
1318        } else {        } else {
1319          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment');
1320          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment
1321          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1322          !!!next-input-character;          !!!next-input-character;
1323          redo A;          redo A;
1324        }        }
1325      } elsif ($self->{state} eq 'DOCTYPE') {      } elsif ($self->{state} == DOCTYPE_STATE) {
1326        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1327            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1328            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
1329            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1330            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
1331          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1332          !!!next-input-character;          !!!next-input-character;
1333          redo A;          redo A;
1334        } else {        } else {
1335          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
1336          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1337          ## reconsume          ## reconsume
1338          redo A;          redo A;
1339        }        }
1340      } elsif ($self->{state} eq 'before DOCTYPE name') {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
1341        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1342            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1343            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 1186  sub _get_next_token ($) { Line 1348  sub _get_next_token ($) {
1348          redo A;          redo A;
1349        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1350          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1351          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1352          !!!next-input-character;          !!!next-input-character;
1353    
1354          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect
1355    
1356          redo A;          redo A;
1357        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1358          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1359          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1360          ## reconsume          ## reconsume
1361    
1362          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect
1363    
1364          redo A;          redo A;
1365        } else {        } else {
1366          $self->{current_token}          $self->{current_token}
1367              = {type => 'DOCTYPE',              = {type => DOCTYPE_TOKEN,
1368                 name => chr ($self->{next_input_character}),                 name => chr ($self->{next_input_character}),
1369                 correct => 1};                 correct => 1};
1370  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
1371          $self->{state} = 'DOCTYPE name';          $self->{state} = DOCTYPE_NAME_STATE;
1372          !!!next-input-character;          !!!next-input-character;
1373          redo A;          redo A;
1374        }        }
1375      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
1376  ## ISSUE: Redundant "First," in the spec.  ## ISSUE: Redundant "First," in the spec.
1377        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1378            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1379            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
1380            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1381            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
1382          $self->{state} = 'after DOCTYPE name';          $self->{state} = AFTER_DOCTYPE_NAME_STATE;
1383          !!!next-input-character;          !!!next-input-character;
1384          redo A;          redo A;
1385        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1386          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1387          !!!next-input-character;          !!!next-input-character;
1388    
1389          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1229  sub _get_next_token ($) { Line 1391  sub _get_next_token ($) {
1391          redo A;          redo A;
1392        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1393          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1394          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1395          ## reconsume          ## reconsume
1396    
1397          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1243  sub _get_next_token ($) { Line 1405  sub _get_next_token ($) {
1405          !!!next-input-character;          !!!next-input-character;
1406          redo A;          redo A;
1407        }        }
1408      } elsif ($self->{state} eq 'after DOCTYPE name') {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
1409        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1410            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1411            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 1253  sub _get_next_token ($) { Line 1415  sub _get_next_token ($) {
1415          !!!next-input-character;          !!!next-input-character;
1416          redo A;          redo A;
1417        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1418          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1419          !!!next-input-character;          !!!next-input-character;
1420    
1421          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1261  sub _get_next_token ($) { Line 1423  sub _get_next_token ($) {
1423          redo A;          redo A;
1424        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1425          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1426          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1427          ## reconsume          ## reconsume
1428    
1429          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1285  sub _get_next_token ($) { Line 1447  sub _get_next_token ($) {
1447                  !!!next-input-character;                  !!!next-input-character;
1448                  if ($self->{next_input_character} == 0x0043 or # C                  if ($self->{next_input_character} == 0x0043 or # C
1449                      $self->{next_input_character} == 0x0063) { # c                      $self->{next_input_character} == 0x0063) { # c
1450                    $self->{state} = 'before DOCTYPE public identifier';                    $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1451                    !!!next-input-character;                    !!!next-input-character;
1452                    redo A;                    redo A;
1453                  }                  }
# Line 1312  sub _get_next_token ($) { Line 1474  sub _get_next_token ($) {
1474                  !!!next-input-character;                  !!!next-input-character;
1475                  if ($self->{next_input_character} == 0x004D or # M                  if ($self->{next_input_character} == 0x004D or # M
1476                      $self->{next_input_character} == 0x006D) { # m                      $self->{next_input_character} == 0x006D) { # m
1477                    $self->{state} = 'before DOCTYPE system identifier';                    $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1478                    !!!next-input-character;                    !!!next-input-character;
1479                    redo A;                    redo A;
1480                  }                  }
# Line 1328  sub _get_next_token ($) { Line 1490  sub _get_next_token ($) {
1490        }        }
1491    
1492        !!!parse-error (type => 'string after DOCTYPE name');        !!!parse-error (type => 'string after DOCTYPE name');
1493        $self->{state} = 'bogus DOCTYPE';        $self->{state} = BOGUS_DOCTYPE_STATE;
1494        # next-input-character is already done        # next-input-character is already done
1495        redo A;        redo A;
1496      } elsif ($self->{state} eq 'before DOCTYPE public identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
1497        if ({        if ({
1498              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1499              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1341  sub _get_next_token ($) { Line 1503  sub _get_next_token ($) {
1503          redo A;          redo A;
1504        } elsif ($self->{next_input_character} eq 0x0022) { # "        } elsif ($self->{next_input_character} eq 0x0022) { # "
1505          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1506          $self->{state} = 'DOCTYPE public identifier (double-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
1507          !!!next-input-character;          !!!next-input-character;
1508          redo A;          redo A;
1509        } elsif ($self->{next_input_character} eq 0x0027) { # '        } elsif ($self->{next_input_character} eq 0x0027) { # '
1510          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1511          $self->{state} = 'DOCTYPE public identifier (single-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
1512          !!!next-input-character;          !!!next-input-character;
1513          redo A;          redo A;
1514        } elsif ($self->{next_input_character} eq 0x003E) { # >        } elsif ($self->{next_input_character} eq 0x003E) { # >
1515          !!!parse-error (type => 'no PUBLIC literal');          !!!parse-error (type => 'no PUBLIC literal');
1516    
1517          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1518          !!!next-input-character;          !!!next-input-character;
1519    
1520          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1362  sub _get_next_token ($) { Line 1524  sub _get_next_token ($) {
1524        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1525          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1526    
1527          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1528          ## reconsume          ## reconsume
1529    
1530          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1371  sub _get_next_token ($) { Line 1533  sub _get_next_token ($) {
1533          redo A;          redo A;
1534        } else {        } else {
1535          !!!parse-error (type => 'string after PUBLIC');          !!!parse-error (type => 'string after PUBLIC');
1536          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1537          !!!next-input-character;          !!!next-input-character;
1538          redo A;          redo A;
1539        }        }
1540      } elsif ($self->{state} eq 'DOCTYPE public identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
1541        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_input_character} == 0x0022) { # "
1542          $self->{state} = 'after DOCTYPE public identifier';          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1543          !!!next-input-character;          !!!next-input-character;
1544          redo A;          redo A;
1545        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1546          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
1547    
1548          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1549          ## reconsume          ## reconsume
1550    
1551          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1397  sub _get_next_token ($) { Line 1559  sub _get_next_token ($) {
1559          !!!next-input-character;          !!!next-input-character;
1560          redo A;          redo A;
1561        }        }
1562      } elsif ($self->{state} eq 'DOCTYPE public identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
1563        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_input_character} == 0x0027) { # '
1564          $self->{state} = 'after DOCTYPE public identifier';          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1565          !!!next-input-character;          !!!next-input-character;
1566          redo A;          redo A;
1567        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1568          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
1569    
1570          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1571          ## reconsume          ## reconsume
1572    
1573          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1419  sub _get_next_token ($) { Line 1581  sub _get_next_token ($) {
1581          !!!next-input-character;          !!!next-input-character;
1582          redo A;          redo A;
1583        }        }
1584      } elsif ($self->{state} eq 'after DOCTYPE public identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
1585        if ({        if ({
1586              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1587              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1429  sub _get_next_token ($) { Line 1591  sub _get_next_token ($) {
1591          redo A;          redo A;
1592        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_input_character} == 0x0022) { # "
1593          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1594          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
1595          !!!next-input-character;          !!!next-input-character;
1596          redo A;          redo A;
1597        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_input_character} == 0x0027) { # '
1598          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1599          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
1600          !!!next-input-character;          !!!next-input-character;
1601          redo A;          redo A;
1602        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1603          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1604          !!!next-input-character;          !!!next-input-character;
1605    
1606          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1447  sub _get_next_token ($) { Line 1609  sub _get_next_token ($) {
1609        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1610          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1611    
1612          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1613          ## reconsume          ## reconsume
1614    
1615          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1456  sub _get_next_token ($) { Line 1618  sub _get_next_token ($) {
1618          redo A;          redo A;
1619        } else {        } else {
1620          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after PUBLIC literal');
1621          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1622          !!!next-input-character;          !!!next-input-character;
1623          redo A;          redo A;
1624        }        }
1625      } elsif ($self->{state} eq 'before DOCTYPE system identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
1626        if ({        if ({
1627              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1628              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1470  sub _get_next_token ($) { Line 1632  sub _get_next_token ($) {
1632          redo A;          redo A;
1633        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_input_character} == 0x0022) { # "
1634          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1635          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
1636          !!!next-input-character;          !!!next-input-character;
1637          redo A;          redo A;
1638        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_input_character} == 0x0027) { # '
1639          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1640          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
1641          !!!next-input-character;          !!!next-input-character;
1642          redo A;          redo A;
1643        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1644          !!!parse-error (type => 'no SYSTEM literal');          !!!parse-error (type => 'no SYSTEM literal');
1645          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1646          !!!next-input-character;          !!!next-input-character;
1647    
1648          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1490  sub _get_next_token ($) { Line 1652  sub _get_next_token ($) {
1652        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1653          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1654    
1655          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1656          ## reconsume          ## reconsume
1657    
1658          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1499  sub _get_next_token ($) { Line 1661  sub _get_next_token ($) {
1661          redo A;          redo A;
1662        } else {        } else {
1663          !!!parse-error (type => 'string after SYSTEM');          !!!parse-error (type => 'string after SYSTEM');
1664          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1665          !!!next-input-character;          !!!next-input-character;
1666          redo A;          redo A;
1667        }        }
1668      } elsif ($self->{state} eq 'DOCTYPE system identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
1669        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_input_character} == 0x0022) { # "
1670          $self->{state} = 'after DOCTYPE system identifier';          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1671          !!!next-input-character;          !!!next-input-character;
1672          redo A;          redo A;
1673        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1674          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
1675    
1676          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1677          ## reconsume          ## reconsume
1678    
1679          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1525  sub _get_next_token ($) { Line 1687  sub _get_next_token ($) {
1687          !!!next-input-character;          !!!next-input-character;
1688          redo A;          redo A;
1689        }        }
1690      } elsif ($self->{state} eq 'DOCTYPE system identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
1691        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_input_character} == 0x0027) { # '
1692          $self->{state} = 'after DOCTYPE system identifier';          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1693          !!!next-input-character;          !!!next-input-character;
1694          redo A;          redo A;
1695        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1696          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
1697    
1698          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1699          ## reconsume          ## reconsume
1700    
1701          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1547  sub _get_next_token ($) { Line 1709  sub _get_next_token ($) {
1709          !!!next-input-character;          !!!next-input-character;
1710          redo A;          redo A;
1711        }        }
1712      } elsif ($self->{state} eq 'after DOCTYPE system identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
1713        if ({        if ({
1714              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1715              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1556  sub _get_next_token ($) { Line 1718  sub _get_next_token ($) {
1718          !!!next-input-character;          !!!next-input-character;
1719          redo A;          redo A;
1720        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1721          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1722          !!!next-input-character;          !!!next-input-character;
1723    
1724          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1565  sub _get_next_token ($) { Line 1727  sub _get_next_token ($) {
1727        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1728          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1729    
1730          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1731          ## reconsume          ## reconsume
1732    
1733          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1574  sub _get_next_token ($) { Line 1736  sub _get_next_token ($) {
1736          redo A;          redo A;
1737        } else {        } else {
1738          !!!parse-error (type => 'string after SYSTEM literal');          !!!parse-error (type => 'string after SYSTEM literal');
1739          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1740          !!!next-input-character;          !!!next-input-character;
1741          redo A;          redo A;
1742        }        }
1743      } elsif ($self->{state} eq 'bogus DOCTYPE') {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
1744        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_input_character} == 0x003E) { # >
1745          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1746          !!!next-input-character;          !!!next-input-character;
1747    
1748          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1589  sub _get_next_token ($) { Line 1751  sub _get_next_token ($) {
1751          redo A;          redo A;
1752        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1753          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1754          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1755          ## reconsume          ## reconsume
1756    
1757          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1670  sub _tokenize_attempt_to_consume_an_enti Line 1832  sub _tokenize_attempt_to_consume_an_enti
1832            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
1833          }          }
1834    
1835          return {type => 'character', data => chr $code};          return {type => CHARACTER_TOKEN, data => chr $code};
1836        } # X        } # X
1837      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_input_character} and
1838               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_input_character} <= 0x0039) { # 0..9
# Line 1705  sub _tokenize_attempt_to_consume_an_enti Line 1867  sub _tokenize_attempt_to_consume_an_enti
1867          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
1868        }        }
1869                
1870        return {type => 'character', data => chr $code};        return {type => CHARACTER_TOKEN, data => chr $code};
1871      } else {      } else {
1872        !!!parse-error (type => 'bare nero');        !!!parse-error (type => 'bare nero');
1873        !!!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 1915  sub _tokenize_attempt_to_consume_an_enti
1915      }      }
1916            
1917      if ($match > 0) {      if ($match > 0) {
1918        return {type => 'character', data => $value};        return {type => CHARACTER_TOKEN, data => $value};
1919      } elsif ($match < 0) {      } elsif ($match < 0) {
1920        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc');
1921        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
1922          return {type => 'character', data => '&'.$entity_name};          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};
1923        } else {        } else {
1924          return {type => 'character', data => $value};          return {type => CHARACTER_TOKEN, data => $value};
1925        }        }
1926      } else {      } else {
1927        !!!parse-error (type => 'bare ero');        !!!parse-error (type => 'bare ero');
1928        ## NOTE: No characters are consumed in the spec.        ## NOTE: No characters are consumed in the spec.
1929        return {type => 'character', data => '&'.$value};        return {type => CHARACTER_TOKEN, data => '&'.$value};
1930      }      }
1931    } else {    } else {
1932      ## no characters are consumed      ## no characters are consumed
# Line 1806  sub _construct_tree ($) { Line 1968  sub _construct_tree ($) {
1968        
1969    !!!next-token;    !!!next-token;
1970    
1971    $self->{insertion_mode} = 'before head';    $self->{insertion_mode} = BEFORE_HEAD_IM;
1972    undef $self->{form_element};    undef $self->{form_element};
1973    undef $self->{head_element};    undef $self->{head_element};
1974    $self->{open_elements} = [];    $self->{open_elements} = [];
# Line 1820  sub _construct_tree ($) { Line 1982  sub _construct_tree ($) {
1982  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
1983    my $self = shift;    my $self = shift;
1984    INITIAL: {    INITIAL: {
1985      if ($token->{type} eq 'DOCTYPE') {      if ($token->{type} == DOCTYPE_TOKEN) {
1986        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
1987        ## error, switch to a conformance checking mode for another        ## error, switch to a conformance checking mode for another
1988        ## language.        ## language.
# Line 1947  sub _tree_construction_initial ($) { Line 2109  sub _tree_construction_initial ($) {
2109        !!!next-token;        !!!next-token;
2110        return;        return;
2111      } elsif ({      } elsif ({
2112                'start tag' => 1,                START_TAG_TOKEN, 1,
2113                'end tag' => 1,                END_TAG_TOKEN, 1,
2114                'end-of-file' => 1,                END_OF_FILE_TOKEN, 1,
2115               }->{$token->{type}}) {               }->{$token->{type}}) {
2116        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE');
2117        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
2118        ## Go to the root element phase        ## Go to the root element phase
2119        ## reprocess        ## reprocess
2120        return;        return;
2121      } elsif ($token->{type} eq 'character') {      } elsif ($token->{type} == CHARACTER_TOKEN) {
2122        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2123          ## Ignore the token          ## Ignore the token
2124    
# Line 1972  sub _tree_construction_initial ($) { Line 2134  sub _tree_construction_initial ($) {
2134        ## Go to the root element phase        ## Go to the root element phase
2135        ## reprocess        ## reprocess
2136        return;        return;
2137      } elsif ($token->{type} eq 'comment') {      } elsif ($token->{type} == COMMENT_TOKEN) {
2138        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
2139        $self->{document}->append_child ($comment);        $self->{document}->append_child ($comment);
2140                
# Line 1980  sub _tree_construction_initial ($) { Line 2142  sub _tree_construction_initial ($) {
2142        !!!next-token;        !!!next-token;
2143        redo INITIAL;        redo INITIAL;
2144      } else {      } else {
2145        die "$0: $token->{type}: Unknown token";        die "$0: $token->{type}: Unknown token type";
2146      }      }
2147    } # INITIAL    } # INITIAL
2148  } # _tree_construction_initial  } # _tree_construction_initial
# Line 1989  sub _tree_construction_root_element ($) Line 2151  sub _tree_construction_root_element ($)
2151    my $self = shift;    my $self = shift;
2152        
2153    B: {    B: {
2154        if ($token->{type} eq 'DOCTYPE') {        if ($token->{type} == DOCTYPE_TOKEN) {
2155          !!!parse-error (type => 'in html:#DOCTYPE');          !!!parse-error (type => 'in html:#DOCTYPE');
2156          ## Ignore the token          ## Ignore the token
2157          ## Stay in the phase          ## Stay in the phase
2158          !!!next-token;          !!!next-token;
2159          redo B;          redo B;
2160        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{type} == COMMENT_TOKEN) {
2161          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
2162          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
2163          ## Stay in the phase          ## Stay in the phase
2164          !!!next-token;          !!!next-token;
2165          redo B;          redo B;
2166        } elsif ($token->{type} eq 'character') {        } elsif ($token->{type} == CHARACTER_TOKEN) {
2167          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2168            ## Ignore the token.            ## Ignore the token.
2169    
# Line 2011  sub _tree_construction_root_element ($) Line 2173  sub _tree_construction_root_element ($)
2173              redo B;              redo B;
2174            }            }
2175          }          }
2176    
2177            $self->{application_cache_selection}->(undef);
2178    
2179            #
2180          } elsif ($token->{type} == START_TAG_TOKEN) {
2181            if ($token->{tag_name} eq 'html' and
2182                $token->{attributes}->{manifest}) { ## ISSUE: Spec spells as "application"
2183              $self->{application_cache_selection}
2184                   ->($token->{attributes}->{manifest}->{value});
2185              ## ISSUE: No relative reference resolution?
2186            } else {
2187              $self->{application_cache_selection}->(undef);
2188            }
2189    
2190            ## ISSUE: There is an issue in the spec
2191          #          #
2192        } elsif ({        } elsif ({
2193                  'start tag' => 1,                  END_TAG_TOKEN, 1,
2194                  'end tag' => 1,                  END_OF_FILE_TOKEN, 1,
                 'end-of-file' => 1,  
2195                 }->{$token->{type}}) {                 }->{$token->{type}}) {
2196            $self->{application_cache_selection}->(undef);
2197    
2198          ## ISSUE: There is an issue in the spec          ## ISSUE: There is an issue in the spec
2199          #          #
2200        } else {        } else {
2201          die "$0: $token->{type}: Unknown token";          die "$0: $token->{type}: Unknown token type";
2202        }        }
2203    
2204        my $root_element; !!!create-element ($root_element, 'html');        my $root_element; !!!create-element ($root_element, 'html');
2205        $self->{document}->append_child ($root_element);        $self->{document}->append_child ($root_element);
2206        push @{$self->{open_elements}}, [$root_element, 'html'];        push @{$self->{open_elements}}, [$root_element, 'html'];
# Line 2062  sub _reset_insertion_mode ($) { Line 2241  sub _reset_insertion_mode ($) {
2241            
2242        ## Step 4..13        ## Step 4..13
2243        my $new_mode = {        my $new_mode = {
2244                        select => 'in select',                        select => IN_SELECT_IM,
2245                        td => 'in cell',                        td => IN_CELL_IM,
2246                        th => 'in cell',                        th => IN_CELL_IM,
2247                        tr => 'in row',                        tr => IN_ROW_IM,
2248                        tbody => 'in table body',                        tbody => IN_TABLE_BODY_IM,
2249                        thead => 'in table body',                        thead => IN_TABLE_BODY_IM,
2250                        tfoot => 'in table body',                        tfoot => IN_TABLE_BODY_IM,
2251                        caption => 'in caption',                        caption => IN_CAPTION_IM,
2252                        colgroup => 'in column group',                        colgroup => IN_COLUMN_GROUP_IM,
2253                        table => 'in table',                        table => IN_TABLE_IM,
2254                        head => 'in body', # not in head!                        head => IN_BODY_IM, # not in head!
2255                        body => 'in body',                        body => IN_BODY_IM,
2256                        frameset => 'in frameset',                        frameset => IN_FRAMESET_IM,
2257                       }->{$node->[1]};                       }->{$node->[1]};
2258        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
2259                
2260        ## Step 14        ## Step 14
2261        if ($node->[1] eq 'html') {        if ($node->[1] eq 'html') {
2262          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
2263            $self->{insertion_mode} = 'before head';            $self->{insertion_mode} = BEFORE_HEAD_IM;
2264          } else {          } else {
2265            $self->{insertion_mode} = 'after head';            $self->{insertion_mode} = AFTER_HEAD_IM;
2266          }          }
2267          return;          return;
2268        }        }
2269                
2270        ## Step 15        ## Step 15
2271        $self->{insertion_mode} = 'in body' and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
2272                
2273        ## Step 16        ## Step 16
2274        $i--;        $i--;
# Line 2203  sub _tree_construction_main ($) { Line 2382  sub _tree_construction_main ($) {
2382      ## Step 4      ## Step 4
2383      my $text = '';      my $text = '';
2384      !!!next-token;      !!!next-token;
2385      while ($token->{type} eq 'character') { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
2386        $text .= $token->{data};        $text .= $token->{data};
2387        !!!next-token;        !!!next-token;
2388      }      }
# Line 2218  sub _tree_construction_main ($) { Line 2397  sub _tree_construction_main ($) {
2397      $self->{content_model} = PCDATA_CONTENT_MODEL;      $self->{content_model} = PCDATA_CONTENT_MODEL;
2398    
2399      ## Step 7      ## Step 7
2400      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) {
2401        ## Ignore the token        ## Ignore the token
2402      } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {      } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {
2403        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!parse-error (type => 'in CDATA:#'.$token->{type});
# Line 2241  sub _tree_construction_main ($) { Line 2420  sub _tree_construction_main ($) {
2420            
2421      my $text = '';      my $text = '';
2422      !!!next-token;      !!!next-token;
2423      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) {
2424        $text .= $token->{data};        $text .= $token->{data};
2425        !!!next-token;        !!!next-token;
2426      } # 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 2430  sub _tree_construction_main ($) {
2430                                
2431      $self->{content_model} = PCDATA_CONTENT_MODEL;      $self->{content_model} = PCDATA_CONTENT_MODEL;
2432    
2433      if ($token->{type} eq 'end tag' and      if ($token->{type} == END_TAG_TOKEN and
2434          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
2435        ## Ignore the token        ## Ignore the token
2436      } else {      } else {
# Line 2497  sub _tree_construction_main ($) { Line 2676  sub _tree_construction_main ($) {
2676    my $insert;    my $insert;
2677    
2678    B: {    B: {
2679      if ($token->{type} eq 'DOCTYPE') {      if ($token->{type} == DOCTYPE_TOKEN) {
2680        !!!parse-error (type => 'DOCTYPE in the middle');        !!!parse-error (type => 'DOCTYPE in the middle');
2681        ## Ignore the token        ## Ignore the token
2682        ## Stay in the phase        ## Stay in the phase
2683        !!!next-token;        !!!next-token;
2684        redo B;        redo B;
2685      } elsif ($token->{type} eq 'end-of-file') {      } elsif ($token->{type} == END_OF_FILE_TOKEN) {
2686        if ($self->{insertion_mode} eq 'after html body' or        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
           $self->{insertion_mode} eq 'after html frameset') {  
2687          #          #
2688        } else {        } else {
2689          ## Generate implied end tags          ## Generate implied end tags
# Line 2514  sub _tree_construction_main ($) { Line 2692  sub _tree_construction_main ($) {
2692               tbody => 1, tfoot=> 1, thead => 1,               tbody => 1, tfoot=> 1, thead => 1,
2693              }->{$self->{open_elements}->[-1]->[1]}) {              }->{$self->{open_elements}->[-1]->[1]}) {
2694            !!!back-token;            !!!back-token;
2695            $token = {type => 'end tag', tag_name => $self->{open_elements}->[-1]->[1]};            $token = {type => END_TAG_TOKEN, tag_name => $self->{open_elements}->[-1]->[1]};
2696            redo B;            redo B;
2697          }          }
2698                    
# Line 2532  sub _tree_construction_main ($) { Line 2710  sub _tree_construction_main ($) {
2710    
2711        ## Stop parsing        ## Stop parsing
2712        last B;        last B;
2713      } elsif ($token->{type} eq 'start tag' and      } elsif ($token->{type} == START_TAG_TOKEN and
2714               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
2715        if ($self->{insertion_mode} eq 'after html body') {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
2716          ## Turn into the main phase          ## Turn into the main phase
2717          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html');
2718          $self->{insertion_mode} = 'after body';          $self->{insertion_mode} = AFTER_BODY_IM;
2719        } elsif ($self->{insertion_mode} eq 'after html frameset') {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
2720          ## Turn into the main phase          ## Turn into the main phase
2721          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html');
2722          $self->{insertion_mode} = 'after frameset';          $self->{insertion_mode} = AFTER_FRAMESET_IM;
2723        }        }
2724    
2725  ## ISSUE: "aa<html>" is not a parse error.  ## ISSUE: "aa<html>" is not a parse error.
# Line 2559  sub _tree_construction_main ($) { Line 2737  sub _tree_construction_main ($) {
2737        }        }
2738        !!!next-token;        !!!next-token;
2739        redo B;        redo B;
2740      } elsif ($token->{type} eq 'comment') {      } elsif ($token->{type} == COMMENT_TOKEN) {
2741        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
2742        if ($self->{insertion_mode} eq 'after html body' or        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
           $self->{insertion_mode} eq 'after html frameset') {  
2743          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
2744        } elsif ($self->{insertion_mode} eq 'after body') {        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
2745          $self->{open_elements}->[0]->[0]->append_child ($comment);          $self->{open_elements}->[0]->[0]->append_child ($comment);
2746        } else {        } else {
2747          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
2748        }        }
2749        !!!next-token;        !!!next-token;
2750        redo B;        redo B;
2751      } elsif ($self->{insertion_mode} eq 'in head' or      } elsif ($self->{insertion_mode} & HEAD_IMS) {
2752               $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') {  
2753          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
2754            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
2755            unless (length $token->{data}) {            unless (length $token->{data}) {
# Line 2584  sub _tree_construction_main ($) { Line 2758  sub _tree_construction_main ($) {
2758            }            }
2759          }          }
2760    
2761          if ($self->{insertion_mode} eq 'before head') {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2762            ## As if <head>            ## As if <head>
2763            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, 'head');
2764            $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 2768  sub _tree_construction_main ($) {
2768            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
2769    
2770            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
2771          } elsif ($self->{insertion_mode} eq 'in head noscript') {          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2772            ## As if </noscript>            ## As if </noscript>
2773            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
2774            !!!parse-error (type => 'in noscript:#character');            !!!parse-error (type => 'in noscript:#character');
# Line 2604  sub _tree_construction_main ($) { Line 2778  sub _tree_construction_main ($) {
2778            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
2779    
2780            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
2781          } elsif ($self->{insertion_mode} eq 'in head') {          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2782            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
2783    
2784            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
# Line 2613  sub _tree_construction_main ($) { Line 2787  sub _tree_construction_main ($) {
2787              ## "after head" insertion mode              ## "after head" insertion mode
2788              ## As if <body>              ## As if <body>
2789              !!!insert-element ('body');              !!!insert-element ('body');
2790              $self->{insertion_mode} = 'in body';              $self->{insertion_mode} = IN_BODY_IM;
2791              ## reprocess              ## reprocess
2792              redo B;              redo B;
2793            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} == START_TAG_TOKEN) {
2794              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
2795                if ($self->{insertion_mode} eq 'before head') {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2796                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});
2797                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2798                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];
2799                  $self->{insertion_mode} = 'in head';                  $self->{insertion_mode} = IN_HEAD_IM;
2800                  !!!next-token;                  !!!next-token;
2801                  redo B;                  redo B;
2802                } elsif ($self->{insertion_mode} ne 'after head') {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2803                    #
2804                  } else {
2805                  !!!parse-error (type => 'in head:head'); # or in head noscript                  !!!parse-error (type => 'in head:head'); # or in head noscript
2806                  ## Ignore the token                  ## Ignore the token
2807                  !!!next-token;                  !!!next-token;
2808                  redo B;                  redo B;
               } else {  
                 #  
2809                }                }
2810              } elsif ($self->{insertion_mode} eq 'before head') {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2811                ## As if <head>                ## As if <head>
2812                !!!create-element ($self->{head_element}, 'head');                !!!create-element ($self->{head_element}, 'head');
2813                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2814                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2815    
2816                $self->{insertion_mode} = 'in head';                $self->{insertion_mode} = IN_HEAD_IM;
2817                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
2818              }              }
2819    
2820              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
2821                if ($self->{insertion_mode} eq 'in head noscript') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2822                  ## As if </noscript>                  ## As if </noscript>
2823                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
2824                  !!!parse-error (type => 'in noscript:base');                  !!!parse-error (type => 'in noscript:base');
2825                                
2826                  $self->{insertion_mode} = 'in head';                  $self->{insertion_mode} = IN_HEAD_IM;
2827                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
2828                }                }
2829    
2830                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
2831                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2832                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2833                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2834                }                }
2835                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
2836                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2837                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2838                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2839                !!!next-token;                !!!next-token;
2840                redo B;                redo B;
2841              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
2842                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
2843                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2844                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2845                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2846                }                }
2847                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
2848                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2849                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2850                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2851                !!!next-token;                !!!next-token;
2852                redo B;                redo B;
2853              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
2854                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
2855                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2856                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2857                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2858                }                }
# Line 2686  sub _tree_construction_main ($) { Line 2860  sub _tree_construction_main ($) {
2860                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2861    
2862                unless ($self->{confident}) {                unless ($self->{confident}) {
                 my $charset;  
2863                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) { ## TODO: And if supported
2864                    $charset = $token->{attributes}->{charset}->{value};                    $self->{change_encoding}
2865                  }                        ->($self, $token->{attributes}->{charset}->{value});
2866                  if ($token->{attributes}->{'http-equiv'}) {                  } elsif ($token->{attributes}->{content}) {
2867                    ## 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.
2868                    if ($token->{attributes}->{'http-equiv'}->{value}                    if ($token->{attributes}->{content}->{value}
2869                        =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=                        =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=
2870                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
2871                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
2872                      $charset = defined $1 ? $1 : defined $2 ? $2 : $3;                      $self->{change_encoding}
2873                    } ## TODO: And if supported                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);
2874                      }
2875                  }                  }
                 ## TODO: Change the encoding  
2876                }                }
2877    
               ## TODO: Extracting |charset| from |meta|.  
2878                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2879                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2880                !!!next-token;                !!!next-token;
2881                redo B;                redo B;
2882              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
2883                if ($self->{insertion_mode} eq 'in head noscript') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2884                  ## As if </noscript>                  ## As if </noscript>
2885                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
2886                  !!!parse-error (type => 'in noscript:title');                  !!!parse-error (type => 'in noscript:title');
2887                                
2888                  $self->{insertion_mode} = 'in head';                  $self->{insertion_mode} = IN_HEAD_IM;
2889                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
2890                } elsif ($self->{insertion_mode} eq 'after head') {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2891                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2892                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2893                }                }
# Line 2726  sub _tree_construction_main ($) { Line 2898  sub _tree_construction_main ($) {
2898                $parse_rcdata->(RCDATA_CONTENT_MODEL,                $parse_rcdata->(RCDATA_CONTENT_MODEL,
2899                                sub { $parent->append_child ($_[0]) });                                sub { $parent->append_child ($_[0]) });
2900                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2901                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2902                redo B;                redo B;
2903              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style') {
2904                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
2905                ## insertion mode 'in head')                ## insertion mode IN_HEAD_IM)
2906                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
2907                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2908                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2909                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2910                }                }
2911                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
2912                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2913                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2914                redo B;                redo B;
2915              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
2916                if ($self->{insertion_mode} eq 'in head') {                if ($self->{insertion_mode} == IN_HEAD_IM) {
2917                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
2918                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2919                  $self->{insertion_mode} = 'in head noscript';                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
2920                  !!!next-token;                  !!!next-token;
2921                  redo B;                  redo B;
2922                } elsif ($self->{insertion_mode} eq 'in head noscript') {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2923                  !!!parse-error (type => 'in noscript:noscript');                  !!!parse-error (type => 'in noscript:noscript');
2924                  ## Ignore the token                  ## Ignore the token
2925                  !!!next-token;                  !!!next-token;
# Line 2756  sub _tree_construction_main ($) { Line 2928  sub _tree_construction_main ($) {
2928                  #                  #
2929                }                }
2930              } elsif ($token->{tag_name} eq 'script') {              } elsif ($token->{tag_name} eq 'script') {
2931                if ($self->{insertion_mode} eq 'in head noscript') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2932                  ## As if </noscript>                  ## As if </noscript>
2933                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
2934                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:script');
2935                                
2936                  $self->{insertion_mode} = 'in head';                  $self->{insertion_mode} = IN_HEAD_IM;
2937                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
2938                } elsif ($self->{insertion_mode} eq 'after head') {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2939                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2940                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2941                }                }
# Line 2771  sub _tree_construction_main ($) { Line 2943  sub _tree_construction_main ($) {
2943                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
2944                $script_start_tag->($insert_to_current);                $script_start_tag->($insert_to_current);
2945                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2946                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2947                redo B;                redo B;
2948              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
2949                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
2950                if ($self->{insertion_mode} eq 'in head noscript') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2951                  ## As if </noscript>                  ## As if </noscript>
2952                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
2953                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});
# Line 2785  sub _tree_construction_main ($) { Line 2957  sub _tree_construction_main ($) {
2957                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
2958                                    
2959                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
2960                } elsif ($self->{insertion_mode} eq 'in head') {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2961                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
2962                                    
2963                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
# Line 2793  sub _tree_construction_main ($) { Line 2965  sub _tree_construction_main ($) {
2965    
2966                ## "after head" insertion mode                ## "after head" insertion mode
2967                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
2968                $self->{insertion_mode} = 'in '.$token->{tag_name};                if ($token->{tag_name} eq 'body') {
2969                    $self->{insertion_mode} = IN_BODY_IM;
2970                  } elsif ($token->{tag_name} eq 'frameset') {
2971                    $self->{insertion_mode} = IN_FRAMESET_IM;
2972                  } else {
2973                    die "$0: tag name: $self->{tag_name}";
2974                  }
2975                !!!next-token;                !!!next-token;
2976                redo B;                redo B;
2977              } else {              } else {
2978                #                #
2979              }              }
2980    
2981              if ($self->{insertion_mode} eq 'in head noscript') {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2982                ## As if </noscript>                ## As if </noscript>
2983                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
2984                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
# Line 2810  sub _tree_construction_main ($) { Line 2988  sub _tree_construction_main ($) {
2988                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
2989    
2990                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
2991              } elsif ($self->{insertion_mode} eq 'in head') {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2992                ## As if </head>                ## As if </head>
2993                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
2994    
# Line 2820  sub _tree_construction_main ($) { Line 2998  sub _tree_construction_main ($) {
2998              ## "after head" insertion mode              ## "after head" insertion mode
2999              ## As if <body>              ## As if <body>
3000              !!!insert-element ('body');              !!!insert-element ('body');
3001              $self->{insertion_mode} = 'in body';              $self->{insertion_mode} = IN_BODY_IM;
3002              ## reprocess              ## reprocess
3003              redo B;              redo B;
3004            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
3005              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
3006                if ($self->{insertion_mode} eq 'before head') {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3007                  ## As if <head>                  ## As if <head>
3008                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, 'head');
3009                  $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 3011  sub _tree_construction_main ($) {
3011    
3012                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3013                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3014                  $self->{insertion_mode} = 'after head';                  $self->{insertion_mode} = AFTER_HEAD_IM;
3015                  !!!next-token;                  !!!next-token;
3016                  redo B;                  redo B;
3017                } elsif ($self->{insertion_mode} eq 'in head noscript') {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3018                  ## As if </noscript>                  ## As if </noscript>
3019                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3020                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:script');
3021                                    
3022                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3023                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3024                  $self->{insertion_mode} = 'after head';                  $self->{insertion_mode} = AFTER_HEAD_IM;
3025                  !!!next-token;                  !!!next-token;
3026                  redo B;                  redo B;
3027                } elsif ($self->{insertion_mode} eq 'in head') {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3028                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3029                  $self->{insertion_mode} = 'after head';                  $self->{insertion_mode} = AFTER_HEAD_IM;
3030                  !!!next-token;                  !!!next-token;
3031                  redo B;                  redo B;
3032                } else {                } else {
3033                  #                  #
3034                }                }
3035              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
3036                if ($self->{insertion_mode} eq 'in head noscript') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3037                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3038                  $self->{insertion_mode} = 'in head';                  $self->{insertion_mode} = IN_HEAD_IM;
3039                  !!!next-token;                  !!!next-token;
3040                  redo B;                  redo B;
3041                } elsif ($self->{insertion_mode} eq 'before head') {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3042                  !!!parse-error (type => 'unmatched end tag:noscript');                  !!!parse-error (type => 'unmatched end tag:noscript');
3043                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
3044                  !!!next-token;                  !!!next-token;
# Line 2871  sub _tree_construction_main ($) { Line 3049  sub _tree_construction_main ($) {
3049              } elsif ({              } elsif ({
3050                        body => 1, html => 1,                        body => 1, html => 1,
3051                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3052                if ($self->{insertion_mode} eq 'before head') {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3053                  ## As if <head>                  ## As if <head>
3054                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, 'head');
3055                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3056                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3057    
3058                  $self->{insertion_mode} = 'in head';                  $self->{insertion_mode} = IN_HEAD_IM;
3059                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3060                } elsif ($self->{insertion_mode} eq 'in head noscript') {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3061                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3062                  ## Ignore the token                  ## Ignore the token
3063                  !!!next-token;                  !!!next-token;
# Line 2890  sub _tree_construction_main ($) { Line 3068  sub _tree_construction_main ($) {
3068              } elsif ({              } elsif ({
3069                        p => 1, br => 1,                        p => 1, br => 1,
3070                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3071                if ($self->{insertion_mode} eq 'before head') {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3072                  ## As if <head>                  ## As if <head>
3073                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, 'head');
3074                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3075                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3076    
3077                  $self->{insertion_mode} = 'in head';                  $self->{insertion_mode} = IN_HEAD_IM;
3078                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3079                }                }
3080    
3081                #                #
3082              } else {              } else {
3083                if ($self->{insertion_mode} ne 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3084                    #
3085                  } else {
3086                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3087                  ## Ignore the token                  ## Ignore the token
3088                  !!!next-token;                  !!!next-token;
3089                  redo B;                  redo B;
               } else {  
                 #  
3090                }                }
3091              }              }
3092    
3093              if ($self->{insertion_mode} eq 'in head noscript') {              if ($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:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
# Line 2922  sub _tree_construction_main ($) { Line 3100  sub _tree_construction_main ($) {
3100                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3101    
3102                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
3103              } elsif ($self->{insertion_mode} eq 'in head') {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3104                ## As if </head>                ## As if </head>
3105                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3106    
3107                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
3108              } elsif ($self->{insertion_mode} eq 'before head') {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3109                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3110                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
3111                !!!next-token;                !!!next-token;
# Line 2937  sub _tree_construction_main ($) { Line 3115  sub _tree_construction_main ($) {
3115              ## "after head" insertion mode              ## "after head" insertion mode
3116              ## As if <body>              ## As if <body>
3117              !!!insert-element ('body');              !!!insert-element ('body');
3118              $self->{insertion_mode} = 'in body';              $self->{insertion_mode} = IN_BODY_IM;
3119              ## reprocess              ## reprocess
3120              redo B;              redo B;
3121            } else {            } else {
# Line 2945  sub _tree_construction_main ($) { Line 3123  sub _tree_construction_main ($) {
3123            }            }
3124    
3125            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
3126      } elsif ($self->{insertion_mode} eq 'in body' or      } elsif ($self->{insertion_mode} & BODY_IMS) {
3127               $self->{insertion_mode} eq 'in cell' or            if ($token->{type} == CHARACTER_TOKEN) {
              $self->{insertion_mode} eq 'in caption') {  
           if ($token->{type} eq 'character') {  
3128              ## NOTE: There is a code clone of "character in body".              ## NOTE: There is a code clone of "character in body".
3129              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
3130                            
# Line 2956  sub _tree_construction_main ($) { Line 3132  sub _tree_construction_main ($) {
3132    
3133              !!!next-token;              !!!next-token;
3134              redo B;              redo B;
3135            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} == START_TAG_TOKEN) {
3136              if ({              if ({
3137                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
3138                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
3139                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
3140                if ($self->{insertion_mode} eq 'in cell') {                if ($self->{insertion_mode} == IN_CELL_IM) {
3141                  ## have an element in table scope                  ## have an element in table scope
3142                  my $tn;                  my $tn;
3143                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 2984  sub _tree_construction_main ($) { Line 3160  sub _tree_construction_main ($) {
3160                                    
3161                  ## Close the cell                  ## Close the cell
3162                  !!!back-token; # <?>                  !!!back-token; # <?>
3163                  $token = {type => 'end tag', tag_name => $tn};                  $token = {type => END_TAG_TOKEN, tag_name => $tn};
3164                  redo B;                  redo B;
3165                } elsif ($self->{insertion_mode} eq 'in caption') {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3166                  !!!parse-error (type => 'not closed:caption');                  !!!parse-error (type => 'not closed:caption');
3167                                    
3168                  ## As if </caption>                  ## As if </caption>
# Line 3017  sub _tree_construction_main ($) { Line 3193  sub _tree_construction_main ($) {
3193                       tbody => 1, tfoot=> 1, thead => 1,                       tbody => 1, tfoot=> 1, thead => 1,
3194                      }->{$self->{open_elements}->[-1]->[1]}) {                      }->{$self->{open_elements}->[-1]->[1]}) {
3195                    !!!back-token; # <?>                    !!!back-token; # <?>
3196                    $token = {type => 'end tag', tag_name => 'caption'};                    $token = {type => END_TAG_TOKEN, tag_name => 'caption'};
3197                    !!!back-token;                    !!!back-token;
3198                    $token = {type => 'end tag',                    $token = {type => END_TAG_TOKEN,
3199                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3200                    redo B;                    redo B;
3201                  }                  }
# Line 3032  sub _tree_construction_main ($) { Line 3208  sub _tree_construction_main ($) {
3208                                    
3209                  $clear_up_to_marker->();                  $clear_up_to_marker->();
3210                                    
3211                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
3212                                    
3213                  ## reprocess                  ## reprocess
3214                  redo B;                  redo B;
# Line 3042  sub _tree_construction_main ($) { Line 3218  sub _tree_construction_main ($) {
3218              } else {              } else {
3219                #                #
3220              }              }
3221            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
3222              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
3223                if ($self->{insertion_mode} eq 'in cell') {                if ($self->{insertion_mode} == IN_CELL_IM) {
3224                  ## have an element in table scope                  ## have an element in table scope
3225                  my $i;                  my $i;
3226                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 3074  sub _tree_construction_main ($) { Line 3250  sub _tree_construction_main ($) {
3250                       tbody => 1, tfoot=> 1, thead => 1,                       tbody => 1, tfoot=> 1, thead => 1,
3251                      }->{$self->{open_elements}->[-1]->[1]}) {                      }->{$self->{open_elements}->[-1]->[1]}) {
3252                    !!!back-token;                    !!!back-token;
3253                    $token = {type => 'end tag',                    $token = {type => END_TAG_TOKEN,
3254                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3255                    redo B;                    redo B;
3256                  }                  }
# Line 3087  sub _tree_construction_main ($) { Line 3263  sub _tree_construction_main ($) {
3263                                    
3264                  $clear_up_to_marker->();                  $clear_up_to_marker->();
3265                                    
3266                  $self->{insertion_mode} = 'in row';                  $self->{insertion_mode} = IN_ROW_IM;
3267                                    
3268                  !!!next-token;                  !!!next-token;
3269                  redo B;                  redo B;
3270                } elsif ($self->{insertion_mode} eq 'in caption') {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3271                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3272                  ## Ignore the token                  ## Ignore the token
3273                  !!!next-token;                  !!!next-token;
# Line 3100  sub _tree_construction_main ($) { Line 3276  sub _tree_construction_main ($) {
3276                  #                  #
3277                }                }
3278              } elsif ($token->{tag_name} eq 'caption') {              } elsif ($token->{tag_name} eq 'caption') {
3279                if ($self->{insertion_mode} eq 'in caption') {                if ($self->{insertion_mode} == IN_CAPTION_IM) {
3280                  ## have a table element in table scope                  ## have a table element in table scope
3281                  my $i;                  my $i;
3282                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 3128  sub _tree_construction_main ($) { Line 3304  sub _tree_construction_main ($) {
3304                       tbody => 1, tfoot=> 1, thead => 1,                       tbody => 1, tfoot=> 1, thead => 1,
3305                      }->{$self->{open_elements}->[-1]->[1]}) {                      }->{$self->{open_elements}->[-1]->[1]}) {
3306                    !!!back-token;                    !!!back-token;
3307                    $token = {type => 'end tag',                    $token = {type => END_TAG_TOKEN,
3308                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3309                    redo B;                    redo B;
3310                  }                  }
# Line 3141  sub _tree_construction_main ($) { Line 3317  sub _tree_construction_main ($) {
3317                                    
3318                  $clear_up_to_marker->();                  $clear_up_to_marker->();
3319                                    
3320                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
3321                                    
3322                  !!!next-token;                  !!!next-token;
3323                  redo B;                  redo B;
3324                } elsif ($self->{insertion_mode} eq 'in cell') {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
3325                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3326                  ## Ignore the token                  ## Ignore the token
3327                  !!!next-token;                  !!!next-token;
# Line 3157  sub _tree_construction_main ($) { Line 3333  sub _tree_construction_main ($) {
3333                        table => 1, tbody => 1, tfoot => 1,                        table => 1, tbody => 1, tfoot => 1,
3334                        thead => 1, tr => 1,                        thead => 1, tr => 1,
3335                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
3336                       $self->{insertion_mode} eq 'in cell') {                       $self->{insertion_mode} == IN_CELL_IM) {
3337                ## have an element in table scope                ## have an element in table scope
3338                my $i;                my $i;
3339                my $tn;                my $tn;
# Line 3185  sub _tree_construction_main ($) { Line 3361  sub _tree_construction_main ($) {
3361    
3362                ## Close the cell                ## Close the cell
3363                !!!back-token; # </?>                !!!back-token; # </?>
3364                $token = {type => 'end tag', tag_name => $tn};                $token = {type => END_TAG_TOKEN, tag_name => $tn};
3365                redo B;                redo B;
3366              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
3367                       $self->{insertion_mode} eq 'in caption') {                       $self->{insertion_mode} == IN_CAPTION_IM) {
3368                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption');
3369    
3370                ## As if </caption>                ## As if </caption>
# Line 3219  sub _tree_construction_main ($) { Line 3395  sub _tree_construction_main ($) {
3395                     tbody => 1, tfoot=> 1, thead => 1,                     tbody => 1, tfoot=> 1, thead => 1,
3396                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
3397                  !!!back-token; # </table>                  !!!back-token; # </table>
3398                  $token = {type => 'end tag', tag_name => 'caption'};                  $token = {type => END_TAG_TOKEN, tag_name => 'caption'};
3399                  !!!back-token;                  !!!back-token;
3400                  $token = {type => 'end tag',                  $token = {type => END_TAG_TOKEN,
3401                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3402                  redo B;                  redo B;
3403                }                }
# Line 3234  sub _tree_construction_main ($) { Line 3410  sub _tree_construction_main ($) {
3410    
3411                $clear_up_to_marker->();                $clear_up_to_marker->();
3412    
3413                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
3414    
3415                ## reprocess                ## reprocess
3416                redo B;                redo B;
3417              } elsif ({              } elsif ({
3418                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
3419                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3420                if ($self->{insertion_mode} eq 'in cell' or                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
                   $self->{insertion_mode} eq 'in caption') {  
3421                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3422                  ## Ignore the token                  ## Ignore the token
3423                  !!!next-token;                  !!!next-token;
# Line 3254  sub _tree_construction_main ($) { Line 3429  sub _tree_construction_main ($) {
3429                        tbody => 1, tfoot => 1,                        tbody => 1, tfoot => 1,
3430                        thead => 1, tr => 1,                        thead => 1, tr => 1,
3431                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
3432                       $self->{insertion_mode} eq 'in caption') {                       $self->{insertion_mode} == IN_CAPTION_IM) {
3433                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3434                ## Ignore the token                ## Ignore the token
3435                !!!next-token;                !!!next-token;
# Line 3268  sub _tree_construction_main ($) { Line 3443  sub _tree_construction_main ($) {
3443    
3444        $insert = $insert_to_current;        $insert = $insert_to_current;
3445        #        #
3446      } elsif ($self->{insertion_mode} eq 'in row' or      } elsif ($self->{insertion_mode} & TABLE_IMS) {
3447               $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.  
3448              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3449                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3450                                
# Line 3329  sub _tree_construction_main ($) { Line 3501  sub _tree_construction_main ($) {
3501                            
3502              !!!next-token;              !!!next-token;
3503              redo B;              redo B;
3504            } elsif ($token->{type} eq 'start tag') {        } elsif ($token->{type} == START_TAG_TOKEN) {
3505              if ({              if ({
3506                   tr => ($self->{insertion_mode} ne 'in row'),                   tr => ($self->{insertion_mode} != IN_ROW_IM),
3507                   th => 1, td => 1,                   th => 1, td => 1,
3508                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
3509                if ($self->{insertion_mode} eq 'in table') {                if ($self->{insertion_mode} == IN_TABLE_IM) {
3510                  ## Clear back to table context                  ## Clear back to table context
3511                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while ($self->{open_elements}->[-1]->[1] ne 'table' and
3512                         $self->{open_elements}->[-1]->[1] ne 'html') {                         $self->{open_elements}->[-1]->[1] ne 'html') {
# Line 3343  sub _tree_construction_main ($) { Line 3515  sub _tree_construction_main ($) {
3515                  }                  }
3516                                    
3517                  !!!insert-element ('tbody');                  !!!insert-element ('tbody');
3518                  $self->{insertion_mode} = 'in table body';                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
3519                  ## reprocess in the "in table body" insertion mode...                  ## reprocess in the "in table body" insertion mode...
3520                }                }
3521    
3522                if ($self->{insertion_mode} eq 'in table body') {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3523                  unless ($token->{tag_name} eq 'tr') {                  unless ($token->{tag_name} eq 'tr') {
3524                    !!!parse-error (type => 'missing start tag:tr');                    !!!parse-error (type => 'missing start tag:tr');
3525                  }                  }
# Line 3360  sub _tree_construction_main ($) { Line 3532  sub _tree_construction_main ($) {
3532                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
3533                  }                  }
3534                                    
3535                  $self->{insertion_mode} = 'in row';                  $self->{insertion_mode} = IN_ROW_IM;
3536                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
3537                    !!!insert-element ($token->{tag_name}, $token->{attributes});                    !!!insert-element ($token->{tag_name}, $token->{attributes});
3538                    !!!next-token;                    !!!next-token;
# Line 3380  sub _tree_construction_main ($) { Line 3552  sub _tree_construction_main ($) {
3552                }                }
3553                                
3554                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
3555                $self->{insertion_mode} = 'in cell';                $self->{insertion_mode} = IN_CELL_IM;
3556    
3557                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
3558                                
# Line 3389  sub _tree_construction_main ($) { Line 3561  sub _tree_construction_main ($) {
3561              } elsif ({              } elsif ({
3562                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
3563                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
3564                        tr => 1, # $self->{insertion_mode} eq 'in row'                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
3565                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3566                if ($self->{insertion_mode} eq 'in row') {                if ($self->{insertion_mode} == IN_ROW_IM) {
3567                  ## As if </tr>                  ## As if </tr>
3568                  ## have an element in table scope                  ## have an element in table scope
3569                  my $i;                  my $i;
# Line 3422  sub _tree_construction_main ($) { Line 3594  sub _tree_construction_main ($) {
3594                  }                  }
3595                                    
3596                  pop @{$self->{open_elements}}; # tr                  pop @{$self->{open_elements}}; # tr
3597                  $self->{insertion_mode} = 'in table body';                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
3598                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
3599                    ## reprocess                    ## reprocess
3600                    redo B;                    redo B;
# Line 3431  sub _tree_construction_main ($) { Line 3603  sub _tree_construction_main ($) {
3603                  }                  }
3604                }                }
3605    
3606                if ($self->{insertion_mode} eq 'in table body') {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3607                  ## have an element in table scope                  ## have an element in table scope
3608                  my $i;                  my $i;
3609                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 3470  sub _tree_construction_main ($) { Line 3642  sub _tree_construction_main ($) {
3642                  ## nop by definition                  ## nop by definition
3643                                    
3644                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3645                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
3646                  ## reprocess in "in table" insertion mode...                  ## reprocess in "in table" insertion mode...
3647                }                }
3648    
# Line 3483  sub _tree_construction_main ($) { Line 3655  sub _tree_construction_main ($) {
3655                  }                  }
3656                                    
3657                  !!!insert-element ('colgroup');                  !!!insert-element ('colgroup');
3658                  $self->{insertion_mode} = 'in column group';                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
3659                  ## reprocess                  ## reprocess
3660                  redo B;                  redo B;
3661                } elsif ({                } elsif ({
# Line 3503  sub _tree_construction_main ($) { Line 3675  sub _tree_construction_main ($) {
3675                                    
3676                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3677                  $self->{insertion_mode} = {                  $self->{insertion_mode} = {
3678                                             caption => 'in caption',                                             caption => IN_CAPTION_IM,
3679                                             colgroup => 'in column group',                                             colgroup => IN_COLUMN_GROUP_IM,
3680                                             tbody => 'in table body',                                             tbody => IN_TABLE_BODY_IM,
3681                                             tfoot => 'in table body',                                             tfoot => IN_TABLE_BODY_IM,
3682                                             thead => 'in table body',                                             thead => IN_TABLE_BODY_IM,
3683                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
3684                  !!!next-token;                  !!!next-token;
3685                  redo B;                  redo B;
# Line 3515  sub _tree_construction_main ($) { Line 3687  sub _tree_construction_main ($) {
3687                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
3688                }                }
3689              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
               ## NOTE: There are code clones for this "table in table"  
3690                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3691    
3692                ## As if </table>                ## As if </table>
# Line 3546  sub _tree_construction_main ($) { Line 3717  sub _tree_construction_main ($) {
3717                     tbody => 1, tfoot=> 1, thead => 1,                     tbody => 1, tfoot=> 1, thead => 1,
3718                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
3719                  !!!back-token; # <table>                  !!!back-token; # <table>
3720                  $token = {type => 'end tag', tag_name => 'table'};                  $token = {type => END_TAG_TOKEN, tag_name => 'table'};
3721                  !!!back-token;                  !!!back-token;
3722                  $token = {type => 'end tag',                  $token = {type => END_TAG_TOKEN,
3723                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3724                  redo B;                  redo B;
3725                }                }
# Line 3563  sub _tree_construction_main ($) { Line 3734  sub _tree_construction_main ($) {
3734    
3735                ## reprocess                ## reprocess
3736                redo B;                redo B;
3737              } else {          } else {
3738                #            !!!parse-error (type => 'in table:'.$token->{tag_name});
3739              }  
3740            } elsif ($token->{type} eq 'end tag') {            $insert = $insert_to_foster;
3741              #
3742            }
3743          } elsif ($token->{type} == END_TAG_TOKEN) {
3744              if ($token->{tag_name} eq 'tr' and              if ($token->{tag_name} eq 'tr' and
3745                  $self->{insertion_mode} eq 'in row') {                  $self->{insertion_mode} == IN_ROW_IM) {
3746                ## have an element in table scope                ## have an element in table scope
3747                my $i;                my $i;
3748                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 3598  sub _tree_construction_main ($) { Line 3772  sub _tree_construction_main ($) {
3772                }                }
3773    
3774                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
3775                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_BODY_IM;
3776                !!!next-token;                !!!next-token;
3777                redo B;                redo B;
3778              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
3779                if ($self->{insertion_mode} eq 'in row') {                if ($self->{insertion_mode} == IN_ROW_IM) {
3780                  ## As if </tr>                  ## As if </tr>
3781                  ## have an element in table scope                  ## have an element in table scope
3782                  my $i;                  my $i;
# Line 3633  sub _tree_construction_main ($) { Line 3807  sub _tree_construction_main ($) {
3807                  }                  }
3808                                    
3809                  pop @{$self->{open_elements}}; # tr                  pop @{$self->{open_elements}}; # tr
3810                  $self->{insertion_mode} = 'in table body';                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
3811                  ## reprocess in the "in table body" insertion mode...                  ## reprocess in the "in table body" insertion mode...
3812                }                }
3813    
3814                if ($self->{insertion_mode} eq 'in table body') {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3815                  ## have an element in table scope                  ## have an element in table scope
3816                  my $i;                  my $i;
3817                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 3676  sub _tree_construction_main ($) { Line 3850  sub _tree_construction_main ($) {
3850                  ## nop by definition                  ## nop by definition
3851                                    
3852                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3853                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
3854                  ## reprocess in the "in table" insertion mode...                  ## reprocess in the "in table" insertion mode...
3855                }                }
3856    
# Line 3707  sub _tree_construction_main ($) { Line 3881  sub _tree_construction_main ($) {
3881                     tbody => 1, tfoot=> 1, thead => 1,                     tbody => 1, tfoot=> 1, thead => 1,
3882                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
3883                  !!!back-token;                  !!!back-token;
3884                  $token = {type => 'end tag',                  $token = {type => END_TAG_TOKEN,
3885                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3886                  redo B;                  redo B;
3887                }                }
# Line 3725  sub _tree_construction_main ($) { Line 3899  sub _tree_construction_main ($) {
3899              } elsif ({              } elsif ({
3900                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
3901                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
3902                       ($self->{insertion_mode} eq 'in row' or                       $self->{insertion_mode} & ROW_IMS) {
3903                        $self->{insertion_mode} eq 'in table body')) {                if ($self->{insertion_mode} == IN_ROW_IM) {
               if ($self->{insertion_mode} eq 'in row') {  
3904                  ## have an element in table scope                  ## have an element in table scope
3905                  my $i;                  my $i;
3906                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 3778  sub _tree_construction_main ($) { Line 3951  sub _tree_construction_main ($) {
3951                  }                  }
3952                                    
3953                  pop @{$self->{open_elements}}; # tr                  pop @{$self->{open_elements}}; # tr
3954                  $self->{insertion_mode} = 'in table body';                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
3955                  ## reprocess in the "in table body" insertion mode...                  ## reprocess in the "in table body" insertion mode...
3956                }                }
3957    
# Line 3811  sub _tree_construction_main ($) { Line 3984  sub _tree_construction_main ($) {
3984                }                }
3985    
3986                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3987                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
3988                !!!next-token;                !!!next-token;
3989                redo B;                redo B;
3990              } elsif ({              } elsif ({
3991                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
3992                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
3993                        tr => 1, # $self->{insertion_mode} eq 'in row'                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
3994                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} eq 'in table'                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
3995                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3996                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3997                ## Ignore the token                ## Ignore the token
3998                !!!next-token;                !!!next-token;
3999                redo B;                redo B;
4000              } else {          } else {
4001                #            !!!parse-error (type => 'in table:/'.$token->{tag_name});
             }  
           } else {  
             die "$0: $token->{type}: Unknown token type";  
           }  
   
       !!!parse-error (type => 'in table:'.$token->{tag_name});  
4002    
4003        $insert = $insert_to_foster;            $insert = $insert_to_foster;
4004        #            #
4005      } elsif ($self->{insertion_mode} eq 'in column group') {          }
4006            if ($token->{type} eq 'character') {        } else {
4007            die "$0: $token->{type}: Unknown token type";
4008          }
4009        } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
4010              if ($token->{type} == CHARACTER_TOKEN) {
4011              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4012                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4013                unless (length $token->{data}) {                unless (length $token->{data}) {
# Line 3846  sub _tree_construction_main ($) { Line 4017  sub _tree_construction_main ($) {
4017              }              }
4018                            
4019              #              #
4020            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} == START_TAG_TOKEN) {
4021              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
4022                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
4023                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
# Line 3855  sub _tree_construction_main ($) { Line 4026  sub _tree_construction_main ($) {
4026              } else {              } else {
4027                #                #
4028              }              }
4029            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
4030              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
4031                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] eq 'html') {
4032                  !!!parse-error (type => 'unmatched end tag:colgroup');                  !!!parse-error (type => 'unmatched end tag:colgroup');
# Line 3864  sub _tree_construction_main ($) { Line 4035  sub _tree_construction_main ($) {
4035                  redo B;                  redo B;
4036                } else {                } else {
4037                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
4038                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
4039                  !!!next-token;                  !!!next-token;
4040                  redo B;                              redo B;            
4041                }                }
# Line 3888  sub _tree_construction_main ($) { Line 4059  sub _tree_construction_main ($) {
4059              redo B;              redo B;
4060            } else {            } else {
4061              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
4062              $self->{insertion_mode} = 'in table';              $self->{insertion_mode} = IN_TABLE_IM;
4063              ## reprocess              ## reprocess
4064              redo B;              redo B;
4065            }            }
4066      } elsif ($self->{insertion_mode} eq 'in select') {      } elsif ($self->{insertion_mode} == IN_SELECT_IM) {
4067            if ($token->{type} eq 'character') {        if ($token->{type} == CHARACTER_TOKEN) {
4068              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4069              !!!next-token;          !!!next-token;
4070              redo B;          redo B;
4071            } elsif ($token->{type} eq 'start tag') {        } elsif ($token->{type} == START_TAG_TOKEN) {
4072              if ($token->{tag_name} eq 'option') {              if ($token->{tag_name} eq 'option') {
4073                if ($self->{open_elements}->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
4074                  ## As if </option>                  ## As if </option>
# Line 3950  sub _tree_construction_main ($) { Line 4121  sub _tree_construction_main ($) {
4121    
4122                !!!next-token;                !!!next-token;
4123                redo B;                redo B;
4124              } else {          } else {
4125                #            !!!parse-error (type => 'in select:'.$token->{tag_name});
4126              }            ## Ignore the token
4127            } elsif ($token->{type} eq 'end tag') {            !!!next-token;
4128              redo B;
4129            }
4130          } elsif ($token->{type} == END_TAG_TOKEN) {
4131              if ($token->{tag_name} eq 'optgroup') {              if ($token->{tag_name} eq 'optgroup') {
4132                if ($self->{open_elements}->[-1]->[1] eq 'option' and                if ($self->{open_elements}->[-1]->[1] eq 'option' and
4133                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {
# Line 4055  sub _tree_construction_main ($) { Line 4229  sub _tree_construction_main ($) {
4229    
4230                ## reprocess                ## reprocess
4231                redo B;                redo B;
4232              } else {          } else {
4233                #            !!!parse-error (type => 'in select:/'.$token->{tag_name});
             }  
           } else {  
             #  
           }  
   
           !!!parse-error (type => 'in select:'.$token->{tag_name});  
4234            ## Ignore the token            ## Ignore the token
4235            !!!next-token;            !!!next-token;
4236            redo B;            redo B;
4237      } elsif ($self->{insertion_mode} eq 'after body' or          }
4238               $self->{insertion_mode} eq 'after html body') {        } else {
4239        if ($token->{type} eq 'character') {          die "$0: $token->{type}: Unknown token type";
4240          }
4241        } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
4242          if ($token->{type} == CHARACTER_TOKEN) {
4243          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4244            my $data = $1;            my $data = $1;
4245            ## As if in body            ## As if in body
# Line 4082  sub _tree_construction_main ($) { Line 4253  sub _tree_construction_main ($) {
4253            }            }
4254          }          }
4255                    
4256          if ($self->{insertion_mode} eq 'after html body') {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4257            !!!parse-error (type => 'after html:#character');            !!!parse-error (type => 'after html:#character');
4258    
4259            ## 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 4262  sub _tree_construction_main ($) {
4262          ## "after body" insertion mode          ## "after body" insertion mode
4263          !!!parse-error (type => 'after body:#character');          !!!parse-error (type => 'after body:#character');
4264    
4265          $self->{insertion_mode} = 'in body';          $self->{insertion_mode} = IN_BODY_IM;
4266          ## reprocess          ## reprocess
4267          redo B;          redo B;
4268        } elsif ($token->{type} eq 'start tag') {        } elsif ($token->{type} == START_TAG_TOKEN) {
4269          if ($self->{insertion_mode} eq 'after html body') {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4270            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html:'.$token->{tag_name});
4271                        
4272            ## 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 4275  sub _tree_construction_main ($) {
4275          ## "after body" insertion mode          ## "after body" insertion mode
4276          !!!parse-error (type => 'after body:'.$token->{tag_name});          !!!parse-error (type => 'after body:'.$token->{tag_name});
4277    
4278          $self->{insertion_mode} = 'in body';          $self->{insertion_mode} = IN_BODY_IM;
4279          ## reprocess          ## reprocess
4280          redo B;          redo B;
4281        } elsif ($token->{type} eq 'end tag') {        } elsif ($token->{type} == END_TAG_TOKEN) {
4282          if ($self->{insertion_mode} eq 'after html body') {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4283            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!parse-error (type => 'after html:/'.$token->{tag_name});
4284                        
4285            $self->{insertion_mode} = 'after body';            $self->{insertion_mode} = AFTER_BODY_IM;
4286            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "main" phase, "after body" insertion mode...
4287          }          }
4288    
# Line 4123  sub _tree_construction_main ($) { Line 4294  sub _tree_construction_main ($) {
4294              !!!next-token;              !!!next-token;
4295              redo B;              redo B;
4296            } else {            } else {
4297              $self->{insertion_mode} = 'after html body';              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
4298              !!!next-token;              !!!next-token;
4299              redo B;              redo B;
4300            }            }
4301          } else {          } else {
4302            !!!parse-error (type => 'after body:/'.$token->{tag_name});            !!!parse-error (type => 'after body:/'.$token->{tag_name});
4303    
4304            $self->{insertion_mode} = 'in body';            $self->{insertion_mode} = IN_BODY_IM;
4305            ## reprocess            ## reprocess
4306            redo B;            redo B;
4307          }          }
4308        } else {        } else {
4309          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
4310        }        }
4311      } elsif ($self->{insertion_mode} eq 'in frameset' or      } elsif ($self->{insertion_mode} & FRAME_IMS) {
4312               $self->{insertion_mode} eq 'after frameset' or        if ($token->{type} == CHARACTER_TOKEN) {
              $self->{insertion_mode} eq 'after html frameset') {  
       if ($token->{type} eq 'character') {  
4313          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4314            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4315                        
# Line 4151  sub _tree_construction_main ($) { Line 4320  sub _tree_construction_main ($) {
4320          }          }
4321                    
4322          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
4323            if ($self->{insertion_mode} eq 'in frameset') {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4324              !!!parse-error (type => 'in frameset:#character');              !!!parse-error (type => 'in frameset:#character');
4325            } elsif ($self->{insertion_mode} eq 'after frameset') {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
4326              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#character');
4327            } else { # "after html frameset"            } else { # "after html frameset"
4328              !!!parse-error (type => 'after html:#character');              !!!parse-error (type => 'after html:#character');
4329    
4330              $self->{insertion_mode} = 'after frameset';              $self->{insertion_mode} = AFTER_FRAMESET_IM;
4331              ## Reprocess in the "main" phase, "after frameset"...              ## Reprocess in the "main" phase, "after frameset"...
4332              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#character');
4333            }            }
# Line 4173  sub _tree_construction_main ($) { Line 4342  sub _tree_construction_main ($) {
4342          }          }
4343                    
4344          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
4345        } elsif ($token->{type} eq 'start tag') {        } elsif ($token->{type} == START_TAG_TOKEN) {
4346          if ($self->{insertion_mode} eq 'after html frameset') {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4347            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html:'.$token->{tag_name});
4348    
4349            $self->{insertion_mode} = 'after frameset';            $self->{insertion_mode} = AFTER_FRAMESET_IM;
4350            ## Process in the "main" phase, "after frameset" insertion mode...            ## Process in the "main" phase, "after frameset" insertion mode...
4351          }          }
4352    
4353          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
4354              $self->{insertion_mode} eq 'in frameset') {              $self->{insertion_mode} == IN_FRAMESET_IM) {
4355            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes});
4356            !!!next-token;            !!!next-token;
4357            redo B;            redo B;
4358          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
4359                   $self->{insertion_mode} eq 'in frameset') {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
4360            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes});
4361            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4362            !!!next-token;            !!!next-token;
# Line 4197  sub _tree_construction_main ($) { Line 4366  sub _tree_construction_main ($) {
4366            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
4367            redo B;            redo B;
4368          } else {          } else {
4369            if ($self->{insertion_mode} eq 'in frameset') {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4370              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!parse-error (type => 'in frameset:'.$token->{tag_name});
4371            } else {            } else {
4372              !!!parse-error (type => 'after frameset:'.$token->{tag_name});              !!!parse-error (type => 'after frameset:'.$token->{tag_name});
# Line 4206  sub _tree_construction_main ($) { Line 4375  sub _tree_construction_main ($) {
4375            !!!next-token;            !!!next-token;
4376            redo B;            redo B;
4377          }          }
4378        } elsif ($token->{type} eq 'end tag') {        } elsif ($token->{type} == END_TAG_TOKEN) {
4379          if ($self->{insertion_mode} eq 'after html frameset') {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4380            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!parse-error (type => 'after html:/'.$token->{tag_name});
4381    
4382            $self->{insertion_mode} = 'after frameset';            $self->{insertion_mode} = AFTER_FRAMESET_IM;
4383            ## Process in the "main" phase, "after frameset" insertion mode...            ## Process in the "main" phase, "after frameset" insertion mode...
4384          }          }
4385    
4386          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
4387              $self->{insertion_mode} eq 'in frameset') {              $self->{insertion_mode} == IN_FRAMESET_IM) {
4388            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] eq 'html' and
4389                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
4390              !!!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 4397  sub _tree_construction_main ($) {
4397    
4398            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
4399                $self->{open_elements}->[-1]->[1] ne 'frameset') {                $self->{open_elements}->[-1]->[1] ne 'frameset') {
4400              $self->{insertion_mode} = 'after frameset';              $self->{insertion_mode} = AFTER_FRAMESET_IM;
4401            }            }
4402            redo B;            redo B;
4403          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
4404                   $self->{insertion_mode} eq 'after frameset') {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
4405            $self->{insertion_mode} = 'after html frameset';            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
4406            !!!next-token;            !!!next-token;
4407            redo B;            redo B;
4408          } else {          } else {
4409            if ($self->{insertion_mode} eq 'in frameset') {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4410              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});
4411            } else {            } else {
4412              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});
# Line 4256  sub _tree_construction_main ($) { Line 4425  sub _tree_construction_main ($) {
4425      }      }
4426    
4427      ## "in body" insertion mode      ## "in body" insertion mode
4428      if ($token->{type} eq 'start tag') {      if ($token->{type} == START_TAG_TOKEN) {
4429        if ($token->{tag_name} eq 'script') {        if ($token->{tag_name} eq 'script') {
4430          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
4431          $script_start_tag->($insert);          $script_start_tag->($insert);
# Line 4279  sub _tree_construction_main ($) { Line 4448  sub _tree_construction_main ($) {
4448          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4449    
4450          unless ($self->{confident}) {          unless ($self->{confident}) {
           my $charset;  
4451            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) { ## TODO: And if supported
4452              $charset = $token->{attributes}->{charset}->{value};              $self->{change_encoding}
4453            }                  ->($self, $token->{attributes}->{charset}->{value});
4454            if ($token->{attributes}->{'http-equiv'}) {            } elsif ($token->{attributes}->{content}) {
4455              ## 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.
4456              if ($token->{attributes}->{'http-equiv'}->{value}              if ($token->{attributes}->{content}->{value}
4457                  =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=                  =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=
4458                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4459                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
4460                $charset = defined $1 ? $1 : defined $2 ? $2 : $3;                $self->{change_encoding}
4461              } ## TODO: And if supported                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);
4462                }
4463            }            }
           ## TODO: Change the encoding  
4464          }          }
4465    
4466          !!!next-token;          !!!next-token;
# Line 4336  sub _tree_construction_main ($) { Line 4504  sub _tree_construction_main ($) {
4504          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4505            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4506              !!!back-token;              !!!back-token;
4507              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4508              redo B;              redo B;
4509            } elsif ({            } elsif ({
4510                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
# Line 4349  sub _tree_construction_main ($) { Line 4517  sub _tree_construction_main ($) {
4517          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4518          if ($token->{tag_name} eq 'pre') {          if ($token->{tag_name} eq 'pre') {
4519            !!!next-token;            !!!next-token;
4520            if ($token->{type} eq 'character') {            if ($token->{type} == CHARACTER_TOKEN) {
4521              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
4522              unless (length $token->{data}) {              unless (length $token->{data}) {
4523                !!!next-token;                !!!next-token;
# Line 4370  sub _tree_construction_main ($) { Line 4538  sub _tree_construction_main ($) {
4538            INSCOPE: for (reverse @{$self->{open_elements}}) {            INSCOPE: for (reverse @{$self->{open_elements}}) {
4539              if ($_->[1] eq 'p') {              if ($_->[1] eq 'p') {
4540                !!!back-token;                !!!back-token;
4541                $token = {type => 'end tag', tag_name => 'p'};                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4542                redo B;                redo B;
4543              } elsif ({              } elsif ({
4544                        table => 1, caption => 1, td => 1, th => 1,                        table => 1, caption => 1, td => 1, th => 1,
# Line 4390  sub _tree_construction_main ($) { Line 4558  sub _tree_construction_main ($) {
4558          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4559            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4560              !!!back-token;              !!!back-token;
4561              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4562              redo B;              redo B;
4563            } elsif ({            } elsif ({
4564                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
# Line 4437  sub _tree_construction_main ($) { Line 4605  sub _tree_construction_main ($) {
4605          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4606            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4607              !!!back-token;              !!!back-token;
4608              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4609              redo B;              redo B;
4610            } elsif ({            } elsif ({
4611                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
# Line 4484  sub _tree_construction_main ($) { Line 4652  sub _tree_construction_main ($) {
4652          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4653            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4654              !!!back-token;              !!!back-token;
4655              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4656              redo B;              redo B;
4657            } elsif ({            } elsif ({
4658                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
# Line 4508  sub _tree_construction_main ($) { Line 4676  sub _tree_construction_main ($) {
4676            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
4677            if ($node->[1] eq 'p') {            if ($node->[1] eq 'p') {
4678              !!!back-token;              !!!back-token;
4679              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4680              redo B;              redo B;
4681            } elsif ({            } elsif ({
4682                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
# Line 4552  sub _tree_construction_main ($) { Line 4720  sub _tree_construction_main ($) {
4720              !!!parse-error (type => 'in a:a');              !!!parse-error (type => 'in a:a');
4721                            
4722              !!!back-token;              !!!back-token;
4723              $token = {type => 'end tag', tag_name => 'a'};              $token = {type => END_TAG_TOKEN, tag_name => 'a'};
4724              $formatting_end_tag->($token->{tag_name});              $formatting_end_tag->($token->{tag_name});
4725                            
4726              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
# Line 4599  sub _tree_construction_main ($) { Line 4767  sub _tree_construction_main ($) {
4767          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4768            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
4769            if ($node->[1] eq 'nobr') {            if ($node->[1] eq 'nobr') {
4770              !!!parse-error (type => 'not closed:nobr');              !!!parse-error (type => 'in nobr:nobr');
4771              !!!back-token;              !!!back-token;
4772              $token = {type => 'end tag', tag_name => 'nobr'};              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};
4773              redo B;              redo B;
4774            } elsif ({            } elsif ({
4775                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
# Line 4623  sub _tree_construction_main ($) { Line 4791  sub _tree_construction_main ($) {
4791            if ($node->[1] eq 'button') {            if ($node->[1] eq 'button') {
4792              !!!parse-error (type => 'in button:button');              !!!parse-error (type => 'in button:button');
4793              !!!back-token;              !!!back-token;
4794              $token = {type => 'end tag', tag_name => 'button'};              $token = {type => END_TAG_TOKEN, tag_name => 'button'};
4795              redo B;              redo B;
4796            } elsif ({            } elsif ({
4797                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
# Line 4658  sub _tree_construction_main ($) { Line 4826  sub _tree_construction_main ($) {
4826          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4827            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4828              !!!back-token;              !!!back-token;
4829              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4830              redo B;              redo B;
4831            } elsif ({            } elsif ({
4832                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
# Line 4670  sub _tree_construction_main ($) { Line 4838  sub _tree_construction_main ($) {
4838                        
4839          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4840                        
4841          $self->{insertion_mode} = 'in table';          $self->{insertion_mode} = IN_TABLE_IM;
4842                        
4843          !!!next-token;          !!!next-token;
4844          redo B;          redo B;
# Line 4697  sub _tree_construction_main ($) { Line 4865  sub _tree_construction_main ($) {
4865          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4866            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4867              !!!back-token;              !!!back-token;
4868              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4869              redo B;              redo B;
4870            } elsif ({            } elsif ({
4871                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
# Line 4737  sub _tree_construction_main ($) { Line 4905  sub _tree_construction_main ($) {
4905            delete $at->{action};            delete $at->{action};
4906            delete $at->{prompt};            delete $at->{prompt};
4907            my @tokens = (            my @tokens = (
4908                          {type => 'start tag', tag_name => 'form',                          {type => START_TAG_TOKEN, tag_name => 'form',
4909                           attributes => $form_attrs},                           attributes => $form_attrs},
4910                          {type => 'start tag', tag_name => 'hr'},                          {type => START_TAG_TOKEN, tag_name => 'hr'},
4911                          {type => 'start tag', tag_name => 'p'},                          {type => START_TAG_TOKEN, tag_name => 'p'},
4912                          {type => 'start tag', tag_name => 'label'},                          {type => START_TAG_TOKEN, tag_name => 'label'},
4913                         );                         );
4914            if ($prompt_attr) {            if ($prompt_attr) {
4915              push @tokens, {type => 'character', data => $prompt_attr->{value}};              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};
4916            } else {            } else {
4917              push @tokens, {type => 'character',              push @tokens, {type => CHARACTER_TOKEN,
4918                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD
4919              ## TODO: make this configurable              ## TODO: make this configurable
4920            }            }
4921            push @tokens,            push @tokens,
4922                          {type => 'start tag', tag_name => 'input', attributes => $at},                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},
4923                          #{type => 'character', data => ''}, # SHOULD                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
4924                          {type => 'end tag', tag_name => 'label'},                          {type => END_TAG_TOKEN, tag_name => 'label'},
4925                          {type => 'end tag', tag_name => 'p'},                          {type => END_TAG_TOKEN, tag_name => 'p'},
4926                          {type => 'start tag', tag_name => 'hr'},                          {type => START_TAG_TOKEN, tag_name => 'hr'},
4927                          {type => 'end tag', tag_name => 'form'};                          {type => END_TAG_TOKEN, tag_name => 'form'};
4928            $token = shift @tokens;            $token = shift @tokens;
4929            !!!back-token (@tokens);            !!!back-token (@tokens);
4930            redo B;            redo B;
# Line 4774  sub _tree_construction_main ($) { Line 4942  sub _tree_construction_main ($) {
4942                    
4943          my $text = '';          my $text = '';
4944          !!!next-token;          !!!next-token;
4945          if ($token->{type} eq 'character') {          if ($token->{type} == CHARACTER_TOKEN) {
4946            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
4947            unless (length $token->{data}) {            unless (length $token->{data}) {
4948              !!!next-token;              !!!next-token;
4949            }            }
4950          }          }
4951          while ($token->{type} eq 'character') {          while ($token->{type} == CHARACTER_TOKEN) {
4952            $text .= $token->{data};            $text .= $token->{data};
4953            !!!next-token;            !!!next-token;
4954          }          }
# Line 4790  sub _tree_construction_main ($) { Line 4958  sub _tree_construction_main ($) {
4958                    
4959          $self->{content_model} = PCDATA_CONTENT_MODEL;          $self->{content_model} = PCDATA_CONTENT_MODEL;
4960                    
4961          if ($token->{type} eq 'end tag' and          if ($token->{type} == END_TAG_TOKEN and
4962              $token->{tag_name} eq $tag_name) {              $token->{tag_name} eq $tag_name) {
4963            ## Ignore the token            ## Ignore the token
4964          } else {          } else {
# Line 4804  sub _tree_construction_main ($) { Line 4972  sub _tree_construction_main ($) {
4972                  noframes => 1,                  noframes => 1,
4973                  noscript => 0, ## TODO: 1 if scripting is enabled                  noscript => 0, ## TODO: 1 if scripting is enabled
4974                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
4975          ## NOTE: There are two "as if in body" code clones.          ## NOTE: There is an "as if in body" code clone.
4976          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4977          redo B;          redo B;
4978        } elsif ($token->{tag_name} eq 'select') {        } elsif ($token->{tag_name} eq 'select') {
# Line 4812  sub _tree_construction_main ($) { Line 4980  sub _tree_construction_main ($) {
4980                    
4981          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4982                    
4983          $self->{insertion_mode} = 'in select';          $self->{insertion_mode} = IN_SELECT_IM;
4984          !!!next-token;          !!!next-token;
4985          redo B;          redo B;
4986        } elsif ({        } elsif ({
# Line 4835  sub _tree_construction_main ($) { Line 5003  sub _tree_construction_main ($) {
5003          !!!next-token;          !!!next-token;
5004          redo B;          redo B;
5005        }        }
5006      } elsif ($token->{type} eq 'end tag') {      } elsif ($token->{type} == END_TAG_TOKEN) {
5007        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
5008          if (@{$self->{open_elements}} > 1 and          if (@{$self->{open_elements}} > 1 and
5009              $self->{open_elements}->[1]->[1] eq 'body') {              $self->{open_elements}->[1]->[1] eq 'body') {
# Line 4849  sub _tree_construction_main ($) { Line 5017  sub _tree_construction_main ($) {
5017              }              }
5018            }            }
5019    
5020            $self->{insertion_mode} = 'after body';            $self->{insertion_mode} = AFTER_BODY_IM;
5021            !!!next-token;            !!!next-token;
5022            redo B;            redo B;
5023          } else {          } else {
# Line 4864  sub _tree_construction_main ($) { Line 5032  sub _tree_construction_main ($) {
5032            if ($self->{open_elements}->[-1]->[1] ne 'body') {            if ($self->{open_elements}->[-1]->[1] ne 'body') {
5033              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);
5034            }            }
5035            $self->{insertion_mode} = 'after body';            $self->{insertion_mode} = AFTER_BODY_IM;
5036            ## reprocess            ## reprocess
5037            redo B;            redo B;
5038          } else {          } else {
# Line 4896  sub _tree_construction_main ($) { Line 5064  sub _tree_construction_main ($) {
5064                   tbody => 1, tfoot=> 1, thead => 1,                   tbody => 1, tfoot=> 1, thead => 1,
5065                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
5066                !!!back-token;                !!!back-token;
5067                $token = {type => 'end tag',                $token = {type => END_TAG_TOKEN,
5068                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5069                redo B;                redo B;
5070              }              }
# Line 4944  sub _tree_construction_main ($) { Line 5112  sub _tree_construction_main ($) {
5112                   tbody => 1, tfoot=> 1, thead => 1,                   tbody => 1, tfoot=> 1, thead => 1,
5113                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
5114                !!!back-token;                !!!back-token;
5115                $token = {type => 'end tag',                $token = {type => END_TAG_TOKEN,
5116                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5117                redo B;                redo B;
5118              }              }
# Line 4960  sub _tree_construction_main ($) { Line 5128  sub _tree_construction_main ($) {
5128          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {
5129            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
5130          } else {          } else {
5131            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5132          }          }
5133    
5134          undef $self->{form_element};          undef $self->{form_element};
# Line 4983  sub _tree_construction_main ($) { Line 5151  sub _tree_construction_main ($) {
5151                   tbody => 1, tfoot=> 1, thead => 1,                   tbody => 1, tfoot=> 1, thead => 1,
5152                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
5153                !!!back-token;                !!!back-token;
5154                $token = {type => 'end tag',                $token = {type => END_TAG_TOKEN,
5155                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5156                redo B;                redo B;
5157              }              }
# Line 4998  sub _tree_construction_main ($) { Line 5166  sub _tree_construction_main ($) {
5166          } # INSCOPE          } # INSCOPE
5167                    
5168          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
5169            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5170          }          }
5171                    
5172          splice @{$self->{open_elements}}, $i if defined $i;          splice @{$self->{open_elements}}, $i if defined $i;
# Line 5057  sub _tree_construction_main ($) { Line 5225  sub _tree_construction_main ($) {
5225              if ({              if ({
5226                   dd => 1, dt => 1, li => 1, p => 1,                   dd => 1, dt => 1, li => 1, p => 1,
5227                   td => 1, th => 1, tr => 1,                   td => 1, th => 1, tr => 1,
5228                   tbody => 1, tfoot=> 1, thead => 1,                   tbody => 1, tfoot => 1, thead => 1,
5229                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
5230                !!!back-token;                !!!back-token;
5231                $token = {type => 'end tag',                $token = {type => END_TAG_TOKEN,
5232                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5233                redo B;                redo B;
5234              }              }
5235                    
5236              ## Step 2              ## Step 2
5237              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {
5238                  ## NOTE: <x><y></x>
5239                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
5240              }              }
5241                            
# Line 5117  sub set_inner_html ($$$) { Line 5286  sub set_inner_html ($$$) {
5286    my $s = \$_[0];    my $s = \$_[0];
5287    my $onerror = $_[1];    my $onerror = $_[1];
5288    
5289      ## ISSUE: Should {confident} be true?
5290    
5291    my $nt = $node->node_type;    my $nt = $node->node_type;
5292    if ($nt == 9) {    if ($nt == 9) {
5293      # MUST      # MUST
# Line 5269  sub set_inner_html ($$$) { Line 5440  sub set_inner_html ($$$) {
5440    
5441  } # tree construction stage  } # tree construction stage
5442    
5443  sub get_inner_html ($$$) {  package Whatpm::HTML::RestartParser;
5444    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  
5445    
5446  1;  1;
5447  # $Date$  # $Date$

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24