/[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.40 by wakaba, Sat Jul 21 05:36:50 2007 UTC revision 1.65 by wakaba, Mon Nov 19 12:18:26 2007 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    
# Line 159  sub CDATA_CONTENT_MODEL () { CM_LIMITED_ Line 246  sub CDATA_CONTENT_MODEL () { CM_LIMITED_
246  sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }  sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }
247  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }  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} = PCDATA_CONTENT_MODEL; # 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};
# Line 177  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    ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
344    
345  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
346    
# Line 194  sub _initialize_tokenizer ($) { Line 350  sub _initialize_tokenizer ($) {
350  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
351  ## and removed from the list.  ## and removed from the list.
352    
353    ## NOTE: HTML5 "Writing HTML documents" section, applied to
354    ## documents and not to user agents and conformance checkers,
355    ## contains some requirements that are not detected by the
356    ## parsing algorithm:
357    ## - Some requirements on character encoding declarations. ## TODO
358    ## - "Elements MUST NOT contain content that their content model disallows."
359    ##   ... Some are parse error, some are not (will be reported by c.c.).
360    ## - Polytheistic slash SHOULD NOT be used. (Applied only to atheists.) ## TODO
361    ## - Text (in elements, attributes, and comments) SHOULD NOT contain
362    ##   control characters other than space characters. ## TODO: (what is control character? C0, C1 and DEL?  Unicode control character?)
363    
364    ## TODO: HTML5 poses authors two SHOULD-level requirements that cannot
365    ## be detected by the HTML5 parsing algorithm:
366    ## - Text,
367    
368  sub _get_next_token ($) {  sub _get_next_token ($) {
369    my $self = shift;    my $self = shift;
370    if (@{$self->{token}}) {    if (@{$self->{token}}) {
# Line 201  sub _get_next_token ($) { Line 372  sub _get_next_token ($) {
372    }    }
373    
374    A: {    A: {
375      if ($self->{state} eq 'data') {      if ($self->{state} == DATA_STATE) {
376        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_input_character} == 0x0026) { # &
377          if ($self->{content_model} & CM_ENTITY) { # PCDATA | RCDATA          if ($self->{content_model} & CM_ENTITY) { # PCDATA | RCDATA
378            $self->{state} = 'entity data';            $self->{state} = ENTITY_DATA_STATE;
379            !!!next-input-character;            !!!next-input-character;
380            redo A;            redo A;
381          } else {          } else {
# Line 226  sub _get_next_token ($) { Line 397  sub _get_next_token ($) {
397          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
398              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
399               not $self->{escape})) {               not $self->{escape})) {
400            $self->{state} = 'tag open';            $self->{state} = TAG_OPEN_STATE;
401            !!!next-input-character;            !!!next-input-character;
402            redo A;            redo A;
403          } else {          } else {
# Line 243  sub _get_next_token ($) { Line 414  sub _get_next_token ($) {
414                    
415          #          #
416        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
417          !!!emit ({type => 'end-of-file'});          !!!emit ({type => END_OF_FILE_TOKEN});
418          last A; ## TODO: ok?          last A; ## TODO: ok?
419        }        }
420        # Anything else        # Anything else
421        my $token = {type => 'character',        my $token = {type => CHARACTER_TOKEN,
422                     data => chr $self->{next_input_character}};                     data => chr $self->{next_input_character}};
423        ## Stay in the data state        ## Stay in the data state
424        !!!next-input-character;        !!!next-input-character;
# Line 255  sub _get_next_token ($) { Line 426  sub _get_next_token ($) {
426        !!!emit ($token);        !!!emit ($token);
427    
428        redo A;        redo A;
429      } elsif ($self->{state} eq 'entity data') {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
430        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
431                
432        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);
433    
434        $self->{state} = 'data';        $self->{state} = DATA_STATE;
435        # next-input-character is already done        # next-input-character is already done
436    
437        unless (defined $token) {        unless (defined $token) {
438          !!!emit ({type => 'character', data => '&'});          !!!emit ({type => CHARACTER_TOKEN, data => '&'});
439        } else {        } else {
440          !!!emit ($token);          !!!emit ($token);
441        }        }
442    
443        redo A;        redo A;
444      } elsif ($self->{state} eq 'tag open') {      } elsif ($self->{state} == TAG_OPEN_STATE) {
445        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
446          if ($self->{next_input_character} == 0x002F) { # /          if ($self->{next_input_character} == 0x002F) { # /
447            !!!next-input-character;            !!!next-input-character;
448            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
449            redo A;            redo A;
450          } else {          } else {
451            ## reconsume            ## reconsume
452            $self->{state} = 'data';            $self->{state} = DATA_STATE;
453    
454            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<'});
455    
456            redo A;            redo A;
457          }          }
458        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
459          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_input_character} == 0x0021) { # !
460            $self->{state} = 'markup declaration open';            $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
461            !!!next-input-character;            !!!next-input-character;
462            redo A;            redo A;
463          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{next_input_character} == 0x002F) { # /
464            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
465            !!!next-input-character;            !!!next-input-character;
466            redo A;            redo A;
467          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_input_character} and
468                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{next_input_character} <= 0x005A) { # A..Z
469            $self->{current_token}            $self->{current_token}
470              = {type => 'start tag',              = {type => START_TAG_TOKEN,
471                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_input_character} + 0x0020)};
472            $self->{state} = 'tag name';            $self->{state} = TAG_NAME_STATE;
473            !!!next-input-character;            !!!next-input-character;
474            redo A;            redo A;
475          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_input_character} and
476                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_input_character} <= 0x007A) { # a..z
477            $self->{current_token} = {type => 'start tag',            $self->{current_token} = {type => START_TAG_TOKEN,
478                              tag_name => chr ($self->{next_input_character})};                              tag_name => chr ($self->{next_input_character})};
479            $self->{state} = 'tag name';            $self->{state} = TAG_NAME_STATE;
480            !!!next-input-character;            !!!next-input-character;
481            redo A;            redo A;
482          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_input_character} == 0x003E) { # >
483            !!!parse-error (type => 'empty start tag');            !!!parse-error (type => 'empty start tag');
484            $self->{state} = 'data';            $self->{state} = DATA_STATE;
485            !!!next-input-character;            !!!next-input-character;
486    
487            !!!emit ({type => 'character', data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});
488    
489            redo A;            redo A;
490          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_input_character} == 0x003F) { # ?
491            !!!parse-error (type => 'pio');            !!!parse-error (type => 'pio');
492            $self->{state} = 'bogus comment';            $self->{state} = BOGUS_COMMENT_STATE;
493            ## $self->{next_input_character} is intentionally left as is            ## $self->{next_input_character} is intentionally left as is
494            redo A;            redo A;
495          } else {          } else {
496            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago');
497            $self->{state} = 'data';            $self->{state} = DATA_STATE;
498            ## reconsume            ## reconsume
499    
500            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<'});
501    
502            redo A;            redo A;
503          }          }
504        } else {        } else {
505          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
506        }        }
507      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
508        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
509          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
510            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
# Line 348  sub _get_next_token ($) { Line 519  sub _get_next_token ($) {
519              } else {              } else {
520                $self->{next_input_character} = shift @next_char; # reconsume                $self->{next_input_character} = shift @next_char; # reconsume
521                !!!back-next-input-character (@next_char);                !!!back-next-input-character (@next_char);
522                $self->{state} = 'data';                $self->{state} = DATA_STATE;
523    
524                !!!emit ({type => 'character', data => '</'});                !!!emit ({type => CHARACTER_TOKEN, data => '</'});
525        
526                redo A;                redo A;
527              }              }
# Line 367  sub _get_next_token ($) { Line 538  sub _get_next_token ($) {
538                    $self->{next_input_character} == -1) {                    $self->{next_input_character} == -1) {
539              $self->{next_input_character} = shift @next_char; # reconsume              $self->{next_input_character} = shift @next_char; # reconsume
540              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
541              $self->{state} = 'data';              $self->{state} = DATA_STATE;
542              !!!emit ({type => 'character', data => '</'});              !!!emit ({type => CHARACTER_TOKEN, data => '</'});
543              redo A;              redo A;
544            } else {            } else {
545              $self->{next_input_character} = shift @next_char;              $self->{next_input_character} = shift @next_char;
# Line 378  sub _get_next_token ($) { Line 549  sub _get_next_token ($) {
549          } else {          } else {
550            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
551            # next-input-character is already done            # next-input-character is already done
552            $self->{state} = 'data';            $self->{state} = DATA_STATE;
553            !!!emit ({type => 'character', data => '</'});            !!!emit ({type => CHARACTER_TOKEN, data => '</'});
554            redo A;            redo A;
555          }          }
556        }        }
557                
558        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_input_character} and
559            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_input_character} <= 0x005A) { # A..Z
560          $self->{current_token} = {type => 'end tag',          $self->{current_token} = {type => END_TAG_TOKEN,
561                            tag_name => chr ($self->{next_input_character} + 0x0020)};                            tag_name => chr ($self->{next_input_character} + 0x0020)};
562          $self->{state} = 'tag name';          $self->{state} = TAG_NAME_STATE;
563          !!!next-input-character;          !!!next-input-character;
564          redo A;          redo A;
565        } elsif (0x0061 <= $self->{next_input_character} and        } elsif (0x0061 <= $self->{next_input_character} and
566                 $self->{next_input_character} <= 0x007A) { # a..z                 $self->{next_input_character} <= 0x007A) { # a..z
567          $self->{current_token} = {type => 'end tag',          $self->{current_token} = {type => END_TAG_TOKEN,
568                            tag_name => chr ($self->{next_input_character})};                            tag_name => chr ($self->{next_input_character})};
569          $self->{state} = 'tag name';          $self->{state} = TAG_NAME_STATE;
570          !!!next-input-character;          !!!next-input-character;
571          redo A;          redo A;
572        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
573          !!!parse-error (type => 'empty end tag');          !!!parse-error (type => 'empty end tag');
574          $self->{state} = 'data';          $self->{state} = DATA_STATE;
575          !!!next-input-character;          !!!next-input-character;
576          redo A;          redo A;
577        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
578          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
579          $self->{state} = 'data';          $self->{state} = DATA_STATE;
580          # reconsume          # reconsume
581    
582          !!!emit ({type => 'character', data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</'});
583    
584          redo A;          redo A;
585        } else {        } else {
586          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
587          $self->{state} = 'bogus comment';          $self->{state} = BOGUS_COMMENT_STATE;
588          ## $self->{next_input_character} is intentionally left as is          ## $self->{next_input_character} is intentionally left as is
589          redo A;          redo A;
590        }        }
591      } elsif ($self->{state} eq 'tag name') {      } elsif ($self->{state} == TAG_NAME_STATE) {
592        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
593            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
594            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
595            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
596            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
597          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
598          !!!next-input-character;          !!!next-input-character;
599          redo A;          redo A;
600        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
601          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
602            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
603                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
604            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
605          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
606            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
607            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
608              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 439  sub _get_next_token ($) { Line 610  sub _get_next_token ($) {
610          } else {          } else {
611            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
612          }          }
613          $self->{state} = 'data';          $self->{state} = DATA_STATE;
614          !!!next-input-character;          !!!next-input-character;
615    
616          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 454  sub _get_next_token ($) { Line 625  sub _get_next_token ($) {
625          redo A;          redo A;
626        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
627          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
628          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
629            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
630                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
631            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
632          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
633            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
634            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
635              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 466  sub _get_next_token ($) { Line 637  sub _get_next_token ($) {
637          } else {          } else {
638            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
639          }          }
640          $self->{state} = 'data';          $self->{state} = DATA_STATE;
641          # reconsume          # reconsume
642    
643          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 475  sub _get_next_token ($) { Line 646  sub _get_next_token ($) {
646        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
647          !!!next-input-character;          !!!next-input-character;
648          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
649              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
650              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
651            # permitted slash            # permitted slash
652            #            #
653          } else {          } else {
654            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
655          }          }
656          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
657          # next-input-character is already done          # next-input-character is already done
658          redo A;          redo A;
659        } else {        } else {
# Line 492  sub _get_next_token ($) { Line 663  sub _get_next_token ($) {
663          !!!next-input-character;          !!!next-input-character;
664          redo A;          redo A;
665        }        }
666      } elsif ($self->{state} eq 'before attribute name') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
667        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
668            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
669            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 502  sub _get_next_token ($) { Line 673  sub _get_next_token ($) {
673          !!!next-input-character;          !!!next-input-character;
674          redo A;          redo A;
675        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
676          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
677            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
678                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
679            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
680          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
681            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
682            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
683              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 514  sub _get_next_token ($) { Line 685  sub _get_next_token ($) {
685          } else {          } else {
686            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
687          }          }
688          $self->{state} = 'data';          $self->{state} = DATA_STATE;
689          !!!next-input-character;          !!!next-input-character;
690    
691          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 524  sub _get_next_token ($) { Line 695  sub _get_next_token ($) {
695                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_input_character} <= 0x005A) { # A..Z
696          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),
697                                value => ''};                                value => ''};
698          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
699          !!!next-input-character;          !!!next-input-character;
700          redo A;          redo A;
701        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
702          !!!next-input-character;          !!!next-input-character;
703          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
704              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
705              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
706            # permitted slash            # permitted slash
707            #            #
# Line 542  sub _get_next_token ($) { Line 713  sub _get_next_token ($) {
713          redo A;          redo A;
714        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
715          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
716          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
717            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
718                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
719            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
720          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
721            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
722            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
723              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 554  sub _get_next_token ($) { Line 725  sub _get_next_token ($) {
725          } else {          } else {
726            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
727          }          }
728          $self->{state} = 'data';          $self->{state} = DATA_STATE;
729          # reconsume          # reconsume
730    
731          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 563  sub _get_next_token ($) { Line 734  sub _get_next_token ($) {
734        } else {        } else {
735          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          $self->{current_attribute} = {name => chr ($self->{next_input_character}),
736                                value => ''};                                value => ''};
737          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
738          !!!next-input-character;          !!!next-input-character;
739          redo A;          redo A;
740        }        }
741      } elsif ($self->{state} eq 'attribute name') {      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {
742        my $before_leave = sub {        my $before_leave = sub {
743          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
744              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
# Line 585  sub _get_next_token ($) { Line 756  sub _get_next_token ($) {
756            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
757            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
758          $before_leave->();          $before_leave->();
759          $self->{state} = 'after attribute name';          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
760          !!!next-input-character;          !!!next-input-character;
761          redo A;          redo A;
762        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_input_character} == 0x003D) { # =
763          $before_leave->();          $before_leave->();
764          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
765          !!!next-input-character;          !!!next-input-character;
766          redo A;          redo A;
767        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
768          $before_leave->();          $before_leave->();
769          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
770            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
771                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
772            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
773          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
774            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
775            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
776              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 607  sub _get_next_token ($) { Line 778  sub _get_next_token ($) {
778          } else {          } else {
779            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
780          }          }
781          $self->{state} = 'data';          $self->{state} = DATA_STATE;
782          !!!next-input-character;          !!!next-input-character;
783    
784          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 623  sub _get_next_token ($) { Line 794  sub _get_next_token ($) {
794          $before_leave->();          $before_leave->();
795          !!!next-input-character;          !!!next-input-character;
796          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
797              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
798              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
799            # permitted slash            # permitted slash
800            #            #
801          } else {          } else {
802            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
803          }          }
804          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
805          # next-input-character is already done          # next-input-character is already done
806          redo A;          redo A;
807        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
808          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
809          $before_leave->();          $before_leave->();
810          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
811            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
812                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
813            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
814          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
815            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
816            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
817              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 648  sub _get_next_token ($) { Line 819  sub _get_next_token ($) {
819          } else {          } else {
820            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
821          }          }
822          $self->{state} = 'data';          $self->{state} = DATA_STATE;
823          # reconsume          # reconsume
824    
825          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 660  sub _get_next_token ($) { Line 831  sub _get_next_token ($) {
831          !!!next-input-character;          !!!next-input-character;
832          redo A;          redo A;
833        }        }
834      } elsif ($self->{state} eq 'after attribute name') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
835        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
836            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
837            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 670  sub _get_next_token ($) { Line 841  sub _get_next_token ($) {
841          !!!next-input-character;          !!!next-input-character;
842          redo A;          redo A;
843        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_input_character} == 0x003D) { # =
844          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
845          !!!next-input-character;          !!!next-input-character;
846          redo A;          redo A;
847        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
848          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
849            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
850                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
851            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
852          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
853            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
854            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
855              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 686  sub _get_next_token ($) { Line 857  sub _get_next_token ($) {
857          } else {          } else {
858            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
859          }          }
860          $self->{state} = 'data';          $self->{state} = DATA_STATE;
861          !!!next-input-character;          !!!next-input-character;
862    
863          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 696  sub _get_next_token ($) { Line 867  sub _get_next_token ($) {
867                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_input_character} <= 0x005A) { # A..Z
868          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),
869                                value => ''};                                value => ''};
870          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
871          !!!next-input-character;          !!!next-input-character;
872          redo A;          redo A;
873        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
874          !!!next-input-character;          !!!next-input-character;
875          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
876              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
877              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
878            # permitted slash            # permitted slash
879            #            #
# Line 710  sub _get_next_token ($) { Line 881  sub _get_next_token ($) {
881            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
882            ## TODO: Different error type for <aa / bb> than <aa/>            ## TODO: Different error type for <aa / bb> than <aa/>
883          }          }
884          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
885          # next-input-character is already done          # next-input-character is already done
886          redo A;          redo A;
887        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
888          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
889          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
890            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
891                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
892            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
893          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
894            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
895            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
896              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 727  sub _get_next_token ($) { Line 898  sub _get_next_token ($) {
898          } else {          } else {
899            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
900          }          }
901          $self->{state} = 'data';          $self->{state} = DATA_STATE;
902          # reconsume          # reconsume
903    
904          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 736  sub _get_next_token ($) { Line 907  sub _get_next_token ($) {
907        } else {        } else {
908          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          $self->{current_attribute} = {name => chr ($self->{next_input_character}),
909                                value => ''};                                value => ''};
910          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
911          !!!next-input-character;          !!!next-input-character;
912          redo A;                  redo A;        
913        }        }
914      } elsif ($self->{state} eq 'before attribute value') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
915        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
916            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
917            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 750  sub _get_next_token ($) { Line 921  sub _get_next_token ($) {
921          !!!next-input-character;          !!!next-input-character;
922          redo A;          redo A;
923        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_input_character} == 0x0022) { # "
924          $self->{state} = 'attribute value (double-quoted)';          $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
925          !!!next-input-character;          !!!next-input-character;
926          redo A;          redo A;
927        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
928          $self->{state} = 'attribute value (unquoted)';          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
929          ## reconsume          ## reconsume
930          redo A;          redo A;
931        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_input_character} == 0x0027) { # '
932          $self->{state} = 'attribute value (single-quoted)';          $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
933          !!!next-input-character;          !!!next-input-character;
934          redo A;          redo A;
935        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
936          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
937            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
938                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
939            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
940          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
941            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
942            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
943              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 774  sub _get_next_token ($) { Line 945  sub _get_next_token ($) {
945          } else {          } else {
946            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
947          }          }
948          $self->{state} = 'data';          $self->{state} = DATA_STATE;
949          !!!next-input-character;          !!!next-input-character;
950    
951          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 782  sub _get_next_token ($) { Line 953  sub _get_next_token ($) {
953          redo A;          redo A;
954        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
955          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
956          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
957            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
958                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
959            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
960          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
961            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
962            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
963              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 794  sub _get_next_token ($) { Line 965  sub _get_next_token ($) {
965          } else {          } else {
966            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
967          }          }
968          $self->{state} = 'data';          $self->{state} = DATA_STATE;
969          ## reconsume          ## reconsume
970    
971          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 802  sub _get_next_token ($) { Line 973  sub _get_next_token ($) {
973          redo A;          redo A;
974        } else {        } else {
975          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});
976          $self->{state} = 'attribute value (unquoted)';          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
977          !!!next-input-character;          !!!next-input-character;
978          redo A;          redo A;
979        }        }
980      } elsif ($self->{state} eq 'attribute value (double-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
981        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_input_character} == 0x0022) { # "
982          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
983          !!!next-input-character;          !!!next-input-character;
984          redo A;          redo A;
985        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
986          $self->{last_attribute_value_state} = 'attribute value (double-quoted)';          $self->{last_attribute_value_state} = $self->{state};
987          $self->{state} = 'entity in attribute value';          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
988          !!!next-input-character;          !!!next-input-character;
989          redo A;          redo A;
990        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
991          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
992          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
993            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
994                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
995            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
996          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
997            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
998            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
999              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 830  sub _get_next_token ($) { Line 1001  sub _get_next_token ($) {
1001          } else {          } else {
1002            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1003          }          }
1004          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1005          ## reconsume          ## reconsume
1006    
1007          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 842  sub _get_next_token ($) { Line 1013  sub _get_next_token ($) {
1013          !!!next-input-character;          !!!next-input-character;
1014          redo A;          redo A;
1015        }        }
1016      } elsif ($self->{state} eq 'attribute value (single-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1017        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_input_character} == 0x0027) { # '
1018          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1019          !!!next-input-character;          !!!next-input-character;
1020          redo A;          redo A;
1021        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
1022          $self->{last_attribute_value_state} = 'attribute value (single-quoted)';          $self->{last_attribute_value_state} = $self->{state};
1023          $self->{state} = 'entity in attribute value';          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1024          !!!next-input-character;          !!!next-input-character;
1025          redo A;          redo A;
1026        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1027          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1028          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1029            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
1030                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
1031            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1032          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1033            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1034            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1035              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 866  sub _get_next_token ($) { Line 1037  sub _get_next_token ($) {
1037          } else {          } else {
1038            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1039          }          }
1040          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1041          ## reconsume          ## reconsume
1042    
1043          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 878  sub _get_next_token ($) { Line 1049  sub _get_next_token ($) {
1049          !!!next-input-character;          !!!next-input-character;
1050          redo A;          redo A;
1051        }        }
1052      } elsif ($self->{state} eq 'attribute value (unquoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1053        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1054            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1055            $self->{next_input_character} == 0x000B or # HT            $self->{next_input_character} == 0x000B or # HT
1056            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1057            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
1058          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1059          !!!next-input-character;          !!!next-input-character;
1060          redo A;          redo A;
1061        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
1062          $self->{last_attribute_value_state} = 'attribute value (unquoted)';          $self->{last_attribute_value_state} = $self->{state};
1063          $self->{state} = 'entity in attribute value';          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1064          !!!next-input-character;          !!!next-input-character;
1065          redo A;          redo A;
1066        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1067          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1068            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
1069                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
1070            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1071          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1072            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1073            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1074              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 905  sub _get_next_token ($) { Line 1076  sub _get_next_token ($) {
1076          } else {          } else {
1077            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1078          }          }
1079          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1080          !!!next-input-character;          !!!next-input-character;
1081    
1082          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 913  sub _get_next_token ($) { Line 1084  sub _get_next_token ($) {
1084          redo A;          redo A;
1085        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1086          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1087          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1088            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
1089                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
1090            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1091          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1092            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1093            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1094              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 925  sub _get_next_token ($) { Line 1096  sub _get_next_token ($) {
1096          } else {          } else {
1097            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1098          }          }
1099          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1100          ## reconsume          ## reconsume
1101    
1102          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 937  sub _get_next_token ($) { Line 1108  sub _get_next_token ($) {
1108          !!!next-input-character;          !!!next-input-character;
1109          redo A;          redo A;
1110        }        }
1111      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {
1112        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);
1113    
1114        unless (defined $token) {        unless (defined $token) {
# Line 950  sub _get_next_token ($) { Line 1121  sub _get_next_token ($) {
1121        $self->{state} = $self->{last_attribute_value_state};        $self->{state} = $self->{last_attribute_value_state};
1122        # next-input-character is already done        # next-input-character is already done
1123        redo A;        redo A;
1124      } elsif ($self->{state} eq 'bogus comment') {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1125        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1126                
1127        my $token = {type => 'comment', data => ''};        my $token = {type => COMMENT_TOKEN, data => ''};
1128    
1129        BC: {        BC: {
1130          if ($self->{next_input_character} == 0x003E) { # >          if ($self->{next_input_character} == 0x003E) { # >
1131            $self->{state} = 'data';            $self->{state} = DATA_STATE;
1132            !!!next-input-character;            !!!next-input-character;
1133    
1134            !!!emit ($token);            !!!emit ($token);
1135    
1136            redo A;            redo A;
1137          } elsif ($self->{next_input_character} == -1) {          } elsif ($self->{next_input_character} == -1) {
1138            $self->{state} = 'data';            $self->{state} = DATA_STATE;
1139            ## reconsume            ## reconsume
1140    
1141            !!!emit ($token);            !!!emit ($token);
# Line 976  sub _get_next_token ($) { Line 1147  sub _get_next_token ($) {
1147            redo BC;            redo BC;
1148          }          }
1149        } # BC        } # BC
1150      } elsif ($self->{state} eq 'markup declaration open') {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1151        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1152    
1153        my @next_char;        my @next_char;
# Line 986  sub _get_next_token ($) { Line 1157  sub _get_next_token ($) {
1157          !!!next-input-character;          !!!next-input-character;
1158          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_input_character};
1159          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_input_character} == 0x002D) { # -
1160            $self->{current_token} = {type => 'comment', data => ''};            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};
1161            $self->{state} = 'comment start';            $self->{state} = COMMENT_START_STATE;
1162            !!!next-input-character;            !!!next-input-character;
1163            redo A;            redo A;
1164          }          }
# Line 1018  sub _get_next_token ($) { Line 1189  sub _get_next_token ($) {
1189                    if ($self->{next_input_character} == 0x0045 or # E                    if ($self->{next_input_character} == 0x0045 or # E
1190                        $self->{next_input_character} == 0x0065) { # e                        $self->{next_input_character} == 0x0065) { # e
1191                      ## ISSUE: What a stupid code this is!                      ## ISSUE: What a stupid code this is!
1192                      $self->{state} = 'DOCTYPE';                      $self->{state} = DOCTYPE_STATE;
1193                      !!!next-input-character;                      !!!next-input-character;
1194                      redo A;                      redo A;
1195                    }                    }
# Line 1032  sub _get_next_token ($) { Line 1203  sub _get_next_token ($) {
1203        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'bogus comment');
1204        $self->{next_input_character} = shift @next_char;        $self->{next_input_character} = shift @next_char;
1205        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
1206        $self->{state} = 'bogus comment';        $self->{state} = BOGUS_COMMENT_STATE;
1207        redo A;        redo A;
1208                
1209        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
1210        ## 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?
1211      } elsif ($self->{state} eq 'comment start') {      } elsif ($self->{state} == COMMENT_START_STATE) {
1212        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1213          $self->{state} = 'comment start dash';          $self->{state} = COMMENT_START_DASH_STATE;
1214          !!!next-input-character;          !!!next-input-character;
1215          redo A;          redo A;
1216        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1217          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
1218          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1219          !!!next-input-character;          !!!next-input-character;
1220    
1221          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1052  sub _get_next_token ($) { Line 1223  sub _get_next_token ($) {
1223          redo A;          redo A;
1224        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1225          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1226          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1227          ## reconsume          ## reconsume
1228    
1229          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1061  sub _get_next_token ($) { Line 1232  sub _get_next_token ($) {
1232        } else {        } else {
1233          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
1234              .= chr ($self->{next_input_character});              .= chr ($self->{next_input_character});
1235          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1236          !!!next-input-character;          !!!next-input-character;
1237          redo A;          redo A;
1238        }        }
1239      } elsif ($self->{state} eq 'comment start dash') {      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
1240        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1241          $self->{state} = 'comment end';          $self->{state} = COMMENT_END_STATE;
1242          !!!next-input-character;          !!!next-input-character;
1243          redo A;          redo A;
1244        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1245          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
1246          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1247          !!!next-input-character;          !!!next-input-character;
1248    
1249          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1080  sub _get_next_token ($) { Line 1251  sub _get_next_token ($) {
1251          redo A;          redo A;
1252        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1253          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1254          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1255          ## reconsume          ## reconsume
1256    
1257          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1089  sub _get_next_token ($) { Line 1260  sub _get_next_token ($) {
1260        } else {        } else {
1261          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
1262              .= '-' . chr ($self->{next_input_character});              .= '-' . chr ($self->{next_input_character});
1263          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1264          !!!next-input-character;          !!!next-input-character;
1265          redo A;          redo A;
1266        }        }
1267      } elsif ($self->{state} eq 'comment') {      } elsif ($self->{state} == COMMENT_STATE) {
1268        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1269          $self->{state} = 'comment end dash';          $self->{state} = COMMENT_END_DASH_STATE;
1270          !!!next-input-character;          !!!next-input-character;
1271          redo A;          redo A;
1272        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1273          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1274          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1275          ## reconsume          ## reconsume
1276    
1277          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1112  sub _get_next_token ($) { Line 1283  sub _get_next_token ($) {
1283          !!!next-input-character;          !!!next-input-character;
1284          redo A;          redo A;
1285        }        }
1286      } elsif ($self->{state} eq 'comment end dash') {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
1287        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1288          $self->{state} = 'comment end';          $self->{state} = COMMENT_END_STATE;
1289          !!!next-input-character;          !!!next-input-character;
1290          redo A;          redo A;
1291        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1292          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1293          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1294          ## reconsume          ## reconsume
1295    
1296          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1127  sub _get_next_token ($) { Line 1298  sub _get_next_token ($) {
1298          redo A;          redo A;
1299        } else {        } else {
1300          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment
1301          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1302          !!!next-input-character;          !!!next-input-character;
1303          redo A;          redo A;
1304        }        }
1305      } elsif ($self->{state} eq 'comment end') {      } elsif ($self->{state} == COMMENT_END_STATE) {
1306        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_input_character} == 0x003E) { # >
1307          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1308          !!!next-input-character;          !!!next-input-character;
1309    
1310          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1147  sub _get_next_token ($) { Line 1318  sub _get_next_token ($) {
1318          redo A;          redo A;
1319        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1320          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1321          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1322          ## reconsume          ## reconsume
1323    
1324          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1156  sub _get_next_token ($) { Line 1327  sub _get_next_token ($) {
1327        } else {        } else {
1328          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment');
1329          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment
1330          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1331          !!!next-input-character;          !!!next-input-character;
1332          redo A;          redo A;
1333        }        }
1334      } elsif ($self->{state} eq 'DOCTYPE') {      } elsif ($self->{state} == DOCTYPE_STATE) {
1335        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1336            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1337            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
1338            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1339            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
1340          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1341          !!!next-input-character;          !!!next-input-character;
1342          redo A;          redo A;
1343        } else {        } else {
1344          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
1345          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1346          ## reconsume          ## reconsume
1347          redo A;          redo A;
1348        }        }
1349      } elsif ($self->{state} eq 'before DOCTYPE name') {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
1350        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1351            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1352            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 1186  sub _get_next_token ($) { Line 1357  sub _get_next_token ($) {
1357          redo A;          redo A;
1358        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1359          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1360          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1361          !!!next-input-character;          !!!next-input-character;
1362    
1363          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect
1364    
1365          redo A;          redo A;
1366        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1367          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1368          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1369          ## reconsume          ## reconsume
1370    
1371          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect
1372    
1373          redo A;          redo A;
1374        } else {        } else {
1375          $self->{current_token}          $self->{current_token}
1376              = {type => 'DOCTYPE',              = {type => DOCTYPE_TOKEN,
1377                 name => chr ($self->{next_input_character}),                 name => chr ($self->{next_input_character}),
1378                 correct => 1};                 correct => 1};
1379  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
1380          $self->{state} = 'DOCTYPE name';          $self->{state} = DOCTYPE_NAME_STATE;
1381          !!!next-input-character;          !!!next-input-character;
1382          redo A;          redo A;
1383        }        }
1384      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
1385  ## ISSUE: Redundant "First," in the spec.  ## ISSUE: Redundant "First," in the spec.
1386        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1387            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1388            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
1389            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1390            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
1391          $self->{state} = 'after DOCTYPE name';          $self->{state} = AFTER_DOCTYPE_NAME_STATE;
1392          !!!next-input-character;          !!!next-input-character;
1393          redo A;          redo A;
1394        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1395          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1396          !!!next-input-character;          !!!next-input-character;
1397    
1398          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1229  sub _get_next_token ($) { Line 1400  sub _get_next_token ($) {
1400          redo A;          redo A;
1401        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1402          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1403          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1404          ## reconsume          ## reconsume
1405    
1406          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1243  sub _get_next_token ($) { Line 1414  sub _get_next_token ($) {
1414          !!!next-input-character;          !!!next-input-character;
1415          redo A;          redo A;
1416        }        }
1417      } elsif ($self->{state} eq 'after DOCTYPE name') {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
1418        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1419            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1420            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 1253  sub _get_next_token ($) { Line 1424  sub _get_next_token ($) {
1424          !!!next-input-character;          !!!next-input-character;
1425          redo A;          redo A;
1426        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1427          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1428          !!!next-input-character;          !!!next-input-character;
1429    
1430          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1261  sub _get_next_token ($) { Line 1432  sub _get_next_token ($) {
1432          redo A;          redo A;
1433        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1434          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1435          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1436          ## reconsume          ## reconsume
1437    
1438          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1285  sub _get_next_token ($) { Line 1456  sub _get_next_token ($) {
1456                  !!!next-input-character;                  !!!next-input-character;
1457                  if ($self->{next_input_character} == 0x0043 or # C                  if ($self->{next_input_character} == 0x0043 or # C
1458                      $self->{next_input_character} == 0x0063) { # c                      $self->{next_input_character} == 0x0063) { # c
1459                    $self->{state} = 'before DOCTYPE public identifier';                    $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1460                    !!!next-input-character;                    !!!next-input-character;
1461                    redo A;                    redo A;
1462                  }                  }
# Line 1312  sub _get_next_token ($) { Line 1483  sub _get_next_token ($) {
1483                  !!!next-input-character;                  !!!next-input-character;
1484                  if ($self->{next_input_character} == 0x004D or # M                  if ($self->{next_input_character} == 0x004D or # M
1485                      $self->{next_input_character} == 0x006D) { # m                      $self->{next_input_character} == 0x006D) { # m
1486                    $self->{state} = 'before DOCTYPE system identifier';                    $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1487                    !!!next-input-character;                    !!!next-input-character;
1488                    redo A;                    redo A;
1489                  }                  }
# Line 1328  sub _get_next_token ($) { Line 1499  sub _get_next_token ($) {
1499        }        }
1500    
1501        !!!parse-error (type => 'string after DOCTYPE name');        !!!parse-error (type => 'string after DOCTYPE name');
1502        $self->{state} = 'bogus DOCTYPE';        $self->{state} = BOGUS_DOCTYPE_STATE;
1503        # next-input-character is already done        # next-input-character is already done
1504        redo A;        redo A;
1505      } elsif ($self->{state} eq 'before DOCTYPE public identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
1506        if ({        if ({
1507              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1508              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1341  sub _get_next_token ($) { Line 1512  sub _get_next_token ($) {
1512          redo A;          redo A;
1513        } elsif ($self->{next_input_character} eq 0x0022) { # "        } elsif ($self->{next_input_character} eq 0x0022) { # "
1514          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1515          $self->{state} = 'DOCTYPE public identifier (double-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
1516          !!!next-input-character;          !!!next-input-character;
1517          redo A;          redo A;
1518        } elsif ($self->{next_input_character} eq 0x0027) { # '        } elsif ($self->{next_input_character} eq 0x0027) { # '
1519          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1520          $self->{state} = 'DOCTYPE public identifier (single-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
1521          !!!next-input-character;          !!!next-input-character;
1522          redo A;          redo A;
1523        } elsif ($self->{next_input_character} eq 0x003E) { # >        } elsif ($self->{next_input_character} eq 0x003E) { # >
1524          !!!parse-error (type => 'no PUBLIC literal');          !!!parse-error (type => 'no PUBLIC literal');
1525    
1526          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1527          !!!next-input-character;          !!!next-input-character;
1528    
1529          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1362  sub _get_next_token ($) { Line 1533  sub _get_next_token ($) {
1533        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1534          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1535    
1536          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1537          ## reconsume          ## reconsume
1538    
1539          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1371  sub _get_next_token ($) { Line 1542  sub _get_next_token ($) {
1542          redo A;          redo A;
1543        } else {        } else {
1544          !!!parse-error (type => 'string after PUBLIC');          !!!parse-error (type => 'string after PUBLIC');
1545          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1546          !!!next-input-character;          !!!next-input-character;
1547          redo A;          redo A;
1548        }        }
1549      } elsif ($self->{state} eq 'DOCTYPE public identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
1550        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_input_character} == 0x0022) { # "
1551          $self->{state} = 'after DOCTYPE public identifier';          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1552          !!!next-input-character;          !!!next-input-character;
1553          redo A;          redo A;
1554        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1555          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
1556    
1557          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1558          ## reconsume          ## reconsume
1559    
1560          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1397  sub _get_next_token ($) { Line 1568  sub _get_next_token ($) {
1568          !!!next-input-character;          !!!next-input-character;
1569          redo A;          redo A;
1570        }        }
1571      } elsif ($self->{state} eq 'DOCTYPE public identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
1572        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_input_character} == 0x0027) { # '
1573          $self->{state} = 'after DOCTYPE public identifier';          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1574          !!!next-input-character;          !!!next-input-character;
1575          redo A;          redo A;
1576        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1577          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
1578    
1579          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1580          ## reconsume          ## reconsume
1581    
1582          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1419  sub _get_next_token ($) { Line 1590  sub _get_next_token ($) {
1590          !!!next-input-character;          !!!next-input-character;
1591          redo A;          redo A;
1592        }        }
1593      } elsif ($self->{state} eq 'after DOCTYPE public identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
1594        if ({        if ({
1595              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1596              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1429  sub _get_next_token ($) { Line 1600  sub _get_next_token ($) {
1600          redo A;          redo A;
1601        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_input_character} == 0x0022) { # "
1602          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1603          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
1604          !!!next-input-character;          !!!next-input-character;
1605          redo A;          redo A;
1606        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_input_character} == 0x0027) { # '
1607          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1608          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
1609          !!!next-input-character;          !!!next-input-character;
1610          redo A;          redo A;
1611        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1612          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1613          !!!next-input-character;          !!!next-input-character;
1614    
1615          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1447  sub _get_next_token ($) { Line 1618  sub _get_next_token ($) {
1618        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1619          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1620    
1621          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1622          ## reconsume          ## reconsume
1623    
1624          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1456  sub _get_next_token ($) { Line 1627  sub _get_next_token ($) {
1627          redo A;          redo A;
1628        } else {        } else {
1629          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after PUBLIC literal');
1630          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1631          !!!next-input-character;          !!!next-input-character;
1632          redo A;          redo A;
1633        }        }
1634      } elsif ($self->{state} eq 'before DOCTYPE system identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
1635        if ({        if ({
1636              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1637              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1470  sub _get_next_token ($) { Line 1641  sub _get_next_token ($) {
1641          redo A;          redo A;
1642        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_input_character} == 0x0022) { # "
1643          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1644          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
1645          !!!next-input-character;          !!!next-input-character;
1646          redo A;          redo A;
1647        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_input_character} == 0x0027) { # '
1648          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1649          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
1650          !!!next-input-character;          !!!next-input-character;
1651          redo A;          redo A;
1652        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1653          !!!parse-error (type => 'no SYSTEM literal');          !!!parse-error (type => 'no SYSTEM literal');
1654          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1655          !!!next-input-character;          !!!next-input-character;
1656    
1657          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1490  sub _get_next_token ($) { Line 1661  sub _get_next_token ($) {
1661        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1662          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1663    
1664          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1665          ## reconsume          ## reconsume
1666    
1667          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1499  sub _get_next_token ($) { Line 1670  sub _get_next_token ($) {
1670          redo A;          redo A;
1671        } else {        } else {
1672          !!!parse-error (type => 'string after SYSTEM');          !!!parse-error (type => 'string after SYSTEM');
1673          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1674          !!!next-input-character;          !!!next-input-character;
1675          redo A;          redo A;
1676        }        }
1677      } elsif ($self->{state} eq 'DOCTYPE system identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
1678        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_input_character} == 0x0022) { # "
1679          $self->{state} = 'after DOCTYPE system identifier';          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1680          !!!next-input-character;          !!!next-input-character;
1681          redo A;          redo A;
1682        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1683          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
1684    
1685          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1686          ## reconsume          ## reconsume
1687    
1688          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1525  sub _get_next_token ($) { Line 1696  sub _get_next_token ($) {
1696          !!!next-input-character;          !!!next-input-character;
1697          redo A;          redo A;
1698        }        }
1699      } elsif ($self->{state} eq 'DOCTYPE system identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
1700        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_input_character} == 0x0027) { # '
1701          $self->{state} = 'after DOCTYPE system identifier';          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1702          !!!next-input-character;          !!!next-input-character;
1703          redo A;          redo A;
1704        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1705          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
1706    
1707          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1708          ## reconsume          ## reconsume
1709    
1710          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1547  sub _get_next_token ($) { Line 1718  sub _get_next_token ($) {
1718          !!!next-input-character;          !!!next-input-character;
1719          redo A;          redo A;
1720        }        }
1721      } elsif ($self->{state} eq 'after DOCTYPE system identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
1722        if ({        if ({
1723              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1724              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1556  sub _get_next_token ($) { Line 1727  sub _get_next_token ($) {
1727          !!!next-input-character;          !!!next-input-character;
1728          redo A;          redo A;
1729        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1730          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1731          !!!next-input-character;          !!!next-input-character;
1732    
1733          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1565  sub _get_next_token ($) { Line 1736  sub _get_next_token ($) {
1736        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1737          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1738    
1739          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1740          ## reconsume          ## reconsume
1741    
1742          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1574  sub _get_next_token ($) { Line 1745  sub _get_next_token ($) {
1745          redo A;          redo A;
1746        } else {        } else {
1747          !!!parse-error (type => 'string after SYSTEM literal');          !!!parse-error (type => 'string after SYSTEM literal');
1748          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1749          !!!next-input-character;          !!!next-input-character;
1750          redo A;          redo A;
1751        }        }
1752      } elsif ($self->{state} eq 'bogus DOCTYPE') {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
1753        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_input_character} == 0x003E) { # >
1754          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1755          !!!next-input-character;          !!!next-input-character;
1756    
1757          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1589  sub _get_next_token ($) { Line 1760  sub _get_next_token ($) {
1760          redo A;          redo A;
1761        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1762          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1763          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1764          ## reconsume          ## reconsume
1765    
1766          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1670  sub _tokenize_attempt_to_consume_an_enti Line 1841  sub _tokenize_attempt_to_consume_an_enti
1841            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
1842          }          }
1843    
1844          return {type => 'character', data => chr $code};          return {type => CHARACTER_TOKEN, data => chr $code};
1845        } # X        } # X
1846      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_input_character} and
1847               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_input_character} <= 0x0039) { # 0..9
# Line 1705  sub _tokenize_attempt_to_consume_an_enti Line 1876  sub _tokenize_attempt_to_consume_an_enti
1876          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
1877        }        }
1878                
1879        return {type => 'character', data => chr $code};        return {type => CHARACTER_TOKEN, data => chr $code};
1880      } else {      } else {
1881        !!!parse-error (type => 'bare nero');        !!!parse-error (type => 'bare nero');
1882        !!!back-next-input-character ($self->{next_input_character});        !!!back-next-input-character ($self->{next_input_character});
# Line 1753  sub _tokenize_attempt_to_consume_an_enti Line 1924  sub _tokenize_attempt_to_consume_an_enti
1924      }      }
1925            
1926      if ($match > 0) {      if ($match > 0) {
1927        return {type => 'character', data => $value};        return {type => CHARACTER_TOKEN, data => $value};
1928      } elsif ($match < 0) {      } elsif ($match < 0) {
1929        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc');
1930        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
1931          return {type => 'character', data => '&'.$entity_name};          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};
1932        } else {        } else {
1933          return {type => 'character', data => $value};          return {type => CHARACTER_TOKEN, data => $value};
1934        }        }
1935      } else {      } else {
1936        !!!parse-error (type => 'bare ero');        !!!parse-error (type => 'bare ero');
1937        ## NOTE: No characters are consumed in the spec.        ## NOTE: No characters are consumed in the spec.
1938        return {type => 'character', data => '&'.$value};        return {type => CHARACTER_TOKEN, data => '&'.$value};
1939      }      }
1940    } else {    } else {
1941      ## no characters are consumed      ## no characters are consumed
# Line 1806  sub _construct_tree ($) { Line 1977  sub _construct_tree ($) {
1977        
1978    !!!next-token;    !!!next-token;
1979    
1980    $self->{insertion_mode} = 'before head';    $self->{insertion_mode} = BEFORE_HEAD_IM;
1981    undef $self->{form_element};    undef $self->{form_element};
1982    undef $self->{head_element};    undef $self->{head_element};
1983    $self->{open_elements} = [];    $self->{open_elements} = [];
# Line 1820  sub _construct_tree ($) { Line 1991  sub _construct_tree ($) {
1991  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
1992    my $self = shift;    my $self = shift;
1993    INITIAL: {    INITIAL: {
1994      if ($token->{type} eq 'DOCTYPE') {      if ($token->{type} == DOCTYPE_TOKEN) {
1995        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
1996        ## error, switch to a conformance checking mode for another        ## error, switch to a conformance checking mode for another
1997        ## language.        ## language.
# Line 1947  sub _tree_construction_initial ($) { Line 2118  sub _tree_construction_initial ($) {
2118        !!!next-token;        !!!next-token;
2119        return;        return;
2120      } elsif ({      } elsif ({
2121                'start tag' => 1,                START_TAG_TOKEN, 1,
2122                'end tag' => 1,                END_TAG_TOKEN, 1,
2123                'end-of-file' => 1,                END_OF_FILE_TOKEN, 1,
2124               }->{$token->{type}}) {               }->{$token->{type}}) {
2125        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE');
2126        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
2127        ## Go to the root element phase        ## Go to the root element phase
2128        ## reprocess        ## reprocess
2129        return;        return;
2130      } elsif ($token->{type} eq 'character') {      } elsif ($token->{type} == CHARACTER_TOKEN) {
2131        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2132          ## Ignore the token          ## Ignore the token
2133    
# Line 1972  sub _tree_construction_initial ($) { Line 2143  sub _tree_construction_initial ($) {
2143        ## Go to the root element phase        ## Go to the root element phase
2144        ## reprocess        ## reprocess
2145        return;        return;
2146      } elsif ($token->{type} eq 'comment') {      } elsif ($token->{type} == COMMENT_TOKEN) {
2147        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
2148        $self->{document}->append_child ($comment);        $self->{document}->append_child ($comment);
2149                
# Line 1980  sub _tree_construction_initial ($) { Line 2151  sub _tree_construction_initial ($) {
2151        !!!next-token;        !!!next-token;
2152        redo INITIAL;        redo INITIAL;
2153      } else {      } else {
2154        die "$0: $token->{type}: Unknown token";        die "$0: $token->{type}: Unknown token type";
2155      }      }
2156    } # INITIAL    } # INITIAL
2157  } # _tree_construction_initial  } # _tree_construction_initial
# Line 1989  sub _tree_construction_root_element ($) Line 2160  sub _tree_construction_root_element ($)
2160    my $self = shift;    my $self = shift;
2161        
2162    B: {    B: {
2163        if ($token->{type} eq 'DOCTYPE') {        if ($token->{type} == DOCTYPE_TOKEN) {
2164          !!!parse-error (type => 'in html:#DOCTYPE');          !!!parse-error (type => 'in html:#DOCTYPE');
2165          ## Ignore the token          ## Ignore the token
2166          ## Stay in the phase          ## Stay in the phase
2167          !!!next-token;          !!!next-token;
2168          redo B;          redo B;
2169        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{type} == COMMENT_TOKEN) {
2170          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
2171          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
2172          ## Stay in the phase          ## Stay in the phase
2173          !!!next-token;          !!!next-token;
2174          redo B;          redo B;
2175        } elsif ($token->{type} eq 'character') {        } elsif ($token->{type} == CHARACTER_TOKEN) {
2176          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2177            ## Ignore the token.            ## Ignore the token.
2178    
# Line 2011  sub _tree_construction_root_element ($) Line 2182  sub _tree_construction_root_element ($)
2182              redo B;              redo B;
2183            }            }
2184          }          }
2185    
2186            $self->{application_cache_selection}->(undef);
2187    
2188            #
2189          } elsif ($token->{type} == START_TAG_TOKEN) {
2190            if ($token->{tag_name} eq 'html' and
2191                $token->{attributes}->{manifest}) { ## ISSUE: Spec spells as "application"
2192              $self->{application_cache_selection}
2193                   ->($token->{attributes}->{manifest}->{value});
2194              ## ISSUE: No relative reference resolution?
2195            } else {
2196              $self->{application_cache_selection}->(undef);
2197            }
2198    
2199            ## ISSUE: There is an issue in the spec
2200          #          #
2201        } elsif ({        } elsif ({
2202                  'start tag' => 1,                  END_TAG_TOKEN, 1,
2203                  'end tag' => 1,                  END_OF_FILE_TOKEN, 1,
                 'end-of-file' => 1,  
2204                 }->{$token->{type}}) {                 }->{$token->{type}}) {
2205            $self->{application_cache_selection}->(undef);
2206    
2207          ## ISSUE: There is an issue in the spec          ## ISSUE: There is an issue in the spec
2208          #          #
2209        } else {        } else {
2210          die "$0: $token->{type}: Unknown token";          die "$0: $token->{type}: Unknown token type";
2211        }        }
2212    
2213        my $root_element; !!!create-element ($root_element, 'html');        my $root_element; !!!create-element ($root_element, 'html');
2214        $self->{document}->append_child ($root_element);        $self->{document}->append_child ($root_element);
2215        push @{$self->{open_elements}}, [$root_element, 'html'];        push @{$self->{open_elements}}, [$root_element, 'html'];
# Line 2062  sub _reset_insertion_mode ($) { Line 2250  sub _reset_insertion_mode ($) {
2250            
2251        ## Step 4..13        ## Step 4..13
2252        my $new_mode = {        my $new_mode = {
2253                        select => 'in select',                        select => IN_SELECT_IM,
2254                        td => 'in cell',                        td => IN_CELL_IM,
2255                        th => 'in cell',                        th => IN_CELL_IM,
2256                        tr => 'in row',                        tr => IN_ROW_IM,
2257                        tbody => 'in table body',                        tbody => IN_TABLE_BODY_IM,
2258                        thead => 'in table head',                        thead => IN_TABLE_BODY_IM,
2259                        tfoot => 'in table foot',                        tfoot => IN_TABLE_BODY_IM,
2260                        caption => 'in caption',                        caption => IN_CAPTION_IM,
2261                        colgroup => 'in column group',                        colgroup => IN_COLUMN_GROUP_IM,
2262                        table => 'in table',                        table => IN_TABLE_IM,
2263                        head => 'in body', # not in head!                        head => IN_BODY_IM, # not in head!
2264                        body => 'in body',                        body => IN_BODY_IM,
2265                        frameset => 'in frameset',                        frameset => IN_FRAMESET_IM,
2266                       }->{$node->[1]};                       }->{$node->[1]};
2267        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
2268                
2269        ## Step 14        ## Step 14
2270        if ($node->[1] eq 'html') {        if ($node->[1] eq 'html') {
2271          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
2272            $self->{insertion_mode} = 'before head';            $self->{insertion_mode} = BEFORE_HEAD_IM;
2273          } else {          } else {
2274            $self->{insertion_mode} = 'after head';            $self->{insertion_mode} = AFTER_HEAD_IM;
2275          }          }
2276          return;          return;
2277        }        }
2278                
2279        ## Step 15        ## Step 15
2280        $self->{insertion_mode} = 'in body' and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
2281                
2282        ## Step 16        ## Step 16
2283        $i--;        $i--;
# Line 2103  sub _reset_insertion_mode ($) { Line 2291  sub _reset_insertion_mode ($) {
2291  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
2292    my $self = shift;    my $self = shift;
2293    
   my $previous_insertion_mode;  
   
2294    my $active_formatting_elements = [];    my $active_formatting_elements = [];
2295    
2296    my $reconstruct_active_formatting_elements = sub { # MUST    my $reconstruct_active_formatting_elements = sub { # MUST
# Line 2205  sub _tree_construction_main ($) { Line 2391  sub _tree_construction_main ($) {
2391      ## Step 4      ## Step 4
2392      my $text = '';      my $text = '';
2393      !!!next-token;      !!!next-token;
2394      while ($token->{type} eq 'character') { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
2395        $text .= $token->{data};        $text .= $token->{data};
2396        !!!next-token;        !!!next-token;
2397      }      }
# Line 2220  sub _tree_construction_main ($) { Line 2406  sub _tree_construction_main ($) {
2406      $self->{content_model} = PCDATA_CONTENT_MODEL;      $self->{content_model} = PCDATA_CONTENT_MODEL;
2407    
2408      ## Step 7      ## Step 7
2409      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) {
2410        ## Ignore the token        ## Ignore the token
2411      } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {      } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {
2412        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!parse-error (type => 'in CDATA:#'.$token->{type});
# Line 2243  sub _tree_construction_main ($) { Line 2429  sub _tree_construction_main ($) {
2429            
2430      my $text = '';      my $text = '';
2431      !!!next-token;      !!!next-token;
2432      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) {
2433        $text .= $token->{data};        $text .= $token->{data};
2434        !!!next-token;        !!!next-token;
2435      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
# Line 2253  sub _tree_construction_main ($) { Line 2439  sub _tree_construction_main ($) {
2439                                
2440      $self->{content_model} = PCDATA_CONTENT_MODEL;      $self->{content_model} = PCDATA_CONTENT_MODEL;
2441    
2442      if ($token->{type} eq 'end tag' and      if ($token->{type} == END_TAG_TOKEN and
2443          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
2444        ## Ignore the token        ## Ignore the token
2445      } else {      } else {
# Line 2496  sub _tree_construction_main ($) { Line 2682  sub _tree_construction_main ($) {
2682                         }                         }
2683    }; # $insert_to_foster    }; # $insert_to_foster
2684    
2685    my $in_body = sub {    my $insert;
     my $insert = shift;  
     if ($token->{type} eq 'start tag') {  
       if ($token->{tag_name} eq 'script') {  
         ## NOTE: This is an "as if in head" code clone  
         $script_start_tag->($insert);  
         return;  
       } elsif ($token->{tag_name} eq 'style') {  
         ## NOTE: This is an "as if in head" code clone  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         return;  
       } elsif ({  
                 base => 1, link => 1,  
                }->{$token->{tag_name}}) {  
         ## NOTE: This is an "as if in head" code clone, only "-t" differs  
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'meta') {  
         ## NOTE: This is an "as if in head" code clone, only "-t" differs  
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.  
   
         unless ($self->{confident}) {  
           my $charset;  
           if ($token->{attributes}->{charset}) { ## TODO: And if supported  
             $charset = $token->{attributes}->{charset}->{value};  
           }  
           if ($token->{attributes}->{'http-equiv'}) {  
             ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
             if ($token->{attributes}->{'http-equiv'}->{value}  
                 =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=  
                     [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|  
                     ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {  
               $charset = defined $1 ? $1 : defined $2 ? $2 : $3;  
             } ## TODO: And if supported  
           }  
           ## TODO: Change the encoding  
         }  
   
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'title') {  
         !!!parse-error (type => 'in body:title');  
         ## NOTE: This is an "as if in head" code clone  
         $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {  
           if (defined $self->{head_element}) {  
             $self->{head_element}->append_child ($_[0]);  
           } else {  
             $insert->($_[0]);  
           }  
         });  
         return;  
       } elsif ($token->{tag_name} eq 'body') {  
         !!!parse-error (type => 'in body:body');  
                 
         if (@{$self->{open_elements}} == 1 or  
             $self->{open_elements}->[1]->[1] ne 'body') {  
           ## Ignore the token  
         } else {  
           my $body_el = $self->{open_elements}->[1]->[0];  
           for my $attr_name (keys %{$token->{attributes}}) {  
             unless ($body_el->has_attribute_ns (undef, $attr_name)) {  
               $body_el->set_attribute_ns  
                 (undef, [undef, $attr_name],  
                  $token->{attributes}->{$attr_name}->{value});  
             }  
           }  
         }  
         !!!next-token;  
         return;  
       } elsif ({  
                 address => 1, blockquote => 1, center => 1, dir => 1,  
                 div => 1, dl => 1, fieldset => 1, listing => 1,  
                 menu => 1, ol => 1, p => 1, ul => 1,  
                 pre => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         if ($token->{tag_name} eq 'pre') {  
           !!!next-token;  
           if ($token->{type} eq 'character') {  
             $token->{data} =~ s/^\x0A//;  
             unless (length $token->{data}) {  
               !!!next-token;  
             }  
           }  
         } else {  
           !!!next-token;  
         }  
         return;  
       } elsif ($token->{tag_name} eq 'form') {  
         if (defined $self->{form_element}) {  
           !!!parse-error (type => 'in form:form');  
           ## Ignore the token  
           !!!next-token;  
           return;  
         } else {  
           ## has a p element in scope  
           INSCOPE: for (reverse @{$self->{open_elements}}) {  
             if ($_->[1] eq 'p') {  
               !!!back-token;  
               $token = {type => 'end tag', tag_name => 'p'};  
               return;  
             } elsif ({  
                       table => 1, caption => 1, td => 1, th => 1,  
                       button => 1, marquee => 1, object => 1, html => 1,  
                      }->{$_->[1]}) {  
               last INSCOPE;  
             }  
           } # INSCOPE  
               
           !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           $self->{form_element} = $self->{open_elements}->[-1]->[0];  
           !!!next-token;  
           return;  
         }  
       } elsif ($token->{tag_name} eq 'li') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'li') {  
             if ($i != -1) {  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
             }  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             last LI;  
           }  
             
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {  
             if ($i != -1) {  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
             }  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             last LI;  
           }  
             
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'plaintext') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{content_model} = PLAINTEXT_CONTENT_MODEL;  
             
         !!!next-token;  
         return;  
       } elsif ({  
                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## NOTE: See <http://html5.org/tools/web-apps-tracker?from=925&to=926>  
         ## has an element in scope  
         #my $i;  
         #INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
         #  my $node = $self->{open_elements}->[$_];  
         #  if ({  
         #       h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
         #      }->{$node->[1]}) {  
         #    $i = $_;  
         #    last INSCOPE;  
         #  } elsif ({  
         #            table => 1, caption => 1, td => 1, th => 1,  
         #            button => 1, marquee => 1, object => 1, html => 1,  
         #           }->{$node->[1]}) {  
         #    last INSCOPE;  
         #  }  
         #} # INSCOPE  
         #    
         #if (defined $i) {  
         #  !!! parse-error (type => 'in hn:hn');  
         #  splice @{$self->{open_elements}}, $i;  
         #}  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'a') {  
         AFE: for my $i (reverse 0..$#$active_formatting_elements) {  
           my $node = $active_formatting_elements->[$i];  
           if ($node->[1] eq 'a') {  
             !!!parse-error (type => 'in a:a');  
               
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'a'};  
             $formatting_end_tag->($token->{tag_name});  
               
             AFE2: for (reverse 0..$#$active_formatting_elements) {  
               if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {  
                 splice @$active_formatting_elements, $_, 1;  
                 last AFE2;  
               }  
             } # AFE2  
             OE: for (reverse 0..$#{$self->{open_elements}}) {  
               if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {  
                 splice @{$self->{open_elements}}, $_, 1;  
                 last OE;  
               }  
             } # OE  
             last AFE;  
           } elsif ($node->[0] eq '#marker') {  
             last AFE;  
           }  
         } # AFE  
             
         $reconstruct_active_formatting_elements->($insert_to_current);  
   
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, $self->{open_elements}->[-1];  
   
         !!!next-token;  
         return;  
       } elsif ({  
                 b => 1, big => 1, em => 1, font => 1, i => 1,  
                 s => 1, small => 1, strile => 1,  
                 strong => 1, tt => 1, u => 1,  
                }->{$token->{tag_name}}) {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, $self->{open_elements}->[-1];  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'nobr') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
   
         ## has a |nobr| element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'nobr') {  
             !!!parse-error (type => 'not closed:nobr');  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'nobr'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, $self->{open_elements}->[-1];  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'button') {  
         ## has a button element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'button') {  
             !!!parse-error (type => 'in button:button');  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'button'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         $reconstruct_active_formatting_elements->($insert_to_current);  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, ['#marker', ''];  
   
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'marquee' or  
                $token->{tag_name} eq 'object') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, ['#marker', ''];  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'xmp') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         return;  
       } elsif ($token->{tag_name} eq 'table') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{insertion_mode} = 'in table';  
             
         !!!next-token;  
         return;  
       } elsif ({  
                 area => 1, basefont => 1, bgsound => 1, br => 1,  
                 embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,  
                 image => 1,  
                }->{$token->{tag_name}}) {  
         if ($token->{tag_name} eq 'image') {  
           !!!parse-error (type => 'image');  
           $token->{tag_name} = 'img';  
         }  
   
         ## NOTE: There is an "as if <br>" code clone.  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'hr') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
             
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'input') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         ## TODO: associate with $self->{form_element} if defined  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'isindex') {  
         !!!parse-error (type => 'isindex');  
           
         if (defined $self->{form_element}) {  
           ## Ignore the token  
           !!!next-token;  
           return;  
         } else {  
           my $at = $token->{attributes};  
           my $form_attrs;  
           $form_attrs->{action} = $at->{action} if $at->{action};  
           my $prompt_attr = $at->{prompt};  
           $at->{name} = {name => 'name', value => 'isindex'};  
           delete $at->{action};  
           delete $at->{prompt};  
           my @tokens = (  
                         {type => 'start tag', tag_name => 'form',  
                          attributes => $form_attrs},  
                         {type => 'start tag', tag_name => 'hr'},  
                         {type => 'start tag', tag_name => 'p'},  
                         {type => 'start tag', tag_name => 'label'},  
                        );  
           if ($prompt_attr) {  
             push @tokens, {type => 'character', data => $prompt_attr->{value}};  
           } else {  
             push @tokens, {type => 'character',  
                            data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD  
             ## TODO: make this configurable  
           }  
           push @tokens,  
                         {type => 'start tag', tag_name => 'input', attributes => $at},  
                         #{type => 'character', data => ''}, # SHOULD  
                         {type => 'end tag', tag_name => 'label'},  
                         {type => 'end tag', tag_name => 'p'},  
                         {type => 'start tag', tag_name => 'hr'},  
                         {type => 'end tag', tag_name => 'form'};  
           $token = shift @tokens;  
           !!!back-token (@tokens);  
           return;  
         }  
       } elsif ($token->{tag_name} eq 'textarea') {  
         my $tag_name = $token->{tag_name};  
         my $el;  
         !!!create-element ($el, $token->{tag_name}, $token->{attributes});  
           
         ## TODO: $self->{form_element} if defined  
         $self->{content_model} = RCDATA_CONTENT_MODEL;  
         delete $self->{escape}; # MUST  
           
         $insert->($el);  
           
         my $text = '';  
         !!!next-token;  
         if ($token->{type} eq 'character') {  
           $token->{data} =~ s/^\x0A//;  
           unless (length $token->{data}) {  
             !!!next-token;  
           }  
         }  
         while ($token->{type} eq 'character') {  
           $text .= $token->{data};  
           !!!next-token;  
         }  
         if (length $text) {  
           $el->manakai_append_text ($text);  
         }  
           
         $self->{content_model} = PCDATA_CONTENT_MODEL;  
           
         if ($token->{type} eq 'end tag' and  
             $token->{tag_name} eq $tag_name) {  
           ## Ignore the token  
         } else {  
           !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
         }  
         !!!next-token;  
         return;  
       } elsif ({  
                 iframe => 1,  
                 noembed => 1,  
                 noframes => 1,  
                 noscript => 0, ## TODO: 1 if scripting is enabled  
                }->{$token->{tag_name}}) {  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         return;  
       } elsif ($token->{tag_name} eq 'select') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           
         $self->{insertion_mode} = 'in select';  
         !!!next-token;  
         return;  
       } elsif ({  
                 caption => 1, col => 1, colgroup => 1, frame => 1,  
                 frameset => 1, head => 1, option => 1, optgroup => 1,  
                 tbody => 1, td => 1, tfoot => 1, th => 1,  
                 thead => 1, tr => 1,  
                }->{$token->{tag_name}}) {  
         !!!parse-error (type => 'in body:'.$token->{tag_name});  
         ## Ignore the token  
         !!!next-token;  
         return;  
           
         ## ISSUE: An issue on HTML5 new elements in the spec.  
       } else {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           
         !!!next-token;  
         return;  
       }  
     } elsif ($token->{type} eq 'end tag') {  
       if ($token->{tag_name} eq 'body') {  
         if (@{$self->{open_elements}} > 1 and  
             $self->{open_elements}->[1]->[1] eq 'body') {  
           for (@{$self->{open_elements}}) {  
             unless ({  
                        dd => 1, dt => 1, li => 1, p => 1, td => 1,  
                        th => 1, tr => 1, body => 1, html => 1,  
                      tbody => 1, tfoot => 1, thead => 1,  
                     }->{$_->[1]}) {  
               !!!parse-error (type => 'not closed:'.$_->[1]);  
             }  
           }  
   
           $self->{insertion_mode} = 'after body';  
           !!!next-token;  
           return;  
         } else {  
           !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           ## Ignore the token  
           !!!next-token;  
           return;  
         }  
       } elsif ($token->{tag_name} eq 'html') {  
         if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {  
           ## ISSUE: There is an issue in the spec.  
           if ($self->{open_elements}->[-1]->[1] ne 'body') {  
             !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);  
           }  
           $self->{insertion_mode} = 'after body';  
           ## reprocess  
           return;  
         } else {  
           !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           ## Ignore the token  
           !!!next-token;  
           return;  
         }  
       } elsif ({  
                 address => 1, blockquote => 1, center => 1, dir => 1,  
                 div => 1, dl => 1, fieldset => 1, listing => 1,  
                 menu => 1, ol => 1, pre => 1, ul => 1,  
                 p => 1,  
                 dd => 1, dt => 1, li => 1,  
                 button => 1, marquee => 1, object => 1,  
                }->{$token->{tag_name}}) {  
         ## has an element in scope  
         my $i;  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq $token->{tag_name}) {  
             ## generate implied end tags  
             if ({  
                  dd => ($token->{tag_name} ne 'dd'),  
                  dt => ($token->{tag_name} ne 'dt'),  
                  li => ($token->{tag_name} ne 'li'),  
                  p => ($token->{tag_name} ne 'p'),  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
             $i = $_;  
             last INSCOPE unless $token->{tag_name} eq 'p';  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
           
         if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {  
           if (defined $i) {  
             !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
           } else {  
             !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           }  
         }  
           
         if (defined $i) {  
           splice @{$self->{open_elements}}, $i;  
         } elsif ($token->{tag_name} eq 'p') {  
           ## As if <p>, then reprocess the current token  
           my $el;  
           !!!create-element ($el, 'p');  
           $insert->($el);  
         }  
         $clear_up_to_marker->()  
           if {  
             button => 1, marquee => 1, object => 1,  
           }->{$token->{tag_name}};  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'form') {  
         ## has an element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq $token->{tag_name}) {  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
             last INSCOPE;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
           
         if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {  
           pop @{$self->{open_elements}};  
         } else {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         }  
   
         undef $self->{form_element};  
         !!!next-token;  
         return;  
       } elsif ({  
                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
                }->{$token->{tag_name}}) {  
         ## has an element in scope  
         my $i;  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ({  
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
             $i = $_;  
             last INSCOPE;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
           
         if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         }  
           
         splice @{$self->{open_elements}}, $i if defined $i;  
         !!!next-token;  
         return;  
       } elsif ({  
                 a => 1,  
                 b => 1, big => 1, em => 1, font => 1, i => 1,  
                 nobr => 1, s => 1, small => 1, strile => 1,  
                 strong => 1, tt => 1, u => 1,  
                }->{$token->{tag_name}}) {  
         $formatting_end_tag->($token->{tag_name});  
         return;  
       } elsif ($token->{tag_name} eq 'br') {  
         !!!parse-error (type => 'unmatched end tag:br');  
   
         ## As if <br>  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         my $el;  
         !!!create-element ($el, 'br');  
         $insert->($el);  
           
         ## Ignore the token.  
         !!!next-token;  
         return;  
       } elsif ({  
                 caption => 1, col => 1, colgroup => 1, frame => 1,  
                 frameset => 1, head => 1, option => 1, optgroup => 1,  
                 tbody => 1, td => 1, tfoot => 1, th => 1,  
                 thead => 1, tr => 1,  
                 area => 1, basefont => 1, bgsound => 1,  
                 embed => 1, hr => 1, iframe => 1, image => 1,  
                 img => 1, input => 1, isindex => 1, noembed => 1,  
                 noframes => 1, param => 1, select => 1, spacer => 1,  
                 table => 1, textarea => 1, wbr => 1,  
                 noscript => 0, ## TODO: if scripting is enabled  
                }->{$token->{tag_name}}) {  
         !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
         ## Ignore the token  
         !!!next-token;  
         return;  
           
         ## ISSUE: Issue on HTML5 new elements in spec  
           
       } else {  
         ## Step 1  
         my $node_i = -1;  
         my $node = $self->{open_elements}->[$node_i];  
   
         ## Step 2  
         S2: {  
           if ($node->[1] eq $token->{tag_name}) {  
             ## Step 1  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
           
             ## Step 2  
             if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {  
               !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
             }  
               
             ## Step 3  
             splice @{$self->{open_elements}}, $node_i;  
   
             !!!next-token;  
             last S2;  
           } else {  
             ## Step 3  
             if (not $formatting_category->{$node->[1]} and  
                 #not $phrasing_category->{$node->[1]} and  
                 ($special_category->{$node->[1]} or  
                  $scoping_category->{$node->[1]})) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               last S2;  
             }  
           }  
             
           ## Step 4  
           $node_i--;  
           $node = $self->{open_elements}->[$node_i];  
             
           ## Step 5;  
           redo S2;  
         } # S2  
         return;  
       }  
     }  
   }; # $in_body  
2686    
2687    B: {    B: {
2688      if ($token->{type} eq 'DOCTYPE') {      if ($token->{type} == DOCTYPE_TOKEN) {
2689        !!!parse-error (type => 'DOCTYPE in the middle');        !!!parse-error (type => 'DOCTYPE in the middle');
2690        ## Ignore the token        ## Ignore the token
2691        ## Stay in the phase        ## Stay in the phase
2692        !!!next-token;        !!!next-token;
2693        redo B;        redo B;
2694      } elsif ($token->{type} eq 'end-of-file') {      } elsif ($token->{type} == END_OF_FILE_TOKEN) {
2695        if ($token->{insertion_mode} ne 'trailing end') {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
2696            #
2697          } else {
2698          ## Generate implied end tags          ## Generate implied end tags
2699          if ({          if ({
2700               dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,               dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,
2701               tbody => 1, tfoot=> 1, thead => 1,               tbody => 1, tfoot=> 1, thead => 1,
2702              }->{$self->{open_elements}->[-1]->[1]}) {              }->{$self->{open_elements}->[-1]->[1]}) {
2703            !!!back-token;            !!!back-token;
2704            $token = {type => 'end tag', tag_name => $self->{open_elements}->[-1]->[1]};            $token = {type => END_TAG_TOKEN, tag_name => $self->{open_elements}->[-1]->[1]};
2705            redo B;            redo B;
2706          }          }
2707                    
# Line 3374  sub _tree_construction_main ($) { Line 2719  sub _tree_construction_main ($) {
2719    
2720        ## Stop parsing        ## Stop parsing
2721        last B;        last B;
2722      } elsif ($token->{type} eq 'start tag' and      } elsif ($token->{type} == START_TAG_TOKEN and
2723               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
2724        if ($self->{insertion_mode} eq 'trailing end') {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
2725          ## Turn into the main phase          ## Turn into the main phase
2726          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html');
2727          $self->{insertion_mode} = $previous_insertion_mode;          $self->{insertion_mode} = AFTER_BODY_IM;
2728          } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
2729            ## Turn into the main phase
2730            !!!parse-error (type => 'after html:html');
2731            $self->{insertion_mode} = AFTER_FRAMESET_IM;
2732        }        }
2733    
2734  ## ISSUE: "aa<html>" is not a parse error.  ## ISSUE: "aa<html>" is not a parse error.
# Line 3397  sub _tree_construction_main ($) { Line 2746  sub _tree_construction_main ($) {
2746        }        }
2747        !!!next-token;        !!!next-token;
2748        redo B;        redo B;
2749      } elsif ($token->{type} eq 'comment') {      } elsif ($token->{type} == COMMENT_TOKEN) {
2750        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
2751        if ($self->{insertion_mode} eq 'trailing end') {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
2752          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
2753        } elsif ($self->{insertion_mode} eq 'after body') {        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
2754          $self->{open_elements}->[0]->[0]->append_child ($comment);          $self->{open_elements}->[0]->[0]->append_child ($comment);
2755        } else {        } else {
2756          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
2757        }        }
2758        !!!next-token;        !!!next-token;
2759        redo B;        redo B;
2760      } elsif ($self->{insertion_mode} eq 'before head') {      } elsif ($self->{insertion_mode} & HEAD_IMS) {
2761            if ($token->{type} eq 'character') {        if ($token->{type} == CHARACTER_TOKEN) {
2762              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
2763                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
2764                unless (length $token->{data}) {            unless (length $token->{data}) {
2765                  !!!next-token;              !!!next-token;
2766                  redo B;              redo B;
2767                }            }
2768              }          }
2769              ## As if <head>  
2770              !!!create-element ($self->{head_element}, 'head');          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2771              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            ## As if <head>
2772              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            !!!create-element ($self->{head_element}, 'head');
2773              $self->{insertion_mode} = 'in head';            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2774              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2775    
2776              ## Reprocess in the "in head" insertion mode...
2777              pop @{$self->{open_elements}};
2778    
2779              ## Reprocess in the "after head" insertion mode...
2780            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2781              ## As if </noscript>
2782              pop @{$self->{open_elements}};
2783              !!!parse-error (type => 'in noscript:#character');
2784              
2785              ## Reprocess in the "in head" insertion mode...
2786              ## As if </head>
2787              pop @{$self->{open_elements}};
2788    
2789              ## Reprocess in the "after head" insertion mode...
2790            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2791              pop @{$self->{open_elements}};
2792    
2793              ## Reprocess in the "after head" insertion mode...
2794            }
2795    
2796                ## "after head" insertion mode
2797                ## As if <body>
2798                !!!insert-element ('body');
2799                $self->{insertion_mode} = IN_BODY_IM;
2800              ## reprocess              ## reprocess
2801              redo B;              redo B;
2802            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} == START_TAG_TOKEN) {
             my $attr = $token->{tag_name} eq 'head' ? $token->{attributes} : {};  
             !!!create-element ($self->{head_element}, 'head', $attr);  
             $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});  
             push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
             $self->{insertion_mode} = 'in head';  
2803              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
2804                !!!next-token;                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2805              #} elsif ({                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});
2806              #          base => 1, link => 1, meta => 1,                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2807              #          script => 1, style => 1, title => 1,                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];
2808              #         }->{$token->{tag_name}}) {                  $self->{insertion_mode} = IN_HEAD_IM;
2809              #  ## reprocess                  !!!next-token;
2810              } else {                  redo B;
2811                ## reprocess                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2812              }                  #
2813              redo B;                } else {
2814            } elsif ($token->{type} eq 'end tag') {                  !!!parse-error (type => 'in head:head'); # or in head noscript
2815              if ({                  ## Ignore the token
2816                   head => 1, body => 1, html => 1,                  !!!next-token;
2817                   p => 1, br => 1,                  redo B;
2818                  }->{$token->{tag_name}}) {                }
2819                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2820                ## As if <head>                ## As if <head>
2821                !!!create-element ($self->{head_element}, 'head');                !!!create-element ($self->{head_element}, 'head');
2822                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2823                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2824                $self->{insertion_mode} = 'in head';  
2825                ## reprocess                $self->{insertion_mode} = IN_HEAD_IM;
2826                redo B;                ## Reprocess in the "in head" insertion mode...
             } else {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token ## ISSUE: An issue in the spec.  
               !!!next-token;  
               redo B;  
2827              }              }
2828            } else {  
2829              die "$0: $token->{type}: Unknown type";              if ($token->{tag_name} eq 'base') {
2830            }                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2831          } elsif ($self->{insertion_mode} eq 'in head' or                  ## As if </noscript>
2832                   $self->{insertion_mode} eq 'in head noscript' or                  pop @{$self->{open_elements}};
2833                   $self->{insertion_mode} eq 'after head') {                  !!!parse-error (type => 'in noscript:base');
2834            if ($token->{type} eq 'character') {                
2835              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {                  $self->{insertion_mode} = IN_HEAD_IM;
2836                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                  ## Reprocess in the "in head" insertion mode...
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
2837                }                }
2838              }  
               
             #  
           } elsif ($token->{type} eq 'start tag') {  
             if ({base => ($self->{insertion_mode} eq 'in head' or  
                           $self->{insertion_mode} eq 'after head'),  
                  link => 1}->{$token->{tag_name}}) {  
2839                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
2840                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2841                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2842                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2843                }                }
2844                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
2845                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2846                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2847                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2848                  !!!next-token;
2849                  redo B;
2850                } elsif ($token->{tag_name} eq 'link') {
2851                  ## NOTE: There is a "as if in head" code clone.
2852                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2853                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2854                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2855                  }
2856                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2857                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2858                  pop @{$self->{open_elements}}
2859                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2860                !!!next-token;                !!!next-token;
2861                redo B;                redo B;
2862              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
2863                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
2864                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2865                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2866                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2867                }                }
# Line 3500  sub _tree_construction_main ($) { Line 2869  sub _tree_construction_main ($) {
2869                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2870    
2871                unless ($self->{confident}) {                unless ($self->{confident}) {
                 my $charset;  
2872                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) { ## TODO: And if supported
2873                    $charset = $token->{attributes}->{charset}->{value};                    $self->{change_encoding}
2874                  }                        ->($self, $token->{attributes}->{charset}->{value});
2875                  if ($token->{attributes}->{'http-equiv'}) {                  } elsif ($token->{attributes}->{content}) {
2876                    ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.                    ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
2877                    if ($token->{attributes}->{'http-equiv'}->{value}                    if ($token->{attributes}->{content}->{value}
2878                        =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=                        =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=
2879                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
2880                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
2881                      $charset = defined $1 ? $1 : defined $2 ? $2 : $3;                      $self->{change_encoding}
2882                    } ## TODO: And if supported                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);
2883                      }
2884                  }                  }
                 ## TODO: Change the encoding  
