/[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.33 by wakaba, Sat Jul 7 13:41:05 2007 UTC revision 1.68 by wakaba, Sun Feb 17 12:18:06 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);
# Line 84  my $formatting_category = { Line 85  my $formatting_category = {
85  };  };
86  # $phrasing_category: all other elements  # $phrasing_category: all other elements
87    
88    sub parse_byte_string ($$$$;$) {
89      my $self = ref $_[0] ? shift : shift->new;
90      my $charset = shift;
91      my $bytes_s = ref $_[0] ? $_[0] : \($_[0]);
92      my $s;
93      
94      if (defined $charset) {
95        require Encode; ## TODO: decode(utf8) don't delete BOM
96        $s = \ (Encode::decode ($charset, $$bytes_s));
97        $self->{input_encoding} = lc $charset; ## TODO: normalize name
98        $self->{confident} = 1;
99      } else {
100        ## TODO: Implement HTML5 detection algorithm
101        require Whatpm::Charset::UniversalCharDet;
102        $charset = Whatpm::Charset::UniversalCharDet->detect_byte_string
103            (substr ($$bytes_s, 0, 1024));
104        $charset ||= 'windows-1252';
105        $s = \ (Encode::decode ($charset, $$bytes_s));
106        $self->{input_encoding} = $charset;
107        $self->{confident} = 0;
108      }
109    
110      $self->{change_encoding} = sub {
111        my $self = shift;
112        my $charset = lc shift;
113        ## TODO: if $charset is supported
114        ## TODO: normalize charset name
115    
116        ## "Change the encoding" algorithm:
117    
118        ## Step 1    
119        if ($charset eq 'utf-16') { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?
120          $charset = 'utf-8';
121        }
122    
123        ## Step 2
124        if (defined $self->{input_encoding} and
125            $self->{input_encoding} eq $charset) {
126          $self->{confident} = 1;
127          return;
128        }
129    
130        !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.
131            ':'.$charset, level => 'w');
132    
133        ## Step 3
134        # if (can) {
135          ## change the encoding on the fly.
136          #$self->{confident} = 1;
137          #return;
138        # }
139    
140        ## Step 4
141        throw Whatpm::HTML::RestartParser (charset => $charset);
142      }; # $self->{change_encoding}
143    
144      my @args = @_; shift @args; # $s
145      my $return;
146      try {
147        $return = $self->parse_char_string ($s, @args);  
148      } catch Whatpm::HTML::RestartParser with {
149        my $charset = shift->{charset};
150        $s = \ (Encode::decode ($charset, $$bytes_s));    
151        $self->{input_encoding} = $charset; ## TODO: normalize
152        $self->{confident} = 1;
153        $return = $self->parse_char_string ($s, @args);
154      };
155      return $return;
156    } # parse_byte_string
157    
158    *parse_char_string = \&parse_string;
159    
160  sub parse_string ($$$;$) {  sub parse_string ($$$;$) {
161    my $self = shift->new;    my $self = ref $_[0] ? shift : shift->new;
162    my $s = \$_[0];    my $s = ref $_[0] ? $_[0] : \($_[0]);
163    $self->{document} = $_[1];    $self->{document} = $_[1];
164      @{$self->{document}->child_nodes} = ();
165    
166    ## NOTE: |set_inner_html| copies most of this method's code    ## NOTE: |set_inner_html| copies most of this method's code
167    
168      $self->{confident} = 1 unless exists $self->{confident};
169      $self->{document}->input_encoding ($self->{input_encoding})
170          if defined $self->{input_encoding};
171    
172    my $i = 0;    my $i = 0;
173    my $line = 1;    my $line = 1;
174    my $column = 0;    my $column = 0;
# Line 147  sub new ($) { Line 225  sub new ($) {
225    $self->{parse_error} = sub {    $self->{parse_error} = sub {
226      #      #
227    };    };
228      $self->{change_encoding} = sub {
229        # if ($_[0] is a supported encoding) {
230        #   run "change the encoding" algorithm;
231        #   throw Whatpm::HTML::RestartParser (charset => $new_encoding);
232        # }
233      };
234      $self->{application_cache_selection} = sub {
235        #
236      };
237    return $self;    return $self;
238  } # new  } # new
239    
240    sub CM_ENTITY () { 0b001 } # & markup in data
241    sub CM_LIMITED_MARKUP () { 0b010 } # < markup in data (limited)
242    sub CM_FULL_MARKUP () { 0b100 } # < markup in data (any)
243    
244    sub PLAINTEXT_CONTENT_MODEL () { 0 }
245    sub CDATA_CONTENT_MODEL () { CM_LIMITED_MARKUP }
246    sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }
247    sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
248    
249    sub DATA_STATE () { 0 }
250    sub ENTITY_DATA_STATE () { 1 }
251    sub TAG_OPEN_STATE () { 2 }
252    sub CLOSE_TAG_OPEN_STATE () { 3 }
253    sub TAG_NAME_STATE () { 4 }
254    sub BEFORE_ATTRIBUTE_NAME_STATE () { 5 }
255    sub ATTRIBUTE_NAME_STATE () { 6 }
256    sub AFTER_ATTRIBUTE_NAME_STATE () { 7 }
257    sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 }
258    sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
259    sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
260    sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
261    sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
262    sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
263    sub COMMENT_START_STATE () { 14 }
264    sub COMMENT_START_DASH_STATE () { 15 }
265    sub COMMENT_STATE () { 16 }
266    sub COMMENT_END_STATE () { 17 }
267    sub COMMENT_END_DASH_STATE () { 18 }
268    sub BOGUS_COMMENT_STATE () { 19 }
269    sub DOCTYPE_STATE () { 20 }
270    sub BEFORE_DOCTYPE_NAME_STATE () { 21 }
271    sub DOCTYPE_NAME_STATE () { 22 }
272    sub AFTER_DOCTYPE_NAME_STATE () { 23 }
273    sub BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 24 }
274    sub DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE () { 25 }
275    sub DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE () { 26 }
276    sub AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 27 }
277    sub BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 28 }
278    sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE () { 29 }
279    sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }
280    sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
281    sub BOGUS_DOCTYPE_STATE () { 32 }
282    
283    sub DOCTYPE_TOKEN () { 1 }
284    sub COMMENT_TOKEN () { 2 }
285    sub START_TAG_TOKEN () { 3 }
286    sub END_TAG_TOKEN () { 4 }
287    sub END_OF_FILE_TOKEN () { 5 }
288    sub CHARACTER_TOKEN () { 6 }
289    
290    sub AFTER_HTML_IMS () { 0b100 }
291    sub HEAD_IMS ()       { 0b1000 }
292    sub BODY_IMS ()       { 0b10000 }
293    sub BODY_TABLE_IMS () { 0b100000 }
294    sub TABLE_IMS ()      { 0b1000000 }
295    sub ROW_IMS ()        { 0b10000000 }
296    sub BODY_AFTER_IMS () { 0b100000000 }
297    sub FRAME_IMS ()      { 0b1000000000 }
298    
299    sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
300    sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
301    sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
302    sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
303    sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
304    sub BEFORE_HEAD_IM () { HEAD_IMS | 0b11 }
305    sub IN_BODY_IM () { BODY_IMS }
306    sub IN_CELL_IM () { BODY_IMS | BODY_TABLE_IMS | 0b01 }
307    sub IN_CAPTION_IM () { BODY_IMS | BODY_TABLE_IMS | 0b10 }
308    sub IN_ROW_IM () { TABLE_IMS | ROW_IMS | 0b01 }
309    sub IN_TABLE_BODY_IM () { TABLE_IMS | ROW_IMS | 0b10 }
310    sub IN_TABLE_IM () { TABLE_IMS }
311    sub AFTER_BODY_IM () { BODY_AFTER_IMS }
312    sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
313    sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
314    sub IN_SELECT_IM () { 0b01 }
315    sub IN_COLUMN_GROUP_IM () { 0b10 }
316    
317  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
318    
319  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
320    my $self = shift;    my $self = shift;
321    $self->{state} = 'data'; # MUST    $self->{state} = DATA_STATE; # MUST
322    $self->{content_model_flag} = 'PCDATA'; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
323    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE
324    undef $self->{current_attribute};    undef $self->{current_attribute};
325    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
# Line 168  sub _initialize_tokenizer ($) { Line 332  sub _initialize_tokenizer ($) {
332  } # _initialize_tokenizer  } # _initialize_tokenizer
333    
334  ## A token has:  ## A token has:
335  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',  ##   ->{type} == DOCTYPE_TOKEN, START_TAG_TOKEN, END_TAG_TOKEN, COMMENT_TOKEN,
336  ##       'character', or 'end-of-file'  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN
337  ##   ->{name} (DOCTYPE, start tag (tag name), end tag (tag name))  ##   ->{name} (DOCTYPE_TOKEN)
338  ##   ->{public_identifier} (DOCTYPE)  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
339  ##   ->{system_identifier} (DOCTYPE)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
340  ##   ->{correct} == 1 or 0 (DOCTYPE)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
341  ##   ->{attributes} isa HASH (start tag, end tag)  ##   ->{correct} == 1 or 0 (DOCTYPE_TOKEN)
342  ##   ->{data} (comment, character)  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
343    ##        ->{name}
344    ##        ->{value}
345    ##        ->{has_reference} == 1 or 0
346    ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
347    
348  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
349    
# Line 185  sub _initialize_tokenizer ($) { Line 353  sub _initialize_tokenizer ($) {
353  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
354  ## and removed from the list.  ## and removed from the list.
355    
356    ## NOTE: HTML5 "Writing HTML documents" section, applied to
357    ## documents and not to user agents and conformance checkers,
358    ## contains some requirements that are not detected by the
359    ## parsing algorithm:
360    ## - Some requirements on character encoding declarations. ## TODO
361    ## - "Elements MUST NOT contain content that their content model disallows."
362    ##   ... Some are parse error, some are not (will be reported by c.c.).
363    ## - Polytheistic slash SHOULD NOT be used. (Applied only to atheists.) ## TODO
364    ## - Text (in elements, attributes, and comments) SHOULD NOT contain
365    ##   control characters other than space characters. ## TODO: (what is control character? C0, C1 and DEL?  Unicode control character?)
366    
367    ## TODO: HTML5 poses authors two SHOULD-level requirements that cannot
368    ## be detected by the HTML5 parsing algorithm:
369    ## - Text,
370    
371  sub _get_next_token ($) {  sub _get_next_token ($) {
372    my $self = shift;    my $self = shift;
373    if (@{$self->{token}}) {    if (@{$self->{token}}) {
# Line 192  sub _get_next_token ($) { Line 375  sub _get_next_token ($) {
375    }    }
376    
377    A: {    A: {
378      if ($self->{state} eq 'data') {      if ($self->{state} == DATA_STATE) {
379        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_input_character} == 0x0026) { # &
380          if ($self->{content_model_flag} eq 'PCDATA' or          if ($self->{content_model} & CM_ENTITY) { # PCDATA | RCDATA
381              $self->{content_model_flag} eq 'RCDATA') {            $self->{state} = ENTITY_DATA_STATE;
           $self->{state} = 'entity data';  
382            !!!next-input-character;            !!!next-input-character;
383            redo A;            redo A;
384          } else {          } else {
385            #            #
386          }          }
387        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_input_character} == 0x002D) { # -
388          if ($self->{content_model_flag} eq 'RCDATA' or          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
             $self->{content_model_flag} eq 'CDATA') {  
389            unless ($self->{escape}) {            unless ($self->{escape}) {
390              if ($self->{prev_input_character}->[0] == 0x002D and # -              if ($self->{prev_input_character}->[0] == 0x002D and # -
391                  $self->{prev_input_character}->[1] == 0x0021 and # !                  $self->{prev_input_character}->[1] == 0x0021 and # !
# Line 216  sub _get_next_token ($) { Line 397  sub _get_next_token ($) {
397                    
398          #          #
399        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_input_character} == 0x003C) { # <
400          if ($self->{content_model_flag} eq 'PCDATA' or          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
401              (($self->{content_model_flag} eq 'CDATA' or              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
               $self->{content_model_flag} eq 'RCDATA') and  
402               not $self->{escape})) {               not $self->{escape})) {
403            $self->{state} = 'tag open';            $self->{state} = TAG_OPEN_STATE;
404            !!!next-input-character;            !!!next-input-character;
405            redo A;            redo A;
406          } else {          } else {
# Line 228  sub _get_next_token ($) { Line 408  sub _get_next_token ($) {
408          }          }
409        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
410          if ($self->{escape} and          if ($self->{escape} and
411              ($self->{content_model_flag} eq 'RCDATA' or              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
              $self->{content_model_flag} eq 'CDATA')) {  
412            if ($self->{prev_input_character}->[0] == 0x002D and # -            if ($self->{prev_input_character}->[0] == 0x002D and # -
413                $self->{prev_input_character}->[1] == 0x002D) { # -                $self->{prev_input_character}->[1] == 0x002D) { # -
414              delete $self->{escape};              delete $self->{escape};
# Line 238  sub _get_next_token ($) { Line 417  sub _get_next_token ($) {
417                    
418          #          #
419        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
420          !!!emit ({type => 'end-of-file'});          !!!emit ({type => END_OF_FILE_TOKEN});
421          last A; ## TODO: ok?          last A; ## TODO: ok?
422        }        }
423        # Anything else        # Anything else
424        my $token = {type => 'character',        my $token = {type => CHARACTER_TOKEN,
425                     data => chr $self->{next_input_character}};                     data => chr $self->{next_input_character}};
426        ## Stay in the data state        ## Stay in the data state
427        !!!next-input-character;        !!!next-input-character;
# Line 250  sub _get_next_token ($) { Line 429  sub _get_next_token ($) {
429        !!!emit ($token);        !!!emit ($token);
430    
431        redo A;        redo A;
432      } elsif ($self->{state} eq 'entity data') {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
433        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
434                
435        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);
436    
437        $self->{state} = 'data';        $self->{state} = DATA_STATE;
438        # next-input-character is already done        # next-input-character is already done
439    
440        unless (defined $token) {        unless (defined $token) {
441          !!!emit ({type => 'character', data => '&'});          !!!emit ({type => CHARACTER_TOKEN, data => '&'});
442        } else {        } else {
443          !!!emit ($token);          !!!emit ($token);
444        }        }
445    
446        redo A;        redo A;
447      } elsif ($self->{state} eq 'tag open') {      } elsif ($self->{state} == TAG_OPEN_STATE) {
448        if ($self->{content_model_flag} eq 'RCDATA' or        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
           $self->{content_model_flag} eq 'CDATA') {  
449          if ($self->{next_input_character} == 0x002F) { # /          if ($self->{next_input_character} == 0x002F) { # /
450            !!!next-input-character;            !!!next-input-character;
451            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
452            redo A;            redo A;
453          } else {          } else {
454            ## reconsume            ## reconsume
455            $self->{state} = 'data';            $self->{state} = DATA_STATE;
456    
457            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<'});
458    
459            redo A;            redo A;
460          }          }
461        } elsif ($self->{content_model_flag} eq 'PCDATA') {        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
462          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_input_character} == 0x0021) { # !
463            $self->{state} = 'markup declaration open';            $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
464            !!!next-input-character;            !!!next-input-character;
465            redo A;            redo A;
466          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{next_input_character} == 0x002F) { # /
467            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
468            !!!next-input-character;            !!!next-input-character;
469            redo A;            redo A;
470          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_input_character} and
471                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{next_input_character} <= 0x005A) { # A..Z
472            $self->{current_token}            $self->{current_token}
473              = {type => 'start tag',              = {type => START_TAG_TOKEN,
474                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_input_character} + 0x0020)};
475            $self->{state} = 'tag name';            $self->{state} = TAG_NAME_STATE;
476            !!!next-input-character;            !!!next-input-character;
477            redo A;            redo A;
478          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_input_character} and
479                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_input_character} <= 0x007A) { # a..z
480            $self->{current_token} = {type => 'start tag',            $self->{current_token} = {type => START_TAG_TOKEN,
481                              tag_name => chr ($self->{next_input_character})};                              tag_name => chr ($self->{next_input_character})};
482            $self->{state} = 'tag name';            $self->{state} = TAG_NAME_STATE;
483            !!!next-input-character;            !!!next-input-character;
484            redo A;            redo A;
485          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_input_character} == 0x003E) { # >
486            !!!parse-error (type => 'empty start tag');            !!!parse-error (type => 'empty start tag');
487            $self->{state} = 'data';            $self->{state} = DATA_STATE;
488            !!!next-input-character;            !!!next-input-character;
489    
490            !!!emit ({type => 'character', data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});
491    
492            redo A;            redo A;
493          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_input_character} == 0x003F) { # ?
494            !!!parse-error (type => 'pio');            !!!parse-error (type => 'pio');
495            $self->{state} = 'bogus comment';            $self->{state} = BOGUS_COMMENT_STATE;
496            ## $self->{next_input_character} is intentionally left as is            ## $self->{next_input_character} is intentionally left as is
497            redo A;            redo A;
498          } else {          } else {
499            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago');
500            $self->{state} = 'data';            $self->{state} = DATA_STATE;
501            ## reconsume            ## reconsume
502    
503            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<'});
504    
505            redo A;            redo A;
506          }          }
507        } else {        } else {
508          die "$0: $self->{content_model_flag}: Unknown content model flag";          die "$0: $self->{content_model} in tag open";
509        }        }
510      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
511        if ($self->{content_model_flag} eq 'RCDATA' or        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
           $self->{content_model_flag} eq 'CDATA') {  
512          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
513            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
514            my @next_char;            my @next_char;
# Line 345  sub _get_next_token ($) { Line 522  sub _get_next_token ($) {
522              } else {              } else {
523                $self->{next_input_character} = shift @next_char; # reconsume                $self->{next_input_character} = shift @next_char; # reconsume
524                !!!back-next-input-character (@next_char);                !!!back-next-input-character (@next_char);
525                $self->{state} = 'data';                $self->{state} = DATA_STATE;
526    
527                !!!emit ({type => 'character', data => '</'});                !!!emit ({type => CHARACTER_TOKEN, data => '</'});
528        
529                redo A;                redo A;
530              }              }
# Line 364  sub _get_next_token ($) { Line 541  sub _get_next_token ($) {
541                    $self->{next_input_character} == -1) {                    $self->{next_input_character} == -1) {
542              $self->{next_input_character} = shift @next_char; # reconsume              $self->{next_input_character} = shift @next_char; # reconsume
543              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
544              $self->{state} = 'data';              $self->{state} = DATA_STATE;
545              !!!emit ({type => 'character', data => '</'});              !!!emit ({type => CHARACTER_TOKEN, data => '</'});
546              redo A;              redo A;
547            } else {            } else {
548              $self->{next_input_character} = shift @next_char;              $self->{next_input_character} = shift @next_char;
# Line 375  sub _get_next_token ($) { Line 552  sub _get_next_token ($) {
552          } else {          } else {
553            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
554            # next-input-character is already done            # next-input-character is already done
555            $self->{state} = 'data';            $self->{state} = DATA_STATE;
556            !!!emit ({type => 'character', data => '</'});            !!!emit ({type => CHARACTER_TOKEN, data => '</'});
557            redo A;            redo A;
558          }          }
559        }        }
560                
561        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_input_character} and
562            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_input_character} <= 0x005A) { # A..Z
563          $self->{current_token} = {type => 'end tag',          $self->{current_token} = {type => END_TAG_TOKEN,
564                            tag_name => chr ($self->{next_input_character} + 0x0020)};                            tag_name => chr ($self->{next_input_character} + 0x0020)};
565          $self->{state} = 'tag name';          $self->{state} = TAG_NAME_STATE;
566          !!!next-input-character;          !!!next-input-character;
567          redo A;          redo A;
568        } elsif (0x0061 <= $self->{next_input_character} and        } elsif (0x0061 <= $self->{next_input_character} and
569                 $self->{next_input_character} <= 0x007A) { # a..z                 $self->{next_input_character} <= 0x007A) { # a..z
570          $self->{current_token} = {type => 'end tag',          $self->{current_token} = {type => END_TAG_TOKEN,
571                            tag_name => chr ($self->{next_input_character})};                            tag_name => chr ($self->{next_input_character})};
572          $self->{state} = 'tag name';          $self->{state} = TAG_NAME_STATE;
573          !!!next-input-character;          !!!next-input-character;
574          redo A;          redo A;
575        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
576          !!!parse-error (type => 'empty end tag');          !!!parse-error (type => 'empty end tag');
577          $self->{state} = 'data';          $self->{state} = DATA_STATE;
578          !!!next-input-character;          !!!next-input-character;
579          redo A;          redo A;
580        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
581          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
582          $self->{state} = 'data';          $self->{state} = DATA_STATE;
583          # reconsume          # reconsume
584    
585          !!!emit ({type => 'character', data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</'});
586    
587          redo A;          redo A;
588        } else {        } else {
589          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
590          $self->{state} = 'bogus comment';          $self->{state} = BOGUS_COMMENT_STATE;
591          ## $self->{next_input_character} is intentionally left as is          ## $self->{next_input_character} is intentionally left as is
592          redo A;          redo A;
593        }        }
594      } elsif ($self->{state} eq 'tag name') {      } elsif ($self->{state} == TAG_NAME_STATE) {
595        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
596            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
597            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
598            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
599            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
600          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
601          !!!next-input-character;          !!!next-input-character;
602          redo A;          redo A;
603        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
604          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
605            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
606                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
607            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
608          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
609            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
610            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
611              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
612            }            }
613          } else {          } else {
614            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
615          }          }
616          $self->{state} = 'data';          $self->{state} = DATA_STATE;
617          !!!next-input-character;          !!!next-input-character;
618    
619          !!!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 628  sub _get_next_token ($) {
628          redo A;          redo A;
629        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
630          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
631          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
632            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
633                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
634            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
635          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
636            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
637            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
638              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
639            }            }
640          } else {          } else {
641            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
642          }          }
643          $self->{state} = 'data';          $self->{state} = DATA_STATE;
644          # reconsume          # reconsume
645    
646          !!!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 649  sub _get_next_token ($) {
649        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
650          !!!next-input-character;          !!!next-input-character;
651          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
652              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
653              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
654            # permitted slash            # permitted slash
655            #            #
656          } else {          } else {
657            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
658          }          }
659          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
660          # next-input-character is already done          # next-input-character is already done
661          redo A;          redo A;
662        } else {        } else {
# Line 489  sub _get_next_token ($) { Line 666  sub _get_next_token ($) {
666          !!!next-input-character;          !!!next-input-character;
667          redo A;          redo A;
668        }        }
669      } elsif ($self->{state} eq 'before attribute name') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
670        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
671            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
672            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 499  sub _get_next_token ($) { Line 676  sub _get_next_token ($) {
676          !!!next-input-character;          !!!next-input-character;
677          redo A;          redo A;
678        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
679          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
680            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
681                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
682            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
683          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
684            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
685            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
686              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
687            }            }
688          } else {          } else {
689            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
690          }          }
691          $self->{state} = 'data';          $self->{state} = DATA_STATE;
692          !!!next-input-character;          !!!next-input-character;
693    
694          !!!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 698  sub _get_next_token ($) {
698                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_input_character} <= 0x005A) { # A..Z
699          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),
700                                value => ''};                                value => ''};
701          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
702          !!!next-input-character;          !!!next-input-character;
703          redo A;          redo A;
704        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
705          !!!next-input-character;          !!!next-input-character;
706          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
707              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
708              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
709            # permitted slash            # permitted slash
710            #            #
# Line 539  sub _get_next_token ($) { Line 716  sub _get_next_token ($) {
716          redo A;          redo A;
717        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
718          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
719          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
720            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
721                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
722            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
723          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
724            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
725            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
726              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
727            }            }
728          } else {          } else {
729            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
730          }          }
731          $self->{state} = 'data';          $self->{state} = DATA_STATE;
732          # reconsume          # reconsume
733    
734          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 560  sub _get_next_token ($) { Line 737  sub _get_next_token ($) {
737        } else {        } else {
738          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          $self->{current_attribute} = {name => chr ($self->{next_input_character}),
739                                value => ''};                                value => ''};
740          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
741          !!!next-input-character;          !!!next-input-character;
742          redo A;          redo A;
743        }        }
744      } elsif ($self->{state} eq 'attribute name') {      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {
745        my $before_leave = sub {        my $before_leave = sub {
746          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
747              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
748            !!!parse-error (type => 'dupulicate attribute');            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});
749            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
750          } else {          } else {
751            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
# Line 582  sub _get_next_token ($) { Line 759  sub _get_next_token ($) {
759            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
760            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
761          $before_leave->();          $before_leave->();
762          $self->{state} = 'after attribute name';          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
763          !!!next-input-character;          !!!next-input-character;
764          redo A;          redo A;
765        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_input_character} == 0x003D) { # =
766          $before_leave->();          $before_leave->();
767          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
768          !!!next-input-character;          !!!next-input-character;
769          redo A;          redo A;
770        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
771          $before_leave->();          $before_leave->();
772          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
773            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
774                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
775            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
776          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
777            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
778            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
779              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
780            }            }
781          } else {          } else {
782            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
783          }          }
784          $self->{state} = 'data';          $self->{state} = DATA_STATE;
785          !!!next-input-character;          !!!next-input-character;
786    
787          !!!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 797  sub _get_next_token ($) {
797          $before_leave->();          $before_leave->();
798          !!!next-input-character;          !!!next-input-character;
799          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
800              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
801              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
802            # permitted slash            # permitted slash
803            #            #
804          } else {          } else {
805            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
806          }          }
807          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
808          # next-input-character is already done          # next-input-character is already done
809          redo A;          redo A;
810        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
811          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
812          $before_leave->();          $before_leave->();
813          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
814            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
815                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
816            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
817          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
818            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
819            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
820              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
821            }            }
822          } else {          } else {
823            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
824          }          }
825          $self->{state} = 'data';          $self->{state} = DATA_STATE;
826          # reconsume          # reconsume
827    
828          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 657  sub _get_next_token ($) { Line 834  sub _get_next_token ($) {
834          !!!next-input-character;          !!!next-input-character;
835          redo A;          redo A;
836        }        }
837      } elsif ($self->{state} eq 'after attribute name') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
838        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
839            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
840            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 667  sub _get_next_token ($) { Line 844  sub _get_next_token ($) {
844          !!!next-input-character;          !!!next-input-character;
845          redo A;          redo A;
846        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_input_character} == 0x003D) { # =
847          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
848          !!!next-input-character;          !!!next-input-character;
849          redo A;          redo A;
850        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
851          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
852            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
853                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
854            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
855          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
856            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
857            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
858              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
859            }            }
860          } else {          } else {
861            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
862          }          }
863          $self->{state} = 'data';          $self->{state} = DATA_STATE;
864          !!!next-input-character;          !!!next-input-character;
865    
866          !!!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 870  sub _get_next_token ($) {
870                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_input_character} <= 0x005A) { # A..Z
871          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),
872                                value => ''};                                value => ''};
873          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
874          !!!next-input-character;          !!!next-input-character;
875          redo A;          redo A;
876        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
877          !!!next-input-character;          !!!next-input-character;
878          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
879              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
880              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
881            # permitted slash            # permitted slash
882            #            #
# Line 707  sub _get_next_token ($) { Line 884  sub _get_next_token ($) {
884            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
885            ## TODO: Different error type for <aa / bb> than <aa/>            ## TODO: Different error type for <aa / bb> than <aa/>
886          }          }
887          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
888          # next-input-character is already done          # next-input-character is already done
889          redo A;          redo A;
890        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
891          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
892          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
893            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
894                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
895            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
896          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
897            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
898            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
899              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
900            }            }
901          } else {          } else {
902            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
903          }          }
904          $self->{state} = 'data';          $self->{state} = DATA_STATE;
905          # reconsume          # reconsume
906    
907          !!!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 910  sub _get_next_token ($) {
910        } else {        } else {
911          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          $self->{current_attribute} = {name => chr ($self->{next_input_character}),
912                                value => ''};                                value => ''};
913          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
914          !!!next-input-character;          !!!next-input-character;
915          redo A;                  redo A;        
916        }        }
917      } elsif ($self->{state} eq 'before attribute value') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
918        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
919            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
920            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 747  sub _get_next_token ($) { Line 924  sub _get_next_token ($) {
924          !!!next-input-character;          !!!next-input-character;
925          redo A;          redo A;
926        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_input_character} == 0x0022) { # "
927          $self->{state} = 'attribute value (double-quoted)';          $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
928          !!!next-input-character;          !!!next-input-character;
929          redo A;          redo A;
930        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
931          $self->{state} = 'attribute value (unquoted)';          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
932          ## reconsume          ## reconsume
933          redo A;          redo A;
934        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_input_character} == 0x0027) { # '
935          $self->{state} = 'attribute value (single-quoted)';          $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
936          !!!next-input-character;          !!!next-input-character;
937          redo A;          redo A;
938        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
939          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
940            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
941                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
942            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
943          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
944            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
945            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
946              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
947            }            }
948          } else {          } else {
949            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
950          }          }
951          $self->{state} = 'data';          $self->{state} = DATA_STATE;
952          !!!next-input-character;          !!!next-input-character;
953    
954          !!!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 956  sub _get_next_token ($) {
956          redo A;          redo A;
957        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
958          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
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          ## reconsume          ## reconsume
973    
974          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 799  sub _get_next_token ($) { Line 976  sub _get_next_token ($) {
976          redo A;          redo A;
977        } else {        } else {
978          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});
979          $self->{state} = 'attribute value (unquoted)';          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
980          !!!next-input-character;          !!!next-input-character;
981          redo A;          redo A;
982        }        }
983      } elsif ($self->{state} eq 'attribute value (double-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
984        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_input_character} == 0x0022) { # "
985          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
986          !!!next-input-character;          !!!next-input-character;
987          redo A;          redo A;
988        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
989          $self->{last_attribute_value_state} = 'attribute value (double-quoted)';          $self->{last_attribute_value_state} = $self->{state};
990          $self->{state} = 'entity in attribute value';          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
991          !!!next-input-character;          !!!next-input-character;
992          redo A;          redo A;
993        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
994          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
995          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
996            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
997                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
998            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
999          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1000            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1001            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1002              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1003            }            }
1004          } else {          } else {
1005            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1006          }          }
1007          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1008          ## reconsume          ## reconsume
1009    
1010          !!!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 1016  sub _get_next_token ($) {
1016          !!!next-input-character;          !!!next-input-character;
1017          redo A;          redo A;
1018        }        }
1019      } elsif ($self->{state} eq 'attribute value (single-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1020        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_input_character} == 0x0027) { # '
1021          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1022          !!!next-input-character;          !!!next-input-character;
1023          redo A;          redo A;
1024        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
1025          $self->{last_attribute_value_state} = 'attribute value (single-quoted)';          $self->{last_attribute_value_state} = $self->{state};
1026          $self->{state} = 'entity in attribute value';          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1027          !!!next-input-character;          !!!next-input-character;
1028          redo A;          redo A;
1029        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1030          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1031          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1032            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
1033                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
1034            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1035          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1036            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1037            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1038              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1039            }            }
1040          } else {          } else {
1041            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1042          }          }
1043          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1044          ## reconsume          ## reconsume
1045    
1046          !!!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 1052  sub _get_next_token ($) {
1052          !!!next-input-character;          !!!next-input-character;
1053          redo A;          redo A;
1054        }        }
1055      } elsif ($self->{state} eq 'attribute value (unquoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1056        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1057            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1058            $self->{next_input_character} == 0x000B or # HT            $self->{next_input_character} == 0x000B or # HT
1059            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1060            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
1061          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1062          !!!next-input-character;          !!!next-input-character;
1063          redo A;          redo A;
1064        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
1065          $self->{last_attribute_value_state} = 'attribute value (unquoted)';          $self->{last_attribute_value_state} = $self->{state};
1066          $self->{state} = 'entity in attribute value';          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1067          !!!next-input-character;          !!!next-input-character;
1068          redo A;          redo A;
1069        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1070          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1071            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
1072                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
1073            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1074          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1075            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1076            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1077              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1078            }            }
1079          } else {          } else {
1080            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1081          }          }
1082          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1083          !!!next-input-character;          !!!next-input-character;
1084    
1085          !!!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 1087  sub _get_next_token ($) {
1087          redo A;          redo A;
1088        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1089          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1090          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1091            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
1092                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
1093            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1094          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1095            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1096            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1097              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1098            }            }
1099          } else {          } else {
1100            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1101          }          }
1102          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1103          ## reconsume          ## reconsume
1104    
1105          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 934  sub _get_next_token ($) { Line 1111  sub _get_next_token ($) {
1111          !!!next-input-character;          !!!next-input-character;
1112          redo A;          redo A;
1113        }        }
1114      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {
1115        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);
1116    
1117        unless (defined $token) {        unless (defined $token) {
1118          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
1119        } else {        } else {
1120          $self->{current_attribute}->{value} .= $token->{data};          $self->{current_attribute}->{value} .= $token->{data};
1121            $self->{current_attribute}->{has_reference} = $token->{has_reference};
1122          ## 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"
1123        }        }
1124    
1125        $self->{state} = $self->{last_attribute_value_state};        $self->{state} = $self->{last_attribute_value_state};
1126        # next-input-character is already done        # next-input-character is already done
1127        redo A;        redo A;
1128      } elsif ($self->{state} eq 'bogus comment') {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1129        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1130                
1131        my $token = {type => 'comment', data => ''};        my $token = {type => COMMENT_TOKEN, data => ''};
1132    
1133        BC: {        BC: {
1134          if ($self->{next_input_character} == 0x003E) { # >          if ($self->{next_input_character} == 0x003E) { # >
1135            $self->{state} = 'data';            $self->{state} = DATA_STATE;
1136            !!!next-input-character;            !!!next-input-character;
1137    
1138            !!!emit ($token);            !!!emit ($token);
1139    
1140            redo A;            redo A;
1141          } elsif ($self->{next_input_character} == -1) {          } elsif ($self->{next_input_character} == -1) {
1142            $self->{state} = 'data';            $self->{state} = DATA_STATE;
1143            ## reconsume            ## reconsume
1144    
1145            !!!emit ($token);            !!!emit ($token);
# Line 973  sub _get_next_token ($) { Line 1151  sub _get_next_token ($) {
1151            redo BC;            redo BC;
1152          }          }
1153        } # BC        } # BC
1154      } elsif ($self->{state} eq 'markup declaration open') {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1155        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1156    
1157        my @next_char;        my @next_char;
# Line 983  sub _get_next_token ($) { Line 1161  sub _get_next_token ($) {
1161          !!!next-input-character;          !!!next-input-character;
1162          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_input_character};
1163          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_input_character} == 0x002D) { # -
1164            $self->{current_token} = {type => 'comment', data => ''};            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};
1165            $self->{state} = 'comment start';            $self->{state} = COMMENT_START_STATE;
1166            !!!next-input-character;            !!!next-input-character;
1167            redo A;            redo A;
1168          }          }
# Line 1015  sub _get_next_token ($) { Line 1193  sub _get_next_token ($) {
1193                    if ($self->{next_input_character} == 0x0045 or # E                    if ($self->{next_input_character} == 0x0045 or # E
1194                        $self->{next_input_character} == 0x0065) { # e                        $self->{next_input_character} == 0x0065) { # e
1195                      ## ISSUE: What a stupid code this is!                      ## ISSUE: What a stupid code this is!
1196                      $self->{state} = 'DOCTYPE';                      $self->{state} = DOCTYPE_STATE;
1197                      !!!next-input-character;                      !!!next-input-character;
1198                      redo A;                      redo A;
1199                    }                    }
# Line 1029  sub _get_next_token ($) { Line 1207  sub _get_next_token ($) {
1207        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'bogus comment');
1208        $self->{next_input_character} = shift @next_char;        $self->{next_input_character} = shift @next_char;
1209        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
1210        $self->{state} = 'bogus comment';        $self->{state} = BOGUS_COMMENT_STATE;
1211        redo A;        redo A;
1212                
1213        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
1214        ## 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?
1215      } elsif ($self->{state} eq 'comment start') {      } elsif ($self->{state} == COMMENT_START_STATE) {
1216        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1217          $self->{state} = 'comment start dash';          $self->{state} = COMMENT_START_DASH_STATE;
1218          !!!next-input-character;          !!!next-input-character;
1219          redo A;          redo A;
1220        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1221          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
1222          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1223          !!!next-input-character;          !!!next-input-character;
1224    
1225          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1049  sub _get_next_token ($) { Line 1227  sub _get_next_token ($) {
1227          redo A;          redo A;
1228        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1229          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1230          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1231          ## reconsume          ## reconsume
1232    
1233          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1058  sub _get_next_token ($) { Line 1236  sub _get_next_token ($) {
1236        } else {        } else {
1237          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
1238              .= chr ($self->{next_input_character});              .= chr ($self->{next_input_character});
1239          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1240          !!!next-input-character;          !!!next-input-character;
1241          redo A;          redo A;
1242        }        }
1243      } elsif ($self->{state} eq 'comment start dash') {      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
1244        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1245          $self->{state} = 'comment end';          $self->{state} = COMMENT_END_STATE;
1246          !!!next-input-character;          !!!next-input-character;
1247          redo A;          redo A;
1248        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1249          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
1250          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1251          !!!next-input-character;          !!!next-input-character;
1252    
1253          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1077  sub _get_next_token ($) { Line 1255  sub _get_next_token ($) {
1255          redo A;          redo A;
1256        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1257          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1258          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1259          ## reconsume          ## reconsume
1260    
1261          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1086  sub _get_next_token ($) { Line 1264  sub _get_next_token ($) {
1264        } else {        } else {
1265          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
1266              .= '-' . chr ($self->{next_input_character});              .= '-' . chr ($self->{next_input_character});
1267          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1268          !!!next-input-character;          !!!next-input-character;
1269          redo A;          redo A;
1270        }        }
1271      } elsif ($self->{state} eq 'comment') {      } elsif ($self->{state} == COMMENT_STATE) {
1272        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1273          $self->{state} = 'comment end dash';          $self->{state} = COMMENT_END_DASH_STATE;
1274          !!!next-input-character;          !!!next-input-character;
1275          redo A;          redo A;
1276        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1277          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1278          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1279          ## reconsume          ## reconsume
1280    
1281          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1109  sub _get_next_token ($) { Line 1287  sub _get_next_token ($) {
1287          !!!next-input-character;          !!!next-input-character;
1288          redo A;          redo A;
1289        }        }
1290      } elsif ($self->{state} eq 'comment end dash') {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
1291        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1292          $self->{state} = 'comment end';          $self->{state} = COMMENT_END_STATE;
1293          !!!next-input-character;          !!!next-input-character;
1294          redo A;          redo A;
1295        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1296          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1297          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1298          ## reconsume          ## reconsume
1299    
1300          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1124  sub _get_next_token ($) { Line 1302  sub _get_next_token ($) {
1302          redo A;          redo A;
1303        } else {        } else {
1304          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment
1305          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1306          !!!next-input-character;          !!!next-input-character;
1307          redo A;          redo A;
1308        }        }
1309      } elsif ($self->{state} eq 'comment end') {      } elsif ($self->{state} == COMMENT_END_STATE) {
1310        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_input_character} == 0x003E) { # >
1311          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1312          !!!next-input-character;          !!!next-input-character;
1313    
1314          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1144  sub _get_next_token ($) { Line 1322  sub _get_next_token ($) {
1322          redo A;          redo A;
1323        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1324          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1325          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1326          ## reconsume          ## reconsume
1327    
1328          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1153  sub _get_next_token ($) { Line 1331  sub _get_next_token ($) {
1331        } else {        } else {
1332          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment');
1333          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment
1334          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1335          !!!next-input-character;          !!!next-input-character;
1336          redo A;          redo A;
1337        }        }
1338      } elsif ($self->{state} eq 'DOCTYPE') {      } elsif ($self->{state} == DOCTYPE_STATE) {
1339        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1340            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1341            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
1342            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1343            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
1344          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1345          !!!next-input-character;          !!!next-input-character;
1346          redo A;          redo A;
1347        } else {        } else {
1348          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
1349          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1350          ## reconsume          ## reconsume
1351          redo A;          redo A;
1352        }        }
1353      } elsif ($self->{state} eq 'before DOCTYPE name') {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
1354        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1355            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1356            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 1183  sub _get_next_token ($) { Line 1361  sub _get_next_token ($) {
1361          redo A;          redo A;
1362        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1363          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1364          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1365          !!!next-input-character;          !!!next-input-character;
1366    
1367          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect
1368    
1369          redo A;          redo A;
1370        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1371          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1372          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1373          ## reconsume          ## reconsume
1374    
1375          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect
1376    
1377          redo A;          redo A;
1378        } else {        } else {
1379          $self->{current_token}          $self->{current_token}
1380              = {type => 'DOCTYPE',              = {type => DOCTYPE_TOKEN,
1381                 name => chr ($self->{next_input_character}),                 name => chr ($self->{next_input_character}),
1382                 correct => 1};                 correct => 1};
1383  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
1384          $self->{state} = 'DOCTYPE name';          $self->{state} = DOCTYPE_NAME_STATE;
1385          !!!next-input-character;          !!!next-input-character;
1386          redo A;          redo A;
1387        }        }
1388      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
1389  ## ISSUE: Redundant "First," in the spec.  ## ISSUE: Redundant "First," in the spec.
1390        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1391            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1392            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
1393            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1394            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
1395          $self->{state} = 'after DOCTYPE name';          $self->{state} = AFTER_DOCTYPE_NAME_STATE;
1396          !!!next-input-character;          !!!next-input-character;
1397          redo A;          redo A;
1398        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1399          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1400          !!!next-input-character;          !!!next-input-character;
1401    
1402          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1226  sub _get_next_token ($) { Line 1404  sub _get_next_token ($) {
1404          redo A;          redo A;
1405        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1406          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1407          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1408          ## reconsume          ## reconsume
1409    
1410          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1240  sub _get_next_token ($) { Line 1418  sub _get_next_token ($) {
1418          !!!next-input-character;          !!!next-input-character;
1419          redo A;          redo A;
1420        }        }
1421      } elsif ($self->{state} eq 'after DOCTYPE name') {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_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
# Line 1250  sub _get_next_token ($) { Line 1428  sub _get_next_token ($) {
1428          !!!next-input-character;          !!!next-input-character;
1429          redo A;          redo A;
1430        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1431          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1432          !!!next-input-character;          !!!next-input-character;
1433    
1434          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1258  sub _get_next_token ($) { Line 1436  sub _get_next_token ($) {
1436          redo A;          redo A;
1437        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1438          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1439          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1440          ## reconsume          ## reconsume
1441    
1442          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1282  sub _get_next_token ($) { Line 1460  sub _get_next_token ($) {
1460                  !!!next-input-character;                  !!!next-input-character;
1461                  if ($self->{next_input_character} == 0x0043 or # C                  if ($self->{next_input_character} == 0x0043 or # C
1462                      $self->{next_input_character} == 0x0063) { # c                      $self->{next_input_character} == 0x0063) { # c
1463                    $self->{state} = 'before DOCTYPE public identifier';                    $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1464                    !!!next-input-character;                    !!!next-input-character;
1465                    redo A;                    redo A;
1466                  }                  }
# Line 1309  sub _get_next_token ($) { Line 1487  sub _get_next_token ($) {
1487                  !!!next-input-character;                  !!!next-input-character;
1488                  if ($self->{next_input_character} == 0x004D or # M                  if ($self->{next_input_character} == 0x004D or # M
1489                      $self->{next_input_character} == 0x006D) { # m                      $self->{next_input_character} == 0x006D) { # m
1490                    $self->{state} = 'before DOCTYPE system identifier';                    $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1491                    !!!next-input-character;                    !!!next-input-character;
1492                    redo A;                    redo A;
1493                  }                  }
# Line 1325  sub _get_next_token ($) { Line 1503  sub _get_next_token ($) {
1503        }        }
1504    
1505        !!!parse-error (type => 'string after DOCTYPE name');        !!!parse-error (type => 'string after DOCTYPE name');
1506        $self->{state} = 'bogus DOCTYPE';        $self->{state} = BOGUS_DOCTYPE_STATE;
1507        # next-input-character is already done        # next-input-character is already done
1508        redo A;        redo A;
1509      } elsif ($self->{state} eq 'before DOCTYPE public identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
1510        if ({        if ({
1511              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1512              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1338  sub _get_next_token ($) { Line 1516  sub _get_next_token ($) {
1516          redo A;          redo A;
1517        } elsif ($self->{next_input_character} eq 0x0022) { # "        } elsif ($self->{next_input_character} eq 0x0022) { # "
1518          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1519          $self->{state} = 'DOCTYPE public identifier (double-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
1520          !!!next-input-character;          !!!next-input-character;
1521          redo A;          redo A;
1522        } elsif ($self->{next_input_character} eq 0x0027) { # '        } elsif ($self->{next_input_character} eq 0x0027) { # '
1523          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1524          $self->{state} = 'DOCTYPE public identifier (single-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
1525          !!!next-input-character;          !!!next-input-character;
1526          redo A;          redo A;
1527        } elsif ($self->{next_input_character} eq 0x003E) { # >        } elsif ($self->{next_input_character} eq 0x003E) { # >
1528          !!!parse-error (type => 'no PUBLIC literal');          !!!parse-error (type => 'no PUBLIC literal');
1529    
1530          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1531          !!!next-input-character;          !!!next-input-character;
1532    
1533          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1359  sub _get_next_token ($) { Line 1537  sub _get_next_token ($) {
1537        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1538          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1539    
1540          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1541          ## reconsume          ## reconsume
1542    
1543          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1368  sub _get_next_token ($) { Line 1546  sub _get_next_token ($) {
1546          redo A;          redo A;
1547        } else {        } else {
1548          !!!parse-error (type => 'string after PUBLIC');          !!!parse-error (type => 'string after PUBLIC');
1549          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1550          !!!next-input-character;          !!!next-input-character;
1551          redo A;          redo A;
1552        }        }
1553      } elsif ($self->{state} eq 'DOCTYPE public identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
1554        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_input_character} == 0x0022) { # "
1555          $self->{state} = 'after DOCTYPE public identifier';          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1556          !!!next-input-character;          !!!next-input-character;
1557          redo A;          redo A;
1558        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1559          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
1560    
1561          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1562          ## reconsume          ## reconsume
1563    
1564          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1394  sub _get_next_token ($) { Line 1572  sub _get_next_token ($) {
1572          !!!next-input-character;          !!!next-input-character;
1573          redo A;          redo A;
1574        }        }
1575      } elsif ($self->{state} eq 'DOCTYPE public identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
1576        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_input_character} == 0x0027) { # '
1577          $self->{state} = 'after DOCTYPE public identifier';          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1578          !!!next-input-character;          !!!next-input-character;
1579          redo A;          redo A;
1580        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1581          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
1582    
1583          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1584          ## reconsume          ## reconsume
1585    
1586          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1416  sub _get_next_token ($) { Line 1594  sub _get_next_token ($) {
1594          !!!next-input-character;          !!!next-input-character;
1595          redo A;          redo A;
1596        }        }
1597      } elsif ($self->{state} eq 'after DOCTYPE public identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
1598        if ({        if ({
1599              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1600              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1426  sub _get_next_token ($) { Line 1604  sub _get_next_token ($) {
1604          redo A;          redo A;
1605        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_input_character} == 0x0022) { # "
1606          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1607          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
1608          !!!next-input-character;          !!!next-input-character;
1609          redo A;          redo A;
1610        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_input_character} == 0x0027) { # '
1611          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1612          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
1613          !!!next-input-character;          !!!next-input-character;
1614          redo A;          redo A;
1615        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1616          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1617          !!!next-input-character;          !!!next-input-character;
1618    
1619          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1444  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 1453  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 literal');          !!!parse-error (type => 'string after PUBLIC literal');
1634          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1635          !!!next-input-character;          !!!next-input-character;
1636          redo A;          redo A;
1637        }        }
1638      } elsif ($self->{state} eq 'before DOCTYPE system identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
1639        if ({        if ({
1640              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1641              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1467  sub _get_next_token ($) { Line 1645  sub _get_next_token ($) {
1645          redo A;          redo A;
1646        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_input_character} == 0x0022) { # "
1647          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1648          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
1649          !!!next-input-character;          !!!next-input-character;
1650          redo A;          redo A;
1651        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_input_character} == 0x0027) { # '
1652          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1653          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
1654          !!!next-input-character;          !!!next-input-character;
1655          redo A;          redo A;
1656        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1657          !!!parse-error (type => 'no SYSTEM literal');          !!!parse-error (type => 'no SYSTEM literal');
1658          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1659          !!!next-input-character;          !!!next-input-character;
1660    
1661          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1487  sub _get_next_token ($) { Line 1665  sub _get_next_token ($) {
1665        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1666          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1667    
1668          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1669          ## reconsume          ## reconsume
1670    
1671          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1496  sub _get_next_token ($) { Line 1674  sub _get_next_token ($) {
1674          redo A;          redo A;
1675        } else {        } else {
1676          !!!parse-error (type => 'string after SYSTEM');          !!!parse-error (type => 'string after SYSTEM');
1677          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1678          !!!next-input-character;          !!!next-input-character;
1679          redo A;          redo A;
1680        }        }
1681      } elsif ($self->{state} eq 'DOCTYPE system identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
1682        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_input_character} == 0x0022) { # "
1683          $self->{state} = 'after DOCTYPE system identifier';          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1684          !!!next-input-character;          !!!next-input-character;
1685          redo A;          redo A;
1686        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1687          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
1688    
1689          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1690          ## reconsume          ## reconsume
1691    
1692          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1522  sub _get_next_token ($) { Line 1700  sub _get_next_token ($) {
1700          !!!next-input-character;          !!!next-input-character;
1701          redo A;          redo A;
1702        }        }
1703      } elsif ($self->{state} eq 'DOCTYPE system identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
1704        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_input_character} == 0x0027) { # '
1705          $self->{state} = 'after DOCTYPE system identifier';          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1706          !!!next-input-character;          !!!next-input-character;
1707          redo A;          redo A;
1708        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1709          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
1710    
1711          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1712          ## reconsume          ## reconsume
1713    
1714          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1544  sub _get_next_token ($) { Line 1722  sub _get_next_token ($) {
1722          !!!next-input-character;          !!!next-input-character;
1723          redo A;          redo A;
1724        }        }
1725      } elsif ($self->{state} eq 'after DOCTYPE system identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
1726        if ({        if ({
1727              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1728              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1553  sub _get_next_token ($) { Line 1731  sub _get_next_token ($) {
1731          !!!next-input-character;          !!!next-input-character;
1732          redo A;          redo A;
1733        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1734          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1735          !!!next-input-character;          !!!next-input-character;
1736    
1737          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1562  sub _get_next_token ($) { Line 1740  sub _get_next_token ($) {
1740        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1741          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1742    
1743          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1744          ## reconsume          ## reconsume
1745    
1746          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1571  sub _get_next_token ($) { Line 1749  sub _get_next_token ($) {
1749          redo A;          redo A;
1750        } else {        } else {
1751          !!!parse-error (type => 'string after SYSTEM literal');          !!!parse-error (type => 'string after SYSTEM literal');
1752          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1753          !!!next-input-character;          !!!next-input-character;
1754          redo A;          redo A;
1755        }        }
1756      } elsif ($self->{state} eq 'bogus DOCTYPE') {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
1757        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_input_character} == 0x003E) { # >
1758          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1759          !!!next-input-character;          !!!next-input-character;
1760    
1761          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1586  sub _get_next_token ($) { Line 1764  sub _get_next_token ($) {
1764          redo A;          redo A;
1765        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1766          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1767          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1768          ## reconsume          ## reconsume
1769    
1770          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1644  sub _tokenize_attempt_to_consume_an_enti Line 1822  sub _tokenize_attempt_to_consume_an_enti
1822            redo X;            redo X;
1823          } elsif (not defined $code) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
1824            !!!parse-error (type => 'bare hcro');            !!!parse-error (type => 'bare hcro');
1825              !!!back-next-input-character ($x_char, $self->{next_input_character});
1826            $self->{next_input_character} = 0x0023; # #            $self->{next_input_character} = 0x0023; # #
           !!!back-next-input-character ($x_char);  
1827            return undef;            return undef;
1828          } elsif ($self->{next_input_character} == 0x003B) { # ;          } elsif ($self->{next_input_character} == 0x003B) { # ;
1829            !!!next-input-character;            !!!next-input-character;
# Line 1667  sub _tokenize_attempt_to_consume_an_enti Line 1845  sub _tokenize_attempt_to_consume_an_enti
1845            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
1846          }          }
1847    
1848          return {type => 'character', data => chr $code};          return {type => CHARACTER_TOKEN, data => chr $code,
1849                    has_reference => 1};
1850        } # X        } # X
1851      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_input_character} and
1852               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_input_character} <= 0x0039) { # 0..9
# Line 1702  sub _tokenize_attempt_to_consume_an_enti Line 1881  sub _tokenize_attempt_to_consume_an_enti
1881          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
1882        }        }
1883                
1884        return {type => 'character', data => chr $code};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1};
1885      } else {      } else {
1886        !!!parse-error (type => 'bare nero');        !!!parse-error (type => 'bare nero');
1887        !!!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 1896  sub _tokenize_attempt_to_consume_an_enti
1896      !!!next-input-character;      !!!next-input-character;
1897    
1898      my $value = $entity_name;      my $value = $entity_name;
1899      my $match;      my $match = 0;
1900      require Whatpm::_NamedEntityList;      require Whatpm::_NamedEntityList;
1901      our $EntityChar;      our $EntityChar;
1902    
# Line 1737  sub _tokenize_attempt_to_consume_an_enti Line 1916  sub _tokenize_attempt_to_consume_an_enti
1916            $match = 1;            $match = 1;
1917            !!!next-input-character;            !!!next-input-character;
1918            last;            last;
1919          } elsif (not $in_attr) {          } else {
1920            $value = $EntityChar->{$entity_name};            $value = $EntityChar->{$entity_name};
1921            $match = -1;            $match = -1;
1922          } else {            !!!next-input-character;
           $value .= chr $self->{next_input_character};  
1923          }          }
1924        } else {        } else {
1925          $value .= chr $self->{next_input_character};          $value .= chr $self->{next_input_character};
1926            $match *= 2;
1927            !!!next-input-character;
1928        }        }
       !!!next-input-character;  
1929      }      }
1930            
1931      if ($match > 0) {      if ($match > 0) {
1932        return {type => 'character', data => $value};        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};
1933      } elsif ($match < 0) {      } elsif ($match < 0) {
1934        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc');
1935        return {type => 'character', data => $value};        if ($in_attr and $match < -1) {
1936            return {type => CHARACTER_TOKEN, data => '&'.$entity_name};
1937          } else {
1938            return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};
1939          }
1940      } else {      } else {
1941        !!!parse-error (type => 'bare ero');        !!!parse-error (type => 'bare ero');
1942        ## NOTE: No characters are consumed in the spec.        ## NOTE: "No characters are consumed" in the spec.
1943        return {type => 'character', data => '&'.$value};        return {type => CHARACTER_TOKEN, data => '&'.$value};
1944      }      }
1945    } else {    } else {
1946      ## no characters are consumed      ## no characters are consumed
# Line 1799  sub _construct_tree ($) { Line 1982  sub _construct_tree ($) {
1982        
1983    !!!next-token;    !!!next-token;
1984    
1985    $self->{insertion_mode} = 'before head';    $self->{insertion_mode} = BEFORE_HEAD_IM;
1986    undef $self->{form_element};    undef $self->{form_element};
1987    undef $self->{head_element};    undef $self->{head_element};
1988    $self->{open_elements} = [];    $self->{open_elements} = [];
# Line 1813  sub _construct_tree ($) { Line 1996  sub _construct_tree ($) {
1996  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
1997    my $self = shift;    my $self = shift;
1998    INITIAL: {    INITIAL: {
1999      if ($token->{type} eq 'DOCTYPE') {      if ($token->{type} == DOCTYPE_TOKEN) {
2000        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
2001        ## error, switch to a conformance checking mode for another        ## error, switch to a conformance checking mode for another
2002        ## language.        ## language.
# Line 1940  sub _tree_construction_initial ($) { Line 2123  sub _tree_construction_initial ($) {
2123        !!!next-token;        !!!next-token;
2124        return;        return;
2125      } elsif ({      } elsif ({
2126                'start tag' => 1,                START_TAG_TOKEN, 1,
2127                'end tag' => 1,                END_TAG_TOKEN, 1,
2128                'end-of-file' => 1,                END_OF_FILE_TOKEN, 1,
2129               }->{$token->{type}}) {               }->{$token->{type}}) {
2130        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE');
2131        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
2132        ## Go to the root element phase        ## Go to the root element phase
2133        ## reprocess        ## reprocess
2134        return;        return;
2135      } elsif ($token->{type} eq 'character') {      } elsif ($token->{type} == CHARACTER_TOKEN) {
2136        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2137          ## Ignore the token          ## Ignore the token
2138    
# Line 1965  sub _tree_construction_initial ($) { Line 2148  sub _tree_construction_initial ($) {
2148        ## Go to the root element phase        ## Go to the root element phase
2149        ## reprocess        ## reprocess
2150        return;        return;
2151      } elsif ($token->{type} eq 'comment') {      } elsif ($token->{type} == COMMENT_TOKEN) {
2152        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
2153        $self->{document}->append_child ($comment);        $self->{document}->append_child ($comment);
2154                
# Line 1973  sub _tree_construction_initial ($) { Line 2156  sub _tree_construction_initial ($) {
2156        !!!next-token;        !!!next-token;
2157        redo INITIAL;        redo INITIAL;
2158      } else {      } else {
2159        die "$0: $token->{type}: Unknown token";        die "$0: $token->{type}: Unknown token type";
2160      }      }
2161    } # INITIAL    } # INITIAL
2162  } # _tree_construction_initial  } # _tree_construction_initial
# Line 1982  sub _tree_construction_root_element ($) Line 2165  sub _tree_construction_root_element ($)
2165    my $self = shift;    my $self = shift;
2166        
2167    B: {    B: {
2168        if ($token->{type} eq 'DOCTYPE') {        if ($token->{type} == DOCTYPE_TOKEN) {
2169          !!!parse-error (type => 'in html:#DOCTYPE');          !!!parse-error (type => 'in html:#DOCTYPE');
2170          ## Ignore the token          ## Ignore the token
2171          ## Stay in the phase          ## Stay in the phase
2172          !!!next-token;          !!!next-token;
2173          redo B;          redo B;
2174        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{type} == COMMENT_TOKEN) {
2175          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
2176          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
2177          ## Stay in the phase          ## Stay in the phase
2178          !!!next-token;          !!!next-token;
2179          redo B;          redo B;
2180        } elsif ($token->{type} eq 'character') {        } elsif ($token->{type} == CHARACTER_TOKEN) {
2181          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2182            ## Ignore the token.            ## Ignore the token.
2183    
# Line 2004  sub _tree_construction_root_element ($) Line 2187  sub _tree_construction_root_element ($)
2187              redo B;              redo B;
2188            }            }
2189          }          }
2190    
2191            $self->{application_cache_selection}->(undef);
2192    
2193            #
2194          } elsif ($token->{type} == START_TAG_TOKEN) {
2195            if ($token->{tag_name} eq 'html' and
2196                $token->{attributes}->{manifest}) {
2197              $self->{application_cache_selection}
2198                   ->($token->{attributes}->{manifest}->{value});
2199              ## ISSUE: No relative reference resolution?
2200            } else {
2201              $self->{application_cache_selection}->(undef);
2202            }
2203    
2204            ## ISSUE: There is an issue in the spec
2205          #          #
2206        } elsif ({        } elsif ({
2207                  'start tag' => 1,                  END_TAG_TOKEN, 1,
2208                  'end tag' => 1,                  END_OF_FILE_TOKEN, 1,
                 'end-of-file' => 1,  
2209                 }->{$token->{type}}) {                 }->{$token->{type}}) {
2210            $self->{application_cache_selection}->(undef);
2211    
2212          ## ISSUE: There is an issue in the spec          ## ISSUE: There is an issue in the spec
2213          #          #
2214        } else {        } else {
2215          die "$0: $token->{type}: Unknown token";          die "$0: $token->{type}: Unknown token type";
2216        }        }
2217    
2218        my $root_element; !!!create-element ($root_element, 'html');        my $root_element; !!!create-element ($root_element, 'html');
2219        $self->{document}->append_child ($root_element);        $self->{document}->append_child ($root_element);
2220        push @{$self->{open_elements}}, [$root_element, 'html'];        push @{$self->{open_elements}}, [$root_element, 'html'];
       #$phase = 'main';  
2221        ## reprocess        ## reprocess
2222        #redo B;        #redo B;
2223        return;        return; ## Go to the main phase.
2224    } # B    } # B
2225  } # _tree_construction_root_element  } # _tree_construction_root_element
2226    
# Line 2056  sub _reset_insertion_mode ($) { Line 2255  sub _reset_insertion_mode ($) {
2255            
2256        ## Step 4..13        ## Step 4..13
2257        my $new_mode = {        my $new_mode = {
2258                        select => 'in select',                        select => IN_SELECT_IM,
2259                        td => 'in cell',                        td => IN_CELL_IM,
2260                        th => 'in cell',                        th => IN_CELL_IM,
2261                        tr => 'in row',                        tr => IN_ROW_IM,
2262                        tbody => 'in table body',                        tbody => IN_TABLE_BODY_IM,
2263                        thead => 'in table head',                        thead => IN_TABLE_BODY_IM,
2264                        tfoot => 'in table foot',                        tfoot => IN_TABLE_BODY_IM,
2265                        caption => 'in caption',                        caption => IN_CAPTION_IM,
2266                        colgroup => 'in column group',                        colgroup => IN_COLUMN_GROUP_IM,
2267                        table => 'in table',                        table => IN_TABLE_IM,
2268                        head => 'in body', # not in head!                        head => IN_BODY_IM, # not in head!
2269                        body => 'in body',                        body => IN_BODY_IM,
2270                        frameset => 'in frameset',                        frameset => IN_FRAMESET_IM,
2271                       }->{$node->[1]};                       }->{$node->[1]};
2272        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
2273                
2274        ## Step 14        ## Step 14
2275        if ($node->[1] eq 'html') {        if ($node->[1] eq 'html') {
2276          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
2277            $self->{insertion_mode} = 'before head';            $self->{insertion_mode} = BEFORE_HEAD_IM;
2278          } else {          } else {
2279            $self->{insertion_mode} = 'after head';            $self->{insertion_mode} = AFTER_HEAD_IM;
2280          }          }
2281          return;          return;
2282        }        }
2283                
2284        ## Step 15        ## Step 15
2285        $self->{insertion_mode} = 'in body' and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
2286                
2287        ## Step 16        ## Step 16
2288        $i--;        $i--;
# Line 2097  sub _reset_insertion_mode ($) { Line 2296  sub _reset_insertion_mode ($) {
2296  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
2297    my $self = shift;    my $self = shift;
2298    
   my $phase = 'main';  
   
2299    my $active_formatting_elements = [];    my $active_formatting_elements = [];
2300    
2301    my $reconstruct_active_formatting_elements = sub { # MUST    my $reconstruct_active_formatting_elements = sub { # MUST
# Line 2193  sub _tree_construction_main ($) { Line 2390  sub _tree_construction_main ($) {
2390      $insert->($el); # /context node/->append_child ($el)      $insert->($el); # /context node/->append_child ($el)
2391    
2392      ## Step 3      ## Step 3
2393      $self->{content_model_flag} = $content_model_flag; # CDATA or RCDATA      $self->{content_model} = $content_model_flag; # CDATA or RCDATA
2394      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
2395    
2396      ## Step 4      ## Step 4
2397      my $text = '';      my $text = '';
2398      !!!next-token;      !!!next-token;
2399      while ($token->{type} eq 'character') { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
2400        $text .= $token->{data};        $text .= $token->{data};
2401        !!!next-token;        !!!next-token;
2402      }      }
# Line 2211  sub _tree_construction_main ($) { Line 2408  sub _tree_construction_main ($) {
2408      }      }
2409    
2410      ## Step 6      ## Step 6
2411      $self->{content_model_flag} = 'PCDATA';      $self->{content_model} = PCDATA_CONTENT_MODEL;
2412    
2413      ## Step 7      ## Step 7
2414      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) {
2415        ## Ignore the token        ## Ignore the token
2416        } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {
2417          !!!parse-error (type => 'in CDATA:#'.$token->{type});
2418        } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
2419          !!!parse-error (type => 'in RCDATA:#'.$token->{type});
2420      } else {      } else {
2421        !!!parse-error (type => 'in '.$content_model_flag.':#'.$token->{type});        die "$0: $content_model_flag in parse_rcdata";
2422      }      }
2423      !!!next-token;      !!!next-token;
2424    }; # $parse_rcdata    }; # $parse_rcdata
# Line 2228  sub _tree_construction_main ($) { Line 2429  sub _tree_construction_main ($) {
2429      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, 'script', $token->{attributes});
2430      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
2431    
2432      $self->{content_model_flag} = 'CDATA';      $self->{content_model} = CDATA_CONTENT_MODEL;
2433      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
2434            
2435      my $text = '';      my $text = '';
2436      !!!next-token;      !!!next-token;
2437      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) {
2438        $text .= $token->{data};        $text .= $token->{data};
2439        !!!next-token;        !!!next-token;
2440      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
# Line 2241  sub _tree_construction_main ($) { Line 2442  sub _tree_construction_main ($) {
2442        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
2443      }      }
2444                                
2445      $self->{content_model_flag} = 'PCDATA';      $self->{content_model} = PCDATA_CONTENT_MODEL;
2446    
2447      if ($token->{type} eq 'end tag' and      if ($token->{type} == END_TAG_TOKEN and
2448          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
2449        ## Ignore the token        ## Ignore the token
2450      } else {      } else {
# Line 2486  sub _tree_construction_main ($) { Line 2687  sub _tree_construction_main ($) {
2687                         }                         }
2688    }; # $insert_to_foster    }; # $insert_to_foster
2689    
2690    my $in_body = sub {    my $insert;
2691      my $insert = shift;  
2692      if ($token->{type} eq 'start tag') {    B: {
2693        if ($token->{type} == DOCTYPE_TOKEN) {
2694          !!!parse-error (type => 'DOCTYPE in the middle');
2695          ## Ignore the token
2696          ## Stay in the phase
2697          !!!next-token;
2698          redo B;
2699        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
2700          if ($self->{insertion_mode} & AFTER_HTML_IMS) {
2701            #
2702          } else {
2703            ## Generate implied end tags
2704            if ({
2705                 dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,
2706                 tbody => 1, tfoot=> 1, thead => 1,
2707                }->{$self->{open_elements}->[-1]->[1]}) {
2708              !!!back-token;
2709              $token = {type => END_TAG_TOKEN, tag_name => $self->{open_elements}->[-1]->[1]};
2710              redo B;
2711            }
2712            
2713            if (@{$self->{open_elements}} > 2 or
2714                (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {
2715              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
2716            } elsif (defined $self->{inner_html_node} and
2717                     @{$self->{open_elements}} > 1 and
2718                     $self->{open_elements}->[1]->[1] ne 'body') {
2719              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
2720            }
2721    
2722            ## ISSUE: There is an issue in the spec.
2723          }
2724    
2725          ## Stop parsing
2726          last B;
2727        } elsif ($token->{type} == START_TAG_TOKEN and
2728                 $token->{tag_name} eq 'html') {
2729          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
2730            ## Turn into the main phase
2731            !!!parse-error (type => 'after html:html');
2732            $self->{insertion_mode} = AFTER_BODY_IM;
2733          } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
2734            ## Turn into the main phase
2735            !!!parse-error (type => 'after html:html');
2736            $self->{insertion_mode} = AFTER_FRAMESET_IM;
2737          }
2738    
2739    ## ISSUE: "aa<html>" is not a parse error.
2740    ## ISSUE: "<html>" in fragment is not a parse error.
2741          unless ($token->{first_start_tag}) {
2742            !!!parse-error (type => 'not first start tag');
2743          }
2744          my $top_el = $self->{open_elements}->[0]->[0];
2745          for my $attr_name (keys %{$token->{attributes}}) {
2746            unless ($top_el->has_attribute_ns (undef, $attr_name)) {
2747              $top_el->set_attribute_ns
2748                (undef, [undef, $attr_name],
2749                 $token->{attributes}->{$attr_name}->{value});
2750            }
2751          }
2752          !!!next-token;
2753          redo B;
2754        } elsif ($token->{type} == COMMENT_TOKEN) {
2755          my $comment = $self->{document}->create_comment ($token->{data});
2756          if ($self->{insertion_mode} & AFTER_HTML_IMS) {
2757            $self->{document}->append_child ($comment);
2758          } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
2759            $self->{open_elements}->[0]->[0]->append_child ($comment);
2760          } else {
2761            $self->{open_elements}->[-1]->[0]->append_child ($comment);
2762          }
2763          !!!next-token;
2764          redo B;
2765        } elsif ($self->{insertion_mode} & HEAD_IMS) {
2766          if ($token->{type} == CHARACTER_TOKEN) {
2767            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
2768              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
2769              unless (length $token->{data}) {
2770                !!!next-token;
2771                redo B;
2772              }
2773            }
2774    
2775            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2776              ## As if <head>
2777              !!!create-element ($self->{head_element}, 'head');
2778              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2779              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2780    
2781              ## Reprocess in the "in head" insertion mode...
2782              pop @{$self->{open_elements}};
2783    
2784              ## Reprocess in the "after head" insertion mode...
2785            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2786              ## As if </noscript>
2787              pop @{$self->{open_elements}};
2788              !!!parse-error (type => 'in noscript:#character');
2789              
2790              ## Reprocess in the "in head" insertion mode...
2791              ## As if </head>
2792              pop @{$self->{open_elements}};
2793    
2794              ## Reprocess in the "after head" insertion mode...
2795            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2796              pop @{$self->{open_elements}};
2797    
2798              ## Reprocess in the "after head" insertion mode...
2799            }
2800    
2801                ## "after head" insertion mode
2802                ## As if <body>
2803                !!!insert-element ('body');
2804                $self->{insertion_mode} = IN_BODY_IM;
2805                ## reprocess
2806                redo B;
2807              } elsif ($token->{type} == START_TAG_TOKEN) {
2808                if ($token->{tag_name} eq 'head') {
2809                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2810                    !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});
2811                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2812                    push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];
2813                    $self->{insertion_mode} = IN_HEAD_IM;
2814                    !!!next-token;
2815                    redo B;
2816                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2817                    #
2818                  } else {
2819                    !!!parse-error (type => 'in head:head'); # or in head noscript
2820                    ## Ignore the token
2821                    !!!next-token;
2822                    redo B;
2823                  }
2824                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2825                  ## As if <head>
2826                  !!!create-element ($self->{head_element}, 'head');
2827                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2828                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2829    
2830                  $self->{insertion_mode} = IN_HEAD_IM;
2831                  ## Reprocess in the "in head" insertion mode...
2832                }
2833    
2834                if ($token->{tag_name} eq 'base') {
2835                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2836                    ## As if </noscript>
2837                    pop @{$self->{open_elements}};
2838                    !!!parse-error (type => 'in noscript:base');
2839                  
2840                    $self->{insertion_mode} = IN_HEAD_IM;
2841                    ## Reprocess in the "in head" insertion mode...
2842                  }
2843    
2844                  ## NOTE: There is a "as if in head" code clone.
2845                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2846                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2847                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2848                  }
2849                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2850                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2851                  pop @{$self->{open_elements}}
2852                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2853                  !!!next-token;
2854                  redo B;
2855                } elsif ($token->{tag_name} eq 'link') {
2856                  ## NOTE: There is a "as if in head" code clone.
2857                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2858                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2859                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2860                  }
2861                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2862                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2863                  pop @{$self->{open_elements}}
2864                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2865                  !!!next-token;
2866                  redo B;
2867                } elsif ($token->{tag_name} eq 'meta') {
2868                  ## NOTE: There is a "as if in head" code clone.
2869                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2870                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2871                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2872                  }
2873                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2874                  my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2875    
2876                  unless ($self->{confident}) {
2877                    if ($token->{attributes}->{charset}) { ## TODO: And if supported
2878                      $self->{change_encoding}
2879                          ->($self, $token->{attributes}->{charset}->{value});
2880                      
2881                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
2882                          ->set_user_data (manakai_has_reference =>
2883                                               $token->{attributes}->{charset}
2884                                                   ->{has_reference});
2885                    } elsif ($token->{attributes}->{content}) {
2886                      ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
2887                      if ($token->{attributes}->{content}->{value}
2888                          =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=
2889                              [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
2890                              ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
2891                        $self->{change_encoding}
2892                            ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);
2893                        $meta_el->[0]->get_attribute_node_ns (undef, 'content')
2894                            ->set_user_data (manakai_has_reference =>
2895                                                 $token->{attributes}->{content}
2896                                                       ->{has_reference});
2897                      }
2898                    }
2899                  } else {
2900                    if ($token->{attributes}->{charset}) {
2901                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
2902                          ->set_user_data (manakai_has_reference =>
2903                                               $token->{attributes}->{charset}
2904                                                   ->{has_reference});
2905                    }
2906                    if ($token->{attributes}->{content}) {
2907                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
2908                          ->set_user_data (manakai_has_reference =>
2909                                               $token->{attributes}->{content}
2910                                                   ->{has_reference});
2911                    }
2912                  }
2913    
2914                  pop @{$self->{open_elements}}
2915                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2916                  !!!next-token;
2917                  redo B;
2918                } elsif ($token->{tag_name} eq 'title') {
2919                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2920                    ## As if </noscript>
2921                    pop @{$self->{open_elements}};
2922                    !!!parse-error (type => 'in noscript:title');
2923                  
2924                    $self->{insertion_mode} = IN_HEAD_IM;
2925                    ## Reprocess in the "in head" insertion mode...
2926                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2927                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2928                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2929                  }
2930    
2931                  ## NOTE: There is a "as if in head" code clone.
2932                  my $parent = defined $self->{head_element} ? $self->{head_element}
2933                      : $self->{open_elements}->[-1]->[0];
2934                  $parse_rcdata->(RCDATA_CONTENT_MODEL,
2935                                  sub { $parent->append_child ($_[0]) });
2936                  pop @{$self->{open_elements}}
2937                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2938                  redo B;
2939                } elsif ($token->{tag_name} eq 'style') {
2940                  ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
2941                  ## insertion mode IN_HEAD_IM)
2942                  ## NOTE: There is a "as if in head" code clone.
2943                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2944                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2945                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2946                  }
2947                  $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
2948                  pop @{$self->{open_elements}}
2949                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2950                  redo B;
2951                } elsif ($token->{tag_name} eq 'noscript') {
2952                  if ($self->{insertion_mode} == IN_HEAD_IM) {
2953                    ## NOTE: and scripting is disalbed
2954                    !!!insert-element ($token->{tag_name}, $token->{attributes});
2955                    $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
2956                    !!!next-token;
2957                    redo B;
2958                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2959                    !!!parse-error (type => 'in noscript:noscript');
2960                    ## Ignore the token
2961                    !!!next-token;
2962                    redo B;
2963                  } else {
2964                    #
2965                  }
2966                } elsif ($token->{tag_name} eq 'script') {
2967                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2968                    ## As if </noscript>
2969                    pop @{$self->{open_elements}};
2970                    !!!parse-error (type => 'in noscript:script');
2971                  
2972                    $self->{insertion_mode} = IN_HEAD_IM;
2973                    ## Reprocess in the "in head" insertion mode...
2974                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2975                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2976                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2977                  }
2978    
2979                  ## NOTE: There is a "as if in head" code clone.
2980                  $script_start_tag->($insert_to_current);
2981                  pop @{$self->{open_elements}}
2982                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2983                  redo B;
2984                } elsif ($token->{tag_name} eq 'body' or
2985                         $token->{tag_name} eq 'frameset') {
2986                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2987                    ## As if </noscript>
2988                    pop @{$self->{open_elements}};
2989                    !!!parse-error (type => 'in noscript:'.$token->{tag_name});
2990                    
2991                    ## Reprocess in the "in head" insertion mode...
2992                    ## As if </head>
2993                    pop @{$self->{open_elements}};
2994                    
2995                    ## Reprocess in the "after head" insertion mode...
2996                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2997                    pop @{$self->{open_elements}};
2998                    
2999                    ## Reprocess in the "after head" insertion mode...
3000                  }
3001    
3002                  ## "after head" insertion mode
3003                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3004                  if ($token->{tag_name} eq 'body') {
3005                    $self->{insertion_mode} = IN_BODY_IM;
3006                  } elsif ($token->{tag_name} eq 'frameset') {
3007                    $self->{insertion_mode} = IN_FRAMESET_IM;
3008                  } else {
3009                    die "$0: tag name: $self->{tag_name}";
3010                  }
3011                  !!!next-token;
3012                  redo B;
3013                } else {
3014                  #
3015                }
3016    
3017                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3018                  ## As if </noscript>
3019                  pop @{$self->{open_elements}};
3020                  !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
3021                  
3022                  ## Reprocess in the "in head" insertion mode...
3023                  ## As if </head>
3024                  pop @{$self->{open_elements}};
3025    
3026                  ## Reprocess in the "after head" insertion mode...
3027                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3028                  ## As if </head>
3029                  pop @{$self->{open_elements}};
3030    
3031                  ## Reprocess in the "after head" insertion mode...
3032                }
3033    
3034                ## "after head" insertion mode
3035                ## As if <body>
3036                !!!insert-element ('body');
3037                $self->{insertion_mode} = IN_BODY_IM;
3038                ## reprocess
3039                redo B;
3040              } elsif ($token->{type} == END_TAG_TOKEN) {
3041                if ($token->{tag_name} eq 'head') {
3042                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3043                    ## As if <head>
3044                    !!!create-element ($self->{head_element}, 'head');
3045                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3046                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3047    
3048                    ## Reprocess in the "in head" insertion mode...
3049                    pop @{$self->{open_elements}};
3050                    $self->{insertion_mode} = AFTER_HEAD_IM;
3051                    !!!next-token;
3052                    redo B;
3053                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3054                    ## As if </noscript>
3055                    pop @{$self->{open_elements}};
3056                    !!!parse-error (type => 'in noscript:script');
3057                    
3058                    ## Reprocess in the "in head" insertion mode...
3059                    pop @{$self->{open_elements}};
3060                    $self->{insertion_mode} = AFTER_HEAD_IM;
3061                    !!!next-token;
3062                    redo B;
3063                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3064                    pop @{$self->{open_elements}};
3065                    $self->{insertion_mode} = AFTER_HEAD_IM;
3066                    !!!next-token;
3067                    redo B;
3068                  } else {
3069                    #
3070                  }
3071                } elsif ($token->{tag_name} eq 'noscript') {
3072                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3073                    pop @{$self->{open_elements}};
3074                    $self->{insertion_mode} = IN_HEAD_IM;
3075                    !!!next-token;
3076                    redo B;
3077                  } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3078                    !!!parse-error (type => 'unmatched end tag:noscript');
3079                    ## Ignore the token ## ISSUE: An issue in the spec.
3080                    !!!next-token;
3081                    redo B;
3082                  } else {
3083                    #
3084                  }
3085                } elsif ({
3086                          body => 1, html => 1,
3087                         }->{$token->{tag_name}}) {
3088                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3089                    ## As if <head>
3090                    !!!create-element ($self->{head_element}, 'head');
3091                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3092                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3093    
3094                    $self->{insertion_mode} = IN_HEAD_IM;
3095                    ## Reprocess in the "in head" insertion mode...
3096                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3097                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3098                    ## Ignore the token
3099                    !!!next-token;
3100                    redo B;
3101                  }
3102                  
3103                  #
3104                } elsif ({
3105                          p => 1, br => 1,
3106                         }->{$token->{tag_name}}) {
3107                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3108                    ## As if <head>
3109                    !!!create-element ($self->{head_element}, 'head');
3110                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3111                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3112    
3113                    $self->{insertion_mode} = IN_HEAD_IM;
3114                    ## Reprocess in the "in head" insertion mode...
3115                  }
3116    
3117                  #
3118                } else {
3119                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3120                    #
3121                  } else {
3122                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3123                    ## Ignore the token
3124                    !!!next-token;
3125                    redo B;
3126                  }
3127                }
3128    
3129                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3130                  ## As if </noscript>
3131                  pop @{$self->{open_elements}};
3132                  !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
3133                  
3134                  ## Reprocess in the "in head" insertion mode...
3135                  ## As if </head>
3136                  pop @{$self->{open_elements}};
3137    
3138                  ## Reprocess in the "after head" insertion mode...
3139                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3140                  ## As if </head>
3141                  pop @{$self->{open_elements}};
3142    
3143                  ## Reprocess in the "after head" insertion mode...
3144                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3145                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3146                  ## Ignore the token ## ISSUE: An issue in the spec.
3147                  !!!next-token;
3148                  redo B;
3149                }
3150    
3151                ## "after head" insertion mode
3152                ## As if <body>
3153                !!!insert-element ('body');
3154                $self->{insertion_mode} = IN_BODY_IM;
3155                ## reprocess
3156                redo B;
3157              } else {
3158                die "$0: $token->{type}: Unknown token type";
3159              }
3160    
3161              ## ISSUE: An issue in the spec.
3162        } elsif ($self->{insertion_mode} & BODY_IMS) {
3163              if ($token->{type} == CHARACTER_TOKEN) {
3164                ## NOTE: There is a code clone of "character in body".
3165                $reconstruct_active_formatting_elements->($insert_to_current);
3166                
3167                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3168    
3169                !!!next-token;
3170                redo B;
3171              } elsif ($token->{type} == START_TAG_TOKEN) {
3172                if ({
3173                     caption => 1, col => 1, colgroup => 1, tbody => 1,
3174                     td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
3175                    }->{$token->{tag_name}}) {
3176                  if ($self->{insertion_mode} == IN_CELL_IM) {
3177                    ## have an element in table scope
3178                    my $tn;
3179                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3180                      my $node = $self->{open_elements}->[$_];
3181                      if ($node->[1] eq 'td' or $node->[1] eq 'th') {
3182                        $tn = $node->[1];
3183                        last INSCOPE;
3184                      } elsif ({
3185                                table => 1, html => 1,
3186                               }->{$node->[1]}) {
3187                        last INSCOPE;
3188                      }
3189                    } # INSCOPE
3190                      unless (defined $tn) {
3191                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3192                        ## Ignore the token
3193                        !!!next-token;
3194                        redo B;
3195                      }
3196                    
3197                    ## Close the cell
3198                    !!!back-token; # <?>
3199                    $token = {type => END_TAG_TOKEN, tag_name => $tn};
3200                    redo B;
3201                  } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3202                    !!!parse-error (type => 'not closed:caption');
3203                    
3204                    ## As if </caption>
3205                    ## have a table element in table scope
3206                    my $i;
3207                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3208                      my $node = $self->{open_elements}->[$_];
3209                      if ($node->[1] eq 'caption') {
3210                        $i = $_;
3211                        last INSCOPE;
3212                      } elsif ({
3213                                table => 1, html => 1,
3214                               }->{$node->[1]}) {
3215                        last INSCOPE;
3216                      }
3217                    } # INSCOPE
3218                      unless (defined $i) {
3219                        !!!parse-error (type => 'unmatched end tag:caption');
3220                        ## Ignore the token
3221                        !!!next-token;
3222                        redo B;
3223                      }
3224                    
3225                    ## generate implied end tags
3226                    if ({
3227                         dd => 1, dt => 1, li => 1, p => 1,
3228                         td => 1, th => 1, tr => 1,
3229                         tbody => 1, tfoot=> 1, thead => 1,
3230                        }->{$self->{open_elements}->[-1]->[1]}) {
3231                      !!!back-token; # <?>
3232                      $token = {type => END_TAG_TOKEN, tag_name => 'caption'};
3233                      !!!back-token;
3234                      $token = {type => END_TAG_TOKEN,
3235                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3236                      redo B;
3237                    }
3238    
3239                    if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3240                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3241                    }
3242                    
3243                    splice @{$self->{open_elements}}, $i;
3244                    
3245                    $clear_up_to_marker->();
3246                    
3247                    $self->{insertion_mode} = IN_TABLE_IM;
3248                    
3249                    ## reprocess
3250                    redo B;
3251                  } else {
3252                    #
3253                  }
3254                } else {
3255                  #
3256                }
3257              } elsif ($token->{type} == END_TAG_TOKEN) {
3258                if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
3259                  if ($self->{insertion_mode} == IN_CELL_IM) {
3260                    ## have an element in table scope
3261                    my $i;
3262                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3263                      my $node = $self->{open_elements}->[$_];
3264                      if ($node->[1] eq $token->{tag_name}) {
3265                        $i = $_;
3266                        last INSCOPE;
3267                      } elsif ({
3268                                table => 1, html => 1,
3269                               }->{$node->[1]}) {
3270                        last INSCOPE;
3271                      }
3272                    } # INSCOPE
3273                      unless (defined $i) {
3274                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3275                        ## Ignore the token
3276                        !!!next-token;
3277                        redo B;
3278                      }
3279                    
3280                    ## generate implied end tags
3281                    if ({
3282                         dd => 1, dt => 1, li => 1, p => 1,
3283                         td => ($token->{tag_name} eq 'th'),
3284                         th => ($token->{tag_name} eq 'td'),
3285                         tr => 1,
3286                         tbody => 1, tfoot=> 1, thead => 1,
3287                        }->{$self->{open_elements}->[-1]->[1]}) {
3288                      !!!back-token;
3289                      $token = {type => END_TAG_TOKEN,
3290                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3291                      redo B;
3292                    }
3293                    
3294                    if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
3295                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3296                    }
3297                    
3298                    splice @{$self->{open_elements}}, $i;
3299                    
3300                    $clear_up_to_marker->();
3301                    
3302                    $self->{insertion_mode} = IN_ROW_IM;
3303                    
3304                    !!!next-token;
3305                    redo B;
3306                  } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3307                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3308                    ## Ignore the token
3309                    !!!next-token;
3310                    redo B;
3311                  } else {
3312                    #
3313                  }
3314                } elsif ($token->{tag_name} eq 'caption') {
3315                  if ($self->{insertion_mode} == IN_CAPTION_IM) {
3316                    ## have a table element in table scope
3317                    my $i;
3318                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3319                      my $node = $self->{open_elements}->[$_];
3320                      if ($node->[1] eq $token->{tag_name}) {
3321                        $i = $_;
3322                        last INSCOPE;
3323                      } elsif ({
3324                                table => 1, html => 1,
3325                               }->{$node->[1]}) {
3326                        last INSCOPE;
3327                      }
3328                    } # INSCOPE
3329                      unless (defined $i) {
3330                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3331                        ## Ignore the token
3332                        !!!next-token;
3333                        redo B;
3334                      }
3335                    
3336                    ## generate implied end tags
3337                    if ({
3338                         dd => 1, dt => 1, li => 1, p => 1,
3339                         td => 1, th => 1, tr => 1,
3340                         tbody => 1, tfoot=> 1, thead => 1,
3341                        }->{$self->{open_elements}->[-1]->[1]}) {
3342                      !!!back-token;
3343                      $token = {type => END_TAG_TOKEN,
3344                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3345                      redo B;
3346                    }
3347                    
3348                    if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3349                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3350                    }
3351                    
3352                    splice @{$self->{open_elements}}, $i;
3353                    
3354                    $clear_up_to_marker->();
3355                    
3356                    $self->{insertion_mode} = IN_TABLE_IM;
3357                    
3358                    !!!next-token;
3359                    redo B;
3360                  } elsif ($self->{insertion_mode} == IN_CELL_IM) {
3361                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3362                    ## Ignore the token
3363                    !!!next-token;
3364                    redo B;
3365                  } else {
3366                    #
3367                  }
3368                } elsif ({
3369                          table => 1, tbody => 1, tfoot => 1,
3370                          thead => 1, tr => 1,
3371                         }->{$token->{tag_name}} and
3372                         $self->{insertion_mode} == IN_CELL_IM) {
3373                  ## have an element in table scope
3374                  my $i;
3375                  my $tn;
3376                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3377                    my $node = $self->{open_elements}->[$_];
3378                    if ($node->[1] eq $token->{tag_name}) {
3379                      $i = $_;
3380                      last INSCOPE;
3381                    } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {
3382                      $tn = $node->[1];
3383                      ## NOTE: There is exactly one |td| or |th| element
3384                      ## in scope in the stack of open elements by definition.
3385                    } elsif ({
3386                              table => 1, html => 1,
3387                             }->{$node->[1]}) {
3388                      last INSCOPE;
3389                    }
3390                  } # INSCOPE
3391                  unless (defined $i) {
3392                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3393                    ## Ignore the token
3394                    !!!next-token;
3395                    redo B;
3396                  }
3397    
3398                  ## Close the cell
3399                  !!!back-token; # </?>
3400                  $token = {type => END_TAG_TOKEN, tag_name => $tn};
3401                  redo B;
3402                } elsif ($token->{tag_name} eq 'table' and
3403                         $self->{insertion_mode} == IN_CAPTION_IM) {
3404                  !!!parse-error (type => 'not closed:caption');
3405    
3406                  ## As if </caption>
3407                  ## have a table element in table scope
3408                  my $i;
3409                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3410                    my $node = $self->{open_elements}->[$_];
3411                    if ($node->[1] eq 'caption') {
3412                      $i = $_;
3413                      last INSCOPE;
3414                    } elsif ({
3415                              table => 1, html => 1,
3416                             }->{$node->[1]}) {
3417                      last INSCOPE;
3418                    }
3419                  } # INSCOPE
3420                  unless (defined $i) {
3421                    !!!parse-error (type => 'unmatched end tag:caption');
3422                    ## Ignore the token
3423                    !!!next-token;
3424                    redo B;
3425                  }
3426                  
3427                  ## generate implied end tags
3428                  if ({
3429                       dd => 1, dt => 1, li => 1, p => 1,
3430                       td => 1, th => 1, tr => 1,
3431                       tbody => 1, tfoot=> 1, thead => 1,
3432                      }->{$self->{open_elements}->[-1]->[1]}) {
3433                    !!!back-token; # </table>
3434                    $token = {type => END_TAG_TOKEN, tag_name => 'caption'};
3435                    !!!back-token;
3436                    $token = {type => END_TAG_TOKEN,
3437                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3438                    redo B;
3439                  }
3440    
3441                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3442                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3443                  }
3444    
3445                  splice @{$self->{open_elements}}, $i;
3446    
3447                  $clear_up_to_marker->();
3448    
3449                  $self->{insertion_mode} = IN_TABLE_IM;
3450    
3451                  ## reprocess
3452                  redo B;
3453                } elsif ({
3454                          body => 1, col => 1, colgroup => 1, html => 1,
3455                         }->{$token->{tag_name}}) {
3456                  if ($self->{insertion_mode} & BODY_TABLE_IMS) {
3457                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3458                    ## Ignore the token
3459                    !!!next-token;
3460                    redo B;
3461                  } else {
3462                    #
3463                  }
3464                } elsif ({
3465                          tbody => 1, tfoot => 1,
3466                          thead => 1, tr => 1,
3467                         }->{$token->{tag_name}} and
3468                         $self->{insertion_mode} == IN_CAPTION_IM) {
3469                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3470                  ## Ignore the token
3471                  !!!next-token;
3472                  redo B;
3473                } else {
3474                  #
3475                }
3476          } else {
3477            die "$0: $token->{type}: Unknown token type";
3478          }
3479    
3480          $insert = $insert_to_current;
3481          #
3482        } elsif ($self->{insertion_mode} & TABLE_IMS) {
3483          if ($token->{type} == CHARACTER_TOKEN) {
3484                if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3485                  $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3486                  
3487                  unless (length $token->{data}) {
3488                    !!!next-token;
3489                    redo B;
3490                  }
3491                }
3492    
3493                !!!parse-error (type => 'in table:#character');
3494    
3495                ## As if in body, but insert into foster parent element
3496                ## ISSUE: Spec says that "whenever a node would be inserted
3497                ## into the current node" while characters might not be
3498                ## result in a new Text node.
3499                $reconstruct_active_formatting_elements->($insert_to_foster);
3500                
3501                if ({
3502                     table => 1, tbody => 1, tfoot => 1,
3503                     thead => 1, tr => 1,
3504                    }->{$self->{open_elements}->[-1]->[1]}) {
3505                  # MUST
3506                  my $foster_parent_element;
3507                  my $next_sibling;
3508                  my $prev_sibling;
3509                  OE: for (reverse 0..$#{$self->{open_elements}}) {
3510                    if ($self->{open_elements}->[$_]->[1] eq 'table') {
3511                      my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3512                      if (defined $parent and $parent->node_type == 1) {
3513                        $foster_parent_element = $parent;
3514                        $next_sibling = $self->{open_elements}->[$_]->[0];
3515                        $prev_sibling = $next_sibling->previous_sibling;
3516                      } else {
3517                        $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
3518                        $prev_sibling = $foster_parent_element->last_child;
3519                      }
3520                      last OE;
3521                    }
3522                  } # OE
3523                  $foster_parent_element = $self->{open_elements}->[0]->[0] and
3524                  $prev_sibling = $foster_parent_element->last_child
3525                    unless defined $foster_parent_element;
3526                  if (defined $prev_sibling and
3527                      $prev_sibling->node_type == 3) {
3528                    $prev_sibling->manakai_append_text ($token->{data});
3529                  } else {
3530                    $foster_parent_element->insert_before
3531                      ($self->{document}->create_text_node ($token->{data}),
3532                       $next_sibling);
3533                  }
3534                } else {
3535                  $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3536                }
3537                
3538                !!!next-token;
3539                redo B;
3540          } elsif ($token->{type} == START_TAG_TOKEN) {
3541                if ({
3542                     tr => ($self->{insertion_mode} != IN_ROW_IM),
3543                     th => 1, td => 1,
3544                    }->{$token->{tag_name}}) {
3545                  if ($self->{insertion_mode} == IN_TABLE_IM) {
3546                    ## Clear back to table context
3547                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
3548                           $self->{open_elements}->[-1]->[1] ne 'html') {
3549                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3550                      pop @{$self->{open_elements}};
3551                    }
3552                    
3553                    !!!insert-element ('tbody');
3554                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
3555                    ## reprocess in the "in table body" insertion mode...
3556                  }
3557    
3558                  if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3559                    unless ($token->{tag_name} eq 'tr') {
3560                      !!!parse-error (type => 'missing start tag:tr');
3561                    }
3562                    
3563                    ## Clear back to table body context
3564                    while (not {
3565                      tbody => 1, tfoot => 1, thead => 1, html => 1,
3566                    }->{$self->{open_elements}->[-1]->[1]}) {
3567                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3568                      pop @{$self->{open_elements}};
3569                    }
3570                    
3571                    $self->{insertion_mode} = IN_ROW_IM;
3572                    if ($token->{tag_name} eq 'tr') {
3573                      !!!insert-element ($token->{tag_name}, $token->{attributes});
3574                      !!!next-token;
3575                      redo B;
3576                    } else {
3577                      !!!insert-element ('tr');
3578                      ## reprocess in the "in row" insertion mode
3579                    }
3580                  }
3581    
3582                  ## Clear back to table row context
3583                  while (not {
3584                    tr => 1, html => 1,
3585                  }->{$self->{open_elements}->[-1]->[1]}) {
3586                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3587                    pop @{$self->{open_elements}};
3588                  }
3589                  
3590                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3591                  $self->{insertion_mode} = IN_CELL_IM;
3592    
3593                  push @$active_formatting_elements, ['#marker', ''];
3594                  
3595                  !!!next-token;
3596                  redo B;
3597                } elsif ({
3598                          caption => 1, col => 1, colgroup => 1,
3599                          tbody => 1, tfoot => 1, thead => 1,
3600                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
3601                         }->{$token->{tag_name}}) {
3602                  if ($self->{insertion_mode} == IN_ROW_IM) {
3603                    ## As if </tr>
3604                    ## have an element in table scope
3605                    my $i;
3606                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3607                      my $node = $self->{open_elements}->[$_];
3608                      if ($node->[1] eq 'tr') {
3609                        $i = $_;
3610                        last INSCOPE;
3611                      } elsif ({
3612                                table => 1, html => 1,
3613                               }->{$node->[1]}) {
3614                        last INSCOPE;
3615                      }
3616                    } # INSCOPE
3617                    unless (defined $i) {
3618                      !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});
3619                      ## Ignore the token
3620                      !!!next-token;
3621                      redo B;
3622                    }
3623                    
3624                    ## Clear back to table row context
3625                    while (not {
3626                      tr => 1, html => 1,
3627                    }->{$self->{open_elements}->[-1]->[1]}) {
3628                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3629                      pop @{$self->{open_elements}};
3630                    }
3631                    
3632                    pop @{$self->{open_elements}}; # tr
3633                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
3634                    if ($token->{tag_name} eq 'tr') {
3635                      ## reprocess
3636                      redo B;
3637                    } else {
3638                      ## reprocess in the "in table body" insertion mode...
3639                    }
3640                  }
3641    
3642                  if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3643                    ## have an element in table scope
3644                    my $i;
3645                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3646                      my $node = $self->{open_elements}->[$_];
3647                      if ({
3648                           tbody => 1, thead => 1, tfoot => 1,
3649                          }->{$node->[1]}) {
3650                        $i = $_;
3651                        last INSCOPE;
3652                      } elsif ({
3653                                table => 1, html => 1,
3654                               }->{$node->[1]}) {
3655                        last INSCOPE;
3656                      }
3657                    } # INSCOPE
3658                    unless (defined $i) {
3659                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3660                      ## Ignore the token
3661                      !!!next-token;
3662                      redo B;
3663                    }
3664    
3665                    ## Clear back to table body context
3666                    while (not {
3667                      tbody => 1, tfoot => 1, thead => 1, html => 1,
3668                    }->{$self->{open_elements}->[-1]->[1]}) {
3669                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3670                      pop @{$self->{open_elements}};
3671                    }
3672                    
3673                    ## As if <{current node}>
3674                    ## have an element in table scope
3675                    ## true by definition
3676                    
3677                    ## Clear back to table body context
3678                    ## nop by definition
3679                    
3680                    pop @{$self->{open_elements}};
3681                    $self->{insertion_mode} = IN_TABLE_IM;
3682                    ## reprocess in "in table" insertion mode...
3683                  }
3684    
3685                  if ($token->{tag_name} eq 'col') {
3686                    ## Clear back to table context
3687                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
3688                           $self->{open_elements}->[-1]->[1] ne 'html') {
3689                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3690                      pop @{$self->{open_elements}};
3691                    }
3692                    
3693                    !!!insert-element ('colgroup');
3694                    $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
3695                    ## reprocess
3696                    redo B;
3697                  } elsif ({
3698                            caption => 1,
3699                            colgroup => 1,
3700                            tbody => 1, tfoot => 1, thead => 1,
3701                           }->{$token->{tag_name}}) {
3702                    ## Clear back to table context
3703                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
3704                           $self->{open_elements}->[-1]->[1] ne 'html') {
3705                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3706                      pop @{$self->{open_elements}};
3707                    }
3708                    
3709                    push @$active_formatting_elements, ['#marker', '']
3710                        if $token->{tag_name} eq 'caption';
3711                    
3712                    !!!insert-element ($token->{tag_name}, $token->{attributes});
3713                    $self->{insertion_mode} = {
3714                                               caption => IN_CAPTION_IM,
3715                                               colgroup => IN_COLUMN_GROUP_IM,
3716                                               tbody => IN_TABLE_BODY_IM,
3717                                               tfoot => IN_TABLE_BODY_IM,
3718                                               thead => IN_TABLE_BODY_IM,
3719                                              }->{$token->{tag_name}};
3720                    !!!next-token;
3721                    redo B;
3722                  } else {
3723                    die "$0: in table: <>: $token->{tag_name}";
3724                  }
3725                } elsif ($token->{tag_name} eq 'table') {
3726                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3727    
3728                  ## As if </table>
3729                  ## have a table element in table scope
3730                  my $i;
3731                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3732                    my $node = $self->{open_elements}->[$_];
3733                    if ($node->[1] eq 'table') {
3734                      $i = $_;
3735                      last INSCOPE;
3736                    } elsif ({
3737                              table => 1, html => 1,
3738                             }->{$node->[1]}) {
3739                      last INSCOPE;
3740                    }
3741                  } # INSCOPE
3742                  unless (defined $i) {
3743                    !!!parse-error (type => 'unmatched end tag:table');
3744                    ## Ignore tokens </table><table>
3745                    !!!next-token;
3746                    redo B;
3747                  }
3748                  
3749                  ## generate implied end tags
3750                  if ({
3751                       dd => 1, dt => 1, li => 1, p => 1,
3752                       td => 1, th => 1, tr => 1,
3753                       tbody => 1, tfoot=> 1, thead => 1,
3754                      }->{$self->{open_elements}->[-1]->[1]}) {
3755                    !!!back-token; # <table>
3756                    $token = {type => END_TAG_TOKEN, tag_name => 'table'};
3757                    !!!back-token;
3758                    $token = {type => END_TAG_TOKEN,
3759                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3760                    redo B;
3761                  }
3762    
3763                  if ($self->{open_elements}->[-1]->[1] ne 'table') {
3764                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3765                  }
3766    
3767                  splice @{$self->{open_elements}}, $i;
3768    
3769                  $self->_reset_insertion_mode;
3770    
3771                  ## reprocess
3772                  redo B;
3773            } else {
3774              !!!parse-error (type => 'in table:'.$token->{tag_name});
3775    
3776              $insert = $insert_to_foster;
3777              #
3778            }
3779          } elsif ($token->{type} == END_TAG_TOKEN) {
3780                if ($token->{tag_name} eq 'tr' and
3781                    $self->{insertion_mode} == IN_ROW_IM) {
3782                  ## have an element in table scope
3783                  my $i;
3784                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3785                    my $node = $self->{open_elements}->[$_];
3786                    if ($node->[1] eq $token->{tag_name}) {
3787                      $i = $_;
3788                      last INSCOPE;
3789                    } elsif ({
3790                              table => 1, html => 1,
3791                             }->{$node->[1]}) {
3792                      last INSCOPE;
3793                    }
3794                  } # INSCOPE
3795                  unless (defined $i) {
3796                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3797                    ## Ignore the token
3798                    !!!next-token;
3799                    redo B;
3800                  }
3801    
3802                  ## Clear back to table row context
3803                  while (not {
3804                    tr => 1, html => 1,
3805                  }->{$self->{open_elements}->[-1]->[1]}) {
3806                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3807                    pop @{$self->{open_elements}};
3808                  }
3809    
3810                  pop @{$self->{open_elements}}; # tr
3811                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
3812                  !!!next-token;
3813                  redo B;
3814                } elsif ($token->{tag_name} eq 'table') {
3815                  if ($self->{insertion_mode} == IN_ROW_IM) {
3816                    ## As if </tr>
3817                    ## have an element in table scope
3818                    my $i;
3819                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3820                      my $node = $self->{open_elements}->[$_];
3821                      if ($node->[1] eq 'tr') {
3822                        $i = $_;
3823                        last INSCOPE;
3824                      } elsif ({
3825                                table => 1, html => 1,
3826                               }->{$node->[1]}) {
3827                        last INSCOPE;
3828                      }
3829                    } # INSCOPE
3830                    unless (defined $i) {
3831                      !!!parse-error (type => 'unmatched end tag:'.$token->{type});
3832                      ## Ignore the token
3833                      !!!next-token;
3834                      redo B;
3835                    }
3836                    
3837                    ## Clear back to table row context
3838                    while (not {
3839                      tr => 1, html => 1,
3840                    }->{$self->{open_elements}->[-1]->[1]}) {
3841                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3842                      pop @{$self->{open_elements}};
3843                    }
3844                    
3845                    pop @{$self->{open_elements}}; # tr
3846                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
3847                    ## reprocess in the "in table body" insertion mode...
3848                  }
3849    
3850                  if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3851                    ## have an element in table scope
3852                    my $i;
3853                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3854                      my $node = $self->{open_elements}->[$_];
3855                      if ({
3856                           tbody => 1, thead => 1, tfoot => 1,
3857                          }->{$node->[1]}) {
3858                        $i = $_;
3859                        last INSCOPE;
3860                      } elsif ({
3861                                table => 1, html => 1,
3862                               }->{$node->[1]}) {
3863                        last INSCOPE;
3864                      }
3865                    } # INSCOPE
3866                    unless (defined $i) {
3867                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3868                      ## Ignore the token
3869                      !!!next-token;
3870                      redo B;
3871                    }
3872                    
3873                    ## Clear back to table body context
3874                    while (not {
3875                      tbody => 1, tfoot => 1, thead => 1, html => 1,
3876                    }->{$self->{open_elements}->[-1]->[1]}) {
3877                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3878                      pop @{$self->{open_elements}};
3879                    }
3880                    
3881                    ## As if <{current node}>
3882                    ## have an element in table scope
3883                    ## true by definition
3884                    
3885                    ## Clear back to table body context
3886                    ## nop by definition
3887                    
3888                    pop @{$self->{open_elements}};
3889                    $self->{insertion_mode} = IN_TABLE_IM;
3890                    ## reprocess in the "in table" insertion mode...
3891                  }
3892    
3893                  ## have a table element in table scope
3894                  my $i;
3895                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3896                    my $node = $self->{open_elements}->[$_];
3897                    if ($node->[1] eq $token->{tag_name}) {
3898                      $i = $_;
3899                      last INSCOPE;
3900                    } elsif ({
3901                              table => 1, html => 1,
3902                             }->{$node->[1]}) {
3903                      last INSCOPE;
3904                    }
3905                  } # INSCOPE
3906                  unless (defined $i) {
3907                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3908                    ## Ignore the token
3909                    !!!next-token;
3910                    redo B;
3911                  }
3912    
3913                  ## generate implied end tags
3914                  if ({
3915                       dd => 1, dt => 1, li => 1, p => 1,
3916                       td => 1, th => 1, tr => 1,
3917                       tbody => 1, tfoot=> 1, thead => 1,
3918                      }->{$self->{open_elements}->[-1]->[1]}) {
3919                    !!!back-token;
3920                    $token = {type => END_TAG_TOKEN,
3921                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3922                    redo B;
3923                  }
3924                  
3925                  if ($self->{open_elements}->[-1]->[1] ne 'table') {
3926                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3927                  }
3928                    
3929                  splice @{$self->{open_elements}}, $i;
3930                  
3931                  $self->_reset_insertion_mode;
3932                  
3933                  !!!next-token;
3934                  redo B;
3935                } elsif ({
3936                          tbody => 1, tfoot => 1, thead => 1,
3937                         }->{$token->{tag_name}} and
3938                         $self->{insertion_mode} & ROW_IMS) {
3939                  if ($self->{insertion_mode} == IN_ROW_IM) {
3940                    ## have an element in table scope
3941                    my $i;
3942                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3943                      my $node = $self->{open_elements}->[$_];
3944                      if ($node->[1] eq $token->{tag_name}) {
3945                        $i = $_;
3946                        last INSCOPE;
3947                      } elsif ({
3948                                table => 1, html => 1,
3949                               }->{$node->[1]}) {
3950                        last INSCOPE;
3951                      }
3952                    } # INSCOPE
3953                      unless (defined $i) {
3954                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3955                        ## Ignore the token
3956                        !!!next-token;
3957                        redo B;
3958                      }
3959                    
3960                    ## As if </tr>
3961                    ## have an element in table scope
3962                    my $i;
3963                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3964                      my $node = $self->{open_elements}->[$_];
3965                      if ($node->[1] eq 'tr') {
3966                        $i = $_;
3967                        last INSCOPE;
3968                      } elsif ({
3969                                table => 1, html => 1,
3970                               }->{$node->[1]}) {
3971                        last INSCOPE;
3972                      }
3973                    } # INSCOPE
3974                      unless (defined $i) {
3975                        !!!parse-error (type => 'unmatched end tag:tr');
3976                        ## Ignore the token
3977                        !!!next-token;
3978                        redo B;
3979                      }
3980                    
3981                    ## Clear back to table row context
3982                    while (not {
3983                      tr => 1, html => 1,
3984                    }->{$self->{open_elements}->[-1]->[1]}) {
3985                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3986                      pop @{$self->{open_elements}};
3987                    }
3988                    
3989                    pop @{$self->{open_elements}}; # tr
3990                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
3991                    ## reprocess in the "in table body" insertion mode...
3992                  }
3993    
3994                  ## have an element in table scope
3995                  my $i;
3996                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3997                    my $node = $self->{open_elements}->[$_];
3998                    if ($node->[1] eq $token->{tag_name}) {
3999                      $i = $_;
4000                      last INSCOPE;
4001                    } elsif ({
4002                              table => 1, html => 1,
4003                             }->{$node->[1]}) {
4004                      last INSCOPE;
4005                    }
4006                  } # INSCOPE
4007                  unless (defined $i) {
4008                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4009                    ## Ignore the token
4010                    !!!next-token;
4011                    redo B;
4012                  }
4013    
4014                  ## Clear back to table body context
4015                  while (not {
4016                    tbody => 1, tfoot => 1, thead => 1, html => 1,
4017                  }->{$self->{open_elements}->[-1]->[1]}) {
4018                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4019                    pop @{$self->{open_elements}};
4020                  }
4021    
4022                  pop @{$self->{open_elements}};
4023                  $self->{insertion_mode} = IN_TABLE_IM;
4024                  !!!next-token;
4025                  redo B;
4026                } elsif ({
4027                          body => 1, caption => 1, col => 1, colgroup => 1,
4028                          html => 1, td => 1, th => 1,
4029                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
4030                          tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
4031                         }->{$token->{tag_name}}) {
4032                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4033                  ## Ignore the token
4034                  !!!next-token;
4035                  redo B;
4036            } else {
4037              !!!parse-error (type => 'in table:/'.$token->{tag_name});
4038    
4039              $insert = $insert_to_foster;
4040              #
4041            }
4042          } else {
4043            die "$0: $token->{type}: Unknown token type";
4044          }
4045        } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
4046              if ($token->{type} == CHARACTER_TOKEN) {
4047                if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4048                  $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4049                  unless (length $token->{data}) {
4050                    !!!next-token;
4051                    redo B;
4052                  }
4053                }
4054                
4055                #
4056              } elsif ($token->{type} == START_TAG_TOKEN) {
4057                if ($token->{tag_name} eq 'col') {
4058                  !!!insert-element ($token->{tag_name}, $token->{attributes});
4059                  pop @{$self->{open_elements}};
4060                  !!!next-token;
4061                  redo B;
4062                } else {
4063                  #
4064                }
4065              } elsif ($token->{type} == END_TAG_TOKEN) {
4066                if ($token->{tag_name} eq 'colgroup') {
4067                  if ($self->{open_elements}->[-1]->[1] eq 'html') {
4068                    !!!parse-error (type => 'unmatched end tag:colgroup');
4069                    ## Ignore the token
4070                    !!!next-token;
4071                    redo B;
4072                  } else {
4073                    pop @{$self->{open_elements}}; # colgroup
4074                    $self->{insertion_mode} = IN_TABLE_IM;
4075                    !!!next-token;
4076                    redo B;            
4077                  }
4078                } elsif ($token->{tag_name} eq 'col') {
4079                  !!!parse-error (type => 'unmatched end tag:col');
4080                  ## Ignore the token
4081                  !!!next-token;
4082                  redo B;
4083                } else {
4084                  #
4085                }
4086              } else {
4087                #
4088              }
4089    
4090              ## As if </colgroup>
4091              if ($self->{open_elements}->[-1]->[1] eq 'html') {
4092                !!!parse-error (type => 'unmatched end tag:colgroup');
4093                ## Ignore the token
4094                !!!next-token;
4095                redo B;
4096              } else {
4097                pop @{$self->{open_elements}}; # colgroup
4098                $self->{insertion_mode} = IN_TABLE_IM;
4099                ## reprocess
4100                redo B;
4101              }
4102        } elsif ($self->{insertion_mode} == IN_SELECT_IM) {
4103          if ($token->{type} == CHARACTER_TOKEN) {
4104            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4105            !!!next-token;
4106            redo B;
4107          } elsif ($token->{type} == START_TAG_TOKEN) {
4108                if ($token->{tag_name} eq 'option') {
4109                  if ($self->{open_elements}->[-1]->[1] eq 'option') {
4110                    ## As if </option>
4111                    pop @{$self->{open_elements}};
4112                  }
4113    
4114                  !!!insert-element ($token->{tag_name}, $token->{attributes});
4115                  !!!next-token;
4116                  redo B;
4117                } elsif ($token->{tag_name} eq 'optgroup') {
4118                  if ($self->{open_elements}->[-1]->[1] eq 'option') {
4119                    ## As if </option>
4120                    pop @{$self->{open_elements}};
4121                  }
4122    
4123                  if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {
4124                    ## As if </optgroup>
4125                    pop @{$self->{open_elements}};
4126                  }
4127    
4128                  !!!insert-element ($token->{tag_name}, $token->{attributes});
4129                  !!!next-token;
4130                  redo B;
4131                } elsif ($token->{tag_name} eq 'select') {
4132                  !!!parse-error (type => 'not closed:select');
4133                  ## As if </select> instead
4134                  ## have an element in table scope
4135                  my $i;
4136                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4137                    my $node = $self->{open_elements}->[$_];
4138                    if ($node->[1] eq $token->{tag_name}) {
4139                      $i = $_;
4140                      last INSCOPE;
4141                    } elsif ({
4142                              table => 1, html => 1,
4143                             }->{$node->[1]}) {
4144                      last INSCOPE;
4145                    }
4146                  } # INSCOPE
4147                  unless (defined $i) {
4148                    !!!parse-error (type => 'unmatched end tag:select');
4149                    ## Ignore the token
4150                    !!!next-token;
4151                    redo B;
4152                  }
4153                  
4154                  splice @{$self->{open_elements}}, $i;
4155    
4156                  $self->_reset_insertion_mode;
4157    
4158                  !!!next-token;
4159                  redo B;
4160            } else {
4161              !!!parse-error (type => 'in select:'.$token->{tag_name});
4162              ## Ignore the token
4163              !!!next-token;
4164              redo B;
4165            }
4166          } elsif ($token->{type} == END_TAG_TOKEN) {
4167                if ($token->{tag_name} eq 'optgroup') {
4168                  if ($self->{open_elements}->[-1]->[1] eq 'option' and
4169                      $self->{open_elements}->[-2]->[1] eq 'optgroup') {
4170                    ## As if </option>
4171                    splice @{$self->{open_elements}}, -2;
4172                  } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {
4173                    pop @{$self->{open_elements}};
4174                  } else {
4175                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4176                    ## Ignore the token
4177                  }
4178                  !!!next-token;
4179                  redo B;
4180                } elsif ($token->{tag_name} eq 'option') {
4181                  if ($self->{open_elements}->[-1]->[1] eq 'option') {
4182                    pop @{$self->{open_elements}};
4183                  } else {
4184                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4185                    ## Ignore the token
4186                  }
4187                  !!!next-token;
4188                  redo B;
4189                } elsif ($token->{tag_name} eq 'select') {
4190                  ## have an element in table scope
4191                  my $i;
4192                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4193                    my $node = $self->{open_elements}->[$_];
4194                    if ($node->[1] eq $token->{tag_name}) {
4195                      $i = $_;
4196                      last INSCOPE;
4197                    } elsif ({
4198                              table => 1, html => 1,
4199                             }->{$node->[1]}) {
4200                      last INSCOPE;
4201                    }
4202                  } # INSCOPE
4203                  unless (defined $i) {
4204                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4205                    ## Ignore the token
4206                    !!!next-token;
4207                    redo B;
4208                  }
4209                  
4210                  splice @{$self->{open_elements}}, $i;
4211    
4212                  $self->_reset_insertion_mode;
4213    
4214                  !!!next-token;
4215                  redo B;
4216                } elsif ({
4217                          caption => 1, table => 1, tbody => 1,
4218                          tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
4219                         }->{$token->{tag_name}}) {
4220                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4221                  
4222                  ## have an element in table scope
4223                  my $i;
4224                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4225                    my $node = $self->{open_elements}->[$_];
4226                    if ($node->[1] eq $token->{tag_name}) {
4227                      $i = $_;
4228                      last INSCOPE;
4229                    } elsif ({
4230                              table => 1, html => 1,
4231                             }->{$node->[1]}) {
4232                      last INSCOPE;
4233                    }
4234                  } # INSCOPE
4235                  unless (defined $i) {
4236                    ## Ignore the token
4237                    !!!next-token;
4238                    redo B;
4239                  }
4240                  
4241                  ## As if </select>
4242                  ## have an element in table scope
4243                  undef $i;
4244                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4245                    my $node = $self->{open_elements}->[$_];
4246                    if ($node->[1] eq 'select') {
4247                      $i = $_;
4248                      last INSCOPE;
4249                    } elsif ({
4250                              table => 1, html => 1,
4251                             }->{$node->[1]}) {
4252                      last INSCOPE;
4253                    }
4254                  } # INSCOPE
4255                  unless (defined $i) {
4256                    !!!parse-error (type => 'unmatched end tag:select');
4257                    ## Ignore the </select> token
4258                    !!!next-token; ## TODO: ok?
4259                    redo B;
4260                  }
4261                  
4262                  splice @{$self->{open_elements}}, $i;
4263    
4264                  $self->_reset_insertion_mode;
4265    
4266                  ## reprocess
4267                  redo B;
4268            } else {
4269              !!!parse-error (type => 'in select:/'.$token->{tag_name});
4270              ## Ignore the token
4271              !!!next-token;
4272              redo B;
4273            }
4274          } else {
4275            die "$0: $token->{type}: Unknown token type";
4276          }
4277        } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
4278          if ($token->{type} == CHARACTER_TOKEN) {
4279            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4280              my $data = $1;
4281              ## As if in body
4282              $reconstruct_active_formatting_elements->($insert_to_current);
4283                  
4284              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4285              
4286              unless (length $token->{data}) {
4287                !!!next-token;
4288                redo B;
4289              }
4290            }
4291            
4292            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4293              !!!parse-error (type => 'after html:#character');
4294    
4295              ## Reprocess in the "main" phase, "after body" insertion mode...
4296            }
4297            
4298            ## "after body" insertion mode
4299            !!!parse-error (type => 'after body:#character');
4300    
4301            $self->{insertion_mode} = IN_BODY_IM;
4302            ## reprocess
4303            redo B;
4304          } elsif ($token->{type} == START_TAG_TOKEN) {
4305            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4306              !!!parse-error (type => 'after html:'.$token->{tag_name});
4307              
4308              ## Reprocess in the "main" phase, "after body" insertion mode...
4309            }
4310    
4311            ## "after body" insertion mode
4312            !!!parse-error (type => 'after body:'.$token->{tag_name});
4313    
4314            $self->{insertion_mode} = IN_BODY_IM;
4315            ## reprocess
4316            redo B;
4317          } elsif ($token->{type} == END_TAG_TOKEN) {
4318            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4319              !!!parse-error (type => 'after html:/'.$token->{tag_name});
4320              
4321              $self->{insertion_mode} = AFTER_BODY_IM;
4322              ## Reprocess in the "main" phase, "after body" insertion mode...
4323            }
4324    
4325            ## "after body" insertion mode
4326            if ($token->{tag_name} eq 'html') {
4327              if (defined $self->{inner_html_node}) {
4328                !!!parse-error (type => 'unmatched end tag:html');
4329                ## Ignore the token
4330                !!!next-token;
4331                redo B;
4332              } else {
4333                $self->{insertion_mode} = AFTER_HTML_BODY_IM;
4334                !!!next-token;
4335                redo B;
4336              }
4337            } else {
4338              !!!parse-error (type => 'after body:/'.$token->{tag_name});
4339    
4340              $self->{insertion_mode} = IN_BODY_IM;
4341              ## reprocess
4342              redo B;
4343            }
4344          } else {
4345            die "$0: $token->{type}: Unknown token type";
4346          }
4347        } elsif ($self->{insertion_mode} & FRAME_IMS) {
4348          if ($token->{type} == CHARACTER_TOKEN) {
4349            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4350              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4351              
4352              unless (length $token->{data}) {
4353                !!!next-token;
4354                redo B;
4355              }
4356            }
4357            
4358            if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
4359              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4360                !!!parse-error (type => 'in frameset:#character');
4361              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
4362                !!!parse-error (type => 'after frameset:#character');
4363              } else { # "after html frameset"
4364                !!!parse-error (type => 'after html:#character');
4365    
4366                $self->{insertion_mode} = AFTER_FRAMESET_IM;
4367                ## Reprocess in the "main" phase, "after frameset"...
4368                !!!parse-error (type => 'after frameset:#character');
4369              }
4370              
4371              ## Ignore the token.
4372              if (length $token->{data}) {
4373                ## reprocess the rest of characters
4374              } else {
4375                !!!next-token;
4376              }
4377              redo B;
4378            }
4379            
4380            die qq[$0: Character "$token->{data}"];
4381          } elsif ($token->{type} == START_TAG_TOKEN) {
4382            if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4383              !!!parse-error (type => 'after html:'.$token->{tag_name});
4384    
4385              $self->{insertion_mode} = AFTER_FRAMESET_IM;
4386              ## Process in the "main" phase, "after frameset" insertion mode...
4387            }
4388    
4389            if ($token->{tag_name} eq 'frameset' and
4390                $self->{insertion_mode} == IN_FRAMESET_IM) {
4391              !!!insert-element ($token->{tag_name}, $token->{attributes});
4392              !!!next-token;
4393              redo B;
4394            } elsif ($token->{tag_name} eq 'frame' and
4395                     $self->{insertion_mode} == IN_FRAMESET_IM) {
4396              !!!insert-element ($token->{tag_name}, $token->{attributes});
4397              pop @{$self->{open_elements}};
4398              !!!next-token;
4399              redo B;
4400            } elsif ($token->{tag_name} eq 'noframes') {
4401              ## NOTE: As if in body.
4402              $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
4403              redo B;
4404            } else {
4405              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4406                !!!parse-error (type => 'in frameset:'.$token->{tag_name});
4407              } else {
4408                !!!parse-error (type => 'after frameset:'.$token->{tag_name});
4409              }
4410              ## Ignore the token
4411              !!!next-token;
4412              redo B;
4413            }
4414          } elsif ($token->{type} == END_TAG_TOKEN) {
4415            if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4416              !!!parse-error (type => 'after html:/'.$token->{tag_name});
4417    
4418              $self->{insertion_mode} = AFTER_FRAMESET_IM;
4419              ## Process in the "main" phase, "after frameset" insertion mode...
4420            }
4421    
4422            if ($token->{tag_name} eq 'frameset' and
4423                $self->{insertion_mode} == IN_FRAMESET_IM) {
4424              if ($self->{open_elements}->[-1]->[1] eq 'html' and
4425                  @{$self->{open_elements}} == 1) {
4426                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4427                ## Ignore the token
4428                !!!next-token;
4429              } else {
4430                pop @{$self->{open_elements}};
4431                !!!next-token;
4432              }
4433    
4434              if (not defined $self->{inner_html_node} and
4435                  $self->{open_elements}->[-1]->[1] ne 'frameset') {
4436                $self->{insertion_mode} = AFTER_FRAMESET_IM;
4437              }
4438              redo B;
4439            } elsif ($token->{tag_name} eq 'html' and
4440                     $self->{insertion_mode} == AFTER_FRAMESET_IM) {
4441              $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
4442              !!!next-token;
4443              redo B;
4444            } else {
4445              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4446                !!!parse-error (type => 'in frameset:/'.$token->{tag_name});
4447              } else {
4448                !!!parse-error (type => 'after frameset:/'.$token->{tag_name});
4449              }
4450              ## Ignore the token
4451              !!!next-token;
4452              redo B;
4453            }
4454          } else {
4455            die "$0: $token->{type}: Unknown token type";
4456          }
4457    
4458          ## ISSUE: An issue in spec here
4459        } else {
4460          die "$0: $self->{insertion_mode}: Unknown insertion mode";
4461        }
4462    
4463        ## "in body" insertion mode
4464        if ($token->{type} == START_TAG_TOKEN) {
4465        if ($token->{tag_name} eq 'script') {        if ($token->{tag_name} eq 'script') {
4466          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
4467          $script_start_tag->($insert);          $script_start_tag->($insert);
4468          return;          redo B;
4469        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
4470          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
4471          $parse_rcdata->('CDATA', $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4472          return;          redo B;
4473        } elsif ({        } elsif ({
4474                  base => 1, link => 1, meta => 1,                  base => 1, link => 1,
4475                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
4476          ## 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
4477          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4478          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4479          !!!next-token;          !!!next-token;
4480          ## TODO: Extracting |charset| from |meta|.          redo B;
4481          return;        } elsif ($token->{tag_name} eq 'meta') {
4482            ## NOTE: This is an "as if in head" code clone, only "-t" differs
4483            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4484            my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4485    
4486            unless ($self->{confident}) {
4487              if ($token->{attributes}->{charset}) { ## TODO: And if supported
4488                $self->{change_encoding}
4489                    ->($self, $token->{attributes}->{charset}->{value});
4490                
4491                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4492                    ->set_user_data (manakai_has_reference =>
4493                                         $token->{attributes}->{charset}
4494                                             ->{has_reference});
4495              } elsif ($token->{attributes}->{content}) {
4496                ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
4497                if ($token->{attributes}->{content}->{value}
4498                    =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=
4499                        [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4500                        ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
4501                  $self->{change_encoding}
4502                      ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);
4503                  $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4504                      ->set_user_data (manakai_has_reference =>
4505                                           $token->{attributes}->{content}
4506                                                 ->{has_reference});
4507                }
4508              }
4509            } else {
4510              if ($token->{attributes}->{charset}) {
4511                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4512                    ->set_user_data (manakai_has_reference =>
4513                                         $token->{attributes}->{charset}
4514                                             ->{has_reference});
4515              }
4516              if ($token->{attributes}->{content}) {
4517                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4518                    ->set_user_data (manakai_has_reference =>
4519                                         $token->{attributes}->{content}
4520                                             ->{has_reference});
4521              }
4522            }
4523    
4524            !!!next-token;
4525            redo B;
4526        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
4527          !!!parse-error (type => 'in body:title');          !!!parse-error (type => 'in body:title');
4528          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
4529          $parse_rcdata->('RCDATA', sub {          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {
4530            if (defined $self->{head_element}) {            if (defined $self->{head_element}) {
4531              $self->{head_element}->append_child ($_[0]);              $self->{head_element}->append_child ($_[0]);
4532            } else {            } else {
4533              $insert->($_[0]);              $insert->($_[0]);
4534            }            }
4535          });          });
4536          return;          redo B;
4537        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
4538          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body:body');
4539                                
# Line 2534  sub _tree_construction_main ($) { Line 4551  sub _tree_construction_main ($) {
4551            }            }
4552          }          }
4553          !!!next-token;          !!!next-token;
4554          return;          redo B;
4555        } elsif ({        } elsif ({
4556                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
4557                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1, listing => 1,
# Line 2545  sub _tree_construction_main ($) { Line 4562  sub _tree_construction_main ($) {
4562          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4563            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4564              !!!back-token;              !!!back-token;
4565              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4566              return;              redo B;
4567            } elsif ({            } elsif ({
4568                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4569                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2558  sub _tree_construction_main ($) { Line 4575  sub _tree_construction_main ($) {
4575          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4576          if ($token->{tag_name} eq 'pre') {          if ($token->{tag_name} eq 'pre') {
4577            !!!next-token;            !!!next-token;
4578            if ($token->{type} eq 'character') {            if ($token->{type} == CHARACTER_TOKEN) {
4579              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
4580              unless (length $token->{data}) {              unless (length $token->{data}) {
4581                !!!next-token;                !!!next-token;
# Line 2567  sub _tree_construction_main ($) { Line 4584  sub _tree_construction_main ($) {
4584          } else {          } else {
4585            !!!next-token;            !!!next-token;
4586          }          }
4587          return;          redo B;
4588        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
4589          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
4590            !!!parse-error (type => 'in form:form');            !!!parse-error (type => 'in form:form');
4591            ## Ignore the token            ## Ignore the token
4592            !!!next-token;            !!!next-token;
4593            return;            redo B;
4594          } else {          } else {
4595            ## has a p element in scope            ## has a p element in scope
4596            INSCOPE: for (reverse @{$self->{open_elements}}) {            INSCOPE: for (reverse @{$self->{open_elements}}) {
4597              if ($_->[1] eq 'p') {              if ($_->[1] eq 'p') {
4598                !!!back-token;                !!!back-token;
4599                $token = {type => 'end tag', tag_name => 'p'};                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4600                return;                redo B;
4601              } elsif ({              } elsif ({
4602                        table => 1, caption => 1, td => 1, th => 1,                        table => 1, caption => 1, td => 1, th => 1,
4603                        button => 1, marquee => 1, object => 1, html => 1,                        button => 1, marquee => 1, object => 1, html => 1,
# Line 2592  sub _tree_construction_main ($) { Line 4609  sub _tree_construction_main ($) {
4609            !!!insert-element-t ($token->{tag_name}, $token->{attributes});            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4610            $self->{form_element} = $self->{open_elements}->[-1]->[0];            $self->{form_element} = $self->{open_elements}->[-1]->[0];
4611            !!!next-token;            !!!next-token;
4612            return;            redo B;
4613          }          }
4614        } elsif ($token->{tag_name} eq 'li') {        } elsif ($token->{tag_name} eq 'li') {
4615          ## has a p element in scope          ## has a p element in scope
4616          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4617            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4618              !!!back-token;              !!!back-token;
4619              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4620              return;              redo B;
4621            } elsif ({            } elsif ({
4622                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4623                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2640  sub _tree_construction_main ($) { Line 4657  sub _tree_construction_main ($) {
4657                        
4658          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4659          !!!next-token;          !!!next-token;
4660          return;          redo B;
4661        } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {        } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {
4662          ## has a p element in scope          ## has a p element in scope
4663          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4664            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4665              !!!back-token;              !!!back-token;
4666              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4667              return;              redo B;
4668            } elsif ({            } elsif ({
4669                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4670                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2687  sub _tree_construction_main ($) { Line 4704  sub _tree_construction_main ($) {
4704                        
4705          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4706          !!!next-token;          !!!next-token;
4707          return;          redo B;
4708        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
4709          ## has a p element in scope          ## has a p element in scope
4710          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4711            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4712              !!!back-token;              !!!back-token;
4713              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4714              return;              redo B;
4715            } elsif ({            } elsif ({
4716                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4717                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2705  sub _tree_construction_main ($) { Line 4722  sub _tree_construction_main ($) {
4722                        
4723          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4724                        
4725          $self->{content_model_flag} = 'PLAINTEXT';          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
4726                        
4727          !!!next-token;          !!!next-token;
4728          return;          redo B;
4729        } elsif ({        } elsif ({
4730                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
4731                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 2717  sub _tree_construction_main ($) { Line 4734  sub _tree_construction_main ($) {
4734            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
4735            if ($node->[1] eq 'p') {            if ($node->[1] eq 'p') {
4736              !!!back-token;              !!!back-token;
4737              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4738              return;              redo B;
4739            } elsif ({            } elsif ({
4740                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4741                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2753  sub _tree_construction_main ($) { Line 4770  sub _tree_construction_main ($) {
4770          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4771                        
4772          !!!next-token;          !!!next-token;
4773          return;          redo B;
4774        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
4775          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
4776            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
# Line 2761  sub _tree_construction_main ($) { Line 4778  sub _tree_construction_main ($) {
4778              !!!parse-error (type => 'in a:a');              !!!parse-error (type => 'in a:a');
4779                            
4780              !!!back-token;              !!!back-token;
4781              $token = {type => 'end tag', tag_name => 'a'};              $token = {type => END_TAG_TOKEN, tag_name => 'a'};
4782              $formatting_end_tag->($token->{tag_name});              $formatting_end_tag->($token->{tag_name});
4783                            
4784              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
# Line 2788  sub _tree_construction_main ($) { Line 4805  sub _tree_construction_main ($) {
4805          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
4806    
4807          !!!next-token;          !!!next-token;
4808          return;          redo B;
4809        } elsif ({        } elsif ({
4810                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
4811                  s => 1, small => 1, strile => 1,                  s => 1, small => 1, strile => 1,
# Line 2800  sub _tree_construction_main ($) { Line 4817  sub _tree_construction_main ($) {
4817          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
4818                    
4819          !!!next-token;          !!!next-token;
4820          return;          redo B;
4821        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
4822          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
4823    
# Line 2808  sub _tree_construction_main ($) { Line 4825  sub _tree_construction_main ($) {
4825          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4826            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
4827            if ($node->[1] eq 'nobr') {            if ($node->[1] eq 'nobr') {
4828              !!!parse-error (type => 'not closed:nobr');              !!!parse-error (type => 'in nobr:nobr');
4829              !!!back-token;              !!!back-token;
4830              $token = {type => 'end tag', tag_name => 'nobr'};              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};
4831              return;              redo B;
4832            } elsif ({            } elsif ({
4833                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4834                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2824  sub _tree_construction_main ($) { Line 4841  sub _tree_construction_main ($) {
4841          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
4842                    
4843          !!!next-token;          !!!next-token;
4844          return;          redo B;
4845        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
4846          ## has a button element in scope          ## has a button element in scope
4847          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 2832  sub _tree_construction_main ($) { Line 4849  sub _tree_construction_main ($) {
4849            if ($node->[1] eq 'button') {            if ($node->[1] eq 'button') {
4850              !!!parse-error (type => 'in button:button');              !!!parse-error (type => 'in button:button');
4851              !!!back-token;              !!!back-token;
4852              $token = {type => 'end tag', tag_name => 'button'};              $token = {type => END_TAG_TOKEN, tag_name => 'button'};
4853              return;              redo B;
4854            } elsif ({            } elsif ({
4855                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4856                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2848  sub _tree_construction_main ($) { Line 4865  sub _tree_construction_main ($) {
4865          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
4866    
4867          !!!next-token;          !!!next-token;
4868          return;          redo B;
4869        } elsif ($token->{tag_name} eq 'marquee' or        } elsif ($token->{tag_name} eq 'marquee' or
4870                 $token->{tag_name} eq 'object') {                 $token->{tag_name} eq 'object') {
4871          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
# Line 2857  sub _tree_construction_main ($) { Line 4874  sub _tree_construction_main ($) {
4874          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
4875                    
4876          !!!next-token;          !!!next-token;
4877          return;          redo B;
4878        } elsif ($token->{tag_name} eq 'xmp') {        } elsif ($token->{tag_name} eq 'xmp') {
4879          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
4880          $parse_rcdata->('CDATA', $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4881          return;          redo B;
4882        } elsif ($token->{tag_name} eq 'table') {        } elsif ($token->{tag_name} eq 'table') {
4883          ## has a p element in scope          ## has a p element in scope
4884          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4885            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4886              !!!back-token;              !!!back-token;
4887              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4888              return;              redo B;
4889            } elsif ({            } elsif ({
4890                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4891                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2879  sub _tree_construction_main ($) { Line 4896  sub _tree_construction_main ($) {
4896                        
4897          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4898                        
4899          $self->{insertion_mode} = 'in table';          $self->{insertion_mode} = IN_TABLE_IM;
4900                        
4901          !!!next-token;          !!!next-token;
4902          return;          redo B;
4903        } elsif ({        } elsif ({
4904                  area => 1, basefont => 1, bgsound => 1, br => 1,                  area => 1, basefont => 1, bgsound => 1, br => 1,
4905                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
# Line 2900  sub _tree_construction_main ($) { Line 4917  sub _tree_construction_main ($) {
4917          pop @{$self->{open_elements}};          pop @{$self->{open_elements}};
4918                    
4919          !!!next-token;          !!!next-token;
4920          return;          redo B;
4921        } elsif ($token->{tag_name} eq 'hr') {        } elsif ($token->{tag_name} eq 'hr') {
4922          ## has a p element in scope          ## has a p element in scope
4923          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4924            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4925              !!!back-token;              !!!back-token;
4926              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4927              return;              redo B;
4928            } elsif ({            } elsif ({
4929                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4930                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2920  sub _tree_construction_main ($) { Line 4937  sub _tree_construction_main ($) {
4937          pop @{$self->{open_elements}};          pop @{$self->{open_elements}};
4938                        
4939          !!!next-token;          !!!next-token;
4940          return;          redo B;
4941        } elsif ($token->{tag_name} eq 'input') {        } elsif ($token->{tag_name} eq 'input') {
4942          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
4943                    
# Line 2929  sub _tree_construction_main ($) { Line 4946  sub _tree_construction_main ($) {
4946          pop @{$self->{open_elements}};          pop @{$self->{open_elements}};
4947                    
4948          !!!next-token;          !!!next-token;
4949          return;          redo B;
4950        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
4951          !!!parse-error (type => 'isindex');          !!!parse-error (type => 'isindex');
4952                    
4953          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
4954            ## Ignore the token            ## Ignore the token
4955            !!!next-token;            !!!next-token;
4956            return;            redo B;
4957          } else {          } else {
4958            my $at = $token->{attributes};            my $at = $token->{attributes};
4959            my $form_attrs;            my $form_attrs;
# Line 2946  sub _tree_construction_main ($) { Line 4963  sub _tree_construction_main ($) {
4963            delete $at->{action};            delete $at->{action};
4964            delete $at->{prompt};            delete $at->{prompt};
4965            my @tokens = (            my @tokens = (
4966                          {type => 'start tag', tag_name => 'form',                          {type => START_TAG_TOKEN, tag_name => 'form',
4967                           attributes => $form_attrs},                           attributes => $form_attrs},
4968                          {type => 'start tag', tag_name => 'hr'},                          {type => START_TAG_TOKEN, tag_name => 'hr'},
4969                          {type => 'start tag', tag_name => 'p'},                          {type => START_TAG_TOKEN, tag_name => 'p'},
4970                          {type => 'start tag', tag_name => 'label'},                          {type => START_TAG_TOKEN, tag_name => 'label'},
4971                         );                         );
4972            if ($prompt_attr) {            if ($prompt_attr) {
4973              push @tokens, {type => 'character', data => $prompt_attr->{value}};              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};
4974            } else {            } else {
4975              push @tokens, {type => 'character',              push @tokens, {type => CHARACTER_TOKEN,
4976                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD
4977              ## TODO: make this configurable              ## TODO: make this configurable
4978            }            }
4979            push @tokens,            push @tokens,
4980                          {type => 'start tag', tag_name => 'input', attributes => $at},                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},
4981                          #{type => 'character', data => ''}, # SHOULD                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
4982                          {type => 'end tag', tag_name => 'label'},                          {type => END_TAG_TOKEN, tag_name => 'label'},
4983                          {type => 'end tag', tag_name => 'p'},                          {type => END_TAG_TOKEN, tag_name => 'p'},
4984                          {type => 'start tag', tag_name => 'hr'},                          {type => START_TAG_TOKEN, tag_name => 'hr'},
4985                          {type => 'end tag', tag_name => 'form'};                          {type => END_TAG_TOKEN, tag_name => 'form'};
4986            $token = shift @tokens;            $token = shift @tokens;
4987            !!!back-token (@tokens);            !!!back-token (@tokens);
4988            return;            redo B;
4989          }          }
4990        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
4991          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
# Line 2976  sub _tree_construction_main ($) { Line 4993  sub _tree_construction_main ($) {
4993          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $token->{tag_name}, $token->{attributes});
4994                    
4995          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
4996          $self->{content_model_flag} = 'RCDATA';          $self->{content_model} = RCDATA_CONTENT_MODEL;
4997          delete $self->{escape}; # MUST          delete $self->{escape}; # MUST
4998                    
4999          $insert->($el);          $insert->($el);
5000                    
5001          my $text = '';          my $text = '';
5002          !!!next-token;          !!!next-token;
5003          if ($token->{type} eq 'character') {          if ($token->{type} == CHARACTER_TOKEN) {
5004            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
5005            unless (length $token->{data}) {            unless (length $token->{data}) {
5006              !!!next-token;              !!!next-token;
5007            }            }
5008          }          }
5009          while ($token->{type} eq 'character') {          while ($token->{type} == CHARACTER_TOKEN) {
5010            $text .= $token->{data};            $text .= $token->{data};
5011            !!!next-token;            !!!next-token;
5012          }          }
# Line 2997  sub _tree_construction_main ($) { Line 5014  sub _tree_construction_main ($) {
5014            $el->manakai_append_text ($text);            $el->manakai_append_text ($text);
5015          }          }
5016                    
5017          $self->{content_model_flag} = 'PCDATA';          $self->{content_model} = PCDATA_CONTENT_MODEL;
5018                    
5019          if ($token->{type} eq 'end tag' and          if ($token->{type} == END_TAG_TOKEN and
5020              $token->{tag_name} eq $tag_name) {              $token->{tag_name} eq $tag_name) {
5021            ## Ignore the token            ## Ignore the token
5022          } else {          } else {
5023            !!!parse-error (type => 'in RCDATA:#'.$token->{type});            !!!parse-error (type => 'in RCDATA:#'.$token->{type});
5024          }          }
5025          !!!next-token;          !!!next-token;
5026          return;          redo B;
5027        } elsif ({        } elsif ({
5028                  iframe => 1,                  iframe => 1,
5029                  noembed => 1,                  noembed => 1,
5030                  noframes => 1,                  noframes => 1,
5031                  noscript => 0, ## TODO: 1 if scripting is enabled                  noscript => 0, ## TODO: 1 if scripting is enabled
5032                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5033          $parse_rcdata->('CDATA', $insert);          ## NOTE: There is an "as if in body" code clone.
5034          return;          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
5035            redo B;
5036        } elsif ($token->{tag_name} eq 'select') {        } elsif ($token->{tag_name} eq 'select') {
5037          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5038                    
5039          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5040                    
5041          $self->{insertion_mode} = 'in select';          $self->{insertion_mode} = IN_SELECT_IM;
5042          !!!next-token;          !!!next-token;
5043          return;          redo B;
5044        } elsif ({        } elsif ({
5045                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
5046                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 3032  sub _tree_construction_main ($) { Line 5050  sub _tree_construction_main ($) {
5050          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!parse-error (type => 'in body:'.$token->{tag_name});
5051          ## Ignore the token          ## Ignore the token
5052          !!!next-token;          !!!next-token;
5053          return;          redo B;
5054                    
5055          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
5056        } else {        } else {
# Line 3041  sub _tree_construction_main ($) { Line 5059  sub _tree_construction_main ($) {
5059          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5060                    
5061          !!!next-token;          !!!next-token;
5062          return;          redo B;
5063        }        }
5064      } elsif ($token->{type} eq 'end tag') {      } elsif ($token->{type} == END_TAG_TOKEN) {
5065        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
5066          if (@{$self->{open_elements}} > 1 and          if (@{$self->{open_elements}} > 1 and
5067              $self->{open_elements}->[1]->[1] eq 'body') {              $self->{open_elements}->[1]->[1] eq 'body') {
# Line 3057  sub _tree_construction_main ($) { Line 5075  sub _tree_construction_main ($) {
5075              }              }
5076            }            }
5077    
5078            $self->{insertion_mode} = 'after body';            $self->{insertion_mode} = AFTER_BODY_IM;
5079            !!!next-token;            !!!next-token;
5080            return;            redo B;
5081          } else {          } else {
5082            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5083            ## Ignore the token            ## Ignore the token
5084            !!!next-token;            !!!next-token;
5085            return;            redo B;
5086          }          }
5087        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
5088          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 3072  sub _tree_construction_main ($) { Line 5090  sub _tree_construction_main ($) {
5090            if ($self->{open_elements}->[-1]->[1] ne 'body') {            if ($self->{open_elements}->[-1]->[1] ne 'body') {
5091              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);
5092            }            }
5093            $self->{insertion_mode} = 'after body';            $self->{insertion_mode} = AFTER_BODY_IM;
5094            ## reprocess            ## reprocess
5095            return;            redo B;
5096          } else {          } else {
5097            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5098            ## Ignore the token            ## Ignore the token
5099            !!!next-token;            !!!next-token;
5100            return;            redo B;
5101          }          }
5102        } elsif ({        } elsif ({
5103                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
# Line 3104  sub _tree_construction_main ($) { Line 5122  sub _tree_construction_main ($) {
5122                   tbody => 1, tfoot=> 1, thead => 1,                   tbody => 1, tfoot=> 1, thead => 1,
5123                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
5124                !!!back-token;                !!!back-token;
5125                $token = {type => 'end tag',                $token = {type => END_TAG_TOKEN,
5126                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5127                return;                redo B;
5128              }              }
5129              $i = $_;              $i = $_;
5130              last INSCOPE unless $token->{tag_name} eq 'p';              last INSCOPE unless $token->{tag_name} eq 'p';
# Line 3139  sub _tree_construction_main ($) { Line 5157  sub _tree_construction_main ($) {
5157              button => 1, marquee => 1, object => 1,              button => 1, marquee => 1, object => 1,
5158            }->{$token->{tag_name}};            }->{$token->{tag_name}};
5159          !!!next-token;          !!!next-token;
5160          return;          redo B;
5161        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
5162          ## has an element in scope          ## has an element in scope
5163          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 3152  sub _tree_construction_main ($) { Line 5170  sub _tree_construction_main ($) {
5170                   tbody => 1, tfoot=> 1, thead => 1,                   tbody => 1, tfoot=> 1, thead => 1,
5171                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
5172                !!!back-token;                !!!back-token;
5173                $token = {type => 'end tag',                $token = {type => END_TAG_TOKEN,
5174                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5175                return;                redo B;
5176              }              }
5177              last INSCOPE;              last INSCOPE;
5178            } elsif ({            } elsif ({
# Line 3168  sub _tree_construction_main ($) { Line 5186  sub _tree_construction_main ($) {
5186          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {
5187            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
5188          } else {          } else {
5189            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5190          }          }
5191    
5192          undef $self->{form_element};          undef $self->{form_element};
5193          !!!next-token;          !!!next-token;
5194          return;          redo B;
5195        } elsif ({        } elsif ({
5196                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5197                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 3191  sub _tree_construction_main ($) { Line 5209  sub _tree_construction_main ($) {
5209                   tbody => 1, tfoot=> 1, thead => 1,                   tbody => 1, tfoot=> 1, thead => 1,
5210                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
5211                !!!back-token;                !!!back-token;
5212                $token = {type => 'end tag',                $token = {type => END_TAG_TOKEN,
5213                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5214                return;                redo B;
5215              }              }
5216              $i = $_;              $i = $_;
5217              last INSCOPE;              last INSCOPE;
# Line 3206  sub _tree_construction_main ($) { Line 5224  sub _tree_construction_main ($) {
5224          } # INSCOPE          } # INSCOPE
5225                    
5226          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
5227            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5228          }          }
5229                    
5230          splice @{$self->{open_elements}}, $i if defined $i;          splice @{$self->{open_elements}}, $i if defined $i;
5231          !!!next-token;          !!!next-token;
5232          return;          redo B;
5233        } elsif ({        } elsif ({
5234                  a => 1,                  a => 1,
5235                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
# Line 3219  sub _tree_construction_main ($) { Line 5237  sub _tree_construction_main ($) {
5237                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
5238                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5239          $formatting_end_tag->($token->{tag_name});          $formatting_end_tag->($token->{tag_name});
5240          return;          redo B;
5241        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
5242          !!!parse-error (type => 'unmatched end tag:br');          !!!parse-error (type => 'unmatched end tag:br');
5243    
# Line 3232  sub _tree_construction_main ($) { Line 5250  sub _tree_construction_main ($) {
5250                    
5251          ## Ignore the token.          ## Ignore the token.
5252          !!!next-token;          !!!next-token;
5253          return;          redo B;
5254        } elsif ({        } elsif ({
5255                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
5256                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 3248  sub _tree_construction_main ($) { Line 5266  sub _tree_construction_main ($) {
5266          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5267          ## Ignore the token          ## Ignore the token
5268          !!!next-token;          !!!next-token;
5269          return;          redo B;
5270                    
5271          ## ISSUE: Issue on HTML5 new elements in spec          ## ISSUE: Issue on HTML5 new elements in spec
5272                    
# Line 3265  sub _tree_construction_main ($) { Line 5283  sub _tree_construction_main ($) {
5283              if ({              if ({
5284                   dd => 1, dt => 1, li => 1, p => 1,                   dd => 1, dt => 1, li => 1, p => 1,
5285                   td => 1, th => 1, tr => 1,                   td => 1, th => 1, tr => 1,
5286                   tbody => 1, tfoot=> 1, thead => 1,                   tbody => 1, tfoot => 1, thead => 1,
5287                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
5288                !!!back-token;                !!!back-token;
5289                $token = {type => 'end tag',                $token = {type => END_TAG_TOKEN,
5290                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5291                return;                redo B;
5292              }              }
5293                    
5294              ## Step 2              ## Step 2
5295              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {
5296                  ## NOTE: <x><y></x>
5297                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
5298              }              }
5299                            
# Line 3303  sub _tree_construction_main ($) { Line 5322  sub _tree_construction_main ($) {
5322            ## Step 5;            ## Step 5;
5323            redo S2;            redo S2;
5324          } # S2          } # S2
5325          return;          redo B;
       }  
     }  
   }; # $in_body  
   
   B: {  
     if ($phase eq 'main') {  
       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, meta => 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.  
               ## 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]+)//) {  
               ## As if in body  
               $reconstruct_active_formatting_elements->($insert_to_current);  
                 
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
   
               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 {  
                 $phase = '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 ($token->{data});  
   
               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 ($token->{data});  
   
               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 'noframes') {  
               $in_body->($insert_to_current);  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'html') {  
               $phase = 'trailing end';  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
             
           if (defined $token->{tag_name}) {  
             !!!parse-error (type => 'after frameset:'.($token->{tag_name} eq 'end tag' ? '/' : '').$token->{tag_name});  
           } else {  
             !!!parse-error (type => 'after frameset:#'.$token->{type});  
           }  
           ## Ignore the token  
           !!!next-token;  
           redo B;  
   
           ## ISSUE: An issue in spec there  
         } else {  
           die "$0: $self->{insertion_mode}: Unknown insertion mode";  
         }  
       }  
     } elsif ($phase 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)  
             if $phase eq 'main';  
             
           $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);  
             
           unless (length $token->{data}) {  
             !!!next-token;  
             redo B;  
           }  
         }  
   
         !!!parse-error (type => 'after html:#character');  
         $phase = 'main';  
         ## 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});  
         $phase = 'main';  
         ## reprocess  
         redo B;  
       } elsif ($token->{type} eq 'end-of-file') {  
         ## Stop parsing  
         last B;  
       } else {  
         die "$0: $token->{type}: Unknown token";  
5326        }        }
5327      }      }
5328        redo B;
5329    } # B    } # B
5330    
5331      ## NOTE: The "trailing end" phase in HTML5 is split into
5332      ## two insertion modes: "after html body" and "after html frameset".
5333      ## NOTE: States in the main stage is preserved while
5334      ## the parser stays in the trailing end phase. # MUST
5335    
5336    ## Stop parsing # MUST    ## Stop parsing # MUST
5337        
5338    ## TODO: script stuffs    ## TODO: script stuffs
# Line 5208  sub set_inner_html ($$$) { Line 5344  sub set_inner_html ($$$) {
5344    my $s = \$_[0];    my $s = \$_[0];
5345    my $onerror = $_[1];    my $onerror = $_[1];
5346    
5347      ## ISSUE: Should {confident} be true?
5348    
5349    my $nt = $node->node_type;    my $nt = $node->node_type;
5350    if ($nt == 9) {    if ($nt == 9) {
5351      # MUST      # MUST
# Line 5281  sub set_inner_html ($$$) { Line 5419  sub set_inner_html ($$$) {
5419    
5420      ## Step 2      ## Step 2
5421      my $node_ln = $node->local_name;      my $node_ln = $node->local_name;
5422      $p->{content_model_flag} = {      $p->{content_model} = {
5423        title => 'RCDATA',        title => RCDATA_CONTENT_MODEL,
5424        textarea => 'RCDATA',        textarea => RCDATA_CONTENT_MODEL,
5425        style => 'CDATA',        style => CDATA_CONTENT_MODEL,
5426        script => 'CDATA',        script => CDATA_CONTENT_MODEL,
5427        xmp => 'CDATA',        xmp => CDATA_CONTENT_MODEL,
5428        iframe => 'CDATA',        iframe => CDATA_CONTENT_MODEL,
5429        noembed => 'CDATA',        noembed => CDATA_CONTENT_MODEL,
5430        noframes => 'CDATA',        noframes => CDATA_CONTENT_MODEL,
5431        noscript => 'CDATA',        noscript => CDATA_CONTENT_MODEL,
5432        plaintext => 'PLAINTEXT',        plaintext => PLAINTEXT_CONTENT_MODEL,
5433      }->{$node_ln} || 'PCDATA';      }->{$node_ln};
5434         ## ISSUE: What is "the name of the element"? local name?      $p->{content_model} = PCDATA_CONTENT_MODEL
5435            unless defined $p->{content_model};
5436            ## ISSUE: What is "the name of the element"? local name?
5437    
5438      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $node_ln];
5439    
# Line 5358  sub set_inner_html ($$$) { Line 5498  sub set_inner_html ($$$) {
5498    
5499  } # tree construction stage  } # tree construction stage
5500    
5501  sub get_inner_html ($$$) {  package Whatpm::HTML::RestartParser;
5502    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  
5503    
5504  1;  1;
5505  # $Date$  # $Date$

Legend:
Removed from v.1.33  
changed lines
  Added in v.1.68

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24