/[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.49 by wakaba, Sat Jul 21 10:39:45 2007 UTC revision 1.75 by wakaba, Mon Mar 3 00:13:22 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)  ##   ->{quirks} == 1 or 0 (DOCTYPE_TOKEN): "force-quirks" flag
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, quirks => 1});
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, quirks => 1});
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};                 #quirks => 0,
1466                  };
1467  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
1468          $self->{state} = 'DOCTYPE name';          $self->{state} = DOCTYPE_NAME_STATE;
1469          !!!next-input-character;          !!!next-input-character;
1470          redo A;          redo A;
1471        }        }
1472      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
1473  ## ISSUE: Redundant "First," in the spec.  ## ISSUE: Redundant "First," in the spec.
1474        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1475            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1476            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
1477            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1478            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
1479          $self->{state} = 'after DOCTYPE name';          $self->{state} = AFTER_DOCTYPE_NAME_STATE;
1480          !!!next-input-character;          !!!next-input-character;
1481          redo A;          redo A;
1482        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1483          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1484          !!!next-input-character;          !!!next-input-character;
1485    
1486          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1229  sub _get_next_token ($) { Line 1488  sub _get_next_token ($) {
1488          redo A;          redo A;
1489        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1490          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1491          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1492          ## reconsume          ## reconsume
1493    
1494          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1495          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1496    
1497          redo A;          redo A;
# Line 1243  sub _get_next_token ($) { Line 1502  sub _get_next_token ($) {
1502          !!!next-input-character;          !!!next-input-character;
1503          redo A;          redo A;
1504        }        }
1505      } elsif ($self->{state} eq 'after DOCTYPE name') {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
1506        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1507            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1508            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 1253  sub _get_next_token ($) { Line 1512  sub _get_next_token ($) {
1512          !!!next-input-character;          !!!next-input-character;
1513          redo A;          redo A;
1514        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1515          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1516          !!!next-input-character;          !!!next-input-character;
1517    
1518          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1261  sub _get_next_token ($) { Line 1520  sub _get_next_token ($) {
1520          redo A;          redo A;
1521        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1522          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1523          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1524          ## reconsume          ## reconsume
1525    
1526          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1527          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1528    
1529          redo A;          redo A;
# Line 1285  sub _get_next_token ($) { Line 1544  sub _get_next_token ($) {
1544                  !!!next-input-character;                  !!!next-input-character;
1545                  if ($self->{next_input_character} == 0x0043 or # C                  if ($self->{next_input_character} == 0x0043 or # C
1546                      $self->{next_input_character} == 0x0063) { # c                      $self->{next_input_character} == 0x0063) { # c
1547                    $self->{state} = 'before DOCTYPE public identifier';                    $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1548                    !!!next-input-character;                    !!!next-input-character;
1549                    redo A;                    redo A;
1550                  }                  }
# Line 1312  sub _get_next_token ($) { Line 1571  sub _get_next_token ($) {
1571                  !!!next-input-character;                  !!!next-input-character;
1572                  if ($self->{next_input_character} == 0x004D or # M                  if ($self->{next_input_character} == 0x004D or # M
1573                      $self->{next_input_character} == 0x006D) { # m                      $self->{next_input_character} == 0x006D) { # m
1574                    $self->{state} = 'before DOCTYPE system identifier';                    $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1575                    !!!next-input-character;                    !!!next-input-character;
1576                    redo A;                    redo A;
1577                  }                  }
# Line 1328  sub _get_next_token ($) { Line 1587  sub _get_next_token ($) {
1587        }        }
1588    
1589        !!!parse-error (type => 'string after DOCTYPE name');        !!!parse-error (type => 'string after DOCTYPE name');
1590        $self->{state} = 'bogus DOCTYPE';        $self->{current_token}->{quirks} = 1;
1591    
1592          $self->{state} = BOGUS_DOCTYPE_STATE;
1593        # next-input-character is already done        # next-input-character is already done
1594        redo A;        redo A;
1595      } elsif ($self->{state} eq 'before DOCTYPE public identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
1596        if ({        if ({
1597              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1598              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1341  sub _get_next_token ($) { Line 1602  sub _get_next_token ($) {
1602          redo A;          redo A;
1603        } elsif ($self->{next_input_character} eq 0x0022) { # "        } elsif ($self->{next_input_character} eq 0x0022) { # "
1604          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1605          $self->{state} = 'DOCTYPE public identifier (double-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
1606          !!!next-input-character;          !!!next-input-character;
1607          redo A;          redo A;
1608        } elsif ($self->{next_input_character} eq 0x0027) { # '        } elsif ($self->{next_input_character} eq 0x0027) { # '
1609          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1610          $self->{state} = 'DOCTYPE public identifier (single-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
1611          !!!next-input-character;          !!!next-input-character;
1612          redo A;          redo A;
1613        } elsif ($self->{next_input_character} eq 0x003E) { # >        } elsif ($self->{next_input_character} eq 0x003E) { # >
1614          !!!parse-error (type => 'no PUBLIC literal');          !!!parse-error (type => 'no PUBLIC literal');
1615    
1616          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1617          !!!next-input-character;          !!!next-input-character;
1618    
1619          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1620          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1621    
1622          redo A;          redo A;
1623        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1624          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1625    
1626          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1627          ## reconsume          ## reconsume
1628    
1629          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1630          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1631    
1632          redo A;          redo A;
1633        } else {        } else {
1634          !!!parse-error (type => 'string after PUBLIC');          !!!parse-error (type => 'string after PUBLIC');
1635          $self->{state} = 'bogus DOCTYPE';          $self->{current_token}->{quirks} = 1;
1636    
1637            $self->{state} = BOGUS_DOCTYPE_STATE;
1638          !!!next-input-character;          !!!next-input-character;
1639          redo A;          redo A;
1640        }        }
1641      } elsif ($self->{state} eq 'DOCTYPE public identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
1642        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_input_character} == 0x0022) { # "
1643          $self->{state} = 'after DOCTYPE public identifier';          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1644          !!!next-input-character;          !!!next-input-character;
1645          redo A;          redo A;
1646          } elsif ($self->{next_input_character} == 0x003E) { # >
1647            !!!parse-error (type => 'unclosed PUBLIC literal');
1648    
1649            $self->{state} = DATA_STATE;
1650            !!!next-input-character;
1651    
1652            $self->{current_token}->{quirks} = 1;
1653            !!!emit ($self->{current_token}); # DOCTYPE
1654    
1655            redo A;
1656        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1657          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
1658    
1659          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1660          ## reconsume          ## reconsume
1661    
1662          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1663          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1664    
1665          redo A;          redo A;
# Line 1397  sub _get_next_token ($) { Line 1670  sub _get_next_token ($) {
1670          !!!next-input-character;          !!!next-input-character;
1671          redo A;          redo A;
1672        }        }
1673      } elsif ($self->{state} eq 'DOCTYPE public identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
1674        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_input_character} == 0x0027) { # '
1675          $self->{state} = 'after DOCTYPE public identifier';          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1676          !!!next-input-character;          !!!next-input-character;
1677          redo A;          redo A;
1678          } elsif ($self->{next_input_character} == 0x003E) { # >
1679            !!!parse-error (type => 'unclosed PUBLIC literal');
1680    
1681            $self->{state} = DATA_STATE;
1682            !!!next-input-character;
1683    
1684            $self->{current_token}->{quirks} = 1;
1685            !!!emit ($self->{current_token}); # DOCTYPE
1686    
1687            redo A;
1688        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1689          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
1690    
1691          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1692          ## reconsume          ## reconsume
1693    
1694          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1695          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1696    
1697          redo A;          redo A;
# Line 1419  sub _get_next_token ($) { Line 1702  sub _get_next_token ($) {
1702          !!!next-input-character;          !!!next-input-character;
1703          redo A;          redo A;
1704        }        }
1705      } elsif ($self->{state} eq 'after DOCTYPE public identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
1706        if ({        if ({
1707              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1708              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1429  sub _get_next_token ($) { Line 1712  sub _get_next_token ($) {
1712          redo A;          redo A;
1713        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_input_character} == 0x0022) { # "
1714          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1715          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
1716          !!!next-input-character;          !!!next-input-character;
1717          redo A;          redo A;
1718        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_input_character} == 0x0027) { # '
1719          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1720          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
1721          !!!next-input-character;          !!!next-input-character;
1722          redo A;          redo A;
1723        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1724          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1725          !!!next-input-character;          !!!next-input-character;
1726    
1727          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1447  sub _get_next_token ($) { Line 1730  sub _get_next_token ($) {
1730        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1731          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1732    
1733          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1734          ## reconsume          ## reconsume
1735    
1736          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1737          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1738    
1739          redo A;          redo A;
1740        } else {        } else {
1741          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after PUBLIC literal');
1742          $self->{state} = 'bogus DOCTYPE';          $self->{current_token}->{quirks} = 1;
1743    
1744            $self->{state} = BOGUS_DOCTYPE_STATE;
1745          !!!next-input-character;          !!!next-input-character;
1746          redo A;          redo A;
1747        }        }
1748      } elsif ($self->{state} eq 'before DOCTYPE system identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
1749        if ({        if ({
1750              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1751              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1470  sub _get_next_token ($) { Line 1755  sub _get_next_token ($) {
1755          redo A;          redo A;
1756        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_input_character} == 0x0022) { # "
1757          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1758          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
1759          !!!next-input-character;          !!!next-input-character;
1760          redo A;          redo A;
1761        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_input_character} == 0x0027) { # '
1762          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1763          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
1764          !!!next-input-character;          !!!next-input-character;
1765          redo A;          redo A;
1766        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1767          !!!parse-error (type => 'no SYSTEM literal');          !!!parse-error (type => 'no SYSTEM literal');
1768          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1769          !!!next-input-character;          !!!next-input-character;
1770    
1771          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1772          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1773    
1774          redo A;          redo A;
1775        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1776          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1777    
1778          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1779          ## reconsume          ## reconsume
1780    
1781          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1782          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1783    
1784          redo A;          redo A;
1785        } else {        } else {
1786          !!!parse-error (type => 'string after SYSTEM');          !!!parse-error (type => 'string after SYSTEM');
1787          $self->{state} = 'bogus DOCTYPE';          $self->{current_token}->{quirks} = 1;
1788    
1789            $self->{state} = BOGUS_DOCTYPE_STATE;
1790          !!!next-input-character;          !!!next-input-character;
1791          redo A;          redo A;
1792        }        }
1793      } elsif ($self->{state} eq 'DOCTYPE system identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
1794        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_input_character} == 0x0022) { # "
1795          $self->{state} = 'after DOCTYPE system identifier';          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1796            !!!next-input-character;
1797            redo A;
1798          } elsif ($self->{next_input_character} == 0x003E) { # >
1799            !!!parse-error (type => 'unclosed PUBLIC literal');
1800    
1801            $self->{state} = DATA_STATE;
1802          !!!next-input-character;          !!!next-input-character;
1803    
1804            $self->{current_token}->{quirks} = 1;
1805            !!!emit ($self->{current_token}); # DOCTYPE
1806    
1807          redo A;          redo A;
1808        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1809          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
1810    
1811          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1812          ## reconsume          ## reconsume
1813    
1814          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1815          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1816    
1817          redo A;          redo A;
# Line 1525  sub _get_next_token ($) { Line 1822  sub _get_next_token ($) {
1822          !!!next-input-character;          !!!next-input-character;
1823          redo A;          redo A;
1824        }        }
1825      } elsif ($self->{state} eq 'DOCTYPE system identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
1826        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_input_character} == 0x0027) { # '
1827          $self->{state} = 'after DOCTYPE system identifier';          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1828            !!!next-input-character;
1829            redo A;
1830          } elsif ($self->{next_input_character} == 0x003E) { # >
1831            !!!parse-error (type => 'unclosed PUBLIC literal');
1832    
1833            $self->{state} = DATA_STATE;
1834          !!!next-input-character;          !!!next-input-character;
1835    
1836            $self->{current_token}->{quirks} = 1;
1837            !!!emit ($self->{current_token}); # DOCTYPE
1838    
1839          redo A;          redo A;
1840        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1841          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
1842    
1843          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1844          ## reconsume          ## reconsume
1845    
1846          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1847          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1848    
1849          redo A;          redo A;
# Line 1547  sub _get_next_token ($) { Line 1854  sub _get_next_token ($) {
1854          !!!next-input-character;          !!!next-input-character;
1855          redo A;          redo A;
1856        }        }
1857      } elsif ($self->{state} eq 'after DOCTYPE system identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
1858        if ({        if ({
1859              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1860              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1556  sub _get_next_token ($) { Line 1863  sub _get_next_token ($) {
1863          !!!next-input-character;          !!!next-input-character;
1864          redo A;          redo A;
1865        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1866          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1867          !!!next-input-character;          !!!next-input-character;
1868    
1869          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1565  sub _get_next_token ($) { Line 1872  sub _get_next_token ($) {
1872        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1873          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1874    
1875          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1876          ## reconsume          ## reconsume
1877    
1878          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1879          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1880    
1881          redo A;          redo A;
1882        } else {        } else {
1883          !!!parse-error (type => 'string after SYSTEM literal');          !!!parse-error (type => 'string after SYSTEM literal');
1884          $self->{state} = 'bogus DOCTYPE';          #$self->{current_token}->{quirks} = 1;
1885    
1886            $self->{state} = BOGUS_DOCTYPE_STATE;
1887          !!!next-input-character;          !!!next-input-character;
1888          redo A;          redo A;
1889        }        }
1890      } elsif ($self->{state} eq 'bogus DOCTYPE') {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
1891        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_input_character} == 0x003E) { # >
1892          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1893          !!!next-input-character;          !!!next-input-character;
1894    
         delete $self->{current_token}->{correct};  
1895          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1896    
1897          redo A;          redo A;
1898        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1899          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1900          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1901          ## reconsume          ## reconsume
1902    
         delete $self->{current_token}->{correct};  
1903          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1904    
1905          redo A;          redo A;
# Line 1609  sub _get_next_token ($) { Line 1916  sub _get_next_token ($) {
1916    die "$0: _get_next_token: unexpected case";    die "$0: _get_next_token: unexpected case";
1917  } # _get_next_token  } # _get_next_token
1918    
1919  sub _tokenize_attempt_to_consume_an_entity ($$) {  sub _tokenize_attempt_to_consume_an_entity ($$$) {
1920    my ($self, $in_attr) = @_;    my ($self, $in_attr, $additional) = @_;
1921    
1922    if ({    if ({
1923         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
1924         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
1925           $additional => 1,
1926        }->{$self->{next_input_character}}) {        }->{$self->{next_input_character}}) {
1927      ## Don't consume      ## Don't consume
1928      ## No error      ## No error
# Line 1670  sub _tokenize_attempt_to_consume_an_enti Line 1978  sub _tokenize_attempt_to_consume_an_enti
1978            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
1979          }          }
1980    
1981          return {type => 'character', data => chr $code};          return {type => CHARACTER_TOKEN, data => chr $code,
1982                    has_reference => 1};
1983        } # X        } # X
1984      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_input_character} and
1985               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_input_character} <= 0x0039) { # 0..9
# Line 1705  sub _tokenize_attempt_to_consume_an_enti Line 2014  sub _tokenize_attempt_to_consume_an_enti
2014          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
2015        }        }
2016                
2017        return {type => 'character', data => chr $code};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1};
2018      } else {      } else {
2019        !!!parse-error (type => 'bare nero');        !!!parse-error (type => 'bare nero');
2020        !!!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 2062  sub _tokenize_attempt_to_consume_an_enti
2062      }      }
2063            
2064      if ($match > 0) {      if ($match > 0) {
2065        return {type => 'character', data => $value};        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};
2066      } elsif ($match < 0) {      } elsif ($match < 0) {
2067        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc');
2068        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
2069          return {type => 'character', data => '&'.$entity_name};          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};
2070        } else {        } else {
2071          return {type => 'character', data => $value};          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};
2072        }        }
2073      } else {      } else {
2074        !!!parse-error (type => 'bare ero');        !!!parse-error (type => 'bare ero');
2075        ## NOTE: No characters are consumed in the spec.        ## NOTE: "No characters are consumed" in the spec.
2076        return {type => 'character', data => '&'.$value};        return {type => CHARACTER_TOKEN, data => '&'.$value};
2077      }      }
2078    } else {    } else {
2079      ## no characters are consumed      ## no characters are consumed
# Line 1806  sub _construct_tree ($) { Line 2115  sub _construct_tree ($) {
2115        
2116    !!!next-token;    !!!next-token;
2117    
2118    $self->{insertion_mode} = 'before head';    $self->{insertion_mode} = BEFORE_HEAD_IM;
2119    undef $self->{form_element};    undef $self->{form_element};
2120    undef $self->{head_element};    undef $self->{head_element};
2121    $self->{open_elements} = [];    $self->{open_elements} = [];
# Line 1820  sub _construct_tree ($) { Line 2129  sub _construct_tree ($) {
2129  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
2130    my $self = shift;    my $self = shift;
2131    INITIAL: {    INITIAL: {
2132      if ($token->{type} eq 'DOCTYPE') {      if ($token->{type} == DOCTYPE_TOKEN) {
2133        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
2134        ## error, switch to a conformance checking mode for another        ## error, switch to a conformance checking mode for another
2135        ## language.        ## language.
# Line 1846  sub _tree_construction_initial ($) { Line 2155  sub _tree_construction_initial ($) {
2155        ## ISSUE: internalSubset = null??        ## ISSUE: internalSubset = null??
2156        $self->{document}->append_child ($doctype);        $self->{document}->append_child ($doctype);
2157                
2158        if (not $token->{correct} or $doctype_name ne 'HTML') {        if ($token->{quirks} or $doctype_name ne 'HTML') {
2159          $self->{document}->manakai_compat_mode ('quirks');          $self->{document}->manakai_compat_mode ('quirks');
2160        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{public_identifier}) {
2161          my $pubid = $token->{public_identifier};          my $pubid = $token->{public_identifier};
# Line 1900  sub _tree_construction_initial ($) { Line 2209  sub _tree_construction_initial ($) {
2209            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,
2210            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,
2211            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,
2212              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//EN" => 1,
2213              "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//EN" => 1,
2214              "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//EN" => 1,
2215            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,
2216            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,
2217            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,
# Line 1947  sub _tree_construction_initial ($) { Line 2259  sub _tree_construction_initial ($) {
2259        !!!next-token;        !!!next-token;
2260        return;        return;
2261      } elsif ({      } elsif ({
2262                'start tag' => 1,                START_TAG_TOKEN, 1,
2263                'end tag' => 1,                END_TAG_TOKEN, 1,
2264                'end-of-file' => 1,                END_OF_FILE_TOKEN, 1,
2265               }->{$token->{type}}) {               }->{$token->{type}}) {
2266        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE');
2267        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
2268        ## Go to the root element phase        ## Go to the root element phase
2269        ## reprocess        ## reprocess
2270        return;        return;
2271      } elsif ($token->{type} eq 'character') {      } elsif ($token->{type} == CHARACTER_TOKEN) {
2272        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2273          ## Ignore the token          ## Ignore the token
2274    
# Line 1972  sub _tree_construction_initial ($) { Line 2284  sub _tree_construction_initial ($) {
2284        ## Go to the root element phase        ## Go to the root element phase
2285        ## reprocess        ## reprocess
2286        return;        return;
2287      } elsif ($token->{type} eq 'comment') {      } elsif ($token->{type} == COMMENT_TOKEN) {
2288        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
2289        $self->{document}->append_child ($comment);        $self->{document}->append_child ($comment);
2290                
# Line 1980  sub _tree_construction_initial ($) { Line 2292  sub _tree_construction_initial ($) {
2292        !!!next-token;        !!!next-token;
2293        redo INITIAL;        redo INITIAL;
2294      } else {      } else {
2295        die "$0: $token->{type}: Unknown token";        die "$0: $token->{type}: Unknown token type";
2296      }      }
2297    } # INITIAL    } # INITIAL
2298  } # _tree_construction_initial  } # _tree_construction_initial
# Line 1989  sub _tree_construction_root_element ($) Line 2301  sub _tree_construction_root_element ($)
2301    my $self = shift;    my $self = shift;
2302        
2303    B: {    B: {
2304        if ($token->{type} eq 'DOCTYPE') {        if ($token->{type} == DOCTYPE_TOKEN) {
2305          !!!parse-error (type => 'in html:#DOCTYPE');          !!!parse-error (type => 'in html:#DOCTYPE');
2306          ## Ignore the token          ## Ignore the token
2307          ## Stay in the phase          ## Stay in the phase
2308          !!!next-token;          !!!next-token;
2309          redo B;          redo B;
2310        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{type} == COMMENT_TOKEN) {
2311          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
2312          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
2313          ## Stay in the phase          ## Stay in the phase
2314          !!!next-token;          !!!next-token;
2315          redo B;          redo B;
2316        } elsif ($token->{type} eq 'character') {        } elsif ($token->{type} == CHARACTER_TOKEN) {
2317          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2318            ## Ignore the token.            ## Ignore the token.
2319    
# Line 2011  sub _tree_construction_root_element ($) Line 2323  sub _tree_construction_root_element ($)
2323              redo B;              redo B;
2324            }            }
2325          }          }
2326    
2327            $self->{application_cache_selection}->(undef);
2328    
2329            #
2330          } elsif ($token->{type} == START_TAG_TOKEN) {
2331            if ($token->{tag_name} eq 'html' and
2332                $token->{attributes}->{manifest}) {
2333              $self->{application_cache_selection}
2334                   ->($token->{attributes}->{manifest}->{value});
2335              ## ISSUE: No relative reference resolution?
2336            } else {
2337              $self->{application_cache_selection}->(undef);
2338            }
2339    
2340            ## ISSUE: There is an issue in the spec
2341          #          #
2342        } elsif ({        } elsif ({
2343                  'start tag' => 1,                  END_TAG_TOKEN, 1,
2344                  'end tag' => 1,                  END_OF_FILE_TOKEN, 1,
                 'end-of-file' => 1,  
2345                 }->{$token->{type}}) {                 }->{$token->{type}}) {
2346            $self->{application_cache_selection}->(undef);
2347    
2348          ## ISSUE: There is an issue in the spec          ## ISSUE: There is an issue in the spec
2349          #          #
2350        } else {        } else {
2351          die "$0: $token->{type}: Unknown token";          die "$0: $token->{type}: Unknown token type";
2352        }        }
2353    
2354        my $root_element; !!!create-element ($root_element, 'html');        my $root_element; !!!create-element ($root_element, 'html');
2355        $self->{document}->append_child ($root_element);        $self->{document}->append_child ($root_element);
2356        push @{$self->{open_elements}}, [$root_element, 'html'];        push @{$self->{open_elements}}, [$root_element, 'html'];
# Line 2062  sub _reset_insertion_mode ($) { Line 2391  sub _reset_insertion_mode ($) {
2391            
2392        ## Step 4..13        ## Step 4..13
2393        my $new_mode = {        my $new_mode = {
2394                        select => 'in select',                        select => IN_SELECT_IM,
2395                        td => 'in cell',                        td => IN_CELL_IM,
2396                        th => 'in cell',                        th => IN_CELL_IM,
2397                        tr => 'in row',                        tr => IN_ROW_IM,
2398                        tbody => 'in table body',                        tbody => IN_TABLE_BODY_IM,
2399                        thead => 'in table body',                        thead => IN_TABLE_BODY_IM,
2400                        tfoot => 'in table body',                        tfoot => IN_TABLE_BODY_IM,
2401                        caption => 'in caption',                        caption => IN_CAPTION_IM,
2402                        colgroup => 'in column group',                        colgroup => IN_COLUMN_GROUP_IM,
2403                        table => 'in table',                        table => IN_TABLE_IM,
2404                        head => 'in body', # not in head!                        head => IN_BODY_IM, # not in head!
2405                        body => 'in body',                        body => IN_BODY_IM,
2406                        frameset => 'in frameset',                        frameset => IN_FRAMESET_IM,
2407                       }->{$node->[1]};                       }->{$node->[1]};
2408        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
2409                
2410        ## Step 14        ## Step 14
2411        if ($node->[1] eq 'html') {        if ($node->[1] eq 'html') {
2412          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
2413            $self->{insertion_mode} = 'before head';            $self->{insertion_mode} = BEFORE_HEAD_IM;
2414          } else {          } else {
2415            $self->{insertion_mode} = 'after head';            $self->{insertion_mode} = AFTER_HEAD_IM;
2416          }          }
2417          return;          return;
2418        }        }
2419                
2420        ## Step 15        ## Step 15
2421        $self->{insertion_mode} = 'in body' and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
2422                
2423        ## Step 16        ## Step 16
2424        $i--;        $i--;
# Line 2103  sub _reset_insertion_mode ($) { Line 2432  sub _reset_insertion_mode ($) {
2432  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
2433    my $self = shift;    my $self = shift;
2434    
   my $previous_insertion_mode;  
   
