/[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.71 by wakaba, Sun Mar 2 03:39:41 2008 UTC
# Line 1  Line 1 
1  package Whatpm::HTML;  package Whatpm::HTML;
2  use strict;  use strict;
3  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
4    use Error qw(:try);
5    
6  ## ISSUE:  ## ISSUE:
7  ## var doc = implementation.createDocument (null, null, null);  ## var doc = implementation.createDocument (null, null, null);
8  ## doc.write ('');  ## doc.write ('');
9  ## alert (doc.compatMode);  ## alert (doc.compatMode);
10    
11  ## ISSUE: HTML5 revision 967 says that the encoding layer MUST NOT  ## TODO: Control charcters and noncharacters are not allowed (HTML5 revision 1263)
12  ## strip BOM and the HTML layer MUST ignore it.  Whether we can do it  ## TODO: 1252 parse error (revision 1264)
13  ## is not yet clear.  ## TODO: 8859-11 = 874 (revision 1271)
 ## "{U+FEFF}..." in UTF-16BE/UTF-16LE is three or four characters?  
 ## "{U+FEFF}..." in GB18030?  
14    
15  my $permitted_slash_tag_name = {  my $permitted_slash_tag_name = {
16    base => 1,    base => 1,
# Line 19  my $permitted_slash_tag_name = { Line 18  my $permitted_slash_tag_name = {
18    meta => 1,    meta => 1,
19    hr => 1,    hr => 1,
20    br => 1,    br => 1,
21    img=> 1,    img => 1,
22    embed => 1,    embed => 1,
23    param => 1,    param => 1,
24    area => 1,    area => 1,
# Line 84  my $formatting_category = { Line 83  my $formatting_category = {
83  };  };
84  # $phrasing_category: all other elements  # $phrasing_category: all other elements
85    
86    sub parse_byte_string ($$$$;$) {
87      my $self = ref $_[0] ? shift : shift->new;
88      my $charset = shift;
89      my $bytes_s = ref $_[0] ? $_[0] : \($_[0]);
90      my $s;
91      
92      if (defined $charset) {
93        require Encode; ## TODO: decode(utf8) don't delete BOM
94        $s = \ (Encode::decode ($charset, $$bytes_s));
95        $self->{input_encoding} = lc $charset; ## TODO: normalize name
96        $self->{confident} = 1;
97      } else {
98        ## TODO: Implement HTML5 detection algorithm
99        require Whatpm::Charset::UniversalCharDet;
100        $charset = Whatpm::Charset::UniversalCharDet->detect_byte_string
101            (substr ($$bytes_s, 0, 1024));
102        $charset ||= 'windows-1252';
103        $s = \ (Encode::decode ($charset, $$bytes_s));
104        $self->{input_encoding} = $charset;
105        $self->{confident} = 0;
106      }
107    
108      $self->{change_encoding} = sub {
109        my $self = shift;
110        my $charset = lc shift;
111        ## TODO: if $charset is supported
112        ## TODO: normalize charset name
113    
114        ## "Change the encoding" algorithm:
115    
116        ## Step 1    
117        if ($charset eq 'utf-16') { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?
118          $charset = 'utf-8';
119        }
120    
121        ## Step 2
122        if (defined $self->{input_encoding} and
123            $self->{input_encoding} eq $charset) {
124          $self->{confident} = 1;
125          return;
126        }
127    
128        !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.
129            ':'.$charset, level => 'w');
130    
131        ## Step 3
132        # if (can) {
133          ## change the encoding on the fly.
134          #$self->{confident} = 1;
135          #return;
136        # }
137    
138        ## Step 4
139        throw Whatpm::HTML::RestartParser (charset => $charset);
140      }; # $self->{change_encoding}
141    
142      my @args = @_; shift @args; # $s
143      my $return;
144      try {
145        $return = $self->parse_char_string ($s, @args);  
146      } catch Whatpm::HTML::RestartParser with {
147        my $charset = shift->{charset};
148        $s = \ (Encode::decode ($charset, $$bytes_s));    
149        $self->{input_encoding} = $charset; ## TODO: normalize
150        $self->{confident} = 1;
151        $return = $self->parse_char_string ($s, @args);
152      };
153      return $return;
154    } # parse_byte_string
155    
156    ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
157    ## and the HTML layer MUST ignore it.  However, we does strip BOM in
158    ## the encoding layer and the HTML layer does not ignore any U+FEFF,
159    ## because the core part of our HTML parser expects a string of character,
160    ## not a string of bytes or code units or anything which might contain a BOM.
161    ## Therefore, any parser interface that accepts a string of bytes,
162    ## such as |parse_byte_string| in this module, must ensure that it does
163    ## strip the BOM and never strip any ZWNBSP.
164    
165    *parse_char_string = \&parse_string;
166    
167  sub parse_string ($$$;$) {  sub parse_string ($$$;$) {
168    my $self = shift->new;    my $self = ref $_[0] ? shift : shift->new;
169    my $s = \$_[0];    my $s = ref $_[0] ? $_[0] : \($_[0]);
170    $self->{document} = $_[1];    $self->{document} = $_[1];
171      @{$self->{document}->child_nodes} = ();
172    
173    ## NOTE: |set_inner_html| copies most of this method's code    ## NOTE: |set_inner_html| copies most of this method's code
174    
175      $self->{confident} = 1 unless exists $self->{confident};
176      $self->{document}->input_encoding ($self->{input_encoding})
177          if defined $self->{input_encoding};
178    
179    my $i = 0;    my $i = 0;
180    my $line = 1;    my $line = 1;
181    my $column = 0;    my $column = 0;
# Line 147  sub new ($) { Line 232  sub new ($) {
232    $self->{parse_error} = sub {    $self->{parse_error} = sub {
233      #      #
234    };    };
235      $self->{change_encoding} = sub {
236        # if ($_[0] is a supported encoding) {
237        #   run "change the encoding" algorithm;
238        #   throw Whatpm::HTML::RestartParser (charset => $new_encoding);
239        # }
240      };
241      $self->{application_cache_selection} = sub {
242        #
243      };
244    return $self;    return $self;
245  } # new  } # new
246    
# Line 159  sub CDATA_CONTENT_MODEL () { CM_LIMITED_ Line 253  sub CDATA_CONTENT_MODEL () { CM_LIMITED_
253  sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }  sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }
254  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
255    
256    sub DATA_STATE () { 0 }
257    sub ENTITY_DATA_STATE () { 1 }
258    sub TAG_OPEN_STATE () { 2 }
259    sub CLOSE_TAG_OPEN_STATE () { 3 }
260    sub TAG_NAME_STATE () { 4 }
261    sub BEFORE_ATTRIBUTE_NAME_STATE () { 5 }
262    sub ATTRIBUTE_NAME_STATE () { 6 }
263    sub AFTER_ATTRIBUTE_NAME_STATE () { 7 }
264    sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 }
265    sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
266    sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
267    sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
268    sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
269    sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
270    sub COMMENT_START_STATE () { 14 }
271    sub COMMENT_START_DASH_STATE () { 15 }
272    sub COMMENT_STATE () { 16 }
273    sub COMMENT_END_STATE () { 17 }
274    sub COMMENT_END_DASH_STATE () { 18 }
275    sub BOGUS_COMMENT_STATE () { 19 }
276    sub DOCTYPE_STATE () { 20 }
277    sub BEFORE_DOCTYPE_NAME_STATE () { 21 }
278    sub DOCTYPE_NAME_STATE () { 22 }
279    sub AFTER_DOCTYPE_NAME_STATE () { 23 }
280    sub BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 24 }
281    sub DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE () { 25 }
282    sub DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE () { 26 }
283    sub AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 27 }
284    sub BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 28 }
285    sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE () { 29 }
286    sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }
287    sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
288    sub BOGUS_DOCTYPE_STATE () { 32 }
289    
290    sub DOCTYPE_TOKEN () { 1 }
291    sub COMMENT_TOKEN () { 2 }
292    sub START_TAG_TOKEN () { 3 }
293    sub END_TAG_TOKEN () { 4 }
294    sub END_OF_FILE_TOKEN () { 5 }
295    sub CHARACTER_TOKEN () { 6 }
296    
297    sub AFTER_HTML_IMS () { 0b100 }
298    sub HEAD_IMS ()       { 0b1000 }
299    sub BODY_IMS ()       { 0b10000 }
300    sub BODY_TABLE_IMS () { 0b100000 }
301    sub TABLE_IMS ()      { 0b1000000 }
302    sub ROW_IMS ()        { 0b10000000 }
303    sub BODY_AFTER_IMS () { 0b100000000 }
304    sub FRAME_IMS ()      { 0b1000000000 }
305    
306    sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
307    sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
308    sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
309    sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
310    sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
311    sub BEFORE_HEAD_IM () { HEAD_IMS | 0b11 }
312    sub IN_BODY_IM () { BODY_IMS }
313    sub IN_CELL_IM () { BODY_IMS | BODY_TABLE_IMS | 0b01 }
314    sub IN_CAPTION_IM () { BODY_IMS | BODY_TABLE_IMS | 0b10 }
315    sub IN_ROW_IM () { TABLE_IMS | ROW_IMS | 0b01 }
316    sub IN_TABLE_BODY_IM () { TABLE_IMS | ROW_IMS | 0b10 }
317    sub IN_TABLE_IM () { TABLE_IMS }
318    sub AFTER_BODY_IM () { BODY_AFTER_IMS }
319    sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
320    sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
321    sub IN_SELECT_IM () { 0b01 }
322    sub IN_COLUMN_GROUP_IM () { 0b10 }
323    
324  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
325    
326  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
327    my $self = shift;    my $self = shift;
328    $self->{state} = 'data'; # MUST    $self->{state} = DATA_STATE; # MUST
329    $self->{content_model} = PCDATA_CONTENT_MODEL; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
330    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE
331    undef $self->{current_attribute};    undef $self->{current_attribute};
# Line 177  sub _initialize_tokenizer ($) { Line 339  sub _initialize_tokenizer ($) {
339  } # _initialize_tokenizer  } # _initialize_tokenizer
340    
341  ## A token has:  ## A token has:
342  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',  ##   ->{type} == DOCTYPE_TOKEN, START_TAG_TOKEN, END_TAG_TOKEN, COMMENT_TOKEN,
343  ##       'character', or 'end-of-file'  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN
344  ##   ->{name} (DOCTYPE, start tag (tag name), end tag (tag name))  ##   ->{name} (DOCTYPE_TOKEN)
345  ##   ->{public_identifier} (DOCTYPE)  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
346  ##   ->{system_identifier} (DOCTYPE)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
347  ##   ->{correct} == 1 or 0 (DOCTYPE)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
348  ##   ->{attributes} isa HASH (start tag, end tag)  ##   ->{correct} == 1 or 0 (DOCTYPE_TOKEN)
349  ##   ->{data} (comment, character)  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
350    ##        ->{name}
351    ##        ->{value}
352    ##        ->{has_reference} == 1 or 0
353    ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
354    
355  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
356    
# Line 194  sub _initialize_tokenizer ($) { Line 360  sub _initialize_tokenizer ($) {
360  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
361  ## and removed from the list.  ## and removed from the list.
362    
363    ## NOTE: HTML5 "Writing HTML documents" section, applied to
364    ## documents and not to user agents and conformance checkers,
365    ## contains some requirements that are not detected by the
366    ## parsing algorithm:
367    ## - Some requirements on character encoding declarations. ## TODO
368    ## - "Elements MUST NOT contain content that their content model disallows."
369    ##   ... Some are parse error, some are not (will be reported by c.c.).
370    ## - Polytheistic slash SHOULD NOT be used. (Applied only to atheists.) ## TODO
371    ## - Text (in elements, attributes, and comments) SHOULD NOT contain
372    ##   control characters other than space characters. ## TODO: (what is control character? C0, C1 and DEL?  Unicode control character?)
373    
374    ## TODO: HTML5 poses authors two SHOULD-level requirements that cannot
375    ## be detected by the HTML5 parsing algorithm:
376    ## - Text,
377    
378  sub _get_next_token ($) {  sub _get_next_token ($) {
379    my $self = shift;    my $self = shift;
380    if (@{$self->{token}}) {    if (@{$self->{token}}) {
# Line 201  sub _get_next_token ($) { Line 382  sub _get_next_token ($) {
382    }    }
383    
384    A: {    A: {
385      if ($self->{state} eq 'data') {      if ($self->{state} == DATA_STATE) {
386        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_input_character} == 0x0026) { # &
387          if ($self->{content_model} & CM_ENTITY) { # PCDATA | RCDATA          if ($self->{content_model} & CM_ENTITY) { # PCDATA | RCDATA
388            $self->{state} = 'entity data';            $self->{state} = ENTITY_DATA_STATE;
389            !!!next-input-character;            !!!next-input-character;
390            redo A;            redo A;
391          } else {          } else {
# Line 226  sub _get_next_token ($) { Line 407  sub _get_next_token ($) {
407          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
408              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
409               not $self->{escape})) {               not $self->{escape})) {
410            $self->{state} = 'tag open';            $self->{state} = TAG_OPEN_STATE;
411            !!!next-input-character;            !!!next-input-character;
412            redo A;            redo A;
413          } else {          } else {
# Line 243  sub _get_next_token ($) { Line 424  sub _get_next_token ($) {
424                    
425          #          #
426        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
427          !!!emit ({type => 'end-of-file'});          !!!emit ({type => END_OF_FILE_TOKEN});
428          last A; ## TODO: ok?          last A; ## TODO: ok?
429        }        }
430        # Anything else        # Anything else
431        my $token = {type => 'character',        my $token = {type => CHARACTER_TOKEN,
432                     data => chr $self->{next_input_character}};                     data => chr $self->{next_input_character}};
433        ## Stay in the data state        ## Stay in the data state
434        !!!next-input-character;        !!!next-input-character;
# Line 255  sub _get_next_token ($) { Line 436  sub _get_next_token ($) {
436        !!!emit ($token);        !!!emit ($token);
437    
438        redo A;        redo A;
439      } elsif ($self->{state} eq 'entity data') {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
440        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
441                
442        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);
443    
444        $self->{state} = 'data';        $self->{state} = DATA_STATE;
445        # next-input-character is already done        # next-input-character is already done
446    
447        unless (defined $token) {        unless (defined $token) {
448          !!!emit ({type => 'character', data => '&'});          !!!emit ({type => CHARACTER_TOKEN, data => '&'});
449        } else {        } else {
450          !!!emit ($token);          !!!emit ($token);
451        }        }
452    
453        redo A;        redo A;
454      } elsif ($self->{state} eq 'tag open') {      } elsif ($self->{state} == TAG_OPEN_STATE) {
455        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
456          if ($self->{next_input_character} == 0x002F) { # /          if ($self->{next_input_character} == 0x002F) { # /
457            !!!next-input-character;            !!!next-input-character;
458            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
459            redo A;            redo A;
460          } else {          } else {
461            ## reconsume            ## reconsume
462            $self->{state} = 'data';            $self->{state} = DATA_STATE;
463    
464            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<'});
465    
466            redo A;            redo A;
467          }          }
468        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
469          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_input_character} == 0x0021) { # !
470            $self->{state} = 'markup declaration open';            $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
471            !!!next-input-character;            !!!next-input-character;
472            redo A;            redo A;
473          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{next_input_character} == 0x002F) { # /
474            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
475            !!!next-input-character;            !!!next-input-character;
476            redo A;            redo A;
477          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_input_character} and
478                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{next_input_character} <= 0x005A) { # A..Z
479            $self->{current_token}            $self->{current_token}
480              = {type => 'start tag',              = {type => START_TAG_TOKEN,
481                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_input_character} + 0x0020)};
482            $self->{state} = 'tag name';            $self->{state} = TAG_NAME_STATE;
483            !!!next-input-character;            !!!next-input-character;
484            redo A;            redo A;
485          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_input_character} and
486                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_input_character} <= 0x007A) { # a..z
487            $self->{current_token} = {type => 'start tag',            $self->{current_token} = {type => START_TAG_TOKEN,
488                              tag_name => chr ($self->{next_input_character})};                              tag_name => chr ($self->{next_input_character})};
489            $self->{state} = 'tag name';            $self->{state} = TAG_NAME_STATE;
490            !!!next-input-character;            !!!next-input-character;
491            redo A;            redo A;
492          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_input_character} == 0x003E) { # >
493            !!!parse-error (type => 'empty start tag');            !!!parse-error (type => 'empty start tag');
494            $self->{state} = 'data';            $self->{state} = DATA_STATE;
495            !!!next-input-character;            !!!next-input-character;
496    
497            !!!emit ({type => 'character', data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});
498    
499            redo A;            redo A;
500          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_input_character} == 0x003F) { # ?
501            !!!parse-error (type => 'pio');            !!!parse-error (type => 'pio');
502            $self->{state} = 'bogus comment';            $self->{state} = BOGUS_COMMENT_STATE;
503            ## $self->{next_input_character} is intentionally left as is            ## $self->{next_input_character} is intentionally left as is
504            redo A;            redo A;
505          } else {          } else {
506            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago');
507            $self->{state} = 'data';            $self->{state} = DATA_STATE;
508            ## reconsume            ## reconsume
509    
510            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<'});
511    
512            redo A;            redo A;
513          }          }
514        } else {        } else {
515          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
516        }        }
517      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
518        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
519          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
520            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
# Line 348  sub _get_next_token ($) { Line 529  sub _get_next_token ($) {
529              } else {              } else {
530                $self->{next_input_character} = shift @next_char; # reconsume                $self->{next_input_character} = shift @next_char; # reconsume
531                !!!back-next-input-character (@next_char);                !!!back-next-input-character (@next_char);
532                $self->{state} = 'data';                $self->{state} = DATA_STATE;
533    
534                !!!emit ({type => 'character', data => '</'});                !!!emit ({type => CHARACTER_TOKEN, data => '</'});
535        
536                redo A;                redo A;
537              }              }
# Line 367  sub _get_next_token ($) { Line 548  sub _get_next_token ($) {
548                    $self->{next_input_character} == -1) {                    $self->{next_input_character} == -1) {
549              $self->{next_input_character} = shift @next_char; # reconsume              $self->{next_input_character} = shift @next_char; # reconsume
550              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
551              $self->{state} = 'data';              $self->{state} = DATA_STATE;
552              !!!emit ({type => 'character', data => '</'});              !!!emit ({type => CHARACTER_TOKEN, data => '</'});
553              redo A;              redo A;
554            } else {            } else {
555              $self->{next_input_character} = shift @next_char;              $self->{next_input_character} = shift @next_char;
# Line 378  sub _get_next_token ($) { Line 559  sub _get_next_token ($) {
559          } else {          } else {
560            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
561            # next-input-character is already done            # next-input-character is already done
562            $self->{state} = 'data';            $self->{state} = DATA_STATE;
563            !!!emit ({type => 'character', data => '</'});            !!!emit ({type => CHARACTER_TOKEN, data => '</'});
564            redo A;            redo A;
565          }          }
566        }        }
567                
568        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_input_character} and
569            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_input_character} <= 0x005A) { # A..Z
570          $self->{current_token} = {type => 'end tag',          $self->{current_token} = {type => END_TAG_TOKEN,
571                            tag_name => chr ($self->{next_input_character} + 0x0020)};                            tag_name => chr ($self->{next_input_character} + 0x0020)};
572          $self->{state} = 'tag name';          $self->{state} = TAG_NAME_STATE;
573          !!!next-input-character;          !!!next-input-character;
574          redo A;          redo A;
575        } elsif (0x0061 <= $self->{next_input_character} and        } elsif (0x0061 <= $self->{next_input_character} and
576                 $self->{next_input_character} <= 0x007A) { # a..z                 $self->{next_input_character} <= 0x007A) { # a..z
577          $self->{current_token} = {type => 'end tag',          $self->{current_token} = {type => END_TAG_TOKEN,
578                            tag_name => chr ($self->{next_input_character})};                            tag_name => chr ($self->{next_input_character})};
579          $self->{state} = 'tag name';          $self->{state} = TAG_NAME_STATE;
580          !!!next-input-character;          !!!next-input-character;
581          redo A;          redo A;
582        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
583          !!!parse-error (type => 'empty end tag');          !!!parse-error (type => 'empty end tag');
584          $self->{state} = 'data';          $self->{state} = DATA_STATE;
585          !!!next-input-character;          !!!next-input-character;
586          redo A;          redo A;
587        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
588          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
589          $self->{state} = 'data';          $self->{state} = DATA_STATE;
590          # reconsume          # reconsume
591    
592          !!!emit ({type => 'character', data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</'});
593    
594          redo A;          redo A;
595        } else {        } else {
596          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
597          $self->{state} = 'bogus comment';          $self->{state} = BOGUS_COMMENT_STATE;
598          ## $self->{next_input_character} is intentionally left as is          ## $self->{next_input_character} is intentionally left as is
599          redo A;          redo A;
600        }        }
601      } elsif ($self->{state} eq 'tag name') {      } elsif ($self->{state} == TAG_NAME_STATE) {
602        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
603            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
604            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
605            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
606            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
607          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
608          !!!next-input-character;          !!!next-input-character;
609          redo A;          redo A;
610        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
611          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
612            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
613                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
614            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
615          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
616            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
617            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
618              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 439  sub _get_next_token ($) { Line 620  sub _get_next_token ($) {
620          } else {          } else {
621            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
622          }          }
623          $self->{state} = 'data';          $self->{state} = DATA_STATE;
624          !!!next-input-character;          !!!next-input-character;
625    
626          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 454  sub _get_next_token ($) { Line 635  sub _get_next_token ($) {
635          redo A;          redo A;
636        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
637          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
638          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
639            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
640                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
641            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
642          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
643            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
644            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
645              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 466  sub _get_next_token ($) { Line 647  sub _get_next_token ($) {
647          } else {          } else {
648            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
649          }          }
650          $self->{state} = 'data';          $self->{state} = DATA_STATE;
651          # reconsume          # reconsume
652    
653          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 475  sub _get_next_token ($) { Line 656  sub _get_next_token ($) {
656        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
657          !!!next-input-character;          !!!next-input-character;
658          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
659              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
660              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
661            # permitted slash            # permitted slash
662            #            #
663          } else {          } else {
664            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
665          }          }
666          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
667          # next-input-character is already done          # next-input-character is already done
668          redo A;          redo A;
669        } else {        } else {
# Line 492  sub _get_next_token ($) { Line 673  sub _get_next_token ($) {
673          !!!next-input-character;          !!!next-input-character;
674          redo A;          redo A;
675        }        }
676      } elsif ($self->{state} eq 'before attribute name') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
677        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
678            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
679            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 502  sub _get_next_token ($) { Line 683  sub _get_next_token ($) {
683          !!!next-input-character;          !!!next-input-character;
684          redo A;          redo A;
685        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
686          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
687            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
688                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
689            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
690          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
691            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
692            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
693              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 514  sub _get_next_token ($) { Line 695  sub _get_next_token ($) {
695          } else {          } else {
696            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
697          }          }
698          $self->{state} = 'data';          $self->{state} = DATA_STATE;
699          !!!next-input-character;          !!!next-input-character;
700    
701          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 524  sub _get_next_token ($) { Line 705  sub _get_next_token ($) {
705                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_input_character} <= 0x005A) { # A..Z
706          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),
707                                value => ''};                                value => ''};
708          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
709          !!!next-input-character;          !!!next-input-character;
710          redo A;          redo A;
711        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
712          !!!next-input-character;          !!!next-input-character;
713          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
714              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
715              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
716            # permitted slash            # permitted slash
717            #            #
# Line 542  sub _get_next_token ($) { Line 723  sub _get_next_token ($) {
723          redo A;          redo A;
724        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
725          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
726          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
727            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
728                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
729            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
730          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
731            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
732            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
733              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 554  sub _get_next_token ($) { Line 735  sub _get_next_token ($) {
735          } else {          } else {
736            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
737          }          }
738          $self->{state} = 'data';          $self->{state} = DATA_STATE;
739          # reconsume          # reconsume
740    
741          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 563  sub _get_next_token ($) { Line 744  sub _get_next_token ($) {
744        } else {        } else {
745          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          $self->{current_attribute} = {name => chr ($self->{next_input_character}),
746                                value => ''};                                value => ''};
747          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
748          !!!next-input-character;          !!!next-input-character;
749          redo A;          redo A;
750        }        }
751      } elsif ($self->{state} eq 'attribute name') {      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {
752        my $before_leave = sub {        my $before_leave = sub {
753          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
754              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
# Line 585  sub _get_next_token ($) { Line 766  sub _get_next_token ($) {
766            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
767            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
768          $before_leave->();          $before_leave->();
769          $self->{state} = 'after attribute name';          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
770          !!!next-input-character;          !!!next-input-character;
771          redo A;          redo A;
772        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_input_character} == 0x003D) { # =
773          $before_leave->();          $before_leave->();
774          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
775          !!!next-input-character;          !!!next-input-character;
776          redo A;          redo A;
777        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
778          $before_leave->();          $before_leave->();
779          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
780            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
781                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
782            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
783          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
784            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
785            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
786              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 607  sub _get_next_token ($) { Line 788  sub _get_next_token ($) {
788          } else {          } else {
789            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
790          }          }
791          $self->{state} = 'data';          $self->{state} = DATA_STATE;
792          !!!next-input-character;          !!!next-input-character;
793    
794          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 623  sub _get_next_token ($) { Line 804  sub _get_next_token ($) {
804          $before_leave->();          $before_leave->();
805          !!!next-input-character;          !!!next-input-character;
806          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
807              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
808              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
809            # permitted slash            # permitted slash
810            #            #
811          } else {          } else {
812            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
813          }          }
814          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
815          # next-input-character is already done          # next-input-character is already done
816          redo A;          redo A;
817        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
818          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
819          $before_leave->();          $before_leave->();
820          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
821            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
822                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
823            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
824          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
825            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
826            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
827              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 648  sub _get_next_token ($) { Line 829  sub _get_next_token ($) {
829          } else {          } else {
830            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
831          }          }
832          $self->{state} = 'data';          $self->{state} = DATA_STATE;
833          # reconsume          # reconsume
834    
835          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 660  sub _get_next_token ($) { Line 841  sub _get_next_token ($) {
841          !!!next-input-character;          !!!next-input-character;
842          redo A;          redo A;
843        }        }
844      } elsif ($self->{state} eq 'after attribute name') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
845        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
846            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
847            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 670  sub _get_next_token ($) { Line 851  sub _get_next_token ($) {
851          !!!next-input-character;          !!!next-input-character;
852          redo A;          redo A;
853        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_input_character} == 0x003D) { # =
854          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
855          !!!next-input-character;          !!!next-input-character;
856          redo A;          redo A;
857        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
858          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
859            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
860                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
861            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
862          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
863            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
864            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
865              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 686  sub _get_next_token ($) { Line 867  sub _get_next_token ($) {
867          } else {          } else {
868            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
869          }          }
870          $self->{state} = 'data';          $self->{state} = DATA_STATE;
871          !!!next-input-character;          !!!next-input-character;
872    
873          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 696  sub _get_next_token ($) { Line 877  sub _get_next_token ($) {
877                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_input_character} <= 0x005A) { # A..Z
878          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),
879                                value => ''};                                value => ''};
880          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
881          !!!next-input-character;          !!!next-input-character;
882          redo A;          redo A;
883        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
884          !!!next-input-character;          !!!next-input-character;
885          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
886              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
887              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
888            # permitted slash            # permitted slash
889            #            #
# Line 710  sub _get_next_token ($) { Line 891  sub _get_next_token ($) {
891            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
892            ## TODO: Different error type for <aa / bb> than <aa/>            ## TODO: Different error type for <aa / bb> than <aa/>
893          }          }
894          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
895          # next-input-character is already done          # next-input-character is already done
896          redo A;          redo A;
897        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
898          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
899          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
900            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
901                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
902            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
903          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
904            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
905            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
906              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 727  sub _get_next_token ($) { Line 908  sub _get_next_token ($) {
908          } else {          } else {
909            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
910          }          }
911          $self->{state} = 'data';          $self->{state} = DATA_STATE;
912          # reconsume          # reconsume
913    
914          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 736  sub _get_next_token ($) { Line 917  sub _get_next_token ($) {
917        } else {        } else {
918          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          $self->{current_attribute} = {name => chr ($self->{next_input_character}),
919                                value => ''};                                value => ''};
920          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
921          !!!next-input-character;          !!!next-input-character;
922          redo A;                  redo A;        
923        }        }
924      } elsif ($self->{state} eq 'before attribute value') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
925        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
926            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
927            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 750  sub _get_next_token ($) { Line 931  sub _get_next_token ($) {
931          !!!next-input-character;          !!!next-input-character;
932          redo A;          redo A;
933        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_input_character} == 0x0022) { # "
934          $self->{state} = 'attribute value (double-quoted)';          $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
935          !!!next-input-character;          !!!next-input-character;
936          redo A;          redo A;
937        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
938          $self->{state} = 'attribute value (unquoted)';          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
939          ## reconsume          ## reconsume
940          redo A;          redo A;
941        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_input_character} == 0x0027) { # '
942          $self->{state} = 'attribute value (single-quoted)';          $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
943          !!!next-input-character;          !!!next-input-character;
944          redo A;          redo A;
945        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
946          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
947            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
948                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
949            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
950          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
951            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
952            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
953              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 774  sub _get_next_token ($) { Line 955  sub _get_next_token ($) {
955          } else {          } else {
956            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
957          }          }
958          $self->{state} = 'data';          $self->{state} = DATA_STATE;
959          !!!next-input-character;          !!!next-input-character;
960    
961          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 782  sub _get_next_token ($) { Line 963  sub _get_next_token ($) {
963          redo A;          redo A;
964        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
965          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
966          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
967            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
968                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
969            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
970          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
971            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
972            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
973              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 794  sub _get_next_token ($) { Line 975  sub _get_next_token ($) {
975          } else {          } else {
976            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
977          }          }
978          $self->{state} = 'data';          $self->{state} = DATA_STATE;
979          ## reconsume          ## reconsume
980    
981          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 802  sub _get_next_token ($) { Line 983  sub _get_next_token ($) {
983          redo A;          redo A;
984        } else {        } else {
985          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});
986          $self->{state} = 'attribute value (unquoted)';          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
987          !!!next-input-character;          !!!next-input-character;
988          redo A;          redo A;
989        }        }
990      } elsif ($self->{state} eq 'attribute value (double-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
991        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_input_character} == 0x0022) { # "
992          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
993          !!!next-input-character;          !!!next-input-character;
994          redo A;          redo A;
995        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
996          $self->{last_attribute_value_state} = 'attribute value (double-quoted)';          $self->{last_attribute_value_state} = $self->{state};
997          $self->{state} = 'entity in attribute value';          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
998          !!!next-input-character;          !!!next-input-character;
999          redo A;          redo A;
1000        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1001          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1002          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1003            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
1004                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
1005            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1006          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1007            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1008            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1009              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 830  sub _get_next_token ($) { Line 1011  sub _get_next_token ($) {
1011          } else {          } else {
1012            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1013          }          }
1014          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1015          ## reconsume          ## reconsume
1016    
1017          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 842  sub _get_next_token ($) { Line 1023  sub _get_next_token ($) {
1023          !!!next-input-character;          !!!next-input-character;
1024          redo A;          redo A;
1025        }        }
1026      } elsif ($self->{state} eq 'attribute value (single-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1027        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_input_character} == 0x0027) { # '
1028          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1029          !!!next-input-character;          !!!next-input-character;
1030          redo A;          redo A;
1031        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
1032          $self->{last_attribute_value_state} = 'attribute value (single-quoted)';          $self->{last_attribute_value_state} = $self->{state};
1033          $self->{state} = 'entity in attribute value';          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1034          !!!next-input-character;          !!!next-input-character;
1035          redo A;          redo A;
1036        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1037          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1038          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1039            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
1040                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
1041            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1042          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1043            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1044            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1045              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 866  sub _get_next_token ($) { Line 1047  sub _get_next_token ($) {
1047          } else {          } else {
1048            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1049          }          }
1050          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1051          ## reconsume          ## reconsume
1052    
1053          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 878  sub _get_next_token ($) { Line 1059  sub _get_next_token ($) {
1059          !!!next-input-character;          !!!next-input-character;
1060          redo A;          redo A;
1061        }        }
1062      } elsif ($self->{state} eq 'attribute value (unquoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1063        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1064            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1065            $self->{next_input_character} == 0x000B or # HT            $self->{next_input_character} == 0x000B or # HT
1066            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1067            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
1068          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1069          !!!next-input-character;          !!!next-input-character;
1070          redo A;          redo A;
1071        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
1072          $self->{last_attribute_value_state} = 'attribute value (unquoted)';          $self->{last_attribute_value_state} = $self->{state};
1073          $self->{state} = 'entity in attribute value';          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1074          !!!next-input-character;          !!!next-input-character;
1075          redo A;          redo A;
1076        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1077          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1078            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
1079                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
1080            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1081          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1082            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1083            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1084              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 905  sub _get_next_token ($) { Line 1086  sub _get_next_token ($) {
1086          } else {          } else {
1087            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1088          }          }
1089          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1090          !!!next-input-character;          !!!next-input-character;
1091    
1092          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 913  sub _get_next_token ($) { Line 1094  sub _get_next_token ($) {
1094          redo A;          redo A;
1095        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1096          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1097          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1098            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
1099                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
1100            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1101          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1102            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1103            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1104              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 925  sub _get_next_token ($) { Line 1106  sub _get_next_token ($) {
1106          } else {          } else {
1107            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1108          }          }
1109          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1110          ## reconsume          ## reconsume
1111    
1112          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 937  sub _get_next_token ($) { Line 1118  sub _get_next_token ($) {
1118          !!!next-input-character;          !!!next-input-character;
1119          redo A;          redo A;
1120        }        }
1121      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {
1122        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);
1123    
1124        unless (defined $token) {        unless (defined $token) {
1125          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
1126        } else {        } else {
1127          $self->{current_attribute}->{value} .= $token->{data};          $self->{current_attribute}->{value} .= $token->{data};
1128            $self->{current_attribute}->{has_reference} = $token->{has_reference};
1129          ## ISSUE: spec says "append the returned character token to the current attribute's value"          ## ISSUE: spec says "append the returned character token to the current attribute's value"
1130        }        }
1131    
1132        $self->{state} = $self->{last_attribute_value_state};        $self->{state} = $self->{last_attribute_value_state};
1133        # next-input-character is already done        # next-input-character is already done
1134        redo A;        redo A;
1135      } elsif ($self->{state} eq 'bogus comment') {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1136        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1137                
1138        my $token = {type => 'comment', data => ''};        my $token = {type => COMMENT_TOKEN, data => ''};
1139    
1140        BC: {        BC: {
1141          if ($self->{next_input_character} == 0x003E) { # >          if ($self->{next_input_character} == 0x003E) { # >
1142            $self->{state} = 'data';            $self->{state} = DATA_STATE;
1143            !!!next-input-character;            !!!next-input-character;
1144    
1145            !!!emit ($token);            !!!emit ($token);
1146    
1147            redo A;            redo A;
1148          } elsif ($self->{next_input_character} == -1) {          } elsif ($self->{next_input_character} == -1) {
1149            $self->{state} = 'data';            $self->{state} = DATA_STATE;
1150            ## reconsume            ## reconsume
1151    
1152            !!!emit ($token);            !!!emit ($token);
# Line 976  sub _get_next_token ($) { Line 1158  sub _get_next_token ($) {
1158            redo BC;            redo BC;
1159          }          }
1160        } # BC        } # BC
1161      } elsif ($self->{state} eq 'markup declaration open') {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1162        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1163    
1164        my @next_char;        my @next_char;
# Line 986  sub _get_next_token ($) { Line 1168  sub _get_next_token ($) {
1168          !!!next-input-character;          !!!next-input-character;
1169          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_input_character};
1170          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_input_character} == 0x002D) { # -
1171            $self->{current_token} = {type => 'comment', data => ''};            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};
1172            $self->{state} = 'comment start';            $self->{state} = COMMENT_START_STATE;
1173            !!!next-input-character;            !!!next-input-character;
1174            redo A;            redo A;
1175          }          }
# Line 1018  sub _get_next_token ($) { Line 1200  sub _get_next_token ($) {
1200                    if ($self->{next_input_character} == 0x0045 or # E                    if ($self->{next_input_character} == 0x0045 or # E
1201                        $self->{next_input_character} == 0x0065) { # e                        $self->{next_input_character} == 0x0065) { # e
1202                      ## ISSUE: What a stupid code this is!                      ## ISSUE: What a stupid code this is!
1203                      $self->{state} = 'DOCTYPE';                      $self->{state} = DOCTYPE_STATE;
1204                      !!!next-input-character;                      !!!next-input-character;
1205                      redo A;                      redo A;
1206                    }                    }
# Line 1032  sub _get_next_token ($) { Line 1214  sub _get_next_token ($) {
1214        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'bogus comment');
1215        $self->{next_input_character} = shift @next_char;        $self->{next_input_character} = shift @next_char;
1216        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
1217        $self->{state} = 'bogus comment';        $self->{state} = BOGUS_COMMENT_STATE;
1218        redo A;        redo A;
1219                
1220        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
1221        ## ISSUE: spec is somewhat unclear on "is the first character that will be in the comment"; what is "that will be in the comment" is what the algorithm defines, isn't it?        ## ISSUE: spec is somewhat unclear on "is the first character that will be in the comment"; what is "that will be in the comment" is what the algorithm defines, isn't it?
1222      } elsif ($self->{state} eq 'comment start') {      } elsif ($self->{state} == COMMENT_START_STATE) {
1223        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1224          $self->{state} = 'comment start dash';          $self->{state} = COMMENT_START_DASH_STATE;
1225          !!!next-input-character;          !!!next-input-character;
1226          redo A;          redo A;
1227        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1228          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
1229          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1230          !!!next-input-character;          !!!next-input-character;
1231    
1232          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1052  sub _get_next_token ($) { Line 1234  sub _get_next_token ($) {
1234          redo A;          redo A;
1235        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1236          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1237          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1238          ## reconsume          ## reconsume
1239    
1240          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1061  sub _get_next_token ($) { Line 1243  sub _get_next_token ($) {
1243        } else {        } else {
1244          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
1245              .= chr ($self->{next_input_character});              .= chr ($self->{next_input_character});
1246          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1247          !!!next-input-character;          !!!next-input-character;
1248          redo A;          redo A;
1249        }        }
1250      } elsif ($self->{state} eq 'comment start dash') {      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
1251        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1252          $self->{state} = 'comment end';          $self->{state} = COMMENT_END_STATE;
1253          !!!next-input-character;          !!!next-input-character;
1254          redo A;          redo A;
1255        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1256          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
1257          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1258          !!!next-input-character;          !!!next-input-character;
1259    
1260          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1080  sub _get_next_token ($) { Line 1262  sub _get_next_token ($) {
1262          redo A;          redo A;
1263        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1264          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1265          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1266          ## reconsume          ## reconsume
1267    
1268          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1089  sub _get_next_token ($) { Line 1271  sub _get_next_token ($) {
1271        } else {        } else {
1272          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
1273              .= '-' . chr ($self->{next_input_character});              .= '-' . chr ($self->{next_input_character});
1274          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1275          !!!next-input-character;          !!!next-input-character;
1276          redo A;          redo A;
1277        }        }
1278      } elsif ($self->{state} eq 'comment') {      } elsif ($self->{state} == COMMENT_STATE) {
1279        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1280          $self->{state} = 'comment end dash';          $self->{state} = COMMENT_END_DASH_STATE;
1281          !!!next-input-character;          !!!next-input-character;
1282          redo A;          redo A;
1283        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1284          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1285          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1286          ## reconsume          ## reconsume
1287    
1288          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1112  sub _get_next_token ($) { Line 1294  sub _get_next_token ($) {
1294          !!!next-input-character;          !!!next-input-character;
1295          redo A;          redo A;
1296        }        }
1297      } elsif ($self->{state} eq 'comment end dash') {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
1298        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1299          $self->{state} = 'comment end';          $self->{state} = COMMENT_END_STATE;
1300          !!!next-input-character;          !!!next-input-character;
1301          redo A;          redo A;
1302        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1303          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1304          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1305          ## reconsume          ## reconsume
1306    
1307          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1127  sub _get_next_token ($) { Line 1309  sub _get_next_token ($) {
1309          redo A;          redo A;
1310        } else {        } else {
1311          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment
1312          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1313          !!!next-input-character;          !!!next-input-character;
1314          redo A;          redo A;
1315        }        }
1316      } elsif ($self->{state} eq 'comment end') {      } elsif ($self->{state} == COMMENT_END_STATE) {
1317        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_input_character} == 0x003E) { # >
1318          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1319          !!!next-input-character;          !!!next-input-character;
1320    
1321          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1147  sub _get_next_token ($) { Line 1329  sub _get_next_token ($) {
1329          redo A;          redo A;
1330        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1331          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1332          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1333          ## reconsume          ## reconsume
1334    
1335          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1156  sub _get_next_token ($) { Line 1338  sub _get_next_token ($) {
1338        } else {        } else {
1339          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment');
1340          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment
1341          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1342          !!!next-input-character;          !!!next-input-character;
1343          redo A;          redo A;
1344        }        }
1345      } elsif ($self->{state} eq 'DOCTYPE') {      } elsif ($self->{state} == DOCTYPE_STATE) {
1346        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1347            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1348            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
1349            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1350            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
1351          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1352          !!!next-input-character;          !!!next-input-character;
1353          redo A;          redo A;
1354        } else {        } else {
1355          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
1356          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1357          ## reconsume          ## reconsume
1358          redo A;          redo A;
1359        }        }
1360      } elsif ($self->{state} eq 'before DOCTYPE name') {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
1361        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1362            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1363            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 1186  sub _get_next_token ($) { Line 1368  sub _get_next_token ($) {
1368          redo A;          redo A;
1369        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1370          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1371          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1372          !!!next-input-character;          !!!next-input-character;
1373    
1374          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect
1375    
1376          redo A;          redo A;
1377        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1378          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1379          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1380          ## reconsume          ## reconsume
1381    
1382          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect
1383    
1384          redo A;          redo A;
1385        } else {        } else {
1386          $self->{current_token}          $self->{current_token}
1387              = {type => 'DOCTYPE',              = {type => DOCTYPE_TOKEN,
1388                 name => chr ($self->{next_input_character}),                 name => chr ($self->{next_input_character}),
1389                 correct => 1};                 correct => 1};
1390  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
1391          $self->{state} = 'DOCTYPE name';          $self->{state} = DOCTYPE_NAME_STATE;
1392          !!!next-input-character;          !!!next-input-character;
1393          redo A;          redo A;
1394        }        }
1395      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
1396  ## ISSUE: Redundant "First," in the spec.  ## ISSUE: Redundant "First," in the spec.
1397        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1398            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1399            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
1400            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1401            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
1402          $self->{state} = 'after DOCTYPE name';          $self->{state} = AFTER_DOCTYPE_NAME_STATE;
1403          !!!next-input-character;          !!!next-input-character;
1404          redo A;          redo A;
1405        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1406          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1407          !!!next-input-character;          !!!next-input-character;
1408    
1409          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1229  sub _get_next_token ($) { Line 1411  sub _get_next_token ($) {
1411          redo A;          redo A;
1412        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1413          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1414          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1415          ## reconsume          ## reconsume
1416    
1417          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1243  sub _get_next_token ($) { Line 1425  sub _get_next_token ($) {
1425          !!!next-input-character;          !!!next-input-character;
1426          redo A;          redo A;
1427        }        }
1428      } elsif ($self->{state} eq 'after DOCTYPE name') {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
1429        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1430            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1431            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 1253  sub _get_next_token ($) { Line 1435  sub _get_next_token ($) {
1435          !!!next-input-character;          !!!next-input-character;
1436          redo A;          redo A;
1437        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1438          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1439          !!!next-input-character;          !!!next-input-character;
1440    
1441          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1261  sub _get_next_token ($) { Line 1443  sub _get_next_token ($) {
1443          redo A;          redo A;
1444        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1445          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1446          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1447          ## reconsume          ## reconsume
1448    
1449          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1285  sub _get_next_token ($) { Line 1467  sub _get_next_token ($) {
1467                  !!!next-input-character;                  !!!next-input-character;
1468                  if ($self->{next_input_character} == 0x0043 or # C                  if ($self->{next_input_character} == 0x0043 or # C
1469                      $self->{next_input_character} == 0x0063) { # c                      $self->{next_input_character} == 0x0063) { # c
1470                    $self->{state} = 'before DOCTYPE public identifier';                    $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1471                    !!!next-input-character;                    !!!next-input-character;
1472                    redo A;                    redo A;
1473                  }                  }
# Line 1312  sub _get_next_token ($) { Line 1494  sub _get_next_token ($) {
1494                  !!!next-input-character;                  !!!next-input-character;
1495                  if ($self->{next_input_character} == 0x004D or # M                  if ($self->{next_input_character} == 0x004D or # M
1496                      $self->{next_input_character} == 0x006D) { # m                      $self->{next_input_character} == 0x006D) { # m
1497                    $self->{state} = 'before DOCTYPE system identifier';                    $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1498                    !!!next-input-character;                    !!!next-input-character;
1499                    redo A;                    redo A;
1500                  }                  }
# Line 1328  sub _get_next_token ($) { Line 1510  sub _get_next_token ($) {
1510        }        }
1511    
1512        !!!parse-error (type => 'string after DOCTYPE name');        !!!parse-error (type => 'string after DOCTYPE name');
1513        $self->{state} = 'bogus DOCTYPE';        $self->{state} = BOGUS_DOCTYPE_STATE;
1514        # next-input-character is already done        # next-input-character is already done
1515        redo A;        redo A;
1516      } elsif ($self->{state} eq 'before DOCTYPE public identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
1517        if ({        if ({
1518              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1519              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1341  sub _get_next_token ($) { Line 1523  sub _get_next_token ($) {
1523          redo A;          redo A;
1524        } elsif ($self->{next_input_character} eq 0x0022) { # "        } elsif ($self->{next_input_character} eq 0x0022) { # "
1525          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1526          $self->{state} = 'DOCTYPE public identifier (double-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
1527          !!!next-input-character;          !!!next-input-character;
1528          redo A;          redo A;
1529        } elsif ($self->{next_input_character} eq 0x0027) { # '        } elsif ($self->{next_input_character} eq 0x0027) { # '
1530          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1531          $self->{state} = 'DOCTYPE public identifier (single-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
1532          !!!next-input-character;          !!!next-input-character;
1533          redo A;          redo A;
1534        } elsif ($self->{next_input_character} eq 0x003E) { # >        } elsif ($self->{next_input_character} eq 0x003E) { # >
1535          !!!parse-error (type => 'no PUBLIC literal');          !!!parse-error (type => 'no PUBLIC literal');
1536    
1537          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1538          !!!next-input-character;          !!!next-input-character;
1539    
1540          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1362  sub _get_next_token ($) { Line 1544  sub _get_next_token ($) {
1544        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1545          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1546    
1547          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1548          ## reconsume          ## reconsume
1549    
1550          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1371  sub _get_next_token ($) { Line 1553  sub _get_next_token ($) {
1553          redo A;          redo A;
1554        } else {        } else {
1555          !!!parse-error (type => 'string after PUBLIC');          !!!parse-error (type => 'string after PUBLIC');
1556          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1557          !!!next-input-character;          !!!next-input-character;
1558          redo A;          redo A;
1559        }        }
1560      } elsif ($self->{state} eq 'DOCTYPE public identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
1561        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_input_character} == 0x0022) { # "
1562          $self->{state} = 'after DOCTYPE public identifier';          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1563            !!!next-input-character;
1564            redo A;
1565          } elsif ($self->{next_input_character} == 0x003E) { # >
1566            !!!parse-error (type => 'unclosed PUBLIC literal');
1567    
1568            $self->{state} = DATA_STATE;
1569          !!!next-input-character;          !!!next-input-character;
1570    
1571            delete $self->{current_token}->{correct};
1572            !!!emit ($self->{current_token}); # DOCTYPE
1573    
1574          redo A;          redo A;
1575        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1576          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
1577    
1578          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1579          ## reconsume          ## reconsume
1580    
1581          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1397  sub _get_next_token ($) { Line 1589  sub _get_next_token ($) {
1589          !!!next-input-character;          !!!next-input-character;
1590          redo A;          redo A;
1591        }        }
1592      } elsif ($self->{state} eq 'DOCTYPE public identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
1593        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_input_character} == 0x0027) { # '
1594          $self->{state} = 'after DOCTYPE public identifier';          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1595          !!!next-input-character;          !!!next-input-character;
1596          redo A;          redo A;
1597          } elsif ($self->{next_input_character} == 0x003E) { # >
1598            !!!parse-error (type => 'unclosed PUBLIC literal');
1599    
1600            $self->{state} = DATA_STATE;
1601            !!!next-input-character;
1602    
1603            delete $self->{current_token}->{correct};
1604            !!!emit ($self->{current_token}); # DOCTYPE
1605    
1606            redo A;
1607        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1608          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
1609    
1610          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1611          ## reconsume          ## reconsume
1612    
1613          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1419  sub _get_next_token ($) { Line 1621  sub _get_next_token ($) {
1621          !!!next-input-character;          !!!next-input-character;
1622          redo A;          redo A;
1623        }        }
1624      } elsif ($self->{state} eq 'after DOCTYPE public identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
1625        if ({        if ({
1626              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1627              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1429  sub _get_next_token ($) { Line 1631  sub _get_next_token ($) {
1631          redo A;          redo A;
1632        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_input_character} == 0x0022) { # "
1633          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1634          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
1635          !!!next-input-character;          !!!next-input-character;
1636          redo A;          redo A;
1637        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_input_character} == 0x0027) { # '
1638          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1639          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
1640          !!!next-input-character;          !!!next-input-character;
1641          redo A;          redo A;
1642        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1643          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1644          !!!next-input-character;          !!!next-input-character;
1645    
1646          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1447  sub _get_next_token ($) { Line 1649  sub _get_next_token ($) {
1649        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1650          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1651    
1652          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1653          ## reconsume          ## reconsume
1654    
1655          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1456  sub _get_next_token ($) { Line 1658  sub _get_next_token ($) {
1658          redo A;          redo A;
1659        } else {        } else {
1660          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after PUBLIC literal');
1661          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1662          !!!next-input-character;          !!!next-input-character;
1663          redo A;          redo A;
1664        }        }
1665      } elsif ($self->{state} eq 'before DOCTYPE system identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
1666        if ({        if ({
1667              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1668              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1470  sub _get_next_token ($) { Line 1672  sub _get_next_token ($) {
1672          redo A;          redo A;
1673        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_input_character} == 0x0022) { # "
1674          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1675          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
1676          !!!next-input-character;          !!!next-input-character;
1677          redo A;          redo A;
1678        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_input_character} == 0x0027) { # '
1679          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1680          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
1681          !!!next-input-character;          !!!next-input-character;
1682          redo A;          redo A;
1683        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1684          !!!parse-error (type => 'no SYSTEM literal');          !!!parse-error (type => 'no SYSTEM literal');
1685          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1686          !!!next-input-character;          !!!next-input-character;
1687    
1688          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1490  sub _get_next_token ($) { Line 1692  sub _get_next_token ($) {
1692        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1693          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1694    
1695          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1696          ## reconsume          ## reconsume
1697    
1698          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1499  sub _get_next_token ($) { Line 1701  sub _get_next_token ($) {
1701          redo A;          redo A;
1702        } else {        } else {
1703          !!!parse-error (type => 'string after SYSTEM');          !!!parse-error (type => 'string after SYSTEM');
1704          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1705          !!!next-input-character;          !!!next-input-character;
1706          redo A;          redo A;
1707        }        }
1708      } elsif ($self->{state} eq 'DOCTYPE system identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
1709        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_input_character} == 0x0022) { # "
1710          $self->{state} = 'after DOCTYPE system identifier';          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1711            !!!next-input-character;
1712            redo A;
1713          } elsif ($self->{next_input_character} == 0x003E) { # >
1714            !!!parse-error (type => 'unclosed PUBLIC literal');
1715    
1716            $self->{state} = DATA_STATE;
1717          !!!next-input-character;          !!!next-input-character;
1718    
1719            delete $self->{current_token}->{correct};
1720            !!!emit ($self->{current_token}); # DOCTYPE
1721    
1722          redo A;          redo A;
1723        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1724          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
1725    
1726          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1727          ## reconsume          ## reconsume
1728    
1729          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1525  sub _get_next_token ($) { Line 1737  sub _get_next_token ($) {
1737          !!!next-input-character;          !!!next-input-character;
1738          redo A;          redo A;
1739        }        }
1740      } elsif ($self->{state} eq 'DOCTYPE system identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
1741        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_input_character} == 0x0027) { # '
1742          $self->{state} = 'after DOCTYPE system identifier';          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1743            !!!next-input-character;
1744            redo A;
1745          } elsif ($self->{next_input_character} == 0x003E) { # >
1746            !!!parse-error (type => 'unclosed PUBLIC literal');
1747    
1748            $self->{state} = DATA_STATE;
1749          !!!next-input-character;          !!!next-input-character;
1750    
1751            delete $self->{current_token}->{correct};
1752            !!!emit ($self->{current_token}); # DOCTYPE
1753    
1754          redo A;          redo A;
1755        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1756          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
1757    
1758          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1759          ## reconsume          ## reconsume
1760    
1761          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1547  sub _get_next_token ($) { Line 1769  sub _get_next_token ($) {
1769          !!!next-input-character;          !!!next-input-character;
1770          redo A;          redo A;
1771        }        }
1772      } elsif ($self->{state} eq 'after DOCTYPE system identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
1773        if ({        if ({
1774              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1775              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1556  sub _get_next_token ($) { Line 1778  sub _get_next_token ($) {
1778          !!!next-input-character;          !!!next-input-character;
1779          redo A;          redo A;
1780        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1781          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1782          !!!next-input-character;          !!!next-input-character;
1783    
1784          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1565  sub _get_next_token ($) { Line 1787  sub _get_next_token ($) {
1787        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1788          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1789    
1790          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1791          ## reconsume          ## reconsume
1792    
1793          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1574  sub _get_next_token ($) { Line 1796  sub _get_next_token ($) {
1796          redo A;          redo A;
1797        } else {        } else {
1798          !!!parse-error (type => 'string after SYSTEM literal');          !!!parse-error (type => 'string after SYSTEM literal');
1799          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1800          !!!next-input-character;          !!!next-input-character;
1801          redo A;          redo A;
1802        }        }
1803      } elsif ($self->{state} eq 'bogus DOCTYPE') {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
1804        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_input_character} == 0x003E) { # >
1805          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1806          !!!next-input-character;          !!!next-input-character;
1807    
1808          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1589  sub _get_next_token ($) { Line 1811  sub _get_next_token ($) {
1811          redo A;          redo A;
1812        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1813          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1814          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1815          ## reconsume          ## reconsume
1816    
1817          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1670  sub _tokenize_attempt_to_consume_an_enti Line 1892  sub _tokenize_attempt_to_consume_an_enti
1892            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
1893          }          }
1894    
1895          return {type => 'character', data => chr $code};          return {type => CHARACTER_TOKEN, data => chr $code,
1896                    has_reference => 1};
1897        } # X        } # X
1898      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_input_character} and
1899               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_input_character} <= 0x0039) { # 0..9
# Line 1705  sub _tokenize_attempt_to_consume_an_enti Line 1928  sub _tokenize_attempt_to_consume_an_enti
1928          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
1929        }        }
1930                
1931        return {type => 'character', data => chr $code};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1};
1932      } else {      } else {
1933        !!!parse-error (type => 'bare nero');        !!!parse-error (type => 'bare nero');
1934        !!!back-next-input-character ($self->{next_input_character});        !!!back-next-input-character ($self->{next_input_character});
# Line 1753  sub _tokenize_attempt_to_consume_an_enti Line 1976  sub _tokenize_attempt_to_consume_an_enti
1976      }      }
1977            
1978      if ($match > 0) {      if ($match > 0) {
1979        return {type => 'character', data => $value};        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};
1980      } elsif ($match < 0) {      } elsif ($match < 0) {
1981        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc');
1982        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
1983          return {type => 'character', data => '&'.$entity_name};          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};
1984        } else {        } else {
1985          return {type => 'character', data => $value};          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};
1986        }        }
1987      } else {      } else {
1988        !!!parse-error (type => 'bare ero');        !!!parse-error (type => 'bare ero');
1989        ## NOTE: No characters are consumed in the spec.        ## NOTE: "No characters are consumed" in the spec.
1990        return {type => 'character', data => '&'.$value};        return {type => CHARACTER_TOKEN, data => '&'.$value};
1991      }      }
1992    } else {    } else {
1993      ## no characters are consumed      ## no characters are consumed
# Line 1806  sub _construct_tree ($) { Line 2029  sub _construct_tree ($) {
2029        
2030    !!!next-token;    !!!next-token;
2031    
2032    $self->{insertion_mode} = 'before head';    $self->{insertion_mode} = BEFORE_HEAD_IM;
2033    undef $self->{form_element};    undef $self->{form_element};
2034    undef $self->{head_element};    undef $self->{head_element};
2035    $self->{open_elements} = [];    $self->{open_elements} = [];
# Line 1820  sub _construct_tree ($) { Line 2043  sub _construct_tree ($) {
2043  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
2044    my $self = shift;    my $self = shift;
2045    INITIAL: {    INITIAL: {
2046      if ($token->{type} eq 'DOCTYPE') {      if ($token->{type} == DOCTYPE_TOKEN) {
2047        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
2048        ## error, switch to a conformance checking mode for another        ## error, switch to a conformance checking mode for another
2049        ## language.        ## language.
# Line 1947  sub _tree_construction_initial ($) { Line 2170  sub _tree_construction_initial ($) {
2170        !!!next-token;        !!!next-token;
2171        return;        return;
2172      } elsif ({      } elsif ({
2173                'start tag' => 1,                START_TAG_TOKEN, 1,
2174                'end tag' => 1,                END_TAG_TOKEN, 1,
2175                'end-of-file' => 1,                END_OF_FILE_TOKEN, 1,
2176               }->{$token->{type}}) {               }->{$token->{type}}) {
2177        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE');
2178        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
2179        ## Go to the root element phase        ## Go to the root element phase
2180        ## reprocess        ## reprocess
2181        return;        return;
2182      } elsif ($token->{type} eq 'character') {      } elsif ($token->{type} == CHARACTER_TOKEN) {
2183        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2184          ## Ignore the token          ## Ignore the token
2185    
# Line 1972  sub _tree_construction_initial ($) { Line 2195  sub _tree_construction_initial ($) {
2195        ## Go to the root element phase        ## Go to the root element phase
2196        ## reprocess        ## reprocess
2197        return;        return;
2198      } elsif ($token->{type} eq 'comment') {      } elsif ($token->{type} == COMMENT_TOKEN) {
2199        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
2200        $self->{document}->append_child ($comment);        $self->{document}->append_child ($comment);
2201                
# Line 1980  sub _tree_construction_initial ($) { Line 2203  sub _tree_construction_initial ($) {
2203        !!!next-token;        !!!next-token;
2204        redo INITIAL;        redo INITIAL;
2205      } else {      } else {
2206        die "$0: $token->{type}: Unknown token";        die "$0: $token->{type}: Unknown token type";
2207      }      }
2208    } # INITIAL    } # INITIAL
2209  } # _tree_construction_initial  } # _tree_construction_initial
# Line 1989  sub _tree_construction_root_element ($) Line 2212  sub _tree_construction_root_element ($)
2212    my $self = shift;    my $self = shift;
2213        
2214    B: {    B: {
2215        if ($token->{type} eq 'DOCTYPE') {        if ($token->{type} == DOCTYPE_TOKEN) {
2216          !!!parse-error (type => 'in html:#DOCTYPE');          !!!parse-error (type => 'in html:#DOCTYPE');
2217          ## Ignore the token          ## Ignore the token
2218          ## Stay in the phase          ## Stay in the phase
2219          !!!next-token;          !!!next-token;
2220          redo B;          redo B;
2221        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{type} == COMMENT_TOKEN) {
2222          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
2223          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
2224          ## Stay in the phase          ## Stay in the phase
2225          !!!next-token;          !!!next-token;
2226          redo B;          redo B;
2227        } elsif ($token->{type} eq 'character') {        } elsif ($token->{type} == CHARACTER_TOKEN) {
2228          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2229            ## Ignore the token.            ## Ignore the token.
2230    
# Line 2011  sub _tree_construction_root_element ($) Line 2234  sub _tree_construction_root_element ($)
2234              redo B;              redo B;
2235            }            }
2236          }          }
2237    
2238            $self->{application_cache_selection}->(undef);
2239    
2240            #
2241          } elsif ($token->{type} == START_TAG_TOKEN) {
2242            if ($token->{tag_name} eq 'html' and
2243                $token->{attributes}->{manifest}) {
2244              $self->{application_cache_selection}
2245                   ->($token->{attributes}->{manifest}->{value});
2246              ## ISSUE: No relative reference resolution?
2247            } else {
2248              $self->{application_cache_selection}->(undef);
2249            }
2250    
2251            ## ISSUE: There is an issue in the spec
2252          #          #
2253        } elsif ({        } elsif ({
2254                  'start tag' => 1,                  END_TAG_TOKEN, 1,
2255                  'end tag' => 1,                  END_OF_FILE_TOKEN, 1,
                 'end-of-file' => 1,  
2256                 }->{$token->{type}}) {                 }->{$token->{type}}) {
2257            $self->{application_cache_selection}->(undef);
2258    
2259          ## ISSUE: There is an issue in the spec          ## ISSUE: There is an issue in the spec
2260          #          #
2261        } else {        } else {
2262          die "$0: $token->{type}: Unknown token";          die "$0: $token->{type}: Unknown token type";
2263        }        }
2264    
2265        my $root_element; !!!create-element ($root_element, 'html');        my $root_element; !!!create-element ($root_element, 'html');
2266        $self->{document}->append_child ($root_element);        $self->{document}->append_child ($root_element);
2267        push @{$self->{open_elements}}, [$root_element, 'html'];        push @{$self->{open_elements}}, [$root_element, 'html'];
# Line 2062  sub _reset_insertion_mode ($) { Line 2302  sub _reset_insertion_mode ($) {
2302            
2303        ## Step 4..13        ## Step 4..13
2304        my $new_mode = {        my $new_mode = {
2305                        select => 'in select',                        select => IN_SELECT_IM,
2306                        td => 'in cell',                        td => IN_CELL_IM,
2307                        th => 'in cell',                        th => IN_CELL_IM,
2308                        tr => 'in row',                        tr => IN_ROW_IM,
2309                        tbody => 'in table body',                        tbody => IN_TABLE_BODY_IM,
2310                        thead => 'in table body',                        thead => IN_TABLE_BODY_IM,
2311                        tfoot => 'in table body',                        tfoot => IN_TABLE_BODY_IM,
2312                        caption => 'in caption',                        caption => IN_CAPTION_IM,
2313                        colgroup => 'in column group',                        colgroup => IN_COLUMN_GROUP_IM,
2314                        table => 'in table',                        table => IN_TABLE_IM,
2315                        head => 'in body', # not in head!                        head => IN_BODY_IM, # not in head!
2316                        body => 'in body',                        body => IN_BODY_IM,
2317                        frameset => 'in frameset',                        frameset => IN_FRAMESET_IM,
2318                       }->{$node->[1]};                       }->{$node->[1]};
2319        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
2320                
2321        ## Step 14        ## Step 14
2322        if ($node->[1] eq 'html') {        if ($node->[1] eq 'html') {
2323          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
2324            $self->{insertion_mode} = 'before head';            $self->{insertion_mode} = BEFORE_HEAD_IM;
2325          } else {          } else {
2326            $self->{insertion_mode} = 'after head';            $self->{insertion_mode} = AFTER_HEAD_IM;
2327          }          }
2328          return;          return;
2329        }        }
2330                
2331        ## Step 15        ## Step 15
2332        $self->{insertion_mode} = 'in body' and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
2333                
2334        ## Step 16        ## Step 16
2335        $i--;        $i--;
# Line 2203  sub _tree_construction_main ($) { Line 2443  sub _tree_construction_main ($) {
2443      ## Step 4      ## Step 4
2444      my $text = '';      my $text = '';
2445      !!!next-token;      !!!next-token;
2446      while ($token->{type} eq 'character') { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
2447        $text .= $token->{data};        $text .= $token->{data};
2448        !!!next-token;        !!!next-token;
2449      }      }
# Line 2218  sub _tree_construction_main ($) { Line 2458  sub _tree_construction_main ($) {
2458      $self->{content_model} = PCDATA_CONTENT_MODEL;      $self->{content_model} = PCDATA_CONTENT_MODEL;
2459    
2460      ## Step 7      ## Step 7
2461      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) {
2462        ## Ignore the token        ## Ignore the token
2463      } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {      } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {
2464        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!parse-error (type => 'in CDATA:#'.$token->{type});
# Line 2241  sub _tree_construction_main ($) { Line 2481  sub _tree_construction_main ($) {
2481            
2482      my $text = '';      my $text = '';
2483      !!!next-token;      !!!next-token;
2484      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) {
2485        $text .= $token->{data};        $text .= $token->{data};
2486        !!!next-token;        !!!next-token;
2487      } # 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 2491  sub _tree_construction_main ($) {
2491                                
2492      $self->{content_model} = PCDATA_CONTENT_MODEL;      $self->{content_model} = PCDATA_CONTENT_MODEL;
2493    
2494      if ($token->{type} eq 'end tag' and      if ($token->{type} == END_TAG_TOKEN and
2495          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
2496        ## Ignore the token        ## Ignore the token
2497      } else {      } else {
# Line 2497  sub _tree_construction_main ($) { Line 2737  sub _tree_construction_main ($) {
2737    my $insert;    my $insert;
2738    
2739    B: {    B: {
2740      if ($token->{type} eq 'DOCTYPE') {      if ($token->{type} == DOCTYPE_TOKEN) {
2741        !!!parse-error (type => 'DOCTYPE in the middle');        !!!parse-error (type => 'DOCTYPE in the middle');
2742        ## Ignore the token        ## Ignore the token
2743        ## Stay in the phase        ## Stay in the phase
2744        !!!next-token;        !!!next-token;
2745        redo B;        redo B;
2746      } elsif ($token->{type} eq 'end-of-file') {      } elsif ($token->{type} == END_OF_FILE_TOKEN) {
2747        if ($self->{insertion_mode} eq 'after html body' or        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
           $self->{insertion_mode} eq 'after html frameset') {  
2748          #          #
2749        } else {        } else {
2750          ## Generate implied end tags          ## Generate implied end tags
# Line 2514  sub _tree_construction_main ($) { Line 2753  sub _tree_construction_main ($) {
2753               tbody => 1, tfoot=> 1, thead => 1,               tbody => 1, tfoot=> 1, thead => 1,
2754              }->{$self->{open_elements}->[-1]->[1]}) {              }->{$self->{open_elements}->[-1]->[1]}) {
2755            !!!back-token;            !!!back-token;
2756            $token = {type => 'end tag', tag_name => $self->{open_elements}->[-1]->[1]};            $token = {type => END_TAG_TOKEN, tag_name => $self->{open_elements}->[-1]->[1]};
2757            redo B;            redo B;
2758          }          }
2759                    
# Line 2532  sub _tree_construction_main ($) { Line 2771  sub _tree_construction_main ($) {
2771    
2772        ## Stop parsing        ## Stop parsing
2773        last B;        last B;
2774      } elsif ($token->{type} eq 'start tag' and      } elsif ($token->{type} == START_TAG_TOKEN and
2775               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
2776        if ($self->{insertion_mode} eq 'after html body') {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
2777          ## Turn into the main phase          ## Turn into the main phase
2778          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html');
2779          $self->{insertion_mode} = 'after body';          $self->{insertion_mode} = AFTER_BODY_IM;
2780        } elsif ($self->{insertion_mode} eq 'after html frameset') {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
2781          ## Turn into the main phase          ## Turn into the main phase
2782          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html');
2783          $self->{insertion_mode} = 'after frameset';          $self->{insertion_mode} = AFTER_FRAMESET_IM;
2784        }        }
2785    
2786  ## ISSUE: "aa<html>" is not a parse error.  ## ISSUE: "aa<html>" is not a parse error.
# Line 2559  sub _tree_construction_main ($) { Line 2798  sub _tree_construction_main ($) {
2798        }        }
2799        !!!next-token;        !!!next-token;
2800        redo B;        redo B;
2801      } elsif ($token->{type} eq 'comment') {      } elsif ($token->{type} == COMMENT_TOKEN) {
2802        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
2803        if ($self->{insertion_mode} eq 'after html body' or        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
           $self->{insertion_mode} eq 'after html frameset') {  
2804          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
2805        } elsif ($self->{insertion_mode} eq 'after body') {        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
2806          $self->{open_elements}->[0]->[0]->append_child ($comment);          $self->{open_elements}->[0]->[0]->append_child ($comment);
2807        } else {        } else {
2808          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
2809        }        }
2810        !!!next-token;        !!!next-token;
2811        redo B;        redo B;
2812      } elsif ($self->{insertion_mode} eq 'in head' or      } elsif ($self->{insertion_mode} & HEAD_IMS) {
2813               $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') {  
2814          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
2815            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
2816            unless (length $token->{data}) {            unless (length $token->{data}) {
# Line 2584  sub _tree_construction_main ($) { Line 2819  sub _tree_construction_main ($) {
2819            }            }
2820          }          }
2821    
2822          if ($self->{insertion_mode} eq 'before head') {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2823            ## As if <head>            ## As if <head>
2824            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, 'head');
2825            $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 2829  sub _tree_construction_main ($) {
2829            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
2830    
2831            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
2832          } elsif ($self->{insertion_mode} eq 'in head noscript') {          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2833            ## As if </noscript>            ## As if </noscript>
2834            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
2835            !!!parse-error (type => 'in noscript:#character');            !!!parse-error (type => 'in noscript:#character');
# Line 2604  sub _tree_construction_main ($) { Line 2839  sub _tree_construction_main ($) {
2839            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
2840    
2841            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
2842          } elsif ($self->{insertion_mode} eq 'in head') {          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2843            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
2844    
2845            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
# Line 2613  sub _tree_construction_main ($) { Line 2848  sub _tree_construction_main ($) {
2848              ## "after head" insertion mode              ## "after head" insertion mode
2849              ## As if <body>              ## As if <body>
2850              !!!insert-element ('body');              !!!insert-element ('body');
2851              $self->{insertion_mode} = 'in body';              $self->{insertion_mode} = IN_BODY_IM;
2852              ## reprocess              ## reprocess
2853              redo B;              redo B;
2854            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} == START_TAG_TOKEN) {
2855              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
2856                if ($self->{insertion_mode} eq 'before head') {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2857                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});
2858                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2859                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];
2860                  $self->{insertion_mode} = 'in head';                  $self->{insertion_mode} = IN_HEAD_IM;
2861                  !!!next-token;                  !!!next-token;
2862                  redo B;                  redo B;
2863                } elsif ($self->{insertion_mode} ne 'after head') {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2864                    #
2865                  } else {
2866                  !!!parse-error (type => 'in head:head'); # or in head noscript                  !!!parse-error (type => 'in head:head'); # or in head noscript
2867                  ## Ignore the token                  ## Ignore the token
2868                  !!!next-token;                  !!!next-token;
2869                  redo B;                  redo B;
               } else {  
                 #  
2870                }                }
2871              } elsif ($self->{insertion_mode} eq 'before head') {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2872                ## As if <head>                ## As if <head>
2873                !!!create-element ($self->{head_element}, 'head');                !!!create-element ($self->{head_element}, 'head');
2874                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2875                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2876    
2877                $self->{insertion_mode} = 'in head';                $self->{insertion_mode} = IN_HEAD_IM;
2878                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
2879              }              }
2880    
2881              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
2882                if ($self->{insertion_mode} eq 'in head noscript') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2883                  ## As if </noscript>                  ## As if </noscript>
2884                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
2885                  !!!parse-error (type => 'in noscript:base');                  !!!parse-error (type => 'in noscript:base');
2886                                
2887                  $self->{insertion_mode} = 'in head';                  $self->{insertion_mode} = IN_HEAD_IM;
2888                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
2889                }                }
2890    
2891                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
2892                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2893                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2894                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2895                }                }
2896                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
2897                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2898                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2899                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2900                !!!next-token;                !!!next-token;
2901                redo B;                redo B;
2902              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
2903                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
2904                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2905                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2906                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2907                }                }
2908                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
2909                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2910                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2911                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2912                !!!next-token;                !!!next-token;
2913                redo B;                redo B;
2914              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
2915                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
2916                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2917                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2918                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2919                }                }
2920                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
2921                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2922    
2923                unless ($self->{confident}) {                unless ($self->{confident}) {
                 my $charset;  
2924                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) { ## TODO: And if supported
2925                    $charset = $token->{attributes}->{charset}->{value};                    $self->{change_encoding}
2926                  }                        ->($self, $token->{attributes}->{charset}->{value});
2927                  if ($token->{attributes}->{'http-equiv'}) {                    
2928                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
2929                          ->set_user_data (manakai_has_reference =>
2930                                               $token->{attributes}->{charset}
2931                                                   ->{has_reference});
2932                    } elsif ($token->{attributes}->{content}) {
2933                    ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.                    ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
2934                    if ($token->{attributes}->{'http-equiv'}->{value}                    if ($token->{attributes}->{content}->{value}
2935                        =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
2936                              [\x09-\x0D\x20]*=
2937                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
2938                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
2939                      $charset = defined $1 ? $1 : defined $2 ? $2 : $3;                      $self->{change_encoding}
2940                    } ## TODO: And if supported                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);
2941                        $meta_el->[0]->get_attribute_node_ns (undef, 'content')
2942                            ->set_user_data (manakai_has_reference =>
2943                                                 $token->{attributes}->{content}
2944                                                       ->{has_reference});
2945                      }
2946                    }
2947                  } else {
2948                    if ($token->{attributes}->{charset}) {
2949                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
2950                          ->set_user_data (manakai_has_reference =>
2951                                               $token->{attributes}->{charset}
2952                                                   ->{has_reference});
2953                    }
2954                    if ($token->{attributes}->{content}) {
2955                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
2956                          ->set_user_data (manakai_has_reference =>
2957                                               $token->{attributes}->{content}
2958                                                   ->{has_reference});
2959                  }                  }
                 ## TODO: Change the encoding  