2885                }                }
2886    
               ## TODO: Extracting |charset| from |meta|.  
2887                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2888                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2889                !!!next-token;                !!!next-token;
2890                redo B;                redo B;
2891              } elsif ($token->{tag_name} eq 'title' and              } elsif ($token->{tag_name} eq 'title') {
2892                       $self->{insertion_mode} eq 'in head') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2893                ## NOTE: There is a "as if in head" code clone.                  ## As if </noscript>
2894                if ($self->{insertion_mode} eq 'after head') {                  pop @{$self->{open_elements}};
2895                    !!!parse-error (type => 'in noscript:title');
2896                  
2897                    $self->{insertion_mode} = IN_HEAD_IM;
2898                    ## Reprocess in the "in head" insertion mode...
2899                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2900                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2901                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2902                }                }
2903    
2904                  ## NOTE: There is a "as if in head" code clone.
2905                my $parent = defined $self->{head_element} ? $self->{head_element}                my $parent = defined $self->{head_element} ? $self->{head_element}
2906                    : $self->{open_elements}->[-1]->[0];                    : $self->{open_elements}->[-1]->[0];
2907                $parse_rcdata->(RCDATA_CONTENT_MODEL,                $parse_rcdata->(RCDATA_CONTENT_MODEL,
2908                                sub { $parent->append_child ($_[0]) });                                sub { $parent->append_child ($_[0]) });
2909                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2910                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2911                redo B;                redo B;
2912              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style') {
2913                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
2914                ## insertion mode 'in head')                ## insertion mode IN_HEAD_IM)
2915                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
2916                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2917                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2918                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2919                }                }
2920                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
2921                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2922                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2923                redo B;                redo B;
2924              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
2925                if ($self->{insertion_mode} eq 'in head') {                if ($self->{insertion_mode} == IN_HEAD_IM) {
2926                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
2927                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2928                  $self->{insertion_mode} = 'in head noscript';                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
2929                  !!!next-token;                  !!!next-token;
2930                  redo B;                  redo B;
2931                } elsif ($self->{insertion_mode} eq 'in head noscript') {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2932                  !!!parse-error (type => 'in noscript:noscript');                  !!!parse-error (type => 'in noscript:noscript');
2933                  ## Ignore the token                  ## Ignore the token
2934                    !!!next-token;
2935                  redo B;                  redo B;
2936                } else {                } else {
2937                  #                  #
2938                }                }
2939              } elsif ($token->{tag_name} eq 'head' and              } elsif ($token->{tag_name} eq 'script') {
2940                       $self->{insertion_mode} ne 'after head') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2941                !!!parse-error (type => 'in head:head'); # or in head noscript                  ## As if </noscript>
2942                ## Ignore the token                  pop @{$self->{open_elements}};
2943                !!!next-token;                  !!!parse-error (type => 'in noscript:script');
2944                redo B;                
2945              } elsif ($self->{insertion_mode} ne 'in head noscript' and                  $self->{insertion_mode} = IN_HEAD_IM;
2946                       $token->{tag_name} eq 'script') {                  ## Reprocess in the "in head" insertion mode...
2947                if ($self->{insertion_mode} eq 'after head') {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2948                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2949                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2950                }                }
2951    
2952                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
2953                $script_start_tag->($insert_to_current);                $script_start_tag->($insert_to_current);
2954                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2955                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2956                redo B;                redo B;
2957              } elsif ($self->{insertion_mode} eq 'after head' and              } elsif ($token->{tag_name} eq 'body' or
                      $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  
2958                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
2959                !!!insert-element ('frameset', $token->{attributes});                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2960                $self->{insertion_mode} = 'in frameset';                  ## As if </noscript>
2961                    pop @{$self->{open_elements}};
2962                    !!!parse-error (type => 'in noscript:'.$token->{tag_name});
2963                    
2964                    ## Reprocess in the "in head" insertion mode...
2965                    ## As if </head>
2966                    pop @{$self->{open_elements}};
2967                    
2968                    ## Reprocess in the "after head" insertion mode...
2969                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2970                    pop @{$self->{open_elements}};
2971                    
2972                    ## Reprocess in the "after head" insertion mode...
2973                  }
2974    
2975                  ## "after head" insertion mode
2976                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2977                  if ($token->{tag_name} eq 'body') {
2978                    $self->{insertion_mode} = IN_BODY_IM;
2979                  } elsif ($token->{tag_name} eq 'frameset') {
2980                    $self->{insertion_mode} = IN_FRAMESET_IM;
2981                  } else {
2982                    die "$0: tag name: $self->{tag_name}";
2983                  }
2984                !!!next-token;                !!!next-token;
2985                redo B;                redo B;
2986              } else {              } else {
2987                #                #
2988              }              }
2989            } elsif ($token->{type} eq 'end tag') {  
2990              if ($self->{insertion_mode} eq 'in head' and              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2991                  $token->{tag_name} eq 'head') {                ## As if </noscript>
2992                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
2993                $self->{insertion_mode} = 'after head';                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
2994                !!!next-token;                
2995                redo B;                ## Reprocess in the "in head" insertion mode...
2996              } elsif ($self->{insertion_mode} eq 'in head noscript' and                ## As if </head>
                 $token->{tag_name} eq 'noscript') {  
2997                pop @{$self->{open_elements}};                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 {  
             #  
           }  
2998    
2999            ## As if </head> or </noscript> or <body>                ## Reprocess in the "after head" insertion mode...
3000            if ($self->{insertion_mode} eq 'in head') {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3001              pop @{$self->{open_elements}};                ## As if </head>
3002              $self->{insertion_mode} = 'after head';                pop @{$self->{open_elements}};
           } 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;  
3003    
3004            ## ISSUE: An issue in the spec.                ## Reprocess in the "after head" insertion mode...
3005          } 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});  
3006    
3007              !!!next-token;              ## "after head" insertion mode
3008              redo B;              ## As if <body>
3009            } else {              !!!insert-element ('body');
3010              $in_body->($insert_to_current);              $self->{insertion_mode} = IN_BODY_IM;
3011                ## reprocess
3012              redo B;              redo B;
3013            }            } elsif ($token->{type} == END_TAG_TOKEN) {
3014          } elsif ($self->{insertion_mode} eq 'in table') {              if ($token->{tag_name} eq 'head') {
3015            if ($token->{type} eq 'character') {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3016              ## NOTE: There are "character in table" code clones.                  ## As if <head>
3017              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {                  !!!create-element ($self->{head_element}, 'head');
3018                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3019                                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3020                unless (length $token->{data}) {  
3021                    ## Reprocess in the "in head" insertion mode...
3022                    pop @{$self->{open_elements}};
3023                    $self->{insertion_mode} = AFTER_HEAD_IM;
3024                    !!!next-token;
3025                    redo B;
3026                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3027                    ## As if </noscript>
3028                    pop @{$self->{open_elements}};
3029                    !!!parse-error (type => 'in noscript:script');
3030                    
3031                    ## Reprocess in the "in head" insertion mode...
3032                    pop @{$self->{open_elements}};
3033                    $self->{insertion_mode} = AFTER_HEAD_IM;
3034                    !!!next-token;
3035                    redo B;
3036                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3037                    pop @{$self->{open_elements}};
3038                    $self->{insertion_mode} = AFTER_HEAD_IM;
3039                  !!!next-token;                  !!!next-token;
3040                  redo B;                  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});  
3041                } else {                } else {
3042                  $foster_parent_element->insert_before                  #
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
3043                }                }
3044              } else {              } elsif ($token->{tag_name} eq 'noscript') {
3045                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
             }  
               
             !!!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]);  