2435    my $active_formatting_elements = [];    my $active_formatting_elements = [];
2436    
2437    my $reconstruct_active_formatting_elements = sub { # MUST    my $reconstruct_active_formatting_elements = sub { # MUST
# Line 2205  sub _tree_construction_main ($) { Line 2532  sub _tree_construction_main ($) {
2532      ## Step 4      ## Step 4
2533      my $text = '';      my $text = '';
2534      !!!next-token;      !!!next-token;
2535      while ($token->{type} eq 'character') { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
2536        $text .= $token->{data};        $text .= $token->{data};
2537        !!!next-token;        !!!next-token;
2538      }      }
# Line 2220  sub _tree_construction_main ($) { Line 2547  sub _tree_construction_main ($) {
2547      $self->{content_model} = PCDATA_CONTENT_MODEL;      $self->{content_model} = PCDATA_CONTENT_MODEL;
2548    
2549      ## Step 7      ## Step 7
2550      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) {
2551        ## Ignore the token        ## Ignore the token
2552      } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {      } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {
2553        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!parse-error (type => 'in CDATA:#'.$token->{type});
# Line 2243  sub _tree_construction_main ($) { Line 2570  sub _tree_construction_main ($) {
2570            
2571      my $text = '';      my $text = '';
2572      !!!next-token;      !!!next-token;
2573      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) {
2574        $text .= $token->{data};        $text .= $token->{data};
2575        !!!next-token;        !!!next-token;
2576      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
# Line 2253  sub _tree_construction_main ($) { Line 2580  sub _tree_construction_main ($) {
2580                                
2581      $self->{content_model} = PCDATA_CONTENT_MODEL;      $self->{content_model} = PCDATA_CONTENT_MODEL;
2582    
2583      if ($token->{type} eq 'end tag' and      if ($token->{type} == END_TAG_TOKEN and
2584          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
2585        ## Ignore the token        ## Ignore the token
2586      } else {      } else {
# Line 2496  sub _tree_construction_main ($) { Line 2823  sub _tree_construction_main ($) {
2823                         }                         }
2824    }; # $insert_to_foster    }; # $insert_to_foster
2825    
2826    my $in_body = sub {    my $insert;
     my $insert = shift;  
     if ($token->{type} eq 'start tag') {  
       if ($token->{tag_name} eq 'script') {  
         ## NOTE: This is an "as if in head" code clone  
         $script_start_tag->($insert);  
         return;  
       } elsif ($token->{tag_name} eq 'style') {  
         ## NOTE: This is an "as if in head" code clone  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         return;  
       } elsif ({  
                 base => 1, link => 1,  
                }->{$token->{tag_name}}) {  
         ## NOTE: This is an "as if in head" code clone, only "-t" differs  
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'meta') {  
         ## NOTE: This is an "as if in head" code clone, only "-t" differs  
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.  
   
         unless ($self->{confident}) {  
           my $charset;  
           if ($token->{attributes}->{charset}) { ## TODO: And if supported  
             $charset = $token->{attributes}->{charset}->{value};  
           }  
           if ($token->{attributes}->{'http-equiv'}) {  
             ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
             if ($token->{attributes}->{'http-equiv'}->{value}  
                 =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=  
                     [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|  
                     ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {  
               $charset = defined $1 ? $1 : defined $2 ? $2 : $3;  
             } ## TODO: And if supported  
           }  
           ## TODO: Change the encoding  
         }  
   
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'title') {  
         !!!parse-error (type => 'in body:title');  
         ## NOTE: This is an "as if in head" code clone  
         $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {  
           if (defined $self->{head_element}) {  
             $self->{head_element}->append_child ($_[0]);  
           } else {  
             $insert->($_[0]);  
           }  
         });  
         return;  
       } elsif ($token->{tag_name} eq 'body') {  
         !!!parse-error (type => 'in body:body');  
                 
         if (@{$self->{open_elements}} == 1 or  
             $self->{open_elements}->[1]->[1] ne 'body') {  
           ## Ignore the token  
         } else {  
           my $body_el = $self->{open_elements}->[1]->[0];  
           for my $attr_name (keys %{$token->{attributes}}) {  
             unless ($body_el->has_attribute_ns (undef, $attr_name)) {  
               $body_el->set_attribute_ns  
                 (undef, [undef, $attr_name],  
                  $token->{attributes}->{$attr_name}->{value});  
             }  
           }  
         }  
         !!!next-token;  
         return;  
       } elsif ({  
                 address => 1, blockquote => 1, center => 1, dir => 1,  
                 div => 1, dl => 1, fieldset => 1, listing => 1,  
                 menu => 1, ol => 1, p => 1, ul => 1,  
                 pre => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         if ($token->{tag_name} eq 'pre') {  
           !!!next-token;  
           if ($token->{type} eq 'character') {  
             $token->{data} =~ s/^\x0A//;  
             unless (length $token->{data}) {  
               !!!next-token;  
             }  
           }  
         } else {  
           !!!next-token;  
         }  
         return;  
       } elsif ($token->{tag_name} eq 'form') {  
         if (defined $self->{form_element}) {  
           !!!parse-error (type => 'in form:form');  
           ## Ignore the token  
           !!!next-token;  
           return;  
         } else {  
           ## has a p element in scope  
           INSCOPE: for (reverse @{$self->{open_elements}}) {  
             if ($_->[1] eq 'p') {  
               !!!back-token;  
               $token = {type => 'end tag', tag_name => 'p'};  
               return;  
             } elsif ({  
                       table => 1, caption => 1, td => 1, th => 1,  
                       button => 1, marquee => 1, object => 1, html => 1,  
                      }->{$_->[1]}) {  
               last INSCOPE;  
             }  
           } # INSCOPE  
               
           !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           $self->{form_element} = $self->{open_elements}->[-1]->[0];  
           !!!next-token;  
           return;  
         }  
       } elsif ($token->{tag_name} eq 'li') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'li') {  
             if ($i != -1) {  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
             }  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             last LI;  
           }  
             
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {  
             if ($i != -1) {  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
             }  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             last LI;  
           }  
             
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'plaintext') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{content_model} = PLAINTEXT_CONTENT_MODEL;  
             
         !!!next-token;  
         return;  
       } elsif ({  
                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## NOTE: See <http://html5.org/tools/web-apps-tracker?from=925&to=926>  
         ## has an element in scope  
         #my $i;  
         #INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
         #  my $node = $self->{open_elements}->[$_];  
         #  if ({  
         #       h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
         #      }->{$node->[1]}) {  
         #    $i = $_;  
         #    last INSCOPE;  
         #  } elsif ({  
         #            table => 1, caption => 1, td => 1, th => 1,  
         #            button => 1, marquee => 1, object => 1, html => 1,  
         #           }->{$node->[1]}) {  
         #    last INSCOPE;  
         #  }  
         #} # INSCOPE  
         #    
         #if (defined $i) {  
         #  !!! parse-error (type => 'in hn:hn');  
         #  splice @{$self->{open_elements}}, $i;  
         #}  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'a') {  
         AFE: for my $i (reverse 0..$#$active_formatting_elements) {  
           my $node = $active_formatting_elements->[$i];  
           if ($node->[1] eq 'a') {  
             !!!parse-error (type => 'in a:a');  
               
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'a'};  
             $formatting_end_tag->($token->{tag_name});  
               
             AFE2: for (reverse 0..$#$active_formatting_elements) {  
               if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {  
                 splice @$active_formatting_elements, $_, 1;  
                 last AFE2;  
               }  
             } # AFE2  
             OE: for (reverse 0..$#{$self->{open_elements}}) {  
               if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {  
                 splice @{$self->{open_elements}}, $_, 1;  
                 last OE;  
               }  
             } # OE  
             last AFE;  
           } elsif ($node->[0] eq '#marker') {  
             last AFE;  
           }  
         } # AFE  
             
         $reconstruct_active_formatting_elements->($insert_to_current);  
   
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, $self->{open_elements}->[-1];  
   
         !!!next-token;  
         return;  
       } elsif ({  
                 b => 1, big => 1, em => 1, font => 1, i => 1,  
                 s => 1, small => 1, strile => 1,  
                 strong => 1, tt => 1, u => 1,  
                }->{$token->{tag_name}}) {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, $self->{open_elements}->[-1];  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'nobr') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
   
         ## has a |nobr| element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'nobr') {  
             !!!parse-error (type => 'not closed:nobr');  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'nobr'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, $self->{open_elements}->[-1];  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'button') {  
         ## has a button element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'button') {  
             !!!parse-error (type => 'in button:button');  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'button'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         $reconstruct_active_formatting_elements->($insert_to_current);  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, ['#marker', ''];  
   
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'marquee' or  
                $token->{tag_name} eq 'object') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, ['#marker', ''];  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'xmp') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         return;  
       } elsif ($token->{tag_name} eq 'table') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{insertion_mode} = 'in table';  
             
         !!!next-token;  
         return;  
       } elsif ({  
                 area => 1, basefont => 1, bgsound => 1, br => 1,  
                 embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,  
                 image => 1,  
                }->{$token->{tag_name}}) {  
         if ($token->{tag_name} eq 'image') {  
           !!!parse-error (type => 'image');  
           $token->{tag_name} = 'img';  
         }  
   
         ## NOTE: There is an "as if <br>" code clone.  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'hr') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
             
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'input') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         ## TODO: associate with $self->{form_element} if defined  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'isindex') {  
         !!!parse-error (type => 'isindex');  
           
         if (defined $self->{form_element}) {  
           ## Ignore the token  
           !!!next-token;  
           return;  
         } else {  
           my $at = $token->{attributes};  
           my $form_attrs;  
           $form_attrs->{action} = $at->{action} if $at->{action};  
           my $prompt_attr = $at->{prompt};  
           $at->{name} = {name => 'name', value => 'isindex'};  
           delete $at->{action};  
           delete $at->{prompt};  
           my @tokens = (  
                         {type => 'start tag', tag_name => 'form',  
                          attributes => $form_attrs},  
                         {type => 'start tag', tag_name => 'hr'},  
                         {type => 'start tag', tag_name => 'p'},  
                         {type => 'start tag', tag_name => 'label'},  
                        );  
           if ($prompt_attr) {  
             push @tokens, {type => 'character', data => $prompt_attr->{value}};  
           } else {  
             push @tokens, {type => 'character',  
                            data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD  
             ## TODO: make this configurable  
           }  
           push @tokens,  
                         {type => 'start tag', tag_name => 'input', attributes => $at},  
                         #{type => 'character', data => ''}, # SHOULD  
                         {type => 'end tag', tag_name => 'label'},  
                         {type => 'end tag', tag_name => 'p'},  
                         {type => 'start tag', tag_name => 'hr'},  
                         {type => 'end tag', tag_name => 'form'};  
           $token = shift @tokens;  
           !!!back-token (@tokens);  
           return;  
         }  
       } elsif ($token->{tag_name} eq 'textarea') {  
         my $tag_name = $token->{tag_name};  
         my $el;  
         !!!create-element ($el, $token->{tag_name}, $token->{attributes});  
           
         ## TODO: $self->{form_element} if defined  
         $self->{content_model} = RCDATA_CONTENT_MODEL;  
         delete $self->{escape}; # MUST  
           
         $insert->($el);  
           
         my $text = '';  
         !!!next-token;  
         if ($token->{type} eq 'character') {  
           $token->{data} =~ s/^\x0A//;  
           unless (length $token->{data}) {  
             !!!next-token;  
           }  
         }  
         while ($token->{type} eq 'character') {  
           $text .= $token->{data};  
           !!!next-token;  
         }  
         if (length $text) {  
           $el->manakai_append_text ($text);  
         }  
           
         $self->{content_model} = PCDATA_CONTENT_MODEL;  
           
         if ($token->{type} eq 'end tag' and  
             $token->{tag_name} eq $tag_name) {  
           ## Ignore the token  
         } else {  
           !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
         }  
         !!!next-token;  
         return;  
       } elsif ({  
                 iframe => 1,  
                 noembed => 1,  
                 noframes => 1,  
                 noscript => 0, ## TODO: 1 if scripting is enabled  
                }->{$token->{tag_name}}) {  
         ## NOTE: There are two "as if in body" code clones.  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         return;  
       } elsif ($token->{tag_name} eq 'select') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           
         $self->{insertion_mode} = 'in select';  
         !!!next-token;  
         return;  
       } elsif ({  
                 caption => 1, col => 1, colgroup => 1, frame => 1,  
                 frameset => 1, head => 1, option => 1, optgroup => 1,  
                 tbody => 1, td => 1, tfoot => 1, th => 1,  
                 thead => 1, tr => 1,  
                }->{$token->{tag_name}}) {  
         !!!parse-error (type => 'in body:'.$token->{tag_name});  
         ## Ignore the token  
         !!!next-token;  
         return;  
           
         ## ISSUE: An issue on HTML5 new elements in the spec.  
       } else {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           
         !!!next-token;  
         return;  
       }  
     } elsif ($token->{type} eq 'end tag') {  
       if ($token->{tag_name} eq 'body') {  
         if (@{$self->{open_elements}} > 1 and  
             $self->{open_elements}->[1]->[1] eq 'body') {  
           for (@{$self->{open_elements}}) {  
             unless ({  
                        dd => 1, dt => 1, li => 1, p => 1, td => 1,  
                        th => 1, tr => 1, body => 1, html => 1,  
                      tbody => 1, tfoot => 1, thead => 1,  
                     }->{$_->[1]}) {  
               !!!parse-error (type => 'not closed:'.$_->[1]);  
             }  
           }  
   
           $self->{insertion_mode} = 'after body';  
           !!!next-token;  
           return;  
         } else {  
           !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           ## Ignore the token  
           !!!next-token;  
           return;  
         }  
       } elsif ($token->{tag_name} eq 'html') {  
         if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {  
           ## ISSUE: There is an issue in the spec.  
           if ($self->{open_elements}->[-1]->[1] ne 'body') {  
             !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);  
           }  
           $self->{insertion_mode} = 'after body';  
           ## reprocess  
           return;  
         } else {  
           !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           ## Ignore the token  
           !!!next-token;  
           return;  
         }  
       } elsif ({  
                 address => 1, blockquote => 1, center => 1, dir => 1,  
                 div => 1, dl => 1, fieldset => 1, listing => 1,  
                 menu => 1, ol => 1, pre => 1, ul => 1,  
                 p => 1,  
                 dd => 1, dt => 1, li => 1,  
                 button => 1, marquee => 1, object => 1,  
                }->{$token->{tag_name}}) {  
         ## has an element in scope  
         my $i;  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq $token->{tag_name}) {  
             ## generate implied end tags  
             if ({  
                  dd => ($token->{tag_name} ne 'dd'),  
                  dt => ($token->{tag_name} ne 'dt'),  
                  li => ($token->{tag_name} ne 'li'),  
                  p => ($token->{tag_name} ne 'p'),  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
             $i = $_;  
             last INSCOPE unless $token->{tag_name} eq 'p';  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
           
         if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {  
           if (defined $i) {  
             !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
           } else {  
             !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           }  
         }  
           
         if (defined $i) {  
           splice @{$self->{open_elements}}, $i;  
         } elsif ($token->{tag_name} eq 'p') {  
           ## As if <p>, then reprocess the current token  
           my $el;  
           !!!create-element ($el, 'p');  
           $insert->($el);  
         }  
         $clear_up_to_marker->()  
           if {  
             button => 1, marquee => 1, object => 1,  
           }->{$token->{tag_name}};  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'form') {  
         ## has an element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq $token->{tag_name}) {  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
             last INSCOPE;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
           
         if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {  
           pop @{$self->{open_elements}};  
         } else {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         }  
   
         undef $self->{form_element};  
         !!!next-token;  
         return;  
       } elsif ({  
                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
                }->{$token->{tag_name}}) {  
         ## has an element in scope  
         my $i;  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ({  
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
             $i = $_;  
             last INSCOPE;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
           
         if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         }  
           
         splice @{$self->{open_elements}}, $i if defined $i;  
         !!!next-token;  
         return;  
       } elsif ({  
                 a => 1,  
                 b => 1, big => 1, em => 1, font => 1, i => 1,  
                 nobr => 1, s => 1, small => 1, strile => 1,  
                 strong => 1, tt => 1, u => 1,  
                }->{$token->{tag_name}}) {  
         $formatting_end_tag->($token->{tag_name});  
         return;  
       } elsif ($token->{tag_name} eq 'br') {  
         !!!parse-error (type => 'unmatched end tag:br');  
   
         ## As if <br>  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         my $el;  
         !!!create-element ($el, 'br');  
         $insert->($el);  
           
         ## Ignore the token.  
         !!!next-token;  
         return;  
       } elsif ({  
                 caption => 1, col => 1, colgroup => 1, frame => 1,  
                 frameset => 1, head => 1, option => 1, optgroup => 1,  
                 tbody => 1, td => 1, tfoot => 1, th => 1,  
                 thead => 1, tr => 1,  
                 area => 1, basefont => 1, bgsound => 1,  
                 embed => 1, hr => 1, iframe => 1, image => 1,  
                 img => 1, input => 1, isindex => 1, noembed => 1,  
                 noframes => 1, param => 1, select => 1, spacer => 1,  
                 table => 1, textarea => 1, wbr => 1,  
                 noscript => 0, ## TODO: if scripting is enabled  
                }->{$token->{tag_name}}) {  
         !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
         ## Ignore the token  
         !!!next-token;  
         return;  
           
         ## ISSUE: Issue on HTML5 new elements in spec  
           
       } else {  
         ## Step 1  
         my $node_i = -1;  
         my $node = $self->{open_elements}->[$node_i];  
   
         ## Step 2  
         S2: {  
           if ($node->[1] eq $token->{tag_name}) {  
             ## Step 1  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
           
             ## Step 2  
             if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {  
               !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
             }  
               
             ## Step 3  
             splice @{$self->{open_elements}}, $node_i;  
   
             !!!next-token;  
             last S2;  
           } else {  
             ## Step 3  
             if (not $formatting_category->{$node->[1]} and  
                 #not $phrasing_category->{$node->[1]} and  
                 ($special_category->{$node->[1]} or  
                  $scoping_category->{$node->[1]})) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               last S2;  
             }  
           }  
             
           ## Step 4  
           $node_i--;  
           $node = $self->{open_elements}->[$node_i];  
             
           ## Step 5;  
           redo S2;  
         } # S2  
         return;  
       }  
     }  
   }; # $in_body  
