/[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.52 by wakaba, Sat Jul 21 12:27:22 2007 UTC revision 1.72 by wakaba, Sun Mar 2 14:32:26 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    sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
290    
291    sub DOCTYPE_TOKEN () { 1 }
292    sub COMMENT_TOKEN () { 2 }
293    sub START_TAG_TOKEN () { 3 }
294    sub END_TAG_TOKEN () { 4 }
295    sub END_OF_FILE_TOKEN () { 5 }
296    sub CHARACTER_TOKEN () { 6 }
297    
298    sub AFTER_HTML_IMS () { 0b100 }
299    sub HEAD_IMS ()       { 0b1000 }
300    sub BODY_IMS ()       { 0b10000 }
301    sub BODY_TABLE_IMS () { 0b100000 }
302    sub TABLE_IMS ()      { 0b1000000 }
303    sub ROW_IMS ()        { 0b10000000 }
304    sub BODY_AFTER_IMS () { 0b100000000 }
305    sub FRAME_IMS ()      { 0b1000000000 }
306    
307    sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
308    sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
309    sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
310    sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
311    sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
312    sub BEFORE_HEAD_IM () { HEAD_IMS | 0b11 }
313    sub IN_BODY_IM () { BODY_IMS }
314    sub IN_CELL_IM () { BODY_IMS | BODY_TABLE_IMS | 0b01 }
315    sub IN_CAPTION_IM () { BODY_IMS | BODY_TABLE_IMS | 0b10 }
316    sub IN_ROW_IM () { TABLE_IMS | ROW_IMS | 0b01 }
317    sub IN_TABLE_BODY_IM () { TABLE_IMS | ROW_IMS | 0b10 }
318    sub IN_TABLE_IM () { TABLE_IMS }
319    sub AFTER_BODY_IM () { BODY_AFTER_IMS }
320    sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
321    sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
322    sub IN_SELECT_IM () { 0b01 }
323    sub IN_COLUMN_GROUP_IM () { 0b10 }
324    
325  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
326    
327  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
328    my $self = shift;    my $self = shift;
329    $self->{state} = 'data'; # MUST    $self->{state} = DATA_STATE; # MUST
330    $self->{content_model} = PCDATA_CONTENT_MODEL; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
331    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE
332    undef $self->{current_attribute};    undef $self->{current_attribute};
# Line 177  sub _initialize_tokenizer ($) { Line 340  sub _initialize_tokenizer ($) {
340  } # _initialize_tokenizer  } # _initialize_tokenizer
341    
342  ## A token has:  ## A token has:
343  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',  ##   ->{type} == DOCTYPE_TOKEN, START_TAG_TOKEN, END_TAG_TOKEN, COMMENT_TOKEN,
344  ##       'character', or 'end-of-file'  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN
345  ##   ->{name} (DOCTYPE, start tag (tag name), end tag (tag name))  ##   ->{name} (DOCTYPE_TOKEN)
346  ##   ->{public_identifier} (DOCTYPE)  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
347  ##   ->{system_identifier} (DOCTYPE)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
348  ##   ->{correct} == 1 or 0 (DOCTYPE)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
349  ##   ->{attributes} isa HASH (start tag, end tag)  ##   ->{correct} == 1 or 0 (DOCTYPE_TOKEN)
350  ##   ->{data} (comment, character)  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
351    ##        ->{name}
352    ##        ->{value}
353    ##        ->{has_reference} == 1 or 0
354    ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
355    
356  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
357    
# Line 194  sub _initialize_tokenizer ($) { Line 361  sub _initialize_tokenizer ($) {
361  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
362  ## and removed from the list.  ## and removed from the list.
363    
364    ## NOTE: HTML5 "Writing HTML documents" section, applied to
365    ## documents and not to user agents and conformance checkers,
366    ## contains some requirements that are not detected by the
367    ## parsing algorithm:
368    ## - Some requirements on character encoding declarations. ## TODO
369    ## - "Elements MUST NOT contain content that their content model disallows."
370    ##   ... Some are parse error, some are not (will be reported by c.c.).
371    ## - Polytheistic slash SHOULD NOT be used. (Applied only to atheists.) ## TODO
372    ## - Text (in elements, attributes, and comments) SHOULD NOT contain
373    ##   control characters other than space characters. ## TODO: (what is control character? C0, C1 and DEL?  Unicode control character?)
374    
375    ## TODO: HTML5 poses authors two SHOULD-level requirements that cannot
376    ## be detected by the HTML5 parsing algorithm:
377    ## - Text,
378    
379  sub _get_next_token ($) {  sub _get_next_token ($) {
380    my $self = shift;    my $self = shift;
381    if (@{$self->{token}}) {    if (@{$self->{token}}) {
# Line 201  sub _get_next_token ($) { Line 383  sub _get_next_token ($) {
383    }    }
384    
385    A: {    A: {
386      if ($self->{state} eq 'data') {      if ($self->{state} == DATA_STATE) {
387        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_input_character} == 0x0026) { # &
388          if ($self->{content_model} & CM_ENTITY) { # PCDATA | RCDATA          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
389            $self->{state} = 'entity data';              not $self->{escape}) {
390              $self->{state} = ENTITY_DATA_STATE;
391            !!!next-input-character;            !!!next-input-character;
392            redo A;            redo A;
393          } else {          } else {
# Line 226  sub _get_next_token ($) { Line 409  sub _get_next_token ($) {
409          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
410              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
411               not $self->{escape})) {               not $self->{escape})) {
412            $self->{state} = 'tag open';            $self->{state} = TAG_OPEN_STATE;
413            !!!next-input-character;            !!!next-input-character;
414            redo A;            redo A;
415          } else {          } else {
# Line 243  sub _get_next_token ($) { Line 426  sub _get_next_token ($) {
426                    
427          #          #
428        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
429          !!!emit ({type => 'end-of-file'});          !!!emit ({type => END_OF_FILE_TOKEN});
430          last A; ## TODO: ok?          last A; ## TODO: ok?
431        }        }
432        # Anything else        # Anything else
433        my $token = {type => 'character',        my $token = {type => CHARACTER_TOKEN,
434                     data => chr $self->{next_input_character}};                     data => chr $self->{next_input_character}};
435        ## Stay in the data state        ## Stay in the data state
436        !!!next-input-character;        !!!next-input-character;
# Line 255  sub _get_next_token ($) { Line 438  sub _get_next_token ($) {
438        !!!emit ($token);        !!!emit ($token);
439    
440        redo A;        redo A;
441      } elsif ($self->{state} eq 'entity data') {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
442        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
443                
444        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);
445    
446        $self->{state} = 'data';        $self->{state} = DATA_STATE;
447        # next-input-character is already done        # next-input-character is already done
448    
449        unless (defined $token) {        unless (defined $token) {
450          !!!emit ({type => 'character', data => '&'});          !!!emit ({type => CHARACTER_TOKEN, data => '&'});
451        } else {        } else {
452          !!!emit ($token);          !!!emit ($token);
453        }        }
454    
455        redo A;        redo A;
456      } elsif ($self->{state} eq 'tag open') {      } elsif ($self->{state} == TAG_OPEN_STATE) {
457        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
458          if ($self->{next_input_character} == 0x002F) { # /          if ($self->{next_input_character} == 0x002F) { # /
459            !!!next-input-character;            !!!next-input-character;
460            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
461            redo A;            redo A;
462          } else {          } else {
463            ## reconsume            ## reconsume
464            $self->{state} = 'data';            $self->{state} = DATA_STATE;
465    
466            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<'});
467    
468            redo A;            redo A;
469          }          }
470        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
471          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_input_character} == 0x0021) { # !
472            $self->{state} = 'markup declaration open';            $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
473            !!!next-input-character;            !!!next-input-character;
474            redo A;            redo A;
475          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{next_input_character} == 0x002F) { # /
476            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
477            !!!next-input-character;            !!!next-input-character;
478            redo A;            redo A;
479          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_input_character} and
480                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{next_input_character} <= 0x005A) { # A..Z
481            $self->{current_token}            $self->{current_token}
482              = {type => 'start tag',              = {type => START_TAG_TOKEN,
483                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_input_character} + 0x0020)};
484            $self->{state} = 'tag name';            $self->{state} = TAG_NAME_STATE;
485            !!!next-input-character;            !!!next-input-character;
486            redo A;            redo A;
487          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_input_character} and
488                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_input_character} <= 0x007A) { # a..z
489            $self->{current_token} = {type => 'start tag',            $self->{current_token} = {type => START_TAG_TOKEN,
490                              tag_name => chr ($self->{next_input_character})};                              tag_name => chr ($self->{next_input_character})};
491            $self->{state} = 'tag name';            $self->{state} = TAG_NAME_STATE;
492            !!!next-input-character;            !!!next-input-character;
493            redo A;            redo A;
494          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_input_character} == 0x003E) { # >
495            !!!parse-error (type => 'empty start tag');            !!!parse-error (type => 'empty start tag');
496            $self->{state} = 'data';            $self->{state} = DATA_STATE;
497            !!!next-input-character;            !!!next-input-character;
498    
499            !!!emit ({type => 'character', data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});
500    
501            redo A;            redo A;
502          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_input_character} == 0x003F) { # ?
503            !!!parse-error (type => 'pio');            !!!parse-error (type => 'pio');
504            $self->{state} = 'bogus comment';            $self->{state} = BOGUS_COMMENT_STATE;
505            ## $self->{next_input_character} is intentionally left as is            ## $self->{next_input_character} is intentionally left as is
506            redo A;            redo A;
507          } else {          } else {
508            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago');
509            $self->{state} = 'data';            $self->{state} = DATA_STATE;
510            ## reconsume            ## reconsume
511    
512            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<'});
513    
514            redo A;            redo A;
515          }          }
516        } else {        } else {
517          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
518        }        }
519      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
520        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
521          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
522            ## 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 531  sub _get_next_token ($) {
531              } else {              } else {
532                $self->{next_input_character} = shift @next_char; # reconsume                $self->{next_input_character} = shift @next_char; # reconsume
533                !!!back-next-input-character (@next_char);                !!!back-next-input-character (@next_char);
534                $self->{state} = 'data';                $self->{state} = DATA_STATE;
535    
536                !!!emit ({type => 'character', data => '</'});                !!!emit ({type => CHARACTER_TOKEN, data => '</'});
537        
538                redo A;                redo A;
539              }              }
# Line 367  sub _get_next_token ($) { Line 550  sub _get_next_token ($) {
550                    $self->{next_input_character} == -1) {                    $self->{next_input_character} == -1) {
551              $self->{next_input_character} = shift @next_char; # reconsume              $self->{next_input_character} = shift @next_char; # reconsume
552              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
553              $self->{state} = 'data';              $self->{state} = DATA_STATE;
554              !!!emit ({type => 'character', data => '</'});              !!!emit ({type => CHARACTER_TOKEN, data => '</'});
555              redo A;              redo A;
556            } else {            } else {
557              $self->{next_input_character} = shift @next_char;              $self->{next_input_character} = shift @next_char;
# Line 378  sub _get_next_token ($) { Line 561  sub _get_next_token ($) {
561          } else {          } else {
562            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
563            # next-input-character is already done            # next-input-character is already done
564            $self->{state} = 'data';            $self->{state} = DATA_STATE;
565            !!!emit ({type => 'character', data => '</'});            !!!emit ({type => CHARACTER_TOKEN, data => '</'});
566            redo A;            redo A;
567          }          }
568        }        }
569                
570        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_input_character} and
571            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_input_character} <= 0x005A) { # A..Z
572          $self->{current_token} = {type => 'end tag',          $self->{current_token} = {type => END_TAG_TOKEN,
573                            tag_name => chr ($self->{next_input_character} + 0x0020)};                            tag_name => chr ($self->{next_input_character} + 0x0020)};
574          $self->{state} = 'tag name';          $self->{state} = TAG_NAME_STATE;
575          !!!next-input-character;          !!!next-input-character;
576          redo A;          redo A;
577        } elsif (0x0061 <= $self->{next_input_character} and        } elsif (0x0061 <= $self->{next_input_character} and
578                 $self->{next_input_character} <= 0x007A) { # a..z                 $self->{next_input_character} <= 0x007A) { # a..z
579          $self->{current_token} = {type => 'end tag',          $self->{current_token} = {type => END_TAG_TOKEN,
580                            tag_name => chr ($self->{next_input_character})};                            tag_name => chr ($self->{next_input_character})};
581          $self->{state} = 'tag name';          $self->{state} = TAG_NAME_STATE;
582          !!!next-input-character;          !!!next-input-character;
583          redo A;          redo A;
584        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
585          !!!parse-error (type => 'empty end tag');          !!!parse-error (type => 'empty end tag');
586          $self->{state} = 'data';          $self->{state} = DATA_STATE;
587          !!!next-input-character;          !!!next-input-character;
588          redo A;          redo A;
589        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
590          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
591          $self->{state} = 'data';          $self->{state} = DATA_STATE;
592          # reconsume          # reconsume
593    
594          !!!emit ({type => 'character', data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</'});
595    
596          redo A;          redo A;
597        } else {        } else {
598          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
599          $self->{state} = 'bogus comment';          $self->{state} = BOGUS_COMMENT_STATE;
600          ## $self->{next_input_character} is intentionally left as is          ## $self->{next_input_character} is intentionally left as is
601          redo A;          redo A;
602        }        }
603      } elsif ($self->{state} eq 'tag name') {      } elsif ($self->{state} == TAG_NAME_STATE) {
604        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
605            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
606            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
607            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
608            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
609          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
610          !!!next-input-character;          !!!next-input-character;
611          redo A;          redo A;
612        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
613          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
614            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
615                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
616            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
617          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
618            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
619            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
620              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 439  sub _get_next_token ($) { Line 622  sub _get_next_token ($) {
622          } else {          } else {
623            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
624          }          }
625          $self->{state} = 'data';          $self->{state} = DATA_STATE;
626          !!!next-input-character;          !!!next-input-character;
627    
628          !!!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 637  sub _get_next_token ($) {
637          redo A;          redo A;
638        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
639          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
640          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
641            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
642                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
643            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
644          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
645            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
646            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
647              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 466  sub _get_next_token ($) { Line 649  sub _get_next_token ($) {
649          } else {          } else {
650            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
651          }          }
652          $self->{state} = 'data';          $self->{state} = DATA_STATE;
653          # reconsume          # reconsume
654    
655          !!!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 658  sub _get_next_token ($) {
658        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
659          !!!next-input-character;          !!!next-input-character;
660          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
661              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
662              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
663            # permitted slash            # permitted slash
664            #            #
665          } else {          } else {
666            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
667          }          }
668          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
669          # next-input-character is already done          # next-input-character is already done
670          redo A;          redo A;
671        } else {        } else {
# Line 492  sub _get_next_token ($) { Line 675  sub _get_next_token ($) {
675          !!!next-input-character;          !!!next-input-character;
676          redo A;          redo A;
677        }        }
678      } elsif ($self->{state} eq 'before attribute name') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
679        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
680            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
681            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 502  sub _get_next_token ($) { Line 685  sub _get_next_token ($) {
685          !!!next-input-character;          !!!next-input-character;
686          redo A;          redo A;
687        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
688          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
689            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
690                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
691            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
692          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
693            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
694            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
695              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 514  sub _get_next_token ($) { Line 697  sub _get_next_token ($) {
697          } else {          } else {
698            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
699          }          }
700          $self->{state} = 'data';          $self->{state} = DATA_STATE;
701          !!!next-input-character;          !!!next-input-character;
702    
703          !!!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 707  sub _get_next_token ($) {
707                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_input_character} <= 0x005A) { # A..Z
708          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),
709                                value => ''};                                value => ''};
710          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
711          !!!next-input-character;          !!!next-input-character;
712          redo A;          redo A;
713        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
714          !!!next-input-character;          !!!next-input-character;
715          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
716              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
717              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
718            # permitted slash            # permitted slash
719            #            #
# Line 542  sub _get_next_token ($) { Line 725  sub _get_next_token ($) {
725          redo A;          redo A;
726        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
727          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
728          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
729            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
730                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
731            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
732          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
733            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
734            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
735              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 554  sub _get_next_token ($) { Line 737  sub _get_next_token ($) {
737          } else {          } else {
738            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
739          }          }
740          $self->{state} = 'data';          $self->{state} = DATA_STATE;
741          # reconsume          # reconsume
742    
743          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
744    
745          redo A;          redo A;
746        } else {        } else {
747            if ({
748                 0x0022 => 1, # "
749                 0x0027 => 1, # '
750                 0x003D => 1, # =
751                }->{$self->{next_input_character}}) {
752              !!!parse-error (type => 'bad attribute name');
753            }
754          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          $self->{current_attribute} = {name => chr ($self->{next_input_character}),
755                                value => ''};                                value => ''};
756          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
757          !!!next-input-character;          !!!next-input-character;
758          redo A;          redo A;
759        }        }
760      } elsif ($self->{state} eq 'attribute name') {      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {
761        my $before_leave = sub {        my $before_leave = sub {
762          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
763              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
# Line 585  sub _get_next_token ($) { Line 775  sub _get_next_token ($) {
775            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
776            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
777          $before_leave->();          $before_leave->();
778          $self->{state} = 'after attribute name';          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
779          !!!next-input-character;          !!!next-input-character;
780          redo A;          redo A;
781        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_input_character} == 0x003D) { # =
782          $before_leave->();          $before_leave->();
783          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
784          !!!next-input-character;          !!!next-input-character;
785          redo A;          redo A;
786        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
787          $before_leave->();          $before_leave->();
788          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
789            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
790                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
791            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
792          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
793            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
794            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
795              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 607  sub _get_next_token ($) { Line 797  sub _get_next_token ($) {
797          } else {          } else {
798            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
799          }          }
800          $self->{state} = 'data';          $self->{state} = DATA_STATE;
801          !!!next-input-character;          !!!next-input-character;
802    
803          !!!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 813  sub _get_next_token ($) {
813          $before_leave->();          $before_leave->();
814          !!!next-input-character;          !!!next-input-character;
815          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
816              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
817              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
818            # permitted slash            # permitted slash
819            #            #
820          } else {          } else {
821            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
822          }          }
823          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
824          # next-input-character is already done          # next-input-character is already done
825          redo A;          redo A;
826        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
827          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
828          $before_leave->();          $before_leave->();
829          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
830            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
831                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
832            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
833          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
834            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
835            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
836              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 648  sub _get_next_token ($) { Line 838  sub _get_next_token ($) {
838          } else {          } else {
839            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
840          }          }
841          $self->{state} = 'data';          $self->{state} = DATA_STATE;
842          # reconsume          # reconsume
843    
844          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
845    
846          redo A;          redo A;
847        } else {        } else {
848            if ($self->{next_input_character} == 0x0022 or # "
849                $self->{next_input_character} == 0x0027) { # '
850              !!!parse-error (type => 'bad attribute name');
851            }
852          $self->{current_attribute}->{name} .= chr ($self->{next_input_character});          $self->{current_attribute}->{name} .= chr ($self->{next_input_character});
853          ## Stay in the state          ## Stay in the state
854          !!!next-input-character;          !!!next-input-character;
855          redo A;          redo A;
856        }        }
857      } elsif ($self->{state} eq 'after attribute name') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
858        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
859            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
860            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 670  sub _get_next_token ($) { Line 864  sub _get_next_token ($) {
864          !!!next-input-character;          !!!next-input-character;
865          redo A;          redo A;
866        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_input_character} == 0x003D) { # =
867          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
868          !!!next-input-character;          !!!next-input-character;
869          redo A;          redo A;
870        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
871          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
872            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
873                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
874            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
875          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
876            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
877            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
878              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 686  sub _get_next_token ($) { Line 880  sub _get_next_token ($) {
880          } else {          } else {
881            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
882          }          }
883          $self->{state} = 'data';          $self->{state} = DATA_STATE;
884          !!!next-input-character;          !!!next-input-character;
885    
886          !!!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 890  sub _get_next_token ($) {
890                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_input_character} <= 0x005A) { # A..Z
891          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),
892                                value => ''};                                value => ''};
893          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
894          !!!next-input-character;          !!!next-input-character;
895          redo A;          redo A;
896        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
897          !!!next-input-character;          !!!next-input-character;
898          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
899              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
900              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
901            # permitted slash            # permitted slash
902            #            #
# Line 710  sub _get_next_token ($) { Line 904  sub _get_next_token ($) {
904            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
905            ## TODO: Different error type for <aa / bb> than <aa/>            ## TODO: Different error type for <aa / bb> than <aa/>
906          }          }
907          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
908          # next-input-character is already done          # next-input-character is already done
909          redo A;          redo A;
910        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
911          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
912          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
913            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
914                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
915            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
916          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
917            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
918            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
919              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 727  sub _get_next_token ($) { Line 921  sub _get_next_token ($) {
921          } else {          } else {
922            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
923          }          }
924          $self->{state} = 'data';          $self->{state} = DATA_STATE;
925          # reconsume          # reconsume
926    
927          !!!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 930  sub _get_next_token ($) {
930        } else {        } else {
931          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          $self->{current_attribute} = {name => chr ($self->{next_input_character}),
932                                value => ''};                                value => ''};
933          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
934          !!!next-input-character;          !!!next-input-character;
935          redo A;                  redo A;        
936        }        }
937      } elsif ($self->{state} eq 'before attribute value') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
938        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
939            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
940            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 750  sub _get_next_token ($) { Line 944  sub _get_next_token ($) {
944          !!!next-input-character;          !!!next-input-character;
945          redo A;          redo A;
946        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_input_character} == 0x0022) { # "
947          $self->{state} = 'attribute value (double-quoted)';          $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
948          !!!next-input-character;          !!!next-input-character;
949          redo A;          redo A;
950        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
951          $self->{state} = 'attribute value (unquoted)';          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
952          ## reconsume          ## reconsume
953          redo A;          redo A;
954        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_input_character} == 0x0027) { # '
955          $self->{state} = 'attribute value (single-quoted)';          $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
956          !!!next-input-character;          !!!next-input-character;
957          redo A;          redo A;
958        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
959          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
960            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
961                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
962            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
963          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
964            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
965            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
966              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 774  sub _get_next_token ($) { Line 968  sub _get_next_token ($) {
968          } else {          } else {
969            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
970          }          }
971          $self->{state} = 'data';          $self->{state} = DATA_STATE;
972          !!!next-input-character;          !!!next-input-character;
973    
974          !!!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 976  sub _get_next_token ($) {
976          redo A;          redo A;
977        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
978          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
979          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
980            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
981                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
982            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
983          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
984            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
985            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
986              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 794  sub _get_next_token ($) { Line 988  sub _get_next_token ($) {
988          } else {          } else {
989            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
990          }          }
991          $self->{state} = 'data';          $self->{state} = DATA_STATE;
992          ## reconsume          ## reconsume
993    
994          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
995    
996          redo A;          redo A;
997        } else {        } else {
998            if ($self->{next_input_character} == 0x003D) { # =
999              !!!parse-error (type => 'bad attribute value');
1000            }
1001          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});
1002          $self->{state} = 'attribute value (unquoted)';          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1003          !!!next-input-character;          !!!next-input-character;
1004          redo A;          redo A;
1005        }        }
1006      } elsif ($self->{state} eq 'attribute value (double-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
1007        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_input_character} == 0x0022) { # "
1008          $self->{state} = 'before attribute name';          $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1009          !!!next-input-character;          !!!next-input-character;
1010          redo A;          redo A;
1011        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
1012          $self->{last_attribute_value_state} = 'attribute value (double-quoted)';          $self->{last_attribute_value_state} = $self->{state};
1013          $self->{state} = 'entity in attribute value';          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1014          !!!next-input-character;          !!!next-input-character;
1015          redo A;          redo A;
1016        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1017          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1018          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1019            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
1020                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
1021            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1022          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1023            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1024            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1025              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 830  sub _get_next_token ($) { Line 1027  sub _get_next_token ($) {
1027          } else {          } else {
1028            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1029          }          }
1030          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1031          ## reconsume          ## reconsume
1032    
1033          !!!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 1039  sub _get_next_token ($) {
1039          !!!next-input-character;          !!!next-input-character;
1040          redo A;          redo A;
1041        }        }
1042      } elsif ($self->{state} eq 'attribute value (single-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1043        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_input_character} == 0x0027) { # '
1044          $self->{state} = 'before attribute name';          $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1045          !!!next-input-character;          !!!next-input-character;
1046          redo A;          redo A;
1047        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
1048          $self->{last_attribute_value_state} = 'attribute value (single-quoted)';          $self->{last_attribute_value_state} = $self->{state};
1049          $self->{state} = 'entity in attribute value';          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1050          !!!next-input-character;          !!!next-input-character;
1051          redo A;          redo A;
1052        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1053          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1054          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1055            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
1056                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
1057            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1058          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1059            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1060            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1061              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 866  sub _get_next_token ($) { Line 1063  sub _get_next_token ($) {
1063          } else {          } else {
1064            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1065          }          }
1066          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1067          ## reconsume          ## reconsume
1068    
1069          !!!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 1075  sub _get_next_token ($) {
1075          !!!next-input-character;          !!!next-input-character;
1076          redo A;          redo A;
1077        }        }
1078      } elsif ($self->{state} eq 'attribute value (unquoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1079        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1080            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1081            $self->{next_input_character} == 0x000B or # HT            $self->{next_input_character} == 0x000B or # HT
1082            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1083            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
1084          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1085          !!!next-input-character;          !!!next-input-character;
1086          redo A;          redo A;
1087        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
1088          $self->{last_attribute_value_state} = 'attribute value (unquoted)';          $self->{last_attribute_value_state} = $self->{state};
1089          $self->{state} = 'entity in attribute value';          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1090          !!!next-input-character;          !!!next-input-character;
1091          redo A;          redo A;
1092        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1093          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1094            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
1095                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
1096            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1097          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1098            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1099            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1100              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 905  sub _get_next_token ($) { Line 1102  sub _get_next_token ($) {
1102          } else {          } else {
1103            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1104          }          }
1105          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1106          !!!next-input-character;          !!!next-input-character;
1107    
1108          !!!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 1110  sub _get_next_token ($) {
1110          redo A;          redo A;
1111        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1112          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1113          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1114            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
1115                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
1116            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1117          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1118            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1119            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1120              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 925  sub _get_next_token ($) { Line 1122  sub _get_next_token ($) {
1122          } else {          } else {
1123            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1124          }          }
1125          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1126          ## reconsume          ## reconsume
1127    
1128          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1129    
1130          redo A;          redo A;
1131        } else {        } else {
1132            if ({
1133                 0x0022 => 1, # "
1134                 0x0027 => 1, # '
1135                 0x003D => 1, # =
1136                }->{$self->{next_input_character}}) {
1137              !!!parse-error (type => 'bad attribute value');
1138            }
1139          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});
1140          ## Stay in the state          ## Stay in the state
1141          !!!next-input-character;          !!!next-input-character;
1142          redo A;          redo A;
1143        }        }
1144      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {
1145        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);        my $token = $self->_tokenize_attempt_to_consume_an_entity
1146              (1,
1147               $self->{last_attribute_value_state}
1148                 == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE ? 0x0022 : # "
1149               $self->{last_attribute_value_state}
1150                 == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE ? 0x0027 : # '
1151               -1);
1152    
1153        unless (defined $token) {        unless (defined $token) {
1154          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
1155        } else {        } else {
1156          $self->{current_attribute}->{value} .= $token->{data};          $self->{current_attribute}->{value} .= $token->{data};
1157            $self->{current_attribute}->{has_reference} = $token->{has_reference};
1158          ## 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"
1159        }        }
1160    
1161        $self->{state} = $self->{last_attribute_value_state};        $self->{state} = $self->{last_attribute_value_state};
1162        # next-input-character is already done        # next-input-character is already done
1163        redo A;        redo A;
1164      } elsif ($self->{state} eq 'bogus comment') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1165          if ($self->{next_input_character} == 0x0009 or # HT
1166              $self->{next_input_character} == 0x000A or # LF
1167              $self->{next_input_character} == 0x000B or # VT
1168              $self->{next_input_character} == 0x000C or # FF
1169              $self->{next_input_character} == 0x0020) { # SP
1170            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1171            !!!next-input-character;
1172            redo A;
1173          } elsif ($self->{next_input_character} == 0x003E) { # >
1174            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1175              $self->{current_token}->{first_start_tag}
1176                  = not defined $self->{last_emitted_start_tag_name};
1177              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1178            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1179              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1180              if ($self->{current_token}->{attributes}) {
1181                !!!parse-error (type => 'end tag attribute');
1182              }
1183            } else {
1184              die "$0: $self->{current_token}->{type}: Unknown token type";
1185            }
1186            $self->{state} = DATA_STATE;
1187            !!!next-input-character;
1188    
1189            !!!emit ($self->{current_token}); # start tag or end tag
1190    
1191            redo A;
1192          } elsif ($self->{next_input_character} == 0x002F) { # /
1193            !!!next-input-character;
1194            if ($self->{next_input_character} == 0x003E and # >
1195                $self->{current_token}->{type} == START_TAG_TOKEN and
1196                $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
1197              # permitted slash
1198              #
1199            } else {
1200              !!!parse-error (type => 'nestc');
1201            }
1202            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1203            # next-input-character is already done
1204            redo A;
1205          } else {
1206            !!!parse-error (type => 'no space between attributes');
1207            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1208            ## reconsume
1209            redo A;
1210          }
1211        } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1212        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1213                
1214        my $token = {type => 'comment', data => ''};        my $token = {type => COMMENT_TOKEN, data => ''};
1215    
1216        BC: {        BC: {
1217          if ($self->{next_input_character} == 0x003E) { # >          if ($self->{next_input_character} == 0x003E) { # >
1218            $self->{state} = 'data';            $self->{state} = DATA_STATE;
1219            !!!next-input-character;            !!!next-input-character;
1220    
1221            !!!emit ($token);            !!!emit ($token);
1222    
1223            redo A;            redo A;
1224          } elsif ($self->{next_input_character} == -1) {          } elsif ($self->{next_input_character} == -1) {
1225            $self->{state} = 'data';            $self->{state} = DATA_STATE;
1226            ## reconsume            ## reconsume
1227    
1228            !!!emit ($token);            !!!emit ($token);
# Line 976  sub _get_next_token ($) { Line 1234  sub _get_next_token ($) {
1234            redo BC;            redo BC;
1235          }          }
1236        } # BC        } # BC
1237      } elsif ($self->{state} eq 'markup declaration open') {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1238        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1239    
1240        my @next_char;        my @next_char;
# Line 986  sub _get_next_token ($) { Line 1244  sub _get_next_token ($) {
1244          !!!next-input-character;          !!!next-input-character;
1245          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_input_character};
1246          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_input_character} == 0x002D) { # -
1247            $self->{current_token} = {type => 'comment', data => ''};            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};
1248            $self->{state} = 'comment start';            $self->{state} = COMMENT_START_STATE;
1249            !!!next-input-character;            !!!next-input-character;
1250            redo A;            redo A;
1251          }          }
# Line 1018  sub _get_next_token ($) { Line 1276  sub _get_next_token ($) {
1276                    if ($self->{next_input_character} == 0x0045 or # E                    if ($self->{next_input_character} == 0x0045 or # E
1277                        $self->{next_input_character} == 0x0065) { # e                        $self->{next_input_character} == 0x0065) { # e
1278                      ## ISSUE: What a stupid code this is!                      ## ISSUE: What a stupid code this is!
1279                      $self->{state} = 'DOCTYPE';                      $self->{state} = DOCTYPE_STATE;
1280                      !!!next-input-character;                      !!!next-input-character;
1281                      redo A;                      redo A;
1282                    }                    }
# Line 1032  sub _get_next_token ($) { Line 1290  sub _get_next_token ($) {
1290        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'bogus comment');
1291        $self->{next_input_character} = shift @next_char;        $self->{next_input_character} = shift @next_char;
1292        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
1293        $self->{state} = 'bogus comment';        $self->{state} = BOGUS_COMMENT_STATE;
1294        redo A;        redo A;
1295                
1296        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
1297        ## 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?
1298      } elsif ($self->{state} eq 'comment start') {      } elsif ($self->{state} == COMMENT_START_STATE) {
1299        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1300          $self->{state} = 'comment start dash';          $self->{state} = COMMENT_START_DASH_STATE;
1301          !!!next-input-character;          !!!next-input-character;
1302          redo A;          redo A;
1303        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1304          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
1305          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1306          !!!next-input-character;          !!!next-input-character;
1307    
1308          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1052  sub _get_next_token ($) { Line 1310  sub _get_next_token ($) {
1310          redo A;          redo A;
1311        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1312          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1313          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1314          ## reconsume          ## reconsume
1315    
1316          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1061  sub _get_next_token ($) { Line 1319  sub _get_next_token ($) {
1319        } else {        } else {
1320          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
1321              .= chr ($self->{next_input_character});              .= chr ($self->{next_input_character});
1322          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1323          !!!next-input-character;          !!!next-input-character;
1324          redo A;          redo A;
1325        }        }
1326      } elsif ($self->{state} eq 'comment start dash') {      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
1327        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1328          $self->{state} = 'comment end';          $self->{state} = COMMENT_END_STATE;
1329          !!!next-input-character;          !!!next-input-character;
1330          redo A;          redo A;
1331        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1332          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
1333          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1334          !!!next-input-character;          !!!next-input-character;
1335    
1336          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1080  sub _get_next_token ($) { Line 1338  sub _get_next_token ($) {
1338          redo A;          redo A;
1339        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1340          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1341          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1342          ## reconsume          ## reconsume
1343    
1344          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1089  sub _get_next_token ($) { Line 1347  sub _get_next_token ($) {
1347        } else {        } else {
1348          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
1349              .= '-' . chr ($self->{next_input_character});              .= '-' . chr ($self->{next_input_character});
1350          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1351          !!!next-input-character;          !!!next-input-character;
1352          redo A;          redo A;
1353        }        }
1354      } elsif ($self->{state} eq 'comment') {      } elsif ($self->{state} == COMMENT_STATE) {
1355        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1356          $self->{state} = 'comment end dash';          $self->{state} = COMMENT_END_DASH_STATE;
1357          !!!next-input-character;          !!!next-input-character;
1358          redo A;          redo A;
1359        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1360          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1361          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1362          ## reconsume          ## reconsume
1363    
1364          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1112  sub _get_next_token ($) { Line 1370  sub _get_next_token ($) {
1370          !!!next-input-character;          !!!next-input-character;
1371          redo A;          redo A;
1372        }        }
1373      } elsif ($self->{state} eq 'comment end dash') {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
1374        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1375          $self->{state} = 'comment end';          $self->{state} = COMMENT_END_STATE;
1376          !!!next-input-character;          !!!next-input-character;
1377          redo A;          redo A;
1378        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1379          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1380          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1381          ## reconsume          ## reconsume
1382    
1383          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1127  sub _get_next_token ($) { Line 1385  sub _get_next_token ($) {
1385          redo A;          redo A;
1386        } else {        } else {
1387          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment
1388          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1389          !!!next-input-character;          !!!next-input-character;
1390          redo A;          redo A;
1391        }        }
1392      } elsif ($self->{state} eq 'comment end') {      } elsif ($self->{state} == COMMENT_END_STATE) {
1393        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_input_character} == 0x003E) { # >
1394          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1395          !!!next-input-character;          !!!next-input-character;
1396    
1397          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1147  sub _get_next_token ($) { Line 1405  sub _get_next_token ($) {
1405          redo A;          redo A;
1406        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1407          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1408          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1409          ## reconsume          ## reconsume
1410    
1411          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1156  sub _get_next_token ($) { Line 1414  sub _get_next_token ($) {
1414        } else {        } else {
1415          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment');
1416          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment
1417          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1418          !!!next-input-character;          !!!next-input-character;
1419          redo A;          redo A;
1420        }        }
1421      } elsif ($self->{state} eq 'DOCTYPE') {      } elsif ($self->{state} == DOCTYPE_STATE) {
1422        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1423            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1424            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
1425            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1426            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
1427          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1428          !!!next-input-character;          !!!next-input-character;
1429          redo A;          redo A;
1430        } else {        } else {
1431          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
1432          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1433          ## reconsume          ## reconsume
1434          redo A;          redo A;
1435        }        }
1436      } elsif ($self->{state} eq 'before DOCTYPE name') {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
1437        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1438            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1439            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 1186  sub _get_next_token ($) { Line 1444  sub _get_next_token ($) {
1444          redo A;          redo A;
1445        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1446          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1447          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1448          !!!next-input-character;          !!!next-input-character;
1449    
1450          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect
1451    
1452          redo A;          redo A;
1453        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1454          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1455          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1456          ## reconsume          ## reconsume
1457    
1458          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect
1459    
1460          redo A;          redo A;
1461        } else {        } else {
1462          $self->{current_token}          $self->{current_token}
1463              = {type => 'DOCTYPE',              = {type => DOCTYPE_TOKEN,
1464                 name => chr ($self->{next_input_character}),                 name => chr ($self->{next_input_character}),
1465                 correct => 1};                 correct => 1};
1466  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
1467          $self->{state} = 'DOCTYPE name';          $self->{state} = DOCTYPE_NAME_STATE;
1468          !!!next-input-character;          !!!next-input-character;
1469          redo A;          redo A;
1470        }        }
1471      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
1472  ## ISSUE: Redundant "First," in the spec.  ## ISSUE: Redundant "First," in the spec.
1473        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1474            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1475            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
1476            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1477            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
1478          $self->{state} = 'after DOCTYPE name';          $self->{state} = AFTER_DOCTYPE_NAME_STATE;
1479          !!!next-input-character;          !!!next-input-character;
1480          redo A;          redo A;
1481        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1482          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1483          !!!next-input-character;          !!!next-input-character;
1484    
1485          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1229  sub _get_next_token ($) { Line 1487  sub _get_next_token ($) {
1487          redo A;          redo A;
1488        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1489          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1490          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1491          ## reconsume          ## reconsume
1492    
1493          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1243  sub _get_next_token ($) { Line 1501  sub _get_next_token ($) {
1501          !!!next-input-character;          !!!next-input-character;
1502          redo A;          redo A;
1503        }        }
1504      } elsif ($self->{state} eq 'after DOCTYPE name') {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
1505        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1506            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1507            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 1253  sub _get_next_token ($) { Line 1511  sub _get_next_token ($) {
1511          !!!next-input-character;          !!!next-input-character;
1512          redo A;          redo A;
1513        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1514          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1515          !!!next-input-character;          !!!next-input-character;
1516    
1517          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1261  sub _get_next_token ($) { Line 1519  sub _get_next_token ($) {
1519          redo A;          redo A;
1520        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1521          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1522          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1523          ## reconsume          ## reconsume
1524    
1525          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1285  sub _get_next_token ($) { Line 1543  sub _get_next_token ($) {
1543                  !!!next-input-character;                  !!!next-input-character;
1544                  if ($self->{next_input_character} == 0x0043 or # C                  if ($self->{next_input_character} == 0x0043 or # C
1545                      $self->{next_input_character} == 0x0063) { # c                      $self->{next_input_character} == 0x0063) { # c
1546                    $self->{state} = 'before DOCTYPE public identifier';                    $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1547                    !!!next-input-character;                    !!!next-input-character;
1548                    redo A;                    redo A;
1549                  }                  }
# Line 1312  sub _get_next_token ($) { Line 1570  sub _get_next_token ($) {
1570                  !!!next-input-character;                  !!!next-input-character;
1571                  if ($self->{next_input_character} == 0x004D or # M                  if ($self->{next_input_character} == 0x004D or # M
1572                      $self->{next_input_character} == 0x006D) { # m                      $self->{next_input_character} == 0x006D) { # m
1573                    $self->{state} = 'before DOCTYPE system identifier';                    $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1574                    !!!next-input-character;                    !!!next-input-character;
1575                    redo A;                    redo A;
1576                  }                  }
# Line 1328  sub _get_next_token ($) { Line 1586  sub _get_next_token ($) {
1586        }        }
1587    
1588        !!!parse-error (type => 'string after DOCTYPE name');        !!!parse-error (type => 'string after DOCTYPE name');
1589        $self->{state} = 'bogus DOCTYPE';        $self->{state} = BOGUS_DOCTYPE_STATE;
1590        # next-input-character is already done        # next-input-character is already done
1591        redo A;        redo A;
1592      } elsif ($self->{state} eq 'before DOCTYPE public identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
1593        if ({        if ({
1594              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1595              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1341  sub _get_next_token ($) { Line 1599  sub _get_next_token ($) {
1599          redo A;          redo A;
1600        } elsif ($self->{next_input_character} eq 0x0022) { # "        } elsif ($self->{next_input_character} eq 0x0022) { # "
1601          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1602          $self->{state} = 'DOCTYPE public identifier (double-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
1603          !!!next-input-character;          !!!next-input-character;
1604          redo A;          redo A;
1605        } elsif ($self->{next_input_character} eq 0x0027) { # '        } elsif ($self->{next_input_character} eq 0x0027) { # '
1606          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1607          $self->{state} = 'DOCTYPE public identifier (single-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
1608          !!!next-input-character;          !!!next-input-character;
1609          redo A;          redo A;
1610        } elsif ($self->{next_input_character} eq 0x003E) { # >        } elsif ($self->{next_input_character} eq 0x003E) { # >
1611          !!!parse-error (type => 'no PUBLIC literal');          !!!parse-error (type => 'no PUBLIC literal');
1612    
1613          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1614          !!!next-input-character;          !!!next-input-character;
1615    
1616          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1362  sub _get_next_token ($) { Line 1620  sub _get_next_token ($) {
1620        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1621          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1622    
1623          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1624          ## reconsume          ## reconsume
1625    
1626          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1371  sub _get_next_token ($) { Line 1629  sub _get_next_token ($) {
1629          redo A;          redo A;
1630        } else {        } else {
1631          !!!parse-error (type => 'string after PUBLIC');          !!!parse-error (type => 'string after PUBLIC');
1632          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1633          !!!next-input-character;          !!!next-input-character;
1634          redo A;          redo A;
1635        }        }
1636      } elsif ($self->{state} eq 'DOCTYPE public identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
1637        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_input_character} == 0x0022) { # "
1638          $self->{state} = 'after DOCTYPE public identifier';          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1639            !!!next-input-character;
1640            redo A;
1641          } elsif ($self->{next_input_character} == 0x003E) { # >
1642            !!!parse-error (type => 'unclosed PUBLIC literal');
1643    
1644            $self->{state} = DATA_STATE;
1645          !!!next-input-character;          !!!next-input-character;
1646    
1647            delete $self->{current_token}->{correct};
1648            !!!emit ($self->{current_token}); # DOCTYPE
1649    
1650          redo A;          redo A;
1651        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1652          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
1653    
1654          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1655          ## reconsume          ## reconsume
1656    
1657          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1397  sub _get_next_token ($) { Line 1665  sub _get_next_token ($) {
1665          !!!next-input-character;          !!!next-input-character;
1666          redo A;          redo A;
1667        }        }
1668      } elsif ($self->{state} eq 'DOCTYPE public identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
1669        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_input_character} == 0x0027) { # '
1670          $self->{state} = 'after DOCTYPE public identifier';          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1671          !!!next-input-character;          !!!next-input-character;
1672          redo A;          redo A;
1673          } elsif ($self->{next_input_character} == 0x003E) { # >
1674            !!!parse-error (type => 'unclosed PUBLIC literal');
1675    
1676            $self->{state} = DATA_STATE;
1677            !!!next-input-character;
1678    
1679            delete $self->{current_token}->{correct};
1680            !!!emit ($self->{current_token}); # DOCTYPE
1681    
1682            redo A;
1683        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1684          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
1685    
1686          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1687          ## reconsume          ## reconsume
1688    
1689          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1419  sub _get_next_token ($) { Line 1697  sub _get_next_token ($) {
1697          !!!next-input-character;          !!!next-input-character;
1698          redo A;          redo A;
1699        }        }
1700      } elsif ($self->{state} eq 'after DOCTYPE public identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
1701        if ({        if ({
1702              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1703              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1429  sub _get_next_token ($) { Line 1707  sub _get_next_token ($) {
1707          redo A;          redo A;
1708        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_input_character} == 0x0022) { # "
1709          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1710          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
1711          !!!next-input-character;          !!!next-input-character;
1712          redo A;          redo A;
1713        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_input_character} == 0x0027) { # '
1714          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1715          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
1716          !!!next-input-character;          !!!next-input-character;
1717          redo A;          redo A;
1718        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1719          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1720          !!!next-input-character;          !!!next-input-character;
1721    
1722          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1447  sub _get_next_token ($) { Line 1725  sub _get_next_token ($) {
1725        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1726          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1727    
1728          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1729          ## reconsume          ## reconsume
1730    
1731          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1456  sub _get_next_token ($) { Line 1734  sub _get_next_token ($) {
1734          redo A;          redo A;
1735        } else {        } else {
1736          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after PUBLIC literal');
1737          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1738          !!!next-input-character;          !!!next-input-character;
1739          redo A;          redo A;
1740        }        }
1741      } elsif ($self->{state} eq 'before DOCTYPE system identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
1742        if ({        if ({
1743              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1744              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1470  sub _get_next_token ($) { Line 1748  sub _get_next_token ($) {
1748          redo A;          redo A;
1749        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_input_character} == 0x0022) { # "
1750          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1751          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
1752          !!!next-input-character;          !!!next-input-character;
1753          redo A;          redo A;
1754        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_input_character} == 0x0027) { # '
1755          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1756          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
1757          !!!next-input-character;          !!!next-input-character;
1758          redo A;          redo A;
1759        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1760          !!!parse-error (type => 'no SYSTEM literal');          !!!parse-error (type => 'no SYSTEM literal');
1761          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1762          !!!next-input-character;          !!!next-input-character;
1763    
1764          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1490  sub _get_next_token ($) { Line 1768  sub _get_next_token ($) {
1768        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1769          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1770    
1771          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1772          ## reconsume          ## reconsume
1773    
1774          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1499  sub _get_next_token ($) { Line 1777  sub _get_next_token ($) {
1777          redo A;          redo A;
1778        } else {        } else {
1779          !!!parse-error (type => 'string after SYSTEM');          !!!parse-error (type => 'string after SYSTEM');
1780          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1781          !!!next-input-character;          !!!next-input-character;
1782          redo A;          redo A;
1783        }        }
1784      } elsif ($self->{state} eq 'DOCTYPE system identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
1785        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_input_character} == 0x0022) { # "
1786          $self->{state} = 'after DOCTYPE system identifier';          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1787          !!!next-input-character;          !!!next-input-character;
1788          redo A;          redo A;
1789          } elsif ($self->{next_input_character} == 0x003E) { # >
1790            !!!parse-error (type => 'unclosed PUBLIC literal');
1791    
1792            $self->{state} = DATA_STATE;
1793            !!!next-input-character;
1794    
1795            delete $self->{current_token}->{correct};
1796            !!!emit ($self->{current_token}); # DOCTYPE
1797    
1798            redo A;
1799        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1800          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
1801    
1802          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1803          ## reconsume          ## reconsume
1804    
1805          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1525  sub _get_next_token ($) { Line 1813  sub _get_next_token ($) {
1813          !!!next-input-character;          !!!next-input-character;
1814          redo A;          redo A;
1815        }        }
1816      } elsif ($self->{state} eq 'DOCTYPE system identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
1817        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_input_character} == 0x0027) { # '
1818          $self->{state} = 'after DOCTYPE system identifier';          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1819          !!!next-input-character;          !!!next-input-character;
1820          redo A;          redo A;
1821          } elsif ($self->{next_input_character} == 0x003E) { # >
1822            !!!parse-error (type => 'unclosed PUBLIC literal');
1823    
1824            $self->{state} = DATA_STATE;
1825            !!!next-input-character;
1826    
1827            delete $self->{current_token}->{correct};
1828            !!!emit ($self->{current_token}); # DOCTYPE
1829    
1830            redo A;
1831        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1832          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
1833    
1834          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1835          ## reconsume          ## reconsume
1836    
1837          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1547  sub _get_next_token ($) { Line 1845  sub _get_next_token ($) {
1845          !!!next-input-character;          !!!next-input-character;
1846          redo A;          redo A;
1847        }        }
1848      } elsif ($self->{state} eq 'after DOCTYPE system identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
1849        if ({        if ({
1850              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1851              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1556  sub _get_next_token ($) { Line 1854  sub _get_next_token ($) {
1854          !!!next-input-character;          !!!next-input-character;
1855          redo A;          redo A;
1856        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1857          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1858          !!!next-input-character;          !!!next-input-character;
1859    
1860          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1565  sub _get_next_token ($) { Line 1863  sub _get_next_token ($) {
1863        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1864          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1865    
1866          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1867          ## reconsume          ## reconsume
1868    
1869          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1574  sub _get_next_token ($) { Line 1872  sub _get_next_token ($) {
1872          redo A;          redo A;
1873        } else {        } else {
1874          !!!parse-error (type => 'string after SYSTEM literal');          !!!parse-error (type => 'string after SYSTEM literal');
1875          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1876          !!!next-input-character;          !!!next-input-character;
1877          redo A;          redo A;
1878        }        }
1879      } elsif ($self->{state} eq 'bogus DOCTYPE') {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
1880        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_input_character} == 0x003E) { # >
1881          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1882          !!!next-input-character;          !!!next-input-character;
1883    
1884          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1589  sub _get_next_token ($) { Line 1887  sub _get_next_token ($) {
1887          redo A;          redo A;
1888        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1889          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1890          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1891          ## reconsume          ## reconsume
1892    
1893          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1609  sub _get_next_token ($) { Line 1907  sub _get_next_token ($) {
1907    die "$0: _get_next_token: unexpected case";    die "$0: _get_next_token: unexpected case";
1908  } # _get_next_token  } # _get_next_token
1909    
1910  sub _tokenize_attempt_to_consume_an_entity ($$) {  sub _tokenize_attempt_to_consume_an_entity ($$$) {
1911    my ($self, $in_attr) = @_;    my ($self, $in_attr, $additional) = @_;
1912    
1913    if ({    if ({
1914         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
1915         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
1916           $additional => 1,
1917        }->{$self->{next_input_character}}) {        }->{$self->{next_input_character}}) {
1918      ## Don't consume      ## Don't consume
1919      ## No error      ## No error
# Line 1670  sub _tokenize_attempt_to_consume_an_enti Line 1969  sub _tokenize_attempt_to_consume_an_enti
1969            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
1970          }          }
1971    
1972          return {type => 'character', data => chr $code};          return {type => CHARACTER_TOKEN, data => chr $code,
1973                    has_reference => 1};
1974        } # X        } # X
1975      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_input_character} and
1976               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_input_character} <= 0x0039) { # 0..9
# Line 1705  sub _tokenize_attempt_to_consume_an_enti Line 2005  sub _tokenize_attempt_to_consume_an_enti
2005          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
2006        }        }
2007                
2008        return {type => 'character', data => chr $code};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1};
2009      } else {      } else {
2010        !!!parse-error (type => 'bare nero');        !!!parse-error (type => 'bare nero');
2011        !!!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 2053  sub _tokenize_attempt_to_consume_an_enti
2053      }      }
2054            
2055      if ($match > 0) {      if ($match > 0) {
2056        return {type => 'character', data => $value};        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};
2057      } elsif ($match < 0) {      } elsif ($match < 0) {
2058        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc');
2059        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
2060          return {type => 'character', data => '&'.$entity_name};          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};
2061        } else {        } else {
2062          return {type => 'character', data => $value};          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};
2063        }        }
2064      } else {      } else {
2065        !!!parse-error (type => 'bare ero');        !!!parse-error (type => 'bare ero');
2066        ## NOTE: No characters are consumed in the spec.        ## NOTE: "No characters are consumed" in the spec.
2067        return {type => 'character', data => '&'.$value};        return {type => CHARACTER_TOKEN, data => '&'.$value};
2068      }      }
2069    } else {    } else {
2070      ## no characters are consumed      ## no characters are consumed
# Line 1806  sub _construct_tree ($) { Line 2106  sub _construct_tree ($) {
2106        
2107    !!!next-token;    !!!next-token;
2108    
2109    $self->{insertion_mode} = 'before head';    $self->{insertion_mode} = BEFORE_HEAD_IM;
2110    undef $self->{form_element};    undef $self->{form_element};
2111    undef $self->{head_element};    undef $self->{head_element};
2112    $self->{open_elements} = [];    $self->{open_elements} = [];
# Line 1820  sub _construct_tree ($) { Line 2120  sub _construct_tree ($) {
2120  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
2121    my $self = shift;    my $self = shift;
2122    INITIAL: {    INITIAL: {
2123      if ($token->{type} eq 'DOCTYPE') {      if ($token->{type} == DOCTYPE_TOKEN) {
2124        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
2125        ## error, switch to a conformance checking mode for another        ## error, switch to a conformance checking mode for another
2126        ## language.        ## language.
# Line 1900  sub _tree_construction_initial ($) { Line 2200  sub _tree_construction_initial ($) {
2200            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,
2201            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,
2202            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,
2203              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//EN" => 1,
2204              "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//EN" => 1,
2205              "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//EN" => 1,
2206            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,
2207            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,
2208            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,
# Line 1947  sub _tree_construction_initial ($) { Line 2250  sub _tree_construction_initial ($) {
2250        !!!next-token;        !!!next-token;
2251        return;        return;
2252      } elsif ({      } elsif ({
2253                'start tag' => 1,                START_TAG_TOKEN, 1,
2254                'end tag' => 1,                END_TAG_TOKEN, 1,
2255                'end-of-file' => 1,                END_OF_FILE_TOKEN, 1,
2256               }->{$token->{type}}) {               }->{$token->{type}}) {
2257        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE');
2258        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
2259        ## Go to the root element phase        ## Go to the root element phase
2260        ## reprocess        ## reprocess
2261        return;        return;
2262      } elsif ($token->{type} eq 'character') {      } elsif ($token->{type} == CHARACTER_TOKEN) {
2263        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2264          ## Ignore the token          ## Ignore the token
2265    
# Line 1972  sub _tree_construction_initial ($) { Line 2275  sub _tree_construction_initial ($) {
2275        ## Go to the root element phase        ## Go to the root element phase
2276        ## reprocess        ## reprocess
2277        return;        return;
2278      } elsif ($token->{type} eq 'comment') {      } elsif ($token->{type} == COMMENT_TOKEN) {
2279        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
2280        $self->{document}->append_child ($comment);        $self->{document}->append_child ($comment);
2281                
# Line 1980  sub _tree_construction_initial ($) { Line 2283  sub _tree_construction_initial ($) {
2283        !!!next-token;        !!!next-token;
2284        redo INITIAL;        redo INITIAL;
2285      } else {      } else {
2286        die "$0: $token->{type}: Unknown token";        die "$0: $token->{type}: Unknown token type";
2287      }      }
2288    } # INITIAL    } # INITIAL
2289  } # _tree_construction_initial  } # _tree_construction_initial
# Line 1989  sub _tree_construction_root_element ($) Line 2292  sub _tree_construction_root_element ($)
2292    my $self = shift;    my $self = shift;
2293        
2294    B: {    B: {
2295        if ($token->{type} eq 'DOCTYPE') {        if ($token->{type} == DOCTYPE_TOKEN) {
2296          !!!parse-error (type => 'in html:#DOCTYPE');          !!!parse-error (type => 'in html:#DOCTYPE');
2297          ## Ignore the token          ## Ignore the token
2298          ## Stay in the phase          ## Stay in the phase
2299          !!!next-token;          !!!next-token;
2300          redo B;          redo B;
2301        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{type} == COMMENT_TOKEN) {
2302          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
2303          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
2304          ## Stay in the phase          ## Stay in the phase
2305          !!!next-token;          !!!next-token;
2306          redo B;          redo B;
2307        } elsif ($token->{type} eq 'character') {        } elsif ($token->{type} == CHARACTER_TOKEN) {
2308          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2309            ## Ignore the token.            ## Ignore the token.
2310    
# Line 2011  sub _tree_construction_root_element ($) Line 2314  sub _tree_construction_root_element ($)
2314              redo B;              redo B;
2315            }            }
2316          }          }
2317    
2318            $self->{application_cache_selection}->(undef);
2319    
2320            #
2321          } elsif ($token->{type} == START_TAG_TOKEN) {
2322            if ($token->{tag_name} eq 'html' and
2323                $token->{attributes}->{manifest}) {
2324              $self->{application_cache_selection}
2325                   ->($token->{attributes}->{manifest}->{value});
2326              ## ISSUE: No relative reference resolution?
2327            } else {
2328              $self->{application_cache_selection}->(undef);
2329            }
2330    
2331            ## ISSUE: There is an issue in the spec
2332          #          #
2333        } elsif ({        } elsif ({
2334                  'start tag' => 1,                  END_TAG_TOKEN, 1,
2335                  'end tag' => 1,                  END_OF_FILE_TOKEN, 1,
                 'end-of-file' => 1,  
2336                 }->{$token->{type}}) {                 }->{$token->{type}}) {
2337            $self->{application_cache_selection}->(undef);
2338    
2339          ## ISSUE: There is an issue in the spec          ## ISSUE: There is an issue in the spec
2340          #          #
2341        } else {        } else {
2342          die "$0: $token->{type}: Unknown token";          die "$0: $token->{type}: Unknown token type";
2343        }        }
2344    
2345        my $root_element; !!!create-element ($root_element, 'html');        my $root_element; !!!create-element ($root_element, 'html');
2346        $self->{document}->append_child ($root_element);        $self->{document}->append_child ($root_element);
2347        push @{$self->{open_elements}}, [$root_element, 'html'];        push @{$self->{open_elements}}, [$root_element, 'html'];
# Line 2062  sub _reset_insertion_mode ($) { Line 2382  sub _reset_insertion_mode ($) {
2382            
2383        ## Step 4..13        ## Step 4..13
2384        my $new_mode = {        my $new_mode = {
2385                        select => 'in select',                        select => IN_SELECT_IM,
2386                        td => 'in cell',                        td => IN_CELL_IM,
2387                        th => 'in cell',                        th => IN_CELL_IM,
2388                        tr => 'in row',                        tr => IN_ROW_IM,
2389                        tbody => 'in table body',                        tbody => IN_TABLE_BODY_IM,
2390                        thead => 'in table body',                        thead => IN_TABLE_BODY_IM,
2391                        tfoot => 'in table body',                        tfoot => IN_TABLE_BODY_IM,
2392                        caption => 'in caption',                        caption => IN_CAPTION_IM,
2393                        colgroup => 'in column group',                        colgroup => IN_COLUMN_GROUP_IM,
2394                        table => 'in table',                        table => IN_TABLE_IM,
2395                        head => 'in body', # not in head!                        head => IN_BODY_IM, # not in head!
2396                        body => 'in body',                        body => IN_BODY_IM,
2397                        frameset => 'in frameset',                        frameset => IN_FRAMESET_IM,
2398                       }->{$node->[1]};                       }->{$node->[1]};
2399        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
2400                
2401        ## Step 14        ## Step 14
2402        if ($node->[1] eq 'html') {        if ($node->[1] eq 'html') {
2403          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
2404            $self->{insertion_mode} = 'before head';            $self->{insertion_mode} = BEFORE_HEAD_IM;
2405          } else {          } else {
2406            $self->{insertion_mode} = 'after head';            $self->{insertion_mode} = AFTER_HEAD_IM;
2407          }          }
2408          return;          return;
2409        }        }
2410                
2411        ## Step 15        ## Step 15
2412        $self->{insertion_mode} = 'in body' and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
2413                
2414        ## Step 16        ## Step 16
2415        $i--;        $i--;
# Line 2203  sub _tree_construction_main ($) { Line 2523  sub _tree_construction_main ($) {
2523      ## Step 4      ## Step 4
2524      my $text = '';      my $text = '';
2525      !!!next-token;      !!!next-token;
2526      while ($token->{type} eq 'character') { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
2527        $text .= $token->{data};        $text .= $token->{data};
2528        !!!next-token;        !!!next-token;
2529      }      }
# Line 2218  sub _tree_construction_main ($) { Line 2538  sub _tree_construction_main ($) {
2538      $self->{content_model} = PCDATA_CONTENT_MODEL;      $self->{content_model} = PCDATA_CONTENT_MODEL;
2539    
2540      ## Step 7      ## Step 7
2541      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) {
2542        ## Ignore the token        ## Ignore the token
2543      } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {      } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {
2544        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!parse-error (type => 'in CDATA:#'.$token->{type});
# Line 2241  sub _tree_construction_main ($) { Line 2561  sub _tree_construction_main ($) {
2561            
2562      my $text = '';      my $text = '';
2563      !!!next-token;      !!!next-token;
2564      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) {
2565        $text .= $token->{data};        $text .= $token->{data};
2566        !!!next-token;        !!!next-token;
2567      } # 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 2571  sub _tree_construction_main ($) {
2571                                
2572      $self->{content_model} = PCDATA_CONTENT_MODEL;      $self->{content_model} = PCDATA_CONTENT_MODEL;
2573    
2574      if ($token->{type} eq 'end tag' and      if ($token->{type} == END_TAG_TOKEN and
2575          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
2576        ## Ignore the token        ## Ignore the token
2577      } else {      } else {
# Line 2497  sub _tree_construction_main ($) { Line 2817  sub _tree_construction_main ($) {
2817    my $insert;    my $insert;
2818    
2819    B: {    B: {
2820      if ($token->{type} eq 'DOCTYPE') {      if ($token->{type} == DOCTYPE_TOKEN) {
2821        !!!parse-error (type => 'DOCTYPE in the middle');        !!!parse-error (type => 'DOCTYPE in the middle');
2822        ## Ignore the token        ## Ignore the token
2823        ## Stay in the phase        ## Stay in the phase
2824        !!!next-token;        !!!next-token;
2825        redo B;        redo B;
2826      } elsif ($token->{type} eq 'end-of-file') {      } elsif ($token->{type} == END_OF_FILE_TOKEN) {
2827        if ($self->{insertion_mode} eq 'after html body' or        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
           $self->{insertion_mode} eq 'after html frameset') {  
2828          #          #
2829        } else {        } else {
2830          ## Generate implied end tags          ## Generate implied end tags
# Line 2514  sub _tree_construction_main ($) { Line 2833  sub _tree_construction_main ($) {
2833               tbody => 1, tfoot=> 1, thead => 1,               tbody => 1, tfoot=> 1, thead => 1,
2834              }->{$self->{open_elements}->[-1]->[1]}) {              }->{$self->{open_elements}->[-1]->[1]}) {
2835            !!!back-token;            !!!back-token;
2836            $token = {type => 'end tag', tag_name => $self->{open_elements}->[-1]->[1]};            $token = {type => END_TAG_TOKEN, tag_name => $self->{open_elements}->[-1]->[1]};
2837            redo B;            redo B;
2838          }          }
2839                    
# Line 2532  sub _tree_construction_main ($) { Line 2851  sub _tree_construction_main ($) {
2851    
2852        ## Stop parsing        ## Stop parsing
2853        last B;        last B;
2854      } elsif ($token->{type} eq 'start tag' and      } elsif ($token->{type} == START_TAG_TOKEN and
2855               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
2856        if ($self->{insertion_mode} eq 'after html body') {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
2857          ## Turn into the main phase          ## Turn into the main phase
2858          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html');
2859          $self->{insertion_mode} = 'after body';          $self->{insertion_mode} = AFTER_BODY_IM;
2860        } elsif ($self->{insertion_mode} eq 'after html frameset') {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
2861          ## Turn into the main phase          ## Turn into the main phase
2862          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html');
2863          $self->{insertion_mode} = 'after frameset';          $self->{insertion_mode} = AFTER_FRAMESET_IM;
2864        }        }
2865    
2866  ## ISSUE: "aa<html>" is not a parse error.  ## ISSUE: "aa<html>" is not a parse error.
# Line 2559  sub _tree_construction_main ($) { Line 2878  sub _tree_construction_main ($) {
2878        }        }
2879        !!!next-token;        !!!next-token;
2880        redo B;        redo B;
2881      } elsif ($token->{type} eq 'comment') {      } elsif ($token->{type} == COMMENT_TOKEN) {
2882        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
2883        if ($self->{insertion_mode} eq 'after html body' or        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
           $self->{insertion_mode} eq 'after html frameset') {  
2884          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
2885        } elsif ($self->{insertion_mode} eq 'after body') {        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
2886          $self->{open_elements}->[0]->[0]->append_child ($comment);          $self->{open_elements}->[0]->[0]->append_child ($comment);
2887        } else {        } else {
2888          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
2889        }        }
2890        !!!next-token;        !!!next-token;
2891        redo B;        redo B;
2892      } elsif ($self->{insertion_mode} eq 'in head' or      } elsif ($self->{insertion_mode} & HEAD_IMS) {
2893               $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') {  
2894          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
2895            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
2896            unless (length $token->{data}) {            unless (length $token->{data}) {
# Line 2584  sub _tree_construction_main ($) { Line 2899  sub _tree_construction_main ($) {
2899            }            }
2900          }          }
2901    
2902          if ($self->{insertion_mode} eq 'before head') {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2903            ## As if <head>            ## As if <head>
2904            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, 'head');
2905            $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 2909  sub _tree_construction_main ($) {
2909            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
2910    
2911            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
2912          } elsif ($self->{insertion_mode} eq 'in head noscript') {          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2913            ## As if </noscript>            ## As if </noscript>
2914            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
2915            !!!parse-error (type => 'in noscript:#character');            !!!parse-error (type => 'in noscript:#character');
# Line 2604  sub _tree_construction_main ($) { Line 2919  sub _tree_construction_main ($) {
2919            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
2920    
2921            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
2922          } elsif ($self->{insertion_mode} eq 'in head') {          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2923            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
2924    
2925            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
# Line 2613  sub _tree_construction_main ($) { Line 2928  sub _tree_construction_main ($) {
2928              ## "after head" insertion mode              ## "after head" insertion mode
2929              ## As if <body>              ## As if <body>
2930              !!!insert-element ('body');              !!!insert-element ('body');
2931              $self->{insertion_mode} = 'in body';              $self->{insertion_mode} = IN_BODY_IM;
2932              ## reprocess              ## reprocess
2933              redo B;              redo B;
2934            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} == START_TAG_TOKEN) {
2935              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
2936                if ($self->{insertion_mode} eq 'before head') {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2937                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});
2938                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2939                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];
2940                  $self->{insertion_mode} = 'in head';                  $self->{insertion_mode} = IN_HEAD_IM;
2941                  !!!next-token;                  !!!next-token;
2942                  redo B;                  redo B;
2943                } elsif ($self->{insertion_mode} ne 'after head') {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2944                    #
2945                  } else {
2946                  !!!parse-error (type => 'in head:head'); # or in head noscript                  !!!parse-error (type => 'in head:head'); # or in head noscript
2947                  ## Ignore the token                  ## Ignore the token
2948                  !!!next-token;                  !!!next-token;
2949                  redo B;                  redo B;
               } else {  
                 #  
2950                }                }
2951              } elsif ($self->{insertion_mode} eq 'before head') {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2952                ## As if <head>                ## As if <head>
2953                !!!create-element ($self->{head_element}, 'head');                !!!create-element ($self->{head_element}, 'head');
2954                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2955                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2956    
2957                $self->{insertion_mode} = 'in head';                $self->{insertion_mode} = IN_HEAD_IM;
2958                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
2959              }              }
2960    
2961              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
2962                if ($self->{insertion_mode} eq 'in head noscript') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2963                  ## As if </noscript>                  ## As if </noscript>
2964                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
2965                  !!!parse-error (type => 'in noscript:base');                  !!!parse-error (type => 'in noscript:base');
2966                                
2967                  $self->{insertion_mode} = 'in head';                  $self->{insertion_mode} = IN_HEAD_IM;
2968                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
2969                }                }
2970    
2971                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
2972                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2973                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2974                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2975                }                }
2976                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
2977                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2978                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2979                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2980                !!!next-token;                !!!next-token;
2981                redo B;                redo B;
2982              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
2983                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
2984                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2985                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2986                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2987                }                }
2988                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
2989                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2990                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2991                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2992                !!!next-token;                !!!next-token;
2993                redo B;                redo B;
2994              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
2995                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
2996                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2997                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2998                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2999                }                }
3000                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
3001                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.
3002    
3003                unless ($self->{confident}) {                unless ($self->{confident}) {
                 my $charset;  
3004                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) { ## TODO: And if supported
3005                    $charset = $token->{attributes}->{charset}->{value};                    $self->{change_encoding}
3006                  }                        ->($self, $token->{attributes}->{charset}->{value});
3007                  if ($token->{attributes}->{'http-equiv'}) {                    
3008                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
3009                          ->set_user_data (manakai_has_reference =>
3010                                               $token->{attributes}->{charset}
3011                                                   ->{has_reference});
3012                    } elsif ($token->{attributes}->{content}) {
3013                    ## 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.
3014                    if ($token->{attributes}->{'http-equiv'}->{value}                    if ($token->{attributes}->{content}->{value}
3015                        =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
3016                              [\x09-\x0D\x20]*=
3017                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
3018                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
3019                      $charset = defined $1 ? $1 : defined $2 ? $2 : $3;                      $self->{change_encoding}
3020                    } ## TODO: And if supported                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);
3021                        $meta_el->[0]->get_attribute_node_ns (undef, 'content')
3022                            ->set_user_data (manakai_has_reference =>
3023                                                 $token->{attributes}->{content}
3024                                                       ->{has_reference});
3025                      }
3026                    }
3027                  } else {
3028                    if ($token->{attributes}->{charset}) {
3029                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
3030                          ->set_user_data (manakai_has_reference =>
3031                                               $token->{attributes}->{charset}
3032                                                   ->{has_reference});
3033                    }
3034                    if ($token->{attributes}->{content}) {
3035                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
3036                          ->set_user_data (manakai_has_reference =>
3037                                               $token->{attributes}->{content}
3038                                                   ->{has_reference});
3039                  }                  }
                 ## TODO: Change the encoding  