3046                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3047                    $self->{insertion_mode} = IN_HEAD_IM;
3048                    !!!next-token;
3049                    redo B;
3050                  } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3051                    !!!parse-error (type => 'unmatched end tag:noscript');
3052                    ## Ignore the token ## ISSUE: An issue in the spec.
3053                    !!!next-token;
3054                    redo B;
3055                  } else {
3056                    #
3057                }                }
   
               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;  
3058              } elsif ({              } elsif ({
3059                        col => 1,                        body => 1, html => 1,
                       td => 1, th => 1, tr => 1,  
3060                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3061                ## Clear back to table context                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3062                while ($self->{open_elements}->[-1]->[1] ne 'table' and                  ## As if <head>
3063                       $self->{open_elements}->[-1]->[1] ne 'html') {                  !!!create-element ($self->{head_element}, 'head');
3064                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3065                  pop @{$self->{open_elements}};                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
               }  
   
               !!!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]);  
3066    
3067                ## As if </table>                  $self->{insertion_mode} = IN_HEAD_IM;
3068                ## have a table element in table scope                  ## Reprocess in the "in head" insertion mode...
3069                my $i;                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3070                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3071                  my $node = $self->{open_elements}->[$_];                  ## Ignore the token
                 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>  
3072                  !!!next-token;                  !!!next-token;
3073                  redo B;                  redo B;
3074                }                }
3075                                
3076                ## generate implied end tags                #
3077                if ({              } elsif ({
3078                     dd => 1, dt => 1, li => 1, p => 1,                        p => 1, br => 1,
3079                     td => 1, th => 1, tr => 1,                       }->{$token->{tag_name}}) {
3080                     tbody => 1, tfoot=> 1, thead => 1,                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3081                    }->{$self->{open_elements}->[-1]->[1]}) {                  ## As if <head>
3082                  !!!back-token; # <table>                  !!!create-element ($self->{head_element}, 'head');
3083                  $token = {type => 'end tag', tag_name => 'table'};                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3084                  !!!back-token;                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
3085    
3086                if ($self->{open_elements}->[-1]->[1] ne 'table') {                  $self->{insertion_mode} = IN_HEAD_IM;
3087                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  ## Reprocess in the "in head" insertion mode...
3088                }                }
3089    
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               ## reprocess  
               redo B;  
             } else {  
3090                #                #
3091              }              } else {
3092            } elsif ($token->{type} eq 'end tag') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3093              if ($token->{tag_name} eq 'table') {                  #
3094                ## have a table element in table scope                } else {
               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) {  
3095                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3096                  ## Ignore the token                  ## Ignore the token
3097                  !!!next-token;                  !!!next-token;
3098                  redo B;                  redo B;
3099                }                }
3100                              }
               ## 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]);  
               }  