2827    
2828    B: {    B: {
2829      if ($token->{type} eq 'DOCTYPE') {      if ($token->{type} == DOCTYPE_TOKEN) {
2830        !!!parse-error (type => 'DOCTYPE in the middle');        !!!parse-error (type => 'DOCTYPE in the middle');
2831        ## Ignore the token        ## Ignore the token
2832        ## Stay in the phase        ## Stay in the phase
2833        !!!next-token;        !!!next-token;
2834        redo B;        redo B;
2835      } elsif ($token->{type} eq 'end-of-file') {      } elsif ($token->{type} == END_OF_FILE_TOKEN) {
2836        if ($token->{insertion_mode} ne 'trailing end') {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
2837            #
2838          } else {
2839          ## Generate implied end tags          ## Generate implied end tags
2840          if ({          if ({
2841               dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,               dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,
2842               tbody => 1, tfoot=> 1, thead => 1,               tbody => 1, tfoot=> 1, thead => 1,
2843              }->{$self->{open_elements}->[-1]->[1]}) {              }->{$self->{open_elements}->[-1]->[1]}) {
2844            !!!back-token;            !!!back-token;
2845            $token = {type => 'end tag', tag_name => $self->{open_elements}->[-1]->[1]};            $token = {type => END_TAG_TOKEN, tag_name => $self->{open_elements}->[-1]->[1]};
2846            redo B;            redo B;
2847          }          }
2848                    
# Line 3375  sub _tree_construction_main ($) { Line 2860  sub _tree_construction_main ($) {
2860    
2861        ## Stop parsing        ## Stop parsing
2862        last B;        last B;
2863      } elsif ($token->{type} eq 'start tag' and      } elsif ($token->{type} == START_TAG_TOKEN and
2864               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
2865        if ($self->{insertion_mode} eq 'trailing end') {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
2866            ## Turn into the main phase
2867            !!!parse-error (type => 'after html:html');
2868            $self->{insertion_mode} = AFTER_BODY_IM;
2869          } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
2870          ## Turn into the main phase          ## Turn into the main phase
2871          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html');
2872          $self->{insertion_mode} = $previous_insertion_mode;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
2873        }        }
2874    
2875  ## ISSUE: "aa<html>" is not a parse error.  ## ISSUE: "aa<html>" is not a parse error.
# Line 3398  sub _tree_construction_main ($) { Line 2887  sub _tree_construction_main ($) {
2887        }        }
2888        !!!next-token;        !!!next-token;
2889        redo B;        redo B;
2890      } elsif ($token->{type} eq 'comment') {      } elsif ($token->{type} == COMMENT_TOKEN) {
2891        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
2892        if ($self->{insertion_mode} eq 'trailing end') {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
2893          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
2894        } elsif ($self->{insertion_mode} eq 'after body') {        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
2895          $self->{open_elements}->[0]->[0]->append_child ($comment);          $self->{open_elements}->[0]->[0]->append_child ($comment);
2896        } else {        } else {
2897          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
2898        }        }
2899        !!!next-token;        !!!next-token;
2900        redo B;        redo B;
2901      } elsif ($self->{insertion_mode} eq 'before head') {      } elsif ($self->{insertion_mode} & HEAD_IMS) {
2902            if ($token->{type} eq 'character') {        if ($token->{type} == CHARACTER_TOKEN) {
2903              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
2904                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
2905                unless (length $token->{data}) {            unless (length $token->{data}) {
2906                  !!!next-token;              !!!next-token;
2907                  redo B;              redo B;
2908                }            }
2909              }          }
2910              ## As if <head>  
2911              !!!create-element ($self->{head_element}, 'head');          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2912              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            ## As if <head>
2913              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            !!!create-element ($self->{head_element}, 'head');
2914              $self->{insertion_mode} = 'in head';            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2915              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2916    
2917              ## Reprocess in the "in head" insertion mode...
2918              pop @{$self->{open_elements}};
2919    
2920              ## Reprocess in the "after head" insertion mode...
2921            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2922              ## As if </noscript>
2923              pop @{$self->{open_elements}};
2924              !!!parse-error (type => 'in noscript:#character');
2925              
2926              ## Reprocess in the "in head" insertion mode...
2927              ## As if </head>
2928              pop @{$self->{open_elements}};
2929    
2930              ## Reprocess in the "after head" insertion mode...
2931            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2932              pop @{$self->{open_elements}};
2933    
2934              ## Reprocess in the "after head" insertion mode...
2935            }
2936    
2937                ## "after head" insertion mode
2938                ## As if <body>
2939                !!!insert-element ('body');
2940                $self->{insertion_mode} = IN_BODY_IM;
2941              ## reprocess              ## reprocess
2942              redo B;              redo B;
2943            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} == START_TAG_TOKEN) {
             my $attr = $token->{tag_name} eq 'head' ? $token->{attributes} : {};  
             !!!create-element ($self->{head_element}, 'head', $attr);  
             $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});  
             push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
             $self->{insertion_mode} = 'in head';  
