/[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.35 by wakaba, Mon Jul 16 03:21:04 2007 UTC revision 1.73 by wakaba, Sun Mar 2 23:38:37 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    
247    sub CM_ENTITY () { 0b001 } # & markup in data
248    sub CM_LIMITED_MARKUP () { 0b010 } # < markup in data (limited)
249    sub CM_FULL_MARKUP () { 0b100 } # < markup in data (any)
250    
251    sub PLAINTEXT_CONTENT_MODEL () { 0 }
252    sub CDATA_CONTENT_MODEL () { CM_LIMITED_MARKUP }
253    sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }
254    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_flag} = 'PCDATA'; # 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};
333    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
# Line 168  sub _initialize_tokenizer ($) { Line 340  sub _initialize_tokenizer ($) {
340  } # _initialize_tokenizer  } # _initialize_tokenizer
341    
342  ## A token has:  ## A token has:
343  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',  ##   ->{type} == DOCTYPE_TOKEN, START_TAG_TOKEN, END_TAG_TOKEN, COMMENT_TOKEN,
344  ##       'character', or 'end-of-file'  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN
345  ##   ->{name} (DOCTYPE, start tag (tag name), end tag (tag name))  ##   ->{name} (DOCTYPE_TOKEN)
346  ##   ->{public_identifier} (DOCTYPE)  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
347  ##   ->{system_identifier} (DOCTYPE)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
348  ##   ->{correct} == 1 or 0 (DOCTYPE)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
349  ##   ->{attributes} isa HASH (start tag, end tag)  ##   ->{correct} == 1 or 0 (DOCTYPE_TOKEN)
350  ##   ->{data} (comment, character)  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
351    ##        ->{name}
352    ##        ->{value}
353    ##        ->{has_reference} == 1 or 0
354    ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
355    
356  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
357    
# Line 185  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 192  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_flag} eq 'PCDATA' or          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
389              $self->{content_model_flag} eq 'RCDATA') {              not $self->{escape}) {
390            $self->{state} = 'entity data';            $self->{state} = ENTITY_DATA_STATE;
391            !!!next-input-character;            !!!next-input-character;
392            redo A;            redo A;
393          } else {          } else {
394            #            #
395          }          }
396        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_input_character} == 0x002D) { # -
397          if ($self->{content_model_flag} eq 'RCDATA' or          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
             $self->{content_model_flag} eq 'CDATA') {  
398            unless ($self->{escape}) {            unless ($self->{escape}) {
399              if ($self->{prev_input_character}->[0] == 0x002D and # -              if ($self->{prev_input_character}->[0] == 0x002D and # -
400                  $self->{prev_input_character}->[1] == 0x0021 and # !                  $self->{prev_input_character}->[1] == 0x0021 and # !
# Line 216  sub _get_next_token ($) { Line 406  sub _get_next_token ($) {
406                    
407          #          #
408        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_input_character} == 0x003C) { # <
409          if ($self->{content_model_flag} eq 'PCDATA' or          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
410              (($self->{content_model_flag} eq 'CDATA' or              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
               $self->{content_model_flag} eq 'RCDATA') and  
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 228  sub _get_next_token ($) { Line 417  sub _get_next_token ($) {
417          }          }
418        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
419          if ($self->{escape} and          if ($self->{escape} and
420              ($self->{content_model_flag} eq 'RCDATA' or              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
              $self->{content_model_flag} eq 'CDATA')) {  
421            if ($self->{prev_input_character}->[0] == 0x002D and # -            if ($self->{prev_input_character}->[0] == 0x002D and # -
422                $self->{prev_input_character}->[1] == 0x002D) { # -                $self->{prev_input_character}->[1] == 0x002D) { # -
423              delete $self->{escape};              delete $self->{escape};
# Line 238  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 250  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_flag} eq 'RCDATA' or        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
           $self->{content_model_flag} eq '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_flag} eq '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_flag}: Unknown content model flag";          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_flag} eq 'RCDATA' or        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
           $self->{content_model_flag} eq '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>
523            my @next_char;            my @next_char;
# Line 345  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 364  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 375  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_flag} = 'PCDATA'; # 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');
621            }            }
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 451  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_flag} = 'PCDATA'; # 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');
648            }            }
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 472  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 489  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 499  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_flag} = 'PCDATA'; # 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');
696            }            }
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 521  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 539  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_flag} = 'PCDATA'; # 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');
736            }            }
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
764            !!!parse-error (type => 'dupulicate attribute');            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});
765            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
766          } else {          } else {
767            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
# Line 582  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_flag} = 'PCDATA'; # 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');
796            }            }
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 620  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_flag} = 'PCDATA'; # 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');
837            }            }
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 667  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_flag} = 'PCDATA'; # 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');
879            }            }
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 693  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 707  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_flag} = 'PCDATA'; # 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');
920            }            }
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 733  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 747  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_flag} = 'PCDATA'; # 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');
967            }            }
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 779  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_flag} = 'PCDATA'; # 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');
987            }            }
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_flag} = 'PCDATA'; # 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');
1026            }            }
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 839  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_flag} = 'PCDATA'; # 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');
1062            }            }
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 875  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_flag} = 'PCDATA'; # 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');
1101            }            }
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 910  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_flag} = 'PCDATA'; # 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');
1121            }            }
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 973  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 983  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 1015  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 1029  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 1049  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 1058  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 1077  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 1086  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 1109  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 1124  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 1144  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 1153  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 1183  sub _get_next_token ($) { Line 1444  sub _get_next_token ($) {
1444          redo A;          redo A;
1445        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1446          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1447          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1448          !!!next-input-character;          !!!next-input-character;
1449    
1450          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect
1451    
1452          redo A;          redo A;
1453        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1454          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1455          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1456          ## reconsume          ## reconsume
1457    
1458          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect
1459    
1460          redo A;          redo A;
1461        } else {        } else {
1462          $self->{current_token}          $self->{current_token}
1463              = {type => 'DOCTYPE',              = {type => DOCTYPE_TOKEN,
1464                 name => chr ($self->{next_input_character}),                 name => chr ($self->{next_input_character}),
1465                 correct => 1};                 correct => 1};
1466  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
1467          $self->{state} = 'DOCTYPE name';          $self->{state} = DOCTYPE_NAME_STATE;
1468          !!!next-input-character;          !!!next-input-character;
1469          redo A;          redo A;
1470        }        }
1471      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
1472  ## ISSUE: Redundant "First," in the spec.  ## ISSUE: Redundant "First," in the spec.
1473        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1474            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1475            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
1476            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1477            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
1478          $self->{state} = 'after DOCTYPE name';          $self->{state} = AFTER_DOCTYPE_NAME_STATE;
1479          !!!next-input-character;          !!!next-input-character;
1480          redo A;          redo A;
1481        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1482          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1483          !!!next-input-character;          !!!next-input-character;
1484    
1485          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1226  sub _get_next_token ($) { Line 1487  sub _get_next_token ($) {
1487          redo A;          redo A;
1488        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1489          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1490          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1491          ## reconsume          ## reconsume
1492    
1493          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1240  sub _get_next_token ($) { Line 1501  sub _get_next_token ($) {
1501          !!!next-input-character;          !!!next-input-character;
1502          redo A;          redo A;
1503        }        }
1504      } elsif ($self->{state} eq 'after DOCTYPE name') {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
1505        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1506            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1507            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 1250  sub _get_next_token ($) { Line 1511  sub _get_next_token ($) {
1511          !!!next-input-character;          !!!next-input-character;
1512          redo A;          redo A;
1513        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1514          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1515          !!!next-input-character;          !!!next-input-character;
1516    
1517          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1258  sub _get_next_token ($) { Line 1519  sub _get_next_token ($) {
1519          redo A;          redo A;
1520        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1521          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1522          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1523          ## reconsume          ## reconsume
1524    
1525          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1282  sub _get_next_token ($) { Line 1543  sub _get_next_token ($) {
1543                  !!!next-input-character;                  !!!next-input-character;
1544                  if ($self->{next_input_character} == 0x0043 or # C                  if ($self->{next_input_character} == 0x0043 or # C
1545                      $self->{next_input_character} == 0x0063) { # c                      $self->{next_input_character} == 0x0063) { # c
1546                    $self->{state} = 'before DOCTYPE public identifier';                    $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1547                    !!!next-input-character;                    !!!next-input-character;
1548                    redo A;                    redo A;
1549                  }                  }
# Line 1309  sub _get_next_token ($) { Line 1570  sub _get_next_token ($) {
1570                  !!!next-input-character;                  !!!next-input-character;
1571                  if ($self->{next_input_character} == 0x004D or # M                  if ($self->{next_input_character} == 0x004D or # M
1572                      $self->{next_input_character} == 0x006D) { # m                      $self->{next_input_character} == 0x006D) { # m
1573                    $self->{state} = 'before DOCTYPE system identifier';                    $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1574                    !!!next-input-character;                    !!!next-input-character;
1575                    redo A;                    redo A;
1576                  }                  }
# Line 1325  sub _get_next_token ($) { Line 1586  sub _get_next_token ($) {
1586        }        }
1587    
1588        !!!parse-error (type => 'string after DOCTYPE name');        !!!parse-error (type => 'string after DOCTYPE name');
1589        $self->{state} = 'bogus DOCTYPE';        delete $self->{current_token}->{correct};
1590    
1591          $self->{state} = BOGUS_DOCTYPE_STATE;
1592        # next-input-character is already done        # next-input-character is already done
1593        redo A;        redo A;
1594      } elsif ($self->{state} eq 'before DOCTYPE public identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
1595        if ({        if ({
1596              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1597              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1338  sub _get_next_token ($) { Line 1601  sub _get_next_token ($) {
1601          redo A;          redo A;
1602        } elsif ($self->{next_input_character} eq 0x0022) { # "        } elsif ($self->{next_input_character} eq 0x0022) { # "
1603          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1604          $self->{state} = 'DOCTYPE public identifier (double-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
1605          !!!next-input-character;          !!!next-input-character;
1606          redo A;          redo A;
1607        } elsif ($self->{next_input_character} eq 0x0027) { # '        } elsif ($self->{next_input_character} eq 0x0027) { # '
1608          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1609          $self->{state} = 'DOCTYPE public identifier (single-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
1610          !!!next-input-character;          !!!next-input-character;
1611          redo A;          redo A;
1612        } elsif ($self->{next_input_character} eq 0x003E) { # >        } elsif ($self->{next_input_character} eq 0x003E) { # >
1613          !!!parse-error (type => 'no PUBLIC literal');          !!!parse-error (type => 'no PUBLIC literal');
1614    
1615          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1616          !!!next-input-character;          !!!next-input-character;
1617    
1618          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1359  sub _get_next_token ($) { Line 1622  sub _get_next_token ($) {
1622        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1623          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1624    
1625          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1626          ## reconsume          ## reconsume
1627    
1628          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1368  sub _get_next_token ($) { Line 1631  sub _get_next_token ($) {
1631          redo A;          redo A;
1632        } else {        } else {
1633          !!!parse-error (type => 'string after PUBLIC');          !!!parse-error (type => 'string after PUBLIC');
1634          $self->{state} = 'bogus DOCTYPE';          delete $self->{current_token}->{correct};
1635    
1636            $self->{state} = BOGUS_DOCTYPE_STATE;
1637          !!!next-input-character;          !!!next-input-character;
1638          redo A;          redo A;
1639        }        }
1640      } elsif ($self->{state} eq 'DOCTYPE public identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
1641        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_input_character} == 0x0022) { # "
1642          $self->{state} = 'after DOCTYPE public identifier';          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1643          !!!next-input-character;          !!!next-input-character;
1644          redo A;          redo A;
1645          } elsif ($self->{next_input_character} == 0x003E) { # >
1646            !!!parse-error (type => 'unclosed PUBLIC literal');
1647    
1648            $self->{state} = DATA_STATE;
1649            !!!next-input-character;
1650    
1651            delete $self->{current_token}->{correct};
1652            !!!emit ($self->{current_token}); # DOCTYPE
1653    
1654            redo A;
1655        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1656          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
1657    
1658          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1659          ## reconsume          ## reconsume
1660    
1661          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1394  sub _get_next_token ($) { Line 1669  sub _get_next_token ($) {
1669          !!!next-input-character;          !!!next-input-character;
1670          redo A;          redo A;
1671        }        }
1672      } elsif ($self->{state} eq 'DOCTYPE public identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
1673        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_input_character} == 0x0027) { # '
1674          $self->{state} = 'after DOCTYPE public identifier';          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1675          !!!next-input-character;          !!!next-input-character;
1676          redo A;          redo A;
1677          } elsif ($self->{next_input_character} == 0x003E) { # >
1678            !!!parse-error (type => 'unclosed PUBLIC literal');
1679    
1680            $self->{state} = DATA_STATE;
1681            !!!next-input-character;
1682    
1683            delete $self->{current_token}->{correct};
1684            !!!emit ($self->{current_token}); # DOCTYPE
1685    
1686            redo A;
1687        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1688          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
1689    
1690          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1691          ## reconsume          ## reconsume
1692    
1693          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1416  sub _get_next_token ($) { Line 1701  sub _get_next_token ($) {
1701          !!!next-input-character;          !!!next-input-character;
1702          redo A;          redo A;
1703        }        }
1704      } elsif ($self->{state} eq 'after DOCTYPE public identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
1705        if ({        if ({
1706              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1707              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1426  sub _get_next_token ($) { Line 1711  sub _get_next_token ($) {
1711          redo A;          redo A;
1712        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_input_character} == 0x0022) { # "
1713          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1714          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
1715          !!!next-input-character;          !!!next-input-character;
1716          redo A;          redo A;
1717        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_input_character} == 0x0027) { # '
1718          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1719          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
1720          !!!next-input-character;          !!!next-input-character;
1721          redo A;          redo A;
1722        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1723          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1724          !!!next-input-character;          !!!next-input-character;
1725    
1726          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1444  sub _get_next_token ($) { Line 1729  sub _get_next_token ($) {
1729        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1730          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1731    
1732          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1733          ## reconsume          ## reconsume
1734    
1735          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1453  sub _get_next_token ($) { Line 1738  sub _get_next_token ($) {
1738          redo A;          redo A;
1739        } else {        } else {
1740          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after PUBLIC literal');
1741          $self->{state} = 'bogus DOCTYPE';          delete $self->{current_token}->{correct};
1742    
1743            $self->{state} = BOGUS_DOCTYPE_STATE;
1744          !!!next-input-character;          !!!next-input-character;
1745          redo A;          redo A;
1746        }        }
1747      } elsif ($self->{state} eq 'before DOCTYPE system identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
1748        if ({        if ({
1749              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1750              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1467  sub _get_next_token ($) { Line 1754  sub _get_next_token ($) {
1754          redo A;          redo A;
1755        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_input_character} == 0x0022) { # "
1756          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1757          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
1758          !!!next-input-character;          !!!next-input-character;
1759          redo A;          redo A;
1760        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_input_character} == 0x0027) { # '
1761          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1762          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
1763          !!!next-input-character;          !!!next-input-character;
1764          redo A;          redo A;
1765        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1766          !!!parse-error (type => 'no SYSTEM literal');          !!!parse-error (type => 'no SYSTEM literal');
1767          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1768          !!!next-input-character;          !!!next-input-character;
1769    
1770          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1487  sub _get_next_token ($) { Line 1774  sub _get_next_token ($) {
1774        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1775          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1776    
1777          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1778          ## reconsume          ## reconsume
1779    
1780          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1496  sub _get_next_token ($) { Line 1783  sub _get_next_token ($) {
1783          redo A;          redo A;
1784        } else {        } else {
1785          !!!parse-error (type => 'string after SYSTEM');          !!!parse-error (type => 'string after SYSTEM');
1786          $self->{state} = 'bogus DOCTYPE';          delete $self->{current_token}->{correct};
1787    
1788            $self->{state} = BOGUS_DOCTYPE_STATE;
1789          !!!next-input-character;          !!!next-input-character;
1790          redo A;          redo A;
1791        }        }
1792      } elsif ($self->{state} eq 'DOCTYPE system identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
1793        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_input_character} == 0x0022) { # "
1794          $self->{state} = 'after DOCTYPE system identifier';          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1795          !!!next-input-character;          !!!next-input-character;
1796          redo A;          redo A;
1797          } elsif ($self->{next_input_character} == 0x003E) { # >
1798            !!!parse-error (type => 'unclosed PUBLIC literal');
1799    
1800            $self->{state} = DATA_STATE;
1801            !!!next-input-character;
1802    
1803            delete $self->{current_token}->{correct};
1804            !!!emit ($self->{current_token}); # DOCTYPE
1805    
1806            redo A;
1807        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1808          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
1809    
1810          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1811          ## reconsume          ## reconsume
1812    
1813          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1522  sub _get_next_token ($) { Line 1821  sub _get_next_token ($) {
1821          !!!next-input-character;          !!!next-input-character;
1822          redo A;          redo A;
1823        }        }
1824      } elsif ($self->{state} eq 'DOCTYPE system identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
1825        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_input_character} == 0x0027) { # '
1826          $self->{state} = 'after DOCTYPE system identifier';          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1827          !!!next-input-character;          !!!next-input-character;
1828          redo A;          redo A;
1829          } elsif ($self->{next_input_character} == 0x003E) { # >
1830            !!!parse-error (type => 'unclosed PUBLIC literal');
1831    
1832            $self->{state} = DATA_STATE;
1833            !!!next-input-character;
1834    
1835            delete $self->{current_token}->{correct};
1836            !!!emit ($self->{current_token}); # DOCTYPE
1837    
1838            redo A;
1839        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1840          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
1841    
1842          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1843          ## reconsume          ## reconsume
1844    
1845          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1544  sub _get_next_token ($) { Line 1853  sub _get_next_token ($) {
1853          !!!next-input-character;          !!!next-input-character;
1854          redo A;          redo A;
1855        }        }
1856      } elsif ($self->{state} eq 'after DOCTYPE system identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
1857        if ({        if ({
1858              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1859              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1553  sub _get_next_token ($) { Line 1862  sub _get_next_token ($) {
1862          !!!next-input-character;          !!!next-input-character;
1863          redo A;          redo A;
1864        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1865          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1866          !!!next-input-character;          !!!next-input-character;
1867    
1868          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1562  sub _get_next_token ($) { Line 1871  sub _get_next_token ($) {
1871        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1872          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1873    
1874          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1875          ## reconsume          ## reconsume
1876    
1877          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1571  sub _get_next_token ($) { Line 1880  sub _get_next_token ($) {
1880          redo A;          redo A;
1881        } else {        } else {
1882          !!!parse-error (type => 'string after SYSTEM literal');          !!!parse-error (type => 'string after SYSTEM literal');
1883          $self->{state} = 'bogus DOCTYPE';          delete $self->{current_token}->{correct};
1884    
1885            $self->{state} = BOGUS_DOCTYPE_STATE;
1886          !!!next-input-character;          !!!next-input-character;
1887          redo A;          redo A;
1888        }        }
1889      } elsif ($self->{state} eq 'bogus DOCTYPE') {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
1890        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_input_character} == 0x003E) { # >
1891          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1892          !!!next-input-character;          !!!next-input-character;
1893    
         delete $self->{current_token}->{correct};  
1894          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1895    
1896          redo A;          redo A;
1897        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1898          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1899          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1900          ## reconsume          ## reconsume
1901    
         delete $self->{current_token}->{correct};  
1902          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1903    
1904          redo A;          redo A;
# Line 1606  sub _get_next_token ($) { Line 1915  sub _get_next_token ($) {
1915    die "$0: _get_next_token: unexpected case";    die "$0: _get_next_token: unexpected case";
1916  } # _get_next_token  } # _get_next_token
1917    
1918  sub _tokenize_attempt_to_consume_an_entity ($$) {  sub _tokenize_attempt_to_consume_an_entity ($$$) {
1919    my ($self, $in_attr) = @_;    my ($self, $in_attr, $additional) = @_;
1920    
1921    if ({    if ({
1922         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
1923         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
1924           $additional => 1,
1925        }->{$self->{next_input_character}}) {        }->{$self->{next_input_character}}) {
1926      ## Don't consume      ## Don't consume
1927      ## No error      ## No error
# Line 1644  sub _tokenize_attempt_to_consume_an_enti Line 1954  sub _tokenize_attempt_to_consume_an_enti
1954            redo X;            redo X;
1955          } elsif (not defined $code) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
1956            !!!parse-error (type => 'bare hcro');            !!!parse-error (type => 'bare hcro');
1957              !!!back-next-input-character ($x_char, $self->{next_input_character});
1958            $self->{next_input_character} = 0x0023; # #            $self->{next_input_character} = 0x0023; # #
           !!!back-next-input-character ($x_char);  
1959            return undef;            return undef;
1960          } elsif ($self->{next_input_character} == 0x003B) { # ;          } elsif ($self->{next_input_character} == 0x003B) { # ;
1961            !!!next-input-character;            !!!next-input-character;
# Line 1667  sub _tokenize_attempt_to_consume_an_enti Line 1977  sub _tokenize_attempt_to_consume_an_enti
1977            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
1978          }          }
1979    
1980          return {type => 'character', data => chr $code};          return {type => CHARACTER_TOKEN, data => chr $code,
1981                    has_reference => 1};
1982        } # X        } # X
1983      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_input_character} and
1984               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_input_character} <= 0x0039) { # 0..9
# Line 1702  sub _tokenize_attempt_to_consume_an_enti Line 2013  sub _tokenize_attempt_to_consume_an_enti
2013          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
2014        }        }
2015                
2016        return {type => 'character', data => chr $code};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1};
2017      } else {      } else {
2018        !!!parse-error (type => 'bare nero');        !!!parse-error (type => 'bare nero');
2019        !!!back-next-input-character ($self->{next_input_character});        !!!back-next-input-character ($self->{next_input_character});
# Line 1717  sub _tokenize_attempt_to_consume_an_enti Line 2028  sub _tokenize_attempt_to_consume_an_enti
2028      !!!next-input-character;      !!!next-input-character;
2029    
2030      my $value = $entity_name;      my $value = $entity_name;
2031      my $match;      my $match = 0;
2032      require Whatpm::_NamedEntityList;      require Whatpm::_NamedEntityList;
2033      our $EntityChar;      our $EntityChar;
2034    
# Line 1737  sub _tokenize_attempt_to_consume_an_enti Line 2048  sub _tokenize_attempt_to_consume_an_enti
2048            $match = 1;            $match = 1;
2049            !!!next-input-character;            !!!next-input-character;
2050            last;            last;
2051          } elsif (not $in_attr) {          } else {
2052            $value = $EntityChar->{$entity_name};            $value = $EntityChar->{$entity_name};
2053            $match = -1;            $match = -1;
2054          } else {            !!!next-input-character;
           $value .= chr $self->{next_input_character};  
2055          }          }
2056        } else {        } else {
2057          $value .= chr $self->{next_input_character};          $value .= chr $self->{next_input_character};
2058            $match *= 2;
2059            !!!next-input-character;
2060        }        }
       !!!next-input-character;  
2061      }      }
2062            
2063      if ($match > 0) {      if ($match > 0) {
2064        return {type => 'character', data => $value};        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};
2065      } elsif ($match < 0) {      } elsif ($match < 0) {
2066        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc');
2067        return {type => 'character', data => $value};        if ($in_attr and $match < -1) {
2068            return {type => CHARACTER_TOKEN, data => '&'.$entity_name};
2069          } else {
2070            return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};
2071          }
2072      } else {      } else {
2073        !!!parse-error (type => 'bare ero');        !!!parse-error (type => 'bare ero');
2074        ## NOTE: No characters are consumed in the spec.        ## NOTE: "No characters are consumed" in the spec.
2075        return {type => 'character', data => '&'.$value};        return {type => CHARACTER_TOKEN, data => '&'.$value};
2076      }      }
2077    } else {    } else {
2078      ## no characters are consumed      ## no characters are consumed
# Line 1799  sub _construct_tree ($) { Line 2114  sub _construct_tree ($) {
2114        
2115    !!!next-token;    !!!next-token;
2116    
2117    $self->{insertion_mode} = 'before head';    $self->{insertion_mode} = BEFORE_HEAD_IM;
2118    undef $self->{form_element};    undef $self->{form_element};
2119    undef $self->{head_element};    undef $self->{head_element};
2120    $self->{open_elements} = [];    $self->{open_elements} = [];
# Line 1813  sub _construct_tree ($) { Line 2128  sub _construct_tree ($) {
2128  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
2129    my $self = shift;    my $self = shift;
2130    INITIAL: {    INITIAL: {
2131      if ($token->{type} eq 'DOCTYPE') {      if ($token->{type} == DOCTYPE_TOKEN) {
2132        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
2133        ## error, switch to a conformance checking mode for another        ## error, switch to a conformance checking mode for another
2134        ## language.        ## language.
# Line 1893  sub _tree_construction_initial ($) { Line 2208  sub _tree_construction_initial ($) {
2208            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,
2209            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,
2210            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,
2211              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//EN" => 1,
2212              "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//EN" => 1,
2213              "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//EN" => 1,
2214            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,
2215            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,
2216            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,
# Line 1940  sub _tree_construction_initial ($) { Line 2258  sub _tree_construction_initial ($) {
2258        !!!next-token;        !!!next-token;
2259        return;        return;
2260      } elsif ({      } elsif ({
2261                'start tag' => 1,                START_TAG_TOKEN, 1,
2262                'end tag' => 1,                END_TAG_TOKEN, 1,
2263                'end-of-file' => 1,                END_OF_FILE_TOKEN, 1,
2264               }->{$token->{type}}) {               }->{$token->{type}}) {
2265        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE');
2266        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
2267        ## Go to the root element phase        ## Go to the root element phase
2268        ## reprocess        ## reprocess
2269        return;        return;
2270      } elsif ($token->{type} eq 'character') {      } elsif ($token->{type} == CHARACTER_TOKEN) {
2271        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2272          ## Ignore the token          ## Ignore the token
2273    
# Line 1965  sub _tree_construction_initial ($) { Line 2283  sub _tree_construction_initial ($) {
2283        ## Go to the root element phase        ## Go to the root element phase
2284        ## reprocess        ## reprocess
2285        return;        return;
2286      } elsif ($token->{type} eq 'comment') {      } elsif ($token->{type} == COMMENT_TOKEN) {
2287        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
2288        $self->{document}->append_child ($comment);        $self->{document}->append_child ($comment);
2289                
# Line 1973  sub _tree_construction_initial ($) { Line 2291  sub _tree_construction_initial ($) {
2291        !!!next-token;        !!!next-token;
2292        redo INITIAL;        redo INITIAL;
2293      } else {      } else {
2294        die "$0: $token->{type}: Unknown token";        die "$0: $token->{type}: Unknown token type";
2295      }      }
2296    } # INITIAL    } # INITIAL
2297  } # _tree_construction_initial  } # _tree_construction_initial
# Line 1982  sub _tree_construction_root_element ($) Line 2300  sub _tree_construction_root_element ($)
2300    my $self = shift;    my $self = shift;
2301        
2302    B: {    B: {
2303        if ($token->{type} eq 'DOCTYPE') {        if ($token->{type} == DOCTYPE_TOKEN) {
2304          !!!parse-error (type => 'in html:#DOCTYPE');          !!!parse-error (type => 'in html:#DOCTYPE');
2305          ## Ignore the token          ## Ignore the token
2306          ## Stay in the phase          ## Stay in the phase
2307          !!!next-token;          !!!next-token;
2308          redo B;          redo B;
2309        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{type} == COMMENT_TOKEN) {
2310          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
2311          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
2312          ## Stay in the phase          ## Stay in the phase
2313          !!!next-token;          !!!next-token;
2314          redo B;          redo B;
2315        } elsif ($token->{type} eq 'character') {        } elsif ($token->{type} == CHARACTER_TOKEN) {
2316          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2317            ## Ignore the token.            ## Ignore the token.
2318    
# Line 2004  sub _tree_construction_root_element ($) Line 2322  sub _tree_construction_root_element ($)
2322              redo B;              redo B;
2323            }            }
2324          }          }
2325    
2326            $self->{application_cache_selection}->(undef);
2327    
2328            #
2329          } elsif ($token->{type} == START_TAG_TOKEN) {
2330            if ($token->{tag_name} eq 'html' and
2331                $token->{attributes}->{manifest}) {
2332              $self->{application_cache_selection}
2333                   ->($token->{attributes}->{manifest}->{value});
2334              ## ISSUE: No relative reference resolution?
2335            } else {
2336              $self->{application_cache_selection}->(undef);
2337            }
2338    
2339            ## ISSUE: There is an issue in the spec
2340          #          #
2341        } elsif ({        } elsif ({
2342                  'start tag' => 1,                  END_TAG_TOKEN, 1,
2343                  'end tag' => 1,                  END_OF_FILE_TOKEN, 1,
                 'end-of-file' => 1,  
2344                 }->{$token->{type}}) {                 }->{$token->{type}}) {
2345            $self->{application_cache_selection}->(undef);
2346    
2347          ## ISSUE: There is an issue in the spec          ## ISSUE: There is an issue in the spec
2348          #          #
2349        } else {        } else {
2350          die "$0: $token->{type}: Unknown token";          die "$0: $token->{type}: Unknown token type";
2351        }        }
2352    
2353        my $root_element; !!!create-element ($root_element, 'html');        my $root_element; !!!create-element ($root_element, 'html');
2354        $self->{document}->append_child ($root_element);        $self->{document}->append_child ($root_element);
2355        push @{$self->{open_elements}}, [$root_element, 'html'];        push @{$self->{open_elements}}, [$root_element, 'html'];
# Line 2055  sub _reset_insertion_mode ($) { Line 2390  sub _reset_insertion_mode ($) {
2390            
2391        ## Step 4..13        ## Step 4..13
2392        my $new_mode = {        my $new_mode = {
2393                        select => 'in select',                        select => IN_SELECT_IM,
2394                        td => 'in cell',                        td => IN_CELL_IM,
2395                        th => 'in cell',                        th => IN_CELL_IM,
2396                        tr => 'in row',                        tr => IN_ROW_IM,
2397                        tbody => 'in table body',                        tbody => IN_TABLE_BODY_IM,
2398                        thead => 'in table head',                        thead => IN_TABLE_BODY_IM,
2399                        tfoot => 'in table foot',                        tfoot => IN_TABLE_BODY_IM,
2400                        caption => 'in caption',                        caption => IN_CAPTION_IM,
2401                        colgroup => 'in column group',                        colgroup => IN_COLUMN_GROUP_IM,
2402                        table => 'in table',                        table => IN_TABLE_IM,
2403                        head => 'in body', # not in head!                        head => IN_BODY_IM, # not in head!
2404                        body => 'in body',                        body => IN_BODY_IM,
2405                        frameset => 'in frameset',                        frameset => IN_FRAMESET_IM,
2406                       }->{$node->[1]};                       }->{$node->[1]};
2407        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
2408                
2409        ## Step 14        ## Step 14
2410        if ($node->[1] eq 'html') {        if ($node->[1] eq 'html') {
2411          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
2412            $self->{insertion_mode} = 'before head';            $self->{insertion_mode} = BEFORE_HEAD_IM;
2413          } else {          } else {
2414            $self->{insertion_mode} = 'after head';            $self->{insertion_mode} = AFTER_HEAD_IM;
2415          }          }
2416          return;          return;
2417        }        }
2418                
2419        ## Step 15        ## Step 15
2420        $self->{insertion_mode} = 'in body' and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
2421                
2422        ## Step 16        ## Step 16
2423        $i--;        $i--;
# Line 2096  sub _reset_insertion_mode ($) { Line 2431  sub _reset_insertion_mode ($) {
2431  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
2432    my $self = shift;    my $self = shift;
2433    
   my $previous_insertion_mode;  
   
2434    my $active_formatting_elements = [];    my $active_formatting_elements = [];
2435    
2436    my $reconstruct_active_formatting_elements = sub { # MUST    my $reconstruct_active_formatting_elements = sub { # MUST
# Line 2192  sub _tree_construction_main ($) { Line 2525  sub _tree_construction_main ($) {
2525      $insert->($el); # /context node/->append_child ($el)      $insert->($el); # /context node/->append_child ($el)
2526    
2527      ## Step 3      ## Step 3
2528      $self->{content_model_flag} = $content_model_flag; # CDATA or RCDATA      $self->{content_model} = $content_model_flag; # CDATA or RCDATA
2529      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
2530    
2531      ## Step 4      ## Step 4
2532      my $text = '';      my $text = '';
2533      !!!next-token;      !!!next-token;
2534      while ($token->{type} eq 'character') { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
2535        $text .= $token->{data};        $text .= $token->{data};
2536        !!!next-token;        !!!next-token;
2537      }      }
# Line 2210  sub _tree_construction_main ($) { Line 2543  sub _tree_construction_main ($) {
2543      }      }
2544    
2545      ## Step 6      ## Step 6
2546      $self->{content_model_flag} = 'PCDATA';      $self->{content_model} = PCDATA_CONTENT_MODEL;
2547    
2548      ## Step 7      ## Step 7
2549      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) {
2550        ## Ignore the token        ## Ignore the token
2551        } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {
2552          !!!parse-error (type => 'in CDATA:#'.$token->{type});
2553        } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
2554          !!!parse-error (type => 'in RCDATA:#'.$token->{type});
2555      } else {      } else {
2556        !!!parse-error (type => 'in '.$content_model_flag.':#'.$token->{type});        die "$0: $content_model_flag in parse_rcdata";
2557      }      }
2558      !!!next-token;      !!!next-token;
2559    }; # $parse_rcdata    }; # $parse_rcdata
# Line 2227  sub _tree_construction_main ($) { Line 2564  sub _tree_construction_main ($) {
2564      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, 'script', $token->{attributes});
2565      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
2566    
2567      $self->{content_model_flag} = 'CDATA';      $self->{content_model} = CDATA_CONTENT_MODEL;
2568      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
2569            
2570      my $text = '';      my $text = '';
2571      !!!next-token;      !!!next-token;
2572      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) {
2573        $text .= $token->{data};        $text .= $token->{data};
2574        !!!next-token;        !!!next-token;
2575      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
# Line 2240  sub _tree_construction_main ($) { Line 2577  sub _tree_construction_main ($) {
2577        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
2578      }      }
2579                                
2580      $self->{content_model_flag} = 'PCDATA';      $self->{content_model} = PCDATA_CONTENT_MODEL;
2581    
2582      if ($token->{type} eq 'end tag' and      if ($token->{type} == END_TAG_TOKEN and
2583          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
2584        ## Ignore the token        ## Ignore the token
2585      } else {      } else {
# Line 2485  sub _tree_construction_main ($) { Line 2822  sub _tree_construction_main ($) {
2822                         }                         }
2823    }; # $insert_to_foster    }; # $insert_to_foster
2824    
2825    my $in_body = sub {    my $insert;
2826      my $insert = shift;  
2827      if ($token->{type} eq 'start tag') {    B: {
2828        if ($token->{type} == DOCTYPE_TOKEN) {
2829          !!!parse-error (type => 'DOCTYPE in the middle');
2830          ## Ignore the token
2831          ## Stay in the phase
2832          !!!next-token;
2833          redo B;
2834        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
2835          if ($self->{insertion_mode} & AFTER_HTML_IMS) {
2836            #
2837          } else {
2838            ## Generate implied end tags
2839            if ({
2840                 dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,
2841                 tbody => 1, tfoot=> 1, thead => 1,
2842                }->{$self->{open_elements}->[-1]->[1]}) {
2843              !!!back-token;
2844              $token = {type => END_TAG_TOKEN, tag_name => $self->{open_elements}->[-1]->[1]};
2845              redo B;
2846            }
2847            
2848            if (@{$self->{open_elements}} > 2 or
2849                (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {
2850              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
2851            } elsif (defined $self->{inner_html_node} and
2852                     @{$self->{open_elements}} > 1 and
2853                     $self->{open_elements}->[1]->[1] ne 'body') {
2854              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
2855            }
2856    
2857            ## ISSUE: There is an issue in the spec.
2858          }
2859    
2860          ## Stop parsing
2861          last B;
2862        } elsif ($token->{type} == START_TAG_TOKEN and
2863                 $token->{tag_name} eq 'html') {
2864          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
2865            ## Turn into the main phase
2866            !!!parse-error (type => 'after html:html');
2867            $self->{insertion_mode} = AFTER_BODY_IM;
2868          } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
2869            ## Turn into the main phase
2870            !!!parse-error (type => 'after html:html');
2871            $self->{insertion_mode} = AFTER_FRAMESET_IM;
2872          }
2873    
2874    ## ISSUE: "aa<html>" is not a parse error.
2875    ## ISSUE: "<html>" in fragment is not a parse error.
2876          unless ($token->{first_start_tag}) {
2877            !!!parse-error (type => 'not first start tag');
2878          }
2879          my $top_el = $self->{open_elements}->[0]->[0];
2880          for my $attr_name (keys %{$token->{attributes}}) {
2881            unless ($top_el->has_attribute_ns (undef, $attr_name)) {
2882              $top_el->set_attribute_ns
2883                (undef, [undef, $attr_name],
2884                 $token->{attributes}->{$attr_name}->{value});
2885            }
2886          }
2887          !!!next-token;
2888          redo B;
2889        } elsif ($token->{type} == COMMENT_TOKEN) {
2890          my $comment = $self->{document}->create_comment ($token->{data});
2891          if ($self->{insertion_mode} & AFTER_HTML_IMS) {
2892            $self->{document}->append_child ($comment);
2893          } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
2894            $self->{open_elements}->[0]->[0]->append_child ($comment);
2895          } else {
2896            $self->{open_elements}->[-1]->[0]->append_child ($comment);
2897          }
2898          !!!next-token;
2899          redo B;
2900        } elsif ($self->{insertion_mode} & HEAD_IMS) {
2901          if ($token->{type} == CHARACTER_TOKEN) {
2902            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
2903              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
2904              unless (length $token->{data}) {
2905                !!!next-token;
2906                redo B;
2907              }
2908            }
2909    
2910            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2911              ## As if <head>
2912              !!!create-element ($self->{head_element}, 'head');
2913              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2914              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2915    
2916              ## Reprocess in the "in head" insertion mode...
2917              pop @{$self->{open_elements}};
2918    
2919              ## Reprocess in the "after head" insertion mode...
2920            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2921              ## As if </noscript>
2922              pop @{$self->{open_elements}};
2923              !!!parse-error (type => 'in noscript:#character');
2924              
2925              ## Reprocess in the "in head" insertion mode...
2926              ## As if </head>
2927              pop @{$self->{open_elements}};
2928    
2929              ## Reprocess in the "after head" insertion mode...
2930            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2931              pop @{$self->{open_elements}};
2932    
2933              ## Reprocess in the "after head" insertion mode...
2934            }
2935    
2936                ## "after head" insertion mode
2937                ## As if <body>
2938                !!!insert-element ('body');
2939                $self->{insertion_mode} = IN_BODY_IM;
2940                ## reprocess
2941                redo B;
2942              } elsif ($token->{type} == START_TAG_TOKEN) {
2943                if ($token->{tag_name} eq 'head') {
2944                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2945                    !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});
2946                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2947                    push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];
2948                    $self->{insertion_mode} = IN_HEAD_IM;
2949                    !!!next-token;
2950                    redo B;
2951                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2952                    #
2953                  } else {
2954                    !!!parse-error (type => 'in head:head'); # or in head noscript
2955                    ## Ignore the token
2956                    !!!next-token;
2957                    redo B;
2958                  }
2959                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2960                  ## As if <head>
2961                  !!!create-element ($self->{head_element}, 'head');
2962                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2963                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2964    
2965                  $self->{insertion_mode} = IN_HEAD_IM;
2966                  ## Reprocess in the "in head" insertion mode...
2967                }
2968    
2969                if ($token->{tag_name} eq 'base') {
2970                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2971                    ## As if </noscript>
2972                    pop @{$self->{open_elements}};
2973                    !!!parse-error (type => 'in noscript:base');
2974                  
2975                    $self->{insertion_mode} = IN_HEAD_IM;
2976                    ## Reprocess in the "in head" insertion mode...
2977                  }
2978    
2979                  ## NOTE: There is a "as if in head" code clone.
2980                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2981                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2982                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2983                  }
2984                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2985                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2986                  pop @{$self->{open_elements}}
2987                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2988                  !!!next-token;
2989                  redo B;
2990                } elsif ($token->{tag_name} eq 'link') {
2991                  ## NOTE: There is a "as if in head" code clone.
2992                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2993                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2994                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2995                  }
2996                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2997                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2998                  pop @{$self->{open_elements}}
2999                      if $self->{insertion_mode} == AFTER_HEAD_IM;
3000                  !!!next-token;
3001                  redo B;
3002                } elsif ($token->{tag_name} eq 'meta') {
3003                  ## NOTE: There is a "as if in head" code clone.
3004                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3005                    !!!parse-error (type => 'after head:'.$token->{tag_name});
3006                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3007                  }
3008                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3009                  my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3010    
3011                  unless ($self->{confident}) {
3012                    if ($token->{attributes}->{charset}) { ## TODO: And if supported
3013                      $self->{change_encoding}
3014                          ->($self, $token->{attributes}->{charset}->{value});
3015                      
3016                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
3017                          ->set_user_data (manakai_has_reference =>
3018                                               $token->{attributes}->{charset}
3019                                                   ->{has_reference});
3020                    } elsif ($token->{attributes}->{content}) {
3021                      ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
3022                      if ($token->{attributes}->{content}->{value}
3023                          =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
3024                              [\x09-\x0D\x20]*=
3025                              [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
3026                              ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
3027                        $self->{change_encoding}
3028                            ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);
3029                        $meta_el->[0]->get_attribute_node_ns (undef, 'content')
3030                            ->set_user_data (manakai_has_reference =>
3031                                                 $token->{attributes}->{content}
3032                                                       ->{has_reference});
3033                      }
3034                    }
3035                  } else {
3036                    if ($token->{attributes}->{charset}) {
3037                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
3038                          ->set_user_data (manakai_has_reference =>
3039                                               $token->{attributes}->{charset}
3040                                                   ->{has_reference});
3041                    }
3042                    if ($token->{attributes}->{content}) {
3043                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
3044                          ->set_user_data (manakai_has_reference =>
3045                                               $token->{attributes}->{content}
3046                                                   ->{has_reference});
3047                    }
3048                  }
3049    
3050                  pop @{$self->{open_elements}}
3051                      if $self->{insertion_mode} == AFTER_HEAD_IM;
3052                  !!!next-token;
3053                  redo B;
3054                } elsif ($token->{tag_name} eq 'title') {
3055                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3056                    ## As if </noscript>
3057                    pop @{$self->{open_elements}};
3058                    !!!parse-error (type => 'in noscript:title');
3059                  
3060                    $self->{insertion_mode} = IN_HEAD_IM;
3061                    ## Reprocess in the "in head" insertion mode...
3062                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3063                    !!!parse-error (type => 'after head:'.$token->{tag_name});
3064                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3065                  }
3066    
3067                  ## NOTE: There is a "as if in head" code clone.
3068                  my $parent = defined $self->{head_element} ? $self->{head_element}
3069                      : $self->{open_elements}->[-1]->[0];
3070                  $parse_rcdata->(RCDATA_CONTENT_MODEL,
3071                                  sub { $parent->append_child ($_[0]) });
3072                  pop @{$self->{open_elements}}
3073                      if $self->{insertion_mode} == AFTER_HEAD_IM;
3074                  redo B;
3075                } elsif ($token->{tag_name} eq 'style') {
3076                  ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
3077                  ## insertion mode IN_HEAD_IM)
3078                  ## NOTE: There is a "as if in head" code clone.
3079                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3080                    !!!parse-error (type => 'after head:'.$token->{tag_name});
3081                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3082                  }
3083                  $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
3084                  pop @{$self->{open_elements}}
3085                      if $self->{insertion_mode} == AFTER_HEAD_IM;
3086                  redo B;
3087                } elsif ($token->{tag_name} eq 'noscript') {
3088                  if ($self->{insertion_mode} == IN_HEAD_IM) {
3089                    ## NOTE: and scripting is disalbed
3090                    !!!insert-element ($token->{tag_name}, $token->{attributes});
3091                    $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
3092                    !!!next-token;
3093                    redo B;
3094                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3095                    !!!parse-error (type => 'in noscript:noscript');
3096                    ## Ignore the token
3097                    !!!next-token;
3098                    redo B;
3099                  } else {
3100                    #
3101                  }
3102                } elsif ($token->{tag_name} eq 'script') {
3103                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3104                    ## As if </noscript>
3105                    pop @{$self->{open_elements}};
3106                    !!!parse-error (type => 'in noscript:script');
3107                  
3108                    $self->{insertion_mode} = IN_HEAD_IM;
3109                    ## Reprocess in the "in head" insertion mode...
3110                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3111                    !!!parse-error (type => 'after head:'.$token->{tag_name});
3112                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3113                  }
3114    
3115                  ## NOTE: There is a "as if in head" code clone.
3116                  $script_start_tag->($insert_to_current);
3117                  pop @{$self->{open_elements}}
3118                      if $self->{insertion_mode} == AFTER_HEAD_IM;
3119                  redo B;
3120                } elsif ($token->{tag_name} eq 'body' or
3121                         $token->{tag_name} eq 'frameset') {
3122                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3123                    ## As if </noscript>
3124                    pop @{$self->{open_elements}};
3125                    !!!parse-error (type => 'in noscript:'.$token->{tag_name});
3126                    
3127                    ## Reprocess in the "in head" insertion mode...
3128                    ## As if </head>
3129                    pop @{$self->{open_elements}};
3130                    
3131                    ## Reprocess in the "after head" insertion mode...
3132                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3133                    pop @{$self->{open_elements}};
3134                    
3135                    ## Reprocess in the "after head" insertion mode...
3136                  }
3137    
3138                  ## "after head" insertion mode
3139                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3140                  if ($token->{tag_name} eq 'body') {
3141                    $self->{insertion_mode} = IN_BODY_IM;
3142                  } elsif ($token->{tag_name} eq 'frameset') {
3143                    $self->{insertion_mode} = IN_FRAMESET_IM;
3144                  } else {
3145                    die "$0: tag name: $self->{tag_name}";
3146                  }
3147                  !!!next-token;
3148                  redo B;
3149                } else {
3150                  #
3151                }
3152    
3153                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3154                  ## As if </noscript>
3155                  pop @{$self->{open_elements}};
3156                  !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
3157                  
3158                  ## Reprocess in the "in head" insertion mode...
3159                  ## As if </head>
3160                  pop @{$self->{open_elements}};
3161    
3162                  ## Reprocess in the "after head" insertion mode...
3163                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3164                  ## As if </head>
3165                  pop @{$self->{open_elements}};
3166    
3167                  ## Reprocess in the "after head" insertion mode...
3168                }
3169    
3170                ## "after head" insertion mode
3171                ## As if <body>
3172                !!!insert-element ('body');
3173                $self->{insertion_mode} = IN_BODY_IM;
3174                ## reprocess
3175                redo B;
3176              } elsif ($token->{type} == END_TAG_TOKEN) {
3177                if ($token->{tag_name} eq 'head') {
3178                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3179                    ## As if <head>
3180                    !!!create-element ($self->{head_element}, 'head');
3181                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3182                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3183    
3184                    ## Reprocess in the "in head" insertion mode...
3185                    pop @{$self->{open_elements}};
3186                    $self->{insertion_mode} = AFTER_HEAD_IM;
3187                    !!!next-token;
3188                    redo B;
3189                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3190                    ## As if </noscript>
3191                    pop @{$self->{open_elements}};
3192                    !!!parse-error (type => 'in noscript:script');
3193                    
3194                    ## Reprocess in the "in head" insertion mode...
3195                    pop @{$self->{open_elements}};
3196                    $self->{insertion_mode} = AFTER_HEAD_IM;
3197                    !!!next-token;
3198                    redo B;
3199                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3200                    pop @{$self->{open_elements}};
3201                    $self->{insertion_mode} = AFTER_HEAD_IM;
3202                    !!!next-token;
3203                    redo B;
3204                  } else {
3205                    #
3206                  }
3207                } elsif ($token->{tag_name} eq 'noscript') {
3208                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3209                    pop @{$self->{open_elements}};
3210                    $self->{insertion_mode} = IN_HEAD_IM;
3211                    !!!next-token;
3212                    redo B;
3213                  } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3214                    !!!parse-error (type => 'unmatched end tag:noscript');
3215                    ## Ignore the token ## ISSUE: An issue in the spec.
3216                    !!!next-token;
3217                    redo B;
3218                  } else {
3219                    #
3220                  }
3221                } elsif ({
3222                          body => 1, html => 1,
3223                         }->{$token->{tag_name}}) {
3224                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3225                    ## As if <head>
3226                    !!!create-element ($self->{head_element}, 'head');
3227                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3228                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3229    
3230                    $self->{insertion_mode} = IN_HEAD_IM;
3231                    ## Reprocess in the "in head" insertion mode...
3232                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3233                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3234                    ## Ignore the token
3235                    !!!next-token;
3236                    redo B;
3237                  }
3238                  
3239                  #
3240                } elsif ({
3241                          p => 1, br => 1,
3242                         }->{$token->{tag_name}}) {
3243                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3244                    ## As if <head>
3245                    !!!create-element ($self->{head_element}, 'head');
3246                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3247                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3248    
3249                    $self->{insertion_mode} = IN_HEAD_IM;
3250                    ## Reprocess in the "in head" insertion mode...
3251                  }
3252    
3253                  #
3254                } else {
3255                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3256                    #
3257                  } else {
3258                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3259                    ## Ignore the token
3260                    !!!next-token;
3261                    redo B;
3262                  }
3263                }
3264    
3265                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3266                  ## As if </noscript>
3267                  pop @{$self->{open_elements}};
3268                  !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
3269                  
3270                  ## Reprocess in the "in head" insertion mode...
3271                  ## As if </head>
3272                  pop @{$self->{open_elements}};
3273    
3274                  ## Reprocess in the "after head" insertion mode...
3275                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3276                  ## As if </head>
3277                  pop @{$self->{open_elements}};
3278    
3279                  ## Reprocess in the "after head" insertion mode...
3280                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3281                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3282                  ## Ignore the token ## ISSUE: An issue in the spec.
3283                  !!!next-token;
3284                  redo B;
3285                }
3286    
3287                ## "after head" insertion mode
3288                ## As if <body>
3289                !!!insert-element ('body');
3290                $self->{insertion_mode} = IN_BODY_IM;
3291                ## reprocess
3292                redo B;
3293              } else {
3294                die "$0: $token->{type}: Unknown token type";
3295              }
3296    
3297              ## ISSUE: An issue in the spec.
3298        } elsif ($self->{insertion_mode} & BODY_IMS) {
3299              if ($token->{type} == CHARACTER_TOKEN) {
3300                ## NOTE: There is a code clone of "character in body".
3301                $reconstruct_active_formatting_elements->($insert_to_current);
3302                
3303                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3304    
3305                !!!next-token;
3306                redo B;
3307              } elsif ($token->{type} == START_TAG_TOKEN) {
3308                if ({
3309                     caption => 1, col => 1, colgroup => 1, tbody => 1,
3310                     td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
3311                    }->{$token->{tag_name}}) {
3312                  if ($self->{insertion_mode} == IN_CELL_IM) {
3313                    ## have an element in table scope
3314                    my $tn;
3315                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3316                      my $node = $self->{open_elements}->[$_];
3317                      if ($node->[1] eq 'td' or $node->[1] eq 'th') {
3318                        $tn = $node->[1];
3319                        last INSCOPE;
3320                      } elsif ({
3321                                table => 1, html => 1,
3322                               }->{$node->[1]}) {
3323                        last INSCOPE;
3324                      }
3325                    } # INSCOPE
3326                      unless (defined $tn) {
3327                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3328                        ## Ignore the token
3329                        !!!next-token;
3330                        redo B;
3331                      }
3332                    
3333                    ## Close the cell
3334                    !!!back-token; # <?>
3335                    $token = {type => END_TAG_TOKEN, tag_name => $tn};
3336                    redo B;
3337                  } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3338                    !!!parse-error (type => 'not closed:caption');
3339                    
3340                    ## As if </caption>
3341                    ## have a table element in table scope
3342                    my $i;
3343                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3344                      my $node = $self->{open_elements}->[$_];
3345                      if ($node->[1] eq 'caption') {
3346                        $i = $_;
3347                        last INSCOPE;
3348                      } elsif ({
3349                                table => 1, html => 1,
3350                               }->{$node->[1]}) {
3351                        last INSCOPE;
3352                      }
3353                    } # INSCOPE
3354                      unless (defined $i) {
3355                        !!!parse-error (type => 'unmatched end tag:caption');
3356                        ## Ignore the token
3357                        !!!next-token;
3358                        redo B;
3359                      }
3360                    
3361                    ## generate implied end tags
3362                    if ({
3363                         dd => 1, dt => 1, li => 1, p => 1,
3364                         td => 1, th => 1, tr => 1,
3365                         tbody => 1, tfoot=> 1, thead => 1,
3366                        }->{$self->{open_elements}->[-1]->[1]}) {
3367                      !!!back-token; # <?>
3368                      $token = {type => END_TAG_TOKEN, tag_name => 'caption'};
3369                      !!!back-token;
3370                      $token = {type => END_TAG_TOKEN,
3371                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3372                      redo B;
3373                    }
3374    
3375                    if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3376                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3377                    }
3378                    
3379                    splice @{$self->{open_elements}}, $i;
3380                    
3381                    $clear_up_to_marker->();
3382                    
3383                    $self->{insertion_mode} = IN_TABLE_IM;
3384                    
3385                    ## reprocess
3386                    redo B;
3387                  } else {
3388                    #
3389                  }
3390                } else {
3391                  #
3392                }
3393              } elsif ($token->{type} == END_TAG_TOKEN) {
3394                if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
3395                  if ($self->{insertion_mode} == IN_CELL_IM) {
3396                    ## have an element in table scope
3397                    my $i;
3398                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3399                      my $node = $self->{open_elements}->[$_];
3400                      if ($node->[1] eq $token->{tag_name}) {
3401                        $i = $_;
3402                        last INSCOPE;
3403                      } elsif ({
3404                                table => 1, html => 1,
3405                               }->{$node->[1]}) {
3406                        last INSCOPE;
3407                      }
3408                    } # INSCOPE
3409                      unless (defined $i) {
3410                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3411                        ## Ignore the token
3412                        !!!next-token;
3413                        redo B;
3414                      }
3415                    
3416                    ## generate implied end tags
3417                    if ({
3418                         dd => 1, dt => 1, li => 1, p => 1,
3419                         td => ($token->{tag_name} eq 'th'),
3420                         th => ($token->{tag_name} eq 'td'),
3421                         tr => 1,
3422                         tbody => 1, tfoot=> 1, thead => 1,
3423                        }->{$self->{open_elements}->[-1]->[1]}) {
3424                      !!!back-token;
3425                      $token = {type => END_TAG_TOKEN,
3426                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3427                      redo B;
3428                    }
3429                    
3430                    if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
3431                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3432                    }
3433                    
3434                    splice @{$self->{open_elements}}, $i;
3435                    
3436                    $clear_up_to_marker->();
3437                    
3438                    $self->{insertion_mode} = IN_ROW_IM;
3439                    
3440                    !!!next-token;
3441                    redo B;
3442                  } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3443                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3444                    ## Ignore the token
3445                    !!!next-token;
3446                    redo B;
3447                  } else {
3448                    #
3449                  }
3450                } elsif ($token->{tag_name} eq 'caption') {
3451                  if ($self->{insertion_mode} == IN_CAPTION_IM) {
3452                    ## have a table element in table scope
3453                    my $i;
3454                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3455                      my $node = $self->{open_elements}->[$_];
3456                      if ($node->[1] eq $token->{tag_name}) {
3457                        $i = $_;
3458                        last INSCOPE;
3459                      } elsif ({
3460                                table => 1, html => 1,
3461                               }->{$node->[1]}) {
3462                        last INSCOPE;
3463                      }
3464                    } # INSCOPE
3465                      unless (defined $i) {
3466                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3467                        ## Ignore the token
3468                        !!!next-token;
3469                        redo B;
3470                      }
3471                    
3472                    ## generate implied end tags
3473                    if ({
3474                         dd => 1, dt => 1, li => 1, p => 1,
3475                         td => 1, th => 1, tr => 1,
3476                         tbody => 1, tfoot=> 1, thead => 1,
3477                        }->{$self->{open_elements}->[-1]->[1]}) {
3478                      !!!back-token;
3479                      $token = {type => END_TAG_TOKEN,
3480                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3481                      redo B;
3482                    }
3483                    
3484                    if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3485                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3486                    }
3487                    
3488                    splice @{$self->{open_elements}}, $i;
3489                    
3490                    $clear_up_to_marker->();
3491                    
3492                    $self->{insertion_mode} = IN_TABLE_IM;
3493                    
3494                    !!!next-token;
3495                    redo B;
3496                  } elsif ($self->{insertion_mode} == IN_CELL_IM) {
3497                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3498                    ## Ignore the token
3499                    !!!next-token;
3500                    redo B;
3501                  } else {
3502                    #
3503                  }
3504                } elsif ({
3505                          table => 1, tbody => 1, tfoot => 1,
3506                          thead => 1, tr => 1,
3507                         }->{$token->{tag_name}} and
3508                         $self->{insertion_mode} == IN_CELL_IM) {
3509                  ## have an element in table scope
3510                  my $i;
3511                  my $tn;
3512                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3513                    my $node = $self->{open_elements}->[$_];
3514                    if ($node->[1] eq $token->{tag_name}) {
3515                      $i = $_;
3516                      last INSCOPE;
3517                    } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {
3518                      $tn = $node->[1];
3519                      ## NOTE: There is exactly one |td| or |th| element
3520                      ## in scope in the stack of open elements by definition.
3521                    } elsif ({
3522                              table => 1, html => 1,
3523                             }->{$node->[1]}) {
3524                      last INSCOPE;
3525                    }
3526                  } # INSCOPE
3527                  unless (defined $i) {
3528                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3529                    ## Ignore the token
3530                    !!!next-token;
3531                    redo B;
3532                  }
3533    
3534                  ## Close the cell
3535                  !!!back-token; # </?>
3536                  $token = {type => END_TAG_TOKEN, tag_name => $tn};
3537                  redo B;
3538                } elsif ($token->{tag_name} eq 'table' and
3539                         $self->{insertion_mode} == IN_CAPTION_IM) {
3540                  !!!parse-error (type => 'not closed:caption');
3541    
3542                  ## As if </caption>
3543                  ## have a table element in table scope
3544                  my $i;
3545                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3546                    my $node = $self->{open_elements}->[$_];
3547                    if ($node->[1] eq 'caption') {
3548                      $i = $_;
3549                      last INSCOPE;
3550                    } elsif ({
3551                              table => 1, html => 1,
3552                             }->{$node->[1]}) {
3553                      last INSCOPE;
3554                    }
3555                  } # INSCOPE
3556                  unless (defined $i) {
3557                    !!!parse-error (type => 'unmatched end tag:caption');
3558                    ## Ignore the token
3559                    !!!next-token;
3560                    redo B;
3561                  }
3562                  
3563                  ## generate implied end tags
3564                  if ({
3565                       dd => 1, dt => 1, li => 1, p => 1,
3566                       td => 1, th => 1, tr => 1,
3567                       tbody => 1, tfoot=> 1, thead => 1,
3568                      }->{$self->{open_elements}->[-1]->[1]}) {
3569                    !!!back-token; # </table>
3570                    $token = {type => END_TAG_TOKEN, tag_name => 'caption'};
3571                    !!!back-token;
3572                    $token = {type => END_TAG_TOKEN,
3573                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3574                    redo B;
3575                  }
3576    
3577                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3578                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3579                  }
3580    
3581                  splice @{$self->{open_elements}}, $i;
3582    
3583                  $clear_up_to_marker->();
3584    
3585                  $self->{insertion_mode} = IN_TABLE_IM;
3586    
3587                  ## reprocess
3588                  redo B;
3589                } elsif ({
3590                          body => 1, col => 1, colgroup => 1, html => 1,
3591                         }->{$token->{tag_name}}) {
3592                  if ($self->{insertion_mode} & BODY_TABLE_IMS) {
3593                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3594                    ## Ignore the token
3595                    !!!next-token;
3596                    redo B;
3597                  } else {
3598                    #
3599                  }
3600                } elsif ({
3601                          tbody => 1, tfoot => 1,
3602                          thead => 1, tr => 1,
3603                         }->{$token->{tag_name}} and
3604                         $self->{insertion_mode} == IN_CAPTION_IM) {
3605                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3606                  ## Ignore the token
3607                  !!!next-token;
3608                  redo B;
3609                } else {
3610                  #
3611                }
3612          } else {
3613            die "$0: $token->{type}: Unknown token type";
3614          }
3615    
3616          $insert = $insert_to_current;
3617          #
3618        } elsif ($self->{insertion_mode} & TABLE_IMS) {
3619          if ($token->{type} == CHARACTER_TOKEN) {
3620                if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3621                  $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3622                  
3623                  unless (length $token->{data}) {
3624                    !!!next-token;
3625                    redo B;
3626                  }
3627                }
3628    
3629                !!!parse-error (type => 'in table:#character');
3630    
3631                ## As if in body, but insert into foster parent element
3632                ## ISSUE: Spec says that "whenever a node would be inserted
3633                ## into the current node" while characters might not be
3634                ## result in a new Text node.
3635                $reconstruct_active_formatting_elements->($insert_to_foster);
3636                
3637                if ({
3638                     table => 1, tbody => 1, tfoot => 1,
3639                     thead => 1, tr => 1,
3640                    }->{$self->{open_elements}->[-1]->[1]}) {
3641                  # MUST
3642                  my $foster_parent_element;
3643                  my $next_sibling;
3644                  my $prev_sibling;
3645                  OE: for (reverse 0..$#{$self->{open_elements}}) {
3646                    if ($self->{open_elements}->[$_]->[1] eq 'table') {
3647                      my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3648                      if (defined $parent and $parent->node_type == 1) {
3649                        $foster_parent_element = $parent;
3650                        $next_sibling = $self->{open_elements}->[$_]->[0];
3651                        $prev_sibling = $next_sibling->previous_sibling;
3652                      } else {
3653                        $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
3654                        $prev_sibling = $foster_parent_element->last_child;
3655                      }
3656                      last OE;
3657                    }
3658                  } # OE
3659                  $foster_parent_element = $self->{open_elements}->[0]->[0] and
3660                  $prev_sibling = $foster_parent_element->last_child
3661                    unless defined $foster_parent_element;
3662                  if (defined $prev_sibling and
3663                      $prev_sibling->node_type == 3) {
3664                    $prev_sibling->manakai_append_text ($token->{data});
3665                  } else {
3666                    $foster_parent_element->insert_before
3667                      ($self->{document}->create_text_node ($token->{data}),
3668                       $next_sibling);
3669                  }
3670                } else {
3671                  $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3672                }
3673                
3674                !!!next-token;
3675                redo B;
3676          } elsif ($token->{type} == START_TAG_TOKEN) {
3677                if ({
3678                     tr => ($self->{insertion_mode} != IN_ROW_IM),
3679                     th => 1, td => 1,
3680                    }->{$token->{tag_name}}) {
3681                  if ($self->{insertion_mode} == IN_TABLE_IM) {
3682                    ## Clear back to table context
3683                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
3684                           $self->{open_elements}->[-1]->[1] ne 'html') {
3685                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3686                      pop @{$self->{open_elements}};
3687                    }
3688                    
3689                    !!!insert-element ('tbody');
3690                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
3691                    ## reprocess in the "in table body" insertion mode...
3692                  }
3693    
3694                  if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3695                    unless ($token->{tag_name} eq 'tr') {
3696                      !!!parse-error (type => 'missing start tag:tr');
3697                    }
3698                    
3699                    ## Clear back to table body context
3700                    while (not {
3701                      tbody => 1, tfoot => 1, thead => 1, html => 1,
3702                    }->{$self->{open_elements}->[-1]->[1]}) {
3703                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3704                      pop @{$self->{open_elements}};
3705                    }
3706                    
3707                    $self->{insertion_mode} = IN_ROW_IM;
3708                    if ($token->{tag_name} eq 'tr') {
3709                      !!!insert-element ($token->{tag_name}, $token->{attributes});
3710                      !!!next-token;
3711                      redo B;
3712                    } else {
3713                      !!!insert-element ('tr');
3714                      ## reprocess in the "in row" insertion mode
3715                    }
3716                  }
3717    
3718                  ## Clear back to table row context
3719                  while (not {
3720                    tr => 1, html => 1,
3721                  }->{$self->{open_elements}->[-1]->[1]}) {
3722                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3723                    pop @{$self->{open_elements}};
3724                  }
3725                  
3726                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3727                  $self->{insertion_mode} = IN_CELL_IM;
3728    
3729                  push @$active_formatting_elements, ['#marker', ''];
3730                  
3731                  !!!next-token;
3732                  redo B;
3733                } elsif ({
3734                          caption => 1, col => 1, colgroup => 1,
3735                          tbody => 1, tfoot => 1, thead => 1,
3736                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
3737                         }->{$token->{tag_name}}) {
3738                  if ($self->{insertion_mode} == IN_ROW_IM) {
3739                    ## As if </tr>
3740                    ## have an element in table scope
3741                    my $i;
3742                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3743                      my $node = $self->{open_elements}->[$_];
3744                      if ($node->[1] eq 'tr') {
3745                        $i = $_;
3746                        last INSCOPE;
3747                      } elsif ({
3748                                table => 1, html => 1,
3749                               }->{$node->[1]}) {
3750                        last INSCOPE;
3751                      }
3752                    } # INSCOPE
3753                    unless (defined $i) {
3754                      !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});
3755                      ## Ignore the token
3756                      !!!next-token;
3757                      redo B;
3758                    }
3759                    
3760                    ## Clear back to table row context
3761                    while (not {
3762                      tr => 1, html => 1,
3763                    }->{$self->{open_elements}->[-1]->[1]}) {
3764                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3765                      pop @{$self->{open_elements}};
3766                    }
3767                    
3768                    pop @{$self->{open_elements}}; # tr
3769                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
3770                    if ($token->{tag_name} eq 'tr') {
3771                      ## reprocess
3772                      redo B;
3773                    } else {
3774                      ## reprocess in the "in table body" insertion mode...
3775                    }
3776                  }
3777    
3778                  if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3779                    ## have an element in table scope
3780                    my $i;
3781                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3782                      my $node = $self->{open_elements}->[$_];
3783                      if ({
3784                           tbody => 1, thead => 1, tfoot => 1,
3785                          }->{$node->[1]}) {
3786                        $i = $_;
3787                        last INSCOPE;
3788                      } elsif ({
3789                                table => 1, html => 1,
3790                               }->{$node->[1]}) {
3791                        last INSCOPE;
3792                      }
3793                    } # INSCOPE
3794                    unless (defined $i) {
3795                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3796                      ## Ignore the token
3797                      !!!next-token;
3798                      redo B;
3799                    }
3800    
3801                    ## Clear back to table body context
3802                    while (not {
3803                      tbody => 1, tfoot => 1, thead => 1, html => 1,
3804                    }->{$self->{open_elements}->[-1]->[1]}) {
3805                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3806                      pop @{$self->{open_elements}};
3807                    }
3808                    
3809                    ## As if <{current node}>
3810                    ## have an element in table scope
3811                    ## true by definition
3812                    
3813                    ## Clear back to table body context
3814                    ## nop by definition
3815                    
3816                    pop @{$self->{open_elements}};
3817                    $self->{insertion_mode} = IN_TABLE_IM;
3818                    ## reprocess in "in table" insertion mode...
3819                  }
3820    
3821                  if ($token->{tag_name} eq 'col') {
3822                    ## Clear back to table context
3823                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
3824                           $self->{open_elements}->[-1]->[1] ne 'html') {
3825                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3826                      pop @{$self->{open_elements}};
3827                    }
3828                    
3829                    !!!insert-element ('colgroup');
3830                    $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
3831                    ## reprocess
3832                    redo B;
3833                  } elsif ({
3834                            caption => 1,
3835                            colgroup => 1,
3836                            tbody => 1, tfoot => 1, thead => 1,
3837                           }->{$token->{tag_name}}) {
3838                    ## Clear back to table context
3839                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
3840                           $self->{open_elements}->[-1]->[1] ne 'html') {
3841                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3842                      pop @{$self->{open_elements}};
3843                    }
3844                    
3845                    push @$active_formatting_elements, ['#marker', '']
3846                        if $token->{tag_name} eq 'caption';
3847                    
3848                    !!!insert-element ($token->{tag_name}, $token->{attributes});
3849                    $self->{insertion_mode} = {
3850                                               caption => IN_CAPTION_IM,
3851                                               colgroup => IN_COLUMN_GROUP_IM,
3852                                               tbody => IN_TABLE_BODY_IM,
3853                                               tfoot => IN_TABLE_BODY_IM,
3854                                               thead => IN_TABLE_BODY_IM,
3855                                              }->{$token->{tag_name}};
3856                    !!!next-token;
3857                    redo B;
3858                  } else {
3859                    die "$0: in table: <>: $token->{tag_name}";
3860                  }
3861                } elsif ($token->{tag_name} eq 'table') {
3862                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3863    
3864                  ## As if </table>
3865                  ## have a table element in table scope
3866                  my $i;
3867                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3868                    my $node = $self->{open_elements}->[$_];
3869                    if ($node->[1] eq 'table') {
3870                      $i = $_;
3871                      last INSCOPE;
3872                    } elsif ({
3873                              table => 1, html => 1,
3874                             }->{$node->[1]}) {
3875                      last INSCOPE;
3876                    }
3877                  } # INSCOPE
3878                  unless (defined $i) {
3879                    !!!parse-error (type => 'unmatched end tag:table');
3880                    ## Ignore tokens </table><table>
3881                    !!!next-token;
3882                    redo B;
3883                  }
3884                  
3885                  ## generate implied end tags
3886                  if ({
3887                       dd => 1, dt => 1, li => 1, p => 1,
3888                       td => 1, th => 1, tr => 1,
3889                       tbody => 1, tfoot=> 1, thead => 1,
3890                      }->{$self->{open_elements}->[-1]->[1]}) {
3891                    !!!back-token; # <table>
3892                    $token = {type => END_TAG_TOKEN, tag_name => 'table'};
3893                    !!!back-token;
3894                    $token = {type => END_TAG_TOKEN,
3895                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3896                    redo B;
3897                  }
3898    
3899                  if ($self->{open_elements}->[-1]->[1] ne 'table') {
3900                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3901                  }
3902    
3903                  splice @{$self->{open_elements}}, $i;
3904    
3905                  $self->_reset_insertion_mode;
3906    
3907                  ## reprocess
3908                  redo B;
3909            } else {
3910              !!!parse-error (type => 'in table:'.$token->{tag_name});
3911    
3912              $insert = $insert_to_foster;
3913              #
3914            }
3915          } elsif ($token->{type} == END_TAG_TOKEN) {
3916                if ($token->{tag_name} eq 'tr' and
3917                    $self->{insertion_mode} == IN_ROW_IM) {
3918                  ## have an element in table scope
3919                  my $i;
3920                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3921                    my $node = $self->{open_elements}->[$_];
3922                    if ($node->[1] eq $token->{tag_name}) {
3923                      $i = $_;
3924                      last INSCOPE;
3925                    } elsif ({
3926                              table => 1, html => 1,
3927                             }->{$node->[1]}) {
3928                      last INSCOPE;
3929                    }
3930                  } # INSCOPE
3931                  unless (defined $i) {
3932                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3933                    ## Ignore the token
3934                    !!!next-token;
3935                    redo B;
3936                  }
3937    
3938                  ## Clear back to table row context
3939                  while (not {
3940                    tr => 1, html => 1,
3941                  }->{$self->{open_elements}->[-1]->[1]}) {
3942                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3943                    pop @{$self->{open_elements}};
3944                  }
3945    
3946                  pop @{$self->{open_elements}}; # tr
3947                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
3948                  !!!next-token;
3949                  redo B;
3950                } elsif ($token->{tag_name} eq 'table') {
3951                  if ($self->{insertion_mode} == IN_ROW_IM) {
3952                    ## As if </tr>
3953                    ## have an element in table scope
3954                    my $i;
3955                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3956                      my $node = $self->{open_elements}->[$_];
3957                      if ($node->[1] eq 'tr') {
3958                        $i = $_;
3959                        last INSCOPE;
3960                      } elsif ({
3961                                table => 1, html => 1,
3962                               }->{$node->[1]}) {
3963                        last INSCOPE;
3964                      }
3965                    } # INSCOPE
3966                    unless (defined $i) {
3967                      !!!parse-error (type => 'unmatched end tag:'.$token->{type});
3968                      ## Ignore the token
3969                      !!!next-token;
3970                      redo B;
3971                    }
3972                    
3973                    ## Clear back to table row context
3974                    while (not {
3975                      tr => 1, html => 1,
3976                    }->{$self->{open_elements}->[-1]->[1]}) {
3977                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3978                      pop @{$self->{open_elements}};
3979                    }
3980                    
3981                    pop @{$self->{open_elements}}; # tr
3982                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
3983                    ## reprocess in the "in table body" insertion mode...
3984                  }
3985    
3986                  if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3987                    ## have an element in table scope
3988                    my $i;
3989                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3990                      my $node = $self->{open_elements}->[$_];
3991                      if ({
3992                           tbody => 1, thead => 1, tfoot => 1,
3993                          }->{$node->[1]}) {
3994                        $i = $_;
3995                        last INSCOPE;
3996                      } elsif ({
3997                                table => 1, html => 1,
3998                               }->{$node->[1]}) {
3999                        last INSCOPE;
4000                      }
4001                    } # INSCOPE
4002                    unless (defined $i) {
4003                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4004                      ## Ignore the token
4005                      !!!next-token;
4006                      redo B;
4007                    }
4008                    
4009                    ## Clear back to table body context
4010                    while (not {
4011                      tbody => 1, tfoot => 1, thead => 1, html => 1,
4012                    }->{$self->{open_elements}->[-1]->[1]}) {
4013                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4014                      pop @{$self->{open_elements}};
4015                    }
4016                    
4017                    ## As if <{current node}>
4018                    ## have an element in table scope
4019                    ## true by definition
4020                    
4021                    ## Clear back to table body context
4022                    ## nop by definition
4023                    
4024                    pop @{$self->{open_elements}};
4025                    $self->{insertion_mode} = IN_TABLE_IM;
4026                    ## reprocess in the "in table" insertion mode...
4027                  }
4028    
4029                  ## have a table element in table scope
4030                  my $i;
4031                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4032                    my $node = $self->{open_elements}->[$_];
4033                    if ($node->[1] eq $token->{tag_name}) {
4034                      $i = $_;
4035                      last INSCOPE;
4036                    } elsif ({
4037                              table => 1, html => 1,
4038                             }->{$node->[1]}) {
4039                      last INSCOPE;
4040                    }
4041                  } # INSCOPE
4042                  unless (defined $i) {
4043                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4044                    ## Ignore the token
4045                    !!!next-token;
4046                    redo B;
4047                  }
4048    
4049                  ## generate implied end tags
4050                  if ({
4051                       dd => 1, dt => 1, li => 1, p => 1,
4052                       td => 1, th => 1, tr => 1,
4053                       tbody => 1, tfoot=> 1, thead => 1,
4054                      }->{$self->{open_elements}->[-1]->[1]}) {
4055                    !!!back-token;
4056                    $token = {type => END_TAG_TOKEN,
4057                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
4058                    redo B;
4059                  }
4060                  
4061                  if ($self->{open_elements}->[-1]->[1] ne 'table') {
4062                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4063                  }
4064                    
4065                  splice @{$self->{open_elements}}, $i;
4066                  
4067                  $self->_reset_insertion_mode;
4068                  
4069                  !!!next-token;
4070                  redo B;
4071                } elsif ({
4072                          tbody => 1, tfoot => 1, thead => 1,
4073                         }->{$token->{tag_name}} and
4074                         $self->{insertion_mode} & ROW_IMS) {
4075                  if ($self->{insertion_mode} == IN_ROW_IM) {
4076                    ## have an element in table scope
4077                    my $i;
4078                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4079                      my $node = $self->{open_elements}->[$_];
4080                      if ($node->[1] eq $token->{tag_name}) {
4081                        $i = $_;
4082                        last INSCOPE;
4083                      } elsif ({
4084                                table => 1, html => 1,
4085                               }->{$node->[1]}) {
4086                        last INSCOPE;
4087                      }
4088                    } # INSCOPE
4089                      unless (defined $i) {
4090                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4091                        ## Ignore the token
4092                        !!!next-token;
4093                        redo B;
4094                      }
4095                    
4096                    ## As if </tr>
4097                    ## have an element in table scope
4098                    my $i;
4099                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4100                      my $node = $self->{open_elements}->[$_];
4101                      if ($node->[1] eq 'tr') {
4102                        $i = $_;
4103                        last INSCOPE;
4104                      } elsif ({
4105                                table => 1, html => 1,
4106                               }->{$node->[1]}) {
4107                        last INSCOPE;
4108                      }
4109                    } # INSCOPE
4110                      unless (defined $i) {
4111                        !!!parse-error (type => 'unmatched end tag:tr');
4112                        ## Ignore the token
4113                        !!!next-token;
4114                        redo B;
4115                      }
4116                    
4117                    ## Clear back to table row context
4118                    while (not {
4119                      tr => 1, html => 1,
4120                    }->{$self->{open_elements}->[-1]->[1]}) {
4121                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4122                      pop @{$self->{open_elements}};
4123                    }
4124                    
4125                    pop @{$self->{open_elements}}; # tr
4126                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
4127                    ## reprocess in the "in table body" insertion mode...
4128                  }
4129    
4130                  ## have an element in table scope
4131                  my $i;
4132                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4133                    my $node = $self->{open_elements}->[$_];
4134                    if ($node->[1] eq $token->{tag_name}) {
4135                      $i = $_;
4136                      last INSCOPE;
4137                    } elsif ({
4138                              table => 1, html => 1,
4139                             }->{$node->[1]}) {
4140                      last INSCOPE;
4141                    }
4142                  } # INSCOPE
4143                  unless (defined $i) {
4144                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4145                    ## Ignore the token
4146                    !!!next-token;
4147                    redo B;
4148                  }
4149    
4150                  ## Clear back to table body context
4151                  while (not {
4152                    tbody => 1, tfoot => 1, thead => 1, html => 1,
4153                  }->{$self->{open_elements}->[-1]->[1]}) {
4154                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4155                    pop @{$self->{open_elements}};
4156                  }
4157    
4158                  pop @{$self->{open_elements}};
4159                  $self->{insertion_mode} = IN_TABLE_IM;
4160                  !!!next-token;
4161                  redo B;
4162                } elsif ({
4163                          body => 1, caption => 1, col => 1, colgroup => 1,
4164                          html => 1, td => 1, th => 1,
4165                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
4166                          tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
4167                         }->{$token->{tag_name}}) {
4168                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4169                  ## Ignore the token
4170                  !!!next-token;
4171                  redo B;
4172            } else {
4173              !!!parse-error (type => 'in table:/'.$token->{tag_name});
4174    
4175              $insert = $insert_to_foster;
4176              #
4177            }
4178          } else {
4179            die "$0: $token->{type}: Unknown token type";
4180          }
4181        } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
4182              if ($token->{type} == CHARACTER_TOKEN) {
4183                if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4184                  $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4185                  unless (length $token->{data}) {
4186                    !!!next-token;
4187                    redo B;
4188                  }
4189                }
4190                
4191                #
4192              } elsif ($token->{type} == START_TAG_TOKEN) {
4193                if ($token->{tag_name} eq 'col') {
4194                  !!!insert-element ($token->{tag_name}, $token->{attributes});
4195                  pop @{$self->{open_elements}};
4196                  !!!next-token;
4197                  redo B;
4198                } else {
4199                  #
4200                }
4201              } elsif ($token->{type} == END_TAG_TOKEN) {
4202                if ($token->{tag_name} eq 'colgroup') {
4203                  if ($self->{open_elements}->[-1]->[1] eq 'html') {
4204                    !!!parse-error (type => 'unmatched end tag:colgroup');
4205                    ## Ignore the token
4206                    !!!next-token;
4207                    redo B;
4208                  } else {
4209                    pop @{$self->{open_elements}}; # colgroup
4210                    $self->{insertion_mode} = IN_TABLE_IM;
4211                    !!!next-token;
4212                    redo B;            
4213                  }
4214                } elsif ($token->{tag_name} eq 'col') {
4215                  !!!parse-error (type => 'unmatched end tag:col');
4216                  ## Ignore the token
4217                  !!!next-token;
4218                  redo B;
4219                } else {
4220                  #
4221                }
4222              } else {
4223                #
4224              }
4225    
4226              ## As if </colgroup>
4227              if ($self->{open_elements}->[-1]->[1] eq 'html') {
4228                !!!parse-error (type => 'unmatched end tag:colgroup');
4229                ## Ignore the token
4230                !!!next-token;
4231                redo B;
4232              } else {
4233                pop @{$self->{open_elements}}; # colgroup
4234                $self->{insertion_mode} = IN_TABLE_IM;
4235                ## reprocess
4236                redo B;
4237              }
4238        } elsif ($self->{insertion_mode} == IN_SELECT_IM) {
4239          if ($token->{type} == CHARACTER_TOKEN) {
4240            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4241            !!!next-token;
4242            redo B;
4243          } elsif ($token->{type} == START_TAG_TOKEN) {
4244                if ($token->{tag_name} eq 'option') {
4245                  if ($self->{open_elements}->[-1]->[1] eq 'option') {
4246                    ## As if </option>
4247                    pop @{$self->{open_elements}};
4248                  }
4249    
4250                  !!!insert-element ($token->{tag_name}, $token->{attributes});
4251                  !!!next-token;
4252                  redo B;
4253                } elsif ($token->{tag_name} eq 'optgroup') {
4254                  if ($self->{open_elements}->[-1]->[1] eq 'option') {
4255                    ## As if </option>
4256                    pop @{$self->{open_elements}};
4257                  }
4258    
4259                  if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {
4260                    ## As if </optgroup>
4261                    pop @{$self->{open_elements}};
4262                  }
4263    
4264                  !!!insert-element ($token->{tag_name}, $token->{attributes});
4265                  !!!next-token;
4266                  redo B;
4267                } elsif ($token->{tag_name} eq 'select') {
4268                  !!!parse-error (type => 'not closed:select');
4269                  ## As if </select> instead
4270                  ## have an element in table scope
4271                  my $i;
4272                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4273                    my $node = $self->{open_elements}->[$_];
4274                    if ($node->[1] eq $token->{tag_name}) {
4275                      $i = $_;
4276                      last INSCOPE;
4277                    } elsif ({
4278                              table => 1, html => 1,
4279                             }->{$node->[1]}) {
4280                      last INSCOPE;
4281                    }
4282                  } # INSCOPE
4283                  unless (defined $i) {
4284                    !!!parse-error (type => 'unmatched end tag:select');
4285                    ## Ignore the token
4286                    !!!next-token;
4287                    redo B;
4288                  }
4289                  
4290                  splice @{$self->{open_elements}}, $i;
4291    
4292                  $self->_reset_insertion_mode;
4293    
4294                  !!!next-token;
4295                  redo B;
4296            } else {
4297              !!!parse-error (type => 'in select:'.$token->{tag_name});
4298              ## Ignore the token
4299              !!!next-token;
4300              redo B;
4301            }
4302          } elsif ($token->{type} == END_TAG_TOKEN) {
4303                if ($token->{tag_name} eq 'optgroup') {
4304                  if ($self->{open_elements}->[-1]->[1] eq 'option' and
4305                      $self->{open_elements}->[-2]->[1] eq 'optgroup') {
4306                    ## As if </option>
4307                    splice @{$self->{open_elements}}, -2;
4308                  } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {
4309                    pop @{$self->{open_elements}};
4310                  } else {
4311                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4312                    ## Ignore the token
4313                  }
4314                  !!!next-token;
4315                  redo B;
4316                } elsif ($token->{tag_name} eq 'option') {
4317                  if ($self->{open_elements}->[-1]->[1] eq 'option') {
4318                    pop @{$self->{open_elements}};
4319                  } else {
4320                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4321                    ## Ignore the token
4322                  }
4323                  !!!next-token;
4324                  redo B;
4325                } elsif ($token->{tag_name} eq 'select') {
4326                  ## have an element in table scope
4327                  my $i;
4328                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4329                    my $node = $self->{open_elements}->[$_];
4330                    if ($node->[1] eq $token->{tag_name}) {
4331                      $i = $_;
4332                      last INSCOPE;
4333                    } elsif ({
4334                              table => 1, html => 1,
4335                             }->{$node->[1]}) {
4336                      last INSCOPE;
4337                    }
4338                  } # INSCOPE
4339                  unless (defined $i) {
4340                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4341                    ## Ignore the token
4342                    !!!next-token;
4343                    redo B;
4344                  }
4345                  
4346                  splice @{$self->{open_elements}}, $i;
4347    
4348                  $self->_reset_insertion_mode;
4349    
4350                  !!!next-token;
4351                  redo B;
4352                } elsif ({
4353                          caption => 1, table => 1, tbody => 1,
4354                          tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
4355                         }->{$token->{tag_name}}) {
4356                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4357                  
4358                  ## have an element in table scope
4359                  my $i;
4360                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4361                    my $node = $self->{open_elements}->[$_];
4362                    if ($node->[1] eq $token->{tag_name}) {
4363                      $i = $_;
4364                      last INSCOPE;
4365                    } elsif ({
4366                              table => 1, html => 1,
4367                             }->{$node->[1]}) {
4368                      last INSCOPE;
4369                    }
4370                  } # INSCOPE
4371                  unless (defined $i) {
4372                    ## Ignore the token
4373                    !!!next-token;
4374                    redo B;
4375                  }
4376                  
4377                  ## As if </select>
4378                  ## have an element in table scope
4379                  undef $i;
4380                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4381                    my $node = $self->{open_elements}->[$_];
4382                    if ($node->[1] eq 'select') {
4383                      $i = $_;
4384                      last INSCOPE;
4385                    } elsif ({
4386                              table => 1, html => 1,
4387                             }->{$node->[1]}) {
4388                      last INSCOPE;
4389                    }
4390                  } # INSCOPE
4391                  unless (defined $i) {
4392                    !!!parse-error (type => 'unmatched end tag:select');
4393                    ## Ignore the </select> token
4394                    !!!next-token; ## TODO: ok?
4395                    redo B;
4396                  }
4397                  
4398                  splice @{$self->{open_elements}}, $i;
4399    
4400                  $self->_reset_insertion_mode;
4401    
4402                  ## reprocess
4403                  redo B;
4404            } else {
4405              !!!parse-error (type => 'in select:/'.$token->{tag_name});
4406              ## Ignore the token
4407              !!!next-token;
4408              redo B;
4409            }
4410          } else {
4411            die "$0: $token->{type}: Unknown token type";
4412          }
4413        } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
4414          if ($token->{type} == CHARACTER_TOKEN) {
4415            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4416              my $data = $1;
4417              ## As if in body
4418              $reconstruct_active_formatting_elements->($insert_to_current);
4419                  
4420              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4421              
4422              unless (length $token->{data}) {
4423                !!!next-token;
4424                redo B;
4425              }
4426            }
4427            
4428            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4429              !!!parse-error (type => 'after html:#character');
4430    
4431              ## Reprocess in the "main" phase, "after body" insertion mode...
4432            }
4433            
4434            ## "after body" insertion mode
4435            !!!parse-error (type => 'after body:#character');
4436    
4437            $self->{insertion_mode} = IN_BODY_IM;
4438            ## reprocess
4439            redo B;
4440          } elsif ($token->{type} == START_TAG_TOKEN) {
4441            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4442              !!!parse-error (type => 'after html:'.$token->{tag_name});
4443              
4444              ## Reprocess in the "main" phase, "after body" insertion mode...
4445            }
4446    
4447            ## "after body" insertion mode
4448            !!!parse-error (type => 'after body:'.$token->{tag_name});
4449    
4450            $self->{insertion_mode} = IN_BODY_IM;
4451            ## reprocess
4452            redo B;
4453          } elsif ($token->{type} == END_TAG_TOKEN) {
4454            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4455              !!!parse-error (type => 'after html:/'.$token->{tag_name});
4456              
4457              $self->{insertion_mode} = AFTER_BODY_IM;
4458              ## Reprocess in the "main" phase, "after body" insertion mode...
4459            }
4460    
4461            ## "after body" insertion mode
4462            if ($token->{tag_name} eq 'html') {
4463              if (defined $self->{inner_html_node}) {
4464                !!!parse-error (type => 'unmatched end tag:html');
4465                ## Ignore the token
4466                !!!next-token;
4467                redo B;
4468              } else {
4469                $self->{insertion_mode} = AFTER_HTML_BODY_IM;
4470                !!!next-token;
4471                redo B;
4472              }
4473            } else {
4474              !!!parse-error (type => 'after body:/'.$token->{tag_name});
4475    
4476              $self->{insertion_mode} = IN_BODY_IM;
4477              ## reprocess
4478              redo B;
4479            }
4480          } else {
4481            die "$0: $token->{type}: Unknown token type";
4482          }
4483        } elsif ($self->{insertion_mode} & FRAME_IMS) {
4484          if ($token->{type} == CHARACTER_TOKEN) {
4485            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4486              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4487              
4488              unless (length $token->{data}) {
4489                !!!next-token;
4490                redo B;
4491              }
4492            }
4493            
4494            if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
4495              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4496                !!!parse-error (type => 'in frameset:#character');
4497              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
4498                !!!parse-error (type => 'after frameset:#character');
4499              } else { # "after html frameset"
4500                !!!parse-error (type => 'after html:#character');
4501    
4502                $self->{insertion_mode} = AFTER_FRAMESET_IM;
4503                ## Reprocess in the "main" phase, "after frameset"...
4504                !!!parse-error (type => 'after frameset:#character');
4505              }
4506              
4507              ## Ignore the token.
4508              if (length $token->{data}) {
4509                ## reprocess the rest of characters
4510              } else {
4511                !!!next-token;
4512              }
4513              redo B;
4514            }
4515            
4516            die qq[$0: Character "$token->{data}"];
4517          } elsif ($token->{type} == START_TAG_TOKEN) {
4518            if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4519              !!!parse-error (type => 'after html:'.$token->{tag_name});
4520    
4521              $self->{insertion_mode} = AFTER_FRAMESET_IM;
4522              ## Process in the "main" phase, "after frameset" insertion mode...
4523            }
4524    
4525            if ($token->{tag_name} eq 'frameset' and
4526                $self->{insertion_mode} == IN_FRAMESET_IM) {
4527              !!!insert-element ($token->{tag_name}, $token->{attributes});
4528              !!!next-token;
4529              redo B;
4530            } elsif ($token->{tag_name} eq 'frame' and
4531                     $self->{insertion_mode} == IN_FRAMESET_IM) {
4532              !!!insert-element ($token->{tag_name}, $token->{attributes});
4533              pop @{$self->{open_elements}};
4534              !!!next-token;
4535              redo B;
4536            } elsif ($token->{tag_name} eq 'noframes') {
4537              ## NOTE: As if in body.
4538              $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
4539              redo B;
4540            } else {
4541              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4542                !!!parse-error (type => 'in frameset:'.$token->{tag_name});
4543              } else {
4544                !!!parse-error (type => 'after frameset:'.$token->{tag_name});
4545              }
4546              ## Ignore the token
4547              !!!next-token;
4548              redo B;
4549            }
4550          } elsif ($token->{type} == END_TAG_TOKEN) {
4551            if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4552              !!!parse-error (type => 'after html:/'.$token->{tag_name});
4553    
4554              $self->{insertion_mode} = AFTER_FRAMESET_IM;
4555              ## Process in the "main" phase, "after frameset" insertion mode...
4556            }
4557    
4558            if ($token->{tag_name} eq 'frameset' and
4559                $self->{insertion_mode} == IN_FRAMESET_IM) {
4560              if ($self->{open_elements}->[-1]->[1] eq 'html' and
4561                  @{$self->{open_elements}} == 1) {
4562                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4563                ## Ignore the token
4564                !!!next-token;
4565              } else {
4566                pop @{$self->{open_elements}};
4567                !!!next-token;
4568              }
4569    
4570              if (not defined $self->{inner_html_node} and
4571                  $self->{open_elements}->[-1]->[1] ne 'frameset') {
4572                $self->{insertion_mode} = AFTER_FRAMESET_IM;
4573              }
4574              redo B;
4575            } elsif ($token->{tag_name} eq 'html' and
4576                     $self->{insertion_mode} == AFTER_FRAMESET_IM) {
4577              $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
4578              !!!next-token;
4579              redo B;
4580            } else {
4581              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4582                !!!parse-error (type => 'in frameset:/'.$token->{tag_name});
4583              } else {
4584                !!!parse-error (type => 'after frameset:/'.$token->{tag_name});
4585              }
4586              ## Ignore the token
4587              !!!next-token;
4588              redo B;
4589            }
4590          } else {
4591            die "$0: $token->{type}: Unknown token type";
4592          }
4593    
4594          ## ISSUE: An issue in spec here
4595        } else {
4596          die "$0: $self->{insertion_mode}: Unknown insertion mode";
4597        }
4598    
4599        ## "in body" insertion mode
4600        if ($token->{type} == START_TAG_TOKEN) {
4601        if ($token->{tag_name} eq 'script') {        if ($token->{tag_name} eq 'script') {
4602          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
4603          $script_start_tag->($insert);          $script_start_tag->($insert);
4604          return;          redo B;
4605        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
4606          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
4607          $parse_rcdata->('CDATA', $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4608          return;          redo B;
4609        } elsif ({        } elsif ({
4610                  base => 1, link => 1,                  base => 1, link => 1,
4611                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 2503  sub _tree_construction_main ($) { Line 4613  sub _tree_construction_main ($) {
4613          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4614          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4615          !!!next-token;          !!!next-token;
4616          return;          redo B;
4617        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
4618          ## NOTE: This is an "as if in head" code clone, only "-t" differs          ## NOTE: This is an "as if in head" code clone, only "-t" differs
4619          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4620          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.
4621    
4622          unless ($self->{confident}) {          unless ($self->{confident}) {
           my $charset;  
4623            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) { ## TODO: And if supported
4624              $charset = $token->{attributes}->{charset}->{value};              $self->{change_encoding}
4625            }                  ->($self, $token->{attributes}->{charset}->{value});
4626            if ($token->{attributes}->{'http-equiv'}) {              
4627                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4628                    ->set_user_data (manakai_has_reference =>
4629                                         $token->{attributes}->{charset}
4630                                             ->{has_reference});
4631              } elsif ($token->{attributes}->{content}) {
4632              ## 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.
4633              if ($token->{attributes}->{'http-equiv'}->{value}              if ($token->{attributes}->{content}->{value}
4634                  =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4635                        [\x09-\x0D\x20]*=
4636                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4637                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
4638                $charset = defined $1 ? $1 : defined $2 ? $2 : $3;                $self->{change_encoding}
4639              } ## TODO: And if supported                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);
4640                  $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4641                      ->set_user_data (manakai_has_reference =>
4642                                           $token->{attributes}->{content}
4643                                                 ->{has_reference});
4644                }
4645              }
4646            } else {
4647              if ($token->{attributes}->{charset}) {
4648                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4649                    ->set_user_data (manakai_has_reference =>
4650                                         $token->{attributes}->{charset}
4651                                             ->{has_reference});
4652              }
4653              if ($token->{attributes}->{content}) {
4654                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4655                    ->set_user_data (manakai_has_reference =>
4656                                         $token->{attributes}->{content}
4657                                             ->{has_reference});
4658            }            }
           ## TODO: Change the encoding  
4659          }          }
4660    
4661          !!!next-token;          !!!next-token;
4662          return;          redo B;
4663        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
4664          !!!parse-error (type => 'in body:title');          !!!parse-error (type => 'in body:title');
4665          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
4666          $parse_rcdata->('RCDATA', sub {          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {
4667            if (defined $self->{head_element}) {            if (defined $self->{head_element}) {
4668              $self->{head_element}->append_child ($_[0]);              $self->{head_element}->append_child ($_[0]);
4669            } else {            } else {
4670              $insert->($_[0]);              $insert->($_[0]);
4671            }            }
4672          });          });
4673          return;          redo B;
4674        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
4675          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body:body');
4676                                
# Line 2556  sub _tree_construction_main ($) { Line 4688  sub _tree_construction_main ($) {
4688            }            }
4689          }          }
4690          !!!next-token;          !!!next-token;
4691          return;          redo B;
4692        } elsif ({        } elsif ({
4693                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
4694                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1, listing => 1,
# Line 2567  sub _tree_construction_main ($) { Line 4699  sub _tree_construction_main ($) {
4699          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4700            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4701              !!!back-token;              !!!back-token;
4702              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4703              return;              redo B;
4704            } elsif ({            } elsif ({
4705                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4706                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2580  sub _tree_construction_main ($) { Line 4712  sub _tree_construction_main ($) {
4712          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4713          if ($token->{tag_name} eq 'pre') {          if ($token->{tag_name} eq 'pre') {
4714            !!!next-token;            !!!next-token;
4715            if ($token->{type} eq 'character') {            if ($token->{type} == CHARACTER_TOKEN) {
4716              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
4717              unless (length $token->{data}) {              unless (length $token->{data}) {
4718                !!!next-token;                !!!next-token;
# Line 2589  sub _tree_construction_main ($) { Line 4721  sub _tree_construction_main ($) {
4721          } else {          } else {
4722            !!!next-token;            !!!next-token;
4723          }          }
4724          return;          redo B;
4725        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
4726          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
4727            !!!parse-error (type => 'in form:form');            !!!parse-error (type => 'in form:form');
4728            ## Ignore the token            ## Ignore the token
4729            !!!next-token;            !!!next-token;
4730            return;            redo B;
4731          } else {          } else {
4732            ## has a p element in scope            ## has a p element in scope
4733            INSCOPE: for (reverse @{$self->{open_elements}}) {            INSCOPE: for (reverse @{$self->{open_elements}}) {
4734              if ($_->[1] eq 'p') {              if ($_->[1] eq 'p') {
4735                !!!back-token;                !!!back-token;
4736                $token = {type => 'end tag', tag_name => 'p'};                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4737                return;                redo B;
4738              } elsif ({              } elsif ({
4739                        table => 1, caption => 1, td => 1, th => 1,                        table => 1, caption => 1, td => 1, th => 1,
4740                        button => 1, marquee => 1, object => 1, html => 1,                        button => 1, marquee => 1, object => 1, html => 1,
# Line 2614  sub _tree_construction_main ($) { Line 4746  sub _tree_construction_main ($) {
4746            !!!insert-element-t ($token->{tag_name}, $token->{attributes});            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4747            $self->{form_element} = $self->{open_elements}->[-1]->[0];            $self->{form_element} = $self->{open_elements}->[-1]->[0];
4748            !!!next-token;            !!!next-token;
4749            return;            redo B;
4750          }          }
4751        } elsif ($token->{tag_name} eq 'li') {        } elsif ($token->{tag_name} eq 'li') {
4752          ## has a p element in scope          ## has a p element in scope
4753          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4754            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4755              !!!back-token;              !!!back-token;
4756              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4757              return;              redo B;
4758            } elsif ({            } elsif ({
4759                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4760                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2662  sub _tree_construction_main ($) { Line 4794  sub _tree_construction_main ($) {
4794                        
4795          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4796          !!!next-token;          !!!next-token;
4797          return;          redo B;
4798        } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {        } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {
4799          ## has a p element in scope          ## has a p element in scope
4800          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4801            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4802              !!!back-token;              !!!back-token;
4803              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4804              return;              redo B;
4805            } elsif ({            } elsif ({
4806                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4807                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2709  sub _tree_construction_main ($) { Line 4841  sub _tree_construction_main ($) {
4841                        
4842          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4843          !!!next-token;          !!!next-token;
4844          return;          redo B;
4845        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
4846          ## has a p element in scope          ## has a p element in scope
4847          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4848            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4849              !!!back-token;              !!!back-token;
4850              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4851              return;              redo B;
4852            } elsif ({            } elsif ({
4853                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4854                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2727  sub _tree_construction_main ($) { Line 4859  sub _tree_construction_main ($) {
4859                        
4860          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4861                        
4862          $self->{content_model_flag} = 'PLAINTEXT';          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
4863                        
4864          !!!next-token;          !!!next-token;
4865          return;          redo B;
4866        } elsif ({        } elsif ({
4867                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
4868                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 2739  sub _tree_construction_main ($) { Line 4871  sub _tree_construction_main ($) {
4871            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
4872            if ($node->[1] eq 'p') {            if ($node->[1] eq 'p') {
4873              !!!back-token;              !!!back-token;
4874              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4875              return;              redo B;
4876            } elsif ({            } elsif ({
4877                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4878                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2775  sub _tree_construction_main ($) { Line 4907  sub _tree_construction_main ($) {
4907          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4908                        
4909          !!!next-token;          !!!next-token;
4910          return;          redo B;
4911        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
4912          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
4913            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
# Line 2783  sub _tree_construction_main ($) { Line 4915  sub _tree_construction_main ($) {
4915              !!!parse-error (type => 'in a:a');              !!!parse-error (type => 'in a:a');
4916                            
4917              !!!back-token;              !!!back-token;
4918              $token = {type => 'end tag', tag_name => 'a'};              $token = {type => END_TAG_TOKEN, tag_name => 'a'};
4919              $formatting_end_tag->($token->{tag_name});              $formatting_end_tag->($token->{tag_name});
4920                            
4921              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
# Line 2810  sub _tree_construction_main ($) { Line 4942  sub _tree_construction_main ($) {
4942          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
4943    
4944          !!!next-token;          !!!next-token;
4945          return;          redo B;
4946        } elsif ({        } elsif ({
4947                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
4948                  s => 1, small => 1, strile => 1,                  s => 1, small => 1, strile => 1,
# Line 2822  sub _tree_construction_main ($) { Line 4954  sub _tree_construction_main ($) {
4954          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
4955                    
4956          !!!next-token;          !!!next-token;
4957          return;          redo B;
4958        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
4959          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
4960    
# Line 2830  sub _tree_construction_main ($) { Line 4962  sub _tree_construction_main ($) {
4962          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4963            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
4964            if ($node->[1] eq 'nobr') {            if ($node->[1] eq 'nobr') {
4965              !!!parse-error (type => 'not closed:nobr');              !!!parse-error (type => 'in nobr:nobr');
4966              !!!back-token;              !!!back-token;
4967              $token = {type => 'end tag', tag_name => 'nobr'};              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};
4968              return;              redo B;
4969            } elsif ({            } elsif ({
4970                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4971                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2846  sub _tree_construction_main ($) { Line 4978  sub _tree_construction_main ($) {
4978          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
4979                    
4980          !!!next-token;          !!!next-token;
4981          return;          redo B;
4982        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
4983          ## has a button element in scope          ## has a button element in scope
4984          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 2854  sub _tree_construction_main ($) { Line 4986  sub _tree_construction_main ($) {
4986            if ($node->[1] eq 'button') {            if ($node->[1] eq 'button') {
4987              !!!parse-error (type => 'in button:button');              !!!parse-error (type => 'in button:button');
4988              !!!back-token;              !!!back-token;
4989              $token = {type => 'end tag', tag_name => 'button'};              $token = {type => END_TAG_TOKEN, tag_name => 'button'};
4990              return;              redo B;
4991            } elsif ({            } elsif ({
4992                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4993                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2870  sub _tree_construction_main ($) { Line 5002  sub _tree_construction_main ($) {
5002          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
5003    
5004          !!!next-token;          !!!next-token;
5005          return;          redo B;
5006        } elsif ($token->{tag_name} eq 'marquee' or        } elsif ($token->{tag_name} eq 'marquee' or
5007                 $token->{tag_name} eq 'object') {                 $token->{tag_name} eq 'object') {
5008          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
# Line 2879  sub _tree_construction_main ($) { Line 5011  sub _tree_construction_main ($) {
5011          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
5012                    
5013          !!!next-token;          !!!next-token;
5014          return;          redo B;
5015        } elsif ($token->{tag_name} eq 'xmp') {        } elsif ($token->{tag_name} eq 'xmp') {
5016          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5017          $parse_rcdata->('CDATA', $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
5018          return;          redo B;
5019        } elsif ($token->{tag_name} eq 'table') {        } elsif ($token->{tag_name} eq 'table') {
5020          ## has a p element in scope          ## has a p element in scope
5021          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5022            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
5023              !!!back-token;              !!!back-token;
5024              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5025              return;              redo B;
5026            } elsif ({            } elsif ({
5027                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
5028                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2901  sub _tree_construction_main ($) { Line 5033  sub _tree_construction_main ($) {
5033                        
5034          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5035                        
5036          $self->{insertion_mode} = 'in table';          $self->{insertion_mode} = IN_TABLE_IM;
5037                        
5038          !!!next-token;          !!!next-token;
5039          return;          redo B;
5040        } elsif ({        } elsif ({
5041                  area => 1, basefont => 1, bgsound => 1, br => 1,                  area => 1, basefont => 1, bgsound => 1, br => 1,
5042                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
# Line 2922  sub _tree_construction_main ($) { Line 5054  sub _tree_construction_main ($) {
5054          pop @{$self->{open_elements}};          pop @{$self->{open_elements}};
5055                    
5056          !!!next-token;          !!!next-token;
5057          return;          redo B;
5058        } elsif ($token->{tag_name} eq 'hr') {        } elsif ($token->{tag_name} eq 'hr') {
5059          ## has a p element in scope          ## has a p element in scope
5060          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5061            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
5062              !!!back-token;              !!!back-token;
5063              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5064              return;              redo B;
5065            } elsif ({            } elsif ({
5066                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
5067                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2942  sub _tree_construction_main ($) { Line 5074  sub _tree_construction_main ($) {
5074          pop @{$self->{open_elements}};          pop @{$self->{open_elements}};
5075                        
5076          !!!next-token;          !!!next-token;
5077          return;          redo B;
5078        } elsif ($token->{tag_name} eq 'input') {        } elsif ($token->{tag_name} eq 'input') {
5079          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5080                    
# Line 2951  sub _tree_construction_main ($) { Line 5083  sub _tree_construction_main ($) {
5083          pop @{$self->{open_elements}};          pop @{$self->{open_elements}};
5084                    
5085          !!!next-token;          !!!next-token;
5086          return;          redo B;
5087        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
5088          !!!parse-error (type => 'isindex');          !!!parse-error (type => 'isindex');
5089                    
5090          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
5091            ## Ignore the token            ## Ignore the token
5092            !!!next-token;            !!!next-token;
5093            return;            redo B;
5094          } else {          } else {
5095            my $at = $token->{attributes};            my $at = $token->{attributes};
5096            my $form_attrs;            my $form_attrs;
# Line 2968  sub _tree_construction_main ($) { Line 5100  sub _tree_construction_main ($) {
5100            delete $at->{action};            delete $at->{action};
5101            delete $at->{prompt};            delete $at->{prompt};
5102            my @tokens = (            my @tokens = (
5103                          {type => 'start tag', tag_name => 'form',                          {type => START_TAG_TOKEN, tag_name => 'form',
5104                           attributes => $form_attrs},                           attributes => $form_attrs},
5105                          {type => 'start tag', tag_name => 'hr'},                          {type => START_TAG_TOKEN, tag_name => 'hr'},
5106                          {type => 'start tag', tag_name => 'p'},                          {type => START_TAG_TOKEN, tag_name => 'p'},
5107                          {type => 'start tag', tag_name => 'label'},                          {type => START_TAG_TOKEN, tag_name => 'label'},
5108                         );                         );
5109            if ($prompt_attr) {            if ($prompt_attr) {
5110              push @tokens, {type => 'character', data => $prompt_attr->{value}};              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};
5111            } else {            } else {
5112              push @tokens, {type => 'character',              push @tokens, {type => CHARACTER_TOKEN,
5113                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD
5114              ## TODO: make this configurable              ## TODO: make this configurable
5115            }            }
5116            push @tokens,            push @tokens,
5117                          {type => 'start tag', tag_name => 'input', attributes => $at},                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},
5118                          #{type => 'character', data => ''}, # SHOULD                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
5119                          {type => 'end tag', tag_name => 'label'},                          {type => END_TAG_TOKEN, tag_name => 'label'},
5120                          {type => 'end tag', tag_name => 'p'},                          {type => END_TAG_TOKEN, tag_name => 'p'},
5121                          {type => 'start tag', tag_name => 'hr'},                          {type => START_TAG_TOKEN, tag_name => 'hr'},
5122                          {type => 'end tag', tag_name => 'form'};                          {type => END_TAG_TOKEN, tag_name => 'form'};
5123            $token = shift @tokens;            $token = shift @tokens;
5124            !!!back-token (@tokens);            !!!back-token (@tokens);
5125            return;            redo B;
5126          }          }
5127        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
5128          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
# Line 2998  sub _tree_construction_main ($) { Line 5130  sub _tree_construction_main ($) {
5130          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $token->{tag_name}, $token->{attributes});
5131                    
5132          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
5133          $self->{content_model_flag} = 'RCDATA';          $self->{content_model} = RCDATA_CONTENT_MODEL;
5134          delete $self->{escape}; # MUST          delete $self->{escape}; # MUST
5135                    
5136          $insert->($el);          $insert->($el);
5137                    
5138          my $text = '';          my $text = '';
5139          !!!next-token;          !!!next-token;
5140          if ($token->{type} eq 'character') {          if ($token->{type} == CHARACTER_TOKEN) {
5141            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
5142            unless (length $token->{data}) {            unless (length $token->{data}) {
5143              !!!next-token;              !!!next-token;
5144            }            }
5145          }          }
5146          while ($token->{type} eq 'character') {          while ($token->{type} == CHARACTER_TOKEN) {
5147            $text .= $token->{data};            $text .= $token->{data};
5148            !!!next-token;            !!!next-token;
5149          }          }
# Line 3019  sub _tree_construction_main ($) { Line 5151  sub _tree_construction_main ($) {
5151            $el->manakai_append_text ($text);            $el->manakai_append_text ($text);
5152          }          }
5153                    
5154          $self->{content_model_flag} = 'PCDATA';          $self->{content_model} = PCDATA_CONTENT_MODEL;
5155                    
5156          if ($token->{type} eq 'end tag' and          if ($token->{type} == END_TAG_TOKEN and
5157              $token->{tag_name} eq $tag_name) {              $token->{tag_name} eq $tag_name) {
5158            ## Ignore the token            ## Ignore the token
5159          } else {          } else {
5160            !!!parse-error (type => 'in RCDATA:#'.$token->{type});            !!!parse-error (type => 'in RCDATA:#'.$token->{type});
5161          }          }
5162          !!!next-token;          !!!next-token;
5163          return;          redo B;
5164        } elsif ({        } elsif ({
5165                  iframe => 1,                  iframe => 1,
5166                  noembed => 1,                  noembed => 1,
5167                  noframes => 1,                  noframes => 1,
5168                  noscript => 0, ## TODO: 1 if scripting is enabled                  noscript => 0, ## TODO: 1 if scripting is enabled
5169                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5170          $parse_rcdata->('CDATA', $insert);          ## NOTE: There is an "as if in body" code clone.
5171          return;          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
5172            redo B;
5173        } elsif ($token->{tag_name} eq 'select') {        } elsif ($token->{tag_name} eq 'select') {
5174          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5175                    
5176          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5177                    
5178          $self->{insertion_mode} = 'in select';          $self->{insertion_mode} = IN_SELECT_IM;
5179          !!!next-token;          !!!next-token;
5180          return;          redo B;
5181        } elsif ({        } elsif ({
5182                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
5183                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 3054  sub _tree_construction_main ($) { Line 5187  sub _tree_construction_main ($) {
5187          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!parse-error (type => 'in body:'.$token->{tag_name});
5188          ## Ignore the token          ## Ignore the token
5189          !!!next-token;          !!!next-token;
5190          return;          redo B;
5191                    
5192          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
5193        } else {        } else {
# Line 3063  sub _tree_construction_main ($) { Line 5196  sub _tree_construction_main ($) {
5196          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5197                    
5198          !!!next-token;          !!!next-token;
5199          return;          redo B;
5200        }        }
5201      } elsif ($token->{type} eq 'end tag') {      } elsif ($token->{type} == END_TAG_TOKEN) {
5202        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
5203          if (@{$self->{open_elements}} > 1 and          if (@{$self->{open_elements}} > 1 and
5204              $self->{open_elements}->[1]->[1] eq 'body') {              $self->{open_elements}->[1]->[1] eq 'body') {
# Line 3079  sub _tree_construction_main ($) { Line 5212  sub _tree_construction_main ($) {
5212              }              }
5213            }            }
5214    
5215            $self->{insertion_mode} = 'after body';            $self->{insertion_mode} = AFTER_BODY_IM;
5216            !!!next-token;            !!!next-token;
5217            return;            redo B;
5218          } else {          } else {
5219            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5220            ## Ignore the token            ## Ignore the token
5221            !!!next-token;            !!!next-token;
5222            return;            redo B;
5223          }          }
5224        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
5225          if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {          if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {
# Line 3094  sub _tree_construction_main ($) { Line 5227  sub _tree_construction_main ($) {
5227            if ($self->{open_elements}->[-1]->[1] ne 'body') {            if ($self->{open_elements}->[-1]->[1] ne 'body') {
5228              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);
5229            }            }
5230            $self->{insertion_mode} = 'after body';            $self->{insertion_mode} = AFTER_BODY_IM;
5231            ## reprocess            ## reprocess
5232            return;            redo B;
5233          } else {          } else {
5234            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5235            ## Ignore the token            ## Ignore the token
5236            !!!next-token;            !!!next-token;
5237            return;            redo B;
5238          }          }
5239        } elsif ({        } elsif ({
5240                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
# Line 3126  sub _tree_construction_main ($) { Line 5259  sub _tree_construction_main ($) {
5259                   tbody => 1, tfoot=> 1, thead => 1,                   tbody => 1, tfoot=> 1, thead => 1,
5260                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
5261                !!!back-token;                !!!back-token;
5262                $token = {type => 'end tag',                $token = {type => END_TAG_TOKEN,
5263                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5264                return;                redo B;
5265              }              }
5266              $i = $_;              $i = $_;
5267              last INSCOPE unless $token->{tag_name} eq 'p';              last INSCOPE unless $token->{tag_name} eq 'p';
# Line 3161  sub _tree_construction_main ($) { Line 5294  sub _tree_construction_main ($) {
5294              button => 1, marquee => 1, object => 1,              button => 1, marquee => 1, object => 1,
5295            }->{$token->{tag_name}};            }->{$token->{tag_name}};
5296          !!!next-token;          !!!next-token;
5297          return;          redo B;
5298        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
5299          ## has an element in scope          ## has an element in scope
5300          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 3174  sub _tree_construction_main ($) { Line 5307  sub _tree_construction_main ($) {
5307                   tbody => 1, tfoot=> 1, thead => 1,                   tbody => 1, tfoot=> 1, thead => 1,
5308                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
5309                !!!back-token;                !!!back-token;
5310                $token = {type => 'end tag',                $token = {type => END_TAG_TOKEN,
5311                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5312                return;                redo B;
5313              }              }
5314              last INSCOPE;              last INSCOPE;
5315            } elsif ({            } elsif ({
# Line 3190  sub _tree_construction_main ($) { Line 5323  sub _tree_construction_main ($) {
5323          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {
5324            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
5325          } else {          } else {
5326            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5327          }          }
5328    
5329          undef $self->{form_element};          undef $self->{form_element};
5330          !!!next-token;          !!!next-token;
5331          return;          redo B;
5332        } elsif ({        } elsif ({
5333                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5334                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 3213  sub _tree_construction_main ($) { Line 5346  sub _tree_construction_main ($) {
5346                   tbody => 1, tfoot=> 1, thead => 1,                   tbody => 1, tfoot=> 1, thead => 1,
5347                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
5348                !!!back-token;                !!!back-token;
5349                $token = {type => 'end tag',                $token = {type => END_TAG_TOKEN,
5350                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5351                return;                redo B;
5352              }              }
5353              $i = $_;              $i = $_;
5354              last INSCOPE;              last INSCOPE;
# Line 3228  sub _tree_construction_main ($) { Line 5361  sub _tree_construction_main ($) {
5361          } # INSCOPE          } # INSCOPE
5362                    
5363          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
5364            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5365          }          }
5366                    
5367          splice @{$self->{open_elements}}, $i if defined $i;          splice @{$self->{open_elements}}, $i if defined $i;
5368          !!!next-token;          !!!next-token;
5369          return;          redo B;
5370        } elsif ({        } elsif ({
5371                  a => 1,                  a => 1,
5372                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
# Line 3241  sub _tree_construction_main ($) { Line 5374  sub _tree_construction_main ($) {
5374                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
5375                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5376          $formatting_end_tag->($token->{tag_name});          $formatting_end_tag->($token->{tag_name});
5377          return;          redo B;
5378        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
5379          !!!parse-error (type => 'unmatched end tag:br');          !!!parse-error (type => 'unmatched end tag:br');
5380    
# Line 3254  sub _tree_construction_main ($) { Line 5387  sub _tree_construction_main ($) {
5387                    
5388          ## Ignore the token.          ## Ignore the token.
5389          !!!next-token;          !!!next-token;
5390          return;          redo B;
5391        } elsif ({        } elsif ({
5392                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
5393                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 3270  sub _tree_construction_main ($) { Line 5403  sub _tree_construction_main ($) {
5403          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5404          ## Ignore the token          ## Ignore the token
5405          !!!next-token;          !!!next-token;
5406          return;          redo B;
5407                    
5408          ## ISSUE: Issue on HTML5 new elements in spec          ## ISSUE: Issue on HTML5 new elements in spec
5409                    
# Line 3287  sub _tree_construction_main ($) { Line 5420  sub _tree_construction_main ($) {
5420              if ({              if ({
5421                   dd => 1, dt => 1, li => 1, p => 1,                   dd => 1, dt => 1, li => 1, p => 1,
5422                   td => 1, th => 1, tr => 1,                   td => 1, th => 1, tr => 1,
5423                   tbody => 1, tfoot=> 1, thead => 1,                   tbody => 1, tfoot => 1, thead => 1,
5424                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
5425                !!!back-token;                !!!back-token;
5426                $token = {type => 'end tag',                $token = {type => END_TAG_TOKEN,
5427                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5428                return;                redo B;
5429              }              }
5430                    
5431              ## Step 2              ## Step 2
5432              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {
5433                  ## NOTE: <x><y></x>
5434                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
5435              }              }
5436                            
# Line 3325  sub _tree_construction_main ($) { Line 5459  sub _tree_construction_main ($) {
5459            ## Step 5;            ## Step 5;
5460            redo S2;            redo S2;
5461          } # S2          } # S2
5462          return;          redo B;
       }  
     }  
   }; # $in_body  
   
   B: {  
     if ($self->{insertion_mode} ne 'trailing end') {  
       if ($token->{type} eq 'DOCTYPE') {  
         !!!parse-error (type => 'in html:#DOCTYPE');  
         ## Ignore the token  
         ## Stay in the phase  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'start tag' and  
                $token->{tag_name} eq 'html') {  
 ## ISSUE: "aa<html>" is not a parse error.  
 ## ISSUE: "<html>" in fragment is not a parse error.  
         unless ($token->{first_start_tag}) {  
           !!!parse-error (type => 'not first start tag');  
         }  
         my $top_el = $self->{open_elements}->[0]->[0];  
         for my $attr_name (keys %{$token->{attributes}}) {  
           unless ($top_el->has_attribute_ns (undef, $attr_name)) {  
             $top_el->set_attribute_ns  
               (undef, [undef, $attr_name],  
                $token->{attributes}->{$attr_name}->{value});  
           }  
         }  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'end-of-file') {  
         ## 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]};  
           redo B;  
         }  
           
         if (@{$self->{open_elements}} > 2 or  
             (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         } elsif (defined $self->{inner_html_node} and  
                  @{$self->{open_elements}} > 1 and  
                  $self->{open_elements}->[1]->[1] ne 'body') {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         }  
   
         ## Stop parsing  
         last B;  
   
         ## ISSUE: There is an issue in the spec.  
       } else {  
         if ($self->{insertion_mode} eq 'before 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;  
               }  
             }  
             ## As if <head>  
             !!!create-element ($self->{head_element}, 'head');  
             $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});  
             push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
             $self->{insertion_mode} = 'in head';  
             ## reprocess  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             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';  
             if ($token->{tag_name} eq 'head') {  
               !!!next-token;  
             #} elsif ({  
             #          base => 1, link => 1, meta => 1,  
             #          script => 1, style => 1, title => 1,  
             #         }->{$token->{tag_name}}) {  
             #  ## reprocess  
             } else {  
               ## reprocess  
             }  
             redo B;  
           } elsif ($token->{type} eq 'end tag') {  
             if ({  
                  head => 1, body => 1, html => 1,  
                  p => 1, br => 1,  
                 }->{$token->{tag_name}}) {  
               ## As if <head>  
               !!!create-element ($self->{head_element}, 'head');  
               $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});  
               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;  
               }  
             }  
               
             #  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ({base => ($self->{insertion_mode} eq 'in head' or  
                           $self->{insertion_mode} eq 'after head'),  
                  link => 1}->{$token->{tag_name}}) {  
               ## NOTE: There is a "as if in head" code clone.  
               if ($self->{insertion_mode} eq 'after head') {  
                 !!!parse-error (type => 'after head:'.$token->{tag_name});  
                 push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
               }  
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.  
               pop @{$self->{open_elements}}  
                   if $self->{insertion_mode} eq 'after head';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'meta') {  
               ## NOTE: There is a "as if in head" code clone.  
               if ($self->{insertion_mode} eq 'after head') {  
                 !!!parse-error (type => 'after head:'.$token->{tag_name});  
                 push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
               }  
               !!!insert-element ($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  
               }  
   
               ## TODO: Extracting |charset| from |meta|.  
               pop @{$self->{open_elements}}  
                   if $self->{insertion_mode} eq 'after head';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'title' and  
                      $self->{insertion_mode} eq 'in head') {  
               ## NOTE: There is a "as if in head" code clone.  
               if ($self->{insertion_mode} eq 'after head') {  
                 !!!parse-error (type => 'after head:'.$token->{tag_name});  
                 push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
               }  
               my $parent = defined $self->{head_element} ? $self->{head_element}  
                   : $self->{open_elements}->[-1]->[0];  
               $parse_rcdata->('RCDATA', sub { $parent->append_child ($_[0]) });  
               pop @{$self->{open_elements}}  
                   if $self->{insertion_mode} eq 'after head';  
               redo B;  
             } elsif ($token->{tag_name} eq 'style') {  
               ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and  
               ## insertion mode 'in head')  
               ## NOTE: There is a "as if in head" code clone.  
               if ($self->{insertion_mode} eq 'after head') {  
                 !!!parse-error (type => 'after head:'.$token->{tag_name});  
                 push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
               }  
               $parse_rcdata->('CDATA', $insert_to_current);  
               pop @{$self->{open_elements}}  
                   if $self->{insertion_mode} eq 'after head';  
               redo B;  
             } elsif ($token->{tag_name} eq 'noscript') {  
               if ($self->{insertion_mode} eq 'in head') {  
                 ## NOTE: and scripting is disalbed  
                 !!!insert-element ($token->{tag_name}, $token->{attributes});  
                 $self->{insertion_mode} = 'in head noscript';  
                 !!!next-token;  
                 redo B;  
               } elsif ($self->{insertion_mode} eq 'in head noscript') {  
                 !!!parse-error (type => 'in noscript:noscript');  
                 ## Ignore the token  
                 redo B;  
               } else {  
                 #  
               }  
             } elsif ($token->{tag_name} eq 'head' and  
                      $self->{insertion_mode} ne 'after head') {  
               !!!parse-error (type => 'in head:head'); # or in head noscript  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } elsif ($self->{insertion_mode} ne 'in head noscript' and  
                      $token->{tag_name} eq 'script') {  
               if ($self->{insertion_mode} eq 'after head') {  
                 !!!parse-error (type => 'after head:'.$token->{tag_name});  
                 push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
               }  
               ## NOTE: There is a "as if in head" code clone.  
               $script_start_tag->($insert_to_current);  
               pop @{$self->{open_elements}}  
                   if $self->{insertion_mode} eq 'after head';  
               redo B;  
             } elsif ($self->{insertion_mode} eq 'after head' and  
                      $token->{tag_name} eq 'body') {  
               !!!insert-element ('body', $token->{attributes});  
               $self->{insertion_mode} = 'in body';  
               !!!next-token;  
               redo B;  
             } elsif ($self->{insertion_mode} eq 'after head' and  
                      $token->{tag_name} eq 'frameset') {  
               !!!insert-element ('frameset', $token->{attributes});  
               $self->{insertion_mode} = 'in frameset';  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($self->{insertion_mode} eq 'in head' and  
                 $token->{tag_name} eq 'head') {  
               pop @{$self->{open_elements}};  
               $self->{insertion_mode} = 'after head';  
               !!!next-token;  
               redo B;  
             } elsif ($self->{insertion_mode} eq 'in head noscript' and  
                 $token->{tag_name} eq 'noscript') {  
               pop @{$self->{open_elements}};  
               $self->{insertion_mode} = 'in head';  
               !!!next-token;  
               redo B;  
             } elsif ($self->{insertion_mode} eq 'in head' and  
                      {  
                       body => 1, html => 1,  
                       p => 1, br => 1,  
                      }->{$token->{tag_name}}) {  
               #  
             } elsif ($self->{insertion_mode} eq 'in head noscript' and  
                      {  
                       p => 1, br => 1,  
                      }->{$token->{tag_name}}) {  
               #  
             } elsif ($self->{insertion_mode} ne 'after head') {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
   
           ## As if </head> or </noscript> or <body>  
           if ($self->{insertion_mode} eq 'in head') {  
             pop @{$self->{open_elements}};  
             $self->{insertion_mode} = 'after head';  
           } elsif ($self->{insertion_mode} eq 'in head noscript') {  
             pop @{$self->{open_elements}};  
             !!!parse-error (type => 'in noscript:'.(defined $token->{tag_name} ? ($token->{type} eq 'end tag' ? '/' : '') . $token->{tag_name} : '#' . $token->{type}));  
             $self->{insertion_mode} = 'in head';  
           } else { # 'after head'  
             !!!insert-element ('body');  
             $self->{insertion_mode} = 'in body';  
           }  
           ## reprocess  
           redo B;  
   
           ## ISSUE: An issue in the spec.  
         } elsif ($self->{insertion_mode} eq 'in body') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: There is a code clone of "character in body".  
             $reconstruct_active_formatting_elements->($insert_to_current);  
               
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
   
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             ## NOTE: There is a code clone of "comment in body".  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } else {  
             $in_body->($insert_to_current);  
             redo B;  
           }  
         } elsif ($self->{insertion_mode} eq 'in table') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: There are "character in table" code clones.  
             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;  
               }  
             }  
   
             !!!parse-error (type => 'in table:#character');  
   
             ## As if in body, but insert into foster parent element  
             ## ISSUE: Spec says that "whenever a node would be inserted  
             ## into the current node" while characters might not be  
             ## result in a new Text node.  
             $reconstruct_active_formatting_elements->($insert_to_foster);  
               
             if ({  
                  table => 1, tbody => 1, tfoot => 1,  
                  thead => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               # MUST  
               my $foster_parent_element;  
               my $next_sibling;  
               my $prev_sibling;  
               OE: for (reverse 0..$#{$self->{open_elements}}) {  
                 if ($self->{open_elements}->[$_]->[1] eq 'table') {  
                   my $parent = $self->{open_elements}->[$_]->[0]->parent_node;  
                   if (defined $parent and $parent->node_type == 1) {  
                     $foster_parent_element = $parent;  
                     $next_sibling = $self->{open_elements}->[$_]->[0];  
                     $prev_sibling = $next_sibling->previous_sibling;  
                   } else {  
                     $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];  
                     $prev_sibling = $foster_parent_element->last_child;  
                   }  
                   last OE;  
                 }  
               } # OE  
               $foster_parent_element = $self->{open_elements}->[0]->[0] and  
               $prev_sibling = $foster_parent_element->last_child  
                 unless defined $foster_parent_element;  
               if (defined $prev_sibling and  
                   $prev_sibling->node_type == 3) {  
                 $prev_sibling->manakai_append_text ($token->{data});  
               } else {  
                 $foster_parent_element->insert_before  
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
               }  
             } else {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
             }  
               
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ({  
                  caption => 1,  
                  colgroup => 1,  
                  tbody => 1, tfoot => 1, thead => 1,  
                 }->{$token->{tag_name}}) {  
               ## Clear back to table context  
               while ($self->{open_elements}->[-1]->[1] ne 'table' and  
                      $self->{open_elements}->[-1]->[1] ne 'html') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               push @$active_formatting_elements, ['#marker', '']  
                 if $token->{tag_name} eq 'caption';  
   
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               $self->{insertion_mode} = {  
                                  caption => 'in caption',  
                                  colgroup => 'in column group',  
                                  tbody => 'in table body',  
                                  tfoot => 'in table body',  
                                  thead => 'in table body',  
                                 }->{$token->{tag_name}};  
               !!!next-token;  
               redo B;  
             } elsif ({  
                       col => 1,  
                       td => 1, th => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               ## Clear back to table context  
               while ($self->{open_elements}->[-1]->[1] ne 'table' and  
                      $self->{open_elements}->[-1]->[1] ne 'html') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               !!!insert-element ($token->{tag_name} eq 'col' ? 'colgroup' : 'tbody');  
               $self->{insertion_mode} = $token->{tag_name} eq 'col'  
                 ? 'in column group' : 'in table body';  
               ## reprocess  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## NOTE: There are code clones for this "table in table"  
               !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
   
               ## As if </table>  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'table') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:table');  
                 ## Ignore tokens </table><table>  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## 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; # <table>  
                 $token = {type => 'end tag', tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'table') {  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## 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  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               !!!next-token;  
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1, colgroup => 1,  
                       html => 1, tbody => 1, td => 1, tfoot => 1, th => 1,  
                       thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
   
           !!!parse-error (type => 'in table:'.$token->{tag_name});  
           $in_body->($insert_to_foster);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in caption') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a code clone of "character in body".  
             $reconstruct_active_formatting_elements->($insert_to_current);  
               
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
   
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             ## NOTE: This is a code clone of "comment in body".  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ({  
                  caption => 1, col => 1, colgroup => 1, tbody => 1,  
                  td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,  
                 }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'not closed:caption');  
   
               ## As if </caption>  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'caption') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:caption');  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## 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 => 'caption'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'caption') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
   
               $self->{insertion_mode} = 'in table';  
   
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'caption') {  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## 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  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'caption') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
   
               $self->{insertion_mode} = 'in table';  
   
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               !!!parse-error (type => 'not closed:caption');  
   
               ## As if </caption>  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'caption') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:caption');  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## 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; # </table>  
                 $token = {type => 'end tag', tag_name => 'caption'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'caption') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
   
               $self->{insertion_mode} = 'in table';  
   
               ## reprocess  
               redo B;  
             } elsif ({  
                       body => 1, col => 1, colgroup => 1,  
                       html => 1, tbody => 1, td => 1, tfoot => 1,  
                       th => 1, thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
                 
           $in_body->($insert_to_current);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in column group') {  
           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;  
               }  
             }  
               
             #  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'col') {  
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               pop @{$self->{open_elements}};  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'colgroup') {  
               if ($self->{open_elements}->[-1]->[1] eq 'html') {  
                 !!!parse-error (type => 'unmatched end tag:colgroup');  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               } else {  
                 pop @{$self->{open_elements}}; # colgroup  
                 $self->{insertion_mode} = 'in table';  
                 !!!next-token;  
                 redo B;              
               }  
             } elsif ($token->{tag_name} eq 'col') {  
               !!!parse-error (type => 'unmatched end tag:col');  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
   
           ## As if </colgroup>  
           if ($self->{open_elements}->[-1]->[1] eq 'html') {  
             !!!parse-error (type => 'unmatched end tag:colgroup');  
             ## Ignore the token  
             !!!next-token;  
             redo B;  
           } else {  
             pop @{$self->{open_elements}}; # colgroup  
             $self->{insertion_mode} = 'in table';  
             ## reprocess  
             redo B;  
           }  
         } elsif ($self->{insertion_mode} eq 'in table body') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a "character in table" code clone.  
             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;  
               }  
             }  
   
             !!!parse-error (type => 'in table:#character');  
   
             ## As if in body, but insert into foster parent element  
             ## ISSUE: Spec says that "whenever a node would be inserted  
             ## into the current node" while characters might not be  
             ## result in a new Text node.  
             $reconstruct_active_formatting_elements->($insert_to_foster);  
   
             if ({  
                  table => 1, tbody => 1, tfoot => 1,  
                  thead => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               # MUST  
               my $foster_parent_element;  
               my $next_sibling;  
               my $prev_sibling;  
               OE: for (reverse 0..$#{$self->{open_elements}}) {  
                 if ($self->{open_elements}->[$_]->[1] eq 'table') {  
                   my $parent = $self->{open_elements}->[$_]->[0]->parent_node;  
                   if (defined $parent and $parent->node_type == 1) {  
                     $foster_parent_element = $parent;  
                     $next_sibling = $self->{open_elements}->[$_]->[0];  
                     $prev_sibling = $next_sibling->previous_sibling;  
                   } else {  
                     $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];  
                     $prev_sibling = $foster_parent_element->last_child;  
                   }  
                   last OE;  
                 }  
               } # OE  
               $foster_parent_element = $self->{open_elements}->[0]->[0] and  
               $prev_sibling = $foster_parent_element->last_child  
                 unless defined $foster_parent_element;  
               if (defined $prev_sibling and  
                   $prev_sibling->node_type == 3) {  
                 $prev_sibling->manakai_append_text ($token->{data});  
               } else {  
                 $foster_parent_element->insert_before  
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
               }  
             } else {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
             }  
               
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             ## Copied from 'in table'  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ({  
                  tr => 1,  
                  th => 1, td => 1,  
                 }->{$token->{tag_name}}) {  
               unless ($token->{tag_name} eq 'tr') {  
                 !!!parse-error (type => 'missing start tag:tr');  
               }  
   
               ## Clear back to table body context  
               while (not {  
                 tbody => 1, tfoot => 1, thead => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
                 
               $self->{insertion_mode} = 'in row';  
               if ($token->{tag_name} eq 'tr') {  
                 !!!insert-element ($token->{tag_name}, $token->{attributes});  
                 !!!next-token;  
               } else {  
                 !!!insert-element ('tr');  
                 ## reprocess  
               }  
               redo B;  
             } elsif ({  
                       caption => 1, col => 1, colgroup => 1,  
                       tbody => 1, tfoot => 1, thead => 1,  
                      }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ({  
                      tbody => 1, thead => 1, tfoot => 1,  
                     }->{$node->[1]}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table body context  
               while (not {  
                 tbody => 1, tfoot => 1, thead => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               ## As if <{current node}>  
               ## have an element in table scope  
               ## true by definition  
   
               ## Clear back to table body context  
               ## nop by definition  
   
               pop @{$self->{open_elements}};  
               $self->{insertion_mode} = 'in table';  
               ## reprocess  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## NOTE: This is a code clone of "table in table"  
               !!!parse-error (type => 'not closed:table');  
   
               ## As if </table>  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'table') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:table');  
                 ## Ignore tokens </table><table>  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## 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; # <table>  
                 $token = {type => 'end tag', tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ({  
                  tbody => 1, tfoot => 1, thead => 1,  
                 }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table body context  
               while (not {  
                 tbody => 1, tfoot => 1, thead => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               pop @{$self->{open_elements}};  
               $self->{insertion_mode} = 'in table';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ({  
                      tbody => 1, thead => 1, tfoot => 1,  
                     }->{$node->[1]}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table body context  
               while (not {  
                 tbody => 1, tfoot => 1, thead => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               ## As if <{current node}>  
               ## have an element in table scope  
               ## true by definition  
   
               ## Clear back to table body context  
               ## nop by definition  
   
               pop @{$self->{open_elements}};  
               $self->{insertion_mode} = 'in table';  
               ## reprocess  
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1, colgroup => 1,  
                       html => 1, td => 1, th => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
             
           ## As if in table  
           !!!parse-error (type => 'in table:'.$token->{tag_name});  
           $in_body->($insert_to_foster);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in row') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a "character in table" code clone.  
             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;  
               }  
             }  
   
             !!!parse-error (type => 'in table:#character');  
   
             ## As if in body, but insert into foster parent element  
             ## ISSUE: Spec says that "whenever a node would be inserted  
             ## into the current node" while characters might not be  
             ## result in a new Text node.  
             $reconstruct_active_formatting_elements->($insert_to_foster);  
               
             if ({  
                  table => 1, tbody => 1, tfoot => 1,  
                  thead => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               # MUST  
               my $foster_parent_element;  
               my $next_sibling;  
               my $prev_sibling;  
               OE: for (reverse 0..$#{$self->{open_elements}}) {  
                 if ($self->{open_elements}->[$_]->[1] eq 'table') {  
                   my $parent = $self->{open_elements}->[$_]->[0]->parent_node;  
                   if (defined $parent and $parent->node_type == 1) {  
                     $foster_parent_element = $parent;  
                     $next_sibling = $self->{open_elements}->[$_]->[0];  
                     $prev_sibling = $next_sibling->previous_sibling;  
                   } else {  
                     $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];  
                     $prev_sibling = $foster_parent_element->last_child;  
                   }  
                   last OE;  
                 }  
               } # OE  
               $foster_parent_element = $self->{open_elements}->[0]->[0] and  
               $prev_sibling = $foster_parent_element->last_child  
                 unless defined $foster_parent_element;  
               if (defined $prev_sibling and  
                   $prev_sibling->node_type == 3) {  
                 $prev_sibling->manakai_append_text ($token->{data});  
               } else {  
                 $foster_parent_element->insert_before  
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
               }  
             } else {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
             }  
               
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             ## Copied from 'in table'  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'th' or  
                 $token->{tag_name} eq 'td') {  
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
                 
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               $self->{insertion_mode} = 'in cell';  
   
               push @$active_formatting_elements, ['#marker', ''];  
                 
               !!!next-token;  
               redo B;  
             } elsif ({  
                       caption => 1, col => 1, colgroup => 1,  
                       tbody => 1, tfoot => 1, thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               ## As if </tr>  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'tr') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               ## reprocess  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## NOTE: This is a code clone of "table in table"  
               !!!parse-error (type => 'not closed:table');  
   
               ## As if </table>  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'table') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:table');  
                 ## Ignore tokens </table><table>  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## 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; # <table>  
                 $token = {type => 'end tag', tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'tr') {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## As if </tr>  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'tr') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{type});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               ## reprocess  
               redo B;  
             } elsif ({  
                       tbody => 1, tfoot => 1, thead => 1,  
                      }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## As if </tr>  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'tr') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:tr');  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               ## reprocess  
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1,  
                       colgroup => 1, html => 1, td => 1, th => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
   
           ## As if in table  
           !!!parse-error (type => 'in table:'.$token->{tag_name});  
           $in_body->($insert_to_foster);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in cell') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a code clone of "character in body".  
             $reconstruct_active_formatting_elements->($insert_to_current);  
               
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
   
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             ## NOTE: This is a code clone of "comment in body".  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ({  
                  caption => 1, col => 1, colgroup => 1,  
                  tbody => 1, td => 1, tfoot => 1, th => 1,  
                  thead => 1, tr => 1,  
                 }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $tn;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'td' or $node->[1] eq 'th') {  
                   $tn = $node->[1];  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $tn) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Close the cell  
               !!!back-token; # <?>  
               $token = {type => 'end tag', tag_name => $tn};  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => ($token->{tag_name} eq 'th'),  
                    th => ($token->{tag_name} eq 'td'),  
                    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  
                 redo B;  
               }  
   
               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;  
   
               $clear_up_to_marker->();  
   
               $self->{insertion_mode} = 'in row';  
   
               !!!next-token;  
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1,  
                       colgroup => 1, html => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } elsif ({  
                       table => 1, tbody => 1, tfoot => 1,  
                       thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $i;  
               my $tn;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {  
                   $tn = $node->[1];  
                   ## NOTE: There is exactly one |td| or |th| element  
                   ## in scope in the stack of open elements by definition.  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => 'end tag', tag_name => $tn};  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
             
           $in_body->($insert_to_current);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in select') {  
           if ($token->{type} eq 'character') {  
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'option') {  
               if ($self->{open_elements}->[-1]->[1] eq 'option') {  
                 ## As if </option>  
                 pop @{$self->{open_elements}};  
               }  
   
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'optgroup') {  
               if ($self->{open_elements}->[-1]->[1] eq 'option') {  
                 ## As if </option>  
                 pop @{$self->{open_elements}};  
               }  
   
               if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {  
                 ## As if </optgroup>  
                 pop @{$self->{open_elements}};  
               }  
   
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'select') {  
               !!!parse-error (type => 'not closed:select');  
               ## As if </select> instead  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:select');  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'optgroup') {  
               if ($self->{open_elements}->[-1]->[1] eq 'option' and  
                   $self->{open_elements}->[-2]->[1] eq 'optgroup') {  
                 ## As if </option>  
                 splice @{$self->{open_elements}}, -2;  
               } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {  
                 pop @{$self->{open_elements}};  
               } else {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
               }  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'option') {  
               if ($self->{open_elements}->[-1]->[1] eq 'option') {  
                 pop @{$self->{open_elements}};  
               } else {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
               }  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'select') {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               !!!next-token;  
               redo B;  
             } elsif ({  
                       caption => 1, table => 1, tbody => 1,  
                       tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## As if </select>  
               ## have an element in table scope  
               undef $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'select') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:select');  
                 ## Ignore the </select> token  
                 !!!next-token; ## TODO: ok?  
                 redo B;  
               }  
                 
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
   
           !!!parse-error (type => 'in select:'.$token->{tag_name});  
           ## Ignore the token  
           !!!next-token;  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'after body') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               my $data = $1;  
               ## As if in body  
               $reconstruct_active_formatting_elements->($insert_to_current);  
                 
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
   
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
               
             #  
             !!!parse-error (type => 'after body:#'.$token->{type});  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[0]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             !!!parse-error (type => 'after body:'.$token->{tag_name});  
             #  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'html') {  
               if (defined $self->{inner_html_node}) {  
                 !!!parse-error (type => 'unmatched end tag:html');  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               } else {  
                 $previous_insertion_mode = $self->{insertion_mode};  
                 $self->{insertion_mode} = 'trailing end';  
                 !!!next-token;  
                 redo B;  
               }  
             } else {  
               !!!parse-error (type => 'after body:/'.$token->{tag_name});  
             }  
           } else {  
             !!!parse-error (type => 'after body:#'.$token->{type});  
           }  
   
           $self->{insertion_mode} = 'in body';  
           ## reprocess  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in frameset') {  
           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;  
               }  
             }  
   
             #  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'frameset') {  
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'frame') {  
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               pop @{$self->{open_elements}};  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'noframes') {  
               $in_body->($insert_to_current);  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'frameset') {  
               if ($self->{open_elements}->[-1]->[1] eq 'html' and  
                   @{$self->{open_elements}} == 1) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
               } else {  
                 pop @{$self->{open_elements}};  
                 !!!next-token;  
               }  
                 
               ## if not inner_html and  
               if ($self->{open_elements}->[-1]->[1] ne 'frameset') {  
                 $self->{insertion_mode} = 'after frameset';  
               }  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
             
           if (defined $token->{tag_name}) {  
             !!!parse-error (type => 'in frameset:'.($token->{type} eq 'end tag' ? '/' : '').$token->{tag_name});  
           } else {  
             !!!parse-error (type => 'in frameset:#'.$token->{type});  
           }  
           ## Ignore the token  
           !!!next-token;  
           redo B;  
         } 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);  
   
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
   
             if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {  
               !!!parse-error (type => 'after frameset:#character');  
   
               ## Ignore the token.  
               if (length $token->{data}) {  
                 ## reprocess the rest of characters  
               } else {  
                 !!!next-token;  
               }  
               redo B;  
             }  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'noframes') {  
               $in_body->($insert_to_current);  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'html') {  
               $previous_insertion_mode = $self->{insertion_mode};  
               $self->{insertion_mode} = 'trailing end';  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             die "$0: $token->{type}: Unknown token type";  
           }  
             
           !!!parse-error (type => 'after frameset:'.($token->{tag_name} eq 'end tag' ? '/' : '').$token->{tag_name});  
           ## Ignore the token  
           !!!next-token;  
           redo B;  
   
           ## ISSUE: An issue in spec there  
         } else {  
           die "$0: $self->{insertion_mode}: Unknown insertion mode";  
         }  
       }  
     } elsif ($self->{insertion_mode} eq 'trailing end') {  
       ## states in the main stage is preserved yet # MUST  
         
       if ($token->{type} eq 'DOCTYPE') {  
         !!!parse-error (type => 'after html:#DOCTYPE');  
         ## Ignore the token  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'comment') {  
         my $comment = $self->{document}->create_comment ($token->{data});  
         $self->{document}->append_child ($comment);  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'character') {  
         if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
           my $data = $1;  
           ## As if in the main phase.  
           ## NOTE: The insertion mode in the main phase  
           ## just before the phase has been changed to the trailing  
           ## end phase is either "after body" or "after frameset".  
           $reconstruct_active_formatting_elements->($insert_to_current);  
             
           $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);  
             
           unless (length $token->{data}) {  
             !!!next-token;  
             redo B;  
           }  
         }  
   
         !!!parse-error (type => 'after html:#character');  
         $self->{insertion_mode} = $previous_insertion_mode;  
         ## reprocess  
         redo B;  
       } elsif ($token->{type} eq 'start tag' or  
                $token->{type} eq 'end tag') {  
         !!!parse-error (type => 'after html:'.($token->{type} eq 'end tag' ? '/' : '').$token->{tag_name});  
         $self->{insertion_mode} = $previous_insertion_mode;  
         ## reprocess  
         redo B;  
       } elsif ($token->{type} eq 'end-of-file') {  
         ## Stop parsing  
         last B;  
       } else {  
         die "$0: $token->{type}: Unknown token";  
5463        }        }
5464      }      }
5465        redo B;
5466    } # B    } # B
5467    
5468      ## NOTE: The "trailing end" phase in HTML5 is split into
5469      ## two insertion modes: "after html body" and "after html frameset".
5470      ## NOTE: States in the main stage is preserved while
5471      ## the parser stays in the trailing end phase. # MUST
5472    
5473    ## Stop parsing # MUST    ## Stop parsing # MUST
5474        
5475    ## TODO: script stuffs    ## TODO: script stuffs
# Line 5268  sub set_inner_html ($$$) { Line 5481  sub set_inner_html ($$$) {
5481    my $s = \$_[0];    my $s = \$_[0];
5482    my $onerror = $_[1];    my $onerror = $_[1];
5483    
5484      ## ISSUE: Should {confident} be true?
5485    
5486    my $nt = $node->node_type;    my $nt = $node->node_type;
5487    if ($nt == 9) {    if ($nt == 9) {
5488      # MUST      # MUST
# Line 5340  sub set_inner_html ($$$) { Line 5555  sub set_inner_html ($$$) {
5555      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
5556    
5557      ## Step 2      ## Step 2
5558      my $node_ln = $node->local_name;      my $node_ln = $node->manakai_local_name;
5559      $p->{content_model_flag} = {      $p->{content_model} = {
5560        title => 'RCDATA',        title => RCDATA_CONTENT_MODEL,
5561        textarea => 'RCDATA',        textarea => RCDATA_CONTENT_MODEL,
5562        style => 'CDATA',        style => CDATA_CONTENT_MODEL,
5563        script => 'CDATA',        script => CDATA_CONTENT_MODEL,
5564        xmp => 'CDATA',        xmp => CDATA_CONTENT_MODEL,
5565        iframe => 'CDATA',        iframe => CDATA_CONTENT_MODEL,
5566        noembed => 'CDATA',        noembed => CDATA_CONTENT_MODEL,
5567        noframes => 'CDATA',        noframes => CDATA_CONTENT_MODEL,
5568        noscript => 'CDATA',        noscript => CDATA_CONTENT_MODEL,
5569        plaintext => 'PLAINTEXT',        plaintext => PLAINTEXT_CONTENT_MODEL,
5570      }->{$node_ln} || 'PCDATA';      }->{$node_ln};
5571         ## ISSUE: What is "the name of the element"? local name?      $p->{content_model} = PCDATA_CONTENT_MODEL
5572            unless defined $p->{content_model};
5573            ## ISSUE: What is "the name of the element"? local name?
5574    
5575      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $node_ln];
5576    
# Line 5378  sub set_inner_html ($$$) { Line 5595  sub set_inner_html ($$$) {
5595        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
5596          my $nsuri = $anode->namespace_uri;          my $nsuri = $anode->namespace_uri;
5597          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
5598            if ($anode->local_name eq 'form') { ## TODO: case?            if ($anode->manakai_local_name eq 'form') {
5599              $p->{form_element} = $anode;              $p->{form_element} = $anode;
5600              last AN;              last AN;
5601            }            }
# Line 5418  sub set_inner_html ($$$) { Line 5635  sub set_inner_html ($$$) {
5635    
5636  } # tree construction stage  } # tree construction stage
5637    
5638  sub get_inner_html ($$$) {  package Whatpm::HTML::RestartParser;
5639    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  
5640    
5641  1;  1;
5642  # $Date$  # $Date$

Legend:
Removed from v.1.35  
changed lines
  Added in v.1.73

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24