3101    
3102                splice @{$self->{open_elements}}, $i;              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3103                  ## As if </noscript>
3104                  pop @{$self->{open_elements}};
3105                  !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
3106                  
3107                  ## Reprocess in the "in head" insertion mode...
3108                  ## As if </head>
3109                  pop @{$self->{open_elements}};
3110    
3111                $self->_reset_insertion_mode;                ## Reprocess in the "after head" insertion mode...
3112                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3113                  ## As if </head>
3114                  pop @{$self->{open_elements}};
3115    
3116                !!!next-token;                ## Reprocess in the "after head" insertion mode...
3117                redo B;              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
             } 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}}) {  
3118                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3119                ## Ignore the token                ## Ignore the token ## ISSUE: An issue in the spec.
3120                !!!next-token;                !!!next-token;
3121                redo B;                redo B;
             } else {  
               #  
3122              }              }
3123    
3124                ## "after head" insertion mode
3125                ## As if <body>
3126                !!!insert-element ('body');
3127                $self->{insertion_mode} = IN_BODY_IM;
3128                ## reprocess
3129                redo B;
3130            } else {            } else {
3131              #              die "$0: $token->{type}: Unknown token type";
3132            }            }
3133    
3134            !!!parse-error (type => 'in table:'.$token->{tag_name});            ## ISSUE: An issue in the spec.
3135            $in_body->($insert_to_foster);      } elsif ($self->{insertion_mode} & BODY_IMS) {
3136            redo B;            if ($token->{type} == CHARACTER_TOKEN) {
3137          } elsif ($self->{insertion_mode} eq 'in caption') {              ## NOTE: There is a code clone of "character in body".
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a code clone of "character in body".  
3138              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
3139                            
3140              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3141    
3142              !!!next-token;              !!!next-token;
3143              redo B;              redo B;
3144            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} == START_TAG_TOKEN) {
3145              if ({              if ({
3146                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
3147                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
3148                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
3149                !!!parse-error (type => 'not closed:caption');                if ($self->{insertion_mode} == IN_CELL_IM) {
3150                    ## have an element in table scope
3151                    my $tn;
3152                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3153                      my $node = $self->{open_elements}->[$_];
3154                      if ($node->[1] eq 'td' or $node->[1] eq 'th') {
3155                        $tn = $node->[1];
3156                        last INSCOPE;
3157                      } elsif ({
3158                                table => 1, html => 1,
3159                               }->{$node->[1]}) {
3160                        last INSCOPE;
3161                      }
3162                    } # INSCOPE
3163                      unless (defined $tn) {
3164                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3165                        ## Ignore the token
3166                        !!!next-token;
3167                        redo B;
3168                      }
3169                    
3170                    ## Close the cell
3171                    !!!back-token; # <?>
3172                    $token = {type => END_TAG_TOKEN, tag_name => $tn};
3173                    redo B;
3174                  } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3175                    !!!parse-error (type => 'not closed:caption');
3176                    
3177                    ## As if </caption>
3178                    ## have a table element in table scope
3179                    my $i;
3180                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3181                      my $node = $self->{open_elements}->[$_];
3182                      if ($node->[1] eq 'caption') {
3183                        $i = $_;
3184                        last INSCOPE;
3185                      } elsif ({
3186                                table => 1, html => 1,
3187                               }->{$node->[1]}) {
3188                        last INSCOPE;
3189                      }
3190                    } # INSCOPE
3191                      unless (defined $i) {
3192                        !!!parse-error (type => 'unmatched end tag:caption');
3193                        ## Ignore the token
3194                        !!!next-token;
3195                        redo B;
3196                      }
3197                    
3198                    ## generate implied end tags
3199                    if ({
3200                         dd => 1, dt => 1, li => 1, p => 1,
3201                         td => 1, th => 1, tr => 1,
3202                         tbody => 1, tfoot=> 1, thead => 1,
3203                        }->{$self->{open_elements}->[-1]->[1]}) {
3204                      !!!back-token; # <?>
3205                      $token = {type => END_TAG_TOKEN, tag_name => 'caption'};
3206                      !!!back-token;
3207                      $token = {type => END_TAG_TOKEN,
3208                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3209                      redo B;
3210                    }
3211    
3212                ## As if </caption>                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3213                ## have a table element in table scope                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
               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;  
3214                  }                  }
3215                } # INSCOPE                  
3216                unless (defined $i) {                  splice @{$self->{open_elements}}, $i;
3217                  !!!parse-error (type => 'unmatched end tag:caption');                  
3218                    $clear_up_to_marker->();
3219                    
3220                    $self->{insertion_mode} = IN_TABLE_IM;
3221                    
3222                    ## reprocess
3223                    redo B;
3224                  } else {
3225                    #
3226                  }
3227                } else {
3228                  #
3229                }
3230              } elsif ($token->{type} == END_TAG_TOKEN) {
3231                if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
3232                  if ($self->{insertion_mode} == IN_CELL_IM) {
3233                    ## have an element in table scope
3234                    my $i;
3235                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3236                      my $node = $self->{open_elements}->[$_];
3237                      if ($node->[1] eq $token->{tag_name}) {
3238                        $i = $_;
3239                        last INSCOPE;
3240                      } elsif ({
3241                                table => 1, html => 1,
3242                               }->{$node->[1]}) {
3243                        last INSCOPE;
3244                      }
3245                    } # INSCOPE
3246                      unless (defined $i) {
3247                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3248                        ## Ignore the token
3249                        !!!next-token;
3250                        redo B;
3251                      }
3252                    
3253                    ## generate implied end tags
3254                    if ({
3255                         dd => 1, dt => 1, li => 1, p => 1,
3256                         td => ($token->{tag_name} eq 'th'),
3257                         th => ($token->{tag_name} eq 'td'),
3258                         tr => 1,
3259                         tbody => 1, tfoot=> 1, thead => 1,
3260                        }->{$self->{open_elements}->[-1]->[1]}) {
3261                      !!!back-token;
3262                      $token = {type => END_TAG_TOKEN,
3263                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3264                      redo B;
3265                    }
3266                    
3267                    if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
3268                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3269                    }
3270                    
3271                    splice @{$self->{open_elements}}, $i;
3272                    
3273                    $clear_up_to_marker->();
3274                    
3275                    $self->{insertion_mode} = IN_ROW_IM;
3276                    
3277                    !!!next-token;
3278                    redo B;
3279                  } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3280                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3281                  ## Ignore the token                  ## Ignore the token
3282                  !!!next-token;                  !!!next-token;
3283                  redo B;                  redo B;
3284                  } else {
3285                    #
3286                }                }
3287                              } elsif ($token->{tag_name} eq 'caption') {
3288                ## generate implied end tags                if ($self->{insertion_mode} == IN_CAPTION_IM) {
3289                if ({                  ## have a table element in table scope
3290                     dd => 1, dt => 1, li => 1, p => 1,                  my $i;
3291                     td => 1, th => 1, tr => 1,                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3292                     tbody => 1, tfoot=> 1, thead => 1,                    my $node = $self->{open_elements}->[$_];
3293                    }->{$self->{open_elements}->[-1]->[1]}) {                    if ($node->[1] eq $token->{tag_name}) {
3294                  !!!back-token; # <?>                      $i = $_;
3295                  $token = {type => 'end tag', tag_name => 'caption'};                      last INSCOPE;
3296                  !!!back-token;                    } elsif ({
3297                  $token = {type => 'end tag',                              table => 1, html => 1,
3298                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                             }->{$node->[1]}) {
3299                        last INSCOPE;
3300                      }
3301                    } # INSCOPE
3302                      unless (defined $i) {
3303                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3304                        ## Ignore the token
3305                        !!!next-token;
3306                        redo B;
3307                      }
3308                    
3309                    ## generate implied end tags
3310                    if ({
3311                         dd => 1, dt => 1, li => 1, p => 1,
3312                         td => 1, th => 1, tr => 1,
3313                         tbody => 1, tfoot=> 1, thead => 1,
3314                        }->{$self->{open_elements}->[-1]->[1]}) {
3315                      !!!back-token;
3316                      $token = {type => END_TAG_TOKEN,
3317                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3318                      redo B;
3319                    }
3320                    
3321                    if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3322                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3323                    }
3324                    
3325                    splice @{$self->{open_elements}}, $i;
3326                    
3327                    $clear_up_to_marker->();
3328                    
3329                    $self->{insertion_mode} = IN_TABLE_IM;
3330                    
3331                    !!!next-token;
3332                  redo B;                  redo B;
3333                  } elsif ($self->{insertion_mode} == IN_CELL_IM) {
3334                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3335                    ## Ignore the token
3336                    !!!next-token;
3337                    redo B;
3338                  } else {
3339                    #
3340                }                }
3341                } elsif ({
3342                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                        table => 1, tbody => 1, tfoot => 1,
3343                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                        thead => 1, tr => 1,
3344                }                       }->{$token->{tag_name}} and
3345                         $self->{insertion_mode} == IN_CELL_IM) {
3346                splice @{$self->{open_elements}}, $i;                ## have an element in table scope
   
               $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  
3347                my $i;                my $i;
3348                  my $tn;
3349                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3350                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
3351                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
3352                    $i = $_;                    $i = $_;
3353                    last INSCOPE;                    last INSCOPE;
3354                    } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {
3355                      $tn = $node->[1];
3356                      ## NOTE: There is exactly one |td| or |th| element
3357                      ## in scope in the stack of open elements by definition.
3358                  } elsif ({                  } elsif ({
3359                            table => 1, html => 1,                            table => 1, html => 1,
3360                           }->{$node->[1]}) {                           }->{$node->[1]}) {
# Line 3961  sub _tree_construction_main ($) { Line 3367  sub _tree_construction_main ($) {
3367                  !!!next-token;                  !!!next-token;
3368                  redo B;                  redo B;
3369                }                }
                 
               ## 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->();  