2944              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
2945                !!!next-token;                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2946              } else {                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});
2947                ## reprocess                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2948              }                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];
2949              redo B;                  $self->{insertion_mode} = IN_HEAD_IM;
2950            } elsif ($token->{type} eq 'end tag') {                  !!!next-token;
2951              if ({                  redo B;
2952                   head => 1, body => 1, html => 1,                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2953                   p => 1, br => 1,                  #
2954                  }->{$token->{tag_name}}) {                } else {
2955                    !!!parse-error (type => 'in head:head'); # or in head noscript
2956                    ## Ignore the token
2957                    !!!next-token;
2958                    redo B;
2959                  }
2960                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2961                ## As if <head>                ## As if <head>
2962                !!!create-element ($self->{head_element}, 'head');                !!!create-element ($self->{head_element}, 'head');
2963                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2964                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
               $self->{insertion_mode} = 'in head';  
               ## reprocess  
               redo B;  
             } else {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token ## ISSUE: An issue in the spec.  
               !!!next-token;  
               redo B;  
             }  
           } else {  
             die "$0: $token->{type}: Unknown type";  
           }  
         } elsif ($self->{insertion_mode} eq 'in head' or  
                  $self->{insertion_mode} eq 'in head noscript' or  
                  $self->{insertion_mode} eq 'after head') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
               
             if ($self->{insertion_mode} eq 'in head noscript') {  
               ## As if </noscript>  
               pop @{$self->{open_elements}};  
               !!!parse-error (type => 'in noscript:#character');  
                 
               ## Reprocess in the "in head" insertion mode...  
               ## As if </head>  
               pop @{$self->{open_elements}};  
2965    
2966                ## Reprocess in the "after head" insertion mode...                $self->{insertion_mode} = IN_HEAD_IM;
2967              } elsif ($self->{insertion_mode} eq 'in head') {                ## Reprocess in the "in head" insertion mode...
               pop @{$self->{open_elements}};  
   
               ## Reprocess in the "after head" insertion mode...  
2968              }              }
2969    
             ## 'after head' insertion mode  
             ## As if <body>  
             !!!insert-element ('body');  
             $self->{insertion_mode} = 'in body';  
             ## reprocess  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
2970              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
2971                if ($self->{insertion_mode} eq 'in head noscript') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2972                  ## As if </noscript>                  ## As if </noscript>
2973                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
2974                  !!!parse-error (type => 'in noscript:base');                  !!!parse-error (type => 'in noscript:base');
2975                                
2976                  $self->{insertion_mode} = 'in head';                  $self->{insertion_mode} = IN_HEAD_IM;
2977                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
2978                }                }
2979    
2980                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
2981                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2982                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2983                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2984                }                }
2985                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
2986                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2987                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2988                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2989                !!!next-token;                !!!next-token;
2990                redo B;                redo B;
2991              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
2992                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
2993                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2994                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2995                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2996                }                }
2997                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
2998                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2999                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
3000                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3001                !!!next-token;                !!!next-token;
3002                redo B;                redo B;
3003              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
3004                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3005                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3006                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3007                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3008                }                }
3009                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
3010                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.
3011    
3012                unless ($self->{confident}) {                unless ($self->{confident}) {
                 my $charset;  
3013                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) { ## TODO: And if supported
3014                    $charset = $token->{attributes}->{charset}->{value};                    $self->{change_encoding}
3015                  }                        ->($self, $token->{attributes}->{charset}->{value});
3016                  if ($token->{attributes}->{'http-equiv'}) {                    
3017                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
3018                          ->set_user_data (manakai_has_reference =>
3019                                               $token->{attributes}->{charset}
3020                                                   ->{has_reference});
3021                    } elsif ($token->{attributes}->{content}) {
3022                    ## 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.
3023                    if ($token->{attributes}->{'http-equiv'}->{value}                    if ($token->{attributes}->{content}->{value}
3024                        =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
3025                              [\x09-\x0D\x20]*=
3026                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
3027                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
3028                      $charset = defined $1 ? $1 : defined $2 ? $2 : $3;                      $self->{change_encoding}
3029                    } ## TODO: And if supported                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);
3030                        $meta_el->[0]->get_attribute_node_ns (undef, 'content')
3031                            ->set_user_data (manakai_has_reference =>
3032                                                 $token->{attributes}->{content}
3033                                                       ->{has_reference});
3034                      }
3035                    }
3036                  } else {
3037                    if ($token->{attributes}->{charset}) {
3038                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
3039                          ->set_user_data (manakai_has_reference =>
3040                                               $token->{attributes}->{charset}
3041                                                   ->{has_reference});
3042                    }
3043                    if ($token->{attributes}->{content}) {
3044                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
3045                          ->set_user_data (manakai_has_reference =>
3046                                               $token->{attributes}->{content}
3047                                                   ->{has_reference});
3048                  }                  }
                 ## TODO: Change the encoding  
3049                }                }
3050    
               ## TODO: Extracting |charset| from |meta|.  