3040                }                }
3041    
               ## TODO: Extracting |charset| from |meta|.  
3042                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
3043                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3044                !!!next-token;                !!!next-token;
3045                redo B;                redo B;
3046              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
3047                if ($self->{insertion_mode} eq 'in head noscript') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3048                  ## As if </noscript>                  ## As if </noscript>
3049                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3050                  !!!parse-error (type => 'in noscript:title');                  !!!parse-error (type => 'in noscript:title');
3051                                
3052                  $self->{insertion_mode} = 'in head';                  $self->{insertion_mode} = IN_HEAD_IM;
3053                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3054                } elsif ($self->{insertion_mode} eq 'after head') {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3055                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3056                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3057                }                }
# Line 2726  sub _tree_construction_main ($) { Line 3062  sub _tree_construction_main ($) {
3062                $parse_rcdata->(RCDATA_CONTENT_MODEL,                $parse_rcdata->(RCDATA_CONTENT_MODEL,
3063                                sub { $parent->append_child ($_[0]) });                                sub { $parent->append_child ($_[0]) });
3064                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
3065                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3066                redo B;                redo B;
3067              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style') {
3068                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
3069                ## insertion mode 'in head')                ## insertion mode IN_HEAD_IM)
3070                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3071                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3072                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3073                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3074                }                }
3075                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
3076                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
3077                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3078                redo B;                redo B;
3079              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
3080                if ($self->{insertion_mode} eq 'in head') {                if ($self->{insertion_mode} == IN_HEAD_IM) {
3081                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
3082                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3083                  $self->{insertion_mode} = 'in head noscript';                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
3084                  !!!next-token;                  !!!next-token;
3085                  redo B;                  redo B;
3086                } elsif ($self->{insertion_mode} eq 'in head noscript') {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3087                  !!!parse-error (type => 'in noscript:noscript');                  !!!parse-error (type => 'in noscript:noscript');
3088                  ## Ignore the token                  ## Ignore the token
3089                  !!!next-token;                  !!!next-token;
# Line 2756  sub _tree_construction_main ($) { Line 3092  sub _tree_construction_main ($) {
3092                  #                  #
3093                }                }
3094              } elsif ($token->{tag_name} eq 'script') {              } elsif ($token->{tag_name} eq 'script') {
3095                if ($self->{insertion_mode} eq 'in head noscript') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3096                  ## As if </noscript>                  ## As if </noscript>
3097                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3098                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:script');
3099                                
3100                  $self->{insertion_mode} = 'in head';                  $self->{insertion_mode} = IN_HEAD_IM;
3101                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3102                } elsif ($self->{insertion_mode} eq 'after head') {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3103                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3104                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3105                }                }
# Line 2771  sub _tree_construction_main ($) { Line 3107  sub _tree_construction_main ($) {
3107                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3108                $script_start_tag->($insert_to_current);                $script_start_tag->($insert_to_current);
3109                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
3110                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3111                redo B;                redo B;
3112              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
3113                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
3114                if ($self->{insertion_mode} eq 'in head noscript') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3115                  ## As if </noscript>                  ## As if </noscript>
3116                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3117                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});
# Line 2785  sub _tree_construction_main ($) { Line 3121  sub _tree_construction_main ($) {
3121                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3122                                    
3123                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
3124                } elsif ($self->{insertion_mode} eq 'in head') {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3125                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3126                                    
3127                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
# Line 2793  sub _tree_construction_main ($) { Line 3129  sub _tree_construction_main ($) {
3129    
3130                ## "after head" insertion mode                ## "after head" insertion mode
3131                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
3132                $self->{insertion_mode} = 'in '.$token->{tag_name};                if ($token->{tag_name} eq 'body') {
3133                    $self->{insertion_mode} = IN_BODY_IM;
3134                  } elsif ($token->{tag_name} eq 'frameset') {
3135                    $self->{insertion_mode} = IN_FRAMESET_IM;
3136                  } else {
3137                    die "$0: tag name: $self->{tag_name}";
3138                  }
3139                !!!next-token;                !!!next-token;
3140                redo B;                redo B;
3141              } else {              } else {
3142                #                #
3143              }              }
3144    
3145              if ($self->{insertion_mode} eq 'in head noscript') {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3146                ## As if </noscript>                ## As if </noscript>
3147                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3148                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
# Line 2810  sub _tree_construction_main ($) { Line 3152  sub _tree_construction_main ($) {
3152                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3153    
3154                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
3155              } elsif ($self->{insertion_mode} eq 'in head') {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3156                ## As if </head>                ## As if </head>
3157                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3158    
# Line 2820  sub _tree_construction_main ($) { Line 3162  sub _tree_construction_main ($) {
3162              ## "after head" insertion mode              ## "after head" insertion mode
3163              ## As if <body>              ## As if <body>
3164              !!!insert-element ('body');              !!!insert-element ('body');
3165              $self->{insertion_mode} = 'in body';              $self->{insertion_mode} = IN_BODY_IM;
3166              ## reprocess              ## reprocess
3167              redo B;              redo B;
3168            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
3169              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
3170                if ($self->{insertion_mode} eq 'before head') {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3171                  ## As if <head>                  ## As if <head>
3172                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, 'head');
3173                  $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 3175  sub _tree_construction_main ($) {
3175    
3176                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3177                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3178                  $self->{insertion_mode} = 'after head';                  $self->{insertion_mode} = AFTER_HEAD_IM;
3179                  !!!next-token;                  !!!next-token;
3180                  redo B;                  redo B;
3181                } elsif ($self->{insertion_mode} eq 'in head noscript') {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3182                  ## As if </noscript>                  ## As if </noscript>
3183                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3184                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:script');
3185                                    
3186                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3187                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3188                  $self->{insertion_mode} = 'after head';                  $self->{insertion_mode} = AFTER_HEAD_IM;
3189                  !!!next-token;                  !!!next-token;
3190                  redo B;                  redo B;
3191                } elsif ($self->{insertion_mode} eq 'in head') {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3192                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3193                  $self->{insertion_mode} = 'after head';                  $self->{insertion_mode} = AFTER_HEAD_IM;
3194                  !!!next-token;                  !!!next-token;
3195                  redo B;                  redo B;
3196                } else {                } else {
3197                  #                  #
3198                }                }
3199              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
3200                if ($self->{insertion_mode} eq 'in head noscript') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3201                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3202                  $self->{insertion_mode} = 'in head';                  $self->{insertion_mode} = IN_HEAD_IM;
3203                  !!!next-token;                  !!!next-token;
3204                  redo B;                  redo B;
3205                } elsif ($self->{insertion_mode} eq 'before head') {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3206                  !!!parse-error (type => 'unmatched end tag:noscript');                  !!!parse-error (type => 'unmatched end tag:noscript');
3207                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
3208                  !!!next-token;                  !!!next-token;
# Line 2871  sub _tree_construction_main ($) { Line 3213  sub _tree_construction_main ($) {
3213              } elsif ({              } elsif ({
3214                        body => 1, html => 1,                        body => 1, html => 1,
3215                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3216                if ($self->{insertion_mode} eq 'before head') {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3217                  ## As if <head>                  ## As if <head>
3218                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, 'head');
3219                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3220                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3221    
3222                  $self->{insertion_mode} = 'in head';                  $self->{insertion_mode} = IN_HEAD_IM;
3223                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3224                } elsif ($self->{insertion_mode} eq 'in head noscript') {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3225                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3226                  ## Ignore the token                  ## Ignore the token
3227                  !!!next-token;                  !!!next-token;
# Line 2890  sub _tree_construction_main ($) { Line 3232  sub _tree_construction_main ($) {
3232              } elsif ({              } elsif ({
3233                        p => 1, br => 1,                        p => 1, br => 1,
3234                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3235                if ($self->{insertion_mode} eq 'before head') {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3236                  ## As if <head>                  ## As if <head>
3237                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, 'head');
3238                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3239                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3240    
3241                  $self->{insertion_mode} = 'in head';                  $self->{insertion_mode} = IN_HEAD_IM;
3242                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3243                }                }
3244    
3245                #                #
3246              } else {              } else {
3247                if ($self->{insertion_mode} ne 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3248                    #
3249                  } else {
3250                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3251                  ## Ignore the token                  ## Ignore the token
3252                  !!!next-token;                  !!!next-token;
3253                  redo B;                  redo B;
               } else {  
                 #  
3254                }                }
3255              }              }
3256    
3257              if ($self->{insertion_mode} eq 'in head noscript') {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3258                ## As if </noscript>                ## As if </noscript>
3259                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3260                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
# Line 2922  sub _tree_construction_main ($) { Line 3264  sub _tree_construction_main ($) {
3264                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3265    
3266                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
3267              } elsif ($self->{insertion_mode} eq 'in head') {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3268                ## As if </head>                ## As if </head>
3269                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3270    
3271                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
3272              } elsif ($self->{insertion_mode} eq 'before head') {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3273                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3274                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
3275                !!!next-token;                !!!next-token;
# Line 2937  sub _tree_construction_main ($) { Line 3279  sub _tree_construction_main ($) {
3279              ## "after head" insertion mode              ## "after head" insertion mode
3280              ## As if <body>              ## As if <body>
3281              !!!insert-element ('body');              !!!insert-element ('body');
3282              $self->{insertion_mode} = 'in body';              $self->{insertion_mode} = IN_BODY_IM;
3283              ## reprocess              ## reprocess
3284              redo B;              redo B;
3285            } else {            } else {
# Line 2945  sub _tree_construction_main ($) { Line 3287  sub _tree_construction_main ($) {
3287            }            }
3288    
3289            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
3290      } elsif ($self->{insertion_mode} eq 'in body' or      } elsif ($self->{insertion_mode} & BODY_IMS) {
3291               $self->{insertion_mode} eq 'in cell' or            if ($token->{type} == CHARACTER_TOKEN) {
              $self->{insertion_mode} eq 'in caption') {  
           if ($token->{type} eq 'character') {  
3292              ## NOTE: There is a code clone of "character in body".              ## NOTE: There is a code clone of "character in body".
3293              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
3294                            
# Line 2956  sub _tree_construction_main ($) { Line 3296  sub _tree_construction_main ($) {
3296    
3297              !!!next-token;              !!!next-token;
3298              redo B;              redo B;
3299            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} == START_TAG_TOKEN) {
3300              if ({              if ({
3301                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
3302                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
3303                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
3304                if ($self->{insertion_mode} eq 'in cell') {                if ($self->{insertion_mode} == IN_CELL_IM) {
3305                  ## have an element in table scope                  ## have an element in table scope
3306                  my $tn;                  my $tn;
3307                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 2984  sub _tree_construction_main ($) { Line 3324  sub _tree_construction_main ($) {
3324                                    
3325                  ## Close the cell                  ## Close the cell
3326                  !!!back-token; # <?>                  !!!back-token; # <?>
3327                  $token = {type => 'end tag', tag_name => $tn};                  $token = {type => END_TAG_TOKEN, tag_name => $tn};
3328                  redo B;                  redo B;
3329                } elsif ($self->{insertion_mode} eq 'in caption') {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3330                  !!!parse-error (type => 'not closed:caption');                  !!!parse-error (type => 'not closed:caption');
3331                                    
3332                  ## As if </caption>                  ## As if </caption>
# Line 3017  sub _tree_construction_main ($) { Line 3357  sub _tree_construction_main ($) {
3357                       tbody => 1, tfoot=> 1, thead => 1,                       tbody => 1, tfoot=> 1, thead => 1,
3358                      }->{$self->{open_elements}->[-1]->[1]}) {                      }->{$self->{open_elements}->[-1]->[1]}) {
3359                    !!!back-token; # <?>                    !!!back-token; # <?>
3360                    $token = {type => 'end tag', tag_name => 'caption'};                    $token = {type => END_TAG_TOKEN, tag_name => 'caption'};
3361                    !!!back-token;                    !!!back-token;
3362                    $token = {type => 'end tag',                    $token = {type => END_TAG_TOKEN,
3363                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3364                    redo B;                    redo B;
3365                  }                  }
# Line 3032  sub _tree_construction_main ($) { Line 3372  sub _tree_construction_main ($) {
3372                                    
3373                  $clear_up_to_marker->();                  $clear_up_to_marker->();
3374                                    
3375                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
3376                                    
3377                  ## reprocess                  ## reprocess
3378                  redo B;                  redo B;
# Line 3042  sub _tree_construction_main ($) { Line 3382  sub _tree_construction_main ($) {
3382              } else {              } else {
3383                #                #
3384              }              }
3385            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
3386              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
3387                if ($self->{insertion_mode} eq 'in cell') {                if ($self->{insertion_mode} == IN_CELL_IM) {
3388                  ## have an element in table scope                  ## have an element in table scope
3389                  my $i;                  my $i;
3390                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 3074  sub _tree_construction_main ($) { Line 3414  sub _tree_construction_main ($) {
3414                       tbody => 1, tfoot=> 1, thead => 1,                       tbody => 1, tfoot=> 1, thead => 1,
3415                      }->{$self->{open_elements}->[-1]->[1]}) {                      }->{$self->{open_elements}->[-1]->[1]}) {
3416                    !!!back-token;                    !!!back-token;
3417                    $token = {type => 'end tag',                    $token = {type => END_TAG_TOKEN,
3418                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3419                    redo B;                    redo B;
3420                  }                  }
# Line 3087  sub _tree_construction_main ($) { Line 3427  sub _tree_construction_main ($) {
3427                                    
3428                  $clear_up_to_marker->();                  $clear_up_to_marker->();
3429                                    
3430                  $self->{insertion_mode} = 'in row';                  $self->{insertion_mode} = IN_ROW_IM;
3431                                    
3432                  !!!next-token;                  !!!next-token;
3433                  redo B;                  redo B;
3434                } elsif ($self->{insertion_mode} eq 'in caption') {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3435                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3436                  ## Ignore the token                  ## Ignore the token
3437                  !!!next-token;                  !!!next-token;
# Line 3100  sub _tree_construction_main ($) { Line 3440  sub _tree_construction_main ($) {
3440                  #                  #
3441                }                }
3442              } elsif ($token->{tag_name} eq 'caption') {              } elsif ($token->{tag_name} eq 'caption') {
3443                if ($self->{insertion_mode} eq 'in caption') {                if ($self->{insertion_mode} == IN_CAPTION_IM) {
3444                  ## have a table element in table scope                  ## have a table element in table scope
3445                  my $i;                  my $i;
3446                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 3128  sub _tree_construction_main ($) { Line 3468  sub _tree_construction_main ($) {
3468                       tbody => 1, tfoot=> 1, thead => 1,                       tbody => 1, tfoot=> 1, thead => 1,
3469                      }->{$self->{open_elements}->[-1]->[1]}) {                      }->{$self->{open_elements}->[-1]->[1]}) {
3470                    !!!back-token;                    !!!back-token;
3471                    $token = {type => 'end tag',                    $token = {type => END_TAG_TOKEN,
3472                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3473                    redo B;                    redo B;
3474                  }                  }
# Line 3141  sub _tree_construction_main ($) { Line 3481  sub _tree_construction_main ($) {
3481                                    
3482                  $clear_up_to_marker->();                  $clear_up_to_marker->();
3483                                    
3484                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
3485                                    
3486                  !!!next-token;                  !!!next-token;
3487                  redo B;                  redo B;
3488                } elsif ($self->{insertion_mode} eq 'in cell') {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
3489                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3490                  ## Ignore the token                  ## Ignore the token
3491                  !!!next-token;                  !!!next-token;
# Line 3157  sub _tree_construction_main ($) { Line 3497  sub _tree_construction_main ($) {
3497                        table => 1, tbody => 1, tfoot => 1,                        table => 1, tbody => 1, tfoot => 1,
3498                        thead => 1, tr => 1,                        thead => 1, tr => 1,
3499                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
3500                       $self->{insertion_mode} eq 'in cell') {                       $self->{insertion_mode} == IN_CELL_IM) {
3501                ## have an element in table scope                ## have an element in table scope
3502                my $i;                my $i;
3503                my $tn;                my $tn;
# Line 3185  sub _tree_construction_main ($) { Line 3525  sub _tree_construction_main ($) {
3525    
3526                ## Close the cell                ## Close the cell
3527                !!!back-token; # </?>                !!!back-token; # </?>
3528                $token = {type => 'end tag', tag_name => $tn};                $token = {type => END_TAG_TOKEN, tag_name => $tn};
3529                redo B;                redo B;
3530              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
3531                       $self->{insertion_mode} eq 'in caption') {                       $self->{insertion_mode} == IN_CAPTION_IM) {
3532                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption');
3533    
3534                ## As if </caption>                ## As if </caption>
# Line 3219  sub _tree_construction_main ($) { Line 3559  sub _tree_construction_main ($) {
3559                     tbody => 1, tfoot=> 1, thead => 1,                     tbody => 1, tfoot=> 1, thead => 1,
3560                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
3561                  !!!back-token; # </table>                  !!!back-token; # </table>
3562                  $token = {type => 'end tag', tag_name => 'caption'};                  $token = {type => END_TAG_TOKEN, tag_name => 'caption'};
3563                  !!!back-token;                  !!!back-token;
3564                  $token = {type => 'end tag',                  $token = {type => END_TAG_TOKEN,
3565                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3566                  redo B;                  redo B;
3567                }                }
# Line 3234  sub _tree_construction_main ($) { Line 3574  sub _tree_construction_main ($) {
3574    
3575                $clear_up_to_marker->();                $clear_up_to_marker->();
3576    
3577                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
3578    
3579                ## reprocess                ## reprocess
3580                redo B;                redo B;
3581              } elsif ({              } elsif ({
3582                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
3583                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3584                if ($self->{insertion_mode} eq 'in cell' or                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
                   $self->{insertion_mode} eq 'in caption') {  
3585                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3586                  ## Ignore the token                  ## Ignore the token
3587                  !!!next-token;                  !!!next-token;
# Line 3254  sub _tree_construction_main ($) { Line 3593  sub _tree_construction_main ($) {
3593                        tbody => 1, tfoot => 1,                        tbody => 1, tfoot => 1,
3594                        thead => 1, tr => 1,                        thead => 1, tr => 1,
3595                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
3596                       $self->{insertion_mode} eq 'in caption') {                       $self->{insertion_mode} == IN_CAPTION_IM) {
3597                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3598                ## Ignore the token                ## Ignore the token
3599                !!!next-token;                !!!next-token;
# Line 3268  sub _tree_construction_main ($) { Line 3607  sub _tree_construction_main ($) {
3607    
3608        $insert = $insert_to_current;        $insert = $insert_to_current;
3609        #        #
3610      } elsif ($self->{insertion_mode} eq 'in row' or      } elsif ($self->{insertion_mode} & TABLE_IMS) {
3611               $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.  
3612              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3613                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3614                                
# Line 3329  sub _tree_construction_main ($) { Line 3665  sub _tree_construction_main ($) {
3665                            
3666              !!!next-token;              !!!next-token;
3667              redo B;              redo B;
3668            } elsif ($token->{type} eq 'start tag') {        } elsif ($token->{type} == START_TAG_TOKEN) {
3669              if ({              if ({
3670                   tr => ($self->{insertion_mode} ne 'in row'),                   tr => ($self->{insertion_mode} != IN_ROW_IM),
3671                   th => 1, td => 1,                   th => 1, td => 1,
3672                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
3673                if ($self->{insertion_mode} eq 'in table') {                if ($self->{insertion_mode} == IN_TABLE_IM) {
3674                  ## Clear back to table context                  ## Clear back to table context
3675                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while ($self->{open_elements}->[-1]->[1] ne 'table' and
3676                         $self->{open_elements}->[-1]->[1] ne 'html') {                         $self->{open_elements}->[-1]->[1] ne 'html') {
# Line 3343  sub _tree_construction_main ($) { Line 3679  sub _tree_construction_main ($) {
3679                  }                  }
3680                                    
3681                  !!!insert-element ('tbody');                  !!!insert-element ('tbody');
3682                  $self->{insertion_mode} = 'in table body';                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
3683                  ## reprocess in the "in table body" insertion mode...                  ## reprocess in the "in table body" insertion mode...
3684                }                }
3685    
3686                if ($self->{insertion_mode} eq 'in table body') {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3687                  unless ($token->{tag_name} eq 'tr') {                  unless ($token->{tag_name} eq 'tr') {
3688                    !!!parse-error (type => 'missing start tag:tr');                    !!!parse-error (type => 'missing start tag:tr');
3689                  }                  }
# Line 3360  sub _tree_construction_main ($) { Line 3696  sub _tree_construction_main ($) {
3696                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
3697                  }                  }
3698                                    
3699                  $self->{insertion_mode} = 'in row';                  $self->{insertion_mode} = IN_ROW_IM;
3700                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
3701                    !!!insert-element ($token->{tag_name}, $token->{attributes});                    !!!insert-element ($token->{tag_name}, $token->{attributes});
3702                    !!!next-token;                    !!!next-token;
# Line 3380  sub _tree_construction_main ($) { Line 3716  sub _tree_construction_main ($) {
3716                }                }
3717                                
3718                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
3719                $self->{insertion_mode} = 'in cell';                $self->{insertion_mode} = IN_CELL_IM;
3720    
3721                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
3722                                
# Line 3389  sub _tree_construction_main ($) { Line 3725  sub _tree_construction_main ($) {
3725              } elsif ({              } elsif ({
3726                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
3727                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
3728                        tr => 1, # $self->{insertion_mode} eq 'in row'                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
3729                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3730                if ($self->{insertion_mode} eq 'in row') {                if ($self->{insertion_mode} == IN_ROW_IM) {
3731                  ## As if </tr>                  ## As if </tr>
3732                  ## have an element in table scope                  ## have an element in table scope
3733                  my $i;                  my $i;
# Line 3422  sub _tree_construction_main ($) { Line 3758  sub _tree_construction_main ($) {
3758                  }                  }
3759                                    
3760                  pop @{$self->{open_elements}}; # tr                  pop @{$self->{open_elements}}; # tr
3761                  $self->{insertion_mode} = 'in table body';                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
3762                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
3763                    ## reprocess                    ## reprocess
3764                    redo B;                    redo B;
# Line 3431  sub _tree_construction_main ($) { Line 3767  sub _tree_construction_main ($) {
3767                  }                  }
3768                }                }
3769    
3770                if ($self->{insertion_mode} eq 'in table body') {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3771                  ## have an element in table scope                  ## have an element in table scope
3772                  my $i;                  my $i;
3773                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 3470  sub _tree_construction_main ($) { Line 3806  sub _tree_construction_main ($) {
3806                  ## nop by definition                  ## nop by definition
3807                                    
3808                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3809                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
3810                  ## reprocess in "in table" insertion mode...                  ## reprocess in "in table" insertion mode...
3811                }                }
3812    
# Line 3483  sub _tree_construction_main ($) { Line 3819  sub _tree_construction_main ($) {
3819                  }                  }
3820                                    
3821                  !!!insert-element ('colgroup');                  !!!insert-element ('colgroup');
3822                  $self->{insertion_mode} = 'in column group';                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
3823                  ## reprocess                  ## reprocess
3824                  redo B;                  redo B;
3825                } elsif ({                } elsif ({
# Line 3503  sub _tree_construction_main ($) { Line 3839  sub _tree_construction_main ($) {
3839                                    
3840                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3841                  $self->{insertion_mode} = {                  $self->{insertion_mode} = {
3842                                             caption => 'in caption',                                             caption => IN_CAPTION_IM,
3843                                             colgroup => 'in column group',                                             colgroup => IN_COLUMN_GROUP_IM,
3844                                             tbody => 'in table body',                                             tbody => IN_TABLE_BODY_IM,
3845                                             tfoot => 'in table body',                                             tfoot => IN_TABLE_BODY_IM,
3846                                             thead => 'in table body',                                             thead => IN_TABLE_BODY_IM,
3847                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
3848                  !!!next-token;                  !!!next-token;
3849                  redo B;                  redo B;
# Line 3515  sub _tree_construction_main ($) { Line 3851  sub _tree_construction_main ($) {
3851                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
3852                }                }
3853              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
               ## NOTE: There are code clones for this "table in table"  
3854                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3855    
3856                ## As if </table>                ## As if </table>
# Line 3546  sub _tree_construction_main ($) { Line 3881  sub _tree_construction_main ($) {
3881                     tbody => 1, tfoot=> 1, thead => 1,                     tbody => 1, tfoot=> 1, thead => 1,
3882                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
3883                  !!!back-token; # <table>                  !!!back-token; # <table>
3884                  $token = {type => 'end tag', tag_name => 'table'};                  $token = {type => END_TAG_TOKEN, tag_name => 'table'};
3885                  !!!back-token;                  !!!back-token;
3886                  $token = {type => 'end tag',                  $token = {type => END_TAG_TOKEN,
3887                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3888                  redo B;                  redo B;
3889                }                }
# Line 3563  sub _tree_construction_main ($) { Line 3898  sub _tree_construction_main ($) {
3898    
3899                ## reprocess                ## reprocess
3900                redo B;                redo B;
3901              } else {          } else {
3902                #            !!!parse-error (type => 'in table:'.$token->{tag_name});
3903              }  
3904            } elsif ($token->{type} eq 'end tag') {            $insert = $insert_to_foster;
3905              #
3906            }
3907          } elsif ($token->{type} == END_TAG_TOKEN) {
3908              if ($token->{tag_name} eq 'tr' and              if ($token->{tag_name} eq 'tr' and
3909                  $self->{insertion_mode} eq 'in row') {                  $self->{insertion_mode} == IN_ROW_IM) {
3910                ## have an element in table scope                ## have an element in table scope
3911                my $i;                my $i;
3912                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 3598  sub _tree_construction_main ($) { Line 3936  sub _tree_construction_main ($) {
3936                }                }
3937    
3938                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
3939                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_BODY_IM;
3940                !!!next-token;                !!!next-token;
3941                redo B;                redo B;
3942              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
3943                if ($self->{insertion_mode} eq 'in row') {                if ($self->{insertion_mode} == IN_ROW_IM) {
3944                  ## As if </tr>                  ## As if </tr>
3945                  ## have an element in table scope                  ## have an element in table scope
3946                  my $i;                  my $i;
# Line 3633  sub _tree_construction_main ($) { Line 3971  sub _tree_construction_main ($) {
3971                  }                  }
3972                                    
3973                  pop @{$self->{open_elements}}; # tr                  pop @{$self->{open_elements}}; # tr
3974                  $self->{insertion_mode} = 'in table body';                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
3975                  ## reprocess in the "in table body" insertion mode...                  ## reprocess in the "in table body" insertion mode...
3976                }                }
3977    
3978                if ($self->{insertion_mode} eq 'in table body') {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3979                  ## have an element in table scope                  ## have an element in table scope
3980                  my $i;                  my $i;
3981                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 3676  sub _tree_construction_main ($) { Line 4014  sub _tree_construction_main ($) {
4014                  ## nop by definition                  ## nop by definition
4015                                    
4016                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4017                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
4018                  ## reprocess in the "in table" insertion mode...                  ## reprocess in the "in table" insertion mode...
4019                }                }
4020    
# Line 3707  sub _tree_construction_main ($) { Line 4045  sub _tree_construction_main ($) {
4045                     tbody => 1, tfoot=> 1, thead => 1,                     tbody => 1, tfoot=> 1, thead => 1,
4046                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
4047                  !!!back-token;                  !!!back-token;
4048                  $token = {type => 'end tag',                  $token = {type => END_TAG_TOKEN,
4049                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
4050                  redo B;                  redo B;
4051                }                }
# Line 3725  sub _tree_construction_main ($) { Line 4063  sub _tree_construction_main ($) {
4063              } elsif ({              } elsif ({
4064                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
4065                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
4066                       ($self->{insertion_mode} eq 'in row' or                       $self->{insertion_mode} & ROW_IMS) {
4067                        $self->{insertion_mode} eq 'in table body')) {                if ($self->{insertion_mode} == IN_ROW_IM) {
               if ($self->{insertion_mode} eq 'in row') {  
4068                  ## have an element in table scope                  ## have an element in table scope
4069                  my $i;                  my $i;
4070                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 3778  sub _tree_construction_main ($) { Line 4115  sub _tree_construction_main ($) {
4115                  }                  }
4116                                    
4117                  pop @{$self->{open_elements}}; # tr                  pop @{$self->{open_elements}}; # tr
4118                  $self->{insertion_mode} = 'in table body';                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
4119                  ## reprocess in the "in table body" insertion mode...                  ## reprocess in the "in table body" insertion mode...
4120                }                }
4121    
# Line 3811  sub _tree_construction_main ($) { Line 4148  sub _tree_construction_main ($) {
4148                }                }
4149    
4150                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4151                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
4152                !!!next-token;                !!!next-token;
4153                redo B;                redo B;
4154              } elsif ({              } elsif ({
4155                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
4156                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
4157                        tr => 1, # $self->{insertion_mode} eq 'in row'                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
4158                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} eq 'in table'                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
4159                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4160                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4161                ## Ignore the token                ## Ignore the token
4162                !!!next-token;                !!!next-token;
4163                redo B;                redo B;
4164              } else {          } else {
4165                #            !!!parse-error (type => 'in table:/'.$token->{tag_name});
             }  
           } else {  
             die "$0: $token->{type}: Unknown token type";  
           }  
   
       !!!parse-error (type => 'in table:'.$token->{tag_name});  
4166    
4167        $insert = $insert_to_foster;            $insert = $insert_to_foster;
4168        #            #
4169      } elsif ($self->{insertion_mode} eq 'in column group') {          }
4170            if ($token->{type} eq 'character') {        } else {
4171            die "$0: $token->{type}: Unknown token type";
4172          }
4173        } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
4174              if ($token->{type} == CHARACTER_TOKEN) {
4175              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4176                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4177                unless (length $token->{data}) {                unless (length $token->{data}) {
# Line 3846  sub _tree_construction_main ($) { Line 4181  sub _tree_construction_main ($) {
4181              }              }
4182                            
4183              #              #
4184            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} == START_TAG_TOKEN) {
4185              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
4186                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
4187                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
# Line 3855  sub _tree_construction_main ($) { Line 4190  sub _tree_construction_main ($) {
4190              } else {              } else {
4191                #                #
4192              }              }
4193            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
4194              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
4195                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] eq 'html') {
4196                  !!!parse-error (type => 'unmatched end tag:colgroup');                  !!!parse-error (type => 'unmatched end tag:colgroup');
# Line 3864  sub _tree_construction_main ($) { Line 4199  sub _tree_construction_main ($) {
4199                  redo B;                  redo B;
4200                } else {                } else {
4201                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
4202                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
4203                  !!!next-token;                  !!!next-token;
4204                  redo B;                              redo B;            
4205                }                }
# Line 3888  sub _tree_construction_main ($) { Line 4223  sub _tree_construction_main ($) {
4223              redo B;              redo B;
4224            } else {            } else {
4225              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
4226              $self->{insertion_mode} = 'in table';              $self->{insertion_mode} = IN_TABLE_IM;
4227              ## reprocess              ## reprocess
4228              redo B;              redo B;
4229            }            }
4230      } elsif ($self->{insertion_mode} eq 'in select') {      } elsif ($self->{insertion_mode} == IN_SELECT_IM) {
4231            if ($token->{type} eq 'character') {        if ($token->{type} == CHARACTER_TOKEN) {
4232              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4233              !!!next-token;          !!!next-token;
4234              redo B;          redo B;
4235            } elsif ($token->{type} eq 'start tag') {        } elsif ($token->{type} == START_TAG_TOKEN) {
4236              if ($token->{tag_name} eq 'option') {              if ($token->{tag_name} eq 'option') {
4237                if ($self->{open_elements}->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
4238                  ## As if </option>                  ## As if </option>
# Line 3950  sub _tree_construction_main ($) { Line 4285  sub _tree_construction_main ($) {
4285    
4286                !!!next-token;                !!!next-token;
4287                redo B;                redo B;
4288              } else {          } else {
4289                #            !!!parse-error (type => 'in select:'.$token->{tag_name});
4290              }            ## Ignore the token
4291            } elsif ($token->{type} eq 'end tag') {            !!!next-token;
4292              redo B;
4293            }
4294          } elsif ($token->{type} == END_TAG_TOKEN) {
4295              if ($token->{tag_name} eq 'optgroup') {              if ($token->{tag_name} eq 'optgroup') {
4296                if ($self->{open_elements}->[-1]->[1] eq 'option' and                if ($self->{open_elements}->[-1]->[1] eq 'option' and
4297                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {
# Line 4055  sub _tree_construction_main ($) { Line 4393  sub _tree_construction_main ($) {
4393    
4394                ## reprocess                ## reprocess
4395                redo B;                redo B;
4396              } else {          } else {
4397                #            !!!parse-error (type => 'in select:/'.$token->{tag_name});
             }  
           } else {  
             #  
           }  
   
           !!!parse-error (type => 'in select:'.$token->{tag_name});  
4398            ## Ignore the token            ## Ignore the token
4399            !!!next-token;            !!!next-token;
4400            redo B;            redo B;
4401      } elsif ($self->{insertion_mode} eq 'after body' or          }
4402               $self->{insertion_mode} eq 'after html body') {        } else {
4403        if ($token->{type} eq 'character') {          die "$0: $token->{type}: Unknown token type";
4404          }
4405        } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
4406          if ($token->{type} == CHARACTER_TOKEN) {
4407          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4408            my $data = $1;            my $data = $1;
4409            ## As if in body            ## As if in body
# Line 4082  sub _tree_construction_main ($) { Line 4417  sub _tree_construction_main ($) {
4417            }            }
4418          }          }
4419                    
4420          if ($self->{insertion_mode} eq 'after html body') {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4421            !!!parse-error (type => 'after html:#character');            !!!parse-error (type => 'after html:#character');
4422    
4423            ## 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 4426  sub _tree_construction_main ($) {
4426          ## "after body" insertion mode          ## "after body" insertion mode
4427          !!!parse-error (type => 'after body:#character');          !!!parse-error (type => 'after body:#character');
4428    
4429          $self->{insertion_mode} = 'in body';          $self->{insertion_mode} = IN_BODY_IM;
4430          ## reprocess          ## reprocess
4431          redo B;          redo B;
4432        } elsif ($token->{type} eq 'start tag') {        } elsif ($token->{type} == START_TAG_TOKEN) {
4433          if ($self->{insertion_mode} eq 'after html body') {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4434            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html:'.$token->{tag_name});
4435                        
4436            ## 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 4439  sub _tree_construction_main ($) {
4439          ## "after body" insertion mode          ## "after body" insertion mode
4440          !!!parse-error (type => 'after body:'.$token->{tag_name});          !!!parse-error (type => 'after body:'.$token->{tag_name});
4441    
4442          $self->{insertion_mode} = 'in body';          $self->{insertion_mode} = IN_BODY_IM;
4443          ## reprocess          ## reprocess
4444          redo B;          redo B;
4445        } elsif ($token->{type} eq 'end tag') {        } elsif ($token->{type} == END_TAG_TOKEN) {
4446          if ($self->{insertion_mode} eq 'after html body') {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4447            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!parse-error (type => 'after html:/'.$token->{tag_name});
4448                        
4449            $self->{insertion_mode} = 'after body';            $self->{insertion_mode} = AFTER_BODY_IM;
4450            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "main" phase, "after body" insertion mode...
4451          }          }
4452    
# Line 4123  sub _tree_construction_main ($) { Line 4458  sub _tree_construction_main ($) {
4458              !!!next-token;              !!!next-token;
4459              redo B;              redo B;
4460            } else {            } else {
4461              $self->{insertion_mode} = 'after html body';              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
4462              !!!next-token;              !!!next-token;
4463              redo B;              redo B;
4464            }            }
4465          } else {          } else {
4466            !!!parse-error (type => 'after body:/'.$token->{tag_name});            !!!parse-error (type => 'after body:/'.$token->{tag_name});
4467    
4468            $self->{insertion_mode} = 'in body';            $self->{insertion_mode} = IN_BODY_IM;
4469            ## reprocess            ## reprocess
4470            redo B;            redo B;
4471          }          }
4472        } else {        } else {
4473          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
4474        }        }
4475      } elsif ($self->{insertion_mode} eq 'in frameset' or      } elsif ($self->{insertion_mode} & FRAME_IMS) {
4476               $self->{insertion_mode} eq 'after frameset' or        if ($token->{type} == CHARACTER_TOKEN) {
              $self->{insertion_mode} eq 'after html frameset') {  
       if ($token->{type} eq 'character') {  
4477          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4478            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4479                        
# Line 4151  sub _tree_construction_main ($) { Line 4484  sub _tree_construction_main ($) {
4484          }          }
4485                    
4486          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
4487            if ($self->{insertion_mode} eq 'in frameset') {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4488              !!!parse-error (type => 'in frameset:#character');              !!!parse-error (type => 'in frameset:#character');
4489            } elsif ($self->{insertion_mode} eq 'after frameset') {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
4490              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#character');
4491            } else { # "after html frameset"            } else { # "after html frameset"
4492              !!!parse-error (type => 'after html:#character');              !!!parse-error (type => 'after html:#character');
4493    
4494              $self->{insertion_mode} = 'after frameset';              $self->{insertion_mode} = AFTER_FRAMESET_IM;
4495              ## Reprocess in the "main" phase, "after frameset"...              ## Reprocess in the "main" phase, "after frameset"...
4496              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#character');
4497            }            }
# Line 4173  sub _tree_construction_main ($) { Line 4506  sub _tree_construction_main ($) {
4506          }          }
4507                    
4508          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
4509        } elsif ($token->{type} eq 'start tag') {        } elsif ($token->{type} == START_TAG_TOKEN) {
4510          if ($self->{insertion_mode} eq 'after html frameset') {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4511            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html:'.$token->{tag_name});
4512    
4513            $self->{insertion_mode} = 'after frameset';            $self->{insertion_mode} = AFTER_FRAMESET_IM;
4514            ## Process in the "main" phase, "after frameset" insertion mode...            ## Process in the "main" phase, "after frameset" insertion mode...
4515          }          }
4516    
4517          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
4518              $self->{insertion_mode} eq 'in frameset') {              $self->{insertion_mode} == IN_FRAMESET_IM) {
4519            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes});
4520            !!!next-token;            !!!next-token;
4521            redo B;            redo B;
4522          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
4523                   $self->{insertion_mode} eq 'in frameset') {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
4524            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes});
4525            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4526            !!!next-token;            !!!next-token;
# Line 4197  sub _tree_construction_main ($) { Line 4530  sub _tree_construction_main ($) {
4530            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
4531            redo B;            redo B;
4532          } else {          } else {
4533            if ($self->{insertion_mode} eq 'in frameset') {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4534              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!parse-error (type => 'in frameset:'.$token->{tag_name});
4535            } else {            } else {
4536              !!!parse-error (type => 'after frameset:'.$token->{tag_name});              !!!parse-error (type => 'after frameset:'.$token->{tag_name});
# Line 4206  sub _tree_construction_main ($) { Line 4539  sub _tree_construction_main ($) {
4539            !!!next-token;            !!!next-token;
4540            redo B;            redo B;
4541          }          }
4542        } elsif ($token->{type} eq 'end tag') {        } elsif ($token->{type} == END_TAG_TOKEN) {
4543          if ($self->{insertion_mode} eq 'after html frameset') {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4544            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!parse-error (type => 'after html:/'.$token->{tag_name});
4545    
4546            $self->{insertion_mode} = 'after frameset';            $self->{insertion_mode} = AFTER_FRAMESET_IM;
4547            ## Process in the "main" phase, "after frameset" insertion mode...            ## Process in the "main" phase, "after frameset" insertion mode...
4548          }          }
4549    
4550          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
4551              $self->{insertion_mode} eq 'in frameset') {              $self->{insertion_mode} == IN_FRAMESET_IM) {
4552            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] eq 'html' and
4553                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
4554              !!!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 4561  sub _tree_construction_main ($) {
4561    
4562            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
4563                $self->{open_elements}->[-1]->[1] ne 'frameset') {                $self->{open_elements}->[-1]->[1] ne 'frameset') {
4564              $self->{insertion_mode} = 'after frameset';              $self->{insertion_mode} = AFTER_FRAMESET_IM;
4565            }            }
4566            redo B;            redo B;
4567          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
4568                   $self->{insertion_mode} eq 'after frameset') {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
4569            $self->{insertion_mode} = 'after html frameset';            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
4570            !!!next-token;            !!!next-token;
4571            redo B;            redo B;
4572          } else {          } else {
4573            if ($self->{insertion_mode} eq 'in frameset') {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4574              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});
4575            } else {            } else {
4576              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});
# Line 4256  sub _tree_construction_main ($) { Line 4589  sub _tree_construction_main ($) {
4589      }      }
4590    
4591      ## "in body" insertion mode      ## "in body" insertion mode
4592    my $in_body = sub {      if ($token->{type} == START_TAG_TOKEN) {
     if ($token->{type} eq 'start tag') {  
4593        if ($token->{tag_name} eq 'script') {        if ($token->{tag_name} eq 'script') {
4594          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
4595          $script_start_tag->($insert);          $script_start_tag->($insert);
4596          return;          redo B;
4597        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
4598          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
4599          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4600          return;          redo B;
4601        } elsif ({        } elsif ({
4602                  base => 1, link => 1,                  base => 1, link => 1,
4603                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 4273  sub _tree_construction_main ($) { Line 4605  sub _tree_construction_main ($) {
4605          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4606          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4607          !!!next-token;          !!!next-token;
4608          return;          redo B;
4609        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
4610          ## 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
4611          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4612          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.
4613    
4614          unless ($self->{confident}) {          unless ($self->{confident}) {
           my $charset;  
4615            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) { ## TODO: And if supported
4616              $charset = $token->{attributes}->{charset}->{value};              $self->{change_encoding}
4617            }                  ->($self, $token->{attributes}->{charset}->{value});
4618            if ($token->{attributes}->{'http-equiv'}) {              
4619                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4620                    ->set_user_data (manakai_has_reference =>
4621                                         $token->{attributes}->{charset}
4622                                             ->{has_reference});
4623              } elsif ($token->{attributes}->{content}) {
4624              ## 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.
4625              if ($token->{attributes}->{'http-equiv'}->{value}              if ($token->{attributes}->{content}->{value}
4626                  =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4627                        [\x09-\x0D\x20]*=
4628                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4629                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
4630                $charset = defined $1 ? $1 : defined $2 ? $2 : $3;                $self->{change_encoding}
4631              } ## TODO: And if supported                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);
4632                  $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4633                      ->set_user_data (manakai_has_reference =>
4634                                           $token->{attributes}->{content}
4635                                                 ->{has_reference});
4636                }
4637              }
4638            } else {
4639              if ($token->{attributes}->{charset}) {
4640                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4641                    ->set_user_data (manakai_has_reference =>
4642                                         $token->{attributes}->{charset}
4643                                             ->{has_reference});
4644              }
4645              if ($token->{attributes}->{content}) {
4646                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4647                    ->set_user_data (manakai_has_reference =>
4648                                         $token->{attributes}->{content}
4649                                             ->{has_reference});
4650            }            }
           ## TODO: Change the encoding  
4651          }          }
4652    
4653          !!!next-token;          !!!next-token;
4654          return;          redo B;
4655        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
4656          !!!parse-error (type => 'in body:title');          !!!parse-error (type => 'in body:title');
4657          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
# Line 4308  sub _tree_construction_main ($) { Line 4662  sub _tree_construction_main ($) {
4662              $insert->($_[0]);              $insert->($_[0]);
4663            }            }
4664          });          });
4665          return;          redo B;
4666        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
4667          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body:body');
4668                                
# Line 4326  sub _tree_construction_main ($) { Line 4680  sub _tree_construction_main ($) {
4680            }            }
4681          }          }
4682          !!!next-token;          !!!next-token;
4683          return;          redo B;
4684        } elsif ({        } elsif ({
4685                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
4686                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1, listing => 1,
# Line 4337  sub _tree_construction_main ($) { Line 4691  sub _tree_construction_main ($) {
4691          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4692            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4693              !!!back-token;              !!!back-token;
4694              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4695              return;              redo B;
4696            } elsif ({            } elsif ({
4697                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4698                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 4350  sub _tree_construction_main ($) { Line 4704  sub _tree_construction_main ($) {
4704          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4705          if ($token->{tag_name} eq 'pre') {          if ($token->{tag_name} eq 'pre') {
4706            !!!next-token;            !!!next-token;
4707            if ($token->{type} eq 'character') {            if ($token->{type} == CHARACTER_TOKEN) {
4708              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
4709              unless (length $token->{data}) {              unless (length $token->{data}) {
4710                !!!next-token;                !!!next-token;
# Line 4359  sub _tree_construction_main ($) { Line 4713  sub _tree_construction_main ($) {
4713          } else {          } else {
4714            !!!next-token;            !!!next-token;
4715          }          }
4716          return;          redo B;
4717        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
4718          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
4719            !!!parse-error (type => 'in form:form');            !!!parse-error (type => 'in form:form');
4720            ## Ignore the token            ## Ignore the token
4721            !!!next-token;            !!!next-token;
4722            return;            redo B;
4723          } else {          } else {
4724            ## has a p element in scope            ## has a p element in scope
4725            INSCOPE: for (reverse @{$self->{open_elements}}) {            INSCOPE: for (reverse @{$self->{open_elements}}) {
4726              if ($_->[1] eq 'p') {              if ($_->[1] eq 'p') {
4727                !!!back-token;                !!!back-token;
4728                $token = {type => 'end tag', tag_name => 'p'};                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4729                return;                redo B;
4730              } elsif ({              } elsif ({
4731                        table => 1, caption => 1, td => 1, th => 1,                        table => 1, caption => 1, td => 1, th => 1,
4732                        button => 1, marquee => 1, object => 1, html => 1,                        button => 1, marquee => 1, object => 1, html => 1,
# Line 4384  sub _tree_construction_main ($) { Line 4738  sub _tree_construction_main ($) {
4738            !!!insert-element-t ($token->{tag_name}, $token->{attributes});            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4739            $self->{form_element} = $self->{open_elements}->[-1]->[0];            $self->{form_element} = $self->{open_elements}->[-1]->[0];
4740            !!!next-token;            !!!next-token;
4741            return;            redo B;
4742          }          }
4743        } elsif ($token->{tag_name} eq 'li') {        } elsif ($token->{tag_name} eq 'li') {
4744          ## has a p element in scope          ## has a p element in scope
4745          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4746            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4747              !!!back-token;              !!!back-token;
4748              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4749              return;              redo B;
4750            } elsif ({            } elsif ({
4751                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4752                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 4432  sub _tree_construction_main ($) { Line 4786  sub _tree_construction_main ($) {
4786                        
4787          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4788          !!!next-token;          !!!next-token;
4789          return;          redo B;
4790        } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {        } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {
4791          ## has a p element in scope          ## has a p element in scope
4792          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4793            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4794              !!!back-token;              !!!back-token;
4795              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4796              return;              redo B;
4797            } elsif ({            } elsif ({
4798                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4799                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 4479  sub _tree_construction_main ($) { Line 4833  sub _tree_construction_main ($) {
4833                        
4834          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4835          !!!next-token;          !!!next-token;
4836          return;          redo B;
4837        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
4838          ## has a p element in scope          ## has a p element in scope
4839          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4840            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4841              !!!back-token;              !!!back-token;
4842              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4843              return;              redo B;
4844            } elsif ({            } elsif ({
4845                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4846                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 4500  sub _tree_construction_main ($) { Line 4854  sub _tree_construction_main ($) {
4854          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
4855                        
4856          !!!next-token;          !!!next-token;
4857          return;          redo B;
4858        } elsif ({        } elsif ({
4859                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
4860                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 4509  sub _tree_construction_main ($) { Line 4863  sub _tree_construction_main ($) {
4863            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
4864            if ($node->[1] eq 'p') {            if ($node->[1] eq 'p') {
4865              !!!back-token;              !!!back-token;
4866              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4867              return;              redo B;
4868            } elsif ({            } elsif ({
4869                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4870                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 4545  sub _tree_construction_main ($) { Line 4899  sub _tree_construction_main ($) {
4899          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4900                        
4901          !!!next-token;          !!!next-token;
4902          return;          redo B;
4903        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
4904          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
4905            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
# Line 4553  sub _tree_construction_main ($) { Line 4907  sub _tree_construction_main ($) {
4907              !!!parse-error (type => 'in a:a');              !!!parse-error (type => 'in a:a');
4908                            
4909              !!!back-token;              !!!back-token;
4910              $token = {type => 'end tag', tag_name => 'a'};              $token = {type => END_TAG_TOKEN, tag_name => 'a'};
4911              $formatting_end_tag->($token->{tag_name});              $formatting_end_tag->($token->{tag_name});
4912                            
4913              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
# Line 4580  sub _tree_construction_main ($) { Line 4934  sub _tree_construction_main ($) {
4934          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
4935    
4936          !!!next-token;          !!!next-token;
4937          return;          redo B;
4938        } elsif ({        } elsif ({
4939                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
4940                  s => 1, small => 1, strile => 1,                  s => 1, small => 1, strile => 1,
# Line 4592  sub _tree_construction_main ($) { Line 4946  sub _tree_construction_main ($) {
4946          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
4947                    
4948          !!!next-token;          !!!next-token;
4949          return;          redo B;
4950        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
4951          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
4952    
# Line 4600  sub _tree_construction_main ($) { Line 4954  sub _tree_construction_main ($) {
4954          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4955            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
4956            if ($node->[1] eq 'nobr') {            if ($node->[1] eq 'nobr') {
4957              !!!parse-error (type => 'not closed:nobr');              !!!parse-error (type => 'in nobr:nobr');
4958              !!!back-token;              !!!back-token;
4959              $token = {type => 'end tag', tag_name => 'nobr'};              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};
4960              return;              redo B;
4961            } elsif ({            } elsif ({
4962                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4963                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 4616  sub _tree_construction_main ($) { Line 4970  sub _tree_construction_main ($) {
4970          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
4971                    
4972          !!!next-token;          !!!next-token;
4973          return;          redo B;
4974        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
4975          ## has a button element in scope          ## has a button element in scope
4976          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 4624  sub _tree_construction_main ($) { Line 4978  sub _tree_construction_main ($) {
4978            if ($node->[1] eq 'button') {            if ($node->[1] eq 'button') {
4979              !!!parse-error (type => 'in button:button');              !!!parse-error (type => 'in button:button');
4980              !!!back-token;              !!!back-token;
4981              $token = {type => 'end tag', tag_name => 'button'};              $token = {type => END_TAG_TOKEN, tag_name => 'button'};
4982              return;              redo B;
4983            } elsif ({            } elsif ({
4984                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4985                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 4640  sub _tree_construction_main ($) { Line 4994  sub _tree_construction_main ($) {
4994          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
4995    
4996          !!!next-token;          !!!next-token;
4997          return;          redo B;
4998        } elsif ($token->{tag_name} eq 'marquee' or        } elsif ($token->{tag_name} eq 'marquee' or
4999                 $token->{tag_name} eq 'object') {                 $token->{tag_name} eq 'object') {
5000          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
# Line 4649  sub _tree_construction_main ($) { Line 5003  sub _tree_construction_main ($) {
5003          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
5004                    
5005          !!!next-token;          !!!next-token;
5006          return;          redo B;
5007        } elsif ($token->{tag_name} eq 'xmp') {        } elsif ($token->{tag_name} eq 'xmp') {
5008          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5009          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
5010          return;          redo B;
5011        } elsif ($token->{tag_name} eq 'table') {        } elsif ($token->{tag_name} eq 'table') {
5012          ## has a p element in scope          ## has a p element in scope
5013          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5014            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
5015              !!!back-token;              !!!back-token;
5016              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5017              return;              redo B;
5018            } elsif ({            } elsif ({
5019                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
5020                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 4671  sub _tree_construction_main ($) { Line 5025  sub _tree_construction_main ($) {
5025                        
5026          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5027                        
5028          $self->{insertion_mode} = 'in table';          $self->{insertion_mode} = IN_TABLE_IM;
5029                        
5030          !!!next-token;          !!!next-token;
5031          return;          redo B;
5032        } elsif ({        } elsif ({
5033                  area => 1, basefont => 1, bgsound => 1, br => 1,                  area => 1, basefont => 1, bgsound => 1, br => 1,
5034                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
# Line 4692  sub _tree_construction_main ($) { Line 5046  sub _tree_construction_main ($) {
5046          pop @{$self->{open_elements}};          pop @{$self->{open_elements}};
5047                    
5048          !!!next-token;          !!!next-token;
5049          return;          redo B;
5050        } elsif ($token->{tag_name} eq 'hr') {        } elsif ($token->{tag_name} eq 'hr') {
5051          ## has a p element in scope          ## has a p element in scope
5052          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5053            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
5054              !!!back-token;              !!!back-token;
5055              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5056              return;              redo B;
5057            } elsif ({            } elsif ({
5058                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
5059                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 4712  sub _tree_construction_main ($) { Line 5066  sub _tree_construction_main ($) {
5066          pop @{$self->{open_elements}};          pop @{$self->{open_elements}};
5067                        
5068          !!!next-token;          !!!next-token;
5069          return;          redo B;
5070        } elsif ($token->{tag_name} eq 'input') {        } elsif ($token->{tag_name} eq 'input') {
5071          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5072                    
# Line 4721  sub _tree_construction_main ($) { Line 5075  sub _tree_construction_main ($) {
5075          pop @{$self->{open_elements}};          pop @{$self->{open_elements}};
5076                    
5077          !!!next-token;          !!!next-token;
5078          return;          redo B;
5079        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
5080          !!!parse-error (type => 'isindex');          !!!parse-error (type => 'isindex');
5081                    
5082          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
5083            ## Ignore the token            ## Ignore the token
5084            !!!next-token;            !!!next-token;
5085            return;            redo B;
5086          } else {          } else {
5087            my $at = $token->{attributes};            my $at = $token->{attributes};
5088            my $form_attrs;            my $form_attrs;
# Line 4738  sub _tree_construction_main ($) { Line 5092  sub _tree_construction_main ($) {
5092            delete $at->{action};            delete $at->{action};
5093            delete $at->{prompt};            delete $at->{prompt};
5094            my @tokens = (            my @tokens = (
5095                          {type => 'start tag', tag_name => 'form',                          {type => START_TAG_TOKEN, tag_name => 'form',
5096                           attributes => $form_attrs},                           attributes => $form_attrs},
5097                          {type => 'start tag', tag_name => 'hr'},                          {type => START_TAG_TOKEN, tag_name => 'hr'},
5098                          {type => 'start tag', tag_name => 'p'},                          {type => START_TAG_TOKEN, tag_name => 'p'},
5099                          {type => 'start tag', tag_name => 'label'},                          {type => START_TAG_TOKEN, tag_name => 'label'},
5100                         );                         );
5101            if ($prompt_attr) {            if ($prompt_attr) {
5102              push @tokens, {type => 'character', data => $prompt_attr->{value}};              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};
5103            } else {            } else {
5104              push @tokens, {type => 'character',              push @tokens, {type => CHARACTER_TOKEN,
5105                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD
5106              ## TODO: make this configurable              ## TODO: make this configurable
5107            }            }
5108            push @tokens,            push @tokens,
5109                          {type => 'start tag', tag_name => 'input', attributes => $at},                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},
5110                          #{type => 'character', data => ''}, # SHOULD                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
5111                          {type => 'end tag', tag_name => 'label'},                          {type => END_TAG_TOKEN, tag_name => 'label'},
5112                          {type => 'end tag', tag_name => 'p'},                          {type => END_TAG_TOKEN, tag_name => 'p'},
5113                          {type => 'start tag', tag_name => 'hr'},                          {type => START_TAG_TOKEN, tag_name => 'hr'},
5114                          {type => 'end tag', tag_name => 'form'};                          {type => END_TAG_TOKEN, tag_name => 'form'};
5115            $token = shift @tokens;            $token = shift @tokens;
5116            !!!back-token (@tokens);            !!!back-token (@tokens);
5117            return;            redo B;
5118          }          }
5119        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
5120          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
# Line 4775  sub _tree_construction_main ($) { Line 5129  sub _tree_construction_main ($) {
5129                    
5130          my $text = '';          my $text = '';
5131          !!!next-token;          !!!next-token;
5132          if ($token->{type} eq 'character') {          if ($token->{type} == CHARACTER_TOKEN) {
5133            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
5134            unless (length $token->{data}) {            unless (length $token->{data}) {
5135              !!!next-token;              !!!next-token;
5136            }            }
5137          }          }
5138          while ($token->{type} eq 'character') {          while ($token->{type} == CHARACTER_TOKEN) {
5139            $text .= $token->{data};            $text .= $token->{data};
5140            !!!next-token;            !!!next-token;
5141          }          }
# Line 4791  sub _tree_construction_main ($) { Line 5145  sub _tree_construction_main ($) {
5145                    
5146          $self->{content_model} = PCDATA_CONTENT_MODEL;          $self->{content_model} = PCDATA_CONTENT_MODEL;
5147                    
5148          if ($token->{type} eq 'end tag' and          if ($token->{type} == END_TAG_TOKEN and
5149              $token->{tag_name} eq $tag_name) {              $token->{tag_name} eq $tag_name) {
5150            ## Ignore the token            ## Ignore the token
5151          } else {          } else {
5152            !!!parse-error (type => 'in RCDATA:#'.$token->{type});            !!!parse-error (type => 'in RCDATA:#'.$token->{type});
5153          }          }
5154          !!!next-token;          !!!next-token;
5155          return;          redo B;
5156        } elsif ({        } elsif ({
5157                  iframe => 1,                  iframe => 1,
5158                  noembed => 1,                  noembed => 1,
5159                  noframes => 1,                  noframes => 1,
5160                  noscript => 0, ## TODO: 1 if scripting is enabled                  noscript => 0, ## TODO: 1 if scripting is enabled
5161                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5162          ## NOTE: There are two "as if in body" code clones.          ## NOTE: There is an "as if in body" code clone.
5163          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
5164          return;          redo B;
5165        } elsif ($token->{tag_name} eq 'select') {        } elsif ($token->{tag_name} eq 'select') {
5166          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5167                    
5168          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5169                    
5170          $self->{insertion_mode} = 'in select';          $self->{insertion_mode} = IN_SELECT_IM;
5171          !!!next-token;          !!!next-token;
5172          return;          redo B;
5173        } elsif ({        } elsif ({
5174                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
5175                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 4825  sub _tree_construction_main ($) { Line 5179  sub _tree_construction_main ($) {
5179          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!parse-error (type => 'in body:'.$token->{tag_name});
5180          ## Ignore the token          ## Ignore the token
5181          !!!next-token;          !!!next-token;
5182          return;          redo B;
5183                    
5184          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
5185        } else {        } else {
# Line 4834  sub _tree_construction_main ($) { Line 5188  sub _tree_construction_main ($) {
5188          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5189                    
5190          !!!next-token;          !!!next-token;
5191          return;          redo B;
5192        }        }
5193      } elsif ($token->{type} eq 'end tag') {      } elsif ($token->{type} == END_TAG_TOKEN) {
5194        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
5195          if (@{$self->{open_elements}} > 1 and          if (@{$self->{open_elements}} > 1 and
5196              $self->{open_elements}->[1]->[1] eq 'body') {              $self->{open_elements}->[1]->[1] eq 'body') {
# Line 4850  sub _tree_construction_main ($) { Line 5204  sub _tree_construction_main ($) {
5204              }              }
5205            }            }
5206    
5207            $self->{insertion_mode} = 'after body';            $self->{insertion_mode} = AFTER_BODY_IM;
5208            !!!next-token;            !!!next-token;
5209            return;            redo B;
5210          } else {          } else {
5211            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5212            ## Ignore the token            ## Ignore the token
5213            !!!next-token;            !!!next-token;
5214            return;            redo B;
5215          }          }
5216        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
5217          if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {          if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {
# Line 4865  sub _tree_construction_main ($) { Line 5219  sub _tree_construction_main ($) {
5219            if ($self->{open_elements}->[-1]->[1] ne 'body') {            if ($self->{open_elements}->[-1]->[1] ne 'body') {
5220              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);
5221            }            }
5222            $self->{insertion_mode} = 'after body';            $self->{insertion_mode} = AFTER_BODY_IM;
5223            ## reprocess            ## reprocess
5224            return;            redo B;
5225          } else {          } else {
5226            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5227            ## Ignore the token            ## Ignore the token
5228            !!!next-token;            !!!next-token;
5229            return;            redo B;
5230          }          }
5231        } elsif ({        } elsif ({
5232                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
# Line 4897  sub _tree_construction_main ($) { Line 5251  sub _tree_construction_main ($) {
5251                   tbody => 1, tfoot=> 1, thead => 1,                   tbody => 1, tfoot=> 1, thead => 1,
5252                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
5253                !!!back-token;                !!!back-token;
5254                $token = {type => 'end tag',                $token = {type => END_TAG_TOKEN,
5255                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5256                return;                redo B;
5257              }              }
5258              $i = $_;              $i = $_;
5259              last INSCOPE unless $token->{tag_name} eq 'p';              last INSCOPE unless $token->{tag_name} eq 'p';
# Line 4932  sub _tree_construction_main ($) { Line 5286  sub _tree_construction_main ($) {
5286              button => 1, marquee => 1, object => 1,              button => 1, marquee => 1, object => 1,
5287            }->{$token->{tag_name}};            }->{$token->{tag_name}};
5288          !!!next-token;          !!!next-token;
5289          return;          redo B;
5290        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
5291          ## has an element in scope          ## has an element in scope
5292          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 4945  sub _tree_construction_main ($) { Line 5299  sub _tree_construction_main ($) {
5299                   tbody => 1, tfoot=> 1, thead => 1,                   tbody => 1, tfoot=> 1, thead => 1,
5300                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
5301                !!!back-token;                !!!back-token;
5302                $token = {type => 'end tag',                $token = {type => END_TAG_TOKEN,
5303                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5304                return;                redo B;
5305              }              }
5306              last INSCOPE;              last INSCOPE;
5307            } elsif ({            } elsif ({
# Line 4961  sub _tree_construction_main ($) { Line 5315  sub _tree_construction_main ($) {
5315          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {
5316            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
5317          } else {          } else {
5318            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5319          }          }
5320    
5321          undef $self->{form_element};          undef $self->{form_element};
5322          !!!next-token;          !!!next-token;
5323          return;          redo B;
5324        } elsif ({        } elsif ({
5325                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5326                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 4984  sub _tree_construction_main ($) { Line 5338  sub _tree_construction_main ($) {
5338                   tbody => 1, tfoot=> 1, thead => 1,                   tbody => 1, tfoot=> 1, thead => 1,
5339                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
5340                !!!back-token;                !!!back-token;
5341                $token = {type => 'end tag',                $token = {type => END_TAG_TOKEN,
5342                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5343                return;                redo B;
5344              }              }
5345              $i = $_;              $i = $_;
5346              last INSCOPE;              last INSCOPE;
# Line 4999  sub _tree_construction_main ($) { Line 5353  sub _tree_construction_main ($) {
5353          } # INSCOPE          } # INSCOPE
5354                    
5355          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
5356            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5357          }          }
5358                    
5359          splice @{$self->{open_elements}}, $i if defined $i;          splice @{$self->{open_elements}}, $i if defined $i;
5360          !!!next-token;          !!!next-token;
5361          return;          redo B;
5362        } elsif ({        } elsif ({
5363                  a => 1,                  a => 1,
5364                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
# Line 5012  sub _tree_construction_main ($) { Line 5366  sub _tree_construction_main ($) {
5366                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
5367                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5368          $formatting_end_tag->($token->{tag_name});          $formatting_end_tag->($token->{tag_name});
5369          return;          redo B;
5370        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
5371          !!!parse-error (type => 'unmatched end tag:br');          !!!parse-error (type => 'unmatched end tag:br');
5372    
# Line 5025  sub _tree_construction_main ($) { Line 5379  sub _tree_construction_main ($) {
5379                    
5380          ## Ignore the token.          ## Ignore the token.
5381          !!!next-token;          !!!next-token;
5382          return;          redo B;
5383        } elsif ({        } elsif ({
5384                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
5385                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 5041  sub _tree_construction_main ($) { Line 5395  sub _tree_construction_main ($) {
5395          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5396          ## Ignore the token          ## Ignore the token
5397          !!!next-token;          !!!next-token;
5398          return;          redo B;
5399                    
5400          ## ISSUE: Issue on HTML5 new elements in spec          ## ISSUE: Issue on HTML5 new elements in spec
5401                    
# Line 5058  sub _tree_construction_main ($) { Line 5412  sub _tree_construction_main ($) {
5412              if ({              if ({
5413                   dd => 1, dt => 1, li => 1, p => 1,                   dd => 1, dt => 1, li => 1, p => 1,
5414                   td => 1, th => 1, tr => 1,                   td => 1, th => 1, tr => 1,
5415                   tbody => 1, tfoot=> 1, thead => 1,                   tbody => 1, tfoot => 1, thead => 1,
5416                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
5417                !!!back-token;                !!!back-token;
5418                $token = {type => 'end tag',                $token = {type => END_TAG_TOKEN,
5419                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5420                return;                redo B;
5421              }              }
5422                    
5423              ## Step 2              ## Step 2
5424              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {
5425                  ## NOTE: <x><y></x>
5426                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
5427              }              }
5428                            
# Line 5096  sub _tree_construction_main ($) { Line 5451  sub _tree_construction_main ($) {
5451            ## Step 5;            ## Step 5;
5452            redo S2;            redo S2;
5453          } # S2          } # S2
5454          return;          redo B;
5455        }        }
5456      }      }
   }; # $in_body  
     $in_body->($insert_to_current);  
5457      redo B;      redo B;
5458    } # B    } # B
5459    
# Line 5120  sub set_inner_html ($$$) { Line 5473  sub set_inner_html ($$$) {
5473    my $s = \$_[0];    my $s = \$_[0];
5474    my $onerror = $_[1];    my $onerror = $_[1];
5475    
5476      ## ISSUE: Should {confident} be true?
5477    
5478    my $nt = $node->node_type;    my $nt = $node->node_type;
5479    if ($nt == 9) {    if ($nt == 9) {
5480      # MUST      # MUST
# Line 5192  sub set_inner_html ($$$) { Line 5547  sub set_inner_html ($$$) {
5547      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
5548    
5549      ## Step 2      ## Step 2
5550      my $node_ln = $node->local_name;      my $node_ln = $node->manakai_local_name;
5551      $p->{content_model} = {      $p->{content_model} = {
5552        title => RCDATA_CONTENT_MODEL,        title => RCDATA_CONTENT_MODEL,
5553        textarea => RCDATA_CONTENT_MODEL,        textarea => RCDATA_CONTENT_MODEL,
# Line 5232  sub set_inner_html ($$$) { Line 5587  sub set_inner_html ($$$) {
5587        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
5588          my $nsuri = $anode->namespace_uri;          my $nsuri = $anode->namespace_uri;
5589          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
5590            if ($anode->local_name eq 'form') { ## TODO: case?            if ($anode->manakai_local_name eq 'form') {
5591              $p->{form_element} = $anode;              $p->{form_element} = $anode;
5592              last AN;              last AN;
5593            }            }
# Line 5272  sub set_inner_html ($$$) { Line 5627  sub set_inner_html ($$$) {
5627    
5628  } # tree construction stage  } # tree construction stage
5629    
5630  sub get_inner_html ($$$) {  package Whatpm::HTML::RestartParser;
5631    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  
5632    
5633  1;  1;
5634  # $Date$  # $Date$

Legend:
Removed from v.1.52  
changed lines
  Added in v.1.72

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24