3370    
3371                $self->{insertion_mode} = 'in table';                ## Close the cell
3372                  !!!back-token; # </?>
3373                !!!next-token;                $token = {type => END_TAG_TOKEN, tag_name => $tn};
3374                redo B;                redo B;
3375              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table' and
3376                         $self->{insertion_mode} == IN_CAPTION_IM) {
3377                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption');
3378    
3379                ## As if </caption>                ## As if </caption>
# Line 4017  sub _tree_construction_main ($) { Line 3404  sub _tree_construction_main ($) {
3404                     tbody => 1, tfoot=> 1, thead => 1,                     tbody => 1, tfoot=> 1, thead => 1,
3405                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
3406                  !!!back-token; # </table>                  !!!back-token; # </table>
3407                  $token = {type => 'end tag', tag_name => 'caption'};                  $token = {type => END_TAG_TOKEN, tag_name => 'caption'};
3408                  !!!back-token;                  !!!back-token;
3409                  $token = {type => 'end tag',                  $token = {type => END_TAG_TOKEN,
3410                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3411                  redo B;                  redo B;
3412                }                }
# Line 4032  sub _tree_construction_main ($) { Line 3419  sub _tree_construction_main ($) {
3419    
3420                $clear_up_to_marker->();                $clear_up_to_marker->();
3421    
3422                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
3423    
3424                ## reprocess                ## reprocess
3425                redo B;                redo B;
3426              } elsif ({              } elsif ({
3427                        body => 1, col => 1, colgroup => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
                       html => 1, tbody => 1, td => 1, tfoot => 1,  
                       th => 1, thead => 1, tr => 1,  
3428                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3429                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
3430                ## Ignore the token                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
               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 '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');  
3431                  ## Ignore the token                  ## Ignore the token
3432                  !!!next-token;                  !!!next-token;
3433                  redo B;                  redo B;
3434                } else {                } else {
3435                  pop @{$self->{open_elements}}; # colgroup                  #
                 $self->{insertion_mode} = 'in table';  
                 !!!next-token;  
                 redo B;              
3436                }                }
3437              } elsif ($token->{tag_name} eq 'col') {              } elsif ({
3438                !!!parse-error (type => 'unmatched end tag:col');                        tbody => 1, tfoot => 1,
3439                          thead => 1, tr => 1,
3440                         }->{$token->{tag_name}} and
3441                         $self->{insertion_mode} == IN_CAPTION_IM) {
3442                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3443                ## Ignore the token                ## Ignore the token
3444                !!!next-token;                !!!next-token;
3445                redo B;                redo B;
3446              } else {              } else {
3447                #                #
3448              }              }
3449            } else {        } else {
3450              #          die "$0: $token->{type}: Unknown token type";
3451            }        }
3452    
3453            ## As if </colgroup>        $insert = $insert_to_current;
3454            if ($self->{open_elements}->[-1]->[1] eq 'html') {        #
3455              !!!parse-error (type => 'unmatched end tag:colgroup');      } elsif ($self->{insertion_mode} & TABLE_IMS) {
3456              ## Ignore the token        if ($token->{type} == CHARACTER_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.  
3457              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3458                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3459                                
# Line 4129  sub _tree_construction_main ($) { Line 3470  sub _tree_construction_main ($) {
3470              ## into the current node" while characters might not be              ## into the current node" while characters might not be
3471              ## result in a new Text node.              ## result in a new Text node.
3472              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
3473                
3474              if ({              if ({
3475                   table => 1, tbody => 1, tfoot => 1,                   table => 1, tbody => 1, tfoot => 1,
3476                   thead => 1, tr => 1,                   thead => 1, tr => 1,
# Line 4169  sub _tree_construction_main ($) { Line 3510  sub _tree_construction_main ($) {
3510                            
3511              !!!next-token;              !!!next-token;
3512              redo B;              redo B;
3513            } elsif ($token->{type} eq 'start tag') {        } elsif ($token->{type} == START_TAG_TOKEN) {
3514              if ({              if ({
3515                   tr => 1,                   tr => ($self->{insertion_mode} != IN_ROW_IM),
3516                   th => 1, td => 1,                   th => 1, td => 1,
3517                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
3518                unless ($token->{tag_name} eq 'tr') {                if ($self->{insertion_mode} == IN_TABLE_IM) {
3519                  !!!parse-error (type => 'missing start tag:tr');                  ## Clear back to table context
3520                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
3521                           $self->{open_elements}->[-1]->[1] ne 'html') {
3522                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3523                      pop @{$self->{open_elements}};
3524                    }
3525                    
3526                    !!!insert-element ('tbody');
3527                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
3528                    ## reprocess in the "in table body" insertion mode...
3529                }                }
3530    
3531                ## Clear back to table body context                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3532                    unless ($token->{tag_name} eq 'tr') {
3533                      !!!parse-error (type => 'missing start tag:tr');
3534                    }
3535                    
3536                    ## Clear back to table body context
3537                    while (not {
3538                      tbody => 1, tfoot => 1, thead => 1, html => 1,
3539                    }->{$self->{open_elements}->[-1]->[1]}) {
3540                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3541                      pop @{$self->{open_elements}};
3542                    }
3543                    
3544                    $self->{insertion_mode} = IN_ROW_IM;
3545                    if ($token->{tag_name} eq 'tr') {
3546                      !!!insert-element ($token->{tag_name}, $token->{attributes});
3547                      !!!next-token;
3548                      redo B;
3549                    } else {
3550                      !!!insert-element ('tr');
3551                      ## reprocess in the "in row" insertion mode
3552                    }
3553                  }
3554    
3555                  ## Clear back to table row context
3556                while (not {                while (not {
3557                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  tr => 1, html => 1,
3558                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
3559                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3560                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3561                }                }
3562                                
3563                $self->{insertion_mode} = 'in row';                !!!insert-element ($token->{tag_name}, $token->{attributes});
3564                if ($token->{tag_name} eq 'tr') {                $self->{insertion_mode} = IN_CELL_IM;
3565                  !!!insert-element ($token->{tag_name}, $token->{attributes});  
3566                  !!!next-token;                push @$active_formatting_elements, ['#marker', ''];
3567                } else {                
3568                  !!!insert-element ('tr');                !!!next-token;
                 ## reprocess  
               }  
3569                redo B;                redo B;
3570              } elsif ({              } elsif ({
3571                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
3572                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
3573                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
3574                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3575                ## have an element in table scope                if ($self->{insertion_mode} == IN_ROW_IM) {
3576                my $i;                  ## As if </tr>
3577                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
3578                  my $node = $self->{open_elements}->[$_];                  my $i;
3579                  if ({                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3580                       tbody => 1, thead => 1, tfoot => 1,                    my $node = $self->{open_elements}->[$_];
3581                      }->{$node->[1]}) {                    if ($node->[1] eq 'tr') {
3582                    $i = $_;                      $i = $_;
3583                    last INSCOPE;                      last INSCOPE;
3584                  } elsif ({                    } elsif ({
3585                            table => 1, html => 1,                              table => 1, html => 1,
3586                           }->{$node->[1]}) {                             }->{$node->[1]}) {
3587                    last INSCOPE;                      last INSCOPE;
3588                      }
3589                    } # INSCOPE
3590                    unless (defined $i) {
3591                      !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});
3592                      ## Ignore the token
3593                      !!!next-token;
3594                      redo B;
3595                    }
3596                    
3597                    ## Clear back to table row context
3598                    while (not {
3599                      tr => 1, html => 1,
3600                    }->{$self->{open_elements}->[-1]->[1]}) {
3601                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3602                      pop @{$self->{open_elements}};
3603                    }
3604                    
3605                    pop @{$self->{open_elements}}; # tr
3606                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
3607                    if ($token->{tag_name} eq 'tr') {
3608                      ## reprocess
3609                      redo B;
3610                    } else {
3611                      ## reprocess in the "in table body" insertion mode...
3612                  }                  }
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
3613                }                }
3614    
3615                ## Clear back to table body context                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3616                while (not {                  ## have an element in table scope
3617                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  my $i;
3618                }->{$self->{open_elements}->[-1]->[1]}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3619                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    my $node = $self->{open_elements}->[$_];
3620                      if ({
3621                           tbody => 1, thead => 1, tfoot => 1,
3622                          }->{$node->[1]}) {
3623                        $i = $_;
3624                        last INSCOPE;
3625                      } elsif ({
3626                                table => 1, html => 1,
3627                               }->{$node->[1]}) {
3628                        last INSCOPE;
3629                      }
3630                    } # INSCOPE
3631                    unless (defined $i) {
3632                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3633                      ## Ignore the token
3634                      !!!next-token;
3635                      redo B;
3636                    }
3637    
3638                    ## Clear back to table body context
3639                    while (not {
3640                      tbody => 1, tfoot => 1, thead => 1, html => 1,
3641                    }->{$self->{open_elements}->[-1]->[1]}) {
3642                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3643                      pop @{$self->{open_elements}};
3644                    }
3645                    
3646                    ## As if <{current node}>
3647                    ## have an element in table scope
3648                    ## true by definition
3649                    
3650                    ## Clear back to table body context
3651                    ## nop by definition
3652                    
3653                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3654                    $self->{insertion_mode} = IN_TABLE_IM;
3655                    ## reprocess in "in table" insertion mode...
3656                }                }
3657    
3658                ## As if <{current node}>                if ($token->{tag_name} eq 'col') {
3659                ## have an element in table scope                  ## Clear back to table context
3660                ## true by definition                  while ($self->{open_elements}->[-1]->[1] ne 'table' and
3661                           $self->{open_elements}->[-1]->[1] ne 'html') {
3662                ## Clear back to table body context                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3663                ## nop by definition                    pop @{$self->{open_elements}};
3664                    }
3665                pop @{$self->{open_elements}};                  
3666                $self->{insertion_mode} = 'in table';                  !!!insert-element ('colgroup');
3667                ## reprocess                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
3668                redo B;                  ## reprocess
3669                    redo B;
3670                  } elsif ({
3671                            caption => 1,
3672                            colgroup => 1,
3673                            tbody => 1, tfoot => 1, thead => 1,
3674                           }->{$token->{tag_name}}) {
3675                    ## Clear back to table context
3676                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
3677                           $self->{open_elements}->[-1]->[1] ne 'html') {
3678                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3679                      pop @{$self->{open_elements}};
3680                    }
3681                    
3682                    push @$active_formatting_elements, ['#marker', '']
3683                        if $token->{tag_name} eq 'caption';
3684                    
3685                    !!!insert-element ($token->{tag_name}, $token->{attributes});
3686                    $self->{insertion_mode} = {
3687                                               caption => IN_CAPTION_IM,
3688                                               colgroup => IN_COLUMN_GROUP_IM,
3689                                               tbody => IN_TABLE_BODY_IM,
3690                                               tfoot => IN_TABLE_BODY_IM,
3691                                               thead => IN_TABLE_BODY_IM,
3692                                              }->{$token->{tag_name}};
3693                    !!!next-token;
3694                    redo B;
3695                  } else {
3696                    die "$0: in table: <>: $token->{tag_name}";
3697                  }
3698              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
3699                ## NOTE: This is a code clone of "table in table"                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
               !!!parse-error (type => 'not closed:table');  