3051                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
3052                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3053                !!!next-token;                !!!next-token;
3054                redo B;                redo B;
3055              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
3056                if ($self->{insertion_mode} eq 'in head noscript') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3057                  ## As if </noscript>                  ## As if </noscript>
3058                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3059                  !!!parse-error (type => 'in noscript:title');                  !!!parse-error (type => 'in noscript:title');
3060                                
3061                  $self->{insertion_mode} = 'in head';                  $self->{insertion_mode} = IN_HEAD_IM;
3062                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3063                } elsif ($self->{insertion_mode} eq 'after head') {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3064                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3065                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3066                }                }
# Line 3576  sub _tree_construction_main ($) { Line 3071  sub _tree_construction_main ($) {
3071                $parse_rcdata->(RCDATA_CONTENT_MODEL,                $parse_rcdata->(RCDATA_CONTENT_MODEL,
3072                                sub { $parent->append_child ($_[0]) });                                sub { $parent->append_child ($_[0]) });
3073                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
3074                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3075                redo B;                redo B;
3076              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style') {
3077                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
3078                ## insertion mode 'in head')                ## insertion mode IN_HEAD_IM)
3079                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3080                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3081                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3082                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3083                }                }
3084                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
3085                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
3086                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3087                redo B;                redo B;
3088              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
3089                if ($self->{insertion_mode} eq 'in head') {                if ($self->{insertion_mode} == IN_HEAD_IM) {
3090                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
3091                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3092                  $self->{insertion_mode} = 'in head noscript';                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
3093                  !!!next-token;                  !!!next-token;
3094                  redo B;                  redo B;
3095                } elsif ($self->{insertion_mode} eq 'in head noscript') {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3096                  !!!parse-error (type => 'in noscript:noscript');                  !!!parse-error (type => 'in noscript:noscript');
3097                  ## Ignore the token                  ## Ignore the token
3098                  !!!next-token;                  !!!next-token;
# Line 3605  sub _tree_construction_main ($) { Line 3100  sub _tree_construction_main ($) {
3100                } else {                } else {
3101                  #                  #
3102                }                }
             } elsif ($token->{tag_name} eq 'head') {  
               if ($self->{insertion_mode} ne 'after head') {  
                 !!!parse-error (type => 'in head:head'); # or in head noscript  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               } else {  
                 #  
               }  
3103              } elsif ($token->{tag_name} eq 'script') {              } elsif ($token->{tag_name} eq 'script') {
3104                if ($self->{insertion_mode} eq 'in head noscript') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3105                  ## As if </noscript>                  ## As if </noscript>
3106                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3107                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:script');
3108                                
3109                  $self->{insertion_mode} = 'in head';                  $self->{insertion_mode} = IN_HEAD_IM;
3110                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3111                } elsif ($self->{insertion_mode} eq 'after head') {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3112                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3113                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3114                }                }
# Line 3630  sub _tree_construction_main ($) { Line 3116  sub _tree_construction_main ($) {
3116                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3117                $script_start_tag->($insert_to_current);                $script_start_tag->($insert_to_current);
3118                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
3119                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3120                redo B;                redo B;
3121              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
3122                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
3123                if ($self->{insertion_mode} eq 'in head noscript') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3124                  ## As if </noscript>                  ## As if </noscript>
3125                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3126                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});
# Line 3644  sub _tree_construction_main ($) { Line 3130  sub _tree_construction_main ($) {
3130                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3131                                    
3132                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
3133                } elsif ($self->{insertion_mode} eq 'in head') {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3134                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3135                                    
3136                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
# Line 3652  sub _tree_construction_main ($) { Line 3138  sub _tree_construction_main ($) {
3138    
3139                ## "after head" insertion mode                ## "after head" insertion mode
3140                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
3141                $self->{insertion_mode} = 'in '.$token->{tag_name};                if ($token->{tag_name} eq 'body') {
3142                    $self->{insertion_mode} = IN_BODY_IM;
3143                  } elsif ($token->{tag_name} eq 'frameset') {
3144                    $self->{insertion_mode} = IN_FRAMESET_IM;
3145                  } else {
3146                    die "$0: tag name: $self->{tag_name}";
3147                  }
3148                !!!next-token;                !!!next-token;
3149                redo B;                redo B;
3150              } else {              } else {
3151                #                #
3152              }              }
3153    
3154              if ($self->{insertion_mode} eq 'in head noscript') {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3155                ## As if </noscript>                ## As if </noscript>
3156                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3157                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
# Line 3669  sub _tree_construction_main ($) { Line 3161  sub _tree_construction_main ($) {
3161                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3162    
3163                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
3164              } elsif ($self->{insertion_mode} eq 'in head') {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3165                ## As if </head>                ## As if </head>
3166                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3167    
# Line 3679  sub _tree_construction_main ($) { Line 3171  sub _tree_construction_main ($) {
3171              ## "after head" insertion mode              ## "after head" insertion mode
3172              ## As if <body>              ## As if <body>
3173              !!!insert-element ('body');              !!!insert-element ('body');
3174              $self->{insertion_mode} = 'in body';              $self->{insertion_mode} = IN_BODY_IM;
3175              ## reprocess              ## reprocess
3176              redo B;              redo B;
3177            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
3178              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
3179                if ($self->{insertion_mode} eq 'in head noscript') {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3180                    ## As if <head>
3181                    !!!create-element ($self->{head_element}, 'head');
3182                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3183                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3184    
3185                    ## Reprocess in the "in head" insertion mode...
3186                    pop @{$self->{open_elements}};
3187                    $self->{insertion_mode} = AFTER_HEAD_IM;
3188                    !!!next-token;
3189                    redo B;
3190                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3191                  ## As if </noscript>                  ## As if </noscript>
3192                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3193                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:script');
3194                                    
                 $self->{insertion_mode} = 'in head';  
3195                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
               }  
                 
               if ($self->{insertion_mode} eq 'in head') {  
3196                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3197                  $self->{insertion_mode} = 'after head';                  $self->{insertion_mode} = AFTER_HEAD_IM;
3198                    !!!next-token;
3199                    redo B;
3200                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3201                    pop @{$self->{open_elements}};
3202                    $self->{insertion_mode} = AFTER_HEAD_IM;
3203                  !!!next-token;                  !!!next-token;
3204                  redo B;                  redo B;
3205                } else {                } else {
3206                  #                  #
3207                }                }
3208              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
3209                if ($self->{insertion_mode} eq 'in head noscript') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3210                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3211                  $self->{insertion_mode} = 'in head';                  $self->{insertion_mode} = IN_HEAD_IM;
3212                    !!!next-token;
3213                    redo B;
3214                  } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3215                    !!!parse-error (type => 'unmatched end tag:noscript');
3216                    ## Ignore the token ## ISSUE: An issue in the spec.
3217                  !!!next-token;                  !!!next-token;
3218                  redo B;                  redo B;
3219                } else {                } else {
# Line 3713  sub _tree_construction_main ($) { Line 3222  sub _tree_construction_main ($) {
3222              } elsif ({              } elsif ({
3223                        body => 1, html => 1,                        body => 1, html => 1,
3224                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3225                if ($self->{insertion_mode} eq 'in head noscript') {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3226                    ## As if <head>
3227                    !!!create-element ($self->{head_element}, 'head');
3228                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3229                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3230    
3231                    $self->{insertion_mode} = IN_HEAD_IM;
3232                    ## Reprocess in the "in head" insertion mode...
3233                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3234                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3235                  ## Ignore the token                  ## Ignore the token
3236                  !!!next-token;                  !!!next-token;
3237                  redo B;                  redo B;
               } else { # 'in head', 'after head'  
                 #  
3238                }                }
3239                  
3240                  #
3241              } elsif ({              } elsif ({
3242                        p => 1, br => 1,                        p => 1, br => 1,
3243                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3244                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3245                    ## As if <head>
3246                    !!!create-element ($self->{head_element}, 'head');
3247                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3248                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3249    
3250                    $self->{insertion_mode} = IN_HEAD_IM;
3251                    ## Reprocess in the "in head" insertion mode...
3252                  }
3253    
3254                #                #
3255              } else {              } else {
3256                if ($self->{insertion_mode} ne 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3257                    #
3258                  } else {
3259                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3260                  ## Ignore the token                  ## Ignore the token
3261                  !!!next-token;                  !!!next-token;
3262                  redo B;                  redo B;
               } else {  
                 #  
3263                }                }
3264              }              }
3265    
3266              if ($self->{insertion_mode} eq 'in head noscript') {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3267                ## As if </noscript>                ## As if </noscript>
3268                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3269                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
# Line 3746  sub _tree_construction_main ($) { Line 3273  sub _tree_construction_main ($) {
3273                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3274    
3275                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
3276              } elsif ($self->{insertion_mode} eq 'in head') {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3277                ## As if </head>                ## As if </head>
3278                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3279    
3280                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
3281                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3282                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3283                  ## Ignore the token ## ISSUE: An issue in the spec.
3284                  !!!next-token;
3285                  redo B;
3286              }              }
3287    
3288              ## "after head" insertion mode              ## "after head" insertion mode
3289              ## As if <body>              ## As if <body>
3290              !!!insert-element ('body');              !!!insert-element ('body');
3291              $self->{insertion_mode} = 'in body';              $self->{insertion_mode} = IN_BODY_IM;
3292              ## reprocess              ## reprocess
3293              redo B;              redo B;
3294            } else {            } else {
# Line 3764  sub _tree_construction_main ($) { Line 3296  sub _tree_construction_main ($) {
3296            }            }
3297    
3298            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
3299          } elsif ($self->{insertion_mode} eq 'in body' or      } elsif ($self->{insertion_mode} & BODY_IMS) {
3300                   $self->{insertion_mode} eq 'in cell' or            if ($token->{type} == CHARACTER_TOKEN) {
                  $self->{insertion_mode} eq 'in caption') {  
           if ($token->{type} eq 'character') {  
3301              ## NOTE: There is a code clone of "character in body".              ## NOTE: There is a code clone of "character in body".
3302              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
3303                            
# Line 3775  sub _tree_construction_main ($) { Line 3305  sub _tree_construction_main ($) {
3305    
3306              !!!next-token;              !!!next-token;
3307              redo B;              redo B;
3308            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} == START_TAG_TOKEN) {
3309              if ({              if ({
3310                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
3311                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
3312                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
3313                if ($self->{insertion_mode} eq 'in cell') {                if ($self->{insertion_mode} == IN_CELL_IM) {
3314                  ## have an element in table scope                  ## have an element in table scope
3315                  my $tn;                  my $tn;
3316                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 3803  sub _tree_construction_main ($) { Line 3333  sub _tree_construction_main ($) {
3333                                    
3334                  ## Close the cell                  ## Close the cell
3335                  !!!back-token; # <?>                  !!!back-token; # <?>
3336                  $token = {type => 'end tag', tag_name => $tn};                  $token = {type => END_TAG_TOKEN, tag_name => $tn};
3337                  redo B;                  redo B;
3338                } elsif ($self->{insertion_mode} eq 'in caption') {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3339                  !!!parse-error (type => 'not closed:caption');                  !!!parse-error (type => 'not closed:caption');
3340                                    
3341                  ## As if </caption>                  ## As if </caption>
# Line 3836  sub _tree_construction_main ($) { Line 3366  sub _tree_construction_main ($) {
3366                       tbody => 1, tfoot=> 1, thead => 1,                       tbody => 1, tfoot=> 1, thead => 1,
3367                      }->{$self->{open_elements}->[-1]->[1]}) {                      }->{$self->{open_elements}->[-1]->[1]}) {
3368                    !!!back-token; # <?>                    !!!back-token; # <?>
3369                    $token = {type => 'end tag', tag_name => 'caption'};                    $token = {type => END_TAG_TOKEN, tag_name => 'caption'};
3370                    !!!back-token;                    !!!back-token;
3371                    $token = {type => 'end tag',                    $token = {type => END_TAG_TOKEN,
3372                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3373                    redo B;                    redo B;
3374                  }                  }
# Line 3851  sub _tree_construction_main ($) { Line 3381  sub _tree_construction_main ($) {
3381                                    
3382                  $clear_up_to_marker->();                  $clear_up_to_marker->();
3383                                    
3384                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
3385                                    
3386                  ## reprocess                  ## reprocess
3387                  redo B;                  redo B;
# Line 3861  sub _tree_construction_main ($) { Line 3391  sub _tree_construction_main ($) {
3391              } else {              } else {
3392                #                #
3393              }              }
3394            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
3395              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
3396                if ($self->{insertion_mode} eq 'in cell') {                if ($self->{insertion_mode} == IN_CELL_IM) {
3397                  ## have an element in table scope                  ## have an element in table scope
3398                  my $i;                  my $i;
3399                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 3893  sub _tree_construction_main ($) { Line 3423  sub _tree_construction_main ($) {
3423                       tbody => 1, tfoot=> 1, thead => 1,                       tbody => 1, tfoot=> 1, thead => 1,
3424                      }->{$self->{open_elements}->[-1]->[1]}) {                      }->{$self->{open_elements}->[-1]->[1]}) {
3425                    !!!back-token;                    !!!back-token;
3426                    $token = {type => 'end tag',                    $token = {type => END_TAG_TOKEN,
3427                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3428                    redo B;                    redo B;
3429                  }                  }
# Line 3906  sub _tree_construction_main ($) { Line 3436  sub _tree_construction_main ($) {
3436                                    
3437                  $clear_up_to_marker->();                  $clear_up_to_marker->();
3438                                    
3439                  $self->{insertion_mode} = 'in row';                  $self->{insertion_mode} = IN_ROW_IM;
3440                                    
3441                  !!!next-token;                  !!!next-token;
3442                  redo B;                  redo B;
3443                } elsif ($self->{insertion_mode} eq 'in caption') {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3444                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3445                  ## Ignore the token                  ## Ignore the token
3446                  !!!next-token;                  !!!next-token;
# Line 3919  sub _tree_construction_main ($) { Line 3449  sub _tree_construction_main ($) {
3449                  #                  #
3450                }                }
3451              } elsif ($token->{tag_name} eq 'caption') {              } elsif ($token->{tag_name} eq 'caption') {
3452                if ($self->{insertion_mode} eq 'in caption') {                if ($self->{insertion_mode} == IN_CAPTION_IM) {
3453                  ## have a table element in table scope                  ## have a table element in table scope
3454                  my $i;                  my $i;
3455                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 3947  sub _tree_construction_main ($) { Line 3477  sub _tree_construction_main ($) {
3477                       tbody => 1, tfoot=> 1, thead => 1,                       tbody => 1, tfoot=> 1, thead => 1,
3478                      }->{$self->{open_elements}->[-1]->[1]}) {                      }->{$self->{open_elements}->[-1]->[1]}) {
3479                    !!!back-token;                    !!!back-token;
3480                    $token = {type => 'end tag',                    $token = {type => END_TAG_TOKEN,
3481                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3482                    redo B;                    redo B;
3483                  }                  }
# Line 3960  sub _tree_construction_main ($) { Line 3490  sub _tree_construction_main ($) {
3490                                    
3491                  $clear_up_to_marker->();                  $clear_up_to_marker->();
3492                                    
3493                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
3494                                    
3495                  !!!next-token;                  !!!next-token;
3496                  redo B;                  redo B;
3497                } elsif ($self->{insertion_mode} eq 'in cell') {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
3498                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3499                  ## Ignore the token                  ## Ignore the token
3500                  !!!next-token;                  !!!next-token;
# Line 3976  sub _tree_construction_main ($) { Line 3506  sub _tree_construction_main ($) {
3506                        table => 1, tbody => 1, tfoot => 1,                        table => 1, tbody => 1, tfoot => 1,
3507                        thead => 1, tr => 1,                        thead => 1, tr => 1,
3508                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
3509                       $self->{insertion_mode} eq 'in cell') {                       $self->{insertion_mode} == IN_CELL_IM) {
3510                ## have an element in table scope                ## have an element in table scope
3511                my $i;                my $i;
3512                my $tn;                my $tn;
# Line 4004  sub _tree_construction_main ($) { Line 3534  sub _tree_construction_main ($) {
3534    
3535                ## Close the cell                ## Close the cell
3536                !!!back-token; # </?>                !!!back-token; # </?>
3537                $token = {type => 'end tag', tag_name => $tn};                $token = {type => END_TAG_TOKEN, tag_name => $tn};
3538                redo B;                redo B;
3539              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
3540                       $self->{insertion_mode} eq 'in caption') {                       $self->{insertion_mode} == IN_CAPTION_IM) {
3541                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption');
3542    
3543                ## As if </caption>                ## As if </caption>
# Line 4038  sub _tree_construction_main ($) { Line 3568  sub _tree_construction_main ($) {
3568                     tbody => 1, tfoot=> 1, thead => 1,                     tbody => 1, tfoot=> 1, thead => 1,
3569                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
3570                  !!!back-token; # </table>                  !!!back-token; # </table>
3571                  $token = {type => 'end tag', tag_name => 'caption'};                  $token = {type => END_TAG_TOKEN, tag_name => 'caption'};
3572                  !!!back-token;                  !!!back-token;
3573                  $token = {type => 'end tag',                  $token = {type => END_TAG_TOKEN,
3574                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3575                  redo B;                  redo B;
3576                }                }
# Line 4053  sub _tree_construction_main ($) { Line 3583  sub _tree_construction_main ($) {
3583    
3584                $clear_up_to_marker->();                $clear_up_to_marker->();
3585    
3586                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
3587    
3588                ## reprocess                ## reprocess
3589                redo B;                redo B;
3590              } elsif ({              } elsif ({
3591                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
3592                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3593                if ($self->{insertion_mode} eq 'in cell' or                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
                   $self->{insertion_mode} eq 'in caption') {  
3594                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3595                  ## Ignore the token                  ## Ignore the token
3596                  !!!next-token;                  !!!next-token;
# Line 4073  sub _tree_construction_main ($) { Line 3602  sub _tree_construction_main ($) {
3602                        tbody => 1, tfoot => 1,                        tbody => 1, tfoot => 1,
3603                        thead => 1, tr => 1,                        thead => 1, tr => 1,
3604                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
3605                       $self->{insertion_mode} eq 'in caption') {                       $self->{insertion_mode} == IN_CAPTION_IM) {
3606                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3607                ## Ignore the token                ## Ignore the token
3608                !!!next-token;                !!!next-token;
# Line 4081  sub _tree_construction_main ($) { Line 3610  sub _tree_construction_main ($) {
3610              } else {              } else {
3611                #                #
3612              }              }
3613            } else {        } else {
3614              #          die "$0: $token->{type}: Unknown token type";
3615            }        }
3616              
3617            $in_body->($insert_to_current);        $insert = $insert_to_current;
3618            redo B;        #
3619          } elsif ($self->{insertion_mode} eq 'in row' or      } elsif ($self->{insertion_mode} & TABLE_IMS) {
3620                   $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.  
3621              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3622                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3623                                
# Line 4148  sub _tree_construction_main ($) { Line 3674  sub _tree_construction_main ($) {
3674                            
3675              !!!next-token;              !!!next-token;
3676              redo B;              redo B;
3677            } elsif ($token->{type} eq 'start tag') {        } elsif ($token->{type} == START_TAG_TOKEN) {
3678              if ({              if ({
3679                   tr => ($self->{insertion_mode} ne 'in row'),                   tr => ($self->{insertion_mode} != IN_ROW_IM),
3680                   th => 1, td => 1,                   th => 1, td => 1,
3681                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
3682                if ($self->{insertion_mode} eq 'in table') {                if ($self->{insertion_mode} == IN_TABLE_IM) {
3683                  ## Clear back to table context                  ## Clear back to table context
3684                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while ($self->{open_elements}->[-1]->[1] ne 'table' and
3685                         $self->{open_elements}->[-1]->[1] ne 'html') {                         $self->{open_elements}->[-1]->[1] ne 'html') {
# Line 4162  sub _tree_construction_main ($) { Line 3688  sub _tree_construction_main ($) {
3688                  }                  }
3689                                    
3690                  !!!insert-element ('tbody');                  !!!insert-element ('tbody');
3691                  $self->{insertion_mode} = 'in table body';                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
3692                  ## reprocess in the "in table body" insertion mode...                  ## reprocess in the "in table body" insertion mode...
3693                }                }
3694    
3695                if ($self->{insertion_mode} eq 'in table body') {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3696                  unless ($token->{tag_name} eq 'tr') {                  unless ($token->{tag_name} eq 'tr') {
3697                    !!!parse-error (type => 'missing start tag:tr');                    !!!parse-error (type => 'missing start tag:tr');
3698                  }                  }
# Line 4179  sub _tree_construction_main ($) { Line 3705  sub _tree_construction_main ($) {
3705                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
3706                  }                  }
3707                                    
3708                  $self->{insertion_mode} = 'in row';                  $self->{insertion_mode} = IN_ROW_IM;
3709                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
3710                    !!!insert-element ($token->{tag_name}, $token->{attributes});                    !!!insert-element ($token->{tag_name}, $token->{attributes});
3711                    !!!next-token;                    !!!next-token;
# Line 4199  sub _tree_construction_main ($) { Line 3725  sub _tree_construction_main ($) {
3725                }                }
3726                                
3727                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
3728                $self->{insertion_mode} = 'in cell';                $self->{insertion_mode} = IN_CELL_IM;
3729    
3730                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
3731                                
# Line 4208  sub _tree_construction_main ($) { Line 3734  sub _tree_construction_main ($) {
3734              } elsif ({              } elsif ({
3735                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
3736                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
3737                        tr => 1, # $self->{insertion_mode} eq 'in row'                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
3738                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3739                if ($self->{insertion_mode} eq 'in row') {                if ($self->{insertion_mode} == IN_ROW_IM) {
3740                  ## As if </tr>                  ## As if </tr>
3741                  ## have an element in table scope                  ## have an element in table scope
3742                  my $i;                  my $i;
# Line 4241  sub _tree_construction_main ($) { Line 3767  sub _tree_construction_main ($) {
3767                  }                  }
3768                                    
3769                  pop @{$self->{open_elements}}; # tr                  pop @{$self->{open_elements}}; # tr
3770                  $self->{insertion_mode} = 'in table body';                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
3771                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
3772                    ## reprocess                    ## reprocess
3773                    redo B;                    redo B;
# Line 4250  sub _tree_construction_main ($) { Line 3776  sub _tree_construction_main ($) {
3776                  }                  }
3777                }                }
3778    
3779                if ($self->{insertion_mode} eq 'in table body') {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3780                  ## have an element in table scope                  ## have an element in table scope
3781                  my $i;                  my $i;
3782                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 4289  sub _tree_construction_main ($) { Line 3815  sub _tree_construction_main ($) {
3815                  ## nop by definition                  ## nop by definition
3816                                    
3817                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3818                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
3819                  ## reprocess in "in table" insertion mode...                  ## reprocess in "in table" insertion mode...
3820                }                }
3821    
# Line 4302  sub _tree_construction_main ($) { Line 3828  sub _tree_construction_main ($) {
3828                  }                  }
3829                                    
3830                  !!!insert-element ('colgroup');                  !!!insert-element ('colgroup');
3831                  $self->{insertion_mode} = 'in column group';                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
3832                  ## reprocess                  ## reprocess
3833                  redo B;                  redo B;
3834                } elsif ({                } elsif ({
# Line 4322  sub _tree_construction_main ($) { Line 3848  sub _tree_construction_main ($) {
3848                                    
3849                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3850                  $self->{insertion_mode} = {                  $self->{insertion_mode} = {
3851                                             caption => 'in caption',                                             caption => IN_CAPTION_IM,
3852                                             colgroup => 'in column group',                                             colgroup => IN_COLUMN_GROUP_IM,
3853                                             tbody => 'in table body',                                             tbody => IN_TABLE_BODY_IM,
3854                                             tfoot => 'in table body',                                             tfoot => IN_TABLE_BODY_IM,
3855                                             thead => 'in table body',                                             thead => IN_TABLE_BODY_IM,
3856                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
3857                  !!!next-token;                  !!!next-token;
3858                  redo B;                  redo B;
# Line 4334  sub _tree_construction_main ($) { Line 3860  sub _tree_construction_main ($) {
3860                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
3861                }                }
3862              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
               ## NOTE: There are code clones for this "table in table"  
3863                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3864    
3865                ## As if </table>                ## As if </table>
# Line 4365  sub _tree_construction_main ($) { Line 3890  sub _tree_construction_main ($) {
3890                     tbody => 1, tfoot=> 1, thead => 1,                     tbody => 1, tfoot=> 1, thead => 1,
3891                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
3892                  !!!back-token; # <table>                  !!!back-token; # <table>
3893                  $token = {type => 'end tag', tag_name => 'table'};                  $token = {type => END_TAG_TOKEN, tag_name => 'table'};
3894                  !!!back-token;                  !!!back-token;
3895                  $token = {type => 'end tag',                  $token = {type => END_TAG_TOKEN,
3896                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3897                  redo B;                  redo B;
3898                }                }
# Line 4382  sub _tree_construction_main ($) { Line 3907  sub _tree_construction_main ($) {
3907    
3908                ## reprocess                ## reprocess
3909                redo B;                redo B;
3910              } else {          } else {
3911                #            !!!parse-error (type => 'in table:'.$token->{tag_name});
3912              }  
3913            } elsif ($token->{type} eq 'end tag') {            $insert = $insert_to_foster;
3914              #
3915            }
3916          } elsif ($token->{type} == END_TAG_TOKEN) {
3917              if ($token->{tag_name} eq 'tr' and              if ($token->{tag_name} eq 'tr' and
3918                  $self->{insertion_mode} eq 'in row') {                  $self->{insertion_mode} == IN_ROW_IM) {
3919                ## have an element in table scope                ## have an element in table scope
3920                my $i;                my $i;
3921                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 4417  sub _tree_construction_main ($) { Line 3945  sub _tree_construction_main ($) {
3945                }                }
3946    
3947                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
3948                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_BODY_IM;
3949                !!!next-token;                !!!next-token;
3950                redo B;                redo B;
3951              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
3952                if ($self->{insertion_mode} eq 'in row') {                if ($self->{insertion_mode} == IN_ROW_IM) {
3953                  ## As if </tr>                  ## As if </tr>
3954                  ## have an element in table scope                  ## have an element in table scope
3955                  my $i;                  my $i;
# Line 4452  sub _tree_construction_main ($) { Line 3980  sub _tree_construction_main ($) {
3980                  }                  }
3981                                    
3982                  pop @{$self->{open_elements}}; # tr                  pop @{$self->{open_elements}}; # tr
3983                  $self->{insertion_mode} = 'in table body';                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
3984                  ## reprocess in the "in table body" insertion mode...                  ## reprocess in the "in table body" insertion mode...
3985                }                }
3986    
3987                if ($self->{insertion_mode} eq 'in table body') {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3988                  ## have an element in table scope                  ## have an element in table scope
3989                  my $i;                  my $i;
3990                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 4495  sub _tree_construction_main ($) { Line 4023  sub _tree_construction_main ($) {
4023                  ## nop by definition                  ## nop by definition
4024                                    
4025                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4026                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
4027                  ## reprocess in the "in table" insertion mode...                  ## reprocess in the "in table" insertion mode...
4028                }                }
4029    
# Line 4526  sub _tree_construction_main ($) { Line 4054  sub _tree_construction_main ($) {
4054                     tbody => 1, tfoot=> 1, thead => 1,                     tbody => 1, tfoot=> 1, thead => 1,
4055                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
4056                  !!!back-token;                  !!!back-token;
4057                  $token = {type => 'end tag',                  $token = {type => END_TAG_TOKEN,
4058                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
4059                  redo B;                  redo B;
4060                }                }
# Line 4544  sub _tree_construction_main ($) { Line 4072  sub _tree_construction_main ($) {
4072              } elsif ({              } elsif ({
4073                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
4074                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
4075                       ($self->{insertion_mode} eq 'in row' or                       $self->{insertion_mode} & ROW_IMS) {
4076                        $self->{insertion_mode} eq 'in table body')) {                if ($self->{insertion_mode} == IN_ROW_IM) {
               if ($self->{insertion_mode} eq 'in row') {  
4077                  ## have an element in table scope                  ## have an element in table scope
4078                  my $i;                  my $i;
4079                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 4597  sub _tree_construction_main ($) { Line 4124  sub _tree_construction_main ($) {
4124                  }                  }
4125                                    
4126                  pop @{$self->{open_elements}}; # tr                  pop @{$self->{open_elements}}; # tr
4127                  $self->{insertion_mode} = 'in table body';                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
4128                  ## reprocess in the "in table body" insertion mode...                  ## reprocess in the "in table body" insertion mode...
4129                }                }
4130    
# Line 4630  sub _tree_construction_main ($) { Line 4157  sub _tree_construction_main ($) {
4157                }                }
4158    
4159                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4160                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
4161                !!!next-token;                !!!next-token;
4162                redo B;                redo B;
4163              } elsif ({              } elsif ({
4164                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
4165                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
4166                        tr => 1, # $self->{insertion_mode} eq 'in row'                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
4167                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} eq 'in table'                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
4168                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4169                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4170                ## Ignore the token                ## Ignore the token
4171                !!!next-token;                !!!next-token;
4172                redo B;                redo B;
4173              } else {          } else {
4174                #            !!!parse-error (type => 'in table:/'.$token->{tag_name});
             }  
           } else {  
             die "$0: $token->{type}: Unknown token type";  
           }  
4175    
4176            !!!parse-error (type => 'in table:'.$token->{tag_name});            $insert = $insert_to_foster;
4177            $in_body->($insert_to_foster);            #
4178            redo B;          }
4179          } elsif ($self->{insertion_mode} eq 'in column group') {        } else {
4180            if ($token->{type} eq 'character') {          die "$0: $token->{type}: Unknown token type";
4181          }
4182        } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
4183              if ($token->{type} == CHARACTER_TOKEN) {
4184              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4185                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4186                unless (length $token->{data}) {                unless (length $token->{data}) {
# Line 4664  sub _tree_construction_main ($) { Line 4190  sub _tree_construction_main ($) {
4190              }              }
4191                            
4192              #              #
4193            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} == START_TAG_TOKEN) {
4194              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
4195                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
4196                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
# Line 4673  sub _tree_construction_main ($) { Line 4199  sub _tree_construction_main ($) {
4199              } else {              } else {
4200                #                #
4201              }              }
4202            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
4203              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
4204                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] eq 'html') {
4205                  !!!parse-error (type => 'unmatched end tag:colgroup');                  !!!parse-error (type => 'unmatched end tag:colgroup');
# Line 4682  sub _tree_construction_main ($) { Line 4208  sub _tree_construction_main ($) {
4208                  redo B;                  redo B;
4209                } else {                } else {
4210                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
4211                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
4212                  !!!next-token;                  !!!next-token;
4213                  redo B;                              redo B;            
4214                }                }
# Line 4706  sub _tree_construction_main ($) { Line 4232  sub _tree_construction_main ($) {
4232              redo B;              redo B;
4233            } else {            } else {
4234              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
4235              $self->{insertion_mode} = 'in table';              $self->{insertion_mode} = IN_TABLE_IM;
4236              ## reprocess              ## reprocess
4237              redo B;              redo B;
4238            }            }
4239          } elsif ($self->{insertion_mode} eq 'in select') {      } elsif ($self->{insertion_mode} == IN_SELECT_IM) {
4240            if ($token->{type} eq 'character') {        if ($token->{type} == CHARACTER_TOKEN) {
4241              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4242              !!!next-token;          !!!next-token;
4243              redo B;          redo B;
4244            } elsif ($token->{type} eq 'start tag') {        } elsif ($token->{type} == START_TAG_TOKEN) {
4245              if ($token->{tag_name} eq 'option') {              if ($token->{tag_name} eq 'option') {
4246                if ($self->{open_elements}->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
4247                  ## As if </option>                  ## As if </option>
# Line 4768  sub _tree_construction_main ($) { Line 4294  sub _tree_construction_main ($) {
4294    
4295                !!!next-token;                !!!next-token;
4296                redo B;                redo B;
4297              } else {          } else {
4298                #            !!!parse-error (type => 'in select:'.$token->{tag_name});
4299              }            ## Ignore the token
4300            } elsif ($token->{type} eq 'end tag') {            !!!next-token;
4301              redo B;
4302            }
4303          } elsif ($token->{type} == END_TAG_TOKEN) {
4304              if ($token->{tag_name} eq 'optgroup') {              if ($token->{tag_name} eq 'optgroup') {
4305                if ($self->{open_elements}->[-1]->[1] eq 'option' and                if ($self->{open_elements}->[-1]->[1] eq 'option' and
4306                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {
# Line 4873  sub _tree_construction_main ($) { Line 4402  sub _tree_construction_main ($) {
4402    
4403                ## reprocess                ## reprocess
4404                redo B;                redo B;
4405              } else {          } else {
4406                #            !!!parse-error (type => 'in select:/'.$token->{tag_name});
             }  
           } else {  
             #  
           }  
   
           !!!parse-error (type => 'in select:'.$token->{tag_name});  
4407            ## Ignore the token            ## Ignore the token
4408            !!!next-token;            !!!next-token;
4409            redo B;            redo B;
4410          } elsif ($self->{insertion_mode} eq 'after body') {          }
4411            if ($token->{type} eq 'character') {        } else {
4412              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          die "$0: $token->{type}: Unknown token type";
4413                my $data = $1;        }
4414                ## As if in body      } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
4415                $reconstruct_active_formatting_elements->($insert_to_current);        if ($token->{type} == CHARACTER_TOKEN) {
4416            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4417              my $data = $1;
4418              ## As if in body
4419              $reconstruct_active_formatting_elements->($insert_to_current);
4420                                
4421                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4422              
4423              unless (length $token->{data}) {
4424                !!!next-token;
4425                redo B;
4426              }
4427            }
4428            
4429            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4430              !!!parse-error (type => 'after html:#character');
4431    
4432                unless (length $token->{data}) {            ## Reprocess in the "main" phase, "after body" insertion mode...
4433                  !!!next-token;          }
4434                  redo B;          
4435                }          ## "after body" insertion mode
4436              }          !!!parse-error (type => 'after body:#character');
4437                
4438              #          $self->{insertion_mode} = IN_BODY_IM;
4439              !!!parse-error (type => 'after body:#character');          ## reprocess
4440            } elsif ($token->{type} eq 'start tag') {          redo B;
4441              !!!parse-error (type => 'after body:'.$token->{tag_name});        } elsif ($token->{type} == START_TAG_TOKEN) {
4442              #          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4443            } elsif ($token->{type} eq 'end tag') {            !!!parse-error (type => 'after html:'.$token->{tag_name});
4444              if ($token->{tag_name} eq 'html') {            
4445                if (defined $self->{inner_html_node}) {            ## Reprocess in the "main" phase, "after body" insertion mode...
4446                  !!!parse-error (type => 'unmatched end tag:html');          }
4447                  ## Ignore the token  
4448                  !!!next-token;          ## "after body" insertion mode
4449                  redo B;          !!!parse-error (type => 'after body:'.$token->{tag_name});
4450                } else {  
4451                  $previous_insertion_mode = $self->{insertion_mode};          $self->{insertion_mode} = IN_BODY_IM;
4452                  $self->{insertion_mode} = 'trailing end';          ## reprocess
4453                  !!!next-token;          redo B;
4454                  redo B;        } elsif ($token->{type} == END_TAG_TOKEN) {
4455                }          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4456              } else {            !!!parse-error (type => 'after html:/'.$token->{tag_name});
4457                !!!parse-error (type => 'after body:/'.$token->{tag_name});            
4458              }            $self->{insertion_mode} = AFTER_BODY_IM;
4459              ## Reprocess in the "main" phase, "after body" insertion mode...
4460            }
4461    
4462            ## "after body" insertion mode
4463            if ($token->{tag_name} eq 'html') {
4464              if (defined $self->{inner_html_node}) {
4465                !!!parse-error (type => 'unmatched end tag:html');
4466                ## Ignore the token
4467                !!!next-token;
4468                redo B;
4469            } else {            } else {
4470              die "$0: $token->{type}: Unknown token type";              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
4471                !!!next-token;
4472                redo B;
4473            }            }
4474            } else {
4475              !!!parse-error (type => 'after body:/'.$token->{tag_name});
4476    
4477            $self->{insertion_mode} = 'in body';            $self->{insertion_mode} = IN_BODY_IM;
4478            ## reprocess            ## reprocess
4479            redo B;            redo B;
4480      } elsif ($self->{insertion_mode} eq 'in frameset') {          }
4481        if ($token->{type} eq 'character') {        } else {
4482            die "$0: $token->{type}: Unknown token type";
4483          }
4484        } elsif ($self->{insertion_mode} & FRAME_IMS) {
4485          if ($token->{type} == CHARACTER_TOKEN) {
4486          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4487            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4488              
4489            unless (length $token->{data}) {            unless (length $token->{data}) {
4490              !!!next-token;              !!!next-token;
4491              redo B;              redo B;
4492            }            }
4493          }          }
4494            
4495            if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
4496              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4497                !!!parse-error (type => 'in frameset:#character');
4498              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
4499                !!!parse-error (type => 'after frameset:#character');
4500              } else { # "after html frameset"
4501                !!!parse-error (type => 'after html:#character');
4502    
4503                $self->{insertion_mode} = AFTER_FRAMESET_IM;
4504                ## Reprocess in the "main" phase, "after frameset"...
4505                !!!parse-error (type => 'after frameset:#character');
4506              }
4507              
4508              ## Ignore the token.
4509              if (length $token->{data}) {
4510                ## reprocess the rest of characters
4511              } else {
4512                !!!next-token;
4513              }
4514              redo B;
4515            }
4516            
4517            die qq[$0: Character "$token->{data}"];
4518          } elsif ($token->{type} == START_TAG_TOKEN) {
4519            if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4520              !!!parse-error (type => 'after html:'.$token->{tag_name});
4521    
4522          !!!parse-error (type => 'in frameset:#character');            $self->{insertion_mode} = AFTER_FRAMESET_IM;
4523          ## Ignore the token            ## Process in the "main" phase, "after frameset" insertion mode...
4524          !!!next-token;          }
4525          redo B;  
4526        } elsif ($token->{type} eq 'start tag') {          if ($token->{tag_name} eq 'frameset' and
4527          if ($token->{tag_name} eq 'frameset') {              $self->{insertion_mode} == IN_FRAMESET_IM) {
4528            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes});
4529            !!!next-token;            !!!next-token;
4530            redo B;            redo B;
4531          } elsif ($token->{tag_name} eq 'frame') {          } elsif ($token->{tag_name} eq 'frame' and
4532                     $self->{insertion_mode} == IN_FRAMESET_IM) {
4533            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes});
4534            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4535            !!!next-token;            !!!next-token;
# Line 4957  sub _tree_construction_main ($) { Line 4539  sub _tree_construction_main ($) {
4539            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
4540            redo B;            redo B;
4541          } else {          } else {
4542            !!!parse-error (type => 'in frameset:'.$token->{tag_name});            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4543                !!!parse-error (type => 'in frameset:'.$token->{tag_name});
4544              } else {
4545                !!!parse-error (type => 'after frameset:'.$token->{tag_name});
4546              }
4547            ## Ignore the token            ## Ignore the token
4548            !!!next-token;            !!!next-token;
4549            redo B;            redo B;
4550          }          }
4551        } elsif ($token->{type} eq 'end tag') {        } elsif ($token->{type} == END_TAG_TOKEN) {
4552          if ($token->{tag_name} eq 'frameset') {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4553              !!!parse-error (type => 'after html:/'.$token->{tag_name});
4554    
4555              $self->{insertion_mode} = AFTER_FRAMESET_IM;
4556              ## Process in the "main" phase, "after frameset" insertion mode...
4557            }
4558    
4559            if ($token->{tag_name} eq 'frameset' and
4560                $self->{insertion_mode} == IN_FRAMESET_IM) {
4561            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] eq 'html' and
4562                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
4563              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
# Line 4976  sub _tree_construction_main ($) { Line 4570  sub _tree_construction_main ($) {
4570    
4571            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
4572                $self->{open_elements}->[-1]->[1] ne 'frameset') {                $self->{open_elements}->[-1]->[1] ne 'frameset') {
4573              $self->{insertion_mode} = 'after frameset';              $self->{insertion_mode} = AFTER_FRAMESET_IM;
4574            }            }
4575            redo B;            redo B;
4576            } elsif ($token->{tag_name} eq 'html' and
4577                     $self->{insertion_mode} == AFTER_FRAMESET_IM) {
4578              $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
4579              !!!next-token;
4580              redo B;
4581          } else {          } else {
4582            !!!parse-error (type => 'in frameset:/'.$token->{tag_name});            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4583                !!!parse-error (type => 'in frameset:/'.$token->{tag_name});
4584              } else {
4585                !!!parse-error (type => 'after frameset:/'.$token->{tag_name});
4586              }
4587            ## Ignore the token            ## Ignore the token
4588            !!!next-token;            !!!next-token;
4589            redo B;            redo B;
# Line 4988  sub _tree_construction_main ($) { Line 4591  sub _tree_construction_main ($) {
4591        } else {        } else {
4592          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
4593        }        }
     } elsif ($self->{insertion_mode} eq 'after frameset') {  
       if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
4594    
4595                unless (length $token->{data}) {        ## ISSUE: An issue in spec here
4596                  !!!next-token;      } else {
4597                  redo B;        die "$0: $self->{insertion_mode}: Unknown insertion mode";
4598                }      }
             }  