2960                }                }
2961    
               ## TODO: Extracting |charset| from |meta|.  
2962                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2963                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2964                !!!next-token;                !!!next-token;
2965                redo B;                redo B;
2966              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
2967                if ($self->{insertion_mode} eq 'in head noscript') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2968                  ## As if </noscript>                  ## As if </noscript>
2969                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
2970                  !!!parse-error (type => 'in noscript:title');                  !!!parse-error (type => 'in noscript:title');
2971                                
2972                  $self->{insertion_mode} = 'in head';                  $self->{insertion_mode} = IN_HEAD_IM;
2973                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
2974                } elsif ($self->{insertion_mode} eq 'after head') {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2975                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2976                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2977                }                }
# Line 2726  sub _tree_construction_main ($) { Line 2982  sub _tree_construction_main ($) {
2982                $parse_rcdata->(RCDATA_CONTENT_MODEL,                $parse_rcdata->(RCDATA_CONTENT_MODEL,
2983                                sub { $parent->append_child ($_[0]) });                                sub { $parent->append_child ($_[0]) });
2984                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2985                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2986                redo B;                redo B;
2987              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style') {
2988                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
2989                ## insertion mode 'in head')                ## insertion mode IN_HEAD_IM)
2990                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
2991                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2992                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2993                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2994                }                }
2995                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
2996                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2997                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2998                redo B;                redo B;
2999              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
3000                if ($self->{insertion_mode} eq 'in head') {                if ($self->{insertion_mode} == IN_HEAD_IM) {
3001                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
3002                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3003                  $self->{insertion_mode} = 'in head noscript';                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
3004                  !!!next-token;                  !!!next-token;
3005                  redo B;                  redo B;
3006                } elsif ($self->{insertion_mode} eq 'in head noscript') {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3007                  !!!parse-error (type => 'in noscript:noscript');                  !!!parse-error (type => 'in noscript:noscript');
3008                  ## Ignore the token                  ## Ignore the token
3009                  !!!next-token;                  !!!next-token;
# Line 2756  sub _tree_construction_main ($) { Line 3012  sub _tree_construction_main ($) {
3012                  #                  #
3013                }                }
3014              } elsif ($token->{tag_name} eq 'script') {              } elsif ($token->{tag_name} eq 'script') {
3015                if ($self->{insertion_mode} eq 'in head noscript') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3016                  ## As if </noscript>                  ## As if </noscript>
3017                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3018                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:script');
3019                                
3020                  $self->{insertion_mode} = 'in head';                  $self->{insertion_mode} = IN_HEAD_IM;
3021                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3022                } elsif ($self->{insertion_mode} eq 'after head') {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3023                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3024                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3025                }                }
# Line 2771  sub _tree_construction_main ($) { Line 3027  sub _tree_construction_main ($) {
3027                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3028                $script_start_tag->($insert_to_current);                $script_start_tag->($insert_to_current);
3029                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
3030                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3031                redo B;                redo B;
3032              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
3033                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
3034                if ($self->{insertion_mode} eq 'in head noscript') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3035                  ## As if </noscript>                  ## As if </noscript>
3036                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3037                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});
# Line 2785  sub _tree_construction_main ($) { Line 3041  sub _tree_construction_main ($) {
3041                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3042                                    
3043                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
3044                } elsif ($self->{insertion_mode} eq 'in head') {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3045                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3046                                    
3047                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
# Line 2793  sub _tree_construction_main ($) { Line 3049  sub _tree_construction_main ($) {
3049    
3050                ## "after head" insertion mode                ## "after head" insertion mode
3051                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
3052                $self->{insertion_mode} = 'in '.$token->{tag_name};                if ($token->{tag_name} eq 'body') {
3053                    $self->{insertion_mode} = IN_BODY_IM;
3054                  } elsif ($token->{tag_name} eq 'frameset') {
3055                    $self->{insertion_mode} = IN_FRAMESET_IM;
3056                  } else {
3057                    die "$0: tag name: $self->{tag_name}";
3058                  }
3059                !!!next-token;                !!!next-token;
3060                redo B;                redo B;
3061              } else {              } else {
3062                #                #
3063              }              }
3064    
3065              if ($self->{insertion_mode} eq 'in head noscript') {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3066                ## As if </noscript>                ## As if </noscript>
3067                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3068                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
# Line 2810  sub _tree_construction_main ($) { Line 3072  sub _tree_construction_main ($) {
3072                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3073    
3074                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
3075              } elsif ($self->{insertion_mode} eq 'in head') {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3076                ## As if </head>                ## As if </head>
3077                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3078    
# Line 2820  sub _tree_construction_main ($) { Line 3082  sub _tree_construction_main ($) {
3082              ## "after head" insertion mode              ## "after head" insertion mode
3083              ## As if <body>              ## As if <body>
3084              !!!insert-element ('body');              !!!insert-element ('body');
3085              $self->{insertion_mode} = 'in body';              $self->{insertion_mode} = IN_BODY_IM;
3086              ## reprocess              ## reprocess
3087              redo B;              redo B;
3088            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
3089              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
3090                if ($self->{insertion_mode} eq 'before head') {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3091                  ## As if <head>                  ## As if <head>
3092                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, 'head');
3093                  $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 3095  sub _tree_construction_main ($) {
3095    
3096                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3097                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3098                  $self->{insertion_mode} = 'after head';                  $self->{insertion_mode} = AFTER_HEAD_IM;
3099                  !!!next-token;                  !!!next-token;
3100                  redo B;                  redo B;
3101                } elsif ($self->{insertion_mode} eq 'in head noscript') {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3102                  ## As if </noscript>                  ## As if </noscript>
3103                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3104                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:script');
3105                                    
3106                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3107                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3108                  $self->{insertion_mode} = 'after head';                  $self->{insertion_mode} = AFTER_HEAD_IM;
3109                  !!!next-token;                  !!!next-token;
3110                  redo B;                  redo B;
3111                } elsif ($self->{insertion_mode} eq 'in head') {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3112                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3113                  $self->{insertion_mode} = 'after head';                  $self->{insertion_mode} = AFTER_HEAD_IM;
3114                  !!!next-token;                  !!!next-token;
3115                  redo B;                  redo B;
3116                } else {                } else {
3117                  #                  #
3118                }                }
3119              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
3120                if ($self->{insertion_mode} eq 'in head noscript') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3121                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3122                  $self->{insertion_mode} = 'in head';                  $self->{insertion_mode} = IN_HEAD_IM;
3123                  !!!next-token;                  !!!next-token;
3124                  redo B;                  redo B;
3125                } elsif ($self->{insertion_mode} eq 'before head') {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3126                  !!!parse-error (type => 'unmatched end tag:noscript');                  !!!parse-error (type => 'unmatched end tag:noscript');
3127                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
3128                  !!!next-token;                  !!!next-token;
# Line 2871  sub _tree_construction_main ($) { Line 3133  sub _tree_construction_main ($) {
3133              } elsif ({              } elsif ({
3134                        body => 1, html => 1,                        body => 1, html => 1,
3135                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3136                if ($self->{insertion_mode} eq 'before head') {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3137                  ## As if <head>                  ## As if <head>
3138                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, 'head');
3139                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3140                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3141    
3142                  $self->{insertion_mode} = 'in head';                  $self->{insertion_mode} = IN_HEAD_IM;
3143                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3144                } elsif ($self->{insertion_mode} eq 'in head noscript') {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3145                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3146                  ## Ignore the token                  ## Ignore the token
3147                  !!!next-token;                  !!!next-token;
# Line 2890  sub _tree_construction_main ($) { Line 3152  sub _tree_construction_main ($) {
3152              } elsif ({              } elsif ({
3153                        p => 1, br => 1,                        p => 1, br => 1,
3154                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3155                if ($self->{insertion_mode} eq 'before head') {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3156                  ## As if <head>                  ## As if <head>
3157                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, 'head');
3158                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3159                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3160    
3161                  $self->{insertion_mode} = 'in head';                  $self->{insertion_mode} = IN_HEAD_IM;
3162                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3163                }                }
3164    
3165                #                #
3166              } else {              } else {
3167                if ($self->{insertion_mode} ne 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3168                    #
3169                  } else {
3170                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3171                  ## Ignore the token                  ## Ignore the token
3172                  !!!next-token;                  !!!next-token;
3173                  redo B;                  redo B;
               } else {  
                 #  
3174                }                }
3175              }              }
3176    
3177              if ($self->{insertion_mode} eq 'in head noscript') {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3178                ## As if </noscript>                ## As if </noscript>
3179                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3180                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
# Line 2922  sub _tree_construction_main ($) { Line 3184  sub _tree_construction_main ($) {
3184                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3185    
3186                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
3187              } elsif ($self->{insertion_mode} eq 'in head') {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3188                ## As if </head>                ## As if </head>
3189                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3190    
3191                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
3192              } elsif ($self->{insertion_mode} eq 'before head') {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3193                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3194                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
3195                !!!next-token;                !!!next-token;
# Line 2937  sub _tree_construction_main ($) { Line 3199  sub _tree_construction_main ($) {
3199              ## "after head" insertion mode              ## "after head" insertion mode
3200              ## As if <body>              ## As if <body>
3201              !!!insert-element ('body');              !!!insert-element ('body');
3202              $self->{insertion_mode} = 'in body';              $self->{insertion_mode} = IN_BODY_IM;
3203              ## reprocess              ## reprocess
3204              redo B;              redo B;
3205            } else {            } else {
# Line 2945  sub _tree_construction_main ($) { Line 3207  sub _tree_construction_main ($) {
3207            }            }
3208    
3209            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
3210      } elsif ($self->{insertion_mode} eq 'in body' or      } elsif ($self->{insertion_mode} & BODY_IMS) {
3211               $self->{insertion_mode} eq 'in cell' or            if ($token->{type} == CHARACTER_TOKEN) {
              $self->{insertion_mode} eq 'in caption') {  
           if ($token->{type} eq 'character') {  
3212              ## NOTE: There is a code clone of "character in body".              ## NOTE: There is a code clone of "character in body".
3213              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
3214                            
# Line 2956  sub _tree_construction_main ($) { Line 3216  sub _tree_construction_main ($) {
3216    
3217              !!!next-token;              !!!next-token;
3218              redo B;              redo B;
3219            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} == START_TAG_TOKEN) {
3220              if ({              if ({
3221                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
3222                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
3223                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
3224                if ($self->{insertion_mode} eq 'in cell') {                if ($self->{insertion_mode} == IN_CELL_IM) {
3225                  ## have an element in table scope                  ## have an element in table scope
3226                  my $tn;                  my $tn;
3227                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 2984  sub _tree_construction_main ($) { Line 3244  sub _tree_construction_main ($) {
3244                                    
3245                  ## Close the cell                  ## Close the cell
3246                  !!!back-token; # <?>                  !!!back-token; # <?>
3247                  $token = {type => 'end tag', tag_name => $tn};                  $token = {type => END_TAG_TOKEN, tag_name => $tn};
3248                  redo B;                  redo B;
3249                } elsif ($self->{insertion_mode} eq 'in caption') {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3250                  !!!parse-error (type => 'not closed:caption');                  !!!parse-error (type => 'not closed:caption');
3251                                    
3252                  ## As if </caption>                  ## As if </caption>
# Line 3017  sub _tree_construction_main ($) { Line 3277  sub _tree_construction_main ($) {
3277                       tbody => 1, tfoot=> 1, thead => 1,                       tbody => 1, tfoot=> 1, thead => 1,
3278                      }->{$self->{open_elements}->[-1]->[1]}) {                      }->{$self->{open_elements}->[-1]->[1]}) {
3279                    !!!back-token; # <?>                    !!!back-token; # <?>
3280                    $token = {type => 'end tag', tag_name => 'caption'};                    $token = {type => END_TAG_TOKEN, tag_name => 'caption'};
3281                    !!!back-token;                    !!!back-token;
3282                    $token = {type => 'end tag',                    $token = {type => END_TAG_TOKEN,
3283                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3284                    redo B;                    redo B;
3285                  }                  }
# Line 3032  sub _tree_construction_main ($) { Line 3292  sub _tree_construction_main ($) {
3292                                    
3293                  $clear_up_to_marker->();                  $clear_up_to_marker->();
3294                                    
3295                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
3296                                    
3297                  ## reprocess                  ## reprocess
3298                  redo B;                  redo B;
# Line 3042  sub _tree_construction_main ($) { Line 3302  sub _tree_construction_main ($) {
3302              } else {              } else {
3303                #                #
3304              }              }
3305            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
3306              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
3307                if ($self->{insertion_mode} eq 'in cell') {                if ($self->{insertion_mode} == IN_CELL_IM) {
3308                  ## have an element in table scope                  ## have an element in table scope
3309                  my $i;                  my $i;
3310                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 3074  sub _tree_construction_main ($) { Line 3334  sub _tree_construction_main ($) {
3334                       tbody => 1, tfoot=> 1, thead => 1,                       tbody => 1, tfoot=> 1, thead => 1,
3335                      }->{$self->{open_elements}->[-1]->[1]}) {                      }->{$self->{open_elements}->[-1]->[1]}) {
3336                    !!!back-token;                    !!!back-token;
3337                    $token = {type => 'end tag',                    $token = {type => END_TAG_TOKEN,
3338                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3339                    redo B;                    redo B;
3340                  }                  }
# Line 3087  sub _tree_construction_main ($) { Line 3347  sub _tree_construction_main ($) {
3347                                    
3348                  $clear_up_to_marker->();                  $clear_up_to_marker->();
3349                                    
3350                  $self->{insertion_mode} = 'in row';                  $self->{insertion_mode} = IN_ROW_IM;
3351                                    
3352                  !!!next-token;                  !!!next-token;
3353                  redo B;                  redo B;
3354                } elsif ($self->{insertion_mode} eq 'in caption') {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3355                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3356                  ## Ignore the token                  ## Ignore the token
3357                  !!!next-token;                  !!!next-token;
# Line 3100  sub _tree_construction_main ($) { Line 3360  sub _tree_construction_main ($) {
3360                  #                  #
3361                }                }
3362              } elsif ($token->{tag_name} eq 'caption') {              } elsif ($token->{tag_name} eq 'caption') {
3363                if ($self->{insertion_mode} eq 'in caption') {                if ($self->{insertion_mode} == IN_CAPTION_IM) {
3364                  ## have a table element in table scope                  ## have a table element in table scope
3365                  my $i;                  my $i;
3366                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 3128  sub _tree_construction_main ($) { Line 3388  sub _tree_construction_main ($) {
3388                       tbody => 1, tfoot=> 1, thead => 1,                       tbody => 1, tfoot=> 1, thead => 1,
3389                      }->{$self->{open_elements}->[-1]->[1]}) {                      }->{$self->{open_elements}->[-1]->[1]}) {
3390                    !!!back-token;                    !!!back-token;
3391                    $token = {type => 'end tag',                    $token = {type => END_TAG_TOKEN,
3392                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3393                    redo B;                    redo B;
3394                  }                  }
# Line 3141  sub _tree_construction_main ($) { Line 3401  sub _tree_construction_main ($) {
3401                                    
3402                  $clear_up_to_marker->();                  $clear_up_to_marker->();
3403                                    
3404                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
3405                                    
3406                  !!!next-token;                  !!!next-token;
3407                  redo B;                  redo B;
3408                } elsif ($self->{insertion_mode} eq 'in cell') {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
3409                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3410                  ## Ignore the token                  ## Ignore the token
3411                  !!!next-token;                  !!!next-token;
# Line 3157  sub _tree_construction_main ($) { Line 3417  sub _tree_construction_main ($) {
3417                        table => 1, tbody => 1, tfoot => 1,                        table => 1, tbody => 1, tfoot => 1,
3418                        thead => 1, tr => 1,                        thead => 1, tr => 1,
3419                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
3420                       $self->{insertion_mode} eq 'in cell') {                       $self->{insertion_mode} == IN_CELL_IM) {
3421                ## have an element in table scope                ## have an element in table scope
3422                my $i;                my $i;
3423                my $tn;                my $tn;
# Line 3185  sub _tree_construction_main ($) { Line 3445  sub _tree_construction_main ($) {
3445    
3446                ## Close the cell                ## Close the cell
3447                !!!back-token; # </?>                !!!back-token; # </?>
3448                $token = {type => 'end tag', tag_name => $tn};                $token = {type => END_TAG_TOKEN, tag_name => $tn};
3449                redo B;                redo B;
3450              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
3451                       $self->{insertion_mode} eq 'in caption') {                       $self->{insertion_mode} == IN_CAPTION_IM) {
3452                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption');
3453    
3454                ## As if </caption>                ## As if </caption>
# Line 3219  sub _tree_construction_main ($) { Line 3479  sub _tree_construction_main ($) {
3479                     tbody => 1, tfoot=> 1, thead => 1,                     tbody => 1, tfoot=> 1, thead => 1,
3480                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
3481                  !!!back-token; # </table>                  !!!back-token; # </table>
3482                  $token = {type => 'end tag', tag_name => 'caption'};                  $token = {type => END_TAG_TOKEN, tag_name => 'caption'};
3483                  !!!back-token;                  !!!back-token;
3484                  $token = {type => 'end tag',                  $token = {type => END_TAG_TOKEN,
3485                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3486                  redo B;                  redo B;
3487                }                }
# Line 3234  sub _tree_construction_main ($) { Line 3494  sub _tree_construction_main ($) {
3494    
3495                $clear_up_to_marker->();                $clear_up_to_marker->();
3496    
3497                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
3498    
3499                ## reprocess                ## reprocess
3500                redo B;                redo B;
3501              } elsif ({              } elsif ({
3502                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
3503                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3504                if ($self->{insertion_mode} eq 'in cell' or                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
                   $self->{insertion_mode} eq 'in caption') {  
3505                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3506                  ## Ignore the token                  ## Ignore the token
3507                  !!!next-token;                  !!!next-token;
# Line 3254  sub _tree_construction_main ($) { Line 3513  sub _tree_construction_main ($) {
3513                        tbody => 1, tfoot => 1,                        tbody => 1, tfoot => 1,
3514                        thead => 1, tr => 1,                        thead => 1, tr => 1,
3515                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
3516                       $self->{insertion_mode} eq 'in caption') {                       $self->{insertion_mode} == IN_CAPTION_IM) {
3517                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3518                ## Ignore the token                ## Ignore the token
3519                !!!next-token;                !!!next-token;
# Line 3268  sub _tree_construction_main ($) { Line 3527  sub _tree_construction_main ($) {
3527    
3528        $insert = $insert_to_current;        $insert = $insert_to_current;
3529        #        #
3530      } elsif ($self->{insertion_mode} eq 'in row' or      } elsif ($self->{insertion_mode} & TABLE_IMS) {
3531               $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.  
3532              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3533                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3534                                
# Line 3329  sub _tree_construction_main ($) { Line 3585  sub _tree_construction_main ($) {
3585                            
3586              !!!next-token;              !!!next-token;
3587              redo B;              redo B;
3588            } elsif ($token->{type} eq 'start tag') {        } elsif ($token->{type} == START_TAG_TOKEN) {
3589              if ({              if ({
3590                   tr => ($self->{insertion_mode} ne 'in row'),                   tr => ($self->{insertion_mode} != IN_ROW_IM),
3591                   th => 1, td => 1,                   th => 1, td => 1,
3592                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
3593                if ($self->{insertion_mode} eq 'in table') {                if ($self->{insertion_mode} == IN_TABLE_IM) {
3594                  ## Clear back to table context                  ## Clear back to table context
3595                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while ($self->{open_elements}->[-1]->[1] ne 'table' and
3596                         $self->{open_elements}->[-1]->[1] ne 'html') {                         $self->{open_elements}->[-1]->[1] ne 'html') {
# Line 3343  sub _tree_construction_main ($) { Line 3599  sub _tree_construction_main ($) {
3599                  }                  }
3600                                    
3601                  !!!insert-element ('tbody');                  !!!insert-element ('tbody');
3602                  $self->{insertion_mode} = 'in table body';                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
3603                  ## reprocess in the "in table body" insertion mode...                  ## reprocess in the "in table body" insertion mode...
3604                }                }
3605    
3606                if ($self->{insertion_mode} eq 'in table body') {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3607                  unless ($token->{tag_name} eq 'tr') {                  unless ($token->{tag_name} eq 'tr') {
3608                    !!!parse-error (type => 'missing start tag:tr');                    !!!parse-error (type => 'missing start tag:tr');
3609                  }                  }
# Line 3360  sub _tree_construction_main ($) { Line 3616  sub _tree_construction_main ($) {
3616                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
3617                  }                  }
3618                                    
3619                  $self->{insertion_mode} = 'in row';                  $self->{insertion_mode} = IN_ROW_IM;
3620                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
3621                    !!!insert-element ($token->{tag_name}, $token->{attributes});                    !!!insert-element ($token->{tag_name}, $token->{attributes});
3622                    !!!next-token;                    !!!next-token;
# Line 3380  sub _tree_construction_main ($) { Line 3636  sub _tree_construction_main ($) {
3636                }                }
3637                                
3638                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
3639                $self->{insertion_mode} = 'in cell';                $self->{insertion_mode} = IN_CELL_IM;
3640    
3641                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
3642                                
# Line 3389  sub _tree_construction_main ($) { Line 3645  sub _tree_construction_main ($) {
3645              } elsif ({              } elsif ({
3646                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
3647                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
3648                        tr => 1, # $self->{insertion_mode} eq 'in row'                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
3649                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3650                if ($self->{insertion_mode} eq 'in row') {                if ($self->{insertion_mode} == IN_ROW_IM) {
3651                  ## As if </tr>                  ## As if </tr>
3652                  ## have an element in table scope                  ## have an element in table scope
3653                  my $i;                  my $i;
# Line 3422  sub _tree_construction_main ($) { Line 3678  sub _tree_construction_main ($) {
3678                  }                  }
3679                                    
3680                  pop @{$self->{open_elements}}; # tr                  pop @{$self->{open_elements}}; # tr
3681                  $self->{insertion_mode} = 'in table body';                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
3682                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
3683                    ## reprocess                    ## reprocess
3684                    redo B;                    redo B;
# Line 3431  sub _tree_construction_main ($) { Line 3687  sub _tree_construction_main ($) {
3687                  }                  }
3688                }                }
3689    
3690                if ($self->{insertion_mode} eq 'in table body') {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3691                  ## have an element in table scope                  ## have an element in table scope
3692                  my $i;                  my $i;
3693                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 3470  sub _tree_construction_main ($) { Line 3726  sub _tree_construction_main ($) {
3726                  ## nop by definition                  ## nop by definition
3727                                    
3728                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3729                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
3730                  ## reprocess in "in table" insertion mode...                  ## reprocess in "in table" insertion mode...
3731                }                }
3732    
# Line 3483  sub _tree_construction_main ($) { Line 3739  sub _tree_construction_main ($) {
3739                  }                  }
3740                                    
3741                  !!!insert-element ('colgroup');                  !!!insert-element ('colgroup');
3742                  $self->{insertion_mode} = 'in column group';                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
3743                  ## reprocess                  ## reprocess
3744                  redo B;                  redo B;
3745                } elsif ({                } elsif ({
# Line 3503  sub _tree_construction_main ($) { Line 3759  sub _tree_construction_main ($) {
3759                                    
3760                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3761                  $self->{insertion_mode} = {                  $self->{insertion_mode} = {
3762                                             caption => 'in caption',                                             caption => IN_CAPTION_IM,
3763                                             colgroup => 'in column group',                                             colgroup => IN_COLUMN_GROUP_IM,
3764                                             tbody => 'in table body',                                             tbody => IN_TABLE_BODY_IM,
3765                                             tfoot => 'in table body',                                             tfoot => IN_TABLE_BODY_IM,
3766                                             thead => 'in table body',                                             thead => IN_TABLE_BODY_IM,
3767                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
3768                  !!!next-token;                  !!!next-token;
3769                  redo B;                  redo B;
# Line 3515  sub _tree_construction_main ($) { Line 3771  sub _tree_construction_main ($) {
3771                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
3772                }                }
3773              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
               ## NOTE: There are code clones for this "table in table"  
3774                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3775    
3776                ## As if </table>                ## As if </table>
# Line 3546  sub _tree_construction_main ($) { Line 3801  sub _tree_construction_main ($) {
3801                     tbody => 1, tfoot=> 1, thead => 1,                     tbody => 1, tfoot=> 1, thead => 1,
3802                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
3803                  !!!back-token; # <table>                  !!!back-token; # <table>
3804                  $token = {type => 'end tag', tag_name => 'table'};                  $token = {type => END_TAG_TOKEN, tag_name => 'table'};
3805                  !!!back-token;                  !!!back-token;
3806                  $token = {type => 'end tag',                  $token = {type => END_TAG_TOKEN,
3807                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3808                  redo B;                  redo B;
3809                }                }
# Line 3563  sub _tree_construction_main ($) { Line 3818  sub _tree_construction_main ($) {
3818    
3819                ## reprocess                ## reprocess
3820                redo B;                redo B;
3821              } else {          } else {
3822                #            !!!parse-error (type => 'in table:'.$token->{tag_name});
3823              }  
3824            } elsif ($token->{type} eq 'end tag') {            $insert = $insert_to_foster;
3825              #
3826            }
3827          } elsif ($token->{type} == END_TAG_TOKEN) {
3828              if ($token->{tag_name} eq 'tr' and              if ($token->{tag_name} eq 'tr' and
3829                  $self->{insertion_mode} eq 'in row') {                  $self->{insertion_mode} == IN_ROW_IM) {
3830                ## have an element in table scope                ## have an element in table scope
3831                my $i;                my $i;
3832                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 3598  sub _tree_construction_main ($) { Line 3856  sub _tree_construction_main ($) {
3856                }                }
3857    
3858                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
3859                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_BODY_IM;
3860                !!!next-token;                !!!next-token;
3861                redo B;                redo B;
3862              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
3863                if ($self->{insertion_mode} eq 'in row') {                if ($self->{insertion_mode} == IN_ROW_IM) {
3864                  ## As if </tr>                  ## As if </tr>
3865                  ## have an element in table scope                  ## have an element in table scope
3866                  my $i;                  my $i;
# Line 3633  sub _tree_construction_main ($) { Line 3891  sub _tree_construction_main ($) {
3891                  }                  }
3892                                    
3893                  pop @{$self->{open_elements}}; # tr                  pop @{$self->{open_elements}}; # tr
3894                  $self->{insertion_mode} = 'in table body';                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
3895                  ## reprocess in the "in table body" insertion mode...                  ## reprocess in the "in table body" insertion mode...
3896                }                }
3897    
3898                if ($self->{insertion_mode} eq 'in table body') {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3899                  ## have an element in table scope                  ## have an element in table scope
3900                  my $i;                  my $i;
3901                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 3676  sub _tree_construction_main ($) { Line 3934  sub _tree_construction_main ($) {
3934                  ## nop by definition                  ## nop by definition
3935                                    
3936                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3937                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
3938                  ## reprocess in the "in table" insertion mode...                  ## reprocess in the "in table" insertion mode...
3939                }                }
3940    
# Line 3707  sub _tree_construction_main ($) { Line 3965  sub _tree_construction_main ($) {
3965                     tbody => 1, tfoot=> 1, thead => 1,                     tbody => 1, tfoot=> 1, thead => 1,
3966                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
3967                  !!!back-token;                  !!!back-token;
3968                  $token = {type => 'end tag',                  $token = {type => END_TAG_TOKEN,
3969                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3970                  redo B;                  redo B;
3971                }                }
# Line 3725  sub _tree_construction_main ($) { Line 3983  sub _tree_construction_main ($) {
3983              } elsif ({              } elsif ({
3984                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
3985                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
3986                       ($self->{insertion_mode} eq 'in row' or                       $self->{insertion_mode} & ROW_IMS) {
3987                        $self->{insertion_mode} eq 'in table body')) {                if ($self->{insertion_mode} == IN_ROW_IM) {
               if ($self->{insertion_mode} eq 'in row') {  
3988                  ## have an element in table scope                  ## have an element in table scope
3989                  my $i;                  my $i;
3990                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 3778  sub _tree_construction_main ($) { Line 4035  sub _tree_construction_main ($) {
4035                  }                  }
4036                                    
4037                  pop @{$self->{open_elements}}; # tr                  pop @{$self->{open_elements}}; # tr
4038                  $self->{insertion_mode} = 'in table body';                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
4039                  ## reprocess in the "in table body" insertion mode...                  ## reprocess in the "in table body" insertion mode...
4040                }                }
4041    
# Line 3811  sub _tree_construction_main ($) { Line 4068  sub _tree_construction_main ($) {
4068                }                }
4069    
4070                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4071                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
4072                !!!next-token;                !!!next-token;
4073                redo B;                redo B;
4074              } elsif ({              } elsif ({
4075                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
4076                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
4077                        tr => 1, # $self->{insertion_mode} eq 'in row'                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
4078                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} eq 'in table'                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
4079                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4080                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4081                ## Ignore the token                ## Ignore the token
4082                !!!next-token;                !!!next-token;
4083                redo B;                redo B;
4084              } else {          } else {
4085                #            !!!parse-error (type => 'in table:/'.$token->{tag_name});
             }  
           } else {  
             die "$0: $token->{type}: Unknown token type";  
           }  
   
       !!!parse-error (type => 'in table:'.$token->{tag_name});  
4086    
4087        $insert = $insert_to_foster;            $insert = $insert_to_foster;
4088        #            #
4089      } elsif ($self->{insertion_mode} eq 'in column group') {          }
4090            if ($token->{type} eq 'character') {        } else {
4091            die "$0: $token->{type}: Unknown token type";
4092          }
4093        } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
4094              if ($token->{type} == CHARACTER_TOKEN) {
4095              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4096                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4097                unless (length $token->{data}) {                unless (length $token->{data}) {
# Line 3846  sub _tree_construction_main ($) { Line 4101  sub _tree_construction_main ($) {
4101              }              }
4102                            
4103              #              #
4104            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} == START_TAG_TOKEN) {
4105              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
4106                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
4107                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
# Line 3855  sub _tree_construction_main ($) { Line 4110  sub _tree_construction_main ($) {
4110              } else {              } else {
4111                #                #
4112              }              }
4113            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
4114              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
4115                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] eq 'html') {
4116                  !!!parse-error (type => 'unmatched end tag:colgroup');                  !!!parse-error (type => 'unmatched end tag:colgroup');
# Line 3864  sub _tree_construction_main ($) { Line 4119  sub _tree_construction_main ($) {
4119                  redo B;                  redo B;
4120                } else {                } else {
4121                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
4122                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
4123                  !!!next-token;                  !!!next-token;
4124                  redo B;                              redo B;            
4125                }                }
# Line 3888  sub _tree_construction_main ($) { Line 4143  sub _tree_construction_main ($) {
4143              redo B;              redo B;
4144            } else {            } else {
4145              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
4146              $self->{insertion_mode} = 'in table';              $self->{insertion_mode} = IN_TABLE_IM;
4147              ## reprocess              ## reprocess
4148              redo B;              redo B;
4149            }            }
4150      } elsif ($self->{insertion_mode} eq 'in select') {      } elsif ($self->{insertion_mode} == IN_SELECT_IM) {
4151            if ($token->{type} eq 'character') {        if ($token->{type} == CHARACTER_TOKEN) {
4152              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4153              !!!next-token;          !!!next-token;
4154              redo B;          redo B;
4155            } elsif ($token->{type} eq 'start tag') {        } elsif ($token->{type} == START_TAG_TOKEN) {
4156              if ($token->{tag_name} eq 'option') {              if ($token->{tag_name} eq 'option') {
4157                if ($self->{open_elements}->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
4158                  ## As if </option>                  ## As if </option>
# Line 3950  sub _tree_construction_main ($) { Line 4205  sub _tree_construction_main ($) {
4205    
4206                !!!next-token;                !!!next-token;
4207                redo B;                redo B;
4208              } else {          } else {
4209                #            !!!parse-error (type => 'in select:'.$token->{tag_name});
4210              }            ## Ignore the token
4211            } elsif ($token->{type} eq 'end tag') {            !!!next-token;
4212              redo B;
4213            }
4214          } elsif ($token->{type} == END_TAG_TOKEN) {
4215              if ($token->{tag_name} eq 'optgroup') {              if ($token->{tag_name} eq 'optgroup') {
4216                if ($self->{open_elements}->[-1]->[1] eq 'option' and                if ($self->{open_elements}->[-1]->[1] eq 'option' and
4217                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {
# Line 4055  sub _tree_construction_main ($) { Line 4313  sub _tree_construction_main ($) {
4313    
4314                ## reprocess                ## reprocess
4315                redo B;                redo B;
4316              } else {          } else {
4317                #            !!!parse-error (type => 'in select:/'.$token->{tag_name});
             }  
           } else {  
             #  
           }  
   
           !!!parse-error (type => 'in select:'.$token->{tag_name});  
4318            ## Ignore the token            ## Ignore the token
4319            !!!next-token;            !!!next-token;
4320            redo B;            redo B;
4321      } elsif ($self->{insertion_mode} eq 'after body' or          }
4322               $self->{insertion_mode} eq 'after html body') {        } else {
4323        if ($token->{type} eq 'character') {          die "$0: $token->{type}: Unknown token type";
4324          }
4325        } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
4326          if ($token->{type} == CHARACTER_TOKEN) {
4327          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4328            my $data = $1;            my $data = $1;
4329            ## As if in body            ## As if in body
# Line 4082  sub _tree_construction_main ($) { Line 4337  sub _tree_construction_main ($) {
4337            }            }
4338          }          }
4339                    
4340          if ($self->{insertion_mode} eq 'after html body') {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4341            !!!parse-error (type => 'after html:#character');            !!!parse-error (type => 'after html:#character');
4342    
4343            ## 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 4346  sub _tree_construction_main ($) {
4346          ## "after body" insertion mode          ## "after body" insertion mode
4347          !!!parse-error (type => 'after body:#character');          !!!parse-error (type => 'after body:#character');
4348    
4349          $self->{insertion_mode} = 'in body';          $self->{insertion_mode} = IN_BODY_IM;
4350          ## reprocess          ## reprocess
4351          redo B;          redo B;
4352        } elsif ($token->{type} eq 'start tag') {        } elsif ($token->{type} == START_TAG_TOKEN) {
4353          if ($self->{insertion_mode} eq 'after html body') {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4354            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html:'.$token->{tag_name});
4355                        
4356            ## 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 4359  sub _tree_construction_main ($) {
4359          ## "after body" insertion mode          ## "after body" insertion mode
4360          !!!parse-error (type => 'after body:'.$token->{tag_name});          !!!parse-error (type => 'after body:'.$token->{tag_name});
4361    
4362          $self->{insertion_mode} = 'in body';          $self->{insertion_mode} = IN_BODY_IM;
4363          ## reprocess          ## reprocess
4364          redo B;          redo B;
4365        } elsif ($token->{type} eq 'end tag') {        } elsif ($token->{type} == END_TAG_TOKEN) {
4366          if ($self->{insertion_mode} eq 'after html body') {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4367            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!parse-error (type => 'after html:/'.$token->{tag_name});
4368                        
4369            $self->{insertion_mode} = 'after body';            $self->{insertion_mode} = AFTER_BODY_IM;
4370            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "main" phase, "after body" insertion mode...
4371          }          }
4372    
# Line 4123  sub _tree_construction_main ($) { Line 4378  sub _tree_construction_main ($) {
4378              !!!next-token;              !!!next-token;
4379              redo B;              redo B;
4380            } else {            } else {
4381              $self->{insertion_mode} = 'after html body';              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
4382              !!!next-token;              !!!next-token;
4383              redo B;              redo B;
4384            }            }
4385          } else {          } else {
4386            !!!parse-error (type => 'after body:/'.$token->{tag_name});            !!!parse-error (type => 'after body:/'.$token->{tag_name});
4387    
4388            $self->{insertion_mode} = 'in body';            $self->{insertion_mode} = IN_BODY_IM;
4389            ## reprocess            ## reprocess
4390            redo B;            redo B;
4391          }          }
4392        } else {        } else {
4393          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
4394        }        }
4395      } elsif ($self->{insertion_mode} eq 'in frameset' or      } elsif ($self->{insertion_mode} & FRAME_IMS) {
4396               $self->{insertion_mode} eq 'after frameset' or        if ($token->{type} == CHARACTER_TOKEN) {
              $self->{insertion_mode} eq 'after html frameset') {  
       if ($token->{type} eq 'character') {  
4397          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4398            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4399                        
# Line 4151  sub _tree_construction_main ($) { Line 4404  sub _tree_construction_main ($) {
4404          }          }
4405                    
4406          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
4407            if ($self->{insertion_mode} eq 'in frameset') {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4408              !!!parse-error (type => 'in frameset:#character');              !!!parse-error (type => 'in frameset:#character');
4409            } elsif ($self->{insertion_mode} eq 'after frameset') {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
4410              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#character');
4411            } else { # "after html frameset"            } else { # "after html frameset"
4412              !!!parse-error (type => 'after html:#character');              !!!parse-error (type => 'after html:#character');
4413    
4414              $self->{insertion_mode} = 'after frameset';              $self->{insertion_mode} = AFTER_FRAMESET_IM;
4415              ## Reprocess in the "main" phase, "after frameset"...              ## Reprocess in the "main" phase, "after frameset"...
4416              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#character');
4417            }            }
# Line 4173  sub _tree_construction_main ($) { Line 4426  sub _tree_construction_main ($) {
4426          }          }
4427                    
4428          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
4429        } elsif ($token->{type} eq 'start tag') {        } elsif ($token->{type} == START_TAG_TOKEN) {
4430          if ($self->{insertion_mode} eq 'after html frameset') {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4431            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html:'.$token->{tag_name});
4432    
4433            $self->{insertion_mode} = 'after frameset';            $self->{insertion_mode} = AFTER_FRAMESET_IM;
4434            ## Process in the "main" phase, "after frameset" insertion mode...            ## Process in the "main" phase, "after frameset" insertion mode...
4435          }          }
4436    
4437          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
4438              $self->{insertion_mode} eq 'in frameset') {              $self->{insertion_mode} == IN_FRAMESET_IM) {
4439            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes});
4440            !!!next-token;            !!!next-token;
4441            redo B;            redo B;
4442          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
4443                   $self->{insertion_mode} eq 'in frameset') {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
4444            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes});
4445            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4446            !!!next-token;            !!!next-token;
# Line 4197  sub _tree_construction_main ($) { Line 4450  sub _tree_construction_main ($) {
4450            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
4451            redo B;            redo B;
4452          } else {          } else {
4453            if ($self->{insertion_mode} eq 'in frameset') {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4454              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!parse-error (type => 'in frameset:'.$token->{tag_name});
4455            } else {            } else {
4456              !!!parse-error (type => 'after frameset:'.$token->{tag_name});              !!!parse-error (type => 'after frameset:'.$token->{tag_name});
# Line 4206  sub _tree_construction_main ($) { Line 4459  sub _tree_construction_main ($) {
4459            !!!next-token;            !!!next-token;
4460            redo B;            redo B;
4461          }          }
4462        } elsif ($token->{type} eq 'end tag') {        } elsif ($token->{type} == END_TAG_TOKEN) {
4463          if ($self->{insertion_mode} eq 'after html frameset') {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4464            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!parse-error (type => 'after html:/'.$token->{tag_name});
4465    
4466            $self->{insertion_mode} = 'after frameset';            $self->{insertion_mode} = AFTER_FRAMESET_IM;
4467            ## Process in the "main" phase, "after frameset" insertion mode...            ## Process in the "main" phase, "after frameset" insertion mode...
4468          }          }
4469    
4470          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
4471              $self->{insertion_mode} eq 'in frameset') {              $self->{insertion_mode} == IN_FRAMESET_IM) {
4472            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] eq 'html' and
4473                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
4474              !!!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 4481  sub _tree_construction_main ($) {
4481    
4482            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
4483                $self->{open_elements}->[-1]->[1] ne 'frameset') {                $self->{open_elements}->[-1]->[1] ne 'frameset') {
4484              $self->{insertion_mode} = 'after frameset';              $self->{insertion_mode} = AFTER_FRAMESET_IM;
4485            }            }
4486            redo B;            redo B;
4487          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
4488                   $self->{insertion_mode} eq 'after frameset') {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
4489            $self->{insertion_mode} = 'after html frameset';            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
4490            !!!next-token;            !!!next-token;
4491            redo B;            redo B;
4492          } else {          } else {
4493            if ($self->{insertion_mode} eq 'in frameset') {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4494              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});
4495            } else {            } else {
4496              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});
# Line 4256  sub _tree_construction_main ($) { Line 4509  sub _tree_construction_main ($) {
4509      }      }
4510    
4511      ## "in body" insertion mode      ## "in body" insertion mode
4512      if ($token->{type} eq 'start tag') {      if ($token->{type} == START_TAG_TOKEN) {
4513        if ($token->{tag_name} eq 'script') {        if ($token->{tag_name} eq 'script') {
4514          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
4515          $script_start_tag->($insert);          $script_start_tag->($insert);
# Line 4276  sub _tree_construction_main ($) { Line 4529  sub _tree_construction_main ($) {
4529        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
4530          ## NOTE: This is an "as if in head" code clone, only "-t" differs          ## NOTE: This is an "as if in head" code clone, only "-t" differs
4531          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4532          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4533    
4534          unless ($self->{confident}) {          unless ($self->{confident}) {
           my $charset;  
4535            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) { ## TODO: And if supported
4536              $charset = $token->{attributes}->{charset}->{value};              $self->{change_encoding}
4537            }                  ->($self, $token->{attributes}->{charset}->{value});
4538            if ($token->{attributes}->{'http-equiv'}) {              
4539                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4540                    ->set_user_data (manakai_has_reference =>
4541                                         $token->{attributes}->{charset}
4542                                             ->{has_reference});
4543              } elsif ($token->{attributes}->{content}) {
4544              ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.              ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
4545              if ($token->{attributes}->{'http-equiv'}->{value}              if ($token->{attributes}->{content}->{value}
4546                  =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4547                        [\x09-\x0D\x20]*=
4548                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4549                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
4550                $charset = defined $1 ? $1 : defined $2 ? $2 : $3;                $self->{change_encoding}
4551              } ## TODO: And if supported                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);
4552                  $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4553                      ->set_user_data (manakai_has_reference =>
4554                                           $token->{attributes}->{content}
4555                                                 ->{has_reference});
4556                }
4557              }
4558            } else {
4559              if ($token->{attributes}->{charset}) {
4560                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4561                    ->set_user_data (manakai_has_reference =>
4562                                         $token->{attributes}->{charset}
4563                                             ->{has_reference});
4564              }
4565              if ($token->{attributes}->{content}) {
4566                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4567                    ->set_user_data (manakai_has_reference =>
4568                                         $token->{attributes}->{content}
4569                                             ->{has_reference});
4570            }            }
           ## TODO: Change the encoding  
4571          }          }
4572    
4573          !!!next-token;          !!!next-token;
# Line 4336  sub _tree_construction_main ($) { Line 4611  sub _tree_construction_main ($) {
4611          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4612            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4613              !!!back-token;              !!!back-token;
4614              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4615              redo B;              redo B;
4616            } elsif ({            } elsif ({
4617                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
# Line 4349  sub _tree_construction_main ($) { Line 4624  sub _tree_construction_main ($) {
4624          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4625          if ($token->{tag_name} eq 'pre') {          if ($token->{tag_name} eq 'pre') {
4626            !!!next-token;            !!!next-token;
4627            if ($token->{type} eq 'character') {            if ($token->{type} == CHARACTER_TOKEN) {
4628              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
4629              unless (length $token->{data}) {              unless (length $token->{data}) {
4630                !!!next-token;                !!!next-token;
# Line 4370  sub _tree_construction_main ($) { Line 4645  sub _tree_construction_main ($) {
4645            INSCOPE: for (reverse @{$self->{open_elements}}) {            INSCOPE: for (reverse @{$self->{open_elements}}) {
4646              if ($_->[1] eq 'p') {              if ($_->[1] eq 'p') {
4647                !!!back-token;                !!!back-token;
4648                $token = {type => 'end tag', tag_name => 'p'};                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4649                redo B;                redo B;
4650              } elsif ({              } elsif ({
4651                        table => 1, caption => 1, td => 1, th => 1,                        table => 1, caption => 1, td => 1, th => 1,
# Line 4390  sub _tree_construction_main ($) { Line 4665  sub _tree_construction_main ($) {
4665          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4666            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4667              !!!back-token;              !!!back-token;
4668              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4669              redo B;              redo B;
4670            } elsif ({            } elsif ({
4671                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
# Line 4437  sub _tree_construction_main ($) { Line 4712  sub _tree_construction_main ($) {
4712          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4713            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4714              !!!back-token;              !!!back-token;
4715              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4716              redo B;              redo B;
4717            } elsif ({            } elsif ({
4718                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
# Line 4484  sub _tree_construction_main ($) { Line 4759  sub _tree_construction_main ($) {
4759          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4760            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4761              !!!back-token;              !!!back-token;
4762              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4763              redo B;              redo B;
4764            } elsif ({            } elsif ({
4765                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
# Line 4508  sub _tree_construction_main ($) { Line 4783  sub _tree_construction_main ($) {
4783            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
4784            if ($node->[1] eq 'p') {            if ($node->[1] eq 'p') {
4785              !!!back-token;              !!!back-token;
4786              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4787              redo B;              redo B;
4788            } elsif ({            } elsif ({
4789                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
# Line 4552  sub _tree_construction_main ($) { Line 4827  sub _tree_construction_main ($) {
4827              !!!parse-error (type => 'in a:a');              !!!parse-error (type => 'in a:a');
4828                            
4829              !!!back-token;              !!!back-token;
4830              $token = {type => 'end tag', tag_name => 'a'};              $token = {type => END_TAG_TOKEN, tag_name => 'a'};
4831              $formatting_end_tag->($token->{tag_name});              $formatting_end_tag->($token->{tag_name});
4832                            
4833              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
# Line 4599  sub _tree_construction_main ($) { Line 4874  sub _tree_construction_main ($) {
4874          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4875            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
4876            if ($node->[1] eq 'nobr') {            if ($node->[1] eq 'nobr') {
4877              !!!parse-error (type => 'not closed:nobr');              !!!parse-error (type => 'in nobr:nobr');
4878              !!!back-token;              !!!back-token;
4879              $token = {type => 'end tag', tag_name => 'nobr'};              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};
4880              redo B;              redo B;
4881            } elsif ({            } elsif ({
4882                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
# Line 4623  sub _tree_construction_main ($) { Line 4898  sub _tree_construction_main ($) {
4898            if ($node->[1] eq 'button') {            if ($node->[1] eq 'button') {
4899              !!!parse-error (type => 'in button:button');              !!!parse-error (type => 'in button:button');
4900              !!!back-token;              !!!back-token;
4901              $token = {type => 'end tag', tag_name => 'button'};              $token = {type => END_TAG_TOKEN, tag_name => 'button'};
4902              redo B;              redo B;
4903            } elsif ({            } elsif ({
4904                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
# Line 4658  sub _tree_construction_main ($) { Line 4933  sub _tree_construction_main ($) {
4933          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4934            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4935              !!!back-token;              !!!back-token;
4936              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4937              redo B;              redo B;
4938            } elsif ({            } elsif ({
4939                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
# Line 4670  sub _tree_construction_main ($) { Line 4945  sub _tree_construction_main ($) {
4945                        
4946          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4947                        
4948          $self->{insertion_mode} = 'in table';          $self->{insertion_mode} = IN_TABLE_IM;
4949                        
4950          !!!next-token;          !!!next-token;
4951          redo B;          redo B;
# Line 4697  sub _tree_construction_main ($) { Line 4972  sub _tree_construction_main ($) {
4972          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4973            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4974              !!!back-token;              !!!back-token;
4975              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4976              redo B;              redo B;
4977            } elsif ({            } elsif ({
4978                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
# Line 4737  sub _tree_construction_main ($) { Line 5012  sub _tree_construction_main ($) {
5012            delete $at->{action};            delete $at->{action};
5013            delete $at->{prompt};            delete $at->{prompt};
5014            my @tokens = (            my @tokens = (
5015                          {type => 'start tag', tag_name => 'form',                          {type => START_TAG_TOKEN, tag_name => 'form',
5016                           attributes => $form_attrs},                           attributes => $form_attrs},
5017                          {type => 'start tag', tag_name => 'hr'},                          {type => START_TAG_TOKEN, tag_name => 'hr'},
5018                          {type => 'start tag', tag_name => 'p'},                          {type => START_TAG_TOKEN, tag_name => 'p'},
5019                          {type => 'start tag', tag_name => 'label'},                          {type => START_TAG_TOKEN, tag_name => 'label'},
5020                         );                         );
5021            if ($prompt_attr) {            if ($prompt_attr) {
5022              push @tokens, {type => 'character', data => $prompt_attr->{value}};              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};
5023            } else {            } else {
5024              push @tokens, {type => 'character',              push @tokens, {type => CHARACTER_TOKEN,
5025                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD
5026              ## TODO: make this configurable              ## TODO: make this configurable
5027            }            }
5028            push @tokens,            push @tokens,
5029                          {type => 'start tag', tag_name => 'input', attributes => $at},                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},
5030                          #{type => 'character', data => ''}, # SHOULD                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
5031                          {type => 'end tag', tag_name => 'label'},                          {type => END_TAG_TOKEN, tag_name => 'label'},
5032                          {type => 'end tag', tag_name => 'p'},                          {type => END_TAG_TOKEN, tag_name => 'p'},
5033                          {type => 'start tag', tag_name => 'hr'},                          {type => START_TAG_TOKEN, tag_name => 'hr'},
5034                          {type => 'end tag', tag_name => 'form'};                          {type => END_TAG_TOKEN, tag_name => 'form'};
5035            $token = shift @tokens;            $token = shift @tokens;
5036            !!!back-token (@tokens);            !!!back-token (@tokens);
5037            redo B;            redo B;
# Line 4774  sub _tree_construction_main ($) { Line 5049  sub _tree_construction_main ($) {
5049                    
5050          my $text = '';          my $text = '';
5051          !!!next-token;          !!!next-token;
5052          if ($token->{type} eq 'character') {          if ($token->{type} == CHARACTER_TOKEN) {
5053            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
5054            unless (length $token->{data}) {            unless (length $token->{data}) {
5055              !!!next-token;              !!!next-token;
5056            }            }
5057          }          }
5058          while ($token->{type} eq 'character') {          while ($token->{type} == CHARACTER_TOKEN) {
5059            $text .= $token->{data};            $text .= $token->{data};
5060            !!!next-token;            !!!next-token;
5061          }          }
# Line 4790  sub _tree_construction_main ($) { Line 5065  sub _tree_construction_main ($) {
5065                    
5066          $self->{content_model} = PCDATA_CONTENT_MODEL;          $self->{content_model} = PCDATA_CONTENT_MODEL;
5067                    
5068          if ($token->{type} eq 'end tag' and          if ($token->{type} == END_TAG_TOKEN and
5069              $token->{tag_name} eq $tag_name) {              $token->{tag_name} eq $tag_name) {
5070            ## Ignore the token            ## Ignore the token
5071          } else {          } else {
# Line 4804  sub _tree_construction_main ($) { Line 5079  sub _tree_construction_main ($) {
5079                  noframes => 1,                  noframes => 1,
5080                  noscript => 0, ## TODO: 1 if scripting is enabled                  noscript => 0, ## TODO: 1 if scripting is enabled
5081                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5082          ## NOTE: There are two "as if in body" code clones.          ## NOTE: There is an "as if in body" code clone.
5083          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
5084          redo B;          redo B;
5085        } elsif ($token->{tag_name} eq 'select') {        } elsif ($token->{tag_name} eq 'select') {
# Line 4812  sub _tree_construction_main ($) { Line 5087  sub _tree_construction_main ($) {
5087                    
5088          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5089                    
5090          $self->{insertion_mode} = 'in select';          $self->{insertion_mode} = IN_SELECT_IM;
5091          !!!next-token;          !!!next-token;
5092          redo B;          redo B;
5093        } elsif ({        } elsif ({
# Line 4835  sub _tree_construction_main ($) { Line 5110  sub _tree_construction_main ($) {
5110          !!!next-token;          !!!next-token;
5111          redo B;          redo B;
5112        }        }
5113      } elsif ($token->{type} eq 'end tag') {      } elsif ($token->{type} == END_TAG_TOKEN) {
5114        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
5115          if (@{$self->{open_elements}} > 1 and          if (@{$self->{open_elements}} > 1 and
5116              $self->{open_elements}->[1]->[1] eq 'body') {              $self->{open_elements}->[1]->[1] eq 'body') {
# Line 4849  sub _tree_construction_main ($) { Line 5124  sub _tree_construction_main ($) {
5124              }              }
5125            }            }
5126    
5127            $self->{insertion_mode} = 'after body';            $self->{insertion_mode} = AFTER_BODY_IM;
5128            !!!next-token;            !!!next-token;
5129            redo B;            redo B;
5130          } else {          } else {
# Line 4864  sub _tree_construction_main ($) { Line 5139  sub _tree_construction_main ($) {
5139            if ($self->{open_elements}->[-1]->[1] ne 'body') {            if ($self->{open_elements}->[-1]->[1] ne 'body') {
5140              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);
5141            }            }
5142            $self->{insertion_mode} = 'after body';            $self->{insertion_mode} = AFTER_BODY_IM;
5143            ## reprocess            ## reprocess
5144            redo B;            redo B;
5145          } else {          } else {
# Line 4896  sub _tree_construction_main ($) { Line 5171  sub _tree_construction_main ($) {
5171                   tbody => 1, tfoot=> 1, thead => 1,                   tbody => 1, tfoot=> 1, thead => 1,
5172                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
5173                !!!back-token;                !!!back-token;
5174                $token = {type => 'end tag',                $token = {type => END_TAG_TOKEN,
5175                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5176                redo B;                redo B;
5177              }              }
# Line 4944  sub _tree_construction_main ($) { Line 5219  sub _tree_construction_main ($) {
5219                   tbody => 1, tfoot=> 1, thead => 1,                   tbody => 1, tfoot=> 1, thead => 1,
5220                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
5221                !!!back-token;                !!!back-token;
5222                $token = {type => 'end tag',                $token = {type => END_TAG_TOKEN,
5223                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5224                redo B;                redo B;
5225              }              }
# Line 4960  sub _tree_construction_main ($) { Line 5235  sub _tree_construction_main ($) {
5235          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {
5236            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
5237          } else {          } else {
5238            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5239          }          }
5240    
5241          undef $self->{form_element};          undef $self->{form_element};
# Line 4983  sub _tree_construction_main ($) { Line 5258  sub _tree_construction_main ($) {
5258                   tbody => 1, tfoot=> 1, thead => 1,                   tbody => 1, tfoot=> 1, thead => 1,
5259                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
5260                !!!back-token;                !!!back-token;
5261                $token = {type => 'end tag',                $token = {type => END_TAG_TOKEN,
5262                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5263                redo B;                redo B;
5264              }              }
# Line 4998  sub _tree_construction_main ($) { Line 5273  sub _tree_construction_main ($) {
5273          } # INSCOPE          } # INSCOPE
5274                    
5275          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
5276            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5277          }          }
5278                    
5279          splice @{$self->{open_elements}}, $i if defined $i;          splice @{$self->{open_elements}}, $i if defined $i;
# Line 5057  sub _tree_construction_main ($) { Line 5332  sub _tree_construction_main ($) {
5332              if ({              if ({
5333                   dd => 1, dt => 1, li => 1, p => 1,                   dd => 1, dt => 1, li => 1, p => 1,
5334                   td => 1, th => 1, tr => 1,                   td => 1, th => 1, tr => 1,
5335                   tbody => 1, tfoot=> 1, thead => 1,                   tbody => 1, tfoot => 1, thead => 1,
5336                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
5337                !!!back-token;                !!!back-token;
5338                $token = {type => 'end tag',                $token = {type => END_TAG_TOKEN,
5339                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5340                redo B;                redo B;
5341              }              }
5342                    
5343              ## Step 2              ## Step 2
5344              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {
5345                  ## NOTE: <x><y></x>
5346                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
5347              }              }
5348                            
# Line 5117  sub set_inner_html ($$$) { Line 5393  sub set_inner_html ($$$) {
5393    my $s = \$_[0];    my $s = \$_[0];
5394    my $onerror = $_[1];    my $onerror = $_[1];
5395    
5396      ## ISSUE: Should {confident} be true?
5397    
5398    my $nt = $node->node_type;    my $nt = $node->node_type;
5399    if ($nt == 9) {    if ($nt == 9) {
5400      # MUST      # MUST
# Line 5189  sub set_inner_html ($$$) { Line 5467  sub set_inner_html ($$$) {
5467      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
5468    
5469      ## Step 2      ## Step 2
5470      my $node_ln = $node->local_name;      my $node_ln = $node->manakai_local_name;
5471      $p->{content_model} = {      $p->{content_model} = {
5472        title => RCDATA_CONTENT_MODEL,        title => RCDATA_CONTENT_MODEL,
5473        textarea => RCDATA_CONTENT_MODEL,        textarea => RCDATA_CONTENT_MODEL,
# Line 5229  sub set_inner_html ($$$) { Line 5507  sub set_inner_html ($$$) {
5507        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
5508          my $nsuri = $anode->namespace_uri;          my $nsuri = $anode->namespace_uri;
5509          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
5510            if ($anode->local_name eq 'form') { ## TODO: case?            if ($anode->manakai_local_name eq 'form') {
5511              $p->{form_element} = $anode;              $p->{form_element} = $anode;
5512              last AN;              last AN;
5513            }            }
# Line 5269  sub set_inner_html ($$$) { Line 5547  sub set_inner_html ($$$) {
5547    
5548  } # tree construction stage  } # tree construction stage
5549    
5550  sub get_inner_html ($$$) {  package Whatpm::HTML::RestartParser;
5551    my (undef, $node, $on_error) = @_;  push our @ISA, 'Error';
   
   ## Step 1  
   my $s = '';  
   
   my $in_cdata;  
   my $parent = $node;  
   while (defined $parent) {  
     if ($parent->node_type == 1 and  
         $parent->namespace_uri eq 'http://www.w3.org/1999/xhtml' and  
         {  
           style => 1, script => 1, xmp => 1, iframe => 1,  
           noembed => 1, noframes => 1, noscript => 1,  
         }->{$parent->local_name}) { ## TODO: case thingy  
       $in_cdata = 1;  
     }  
     $parent = $parent->parent_node;  
   }  
   
   ## Step 2  
   my @node = @{$node->child_nodes};  
   C: while (@node) {  
     my $child = shift @node;  
     unless (ref $child) {  
       if ($child eq 'cdata-out') {  
         $in_cdata = 0;  
       } else {  
         $s .= $child; # end tag  
       }  
       next C;  
     }  
       
     my $nt = $child->node_type;  
     if ($nt == 1) { # Element  
       my $tag_name = $child->tag_name; ## TODO: manakai_tag_name  
       $s .= '<' . $tag_name;  
       ## NOTE: Non-HTML case:  
       ## <http://permalink.gmane.org/gmane.org.w3c.whatwg.discuss/11191>  
   
       my @attrs = @{$child->attributes}; # sort order MUST be stable  
       for my $attr (@attrs) { # order is implementation dependent  
         my $attr_name = $attr->name; ## TODO: manakai_name  
         $s .= ' ' . $attr_name . '="';  
         my $attr_value = $attr->value;  
         ## escape  
         $attr_value =~ s/&/&amp;/g;  
         $attr_value =~ s/</&lt;/g;  
         $attr_value =~ s/>/&gt;/g;  
         $attr_value =~ s/"/&quot;/g;  
         $s .= $attr_value . '"';  
       }  
       $s .= '>';  
         
       next C if {  
         area => 1, base => 1, basefont => 1, bgsound => 1,  
         br => 1, col => 1, embed => 1, frame => 1, hr => 1,  
         img => 1, input => 1, link => 1, meta => 1, param => 1,  
         spacer => 1, wbr => 1,  
       }->{$tag_name};  
   
       $s .= "\x0A" if $tag_name eq 'pre' or $tag_name eq 'textarea';  
   
       if (not $in_cdata and {  
         style => 1, script => 1, xmp => 1, iframe => 1,  
         noembed => 1, noframes => 1, noscript => 1,  
         plaintext => 1,  
       }->{$tag_name}) {  
         unshift @node, 'cdata-out';  
         $in_cdata = 1;  
       }  
   
       unshift @node, @{$child->child_nodes}, '</' . $tag_name . '>';  
     } elsif ($nt == 3 or $nt == 4) {  
       if ($in_cdata) {  
         $s .= $child->data;  
       } else {  
         my $value = $child->data;  
         $value =~ s/&/&amp;/g;  
         $value =~ s/</&lt;/g;  
         $value =~ s/>/&gt;/g;  
         $value =~ s/"/&quot;/g;  
         $s .= $value;  
       }  
     } elsif ($nt == 8) {  
       $s .= '<!--' . $child->data . '-->';  
     } elsif ($nt == 10) {  
       $s .= '<!DOCTYPE ' . $child->name . '>';  
     } elsif ($nt == 5) { # entrefs  
       push @node, @{$child->child_nodes};  
     } else {  
       $on_error->($child) if defined $on_error;  
     }  
     ## ISSUE: This code does not support PIs.  
   } # C  
     
   ## Step 3  
   return \$s;  
 } # get_inner_html  
5552    
5553  1;  1;
5554  # $Date$  # $Date$

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24