3700    
3701                ## As if </table>                ## As if </table>
3702                ## have a table element in table scope                ## have a table element in table scope
# Line 4272  sub _tree_construction_main ($) { Line 3726  sub _tree_construction_main ($) {
3726                     tbody => 1, tfoot=> 1, thead => 1,                     tbody => 1, tfoot=> 1, thead => 1,
3727                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
3728                  !!!back-token; # <table>                  !!!back-token; # <table>
3729                  $token = {type => 'end tag', tag_name => 'table'};                  $token = {type => END_TAG_TOKEN, tag_name => 'table'};
3730                  !!!back-token;                  !!!back-token;
3731                  $token = {type => 'end tag',                  $token = {type => END_TAG_TOKEN,
3732                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3733                  redo B;                  redo B;
3734                }                }
# Line 4285  sub _tree_construction_main ($) { Line 3739  sub _tree_construction_main ($) {
3739    
3740                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
3741    
3742                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
3743    
3744                ## reprocess                ## reprocess
3745                redo B;                redo B;
3746              } else {          } else {
3747                #            !!!parse-error (type => 'in table:'.$token->{tag_name});
3748              }  
3749            } elsif ($token->{type} eq 'end tag') {            $insert = $insert_to_foster;
3750              if ({            #
3751                   tbody => 1, tfoot => 1, thead => 1,          }
3752                  }->{$token->{tag_name}}) {        } elsif ($token->{type} == END_TAG_TOKEN) {
3753                if ($token->{tag_name} eq 'tr' and
3754                    $self->{insertion_mode} == IN_ROW_IM) {
3755                ## have an element in table scope                ## have an element in table scope
3756                my $i;                my $i;
3757                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 4316  sub _tree_construction_main ($) { Line 3772  sub _tree_construction_main ($) {
3772                  redo B;                  redo B;
3773                }                }
3774    
3775                ## Clear back to table body context                ## Clear back to table row context
3776                while (not {                while (not {
3777                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  tr => 1, html => 1,
3778                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
3779                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3780                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3781                }                }
3782    
3783                pop @{$self->{open_elements}};                pop @{$self->{open_elements}}; # tr
3784                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_BODY_IM;
3785                !!!next-token;                !!!next-token;
3786                redo B;                redo B;
3787              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
3788                ## have an element in table scope                if ($self->{insertion_mode} == IN_ROW_IM) {
3789                my $i;                  ## As if </tr>
3790                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
3791                  my $node = $self->{open_elements}->[$_];                  my $i;
3792                  if ({                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3793                       tbody => 1, thead => 1, tfoot => 1,                    my $node = $self->{open_elements}->[$_];
3794                      }->{$node->[1]}) {                    if ($node->[1] eq 'tr') {
3795                    $i = $_;                      $i = $_;
3796                    last INSCOPE;                      last INSCOPE;
3797                  } elsif ({                    } elsif ({
3798                            table => 1, html => 1,                              table => 1, html => 1,
3799                           }->{$node->[1]}) {                             }->{$node->[1]}) {
3800                    last INSCOPE;                      last INSCOPE;
3801                      }
3802                    } # INSCOPE
3803                    unless (defined $i) {
3804                      !!!parse-error (type => 'unmatched end tag:'.$token->{type});
3805                      ## Ignore the token
3806                      !!!next-token;
3807                      redo B;
3808                  }                  }
3809                } # INSCOPE                  
3810                unless (defined $i) {                  ## Clear back to table row context
3811                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  while (not {
3812                  ## Ignore the token                    tr => 1, html => 1,
                 !!!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,  
3813                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
3814                # MUST                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3815                my $foster_parent_element;                    pop @{$self->{open_elements}};
3816                my $next_sibling;                  }
3817                my $prev_sibling;                  
3818                OE: for (reverse 0..$#{$self->{open_elements}}) {                  pop @{$self->{open_elements}}; # tr
3819                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
3820                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                  ## reprocess in the "in table body" insertion mode...
3821                    if (defined $parent and $parent->node_type == 1) {                }
3822                      $foster_parent_element = $parent;  
3823                      $next_sibling = $self->{open_elements}->[$_]->[0];                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3824                      $prev_sibling = $next_sibling->previous_sibling;                  ## have an element in table scope
3825                    } else {                  my $i;
3826                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3827                      $prev_sibling = $foster_parent_element->last_child;                    my $node = $self->{open_elements}->[$_];
3828                      if ({
3829                           tbody => 1, thead => 1, tfoot => 1,
3830                          }->{$node->[1]}) {
3831                        $i = $_;
3832                        last INSCOPE;
3833                      } elsif ({
3834                                table => 1, html => 1,
3835                               }->{$node->[1]}) {
3836                        last INSCOPE;
3837                    }                    }
3838                    last OE;                  } # INSCOPE
3839                    unless (defined $i) {
3840                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3841                      ## Ignore the token
3842                      !!!next-token;
3843                      redo B;
3844                  }                  }
3845                } # OE                  
3846                $foster_parent_element = $self->{open_elements}->[0]->[0] and                  ## Clear back to table body context
3847                $prev_sibling = $foster_parent_element->last_child                  while (not {
3848                  unless defined $foster_parent_element;                    tbody => 1, tfoot => 1, thead => 1, html => 1,
3849                if (defined $prev_sibling and                  }->{$self->{open_elements}->[-1]->[1]}) {
3850                    $prev_sibling->node_type == 3) {                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3851                  $prev_sibling->manakai_append_text ($token->{data});                    pop @{$self->{open_elements}};
               } 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 '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;  
3852                  }                  }
3853                } # INSCOPE                  
3854                unless (defined $i) {                  ## As if <{current node}>
3855                  !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                  ## have an element in table scope
3856                  ## Ignore the token                  ## true by definition
3857                  !!!next-token;                  
3858                  redo B;                  ## Clear back to table body context
3859                }                  ## nop by definition
3860                    
               ## 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]);  
3861                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3862                    $self->{insertion_mode} = IN_TABLE_IM;
3863                    ## reprocess in the "in table" insertion mode...
3864                }                }
3865    
               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>  
3866                ## have a table element in table scope                ## have a table element in table scope
3867                my $i;                my $i;
3868                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3869                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
3870                  if ($node->[1] eq 'table') {                  if ($node->[1] eq $token->{tag_name}) {
3871                    $i = $_;                    $i = $_;
3872                    last INSCOPE;                    last INSCOPE;
3873                  } elsif ({                  } elsif ({
# Line 4522  sub _tree_construction_main ($) { Line 3877  sub _tree_construction_main ($) {
3877                  }                  }
3878                } # INSCOPE                } # INSCOPE
3879                unless (defined $i) {                unless (defined $i) {
3880                  !!!parse-error (type => 'unmatched end tag:table');                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3881                  ## Ignore tokens </table><table>                  ## Ignore the token
3882                  !!!next-token;                  !!!next-token;
3883                  redo B;                  redo B;
3884                }                }
3885                  
3886                ## generate implied end tags                ## generate implied end tags
3887                if ({                if ({
3888                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
3889                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
3890                     tbody => 1, tfoot=> 1, thead => 1,                     tbody => 1, tfoot=> 1, thead => 1,
3891                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
                 !!!back-token; # <table>  
                 $token = {type => 'end tag', tag_name => 'table'};  
3892                  !!!back-token;                  !!!back-token;
3893                  $token = {type => 'end tag',                  $token = {type => END_TAG_TOKEN,
3894                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3895                  redo B;                  redo B;
3896                }                }
3897                  
3898                if ($self->{open_elements}->[-1]->[1] ne 'table') {                if ($self->{open_elements}->[-1]->[1] ne 'table') {
3899                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3900                }                }
3901                    
3902                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
3903                  
3904                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
3905                  
               ## 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';  
3906                !!!next-token;                !!!next-token;
3907                redo B;                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;  
3908              } elsif ({              } elsif ({
3909                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
3910                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}} and
3911                ## have an element in table scope                       $self->{insertion_mode} & ROW_IMS) {
3912                my $i;                if ($self->{insertion_mode} == IN_ROW_IM) {
3913                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
3914                  my $node = $self->{open_elements}->[$_];                  my $i;
3915                  if ($node->[1] eq $token->{tag_name}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3916                    $i = $_;                    my $node = $self->{open_elements}->[$_];
3917                    last INSCOPE;                    if ($node->[1] eq $token->{tag_name}) {
3918                  } elsif ({                      $i = $_;
3919                            table => 1, html => 1,                      last INSCOPE;
3920                           }->{$node->[1]}) {                    } elsif ({
3921                    last INSCOPE;                              table => 1, html => 1,
3922                               }->{$node->[1]}) {
3923                        last INSCOPE;
3924                      }
3925                    } # INSCOPE
3926                      unless (defined $i) {
3927                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3928                        ## Ignore the token
3929                        !!!next-token;
3930                        redo B;
3931                      }
3932                    
3933                    ## As if </tr>
3934                    ## have an element in table scope
3935                    my $i;
3936                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3937                      my $node = $self->{open_elements}->[$_];
3938                      if ($node->[1] eq 'tr') {
3939                        $i = $_;
3940                        last INSCOPE;
3941                      } elsif ({
3942                                table => 1, html => 1,
3943                               }->{$node->[1]}) {
3944                        last INSCOPE;
3945                      }
3946                    } # INSCOPE
3947                      unless (defined $i) {
3948                        !!!parse-error (type => 'unmatched end tag:tr');
3949                        ## Ignore the token
3950                        !!!next-token;
3951                        redo B;
3952                      }
3953                    
3954                    ## Clear back to table row context
3955                    while (not {
3956                      tr => 1, html => 1,
3957                    }->{$self->{open_elements}->[-1]->[1]}) {
3958                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3959                      pop @{$self->{open_elements}};
3960                  }                  }
3961                } # INSCOPE                  
3962                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
3963                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
3964                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
                 !!!next-token;  
                 redo B;  
3965                }                }
3966    
               ## As if </tr>  
3967                ## have an element in table scope                ## have an element in table scope
3968                my $i;                my $i;
3969                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3970                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
3971                  if ($node->[1] eq 'tr') {                  if ($node->[1] eq $token->{tag_name}) {
3972                    $i = $_;                    $i = $_;
3973                    last INSCOPE;                    last INSCOPE;
3974                  } elsif ({                  } elsif ({
# Line 4661  sub _tree_construction_main ($) { Line 3978  sub _tree_construction_main ($) {
3978                  }                  }
3979                } # INSCOPE                } # INSCOPE
3980                unless (defined $i) {                unless (defined $i) {
3981                  !!!parse-error (type => 'unmatched end tag:tr');                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3982                  ## Ignore the token                  ## Ignore the token
3983                  !!!next-token;                  !!!next-token;
3984                  redo B;                  redo B;
3985                }                }
3986    
3987                ## Clear back to table row context                ## Clear back to table body context
3988                while (not {                while (not {
3989                  tr => 1, html => 1,                  tbody => 1, tfoot => 1, thead => 1, html => 1,
3990                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
3991                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3992                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3993                }                }
3994    
3995                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}};
3996                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_IM;
3997                ## reprocess                !!!next-token;
3998                redo B;                redo B;
3999              } elsif ({              } elsif ({
4000                        body => 1, caption => 1, col => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
4001                        colgroup => 1, html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
4002                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
4003                          tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
4004                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4005                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4006                ## Ignore the token                ## Ignore the token
4007                !!!next-token;                !!!next-token;
4008                redo B;                redo B;
4009              } else {          } else {
4010                #            !!!parse-error (type => 'in table:/'.$token->{tag_name});
             }  
           } 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});  
4011    
4012              !!!next-token;            $insert = $insert_to_foster;
4013              redo B;            #
4014            } elsif ($token->{type} eq 'start tag') {          }
4015              if ({        } else {
4016                   caption => 1, col => 1, colgroup => 1,          die "$0: $token->{type}: Unknown token type";
4017                   tbody => 1, td => 1, tfoot => 1, th => 1,        }
4018                   thead => 1, tr => 1,      } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
4019                  }->{$token->{tag_name}}) {            if ($token->{type} == CHARACTER_TOKEN) {
4020                ## have an element in table scope              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4021                my $tn;                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4022                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                unless (length $token->{data}) {
                 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  
4023                  !!!next-token;                  !!!next-token;
4024                  redo B;                  redo B;
4025                }                }
4026                }
4027                ## Close the cell              
4028                !!!back-token; # <?>              #
4029                $token = {type => 'end tag', tag_name => $tn};            } elsif ($token->{type} == START_TAG_TOKEN) {
4030                if ($token->{tag_name} eq 'col') {
4031                  !!!insert-element ($token->{tag_name}, $token->{attributes});
4032                  pop @{$self->{open_elements}};
4033                  !!!next-token;
4034                redo B;                redo B;
4035              } else {              } else {
4036                #                #
4037              }              }
4038            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
4039              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {              if ($token->{tag_name} eq 'colgroup') {
4040                ## have an element in table scope                if ($self->{open_elements}->[-1]->[1] eq 'html') {
4041                my $i;                  !!!parse-error (type => 'unmatched end tag:colgroup');
               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});  
4042                  ## Ignore the token                  ## Ignore the token
4043                  !!!next-token;                  !!!next-token;
4044                  redo B;                  redo B;
4045                  } else {
4046                    pop @{$self->{open_elements}}; # colgroup
4047                    $self->{insertion_mode} = IN_TABLE_IM;
4048                    !!!next-token;
4049                    redo B;            
4050                }                }
4051                              } elsif ($token->{tag_name} eq 'col') {
4052                ## generate implied end tags                !!!parse-error (type => 'unmatched end tag:col');
               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});  