4599    
4600              if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {      ## "in body" insertion mode
4601                !!!parse-error (type => 'after frameset:#character');      if ($token->{type} == START_TAG_TOKEN) {
4602          if ($token->{tag_name} eq 'script') {
4603            ## NOTE: This is an "as if in head" code clone
4604            $script_start_tag->($insert);
4605            redo B;
4606          } elsif ($token->{tag_name} eq 'style') {
4607            ## NOTE: This is an "as if in head" code clone
4608            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4609            redo B;
4610          } elsif ({
4611                    base => 1, link => 1,
4612                   }->{$token->{tag_name}}) {
4613            ## NOTE: This is an "as if in head" code clone, only "-t" differs
4614            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4615            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4616            !!!next-token;
4617            redo B;
4618          } elsif ($token->{tag_name} eq 'meta') {
4619            ## NOTE: This is an "as if in head" code clone, only "-t" differs
4620            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4621            my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4622    
4623                ## Ignore the token.          unless ($self->{confident}) {
4624                if (length $token->{data}) {            if ($token->{attributes}->{charset}) { ## TODO: And if supported
4625                  ## reprocess the rest of characters              $self->{change_encoding}
4626                } else {                  ->($self, $token->{attributes}->{charset}->{value});
4627                  !!!next-token;              
4628                }              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4629                redo B;                  ->set_user_data (manakai_has_reference =>
4630                                         $token->{attributes}->{charset}
4631                                             ->{has_reference});
4632              } elsif ($token->{attributes}->{content}) {
4633                ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
4634                if ($token->{attributes}->{content}->{value}
4635                    =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4636                        [\x09-\x0D\x20]*=
4637                        [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4638                        ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
4639                  $self->{change_encoding}
4640                      ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);
4641                  $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4642                      ->set_user_data (manakai_has_reference =>
4643                                           $token->{attributes}->{content}
4644                                                 ->{has_reference});
4645              }              }
4646              }
         die qq[$0: Character "$token->{data}"];  
       } elsif ($token->{type} eq 'start tag') {  
         if ($token->{tag_name} eq 'noframes') {  
           ## NOTE: As if in body.  
           $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);  
           redo B;  
4647          } else {          } else {
4648            !!!parse-error (type => 'after frameset:'.$token->{tag_name});            if ($token->{attributes}->{charset}) {
4649                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4650                    ->set_user_data (manakai_has_reference =>
4651                                         $token->{attributes}->{charset}
4652                                             ->{has_reference});
4653              }
4654              if ($token->{attributes}->{content}) {
4655                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4656                    ->set_user_data (manakai_has_reference =>
4657                                         $token->{attributes}->{content}
4658                                             ->{has_reference});
4659              }
4660            }
4661    
4662            !!!next-token;
4663            redo B;
4664          } elsif ($token->{tag_name} eq 'title') {
4665            !!!parse-error (type => 'in body:title');
4666            ## NOTE: This is an "as if in head" code clone
4667            $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {
4668              if (defined $self->{head_element}) {
4669                $self->{head_element}->append_child ($_[0]);
4670              } else {
4671                $insert->($_[0]);
4672              }
4673            });
4674            redo B;
4675          } elsif ($token->{tag_name} eq 'body') {
4676            !!!parse-error (type => 'in body:body');
4677                  
4678            if (@{$self->{open_elements}} == 1 or
4679                $self->{open_elements}->[1]->[1] ne 'body') {
4680            ## Ignore the token            ## Ignore the token
4681            } else {
4682              my $body_el = $self->{open_elements}->[1]->[0];
4683              for my $attr_name (keys %{$token->{attributes}}) {
4684                unless ($body_el->has_attribute_ns (undef, $attr_name)) {
4685                  $body_el->set_attribute_ns
4686                    (undef, [undef, $attr_name],
4687                     $token->{attributes}->{$attr_name}->{value});
4688                }
4689              }
4690            }
4691            !!!next-token;
4692            redo B;
4693          } elsif ({
4694                    address => 1, blockquote => 1, center => 1, dir => 1,
4695                    div => 1, dl => 1, fieldset => 1, listing => 1,
4696                    menu => 1, ol => 1, p => 1, ul => 1,
4697                    pre => 1,
4698                   }->{$token->{tag_name}}) {
4699            ## has a p element in scope
4700            INSCOPE: for (reverse @{$self->{open_elements}}) {
4701              if ($_->[1] eq 'p') {
4702                !!!back-token;
4703                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4704                redo B;
4705              } elsif ({
4706                        table => 1, caption => 1, td => 1, th => 1,
4707                        button => 1, marquee => 1, object => 1, html => 1,
4708                       }->{$_->[1]}) {
4709                last INSCOPE;
4710              }
4711            } # INSCOPE
4712              
4713            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4714            if ($token->{tag_name} eq 'pre') {
4715              !!!next-token;
4716              if ($token->{type} == CHARACTER_TOKEN) {
4717                $token->{data} =~ s/^\x0A//;
4718                unless (length $token->{data}) {
4719                  !!!next-token;
4720                }
4721              }
4722            } else {
4723            !!!next-token;            !!!next-token;
           redo B;  
4724          }          }
4725        } elsif ($token->{type} eq 'end tag') {          redo B;
4726          if ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'form') {
4727            $previous_insertion_mode = $self->{insertion_mode};          if (defined $self->{form_element}) {
4728            $self->{insertion_mode} = 'trailing end';            !!!parse-error (type => 'in form:form');
4729              ## Ignore the token
4730            !!!next-token;            !!!next-token;
4731            redo B;            redo B;
4732          } else {          } else {
4733            !!!parse-error (type => 'after frameset:/'.$token->{tag_name});            ## has a p element in scope
4734            ## Ignore the token            INSCOPE: for (reverse @{$self->{open_elements}}) {
4735                if ($_->[1] eq 'p') {
4736                  !!!back-token;
4737                  $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4738                  redo B;
4739                } elsif ({
4740                          table => 1, caption => 1, td => 1, th => 1,
4741                          button => 1, marquee => 1, object => 1, html => 1,
4742                         }->{$_->[1]}) {
4743                  last INSCOPE;
4744                }
4745              } # INSCOPE
4746                
4747              !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4748              $self->{form_element} = $self->{open_elements}->[-1]->[0];
4749            !!!next-token;            !!!next-token;
4750            redo B;            redo B;
4751          }          }
4752        } else {        } elsif ($token->{tag_name} eq 'li') {
4753          die "$0: $token->{type}: Unknown token type";          ## has a p element in scope
4754        }          INSCOPE: for (reverse @{$self->{open_elements}}) {
4755              if ($_->[1] eq 'p') {
4756                !!!back-token;
4757                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4758                redo B;
4759              } elsif ({
4760                        table => 1, caption => 1, td => 1, th => 1,
4761                        button => 1, marquee => 1, object => 1, html => 1,
4762                       }->{$_->[1]}) {
4763                last INSCOPE;
4764              }
4765            } # INSCOPE
4766              
4767            ## Step 1
4768            my $i = -1;
4769            my $node = $self->{open_elements}->[$i];
4770            LI: {
4771              ## Step 2
4772              if ($node->[1] eq 'li') {
4773                if ($i != -1) {
4774                  !!!parse-error (type => 'end tag missing:'.
4775                                  $self->{open_elements}->[-1]->[1]);
4776                }
4777                splice @{$self->{open_elements}}, $i;
4778                last LI;
4779              }
4780              
4781              ## Step 3
4782              if (not $formatting_category->{$node->[1]} and
4783                  #not $phrasing_category->{$node->[1]} and
4784                  ($special_category->{$node->[1]} or
4785                   $scoping_category->{$node->[1]}) and
4786                  $node->[1] ne 'address' and $node->[1] ne 'div') {
4787                last LI;
4788              }
4789              
4790              ## Step 4
4791              $i--;
4792              $node = $self->{open_elements}->[$i];
4793              redo LI;
4794            } # LI
4795              
4796            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4797            !!!next-token;
4798            redo B;
4799          } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {
4800            ## has a p element in scope
4801            INSCOPE: for (reverse @{$self->{open_elements}}) {
4802              if ($_->[1] eq 'p') {
4803                !!!back-token;
4804                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4805                redo B;
4806              } elsif ({
4807                        table => 1, caption => 1, td => 1, th => 1,
4808                        button => 1, marquee => 1, object => 1, html => 1,
4809                       }->{$_->[1]}) {
4810                last INSCOPE;
4811              }
4812            } # INSCOPE
4813              
4814            ## Step 1
4815            my $i = -1;
4816            my $node = $self->{open_elements}->[$i];
4817            LI: {
4818              ## Step 2
4819              if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {
4820                if ($i != -1) {
4821                  !!!parse-error (type => 'end tag missing:'.
4822                                  $self->{open_elements}->[-1]->[1]);
4823                }
4824                splice @{$self->{open_elements}}, $i;
4825                last LI;
4826              }
4827              
4828              ## Step 3
4829              if (not $formatting_category->{$node->[1]} and
4830                  #not $phrasing_category->{$node->[1]} and
4831                  ($special_category->{$node->[1]} or
4832                   $scoping_category->{$node->[1]}) and
4833                  $node->[1] ne 'address' and $node->[1] ne 'div') {
4834                last LI;
4835              }
4836              
4837              ## Step 4
4838              $i--;
4839              $node = $self->{open_elements}->[$i];
4840              redo LI;
4841            } # LI
4842              
4843            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4844            !!!next-token;
4845            redo B;
4846          } elsif ($token->{tag_name} eq 'plaintext') {
4847            ## has a p element in scope
4848            INSCOPE: for (reverse @{$self->{open_elements}}) {
4849              if ($_->[1] eq 'p') {
4850                !!!back-token;
4851                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4852                redo B;
4853              } elsif ({
4854                        table => 1, caption => 1, td => 1, th => 1,
4855                        button => 1, marquee => 1, object => 1, html => 1,
4856                       }->{$_->[1]}) {
4857                last INSCOPE;
4858              }
4859            } # INSCOPE
4860              
4861            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4862              
4863            $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
4864              
4865            !!!next-token;
4866            redo B;
4867          } elsif ({
4868                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
4869                   }->{$token->{tag_name}}) {
4870            ## has a p element in scope
4871            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4872              my $node = $self->{open_elements}->[$_];
4873              if ($node->[1] eq 'p') {
4874                !!!back-token;
4875                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4876                redo B;
4877              } elsif ({
4878                        table => 1, caption => 1, td => 1, th => 1,
4879                        button => 1, marquee => 1, object => 1, html => 1,
4880                       }->{$node->[1]}) {
4881                last INSCOPE;
4882              }
4883            } # INSCOPE
4884              
4885            ## NOTE: See <http://html5.org/tools/web-apps-tracker?from=925&to=926>
4886            ## has an element in scope
4887            #my $i;
4888            #INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4889            #  my $node = $self->{open_elements}->[$_];
4890            #  if ({
4891            #       h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
4892            #      }->{$node->[1]}) {
4893            #    $i = $_;
4894            #    last INSCOPE;
4895            #  } elsif ({
4896            #            table => 1, caption => 1, td => 1, th => 1,
4897            #            button => 1, marquee => 1, object => 1, html => 1,
4898            #           }->{$node->[1]}) {
4899            #    last INSCOPE;
4900            #  }
4901            #} # INSCOPE
4902            #  
4903            #if (defined $i) {
4904            #  !!! parse-error (type => 'in hn:hn');
4905            #  splice @{$self->{open_elements}}, $i;
4906            #}
4907              
4908            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4909              
4910            !!!next-token;
4911            redo B;
4912          } elsif ($token->{tag_name} eq 'a') {
4913            AFE: for my $i (reverse 0..$#$active_formatting_elements) {
4914              my $node = $active_formatting_elements->[$i];
4915              if ($node->[1] eq 'a') {
4916                !!!parse-error (type => 'in a:a');
4917                
4918                !!!back-token;
4919                $token = {type => END_TAG_TOKEN, tag_name => 'a'};
4920                $formatting_end_tag->($token->{tag_name});
4921                
4922                AFE2: for (reverse 0..$#$active_formatting_elements) {
4923                  if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
4924                    splice @$active_formatting_elements, $_, 1;
4925                    last AFE2;
4926                  }
4927                } # AFE2
4928                OE: for (reverse 0..$#{$self->{open_elements}}) {
4929                  if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
4930                    splice @{$self->{open_elements}}, $_, 1;
4931                    last OE;
4932                  }
4933                } # OE
4934                last AFE;
4935              } elsif ($node->[0] eq '#marker') {
4936                last AFE;
4937              }
4938            } # AFE
4939              
4940            $reconstruct_active_formatting_elements->($insert_to_current);
4941    
4942        ## ISSUE: An issue in spec here          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4943      } elsif ($self->{insertion_mode} eq 'trailing end') {          push @$active_formatting_elements, $self->{open_elements}->[-1];
4944        ## states in the main stage is preserved yet # MUST  
4945                  !!!next-token;
4946        if ($token->{type} eq 'character') {          redo B;
4947          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {        } elsif ({
4948            my $data = $1;                  b => 1, big => 1, em => 1, font => 1, i => 1,
4949            ## As if in the main phase.                  s => 1, small => 1, strile => 1,
4950            ## NOTE: The insertion mode in the main phase                  strong => 1, tt => 1, u => 1,
4951            ## just before the phase has been changed to the trailing                 }->{$token->{tag_name}}) {
4952            ## end phase is either "after body" or "after frameset".          $reconstruct_active_formatting_elements->($insert_to_current);
4953            $reconstruct_active_formatting_elements->($insert_to_current);          
4954            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4955            push @$active_formatting_elements, $self->{open_elements}->[-1];
4956            
4957            !!!next-token;
4958            redo B;
4959          } elsif ($token->{tag_name} eq 'nobr') {
4960            $reconstruct_active_formatting_elements->($insert_to_current);
4961    
4962            ## has a |nobr| element in scope
4963            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4964              my $node = $self->{open_elements}->[$_];
4965              if ($node->[1] eq 'nobr') {
4966                !!!parse-error (type => 'in nobr:nobr');
4967                !!!back-token;
4968                $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};
4969                redo B;
4970              } elsif ({
4971                        table => 1, caption => 1, td => 1, th => 1,
4972                        button => 1, marquee => 1, object => 1, html => 1,
4973                       }->{$node->[1]}) {
4974                last INSCOPE;
4975              }
4976            } # INSCOPE
4977            
4978            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4979            push @$active_formatting_elements, $self->{open_elements}->[-1];
4980            
4981            !!!next-token;
4982            redo B;
4983          } elsif ($token->{tag_name} eq 'button') {
4984            ## has a button element in scope
4985            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4986              my $node = $self->{open_elements}->[$_];
4987              if ($node->[1] eq 'button') {
4988                !!!parse-error (type => 'in button:button');
4989                !!!back-token;
4990                $token = {type => END_TAG_TOKEN, tag_name => 'button'};
4991                redo B;
4992              } elsif ({
4993                        table => 1, caption => 1, td => 1, th => 1,
4994                        button => 1, marquee => 1, object => 1, html => 1,
4995                       }->{$node->[1]}) {
4996                last INSCOPE;
4997              }
4998            } # INSCOPE
4999                        
5000            $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);          $reconstruct_active_formatting_elements->($insert_to_current);
5001                        
5002            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5003            push @$active_formatting_elements, ['#marker', ''];
5004    
5005            !!!next-token;
5006            redo B;
5007          } elsif ($token->{tag_name} eq 'marquee' or
5008                   $token->{tag_name} eq 'object') {
5009            $reconstruct_active_formatting_elements->($insert_to_current);
5010            
5011            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5012            push @$active_formatting_elements, ['#marker', ''];
5013            
5014            !!!next-token;
5015            redo B;
5016          } elsif ($token->{tag_name} eq 'xmp') {
5017            $reconstruct_active_formatting_elements->($insert_to_current);
5018            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
5019            redo B;
5020          } elsif ($token->{tag_name} eq 'table') {
5021            ## has a p element in scope
5022            INSCOPE: for (reverse @{$self->{open_elements}}) {
5023              if ($_->[1] eq 'p') {
5024                !!!back-token;
5025                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5026                redo B;
5027              } elsif ({
5028                        table => 1, caption => 1, td => 1, th => 1,
5029                        button => 1, marquee => 1, object => 1, html => 1,
5030                       }->{$_->[1]}) {
5031                last INSCOPE;
5032              }
5033            } # INSCOPE
5034              
5035            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5036              
5037            $self->{insertion_mode} = IN_TABLE_IM;
5038              
5039            !!!next-token;
5040            redo B;
5041          } elsif ({
5042                    area => 1, basefont => 1, bgsound => 1, br => 1,
5043                    embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
5044                    image => 1,
5045                   }->{$token->{tag_name}}) {
5046            if ($token->{tag_name} eq 'image') {
5047              !!!parse-error (type => 'image');
5048              $token->{tag_name} = 'img';
5049            }
5050    
5051            ## NOTE: There is an "as if <br>" code clone.
5052            $reconstruct_active_formatting_elements->($insert_to_current);
5053            
5054            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5055            pop @{$self->{open_elements}};
5056            
5057            !!!next-token;
5058            redo B;
5059          } elsif ($token->{tag_name} eq 'hr') {
5060            ## has a p element in scope
5061            INSCOPE: for (reverse @{$self->{open_elements}}) {
5062              if ($_->[1] eq 'p') {
5063                !!!back-token;
5064                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5065                redo B;
5066              } elsif ({
5067                        table => 1, caption => 1, td => 1, th => 1,
5068                        button => 1, marquee => 1, object => 1, html => 1,
5069                       }->{$_->[1]}) {
5070                last INSCOPE;
5071              }
5072            } # INSCOPE
5073              
5074            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5075            pop @{$self->{open_elements}};
5076              
5077            !!!next-token;
5078            redo B;
5079          } elsif ($token->{tag_name} eq 'input') {
5080            $reconstruct_active_formatting_elements->($insert_to_current);
5081            
5082            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5083            ## TODO: associate with $self->{form_element} if defined
5084            pop @{$self->{open_elements}};
5085            
5086            !!!next-token;
5087            redo B;
5088          } elsif ($token->{tag_name} eq 'isindex') {
5089            !!!parse-error (type => 'isindex');
5090            
5091            if (defined $self->{form_element}) {
5092              ## Ignore the token
5093              !!!next-token;
5094              redo B;
5095            } else {
5096              my $at = $token->{attributes};
5097              my $form_attrs;
5098              $form_attrs->{action} = $at->{action} if $at->{action};
5099              my $prompt_attr = $at->{prompt};
5100              $at->{name} = {name => 'name', value => 'isindex'};
5101              delete $at->{action};
5102              delete $at->{prompt};
5103              my @tokens = (
5104                            {type => START_TAG_TOKEN, tag_name => 'form',
5105                             attributes => $form_attrs},
5106                            {type => START_TAG_TOKEN, tag_name => 'hr'},
5107                            {type => START_TAG_TOKEN, tag_name => 'p'},
5108                            {type => START_TAG_TOKEN, tag_name => 'label'},
5109                           );
5110              if ($prompt_attr) {
5111                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};
5112              } else {
5113                push @tokens, {type => CHARACTER_TOKEN,
5114                               data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD
5115                ## TODO: make this configurable
5116              }
5117              push @tokens,
5118                            {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},
5119                            #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
5120                            {type => END_TAG_TOKEN, tag_name => 'label'},
5121                            {type => END_TAG_TOKEN, tag_name => 'p'},
5122                            {type => START_TAG_TOKEN, tag_name => 'hr'},
5123                            {type => END_TAG_TOKEN, tag_name => 'form'};
5124              $token = shift @tokens;
5125              !!!back-token (@tokens);
5126              redo B;
5127            }
5128          } elsif ($token->{tag_name} eq 'textarea') {
5129            my $tag_name = $token->{tag_name};
5130            my $el;
5131            !!!create-element ($el, $token->{tag_name}, $token->{attributes});
5132            
5133            ## TODO: $self->{form_element} if defined
5134            $self->{content_model} = RCDATA_CONTENT_MODEL;
5135            delete $self->{escape}; # MUST
5136            
5137            $insert->($el);
5138            
5139            my $text = '';
5140            !!!next-token;
5141            if ($token->{type} == CHARACTER_TOKEN) {
5142              $token->{data} =~ s/^\x0A//;
5143            unless (length $token->{data}) {            unless (length $token->{data}) {
5144              !!!next-token;              !!!next-token;
             redo B;  
5145            }            }
5146          }          }
5147            while ($token->{type} == CHARACTER_TOKEN) {
5148              $text .= $token->{data};
5149              !!!next-token;
5150            }
5151            if (length $text) {
5152              $el->manakai_append_text ($text);
5153            }
5154            
5155            $self->{content_model} = PCDATA_CONTENT_MODEL;
5156            
5157            if ($token->{type} == END_TAG_TOKEN and
5158                $token->{tag_name} eq $tag_name) {
5159              ## Ignore the token
5160            } else {
5161              !!!parse-error (type => 'in RCDATA:#'.$token->{type});
5162            }
5163            !!!next-token;
5164            redo B;
5165          } elsif ({
5166                    iframe => 1,
5167                    noembed => 1,
5168                    noframes => 1,
5169                    noscript => 0, ## TODO: 1 if scripting is enabled
5170                   }->{$token->{tag_name}}) {
5171            ## NOTE: There is an "as if in body" code clone.
5172            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
5173            redo B;
5174          } elsif ($token->{tag_name} eq 'select') {
5175            $reconstruct_active_formatting_elements->($insert_to_current);
5176            
5177            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5178            
5179            $self->{insertion_mode} = IN_SELECT_IM;
5180            !!!next-token;
5181            redo B;
5182          } elsif ({
5183                    caption => 1, col => 1, colgroup => 1, frame => 1,
5184                    frameset => 1, head => 1, option => 1, optgroup => 1,
5185                    tbody => 1, td => 1, tfoot => 1, th => 1,
5186                    thead => 1, tr => 1,
5187                   }->{$token->{tag_name}}) {
5188            !!!parse-error (type => 'in body:'.$token->{tag_name});
5189            ## Ignore the token
5190            !!!next-token;
5191            redo B;
5192            
5193            ## ISSUE: An issue on HTML5 new elements in the spec.
5194          } else {
5195            $reconstruct_active_formatting_elements->($insert_to_current);
5196            
5197            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5198            
5199            !!!next-token;
5200            redo B;
5201          }
5202        } elsif ($token->{type} == END_TAG_TOKEN) {
5203          if ($token->{tag_name} eq 'body') {
5204            if (@{$self->{open_elements}} > 1 and
5205                $self->{open_elements}->[1]->[1] eq 'body') {
5206              for (@{$self->{open_elements}}) {
5207                unless ({
5208                           dd => 1, dt => 1, li => 1, p => 1, td => 1,
5209                           th => 1, tr => 1, body => 1, html => 1,
5210                         tbody => 1, tfoot => 1, thead => 1,
5211                        }->{$_->[1]}) {
5212                  !!!parse-error (type => 'not closed:'.$_->[1]);
5213                }
5214              }
5215    
5216          !!!parse-error (type => 'after html:#character');            $self->{insertion_mode} = AFTER_BODY_IM;
5217          $self->{insertion_mode} = $previous_insertion_mode;            !!!next-token;
5218          ## reprocess            redo B;
5219            } else {
5220              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5221              ## Ignore the token
5222              !!!next-token;
5223              redo B;
5224            }
5225          } elsif ($token->{tag_name} eq 'html') {
5226            if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {
5227              ## ISSUE: There is an issue in the spec.
5228              if ($self->{open_elements}->[-1]->[1] ne 'body') {
5229                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);
5230              }
5231              $self->{insertion_mode} = AFTER_BODY_IM;
5232              ## reprocess
5233              redo B;
5234            } else {
5235              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5236              ## Ignore the token
5237              !!!next-token;
5238              redo B;
5239            }
5240          } elsif ({
5241                    address => 1, blockquote => 1, center => 1, dir => 1,
5242                    div => 1, dl => 1, fieldset => 1, listing => 1,
5243                    menu => 1, ol => 1, pre => 1, ul => 1,
5244                    p => 1,
5245                    dd => 1, dt => 1, li => 1,
5246                    button => 1, marquee => 1, object => 1,
5247                   }->{$token->{tag_name}}) {
5248            ## has an element in scope
5249            my $i;
5250            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5251              my $node = $self->{open_elements}->[$_];
5252              if ($node->[1] eq $token->{tag_name}) {
5253                ## generate implied end tags
5254                if ({
5255                     dd => ($token->{tag_name} ne 'dd'),
5256                     dt => ($token->{tag_name} ne 'dt'),
5257                     li => ($token->{tag_name} ne 'li'),
5258                     p => ($token->{tag_name} ne 'p'),
5259                     td => 1, th => 1, tr => 1,
5260                     tbody => 1, tfoot=> 1, thead => 1,
5261                    }->{$self->{open_elements}->[-1]->[1]}) {
5262                  !!!back-token;
5263                  $token = {type => END_TAG_TOKEN,
5264                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5265                  redo B;
5266                }
5267                $i = $_;
5268                last INSCOPE unless $token->{tag_name} eq 'p';
5269              } elsif ({
5270                        table => 1, caption => 1, td => 1, th => 1,
5271                        button => 1, marquee => 1, object => 1, html => 1,
5272                       }->{$node->[1]}) {
5273                last INSCOPE;
5274              }
5275            } # INSCOPE
5276            
5277            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
5278              if (defined $i) {
5279                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
5280              } else {
5281                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5282              }
5283            }
5284            
5285            if (defined $i) {
5286              splice @{$self->{open_elements}}, $i;
5287            } elsif ($token->{tag_name} eq 'p') {
5288              ## As if <p>, then reprocess the current token
5289              my $el;
5290              !!!create-element ($el, 'p');
5291              $insert->($el);
5292            }
5293            $clear_up_to_marker->()
5294              if {
5295                button => 1, marquee => 1, object => 1,
5296              }->{$token->{tag_name}};
5297            !!!next-token;
5298          redo B;          redo B;
5299        } elsif ($token->{type} eq 'start tag') {        } elsif ($token->{tag_name} eq 'form') {
5300          !!!parse-error (type => 'after html:'.$token->{tag_name});          ## has an element in scope
5301          $self->{insertion_mode} = $previous_insertion_mode;          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5302          ## reprocess            my $node = $self->{open_elements}->[$_];
5303              if ($node->[1] eq $token->{tag_name}) {
5304                ## generate implied end tags
5305                if ({
5306                     dd => 1, dt => 1, li => 1, p => 1,
5307                     td => 1, th => 1, tr => 1,
5308                     tbody => 1, tfoot=> 1, thead => 1,
5309                    }->{$self->{open_elements}->[-1]->[1]}) {
5310                  !!!back-token;
5311                  $token = {type => END_TAG_TOKEN,
5312                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5313                  redo B;
5314                }
5315                last INSCOPE;
5316              } elsif ({
5317                        table => 1, caption => 1, td => 1, th => 1,
5318                        button => 1, marquee => 1, object => 1, html => 1,
5319                       }->{$node->[1]}) {
5320                last INSCOPE;
5321              }
5322            } # INSCOPE
5323            
5324            if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {
5325              pop @{$self->{open_elements}};
5326            } else {
5327              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5328            }
5329    
5330            undef $self->{form_element};
5331            !!!next-token;
5332          redo B;          redo B;
5333        } elsif ($token->{type} eq 'end tag') {        } elsif ({
5334          !!!parse-error (type => 'after html:/'.$token->{tag_name});                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5335          $self->{insertion_mode} = $previous_insertion_mode;                 }->{$token->{tag_name}}) {
5336          ## reprocess          ## has an element in scope
5337            my $i;
5338            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5339              my $node = $self->{open_elements}->[$_];
5340              if ({
5341                   h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5342                  }->{$node->[1]}) {
5343                ## generate implied end tags
5344                if ({
5345                     dd => 1, dt => 1, li => 1, p => 1,
5346                     td => 1, th => 1, tr => 1,
5347                     tbody => 1, tfoot=> 1, thead => 1,
5348                    }->{$self->{open_elements}->[-1]->[1]}) {
5349                  !!!back-token;
5350                  $token = {type => END_TAG_TOKEN,
5351                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5352                  redo B;
5353                }
5354                $i = $_;
5355                last INSCOPE;
5356              } elsif ({
5357                        table => 1, caption => 1, td => 1, th => 1,
5358                        button => 1, marquee => 1, object => 1, html => 1,
5359                       }->{$node->[1]}) {
5360                last INSCOPE;
5361              }
5362            } # INSCOPE
5363            
5364            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
5365              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5366            }
5367            
5368            splice @{$self->{open_elements}}, $i if defined $i;
5369            !!!next-token;
5370            redo B;
5371          } elsif ({
5372                    a => 1,
5373                    b => 1, big => 1, em => 1, font => 1, i => 1,
5374                    nobr => 1, s => 1, small => 1, strile => 1,
5375                    strong => 1, tt => 1, u => 1,
5376                   }->{$token->{tag_name}}) {
5377            $formatting_end_tag->($token->{tag_name});
5378            redo B;
5379          } elsif ($token->{tag_name} eq 'br') {
5380            !!!parse-error (type => 'unmatched end tag:br');
5381    
5382            ## As if <br>
5383            $reconstruct_active_formatting_elements->($insert_to_current);
5384            
5385            my $el;
5386            !!!create-element ($el, 'br');
5387            $insert->($el);
5388            
5389            ## Ignore the token.
5390            !!!next-token;
5391          redo B;          redo B;
5392          } elsif ({
5393                    caption => 1, col => 1, colgroup => 1, frame => 1,
5394                    frameset => 1, head => 1, option => 1, optgroup => 1,
5395                    tbody => 1, td => 1, tfoot => 1, th => 1,
5396                    thead => 1, tr => 1,
5397                    area => 1, basefont => 1, bgsound => 1,
5398                    embed => 1, hr => 1, iframe => 1, image => 1,
5399                    img => 1, input => 1, isindex => 1, noembed => 1,
5400                    noframes => 1, param => 1, select => 1, spacer => 1,
5401                    table => 1, textarea => 1, wbr => 1,
5402                    noscript => 0, ## TODO: if scripting is enabled
5403                   }->{$token->{tag_name}}) {
5404            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5405            ## Ignore the token
5406            !!!next-token;
5407            redo B;
5408            
5409            ## ISSUE: Issue on HTML5 new elements in spec
5410            
5411        } else {        } else {
5412          die "$0: $token->{type}: Unknown token";          ## Step 1
5413            my $node_i = -1;
5414            my $node = $self->{open_elements}->[$node_i];
5415    
5416            ## Step 2
5417            S2: {
5418              if ($node->[1] eq $token->{tag_name}) {
5419                ## Step 1
5420                ## generate implied end tags
5421                if ({
5422                     dd => 1, dt => 1, li => 1, p => 1,
5423                     td => 1, th => 1, tr => 1,
5424                     tbody => 1, tfoot => 1, thead => 1,
5425                    }->{$self->{open_elements}->[-1]->[1]}) {
5426                  !!!back-token;
5427                  $token = {type => END_TAG_TOKEN,
5428                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5429                  redo B;
5430                }
5431            
5432                ## Step 2
5433                if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {
5434                  ## NOTE: <x><y></x>
5435                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
5436                }
5437                
5438                ## Step 3
5439                splice @{$self->{open_elements}}, $node_i;
5440    
5441                !!!next-token;
5442                last S2;
5443              } else {
5444                ## Step 3
5445                if (not $formatting_category->{$node->[1]} and
5446                    #not $phrasing_category->{$node->[1]} and
5447                    ($special_category->{$node->[1]} or
5448                     $scoping_category->{$node->[1]})) {
5449                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5450                  ## Ignore the token
5451                  !!!next-token;
5452                  last S2;
5453                }
5454              }
5455              
5456              ## Step 4
5457              $node_i--;
5458              $node = $self->{open_elements}->[$node_i];
5459              
5460              ## Step 5;
5461              redo S2;
5462            } # S2
5463            redo B;
5464        }        }
     } else {  
       die "$0: $self->{insertion_mode}: Unknown insertion mode";  
5465      }      }
5466        redo B;
5467    } # B    } # B
5468    
5469      ## NOTE: The "trailing end" phase in HTML5 is split into
5470      ## two insertion modes: "after html body" and "after html frameset".
5471      ## NOTE: States in the main stage is preserved while
5472      ## the parser stays in the trailing end phase. # MUST
5473    
5474    ## Stop parsing # MUST    ## Stop parsing # MUST
5475        
5476    ## TODO: script stuffs    ## TODO: script stuffs
# Line 5093  sub set_inner_html ($$$) { Line 5482  sub set_inner_html ($$$) {
5482    my $s = \$_[0];    my $s = \$_[0];
5483    my $onerror = $_[1];    my $onerror = $_[1];
5484    
5485      ## ISSUE: Should {confident} be true?
5486    
5487    my $nt = $node->node_type;    my $nt = $node->node_type;
5488    if ($nt == 9) {    if ($nt == 9) {
5489      # MUST      # MUST
# Line 5165  sub set_inner_html ($$$) { Line 5556  sub set_inner_html ($$$) {
5556      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
5557    
5558      ## Step 2      ## Step 2
5559      my $node_ln = $node->local_name;      my $node_ln = $node->manakai_local_name;
5560      $p->{content_model} = {      $p->{content_model} = {
5561        title => RCDATA_CONTENT_MODEL,        title => RCDATA_CONTENT_MODEL,
5562        textarea => RCDATA_CONTENT_MODEL,        textarea => RCDATA_CONTENT_MODEL,
# Line 5205  sub set_inner_html ($$$) { Line 5596  sub set_inner_html ($$$) {
5596        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
5597          my $nsuri = $anode->namespace_uri;          my $nsuri = $anode->namespace_uri;
5598          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
5599            if ($anode->local_name eq 'form') { ## TODO: case?            if ($anode->manakai_local_name eq 'form') {
5600              $p->{form_element} = $anode;              $p->{form_element} = $anode;
5601              last AN;              last AN;
5602            }            }
# Line 5245  sub set_inner_html ($$$) { Line 5636  sub set_inner_html ($$$) {
5636    
5637  } # tree construction stage  } # tree construction stage
5638    
5639  sub get_inner_html ($$$) {  package Whatpm::HTML::RestartParser;
5640    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  
5641    
5642  1;  1;
5643  # $Date$  # $Date$

Legend:
Removed from v.1.49  
changed lines
  Added in v.1.75

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24