4053                ## Ignore the token                ## Ignore the token
4054                !!!next-token;                !!!next-token;
4055                redo B;                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;  
4056              } else {              } else {
4057                #                #
4058              }              }
4059            } else {            } else {
4060              #              #
4061            }            }
4062              
4063            $in_body->($insert_to_current);            ## As if </colgroup>
4064            redo B;            if ($self->{open_elements}->[-1]->[1] eq 'html') {
4065          } elsif ($self->{insertion_mode} eq 'in select') {              !!!parse-error (type => 'unmatched end tag:colgroup');
4066            if ($token->{type} eq 'character') {              ## Ignore the token
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
4067              !!!next-token;              !!!next-token;
4068              redo B;              redo B;
4069            } elsif ($token->{type} eq 'start tag') {            } else {
4070                pop @{$self->{open_elements}}; # colgroup
4071                $self->{insertion_mode} = IN_TABLE_IM;
4072                ## reprocess
4073                redo B;
4074              }
4075        } elsif ($self->{insertion_mode} == IN_SELECT_IM) {
4076          if ($token->{type} == CHARACTER_TOKEN) {
4077            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4078            !!!next-token;
4079            redo B;
4080          } elsif ($token->{type} == START_TAG_TOKEN) {
4081              if ($token->{tag_name} eq 'option') {              if ($token->{tag_name} eq 'option') {
4082                if ($self->{open_elements}->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
4083                  ## As if </option>                  ## As if </option>
# Line 4896  sub _tree_construction_main ($) { Line 4130  sub _tree_construction_main ($) {
4130    
4131                !!!next-token;                !!!next-token;
4132                redo B;                redo B;
4133              } else {          } else {
4134                #            !!!parse-error (type => 'in select:'.$token->{tag_name});
4135              }            ## Ignore the token
4136            } elsif ($token->{type} eq 'end tag') {            !!!next-token;
4137              redo B;
4138            }
4139          } elsif ($token->{type} == END_TAG_TOKEN) {
4140              if ($token->{tag_name} eq 'optgroup') {              if ($token->{tag_name} eq 'optgroup') {
4141                if ($self->{open_elements}->[-1]->[1] eq 'option' and                if ($self->{open_elements}->[-1]->[1] eq 'option' and
4142                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {
# Line 5001  sub _tree_construction_main ($) { Line 4238  sub _tree_construction_main ($) {
4238    
4239                ## reprocess                ## reprocess
4240                redo B;                redo B;
4241              } else {          } else {
4242                #            !!!parse-error (type => 'in select:/'.$token->{tag_name});
             }  
           } else {  
             #  
           }  
   
           !!!parse-error (type => 'in select:'.$token->{tag_name});  
4243            ## Ignore the token            ## Ignore the token
4244            !!!next-token;            !!!next-token;
4245            redo B;            redo B;
4246          } elsif ($self->{insertion_mode} eq 'after body') {          }
4247            if ($token->{type} eq 'character') {        } else {
4248              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          die "$0: $token->{type}: Unknown token type";
4249                my $data = $1;        }
4250                ## As if in body      } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
4251                $reconstruct_active_formatting_elements->($insert_to_current);        if ($token->{type} == CHARACTER_TOKEN) {
4252            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4253              my $data = $1;
4254              ## As if in body
4255              $reconstruct_active_formatting_elements->($insert_to_current);
4256                                
4257                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4258              
4259              unless (length $token->{data}) {
4260                !!!next-token;
4261                redo B;
4262              }
4263            }
4264            
4265            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4266              !!!parse-error (type => 'after html:#character');
4267    
4268                unless (length $token->{data}) {            ## Reprocess in the "main" phase, "after body" insertion mode...
4269                  !!!next-token;          }
4270                  redo B;          
4271                }          ## "after body" insertion mode
4272              }          !!!parse-error (type => 'after body:#character');
4273                
4274              #          $self->{insertion_mode} = IN_BODY_IM;
4275              !!!parse-error (type => 'after body:#character');          ## reprocess
4276            } elsif ($token->{type} eq 'start tag') {          redo B;
4277              !!!parse-error (type => 'after body:'.$token->{tag_name});        } elsif ($token->{type} == START_TAG_TOKEN) {
4278              #          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4279            } elsif ($token->{type} eq 'end tag') {            !!!parse-error (type => 'after html:'.$token->{tag_name});
4280              if ($token->{tag_name} eq 'html') {            
4281                if (defined $self->{inner_html_node}) {            ## Reprocess in the "main" phase, "after body" insertion mode...
4282                  !!!parse-error (type => 'unmatched end tag:html');          }
4283                  ## Ignore the token  
4284                  !!!next-token;          ## "after body" insertion mode
4285                  redo B;          !!!parse-error (type => 'after body:'.$token->{tag_name});
4286                } else {  
4287                  $previous_insertion_mode = $self->{insertion_mode};          $self->{insertion_mode} = IN_BODY_IM;
4288                  $self->{insertion_mode} = 'trailing end';          ## reprocess
4289                  !!!next-token;          redo B;
4290                  redo B;        } elsif ($token->{type} == END_TAG_TOKEN) {
4291                }          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4292              } else {            !!!parse-error (type => 'after html:/'.$token->{tag_name});
4293                !!!parse-error (type => 'after body:/'.$token->{tag_name});            
4294              }            $self->{insertion_mode} = AFTER_BODY_IM;
4295              ## Reprocess in the "main" phase, "after body" insertion mode...
4296            }
4297    
4298            ## "after body" insertion mode
4299            if ($token->{tag_name} eq 'html') {
4300              if (defined $self->{inner_html_node}) {
4301                !!!parse-error (type => 'unmatched end tag:html');
4302                ## Ignore the token
4303                !!!next-token;
4304                redo B;
4305            } else {            } else {
4306              die "$0: $token->{type}: Unknown token type";              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
4307                !!!next-token;
4308                redo B;
4309            }            }
4310            } else {
4311              !!!parse-error (type => 'after body:/'.$token->{tag_name});
4312    
4313            $self->{insertion_mode} = 'in body';            $self->{insertion_mode} = IN_BODY_IM;
4314            ## reprocess            ## reprocess
4315            redo B;            redo B;
4316      } elsif ($self->{insertion_mode} eq 'in frameset') {          }
4317        if ($token->{type} eq 'character') {        } else {
4318            die "$0: $token->{type}: Unknown token type";
4319          }
4320        } elsif ($self->{insertion_mode} & FRAME_IMS) {
4321          if ($token->{type} == CHARACTER_TOKEN) {
4322          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4323            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4324              
4325            unless (length $token->{data}) {            unless (length $token->{data}) {
4326              !!!next-token;              !!!next-token;
4327              redo B;              redo B;
4328            }            }
4329          }          }
4330            
4331            if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
4332              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4333                !!!parse-error (type => 'in frameset:#character');
4334              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
4335                !!!parse-error (type => 'after frameset:#character');
4336              } else { # "after html frameset"
4337                !!!parse-error (type => 'after html:#character');
4338    
4339                $self->{insertion_mode} = AFTER_FRAMESET_IM;
4340                ## Reprocess in the "main" phase, "after frameset"...
4341                !!!parse-error (type => 'after frameset:#character');
4342              }
4343              
4344              ## Ignore the token.
4345              if (length $token->{data}) {
4346                ## reprocess the rest of characters
4347              } else {
4348                !!!next-token;
4349              }
4350              redo B;
4351            }
4352            
4353            die qq[$0: Character "$token->{data}"];
4354          } elsif ($token->{type} == START_TAG_TOKEN) {
4355            if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4356              !!!parse-error (type => 'after html:'.$token->{tag_name});
4357    
4358          !!!parse-error (type => 'in frameset:#character');            $self->{insertion_mode} = AFTER_FRAMESET_IM;
4359          ## Ignore the token            ## Process in the "main" phase, "after frameset" insertion mode...
4360          !!!next-token;          }
4361          redo B;  
4362        } elsif ($token->{type} eq 'start tag') {          if ($token->{tag_name} eq 'frameset' and
4363          if ($token->{tag_name} eq 'frameset') {              $self->{insertion_mode} == IN_FRAMESET_IM) {
4364            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes});
4365            !!!next-token;            !!!next-token;
4366            redo B;            redo B;
4367          } elsif ($token->{tag_name} eq 'frame') {          } elsif ($token->{tag_name} eq 'frame' and
4368                     $self->{insertion_mode} == IN_FRAMESET_IM) {
4369            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes});
4370            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4371            !!!next-token;            !!!next-token;
4372            redo B;            redo B;
4373          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
4374            $in_body->($insert_to_current);            ## NOTE: As if in body.
4375              $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
4376            redo B;            redo B;
4377          } else {          } else {
4378            !!!parse-error (type => 'in frameset:'.$token->{tag_name});            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4379                !!!parse-error (type => 'in frameset:'.$token->{tag_name});
4380              } else {
4381                !!!parse-error (type => 'after frameset:'.$token->{tag_name});
4382              }
4383            ## Ignore the token            ## Ignore the token
4384            !!!next-token;            !!!next-token;
4385            redo B;            redo B;
4386          }          }
4387        } elsif ($token->{type} eq 'end tag') {        } elsif ($token->{type} == END_TAG_TOKEN) {
4388          if ($token->{tag_name} eq 'frameset') {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4389              !!!parse-error (type => 'after html:/'.$token->{tag_name});
4390    
4391              $self->{insertion_mode} = AFTER_FRAMESET_IM;
4392              ## Process in the "main" phase, "after frameset" insertion mode...
4393            }
4394    
4395            if ($token->{tag_name} eq 'frameset' and
4396                $self->{insertion_mode} == IN_FRAMESET_IM) {
4397            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] eq 'html' and
4398                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
4399              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
# Line 5103  sub _tree_construction_main ($) { Line 4406  sub _tree_construction_main ($) {
4406    
4407            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
4408                $self->{open_elements}->[-1]->[1] ne 'frameset') {                $self->{open_elements}->[-1]->[1] ne 'frameset') {
4409              $self->{insertion_mode} = 'after frameset';              $self->{insertion_mode} = AFTER_FRAMESET_IM;
4410            }            }
4411            redo B;            redo B;
4412            } elsif ($token->{tag_name} eq 'html' and
4413                     $self->{insertion_mode} == AFTER_FRAMESET_IM) {
4414              $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
4415              !!!next-token;
4416              redo B;
4417          } else {          } else {
4418            !!!parse-error (type => 'in frameset:/'.$token->{tag_name});            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4419                !!!parse-error (type => 'in frameset:/'.$token->{tag_name});
4420              } else {
4421                !!!parse-error (type => 'after frameset:/'.$token->{tag_name});
4422              }
4423            ## Ignore the token            ## Ignore the token
4424            !!!next-token;            !!!next-token;
4425            redo B;            redo B;
# Line 5115  sub _tree_construction_main ($) { Line 4427  sub _tree_construction_main ($) {
4427        } else {        } else {
4428          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
4429        }        }
     } elsif ($self->{insertion_mode} eq 'after frameset') {  
       if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
4430    
4431                unless (length $token->{data}) {        ## ISSUE: An issue in spec here
4432                  !!!next-token;      } else {
4433                  redo B;        die "$0: $self->{insertion_mode}: Unknown insertion mode";
4434                }      }
             }  
4435    
4436              if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {      ## "in body" insertion mode
4437                !!!parse-error (type => 'after frameset:#character');      if ($token->{type} == START_TAG_TOKEN) {
4438          if ($token->{tag_name} eq 'script') {
4439            ## NOTE: This is an "as if in head" code clone
4440            $script_start_tag->($insert);
4441            redo B;
4442          } elsif ($token->{tag_name} eq 'style') {
4443            ## NOTE: This is an "as if in head" code clone
4444            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4445            redo B;
4446          } elsif ({
4447                    base => 1, link => 1,
4448                   }->{$token->{tag_name}}) {
4449            ## NOTE: This is an "as if in head" code clone, only "-t" differs
4450            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4451            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4452            !!!next-token;
4453            redo B;
4454          } elsif ($token->{tag_name} eq 'meta') {
4455            ## NOTE: This is an "as if in head" code clone, only "-t" differs
4456            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4457            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4458    
4459                ## Ignore the token.          unless ($self->{confident}) {
4460                if (length $token->{data}) {            if ($token->{attributes}->{charset}) { ## TODO: And if supported
4461                  ## reprocess the rest of characters              $self->{change_encoding}
4462                } else {                  ->($self, $token->{attributes}->{charset}->{value});
4463                  !!!next-token;            } elsif ($token->{attributes}->{content}) {
4464                }              ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
4465                redo B;              if ($token->{attributes}->{content}->{value}
4466                    =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=
4467                        [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4468                        ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
4469                  $self->{change_encoding}
4470                      ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);
4471              }              }
4472              }
4473            }
4474    
4475          die qq[$0: Character "$token->{data}"];          !!!next-token;
4476        } elsif ($token->{type} eq 'start tag') {          redo B;
4477          if ($token->{tag_name} eq 'noframes') {        } elsif ($token->{tag_name} eq 'title') {
4478            $in_body->($insert_to_current);          !!!parse-error (type => 'in body:title');
4479            redo B;          ## NOTE: This is an "as if in head" code clone
4480          } else {          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {
4481            !!!parse-error (type => 'after frameset:'.$token->{tag_name});            if (defined $self->{head_element}) {
4482                $self->{head_element}->append_child ($_[0]);
4483              } else {
4484                $insert->($_[0]);
4485              }
4486            });
4487            redo B;
4488          } elsif ($token->{tag_name} eq 'body') {
4489            !!!parse-error (type => 'in body:body');
4490                  
4491            if (@{$self->{open_elements}} == 1 or
4492                $self->{open_elements}->[1]->[1] ne 'body') {
4493            ## Ignore the token            ## Ignore the token
4494            } else {
4495              my $body_el = $self->{open_elements}->[1]->[0];
4496              for my $attr_name (keys %{$token->{attributes}}) {
4497                unless ($body_el->has_attribute_ns (undef, $attr_name)) {
4498                  $body_el->set_attribute_ns
4499                    (undef, [undef, $attr_name],
4500                     $token->{attributes}->{$attr_name}->{value});
4501                }
4502              }
4503            }
4504            !!!next-token;
4505            redo B;
4506          } elsif ({
4507                    address => 1, blockquote => 1, center => 1, dir => 1,
4508                    div => 1, dl => 1, fieldset => 1, listing => 1,
4509                    menu => 1, ol => 1, p => 1, ul => 1,
4510                    pre => 1,
4511                   }->{$token->{tag_name}}) {
4512            ## has a p element in scope
4513            INSCOPE: for (reverse @{$self->{open_elements}}) {
4514              if ($_->[1] eq 'p') {
4515                !!!back-token;
4516                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4517                redo B;
4518              } elsif ({
4519                        table => 1, caption => 1, td => 1, th => 1,
4520                        button => 1, marquee => 1, object => 1, html => 1,
4521                       }->{$_->[1]}) {
4522                last INSCOPE;
4523              }
4524            } # INSCOPE
4525              
4526            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4527            if ($token->{tag_name} eq 'pre') {
4528              !!!next-token;
4529              if ($token->{type} == CHARACTER_TOKEN) {
4530                $token->{data} =~ s/^\x0A//;
4531                unless (length $token->{data}) {
4532                  !!!next-token;
4533                }
4534              }
4535            } else {
4536            !!!next-token;            !!!next-token;
           redo B;  
4537          }          }
4538        } elsif ($token->{type} eq 'end tag') {          redo B;
4539          if ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'form') {
4540            $previous_insertion_mode = $self->{insertion_mode};          if (defined $self->{form_element}) {
4541            $self->{insertion_mode} = 'trailing end';            !!!parse-error (type => 'in form:form');
4542              ## Ignore the token
4543            !!!next-token;            !!!next-token;
4544            redo B;            redo B;
4545          } else {          } else {
4546            !!!parse-error (type => 'after frameset:/'.$token->{tag_name});            ## has a p element in scope
4547            ## Ignore the token            INSCOPE: for (reverse @{$self->{open_elements}}) {
4548                if ($_->[1] eq 'p') {
4549                  !!!back-token;
4550                  $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4551                  redo B;
4552                } elsif ({
4553                          table => 1, caption => 1, td => 1, th => 1,
4554                          button => 1, marquee => 1, object => 1, html => 1,
4555                         }->{$_->[1]}) {
4556                  last INSCOPE;
4557                }
4558              } # INSCOPE
4559                
4560              !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4561              $self->{form_element} = $self->{open_elements}->[-1]->[0];
4562            !!!next-token;            !!!next-token;
4563            redo B;            redo B;
4564          }          }
4565        } else {        } elsif ($token->{tag_name} eq 'li') {
4566          die "$0: $token->{type}: Unknown token type";          ## has a p element in scope
4567        }          INSCOPE: for (reverse @{$self->{open_elements}}) {
4568              if ($_->[1] eq 'p') {
4569                !!!back-token;
4570                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4571                redo B;
4572              } elsif ({
4573                        table => 1, caption => 1, td => 1, th => 1,
4574                        button => 1, marquee => 1, object => 1, html => 1,
4575                       }->{$_->[1]}) {
4576                last INSCOPE;
4577              }
4578            } # INSCOPE
4579              
4580            ## Step 1
4581            my $i = -1;
4582            my $node = $self->{open_elements}->[$i];
4583            LI: {
4584              ## Step 2
4585              if ($node->[1] eq 'li') {
4586                if ($i != -1) {
4587                  !!!parse-error (type => 'end tag missing:'.
4588                                  $self->{open_elements}->[-1]->[1]);
4589                }
4590                splice @{$self->{open_elements}}, $i;
4591                last LI;
4592              }
4593              
4594              ## Step 3
4595              if (not $formatting_category->{$node->[1]} and
4596                  #not $phrasing_category->{$node->[1]} and
4597                  ($special_category->{$node->[1]} or
4598                   $scoping_category->{$node->[1]}) and
4599                  $node->[1] ne 'address' and $node->[1] ne 'div') {
4600                last LI;
4601              }
4602              
4603              ## Step 4
4604              $i--;
4605              $node = $self->{open_elements}->[$i];
4606              redo LI;
4607            } # LI
4608              
4609            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4610            !!!next-token;
4611            redo B;
4612          } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {
4613            ## has a p element in scope
4614            INSCOPE: for (reverse @{$self->{open_elements}}) {
4615              if ($_->[1] eq 'p') {
4616                !!!back-token;
4617                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4618                redo B;
4619              } elsif ({
4620                        table => 1, caption => 1, td => 1, th => 1,
4621                        button => 1, marquee => 1, object => 1, html => 1,
4622                       }->{$_->[1]}) {
4623                last INSCOPE;
4624              }
4625            } # INSCOPE
4626              
4627            ## Step 1
4628            my $i = -1;
4629            my $node = $self->{open_elements}->[$i];
4630            LI: {
4631              ## Step 2
4632              if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {
4633                if ($i != -1) {
4634                  !!!parse-error (type => 'end tag missing:'.
4635                                  $self->{open_elements}->[-1]->[1]);
4636                }
4637                splice @{$self->{open_elements}}, $i;
4638                last LI;
4639              }
4640              
4641              ## Step 3
4642              if (not $formatting_category->{$node->[1]} and
4643                  #not $phrasing_category->{$node->[1]} and
4644                  ($special_category->{$node->[1]} or
4645                   $scoping_category->{$node->[1]}) and
4646                  $node->[1] ne 'address' and $node->[1] ne 'div') {
4647                last LI;
4648              }
4649              
4650              ## Step 4
4651              $i--;
4652              $node = $self->{open_elements}->[$i];
4653              redo LI;
4654            } # LI
4655              
4656            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4657            !!!next-token;
4658            redo B;
4659          } elsif ($token->{tag_name} eq 'plaintext') {
4660            ## has a p element in scope
4661            INSCOPE: for (reverse @{$self->{open_elements}}) {
4662              if ($_->[1] eq 'p') {
4663                !!!back-token;
4664                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4665                redo B;
4666              } elsif ({
4667                        table => 1, caption => 1, td => 1, th => 1,
4668                        button => 1, marquee => 1, object => 1, html => 1,
4669                       }->{$_->[1]}) {
4670                last INSCOPE;
4671              }
4672            } # INSCOPE
4673              
4674            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4675              
4676            $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
4677              
4678            !!!next-token;
4679            redo B;
4680          } elsif ({
4681                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
4682                   }->{$token->{tag_name}}) {
4683            ## has a p element in scope
4684            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4685              my $node = $self->{open_elements}->[$_];
4686              if ($node->[1] eq 'p') {
4687                !!!back-token;
4688                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4689                redo B;
4690              } elsif ({
4691                        table => 1, caption => 1, td => 1, th => 1,
4692                        button => 1, marquee => 1, object => 1, html => 1,
4693                       }->{$node->[1]}) {
4694                last INSCOPE;
4695              }
4696            } # INSCOPE
4697              
4698            ## NOTE: See <http://html5.org/tools/web-apps-tracker?from=925&to=926>
4699            ## has an element in scope
4700            #my $i;
4701            #INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4702            #  my $node = $self->{open_elements}->[$_];
4703            #  if ({
4704            #       h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
4705            #      }->{$node->[1]}) {
4706            #    $i = $_;
4707            #    last INSCOPE;
4708            #  } elsif ({
4709            #            table => 1, caption => 1, td => 1, th => 1,
4710            #            button => 1, marquee => 1, object => 1, html => 1,
4711            #           }->{$node->[1]}) {
4712            #    last INSCOPE;
4713            #  }
4714            #} # INSCOPE
4715            #  
4716            #if (defined $i) {
4717            #  !!! parse-error (type => 'in hn:hn');
4718            #  splice @{$self->{open_elements}}, $i;
4719            #}
4720              
4721            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4722              
4723            !!!next-token;
4724            redo B;
4725          } elsif ($token->{tag_name} eq 'a') {
4726            AFE: for my $i (reverse 0..$#$active_formatting_elements) {
4727              my $node = $active_formatting_elements->[$i];
4728              if ($node->[1] eq 'a') {
4729                !!!parse-error (type => 'in a:a');
4730                
4731                !!!back-token;
4732                $token = {type => END_TAG_TOKEN, tag_name => 'a'};
4733                $formatting_end_tag->($token->{tag_name});
4734                
4735                AFE2: for (reverse 0..$#$active_formatting_elements) {
4736                  if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
4737                    splice @$active_formatting_elements, $_, 1;
4738                    last AFE2;
4739                  }
4740                } # AFE2
4741                OE: for (reverse 0..$#{$self->{open_elements}}) {
4742                  if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
4743                    splice @{$self->{open_elements}}, $_, 1;
4744                    last OE;
4745                  }
4746                } # OE
4747                last AFE;
4748              } elsif ($node->[0] eq '#marker') {
4749                last AFE;
4750              }
4751            } # AFE
4752              
4753            $reconstruct_active_formatting_elements->($insert_to_current);
4754    
4755        ## ISSUE: An issue in spec here          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4756      } elsif ($self->{insertion_mode} eq 'trailing end') {          push @$active_formatting_elements, $self->{open_elements}->[-1];
4757        ## states in the main stage is preserved yet # MUST  
4758                  !!!next-token;
4759        if ($token->{type} eq 'character') {          redo B;
4760          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {        } elsif ({
4761            my $data = $1;                  b => 1, big => 1, em => 1, font => 1, i => 1,
4762            ## As if in the main phase.                  s => 1, small => 1, strile => 1,
4763            ## NOTE: The insertion mode in the main phase                  strong => 1, tt => 1, u => 1,
4764            ## just before the phase has been changed to the trailing                 }->{$token->{tag_name}}) {
4765            ## end phase is either "after body" or "after frameset".          $reconstruct_active_formatting_elements->($insert_to_current);
4766            $reconstruct_active_formatting_elements->($insert_to_current);          
4767            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4768            push @$active_formatting_elements, $self->{open_elements}->[-1];
4769            
4770            !!!next-token;
4771            redo B;
4772          } elsif ($token->{tag_name} eq 'nobr') {
4773            $reconstruct_active_formatting_elements->($insert_to_current);
4774    
4775            ## has a |nobr| element in scope
4776            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4777              my $node = $self->{open_elements}->[$_];
4778              if ($node->[1] eq 'nobr') {
4779                !!!parse-error (type => 'in nobr:nobr');
4780                !!!back-token;
4781                $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};
4782                redo B;
4783              } elsif ({
4784                        table => 1, caption => 1, td => 1, th => 1,
4785                        button => 1, marquee => 1, object => 1, html => 1,
4786                       }->{$node->[1]}) {
4787                last INSCOPE;
4788              }
4789            } # INSCOPE
4790            
4791            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4792            push @$active_formatting_elements, $self->{open_elements}->[-1];
4793            
4794            !!!next-token;
4795            redo B;
4796          } elsif ($token->{tag_name} eq 'button') {
4797            ## has a button element in scope
4798            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4799              my $node = $self->{open_elements}->[$_];
4800              if ($node->[1] eq 'button') {
4801                !!!parse-error (type => 'in button:button');
4802                !!!back-token;
4803                $token = {type => END_TAG_TOKEN, tag_name => 'button'};
4804                redo B;
4805              } elsif ({
4806                        table => 1, caption => 1, td => 1, th => 1,
4807                        button => 1, marquee => 1, object => 1, html => 1,
4808                       }->{$node->[1]}) {
4809                last INSCOPE;
4810              }
4811            } # INSCOPE
4812              
4813            $reconstruct_active_formatting_elements->($insert_to_current);
4814              
4815            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4816            push @$active_formatting_elements, ['#marker', ''];
4817    
4818            !!!next-token;
4819            redo B;
4820          } elsif ($token->{tag_name} eq 'marquee' or
4821                   $token->{tag_name} eq 'object') {
4822            $reconstruct_active_formatting_elements->($insert_to_current);
4823            
4824            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4825            push @$active_formatting_elements, ['#marker', ''];
4826            
4827            !!!next-token;
4828            redo B;
4829          } elsif ($token->{tag_name} eq 'xmp') {
4830            $reconstruct_active_formatting_elements->($insert_to_current);
4831            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4832            redo B;
4833          } elsif ($token->{tag_name} eq 'table') {
4834            ## has a p element in scope
4835            INSCOPE: for (reverse @{$self->{open_elements}}) {
4836              if ($_->[1] eq 'p') {
4837                !!!back-token;
4838                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4839                redo B;
4840              } elsif ({
4841                        table => 1, caption => 1, td => 1, th => 1,
4842                        button => 1, marquee => 1, object => 1, html => 1,
4843                       }->{$_->[1]}) {
4844                last INSCOPE;
4845              }
4846            } # INSCOPE
4847              
4848            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4849              
4850            $self->{insertion_mode} = IN_TABLE_IM;
4851              
4852            !!!next-token;
4853            redo B;
4854          } elsif ({
4855                    area => 1, basefont => 1, bgsound => 1, br => 1,
4856                    embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
4857                    image => 1,
4858                   }->{$token->{tag_name}}) {
4859            if ($token->{tag_name} eq 'image') {
4860              !!!parse-error (type => 'image');
4861              $token->{tag_name} = 'img';
4862            }
4863    
4864            ## NOTE: There is an "as if <br>" code clone.
4865            $reconstruct_active_formatting_elements->($insert_to_current);
4866            
4867            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4868            pop @{$self->{open_elements}};
4869            
4870            !!!next-token;
4871            redo B;
4872          } elsif ($token->{tag_name} eq 'hr') {
4873            ## has a p element in scope
4874            INSCOPE: for (reverse @{$self->{open_elements}}) {
4875              if ($_->[1] eq 'p') {
4876                !!!back-token;
4877                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4878                redo B;
4879              } elsif ({
4880                        table => 1, caption => 1, td => 1, th => 1,
4881                        button => 1, marquee => 1, object => 1, html => 1,
4882                       }->{$_->[1]}) {
4883                last INSCOPE;
4884              }
4885            } # INSCOPE
4886                        
4887            $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4888            pop @{$self->{open_elements}};
4889                        
4890            !!!next-token;
4891            redo B;
4892          } elsif ($token->{tag_name} eq 'input') {
4893            $reconstruct_active_formatting_elements->($insert_to_current);
4894            
4895            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4896            ## TODO: associate with $self->{form_element} if defined
4897            pop @{$self->{open_elements}};
4898            
4899            !!!next-token;
4900            redo B;
4901          } elsif ($token->{tag_name} eq 'isindex') {
4902            !!!parse-error (type => 'isindex');
4903            
4904            if (defined $self->{form_element}) {
4905              ## Ignore the token
4906              !!!next-token;
4907              redo B;
4908            } else {
4909              my $at = $token->{attributes};
4910              my $form_attrs;
4911              $form_attrs->{action} = $at->{action} if $at->{action};
4912              my $prompt_attr = $at->{prompt};
4913              $at->{name} = {name => 'name', value => 'isindex'};
4914              delete $at->{action};
4915              delete $at->{prompt};
4916              my @tokens = (
4917                            {type => START_TAG_TOKEN, tag_name => 'form',
4918                             attributes => $form_attrs},
4919                            {type => START_TAG_TOKEN, tag_name => 'hr'},
4920                            {type => START_TAG_TOKEN, tag_name => 'p'},
4921                            {type => START_TAG_TOKEN, tag_name => 'label'},
4922                           );
4923              if ($prompt_attr) {
4924                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};
4925              } else {
4926                push @tokens, {type => CHARACTER_TOKEN,
4927                               data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD
4928                ## TODO: make this configurable
4929              }
4930              push @tokens,
4931                            {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},
4932                            #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
4933                            {type => END_TAG_TOKEN, tag_name => 'label'},
4934                            {type => END_TAG_TOKEN, tag_name => 'p'},
4935                            {type => START_TAG_TOKEN, tag_name => 'hr'},
4936                            {type => END_TAG_TOKEN, tag_name => 'form'};
4937              $token = shift @tokens;
4938              !!!back-token (@tokens);
4939              redo B;
4940            }
4941          } elsif ($token->{tag_name} eq 'textarea') {
4942            my $tag_name = $token->{tag_name};
4943            my $el;
4944            !!!create-element ($el, $token->{tag_name}, $token->{attributes});
4945            
4946            ## TODO: $self->{form_element} if defined
4947            $self->{content_model} = RCDATA_CONTENT_MODEL;
4948            delete $self->{escape}; # MUST
4949            
4950            $insert->($el);
4951            
4952            my $text = '';
4953            !!!next-token;
4954            if ($token->{type} == CHARACTER_TOKEN) {
4955              $token->{data} =~ s/^\x0A//;
4956            unless (length $token->{data}) {            unless (length $token->{data}) {
4957              !!!next-token;              !!!next-token;
             redo B;  
4958            }            }
4959          }          }
4960            while ($token->{type} == CHARACTER_TOKEN) {
4961              $text .= $token->{data};
4962              !!!next-token;
4963            }
4964            if (length $text) {
4965              $el->manakai_append_text ($text);
4966            }
4967            
4968            $self->{content_model} = PCDATA_CONTENT_MODEL;
4969            
4970            if ($token->{type} == END_TAG_TOKEN and
4971                $token->{tag_name} eq $tag_name) {
4972              ## Ignore the token
4973            } else {
4974              !!!parse-error (type => 'in RCDATA:#'.$token->{type});
4975            }
4976            !!!next-token;
4977            redo B;
4978          } elsif ({
4979                    iframe => 1,
4980                    noembed => 1,
4981                    noframes => 1,
4982                    noscript => 0, ## TODO: 1 if scripting is enabled
4983                   }->{$token->{tag_name}}) {
4984            ## NOTE: There is an "as if in body" code clone.
4985            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4986            redo B;
4987          } elsif ($token->{tag_name} eq 'select') {
4988            $reconstruct_active_formatting_elements->($insert_to_current);
4989            
4990            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4991            
4992            $self->{insertion_mode} = IN_SELECT_IM;
4993            !!!next-token;
4994            redo B;
4995          } elsif ({
4996                    caption => 1, col => 1, colgroup => 1, frame => 1,
4997                    frameset => 1, head => 1, option => 1, optgroup => 1,
4998                    tbody => 1, td => 1, tfoot => 1, th => 1,
4999                    thead => 1, tr => 1,
5000                   }->{$token->{tag_name}}) {
5001            !!!parse-error (type => 'in body:'.$token->{tag_name});
5002            ## Ignore the token
5003            !!!next-token;
5004            redo B;
5005            
5006            ## ISSUE: An issue on HTML5 new elements in the spec.
5007          } else {
5008            $reconstruct_active_formatting_elements->($insert_to_current);
5009            
5010            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5011            
5012            !!!next-token;
5013            redo B;
5014          }
5015        } elsif ($token->{type} == END_TAG_TOKEN) {
5016          if ($token->{tag_name} eq 'body') {
5017            if (@{$self->{open_elements}} > 1 and
5018                $self->{open_elements}->[1]->[1] eq 'body') {
5019              for (@{$self->{open_elements}}) {
5020                unless ({
5021                           dd => 1, dt => 1, li => 1, p => 1, td => 1,
5022                           th => 1, tr => 1, body => 1, html => 1,
5023                         tbody => 1, tfoot => 1, thead => 1,
5024                        }->{$_->[1]}) {
5025                  !!!parse-error (type => 'not closed:'.$_->[1]);
5026                }
5027              }
5028    
5029          !!!parse-error (type => 'after html:#character');            $self->{insertion_mode} = AFTER_BODY_IM;
5030          $self->{insertion_mode} = $previous_insertion_mode;            !!!next-token;
5031          ## reprocess            redo B;
5032            } else {
5033              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5034              ## Ignore the token
5035              !!!next-token;
5036              redo B;
5037            }
5038          } elsif ($token->{tag_name} eq 'html') {
5039            if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {
5040              ## ISSUE: There is an issue in the spec.
5041              if ($self->{open_elements}->[-1]->[1] ne 'body') {
5042                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);
5043              }
5044              $self->{insertion_mode} = AFTER_BODY_IM;
5045              ## reprocess
5046              redo B;
5047            } else {
5048              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5049              ## Ignore the token
5050              !!!next-token;
5051              redo B;
5052            }
5053          } elsif ({
5054                    address => 1, blockquote => 1, center => 1, dir => 1,
5055                    div => 1, dl => 1, fieldset => 1, listing => 1,
5056                    menu => 1, ol => 1, pre => 1, ul => 1,
5057                    p => 1,
5058                    dd => 1, dt => 1, li => 1,
5059                    button => 1, marquee => 1, object => 1,
5060                   }->{$token->{tag_name}}) {
5061            ## has an element in scope
5062            my $i;
5063            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5064              my $node = $self->{open_elements}->[$_];
5065              if ($node->[1] eq $token->{tag_name}) {
5066                ## generate implied end tags
5067                if ({
5068                     dd => ($token->{tag_name} ne 'dd'),
5069                     dt => ($token->{tag_name} ne 'dt'),
5070                     li => ($token->{tag_name} ne 'li'),
5071                     p => ($token->{tag_name} ne 'p'),
5072                     td => 1, th => 1, tr => 1,
5073                     tbody => 1, tfoot=> 1, thead => 1,
5074                    }->{$self->{open_elements}->[-1]->[1]}) {
5075                  !!!back-token;
5076                  $token = {type => END_TAG_TOKEN,
5077                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5078                  redo B;
5079                }
5080                $i = $_;
5081                last INSCOPE unless $token->{tag_name} eq 'p';
5082              } elsif ({
5083                        table => 1, caption => 1, td => 1, th => 1,
5084                        button => 1, marquee => 1, object => 1, html => 1,
5085                       }->{$node->[1]}) {
5086                last INSCOPE;
5087              }
5088            } # INSCOPE
5089            
5090            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
5091              if (defined $i) {
5092                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
5093              } else {
5094                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5095              }
5096            }
5097            
5098            if (defined $i) {
5099              splice @{$self->{open_elements}}, $i;
5100            } elsif ($token->{tag_name} eq 'p') {
5101              ## As if <p>, then reprocess the current token
5102              my $el;
5103              !!!create-element ($el, 'p');
5104              $insert->($el);
5105            }
5106            $clear_up_to_marker->()
5107              if {
5108                button => 1, marquee => 1, object => 1,
5109              }->{$token->{tag_name}};
5110            !!!next-token;
5111          redo B;          redo B;
5112        } elsif ($token->{type} eq 'start tag') {        } elsif ($token->{tag_name} eq 'form') {
5113          !!!parse-error (type => 'after html:'.$token->{tag_name});          ## has an element in scope
5114          $self->{insertion_mode} = $previous_insertion_mode;          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5115          ## reprocess            my $node = $self->{open_elements}->[$_];
5116              if ($node->[1] eq $token->{tag_name}) {
5117                ## generate implied end tags
5118                if ({
5119                     dd => 1, dt => 1, li => 1, p => 1,
5120                     td => 1, th => 1, tr => 1,
5121                     tbody => 1, tfoot=> 1, thead => 1,
5122                    }->{$self->{open_elements}->[-1]->[1]}) {
5123                  !!!back-token;
5124                  $token = {type => END_TAG_TOKEN,
5125                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5126                  redo B;
5127                }
5128                last INSCOPE;
5129              } elsif ({
5130                        table => 1, caption => 1, td => 1, th => 1,
5131                        button => 1, marquee => 1, object => 1, html => 1,
5132                       }->{$node->[1]}) {
5133                last INSCOPE;
5134              }
5135            } # INSCOPE
5136            
5137            if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {
5138              pop @{$self->{open_elements}};
5139            } else {
5140              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5141            }
5142    
5143            undef $self->{form_element};
5144            !!!next-token;
5145          redo B;          redo B;
5146        } elsif ($token->{type} eq 'end tag') {        } elsif ({
5147          !!!parse-error (type => 'after html:/'.$token->{tag_name});                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5148          $self->{insertion_mode} = $previous_insertion_mode;                 }->{$token->{tag_name}}) {
5149          ## reprocess          ## has an element in scope
5150            my $i;
5151            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5152              my $node = $self->{open_elements}->[$_];
5153              if ({
5154                   h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5155                  }->{$node->[1]}) {
5156                ## generate implied end tags
5157                if ({
5158                     dd => 1, dt => 1, li => 1, p => 1,
5159                     td => 1, th => 1, tr => 1,
5160                     tbody => 1, tfoot=> 1, thead => 1,
5161                    }->{$self->{open_elements}->[-1]->[1]}) {
5162                  !!!back-token;
5163                  $token = {type => END_TAG_TOKEN,
5164                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5165                  redo B;
5166                }
5167                $i = $_;
5168                last INSCOPE;
5169              } elsif ({
5170                        table => 1, caption => 1, td => 1, th => 1,
5171                        button => 1, marquee => 1, object => 1, html => 1,
5172                       }->{$node->[1]}) {
5173                last INSCOPE;
5174              }
5175            } # INSCOPE
5176            
5177            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
5178              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5179            }
5180            
5181            splice @{$self->{open_elements}}, $i if defined $i;
5182            !!!next-token;
5183            redo B;
5184          } elsif ({
5185                    a => 1,
5186                    b => 1, big => 1, em => 1, font => 1, i => 1,
5187                    nobr => 1, s => 1, small => 1, strile => 1,
5188                    strong => 1, tt => 1, u => 1,
5189                   }->{$token->{tag_name}}) {
5190            $formatting_end_tag->($token->{tag_name});
5191            redo B;
5192          } elsif ($token->{tag_name} eq 'br') {
5193            !!!parse-error (type => 'unmatched end tag:br');
5194    
5195            ## As if <br>
5196            $reconstruct_active_formatting_elements->($insert_to_current);
5197            
5198            my $el;
5199            !!!create-element ($el, 'br');
5200            $insert->($el);
5201            
5202            ## Ignore the token.
5203            !!!next-token;
5204          redo B;          redo B;
5205          } elsif ({
5206                    caption => 1, col => 1, colgroup => 1, frame => 1,
5207                    frameset => 1, head => 1, option => 1, optgroup => 1,
5208                    tbody => 1, td => 1, tfoot => 1, th => 1,
5209                    thead => 1, tr => 1,
5210                    area => 1, basefont => 1, bgsound => 1,
5211                    embed => 1, hr => 1, iframe => 1, image => 1,
5212                    img => 1, input => 1, isindex => 1, noembed => 1,
5213                    noframes => 1, param => 1, select => 1, spacer => 1,
5214                    table => 1, textarea => 1, wbr => 1,
5215                    noscript => 0, ## TODO: if scripting is enabled
5216                   }->{$token->{tag_name}}) {
5217            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5218            ## Ignore the token
5219            !!!next-token;
5220            redo B;
5221            
5222            ## ISSUE: Issue on HTML5 new elements in spec
5223            
5224        } else {        } else {
5225          die "$0: $token->{type}: Unknown token";          ## Step 1
5226            my $node_i = -1;
5227            my $node = $self->{open_elements}->[$node_i];
5228    
5229            ## Step 2
5230            S2: {
5231              if ($node->[1] eq $token->{tag_name}) {
5232                ## Step 1
5233                ## generate implied end tags
5234                if ({
5235                     dd => 1, dt => 1, li => 1, p => 1,
5236                     td => 1, th => 1, tr => 1,
5237                     tbody => 1, tfoot => 1, thead => 1,
5238                    }->{$self->{open_elements}->[-1]->[1]}) {
5239                  !!!back-token;
5240                  $token = {type => END_TAG_TOKEN,
5241                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5242                  redo B;
5243                }
5244            
5245                ## Step 2
5246                if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {
5247                  ## NOTE: <x><y></x>
5248                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
5249                }
5250                
5251                ## Step 3
5252                splice @{$self->{open_elements}}, $node_i;
5253    
5254                !!!next-token;
5255                last S2;
5256              } else {
5257                ## Step 3
5258                if (not $formatting_category->{$node->[1]} and
5259                    #not $phrasing_category->{$node->[1]} and
5260                    ($special_category->{$node->[1]} or
5261                     $scoping_category->{$node->[1]})) {
5262                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5263                  ## Ignore the token
5264                  !!!next-token;
5265                  last S2;
5266                }
5267              }
5268              
5269              ## Step 4
5270              $node_i--;
5271              $node = $self->{open_elements}->[$node_i];
5272              
5273              ## Step 5;
5274              redo S2;
5275            } # S2
5276            redo B;
5277        }        }
     } else {  
       die "$0: $self->{insertion_mode}: Unknown insertion mode";  
5278      }      }
5279        redo B;
5280    } # B    } # B
5281    
5282      ## NOTE: The "trailing end" phase in HTML5 is split into
5283      ## two insertion modes: "after html body" and "after html frameset".
5284      ## NOTE: States in the main stage is preserved while
5285      ## the parser stays in the trailing end phase. # MUST
5286    
5287    ## Stop parsing # MUST    ## Stop parsing # MUST
5288        
5289    ## TODO: script stuffs    ## TODO: script stuffs
# Line 5219  sub set_inner_html ($$$) { Line 5295  sub set_inner_html ($$$) {
5295    my $s = \$_[0];    my $s = \$_[0];
5296    my $onerror = $_[1];    my $onerror = $_[1];
5297    
5298      ## ISSUE: Should {confident} be true?
5299    
5300    my $nt = $node->node_type;    my $nt = $node->node_type;
5301    if ($nt == 9) {    if ($nt == 9) {
5302      # MUST      # MUST
# Line 5371  sub set_inner_html ($$$) { Line 5449  sub set_inner_html ($$$) {
5449    
5450  } # tree construction stage  } # tree construction stage
5451    
5452  sub get_inner_html ($$$) {  package Whatpm::HTML::RestartParser;
5453    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  
5454    
5455  1;  1;
5456  # $Date$  # $Date$

Legend:
Removed from v.1.40  
changed lines
  Added in v.1.65

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24