/[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.25 by wakaba, Sun Jun 24 05:12:11 2007 UTC revision 1.69 by wakaba, Sun Feb 17 12:39:32 2008 UTC
# Line 1  Line 1 
1  package Whatpm::HTML;  package Whatpm::HTML;
2  use strict;  use strict;
3  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
4    use Error qw(:try);
5    
6  ## ISSUE:  ## ISSUE:
7  ## var doc = implementation.createDocument (null, null, null);  ## var doc = implementation.createDocument (null, null, null);
8  ## doc.write ('');  ## doc.write ('');
9  ## alert (doc.compatMode);  ## alert (doc.compatMode);
10    
11    ## ISSUE: HTML5 revision 967 says that the encoding layer MUST NOT
12    ## strip BOM and the HTML layer MUST ignore it.  Whether we can do it
13    ## is not yet clear.
14    ## "{U+FEFF}..." in UTF-16BE/UTF-16LE is three or four characters?
15    ## "{U+FEFF}..." in GB18030?
16    
17  my $permitted_slash_tag_name = {  my $permitted_slash_tag_name = {
18    base => 1,    base => 1,
19    link => 1,    link => 1,
# Line 78  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 141  sub new ($) { Line 225  sub new ($) {
225    $self->{parse_error} = sub {    $self->{parse_error} = sub {
226      #      #
227    };    };
228      $self->{change_encoding} = sub {
229        # if ($_[0] is a supported encoding) {
230        #   run "change the encoding" algorithm;
231        #   throw Whatpm::HTML::RestartParser (charset => $new_encoding);
232        # }
233      };
234      $self->{application_cache_selection} = sub {
235        #
236      };
237    return $self;    return $self;
238  } # new  } # new
239    
240    sub CM_ENTITY () { 0b001 } # & markup in data
241    sub CM_LIMITED_MARKUP () { 0b010 } # < markup in data (limited)
242    sub CM_FULL_MARKUP () { 0b100 } # < markup in data (any)
243    
244    sub PLAINTEXT_CONTENT_MODEL () { 0 }
245    sub CDATA_CONTENT_MODEL () { CM_LIMITED_MARKUP }
246    sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }
247    sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
248    
249    sub DATA_STATE () { 0 }
250    sub ENTITY_DATA_STATE () { 1 }
251    sub TAG_OPEN_STATE () { 2 }
252    sub CLOSE_TAG_OPEN_STATE () { 3 }
253    sub TAG_NAME_STATE () { 4 }
254    sub BEFORE_ATTRIBUTE_NAME_STATE () { 5 }
255    sub ATTRIBUTE_NAME_STATE () { 6 }
256    sub AFTER_ATTRIBUTE_NAME_STATE () { 7 }
257    sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 }
258    sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
259    sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
260    sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
261    sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
262    sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
263    sub COMMENT_START_STATE () { 14 }
264    sub COMMENT_START_DASH_STATE () { 15 }
265    sub COMMENT_STATE () { 16 }
266    sub COMMENT_END_STATE () { 17 }
267    sub COMMENT_END_DASH_STATE () { 18 }
268    sub BOGUS_COMMENT_STATE () { 19 }
269    sub DOCTYPE_STATE () { 20 }
270    sub BEFORE_DOCTYPE_NAME_STATE () { 21 }
271    sub DOCTYPE_NAME_STATE () { 22 }
272    sub AFTER_DOCTYPE_NAME_STATE () { 23 }
273    sub BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 24 }
274    sub DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE () { 25 }
275    sub DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE () { 26 }
276    sub AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 27 }
277    sub BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 28 }
278    sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE () { 29 }
279    sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }
280    sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
281    sub BOGUS_DOCTYPE_STATE () { 32 }
282    
283    sub DOCTYPE_TOKEN () { 1 }
284    sub COMMENT_TOKEN () { 2 }
285    sub START_TAG_TOKEN () { 3 }
286    sub END_TAG_TOKEN () { 4 }
287    sub END_OF_FILE_TOKEN () { 5 }
288    sub CHARACTER_TOKEN () { 6 }
289    
290    sub AFTER_HTML_IMS () { 0b100 }
291    sub HEAD_IMS ()       { 0b1000 }
292    sub BODY_IMS ()       { 0b10000 }
293    sub BODY_TABLE_IMS () { 0b100000 }
294    sub TABLE_IMS ()      { 0b1000000 }
295    sub ROW_IMS ()        { 0b10000000 }
296    sub BODY_AFTER_IMS () { 0b100000000 }
297    sub FRAME_IMS ()      { 0b1000000000 }
298    
299    sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
300    sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
301    sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
302    sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
303    sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
304    sub BEFORE_HEAD_IM () { HEAD_IMS | 0b11 }
305    sub IN_BODY_IM () { BODY_IMS }
306    sub IN_CELL_IM () { BODY_IMS | BODY_TABLE_IMS | 0b01 }
307    sub IN_CAPTION_IM () { BODY_IMS | BODY_TABLE_IMS | 0b10 }
308    sub IN_ROW_IM () { TABLE_IMS | ROW_IMS | 0b01 }
309    sub IN_TABLE_BODY_IM () { TABLE_IMS | ROW_IMS | 0b10 }
310    sub IN_TABLE_IM () { TABLE_IMS }
311    sub AFTER_BODY_IM () { BODY_AFTER_IMS }
312    sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
313    sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
314    sub IN_SELECT_IM () { 0b01 }
315    sub IN_COLUMN_GROUP_IM () { 0b10 }
316    
317  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
318    
319  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
320    my $self = shift;    my $self = shift;
321    $self->{state} = 'data'; # MUST    $self->{state} = DATA_STATE; # MUST
322    $self->{content_model_flag} = 'PCDATA'; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
323    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE
324    undef $self->{current_attribute};    undef $self->{current_attribute};
325    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
# Line 162  sub _initialize_tokenizer ($) { Line 332  sub _initialize_tokenizer ($) {
332  } # _initialize_tokenizer  } # _initialize_tokenizer
333    
334  ## A token has:  ## A token has:
335  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',  ##   ->{type} == DOCTYPE_TOKEN, START_TAG_TOKEN, END_TAG_TOKEN, COMMENT_TOKEN,
336  ##       'character', or 'end-of-file'  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN
337  ##   ->{name} (DOCTYPE, start tag (tag name), end tag (tag name))  ##   ->{name} (DOCTYPE_TOKEN)
338  ##   ->{public_identifier} (DOCTYPE)  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
339  ##   ->{system_identifier} (DOCTYPE)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
340  ##   ->{correct} == 1 or 0 (DOCTYPE)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
341  ##   ->{attributes} isa HASH (start tag, end tag)  ##   ->{correct} == 1 or 0 (DOCTYPE_TOKEN)
342  ##   ->{data} (comment, character)  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
343    ##        ->{name}
344    ##        ->{value}
345    ##        ->{has_reference} == 1 or 0
346    ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
347    
348  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
349    
# Line 179  sub _initialize_tokenizer ($) { Line 353  sub _initialize_tokenizer ($) {
353  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
354  ## and removed from the list.  ## and removed from the list.
355    
356    ## NOTE: HTML5 "Writing HTML documents" section, applied to
357    ## documents and not to user agents and conformance checkers,
358    ## contains some requirements that are not detected by the
359    ## parsing algorithm:
360    ## - Some requirements on character encoding declarations. ## TODO
361    ## - "Elements MUST NOT contain content that their content model disallows."
362    ##   ... Some are parse error, some are not (will be reported by c.c.).
363    ## - Polytheistic slash SHOULD NOT be used. (Applied only to atheists.) ## TODO
364    ## - Text (in elements, attributes, and comments) SHOULD NOT contain
365    ##   control characters other than space characters. ## TODO: (what is control character? C0, C1 and DEL?  Unicode control character?)
366    
367    ## TODO: HTML5 poses authors two SHOULD-level requirements that cannot
368    ## be detected by the HTML5 parsing algorithm:
369    ## - Text,
370    
371  sub _get_next_token ($) {  sub _get_next_token ($) {
372    my $self = shift;    my $self = shift;
373    if (@{$self->{token}}) {    if (@{$self->{token}}) {
# Line 186  sub _get_next_token ($) { Line 375  sub _get_next_token ($) {
375    }    }
376    
377    A: {    A: {
378      if ($self->{state} eq 'data') {      if ($self->{state} == DATA_STATE) {
379        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_input_character} == 0x0026) { # &
380          if ($self->{content_model_flag} eq 'PCDATA' or          if ($self->{content_model} & CM_ENTITY) { # PCDATA | RCDATA
381              $self->{content_model_flag} eq 'RCDATA') {            $self->{state} = ENTITY_DATA_STATE;
           $self->{state} = 'entity data';  
382            !!!next-input-character;            !!!next-input-character;
383            redo A;            redo A;
384          } else {          } else {
385            #            #
386          }          }
387        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_input_character} == 0x002D) { # -
388          if ($self->{content_model_flag} eq 'RCDATA' or          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
             $self->{content_model_flag} eq 'CDATA') {  
389            unless ($self->{escape}) {            unless ($self->{escape}) {
390              if ($self->{prev_input_character}->[0] == 0x002D and # -              if ($self->{prev_input_character}->[0] == 0x002D and # -
391                  $self->{prev_input_character}->[1] == 0x0021 and # !                  $self->{prev_input_character}->[1] == 0x0021 and # !
# Line 210  sub _get_next_token ($) { Line 397  sub _get_next_token ($) {
397                    
398          #          #
399        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_input_character} == 0x003C) { # <
400          if ($self->{content_model_flag} eq 'PCDATA' or          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
401              (($self->{content_model_flag} eq 'CDATA' or              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
               $self->{content_model_flag} eq 'RCDATA') and  
402               not $self->{escape})) {               not $self->{escape})) {
403            $self->{state} = 'tag open';            $self->{state} = TAG_OPEN_STATE;
404            !!!next-input-character;            !!!next-input-character;
405            redo A;            redo A;
406          } else {          } else {
# Line 222  sub _get_next_token ($) { Line 408  sub _get_next_token ($) {
408          }          }
409        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
410          if ($self->{escape} and          if ($self->{escape} and
411              ($self->{content_model_flag} eq 'RCDATA' or              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
              $self->{content_model_flag} eq 'CDATA')) {  
412            if ($self->{prev_input_character}->[0] == 0x002D and # -            if ($self->{prev_input_character}->[0] == 0x002D and # -
413                $self->{prev_input_character}->[1] == 0x002D) { # -                $self->{prev_input_character}->[1] == 0x002D) { # -
414              delete $self->{escape};              delete $self->{escape};
# Line 232  sub _get_next_token ($) { Line 417  sub _get_next_token ($) {
417                    
418          #          #
419        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
420          !!!emit ({type => 'end-of-file'});          !!!emit ({type => END_OF_FILE_TOKEN});
421          last A; ## TODO: ok?          last A; ## TODO: ok?
422        }        }
423        # Anything else        # Anything else
424        my $token = {type => 'character',        my $token = {type => CHARACTER_TOKEN,
425                     data => chr $self->{next_input_character}};                     data => chr $self->{next_input_character}};
426        ## Stay in the data state        ## Stay in the data state
427        !!!next-input-character;        !!!next-input-character;
# Line 244  sub _get_next_token ($) { Line 429  sub _get_next_token ($) {
429        !!!emit ($token);        !!!emit ($token);
430    
431        redo A;        redo A;
432      } elsif ($self->{state} eq 'entity data') {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
433        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
434                
435        my $token = $self->_tokenize_attempt_to_consume_an_entity;        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);
436    
437        $self->{state} = 'data';        $self->{state} = DATA_STATE;
438        # next-input-character is already done        # next-input-character is already done
439    
440        unless (defined $token) {        unless (defined $token) {
441          !!!emit ({type => 'character', data => '&'});          !!!emit ({type => CHARACTER_TOKEN, data => '&'});
442        } else {        } else {
443          !!!emit ($token);          !!!emit ($token);
444        }        }
445    
446        redo A;        redo A;
447      } elsif ($self->{state} eq 'tag open') {      } elsif ($self->{state} == TAG_OPEN_STATE) {
448        if ($self->{content_model_flag} eq 'RCDATA' or        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
           $self->{content_model_flag} eq 'CDATA') {  
449          if ($self->{next_input_character} == 0x002F) { # /          if ($self->{next_input_character} == 0x002F) { # /
450            !!!next-input-character;            !!!next-input-character;
451            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
452            redo A;            redo A;
453          } else {          } else {
454            ## reconsume            ## reconsume
455            $self->{state} = 'data';            $self->{state} = DATA_STATE;
456    
457            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<'});
458    
459            redo A;            redo A;
460          }          }
461        } elsif ($self->{content_model_flag} eq 'PCDATA') {        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
462          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_input_character} == 0x0021) { # !
463            $self->{state} = 'markup declaration open';            $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
464            !!!next-input-character;            !!!next-input-character;
465            redo A;            redo A;
466          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{next_input_character} == 0x002F) { # /
467            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
468            !!!next-input-character;            !!!next-input-character;
469            redo A;            redo A;
470          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_input_character} and
471                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{next_input_character} <= 0x005A) { # A..Z
472            $self->{current_token}            $self->{current_token}
473              = {type => 'start tag',              = {type => START_TAG_TOKEN,
474                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_input_character} + 0x0020)};
475            $self->{state} = 'tag name';            $self->{state} = TAG_NAME_STATE;
476            !!!next-input-character;            !!!next-input-character;
477            redo A;            redo A;
478          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_input_character} and
479                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_input_character} <= 0x007A) { # a..z
480            $self->{current_token} = {type => 'start tag',            $self->{current_token} = {type => START_TAG_TOKEN,
481                              tag_name => chr ($self->{next_input_character})};                              tag_name => chr ($self->{next_input_character})};
482            $self->{state} = 'tag name';            $self->{state} = TAG_NAME_STATE;
483            !!!next-input-character;            !!!next-input-character;
484            redo A;            redo A;
485          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_input_character} == 0x003E) { # >
486            !!!parse-error (type => 'empty start tag');            !!!parse-error (type => 'empty start tag');
487            $self->{state} = 'data';            $self->{state} = DATA_STATE;
488            !!!next-input-character;            !!!next-input-character;
489    
490            !!!emit ({type => 'character', data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});
491    
492            redo A;            redo A;
493          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_input_character} == 0x003F) { # ?
494            !!!parse-error (type => 'pio');            !!!parse-error (type => 'pio');
495            $self->{state} = 'bogus comment';            $self->{state} = BOGUS_COMMENT_STATE;
496            ## $self->{next_input_character} is intentionally left as is            ## $self->{next_input_character} is intentionally left as is
497            redo A;            redo A;
498          } else {          } else {
499            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago');
500            $self->{state} = 'data';            $self->{state} = DATA_STATE;
501            ## reconsume            ## reconsume
502    
503            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<'});
504    
505            redo A;            redo A;
506          }          }
507        } else {        } else {
508          die "$0: $self->{content_model_flag}: Unknown content model flag";          die "$0: $self->{content_model} in tag open";
509        }        }
510      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
511        if ($self->{content_model_flag} eq 'RCDATA' or        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
           $self->{content_model_flag} eq 'CDATA') {  
512          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
513              ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
514            my @next_char;            my @next_char;
515            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {
516              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_input_character};
# Line 338  sub _get_next_token ($) { Line 522  sub _get_next_token ($) {
522              } else {              } else {
523                $self->{next_input_character} = shift @next_char; # reconsume                $self->{next_input_character} = shift @next_char; # reconsume
524                !!!back-next-input-character (@next_char);                !!!back-next-input-character (@next_char);
525                $self->{state} = 'data';                $self->{state} = DATA_STATE;
526    
527                !!!emit ({type => 'character', data => '</'});                !!!emit ({type => CHARACTER_TOKEN, data => '</'});
528        
529                redo A;                redo A;
530              }              }
# Line 357  sub _get_next_token ($) { Line 541  sub _get_next_token ($) {
541                    $self->{next_input_character} == -1) {                    $self->{next_input_character} == -1) {
542              $self->{next_input_character} = shift @next_char; # reconsume              $self->{next_input_character} = shift @next_char; # reconsume
543              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
544              $self->{state} = 'data';              $self->{state} = DATA_STATE;
545              !!!emit ({type => 'character', data => '</'});              !!!emit ({type => CHARACTER_TOKEN, data => '</'});
546              redo A;              redo A;
547            } else {            } else {
548              $self->{next_input_character} = shift @next_char;              $self->{next_input_character} = shift @next_char;
# Line 368  sub _get_next_token ($) { Line 552  sub _get_next_token ($) {
552          } else {          } else {
553            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
554            # next-input-character is already done            # next-input-character is already done
555            $self->{state} = 'data';            $self->{state} = DATA_STATE;
556            !!!emit ({type => 'character', data => '</'});            !!!emit ({type => CHARACTER_TOKEN, data => '</'});
557            redo A;            redo A;
558          }          }
559        }        }
560                
561        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_input_character} and
562            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_input_character} <= 0x005A) { # A..Z
563          $self->{current_token} = {type => 'end tag',          $self->{current_token} = {type => END_TAG_TOKEN,
564                            tag_name => chr ($self->{next_input_character} + 0x0020)};                            tag_name => chr ($self->{next_input_character} + 0x0020)};
565          $self->{state} = 'tag name';          $self->{state} = TAG_NAME_STATE;
566          !!!next-input-character;          !!!next-input-character;
567          redo A;          redo A;
568        } elsif (0x0061 <= $self->{next_input_character} and        } elsif (0x0061 <= $self->{next_input_character} and
569                 $self->{next_input_character} <= 0x007A) { # a..z                 $self->{next_input_character} <= 0x007A) { # a..z
570          $self->{current_token} = {type => 'end tag',          $self->{current_token} = {type => END_TAG_TOKEN,
571                            tag_name => chr ($self->{next_input_character})};                            tag_name => chr ($self->{next_input_character})};
572          $self->{state} = 'tag name';          $self->{state} = TAG_NAME_STATE;
573          !!!next-input-character;          !!!next-input-character;
574          redo A;          redo A;
575        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
576          !!!parse-error (type => 'empty end tag');          !!!parse-error (type => 'empty end tag');
577          $self->{state} = 'data';          $self->{state} = DATA_STATE;
578          !!!next-input-character;          !!!next-input-character;
579          redo A;          redo A;
580        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
581          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
582          $self->{state} = 'data';          $self->{state} = DATA_STATE;
583          # reconsume          # reconsume
584    
585          !!!emit ({type => 'character', data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</'});
586    
587          redo A;          redo A;
588        } else {        } else {
589          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
590          $self->{state} = 'bogus comment';          $self->{state} = BOGUS_COMMENT_STATE;
591          ## $self->{next_input_character} is intentionally left as is          ## $self->{next_input_character} is intentionally left as is
592          redo A;          redo A;
593        }        }
594      } elsif ($self->{state} eq 'tag name') {      } elsif ($self->{state} == TAG_NAME_STATE) {
595        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
596            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
597            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
598            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
599            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
600          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
601          !!!next-input-character;          !!!next-input-character;
602          redo A;          redo A;
603        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
604          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
605              $self->{current_token}->{first_start_tag}
606                  = not defined $self->{last_emitted_start_tag_name};
607            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
608          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
609            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
610            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
611              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
612            }            }
613          } else {          } else {
614            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
615          }          }
616          $self->{state} = 'data';          $self->{state} = DATA_STATE;
617          !!!next-input-character;          !!!next-input-character;
618    
619          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 442  sub _get_next_token ($) { Line 628  sub _get_next_token ($) {
628          redo A;          redo A;
629        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
630          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
631          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
632              $self->{current_token}->{first_start_tag}
633                  = not defined $self->{last_emitted_start_tag_name};
634            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
635          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
636            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
637            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
638              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
639            }            }
640          } else {          } else {
641            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
642          }          }
643          $self->{state} = 'data';          $self->{state} = DATA_STATE;
644          # reconsume          # reconsume
645    
646          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 461  sub _get_next_token ($) { Line 649  sub _get_next_token ($) {
649        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
650          !!!next-input-character;          !!!next-input-character;
651          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
652              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
653              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
654            # permitted slash            # permitted slash
655            #            #
656          } else {          } else {
657            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
658          }          }
659          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
660          # next-input-character is already done          # next-input-character is already done
661          redo A;          redo A;
662        } else {        } else {
# Line 478  sub _get_next_token ($) { Line 666  sub _get_next_token ($) {
666          !!!next-input-character;          !!!next-input-character;
667          redo A;          redo A;
668        }        }
669      } elsif ($self->{state} eq 'before attribute name') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
670        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
671            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
672            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 488  sub _get_next_token ($) { Line 676  sub _get_next_token ($) {
676          !!!next-input-character;          !!!next-input-character;
677          redo A;          redo A;
678        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
679          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
680              $self->{current_token}->{first_start_tag}
681                  = not defined $self->{last_emitted_start_tag_name};
682            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
683          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
684            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
685            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
686              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
687            }            }
688          } else {          } else {
689            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
690          }          }
691          $self->{state} = 'data';          $self->{state} = DATA_STATE;
692          !!!next-input-character;          !!!next-input-character;
693    
694          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 508  sub _get_next_token ($) { Line 698  sub _get_next_token ($) {
698                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_input_character} <= 0x005A) { # A..Z
699          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),
700                                value => ''};                                value => ''};
701          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
702          !!!next-input-character;          !!!next-input-character;
703          redo A;          redo A;
704        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
705          !!!next-input-character;          !!!next-input-character;
706          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
707              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
708              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
709            # permitted slash            # permitted slash
710            #            #
# Line 526  sub _get_next_token ($) { Line 716  sub _get_next_token ($) {
716          redo A;          redo A;
717        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
718          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
719          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
720              $self->{current_token}->{first_start_tag}
721                  = not defined $self->{last_emitted_start_tag_name};
722            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
723          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
724            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
725            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
726              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
727            }            }
728          } else {          } else {
729            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
730          }          }
731          $self->{state} = 'data';          $self->{state} = DATA_STATE;
732          # reconsume          # reconsume
733    
734          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 545  sub _get_next_token ($) { Line 737  sub _get_next_token ($) {
737        } else {        } else {
738          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          $self->{current_attribute} = {name => chr ($self->{next_input_character}),
739                                value => ''};                                value => ''};
740          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
741          !!!next-input-character;          !!!next-input-character;
742          redo A;          redo A;
743        }        }
744      } elsif ($self->{state} eq 'attribute name') {      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {
745        my $before_leave = sub {        my $before_leave = sub {
746          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
747              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
748            !!!parse-error (type => 'dupulicate attribute');            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});
749            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
750          } else {          } else {
751            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
# Line 567  sub _get_next_token ($) { Line 759  sub _get_next_token ($) {
759            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
760            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
761          $before_leave->();          $before_leave->();
762          $self->{state} = 'after attribute name';          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
763          !!!next-input-character;          !!!next-input-character;
764          redo A;          redo A;
765        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_input_character} == 0x003D) { # =
766          $before_leave->();          $before_leave->();
767          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
768          !!!next-input-character;          !!!next-input-character;
769          redo A;          redo A;
770        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
771          $before_leave->();          $before_leave->();
772          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
773              $self->{current_token}->{first_start_tag}
774                  = not defined $self->{last_emitted_start_tag_name};
775            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
776          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
777            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
778            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
779              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
780            }            }
781          } else {          } else {
782            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
783          }          }
784          $self->{state} = 'data';          $self->{state} = DATA_STATE;
785          !!!next-input-character;          !!!next-input-character;
786    
787          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 603  sub _get_next_token ($) { Line 797  sub _get_next_token ($) {
797          $before_leave->();          $before_leave->();
798          !!!next-input-character;          !!!next-input-character;
799          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
800              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
801              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
802            # permitted slash            # permitted slash
803            #            #
804          } else {          } else {
805            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
806          }          }
807          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
808          # next-input-character is already done          # next-input-character is already done
809          redo A;          redo A;
810        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
811          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
812          $before_leave->();          $before_leave->();
813          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
814              $self->{current_token}->{first_start_tag}
815                  = not defined $self->{last_emitted_start_tag_name};
816            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
817          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
818            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
819            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
820              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
821            }            }
822          } else {          } else {
823            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
824          }          }
825          $self->{state} = 'data';          $self->{state} = DATA_STATE;
826          # reconsume          # reconsume
827    
828          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 638  sub _get_next_token ($) { Line 834  sub _get_next_token ($) {
834          !!!next-input-character;          !!!next-input-character;
835          redo A;          redo A;
836        }        }
837      } elsif ($self->{state} eq 'after attribute name') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
838        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
839            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
840            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 648  sub _get_next_token ($) { Line 844  sub _get_next_token ($) {
844          !!!next-input-character;          !!!next-input-character;
845          redo A;          redo A;
846        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_input_character} == 0x003D) { # =
847          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
848          !!!next-input-character;          !!!next-input-character;
849          redo A;          redo A;
850        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
851          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
852              $self->{current_token}->{first_start_tag}
853                  = not defined $self->{last_emitted_start_tag_name};
854            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
855          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
856            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
857            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
858              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
859            }            }
860          } else {          } else {
861            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
862          }          }
863          $self->{state} = 'data';          $self->{state} = DATA_STATE;
864          !!!next-input-character;          !!!next-input-character;
865    
866          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 672  sub _get_next_token ($) { Line 870  sub _get_next_token ($) {
870                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_input_character} <= 0x005A) { # A..Z
871          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),
872                                value => ''};                                value => ''};
873          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
874          !!!next-input-character;          !!!next-input-character;
875          redo A;          redo A;
876        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
877          !!!next-input-character;          !!!next-input-character;
878          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
879              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
880              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
881            # permitted slash            # permitted slash
882            #            #
883          } else {          } else {
884            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
885              ## TODO: Different error type for <aa / bb> than <aa/>
886          }          }
887          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
888          # next-input-character is already done          # next-input-character is already done
889          redo A;          redo A;
890        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
891          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
892          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
893              $self->{current_token}->{first_start_tag}
894                  = not defined $self->{last_emitted_start_tag_name};
895            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
896          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
897            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
898            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
899              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
900            }            }
901          } else {          } else {
902            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
903          }          }
904          $self->{state} = 'data';          $self->{state} = DATA_STATE;
905          # reconsume          # reconsume
906    
907          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 709  sub _get_next_token ($) { Line 910  sub _get_next_token ($) {
910        } else {        } else {
911          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          $self->{current_attribute} = {name => chr ($self->{next_input_character}),
912                                value => ''};                                value => ''};
913          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
914          !!!next-input-character;          !!!next-input-character;
915          redo A;                  redo A;        
916        }        }
917      } elsif ($self->{state} eq 'before attribute value') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
918        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
919            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
920            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 723  sub _get_next_token ($) { Line 924  sub _get_next_token ($) {
924          !!!next-input-character;          !!!next-input-character;
925          redo A;          redo A;
926        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_input_character} == 0x0022) { # "
927          $self->{state} = 'attribute value (double-quoted)';          $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
928          !!!next-input-character;          !!!next-input-character;
929          redo A;          redo A;
930        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
931          $self->{state} = 'attribute value (unquoted)';          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
932          ## reconsume          ## reconsume
933          redo A;          redo A;
934        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_input_character} == 0x0027) { # '
935          $self->{state} = 'attribute value (single-quoted)';          $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
936          !!!next-input-character;          !!!next-input-character;
937          redo A;          redo A;
938        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
939          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
940              $self->{current_token}->{first_start_tag}
941                  = not defined $self->{last_emitted_start_tag_name};
942            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
943          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
944            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
945            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
946              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
947            }            }
948          } else {          } else {
949            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
950          }          }
951          $self->{state} = 'data';          $self->{state} = DATA_STATE;
952          !!!next-input-character;          !!!next-input-character;
953    
954          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 753  sub _get_next_token ($) { Line 956  sub _get_next_token ($) {
956          redo A;          redo A;
957        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
958          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
959          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
960              $self->{current_token}->{first_start_tag}
961                  = not defined $self->{last_emitted_start_tag_name};
962            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
963          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
964            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
965            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
966              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
967            }            }
968          } else {          } else {
969            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
970          }          }
971          $self->{state} = 'data';          $self->{state} = DATA_STATE;
972          ## reconsume          ## reconsume
973    
974          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 771  sub _get_next_token ($) { Line 976  sub _get_next_token ($) {
976          redo A;          redo A;
977        } else {        } else {
978          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});
979          $self->{state} = 'attribute value (unquoted)';          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
980          !!!next-input-character;          !!!next-input-character;
981          redo A;          redo A;
982        }        }
983      } elsif ($self->{state} eq 'attribute value (double-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
984        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_input_character} == 0x0022) { # "
985          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
986          !!!next-input-character;          !!!next-input-character;
987          redo A;          redo A;
988        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
989          $self->{last_attribute_value_state} = 'attribute value (double-quoted)';          $self->{last_attribute_value_state} = $self->{state};
990          $self->{state} = 'entity in attribute value';          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
991          !!!next-input-character;          !!!next-input-character;
992          redo A;          redo A;
993        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
994          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
995          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
996              $self->{current_token}->{first_start_tag}
997                  = not defined $self->{last_emitted_start_tag_name};
998            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
999          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1000            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1001            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1002              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1003            }            }
1004          } else {          } else {
1005            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1006          }          }
1007          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1008          ## reconsume          ## reconsume
1009    
1010          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 809  sub _get_next_token ($) { Line 1016  sub _get_next_token ($) {
1016          !!!next-input-character;          !!!next-input-character;
1017          redo A;          redo A;
1018        }        }
1019      } elsif ($self->{state} eq 'attribute value (single-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1020        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_input_character} == 0x0027) { # '
1021          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1022          !!!next-input-character;          !!!next-input-character;
1023          redo A;          redo A;
1024        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
1025          $self->{last_attribute_value_state} = 'attribute value (single-quoted)';          $self->{last_attribute_value_state} = $self->{state};
1026          $self->{state} = 'entity in attribute value';          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1027          !!!next-input-character;          !!!next-input-character;
1028          redo A;          redo A;
1029        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1030          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1031          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1032              $self->{current_token}->{first_start_tag}
1033                  = not defined $self->{last_emitted_start_tag_name};
1034            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1035          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1036            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1037            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1038              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1039            }            }
1040          } else {          } else {
1041            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1042          }          }
1043          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1044          ## reconsume          ## reconsume
1045    
1046          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 843  sub _get_next_token ($) { Line 1052  sub _get_next_token ($) {
1052          !!!next-input-character;          !!!next-input-character;
1053          redo A;          redo A;
1054        }        }
1055      } elsif ($self->{state} eq 'attribute value (unquoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1056        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1057            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1058            $self->{next_input_character} == 0x000B or # HT            $self->{next_input_character} == 0x000B or # HT
1059            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1060            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
1061          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1062          !!!next-input-character;          !!!next-input-character;
1063          redo A;          redo A;
1064        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
1065          $self->{last_attribute_value_state} = 'attribute value (unquoted)';          $self->{last_attribute_value_state} = $self->{state};
1066          $self->{state} = 'entity in attribute value';          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1067          !!!next-input-character;          !!!next-input-character;
1068          redo A;          redo A;
1069        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1070          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1071              $self->{current_token}->{first_start_tag}
1072                  = not defined $self->{last_emitted_start_tag_name};
1073            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1074          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1075            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1076            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1077              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1078            }            }
1079          } else {          } else {
1080            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1081          }          }
1082          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1083          !!!next-input-character;          !!!next-input-character;
1084    
1085          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 876  sub _get_next_token ($) { Line 1087  sub _get_next_token ($) {
1087          redo A;          redo A;
1088        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1089          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1090          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1091              $self->{current_token}->{first_start_tag}
1092                  = not defined $self->{last_emitted_start_tag_name};
1093            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1094          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1095            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1096            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1097              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1098            }            }
1099          } else {          } else {
1100            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1101          }          }
1102          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1103          ## reconsume          ## reconsume
1104    
1105          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 898  sub _get_next_token ($) { Line 1111  sub _get_next_token ($) {
1111          !!!next-input-character;          !!!next-input-character;
1112          redo A;          redo A;
1113        }        }
1114      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {
1115        my $token = $self->_tokenize_attempt_to_consume_an_entity;        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);
1116    
1117        unless (defined $token) {        unless (defined $token) {
1118          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
1119        } else {        } else {
1120          $self->{current_attribute}->{value} .= $token->{data};          $self->{current_attribute}->{value} .= $token->{data};
1121            $self->{current_attribute}->{has_reference} = $token->{has_reference};
1122          ## ISSUE: spec says "append the returned character token to the current attribute's value"          ## ISSUE: spec says "append the returned character token to the current attribute's value"
1123        }        }
1124    
1125        $self->{state} = $self->{last_attribute_value_state};        $self->{state} = $self->{last_attribute_value_state};
1126        # next-input-character is already done        # next-input-character is already done
1127        redo A;        redo A;
1128      } elsif ($self->{state} eq 'bogus comment') {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1129        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1130                
1131        my $token = {type => 'comment', data => ''};        my $token = {type => COMMENT_TOKEN, data => ''};
1132    
1133        BC: {        BC: {
1134          if ($self->{next_input_character} == 0x003E) { # >          if ($self->{next_input_character} == 0x003E) { # >
1135            $self->{state} = 'data';            $self->{state} = DATA_STATE;
1136            !!!next-input-character;            !!!next-input-character;
1137    
1138            !!!emit ($token);            !!!emit ($token);
1139    
1140            redo A;            redo A;
1141          } elsif ($self->{next_input_character} == -1) {          } elsif ($self->{next_input_character} == -1) {
1142            $self->{state} = 'data';            $self->{state} = DATA_STATE;
1143            ## reconsume            ## reconsume
1144    
1145            !!!emit ($token);            !!!emit ($token);
# Line 937  sub _get_next_token ($) { Line 1151  sub _get_next_token ($) {
1151            redo BC;            redo BC;
1152          }          }
1153        } # BC        } # BC
1154      } elsif ($self->{state} eq 'markup declaration open') {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1155        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1156    
1157        my @next_char;        my @next_char;
# Line 947  sub _get_next_token ($) { Line 1161  sub _get_next_token ($) {
1161          !!!next-input-character;          !!!next-input-character;
1162          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_input_character};
1163          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_input_character} == 0x002D) { # -
1164            $self->{current_token} = {type => 'comment', data => ''};            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};
1165            $self->{state} = 'comment start';            $self->{state} = COMMENT_START_STATE;
1166            !!!next-input-character;            !!!next-input-character;
1167            redo A;            redo A;
1168          }          }
# Line 979  sub _get_next_token ($) { Line 1193  sub _get_next_token ($) {
1193                    if ($self->{next_input_character} == 0x0045 or # E                    if ($self->{next_input_character} == 0x0045 or # E
1194                        $self->{next_input_character} == 0x0065) { # e                        $self->{next_input_character} == 0x0065) { # e
1195                      ## ISSUE: What a stupid code this is!                      ## ISSUE: What a stupid code this is!
1196                      $self->{state} = 'DOCTYPE';                      $self->{state} = DOCTYPE_STATE;
1197                      !!!next-input-character;                      !!!next-input-character;
1198                      redo A;                      redo A;
1199                    }                    }
# Line 990  sub _get_next_token ($) { Line 1204  sub _get_next_token ($) {
1204          }          }
1205        }        }
1206    
1207        !!!parse-error (type => 'bogus comment open');        !!!parse-error (type => 'bogus comment');
1208        $self->{next_input_character} = shift @next_char;        $self->{next_input_character} = shift @next_char;
1209        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
1210        $self->{state} = 'bogus comment';        $self->{state} = BOGUS_COMMENT_STATE;
1211        redo A;        redo A;
1212                
1213        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
1214        ## ISSUE: spec is somewhat unclear on "is the first character that will be in the comment"; what is "that will be in the comment" is what the algorithm defines, isn't it?        ## ISSUE: spec is somewhat unclear on "is the first character that will be in the comment"; what is "that will be in the comment" is what the algorithm defines, isn't it?
1215      } elsif ($self->{state} eq 'comment start') {      } elsif ($self->{state} == COMMENT_START_STATE) {
1216        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1217          $self->{state} = 'comment start dash';          $self->{state} = COMMENT_START_DASH_STATE;
1218          !!!next-input-character;          !!!next-input-character;
1219          redo A;          redo A;
1220        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1221          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
1222          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1223          !!!next-input-character;          !!!next-input-character;
1224    
1225          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1013  sub _get_next_token ($) { Line 1227  sub _get_next_token ($) {
1227          redo A;          redo A;
1228        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1229          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1230          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1231          ## reconsume          ## reconsume
1232    
1233          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1022  sub _get_next_token ($) { Line 1236  sub _get_next_token ($) {
1236        } else {        } else {
1237          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
1238              .= chr ($self->{next_input_character});              .= chr ($self->{next_input_character});
1239          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1240          !!!next-input-character;          !!!next-input-character;
1241          redo A;          redo A;
1242        }        }
1243      } elsif ($self->{state} eq 'comment start dash') {      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
1244        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1245          $self->{state} = 'comment end';          $self->{state} = COMMENT_END_STATE;
1246          !!!next-input-character;          !!!next-input-character;
1247          redo A;          redo A;
1248        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1249          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
1250          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1251          !!!next-input-character;          !!!next-input-character;
1252    
1253          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1041  sub _get_next_token ($) { Line 1255  sub _get_next_token ($) {
1255          redo A;          redo A;
1256        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1257          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1258          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1259          ## reconsume          ## reconsume
1260    
1261          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1049  sub _get_next_token ($) { Line 1263  sub _get_next_token ($) {
1263          redo A;          redo A;
1264        } else {        } else {
1265          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
1266              .= chr ($self->{next_input_character});              .= '-' . chr ($self->{next_input_character});
1267          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1268          !!!next-input-character;          !!!next-input-character;
1269          redo A;          redo A;
1270        }        }
1271      } elsif ($self->{state} eq 'comment') {      } elsif ($self->{state} == COMMENT_STATE) {
1272        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1273          $self->{state} = 'comment end dash';          $self->{state} = COMMENT_END_DASH_STATE;
1274          !!!next-input-character;          !!!next-input-character;
1275          redo A;          redo A;
1276        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1277          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1278          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1279          ## reconsume          ## reconsume
1280    
1281          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1073  sub _get_next_token ($) { Line 1287  sub _get_next_token ($) {
1287          !!!next-input-character;          !!!next-input-character;
1288          redo A;          redo A;
1289        }        }
1290      } elsif ($self->{state} eq 'comment end dash') {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
1291        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1292          $self->{state} = 'comment end';          $self->{state} = COMMENT_END_STATE;
1293          !!!next-input-character;          !!!next-input-character;
1294          redo A;          redo A;
1295        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1296          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1297          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1298          ## reconsume          ## reconsume
1299    
1300          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1088  sub _get_next_token ($) { Line 1302  sub _get_next_token ($) {
1302          redo A;          redo A;
1303        } else {        } else {
1304          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment
1305          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1306          !!!next-input-character;          !!!next-input-character;
1307          redo A;          redo A;
1308        }        }
1309      } elsif ($self->{state} eq 'comment end') {      } elsif ($self->{state} == COMMENT_END_STATE) {
1310        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_input_character} == 0x003E) { # >
1311          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1312          !!!next-input-character;          !!!next-input-character;
1313    
1314          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1108  sub _get_next_token ($) { Line 1322  sub _get_next_token ($) {
1322          redo A;          redo A;
1323        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1324          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1325          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1326          ## reconsume          ## reconsume
1327    
1328          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1117  sub _get_next_token ($) { Line 1331  sub _get_next_token ($) {
1331        } else {        } else {
1332          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment');
1333          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment
1334          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1335          !!!next-input-character;          !!!next-input-character;
1336          redo A;          redo A;
1337        }        }
1338      } elsif ($self->{state} eq 'DOCTYPE') {      } elsif ($self->{state} == DOCTYPE_STATE) {
1339        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1340            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1341            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
1342            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1343            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
1344          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1345          !!!next-input-character;          !!!next-input-character;
1346          redo A;          redo A;
1347        } else {        } else {
1348          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
1349          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1350          ## reconsume          ## reconsume
1351          redo A;          redo A;
1352        }        }
1353      } elsif ($self->{state} eq 'before DOCTYPE name') {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
1354        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1355            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1356            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 1147  sub _get_next_token ($) { Line 1361  sub _get_next_token ($) {
1361          redo A;          redo A;
1362        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1363          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1364          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1365          !!!next-input-character;          !!!next-input-character;
1366    
1367          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect
1368    
1369          redo A;          redo A;
1370        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1371          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1372          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1373          ## reconsume          ## reconsume
1374    
1375          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect
1376    
1377          redo A;          redo A;
1378        } else {        } else {
1379          $self->{current_token}          $self->{current_token}
1380              = {type => 'DOCTYPE',              = {type => DOCTYPE_TOKEN,
1381                 name => chr ($self->{next_input_character}),                 name => chr ($self->{next_input_character}),
1382                 correct => 1};                 correct => 1};
1383  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
1384          $self->{state} = 'DOCTYPE name';          $self->{state} = DOCTYPE_NAME_STATE;
1385          !!!next-input-character;          !!!next-input-character;
1386          redo A;          redo A;
1387        }        }
1388      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
1389  ## ISSUE: Redundant "First," in the spec.  ## ISSUE: Redundant "First," in the spec.
1390        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1391            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1392            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
1393            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1394            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
1395          $self->{state} = 'after DOCTYPE name';          $self->{state} = AFTER_DOCTYPE_NAME_STATE;
1396          !!!next-input-character;          !!!next-input-character;
1397          redo A;          redo A;
1398        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1399          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1400          !!!next-input-character;          !!!next-input-character;
1401    
1402          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1190  sub _get_next_token ($) { Line 1404  sub _get_next_token ($) {
1404          redo A;          redo A;
1405        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1406          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1407          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1408          ## reconsume          ## reconsume
1409    
1410          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1204  sub _get_next_token ($) { Line 1418  sub _get_next_token ($) {
1418          !!!next-input-character;          !!!next-input-character;
1419          redo A;          redo A;
1420        }        }
1421      } elsif ($self->{state} eq 'after DOCTYPE name') {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
1422        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1423            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1424            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 1214  sub _get_next_token ($) { Line 1428  sub _get_next_token ($) {
1428          !!!next-input-character;          !!!next-input-character;
1429          redo A;          redo A;
1430        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1431          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1432          !!!next-input-character;          !!!next-input-character;
1433    
1434          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1222  sub _get_next_token ($) { Line 1436  sub _get_next_token ($) {
1436          redo A;          redo A;
1437        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1438          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1439          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1440          ## reconsume          ## reconsume
1441    
1442          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1246  sub _get_next_token ($) { Line 1460  sub _get_next_token ($) {
1460                  !!!next-input-character;                  !!!next-input-character;
1461                  if ($self->{next_input_character} == 0x0043 or # C                  if ($self->{next_input_character} == 0x0043 or # C
1462                      $self->{next_input_character} == 0x0063) { # c                      $self->{next_input_character} == 0x0063) { # c
1463                    $self->{state} = 'before DOCTYPE public identifier';                    $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1464                    !!!next-input-character;                    !!!next-input-character;
1465                    redo A;                    redo A;
1466                  }                  }
# Line 1273  sub _get_next_token ($) { Line 1487  sub _get_next_token ($) {
1487                  !!!next-input-character;                  !!!next-input-character;
1488                  if ($self->{next_input_character} == 0x004D or # M                  if ($self->{next_input_character} == 0x004D or # M
1489                      $self->{next_input_character} == 0x006D) { # m                      $self->{next_input_character} == 0x006D) { # m
1490                    $self->{state} = 'before DOCTYPE system identifier';                    $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1491                    !!!next-input-character;                    !!!next-input-character;
1492                    redo A;                    redo A;
1493                  }                  }
# Line 1289  sub _get_next_token ($) { Line 1503  sub _get_next_token ($) {
1503        }        }
1504    
1505        !!!parse-error (type => 'string after DOCTYPE name');        !!!parse-error (type => 'string after DOCTYPE name');
1506        $self->{state} = 'bogus DOCTYPE';        $self->{state} = BOGUS_DOCTYPE_STATE;
1507        # next-input-character is already done        # next-input-character is already done
1508        redo A;        redo A;
1509      } elsif ($self->{state} eq 'before DOCTYPE public identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
1510        if ({        if ({
1511              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1512              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1302  sub _get_next_token ($) { Line 1516  sub _get_next_token ($) {
1516          redo A;          redo A;
1517        } elsif ($self->{next_input_character} eq 0x0022) { # "        } elsif ($self->{next_input_character} eq 0x0022) { # "
1518          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1519          $self->{state} = 'DOCTYPE public identifier (double-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
1520          !!!next-input-character;          !!!next-input-character;
1521          redo A;          redo A;
1522        } elsif ($self->{next_input_character} eq 0x0027) { # '        } elsif ($self->{next_input_character} eq 0x0027) { # '
1523          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1524          $self->{state} = 'DOCTYPE public identifier (single-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
1525          !!!next-input-character;          !!!next-input-character;
1526          redo A;          redo A;
1527        } elsif ($self->{next_input_character} eq 0x003E) { # >        } elsif ($self->{next_input_character} eq 0x003E) { # >
1528          !!!parse-error (type => 'no PUBLIC literal');          !!!parse-error (type => 'no PUBLIC literal');
1529    
1530          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1531          !!!next-input-character;          !!!next-input-character;
1532    
1533          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1323  sub _get_next_token ($) { Line 1537  sub _get_next_token ($) {
1537        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1538          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1539    
1540          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1541          ## reconsume          ## reconsume
1542    
1543          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1332  sub _get_next_token ($) { Line 1546  sub _get_next_token ($) {
1546          redo A;          redo A;
1547        } else {        } else {
1548          !!!parse-error (type => 'string after PUBLIC');          !!!parse-error (type => 'string after PUBLIC');
1549          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1550          !!!next-input-character;          !!!next-input-character;
1551          redo A;          redo A;
1552        }        }
1553      } elsif ($self->{state} eq 'DOCTYPE public identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
1554        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_input_character} == 0x0022) { # "
1555          $self->{state} = 'after DOCTYPE public identifier';          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1556          !!!next-input-character;          !!!next-input-character;
1557          redo A;          redo A;
1558          } elsif ($self->{next_input_character} == 0x003E) { # >
1559            !!!parse-error (type => 'unclosed PUBLIC literal');
1560    
1561            $self->{state} = DATA_STATE;
1562            !!!next-input-character;
1563    
1564            delete $self->{current_token}->{correct};
1565            !!!emit ($self->{current_token}); # DOCTYPE
1566    
1567            redo A;
1568        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1569          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
1570    
1571          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1572          ## reconsume          ## reconsume
1573    
1574          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1358  sub _get_next_token ($) { Line 1582  sub _get_next_token ($) {
1582          !!!next-input-character;          !!!next-input-character;
1583          redo A;          redo A;
1584        }        }
1585      } elsif ($self->{state} eq 'DOCTYPE public identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
1586        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_input_character} == 0x0027) { # '
1587          $self->{state} = 'after DOCTYPE public identifier';          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1588          !!!next-input-character;          !!!next-input-character;
1589          redo A;          redo A;
1590          } elsif ($self->{next_input_character} == 0x003E) { # >
1591            !!!parse-error (type => 'unclosed PUBLIC literal');
1592    
1593            $self->{state} = DATA_STATE;
1594            !!!next-input-character;
1595    
1596            delete $self->{current_token}->{correct};
1597            !!!emit ($self->{current_token}); # DOCTYPE
1598    
1599            redo A;
1600        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1601          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
1602    
1603          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1604          ## reconsume          ## reconsume
1605    
1606          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1380  sub _get_next_token ($) { Line 1614  sub _get_next_token ($) {
1614          !!!next-input-character;          !!!next-input-character;
1615          redo A;          redo A;
1616        }        }
1617      } elsif ($self->{state} eq 'after DOCTYPE public identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
1618        if ({        if ({
1619              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1620              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1390  sub _get_next_token ($) { Line 1624  sub _get_next_token ($) {
1624          redo A;          redo A;
1625        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_input_character} == 0x0022) { # "
1626          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1627          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
1628          !!!next-input-character;          !!!next-input-character;
1629          redo A;          redo A;
1630        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_input_character} == 0x0027) { # '
1631          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1632          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
1633          !!!next-input-character;          !!!next-input-character;
1634          redo A;          redo A;
1635        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1636          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1637          !!!next-input-character;          !!!next-input-character;
1638    
1639          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1408  sub _get_next_token ($) { Line 1642  sub _get_next_token ($) {
1642        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1643          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1644    
1645          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1646          ## recomsume          ## reconsume
1647    
1648          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
1649          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1417  sub _get_next_token ($) { Line 1651  sub _get_next_token ($) {
1651          redo A;          redo A;
1652        } else {        } else {
1653          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after PUBLIC literal');
1654          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1655          !!!next-input-character;          !!!next-input-character;
1656          redo A;          redo A;
1657        }        }
1658      } elsif ($self->{state} eq 'before DOCTYPE system identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
1659        if ({        if ({
1660              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1661              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1431  sub _get_next_token ($) { Line 1665  sub _get_next_token ($) {
1665          redo A;          redo A;
1666        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_input_character} == 0x0022) { # "
1667          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1668          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
1669          !!!next-input-character;          !!!next-input-character;
1670          redo A;          redo A;
1671        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_input_character} == 0x0027) { # '
1672          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1673          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
1674          !!!next-input-character;          !!!next-input-character;
1675          redo A;          redo A;
1676        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1677          !!!parse-error (type => 'no SYSTEM literal');          !!!parse-error (type => 'no SYSTEM literal');
1678          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1679          !!!next-input-character;          !!!next-input-character;
1680    
1681          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1451  sub _get_next_token ($) { Line 1685  sub _get_next_token ($) {
1685        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1686          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1687    
1688          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1689          ## recomsume          ## reconsume
1690    
1691          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
1692          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1693    
1694          redo A;          redo A;
1695        } else {        } else {
1696          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after SYSTEM');
1697          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1698          !!!next-input-character;          !!!next-input-character;
1699          redo A;          redo A;
1700        }        }
1701      } elsif ($self->{state} eq 'DOCTYPE system identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
1702        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_input_character} == 0x0022) { # "
1703          $self->{state} = 'after DOCTYPE system identifier';          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1704          !!!next-input-character;          !!!next-input-character;
1705          redo A;          redo A;
1706          } elsif ($self->{next_input_character} == 0x003E) { # >
1707            !!!parse-error (type => 'unclosed PUBLIC literal');
1708    
1709            $self->{state} = DATA_STATE;
1710            !!!next-input-character;
1711    
1712            delete $self->{current_token}->{correct};
1713            !!!emit ($self->{current_token}); # DOCTYPE
1714    
1715            redo A;
1716        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1717          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
1718    
1719          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1720          ## reconsume          ## reconsume
1721    
1722          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1486  sub _get_next_token ($) { Line 1730  sub _get_next_token ($) {
1730          !!!next-input-character;          !!!next-input-character;
1731          redo A;          redo A;
1732        }        }
1733      } elsif ($self->{state} eq 'DOCTYPE system identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
1734        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_input_character} == 0x0027) { # '
1735          $self->{state} = 'after DOCTYPE system identifier';          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1736          !!!next-input-character;          !!!next-input-character;
1737          redo A;          redo A;
1738          } elsif ($self->{next_input_character} == 0x003E) { # >
1739            !!!parse-error (type => 'unclosed PUBLIC literal');
1740    
1741            $self->{state} = DATA_STATE;
1742            !!!next-input-character;
1743    
1744            delete $self->{current_token}->{correct};
1745            !!!emit ($self->{current_token}); # DOCTYPE
1746    
1747            redo A;
1748        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1749          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
1750    
1751          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1752          ## reconsume          ## reconsume
1753    
1754          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1508  sub _get_next_token ($) { Line 1762  sub _get_next_token ($) {
1762          !!!next-input-character;          !!!next-input-character;
1763          redo A;          redo A;
1764        }        }
1765      } elsif ($self->{state} eq 'after DOCTYPE system identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
1766        if ({        if ({
1767              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1768              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1517  sub _get_next_token ($) { Line 1771  sub _get_next_token ($) {
1771          !!!next-input-character;          !!!next-input-character;
1772          redo A;          redo A;
1773        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1774          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1775          !!!next-input-character;          !!!next-input-character;
1776    
1777          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1526  sub _get_next_token ($) { Line 1780  sub _get_next_token ($) {
1780        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1781          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1782    
1783          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1784          ## recomsume          ## reconsume
1785    
1786          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
1787          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1535  sub _get_next_token ($) { Line 1789  sub _get_next_token ($) {
1789          redo A;          redo A;
1790        } else {        } else {
1791          !!!parse-error (type => 'string after SYSTEM literal');          !!!parse-error (type => 'string after SYSTEM literal');
1792          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1793          !!!next-input-character;          !!!next-input-character;
1794          redo A;          redo A;
1795        }        }
1796      } elsif ($self->{state} eq 'bogus DOCTYPE') {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
1797        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_input_character} == 0x003E) { # >
1798          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1799          !!!next-input-character;          !!!next-input-character;
1800    
1801          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1550  sub _get_next_token ($) { Line 1804  sub _get_next_token ($) {
1804          redo A;          redo A;
1805        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1806          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1807          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1808          ## reconsume          ## reconsume
1809    
1810          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1570  sub _get_next_token ($) { Line 1824  sub _get_next_token ($) {
1824    die "$0: _get_next_token: unexpected case";    die "$0: _get_next_token: unexpected case";
1825  } # _get_next_token  } # _get_next_token
1826    
1827  sub _tokenize_attempt_to_consume_an_entity ($) {  sub _tokenize_attempt_to_consume_an_entity ($$) {
1828    my $self = shift;    my ($self, $in_attr) = @_;
1829    
1830    if ({    if ({
1831         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
# Line 1584  sub _tokenize_attempt_to_consume_an_enti Line 1838  sub _tokenize_attempt_to_consume_an_enti
1838      !!!next-input-character;      !!!next-input-character;
1839      if ($self->{next_input_character} == 0x0078 or # x      if ($self->{next_input_character} == 0x0078 or # x
1840          $self->{next_input_character} == 0x0058) { # X          $self->{next_input_character} == 0x0058) { # X
1841        my $num;        my $code;
1842        X: {        X: {
1843          my $x_char = $self->{next_input_character};          my $x_char = $self->{next_input_character};
1844          !!!next-input-character;          !!!next-input-character;
1845          if (0x0030 <= $self->{next_input_character} and          if (0x0030 <= $self->{next_input_character} and
1846              $self->{next_input_character} <= 0x0039) { # 0..9              $self->{next_input_character} <= 0x0039) { # 0..9
1847            $num ||= 0;            $code ||= 0;
1848            $num *= 0x10;            $code *= 0x10;
1849            $num += $self->{next_input_character} - 0x0030;            $code += $self->{next_input_character} - 0x0030;
1850            redo X;            redo X;
1851          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_input_character} and
1852                   $self->{next_input_character} <= 0x0066) { # a..f                   $self->{next_input_character} <= 0x0066) { # a..f
1853            ## ISSUE: the spec says U+0078, which is apparently incorrect            $code ||= 0;
1854            $num ||= 0;            $code *= 0x10;
1855            $num *= 0x10;            $code += $self->{next_input_character} - 0x0060 + 9;
           $num += $self->{next_input_character} - 0x0060 + 9;  
1856            redo X;            redo X;
1857          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_input_character} and
1858                   $self->{next_input_character} <= 0x0046) { # A..F                   $self->{next_input_character} <= 0x0046) { # A..F
1859            ## ISSUE: the spec says U+0058, which is apparently incorrect            $code ||= 0;
1860            $num ||= 0;            $code *= 0x10;
1861            $num *= 0x10;            $code += $self->{next_input_character} - 0x0040 + 9;
           $num += $self->{next_input_character} - 0x0040 + 9;  
1862            redo X;            redo X;
1863          } elsif (not defined $num) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
1864            !!!parse-error (type => 'bare hcro');            !!!parse-error (type => 'bare hcro');
1865              !!!back-next-input-character ($x_char, $self->{next_input_character});
1866            $self->{next_input_character} = 0x0023; # #            $self->{next_input_character} = 0x0023; # #
           !!!back-next-input-character ($x_char);  
1867            return undef;            return undef;
1868          } elsif ($self->{next_input_character} == 0x003B) { # ;          } elsif ($self->{next_input_character} == 0x003B) { # ;
1869            !!!next-input-character;            !!!next-input-character;
# Line 1619  sub _tokenize_attempt_to_consume_an_enti Line 1871  sub _tokenize_attempt_to_consume_an_enti
1871            !!!parse-error (type => 'no refc');            !!!parse-error (type => 'no refc');
1872          }          }
1873    
1874          ## TODO: check the definition for |a valid Unicode character|.          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
1875          ## <http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2006-December/thread.html#8189>            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);
1876          if ($num > 1114111 or $num == 0) {            $code = 0xFFFD;
1877            $num = 0xFFFD; # REPLACEMENT CHARACTER          } elsif ($code > 0x10FFFF) {
1878            ## ISSUE: Why this is not an error?            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);
1879          } elsif (0x80 <= $num and $num <= 0x9F) {            $code = 0xFFFD;
1880            !!!parse-error (type => sprintf 'c1 entity:U+%04X', $num);          } elsif ($code == 0x000D) {
1881            $num = $c1_entity_char->{$num};            !!!parse-error (type => 'CR character reference');
1882              $code = 0x000A;
1883            } elsif (0x80 <= $code and $code <= 0x9F) {
1884              !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);
1885              $code = $c1_entity_char->{$code};
1886          }          }
1887    
1888          return {type => 'character', data => chr $num};          return {type => CHARACTER_TOKEN, data => chr $code,
1889                    has_reference => 1};
1890        } # X        } # X
1891      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_input_character} and
1892               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_input_character} <= 0x0039) { # 0..9
# Line 1650  sub _tokenize_attempt_to_consume_an_enti Line 1907  sub _tokenize_attempt_to_consume_an_enti
1907          !!!parse-error (type => 'no refc');          !!!parse-error (type => 'no refc');
1908        }        }
1909    
1910        ## TODO: check the definition for |a valid Unicode character|.        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
1911        if ($code > 1114111 or $code == 0) {          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);
1912          $code = 0xFFFD; # REPLACEMENT CHARACTER          $code = 0xFFFD;
1913          ## ISSUE: Why this is not an error?        } elsif ($code > 0x10FFFF) {
1914            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);
1915            $code = 0xFFFD;
1916          } elsif ($code == 0x000D) {
1917            !!!parse-error (type => 'CR character reference');
1918            $code = 0x000A;
1919        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
1920          !!!parse-error (type => sprintf 'c1 entity:U+%04X', $code);          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);
1921          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
1922        }        }
1923                
1924        return {type => 'character', data => chr $code};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1};
1925      } else {      } else {
1926        !!!parse-error (type => 'bare nero');        !!!parse-error (type => 'bare nero');
1927        !!!back-next-input-character ($self->{next_input_character});        !!!back-next-input-character ($self->{next_input_character});
# Line 1674  sub _tokenize_attempt_to_consume_an_enti Line 1936  sub _tokenize_attempt_to_consume_an_enti
1936      !!!next-input-character;      !!!next-input-character;
1937    
1938      my $value = $entity_name;      my $value = $entity_name;
1939      my $match;      my $match = 0;
1940      require Whatpm::_NamedEntityList;      require Whatpm::_NamedEntityList;
1941      our $EntityChar;      our $EntityChar;
1942    
# Line 1689  sub _tokenize_attempt_to_consume_an_enti Line 1951  sub _tokenize_attempt_to_consume_an_enti
1951              $self->{next_input_character} == 0x003B)) { # ;              $self->{next_input_character} == 0x003B)) { # ;
1952        $entity_name .= chr $self->{next_input_character};        $entity_name .= chr $self->{next_input_character};
1953        if (defined $EntityChar->{$entity_name}) {        if (defined $EntityChar->{$entity_name}) {
         $value = $EntityChar->{$entity_name};  
1954          if ($self->{next_input_character} == 0x003B) { # ;          if ($self->{next_input_character} == 0x003B) { # ;
1955              $value = $EntityChar->{$entity_name};
1956            $match = 1;            $match = 1;
1957            !!!next-input-character;            !!!next-input-character;
1958            last;            last;
1959          } else {          } else {
1960              $value = $EntityChar->{$entity_name};
1961            $match = -1;            $match = -1;
1962              !!!next-input-character;
1963          }          }
1964        } else {        } else {
1965          $value .= chr $self->{next_input_character};          $value .= chr $self->{next_input_character};
1966            $match *= 2;
1967            !!!next-input-character;
1968        }        }
       !!!next-input-character;  
1969      }      }
1970            
1971      if ($match > 0) {      if ($match > 0) {
1972        return {type => 'character', data => $value};        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};
1973      } elsif ($match < 0) {      } elsif ($match < 0) {
1974        !!!parse-error (type => 'refc');        !!!parse-error (type => 'no refc');
1975        return {type => 'character', data => $value};        if ($in_attr and $match < -1) {
1976            return {type => CHARACTER_TOKEN, data => '&'.$entity_name};
1977          } else {
1978            return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};
1979          }
1980      } else {      } else {
1981        !!!parse-error (type => 'bare ero');        !!!parse-error (type => 'bare ero');
1982        ## NOTE: No characters are consumed in the spec.        ## NOTE: "No characters are consumed" in the spec.
1983        !!!back-token ({type => 'character', data => $value});        return {type => CHARACTER_TOKEN, data => '&'.$value};
       return undef;  
1984      }      }
1985    } else {    } else {
1986      ## no characters are consumed      ## no characters are consumed
# Line 1754  sub _construct_tree ($) { Line 2022  sub _construct_tree ($) {
2022        
2023    !!!next-token;    !!!next-token;
2024    
2025    $self->{insertion_mode} = 'before head';    $self->{insertion_mode} = BEFORE_HEAD_IM;
2026    undef $self->{form_element};    undef $self->{form_element};
2027    undef $self->{head_element};    undef $self->{head_element};
2028    $self->{open_elements} = [];    $self->{open_elements} = [];
# Line 1768  sub _construct_tree ($) { Line 2036  sub _construct_tree ($) {
2036  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
2037    my $self = shift;    my $self = shift;
2038    INITIAL: {    INITIAL: {
2039      if ($token->{type} eq 'DOCTYPE') {      if ($token->{type} == DOCTYPE_TOKEN) {
2040        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
2041        ## error, switch to a conformance checking mode for another        ## error, switch to a conformance checking mode for another
2042        ## language.        ## language.
# Line 1895  sub _tree_construction_initial ($) { Line 2163  sub _tree_construction_initial ($) {
2163        !!!next-token;        !!!next-token;
2164        return;        return;
2165      } elsif ({      } elsif ({
2166                'start tag' => 1,                START_TAG_TOKEN, 1,
2167                'end tag' => 1,                END_TAG_TOKEN, 1,
2168                'end-of-file' => 1,                END_OF_FILE_TOKEN, 1,
2169               }->{$token->{type}}) {               }->{$token->{type}}) {
2170        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE');
2171        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
2172        ## Go to the root element phase        ## Go to the root element phase
2173        ## reprocess        ## reprocess
2174        return;        return;
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    
2179          unless (length $token->{data}) {          unless (length $token->{data}) {
2180            ## Stay in the phase            ## Stay in the phase
2181            !!!next-token;            !!!next-token;
# Line 1919  sub _tree_construction_initial ($) { Line 2188  sub _tree_construction_initial ($) {
2188        ## Go to the root element phase        ## Go to the root element phase
2189        ## reprocess        ## reprocess
2190        return;        return;
2191      } elsif ($token->{type} eq 'comment') {      } elsif ($token->{type} == COMMENT_TOKEN) {
2192        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
2193        $self->{document}->append_child ($comment);        $self->{document}->append_child ($comment);
2194                
# Line 1927  sub _tree_construction_initial ($) { Line 2196  sub _tree_construction_initial ($) {
2196        !!!next-token;        !!!next-token;
2197        redo INITIAL;        redo INITIAL;
2198      } else {      } else {
2199        die "$0: $token->{type}: Unknown token";        die "$0: $token->{type}: Unknown token type";
2200      }      }
2201    } # INITIAL    } # INITIAL
2202  } # _tree_construction_initial  } # _tree_construction_initial
# Line 1936  sub _tree_construction_root_element ($) Line 2205  sub _tree_construction_root_element ($)
2205    my $self = shift;    my $self = shift;
2206        
2207    B: {    B: {
2208        if ($token->{type} eq 'DOCTYPE') {        if ($token->{type} == DOCTYPE_TOKEN) {
2209          !!!parse-error (type => 'in html:#DOCTYPE');          !!!parse-error (type => 'in html:#DOCTYPE');
2210          ## Ignore the token          ## Ignore the token
2211          ## Stay in the phase          ## Stay in the phase
2212          !!!next-token;          !!!next-token;
2213          redo B;          redo B;
2214        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{type} == COMMENT_TOKEN) {
2215          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
2216          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
2217          ## Stay in the phase          ## Stay in the phase
2218          !!!next-token;          !!!next-token;
2219          redo B;          redo B;
2220        } elsif ($token->{type} eq 'character') {        } elsif ($token->{type} == CHARACTER_TOKEN) {
2221          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2222            $self->{document}->manakai_append_text ($1);            ## Ignore the token.
2223            ## ISSUE: DOM3 Core does not allow Document > Text  
2224            unless (length $token->{data}) {            unless (length $token->{data}) {
2225              ## Stay in the phase              ## Stay in the phase
2226              !!!next-token;              !!!next-token;
2227              redo B;              redo B;
2228            }            }
2229          }          }
2230    
2231            $self->{application_cache_selection}->(undef);
2232    
2233            #
2234          } elsif ($token->{type} == START_TAG_TOKEN) {
2235            if ($token->{tag_name} eq 'html' and
2236                $token->{attributes}->{manifest}) {
2237              $self->{application_cache_selection}
2238                   ->($token->{attributes}->{manifest}->{value});
2239              ## ISSUE: No relative reference resolution?
2240            } else {
2241              $self->{application_cache_selection}->(undef);
2242            }
2243    
2244            ## ISSUE: There is an issue in the spec
2245          #          #
2246        } elsif ({        } elsif ({
2247                  'start tag' => 1,                  END_TAG_TOKEN, 1,
2248                  'end tag' => 1,                  END_OF_FILE_TOKEN, 1,
                 'end-of-file' => 1,  
2249                 }->{$token->{type}}) {                 }->{$token->{type}}) {
2250            $self->{application_cache_selection}->(undef);
2251    
2252          ## ISSUE: There is an issue in the spec          ## ISSUE: There is an issue in the spec
2253          #          #
2254        } else {        } else {
2255          die "$0: $token->{type}: Unknown token";          die "$0: $token->{type}: Unknown token type";
2256        }        }
2257    
2258        my $root_element; !!!create-element ($root_element, 'html');        my $root_element; !!!create-element ($root_element, 'html');
2259        $self->{document}->append_child ($root_element);        $self->{document}->append_child ($root_element);
2260        push @{$self->{open_elements}}, [$root_element, 'html'];        push @{$self->{open_elements}}, [$root_element, 'html'];
       #$phase = 'main';  
2261        ## reprocess        ## reprocess
2262        #redo B;        #redo B;
2263        return;        return; ## Go to the main phase.
2264    } # B    } # B
2265  } # _tree_construction_root_element  } # _tree_construction_root_element
2266    
# Line 1991  sub _reset_insertion_mode ($) { Line 2276  sub _reset_insertion_mode ($) {
2276            
2277      ## Step 3      ## Step 3
2278      S3: {      S3: {
2279        $last = 1 if $self->{open_elements}->[0]->[0] eq $node->[0];        ## ISSUE: Oops! "If node is the first node in the stack of open
2280        if (defined $self->{inner_html_node}) {        ## elements, then set last to true. If the context element of the
2281          if ($self->{inner_html_node}->[1] eq 'td' or        ## HTML fragment parsing algorithm is neither a td element nor a
2282              $self->{inner_html_node}->[1] eq 'th') {        ## th element, then set node to the context element. (fragment case)":
2283            #        ## The second "if" is in the scope of the first "if"!?
2284          } else {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
2285            $node = $self->{inner_html_node};          $last = 1;
2286            if (defined $self->{inner_html_node}) {
2287              if ($self->{inner_html_node}->[1] eq 'td' or
2288                  $self->{inner_html_node}->[1] eq 'th') {
2289                #
2290              } else {
2291                $node = $self->{inner_html_node};
2292              }
2293          }          }
2294        }        }
2295            
2296        ## Step 4..13        ## Step 4..13
2297        my $new_mode = {        my $new_mode = {
2298                        select => 'in select',                        select => IN_SELECT_IM,
2299                        td => 'in cell',                        td => IN_CELL_IM,
2300                        th => 'in cell',                        th => IN_CELL_IM,
2301                        tr => 'in row',                        tr => IN_ROW_IM,
2302                        tbody => 'in table body',                        tbody => IN_TABLE_BODY_IM,
2303                        thead => 'in table head',                        thead => IN_TABLE_BODY_IM,
2304                        tfoot => 'in table foot',                        tfoot => IN_TABLE_BODY_IM,
2305                        caption => 'in caption',                        caption => IN_CAPTION_IM,
2306                        colgroup => 'in column group',                        colgroup => IN_COLUMN_GROUP_IM,
2307                        table => 'in table',                        table => IN_TABLE_IM,
2308                        head => 'in body', # not in head!                        head => IN_BODY_IM, # not in head!
2309                        body => 'in body',                        body => IN_BODY_IM,
2310                        frameset => 'in frameset',                        frameset => IN_FRAMESET_IM,
2311                       }->{$node->[1]};                       }->{$node->[1]};
2312        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
2313                
2314        ## Step 14        ## Step 14
2315        if ($node->[1] eq 'html') {        if ($node->[1] eq 'html') {
2316          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
2317            $self->{insertion_mode} = 'before head';            $self->{insertion_mode} = BEFORE_HEAD_IM;
2318          } else {          } else {
2319            $self->{insertion_mode} = 'after head';            $self->{insertion_mode} = AFTER_HEAD_IM;
2320          }          }
2321          return;          return;
2322        }        }
2323                
2324        ## Step 15        ## Step 15
2325        $self->{insertion_mode} = 'in body' and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
2326                
2327        ## Step 16        ## Step 16
2328        $i--;        $i--;
# Line 2044  sub _reset_insertion_mode ($) { Line 2336  sub _reset_insertion_mode ($) {
2336  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
2337    my $self = shift;    my $self = shift;
2338    
   my $phase = 'main';  
   
2339    my $active_formatting_elements = [];    my $active_formatting_elements = [];
2340    
2341    my $reconstruct_active_formatting_elements = sub { # MUST    my $reconstruct_active_formatting_elements = sub { # MUST
# Line 2140  sub _tree_construction_main ($) { Line 2430  sub _tree_construction_main ($) {
2430      $insert->($el); # /context node/->append_child ($el)      $insert->($el); # /context node/->append_child ($el)
2431    
2432      ## Step 3      ## Step 3
2433      $self->{content_model_flag} = $content_model_flag; # CDATA or RCDATA      $self->{content_model} = $content_model_flag; # CDATA or RCDATA
2434      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
2435    
2436      ## Step 4      ## Step 4
2437      my $text = '';      my $text = '';
2438      !!!next-token;      !!!next-token;
2439      while ($token->{type} eq 'character') { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
2440        $text .= $token->{data};        $text .= $token->{data};
2441        !!!next-token;        !!!next-token;
2442      }      }
# Line 2158  sub _tree_construction_main ($) { Line 2448  sub _tree_construction_main ($) {
2448      }      }
2449    
2450      ## Step 6      ## Step 6
2451      $self->{content_model_flag} = 'PCDATA';      $self->{content_model} = PCDATA_CONTENT_MODEL;
2452    
2453      ## Step 7      ## Step 7
2454      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) {
2455        ## Ignore the token        ## Ignore the token
2456        } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {
2457          !!!parse-error (type => 'in CDATA:#'.$token->{type});
2458        } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
2459          !!!parse-error (type => 'in RCDATA:#'.$token->{type});
2460      } else {      } else {
2461        !!!parse-error (type => 'in '.$content_model_flag.':#'.$token->{type});        die "$0: $content_model_flag in parse_rcdata";
2462      }      }
2463      !!!next-token;      !!!next-token;
2464    }; # $parse_rcdata    }; # $parse_rcdata
# Line 2175  sub _tree_construction_main ($) { Line 2469  sub _tree_construction_main ($) {
2469      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, 'script', $token->{attributes});
2470      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
2471    
2472      $self->{content_model_flag} = 'CDATA';      $self->{content_model} = CDATA_CONTENT_MODEL;
2473      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
2474            
2475      my $text = '';      my $text = '';
2476      !!!next-token;      !!!next-token;
2477      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) {
2478        $text .= $token->{data};        $text .= $token->{data};
2479        !!!next-token;        !!!next-token;
2480      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
# Line 2188  sub _tree_construction_main ($) { Line 2482  sub _tree_construction_main ($) {
2482        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
2483      }      }
2484                                
2485      $self->{content_model_flag} = 'PCDATA';      $self->{content_model} = PCDATA_CONTENT_MODEL;
2486    
2487      if ($token->{type} eq 'end tag' and      if ($token->{type} == END_TAG_TOKEN and
2488          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
2489        ## Ignore the token        ## Ignore the token
2490      } else {      } else {
# Line 2433  sub _tree_construction_main ($) { Line 2727  sub _tree_construction_main ($) {
2727                         }                         }
2728    }; # $insert_to_foster    }; # $insert_to_foster
2729    
2730    my $in_body = sub {    my $insert;
2731      my $insert = shift;  
2732      if ($token->{type} eq 'start tag') {    B: {
2733        if ($token->{type} == DOCTYPE_TOKEN) {
2734          !!!parse-error (type => 'DOCTYPE in the middle');
2735          ## Ignore the token
2736          ## Stay in the phase
2737          !!!next-token;
2738          redo B;
2739        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
2740          if ($self->{insertion_mode} & AFTER_HTML_IMS) {
2741            #
2742          } else {
2743            ## Generate implied end tags
2744            if ({
2745                 dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,
2746                 tbody => 1, tfoot=> 1, thead => 1,
2747                }->{$self->{open_elements}->[-1]->[1]}) {
2748              !!!back-token;
2749              $token = {type => END_TAG_TOKEN, tag_name => $self->{open_elements}->[-1]->[1]};
2750              redo B;
2751            }
2752            
2753            if (@{$self->{open_elements}} > 2 or
2754                (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {
2755              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
2756            } elsif (defined $self->{inner_html_node} and
2757                     @{$self->{open_elements}} > 1 and
2758                     $self->{open_elements}->[1]->[1] ne 'body') {
2759              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
2760            }
2761    
2762            ## ISSUE: There is an issue in the spec.
2763          }
2764    
2765          ## Stop parsing
2766          last B;
2767        } elsif ($token->{type} == START_TAG_TOKEN and
2768                 $token->{tag_name} eq 'html') {
2769          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
2770            ## Turn into the main phase
2771            !!!parse-error (type => 'after html:html');
2772            $self->{insertion_mode} = AFTER_BODY_IM;
2773          } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
2774            ## Turn into the main phase
2775            !!!parse-error (type => 'after html:html');
2776            $self->{insertion_mode} = AFTER_FRAMESET_IM;
2777          }
2778    
2779    ## ISSUE: "aa<html>" is not a parse error.
2780    ## ISSUE: "<html>" in fragment is not a parse error.
2781          unless ($token->{first_start_tag}) {
2782            !!!parse-error (type => 'not first start tag');
2783          }
2784          my $top_el = $self->{open_elements}->[0]->[0];
2785          for my $attr_name (keys %{$token->{attributes}}) {
2786            unless ($top_el->has_attribute_ns (undef, $attr_name)) {
2787              $top_el->set_attribute_ns
2788                (undef, [undef, $attr_name],
2789                 $token->{attributes}->{$attr_name}->{value});
2790            }
2791          }
2792          !!!next-token;
2793          redo B;
2794        } elsif ($token->{type} == COMMENT_TOKEN) {
2795          my $comment = $self->{document}->create_comment ($token->{data});
2796          if ($self->{insertion_mode} & AFTER_HTML_IMS) {
2797            $self->{document}->append_child ($comment);
2798          } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
2799            $self->{open_elements}->[0]->[0]->append_child ($comment);
2800          } else {
2801            $self->{open_elements}->[-1]->[0]->append_child ($comment);
2802          }
2803          !!!next-token;
2804          redo B;
2805        } elsif ($self->{insertion_mode} & HEAD_IMS) {
2806          if ($token->{type} == CHARACTER_TOKEN) {
2807            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
2808              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
2809              unless (length $token->{data}) {
2810                !!!next-token;
2811                redo B;
2812              }
2813            }
2814    
2815            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2816              ## As if <head>
2817              !!!create-element ($self->{head_element}, 'head');
2818              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2819              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2820    
2821              ## Reprocess in the "in head" insertion mode...
2822              pop @{$self->{open_elements}};
2823    
2824              ## Reprocess in the "after head" insertion mode...
2825            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2826              ## As if </noscript>
2827              pop @{$self->{open_elements}};
2828              !!!parse-error (type => 'in noscript:#character');
2829              
2830              ## Reprocess in the "in head" insertion mode...
2831              ## As if </head>
2832              pop @{$self->{open_elements}};
2833    
2834              ## Reprocess in the "after head" insertion mode...
2835            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2836              pop @{$self->{open_elements}};
2837    
2838              ## Reprocess in the "after head" insertion mode...
2839            }
2840    
2841                ## "after head" insertion mode
2842                ## As if <body>
2843                !!!insert-element ('body');
2844                $self->{insertion_mode} = IN_BODY_IM;
2845                ## reprocess
2846                redo B;
2847              } elsif ($token->{type} == START_TAG_TOKEN) {
2848                if ($token->{tag_name} eq 'head') {
2849                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2850                    !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});
2851                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2852                    push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];
2853                    $self->{insertion_mode} = IN_HEAD_IM;
2854                    !!!next-token;
2855                    redo B;
2856                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2857                    #
2858                  } else {
2859                    !!!parse-error (type => 'in head:head'); # or in head noscript
2860                    ## Ignore the token
2861                    !!!next-token;
2862                    redo B;
2863                  }
2864                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2865                  ## As if <head>
2866                  !!!create-element ($self->{head_element}, 'head');
2867                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2868                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2869    
2870                  $self->{insertion_mode} = IN_HEAD_IM;
2871                  ## Reprocess in the "in head" insertion mode...
2872                }
2873    
2874                if ($token->{tag_name} eq 'base') {
2875                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2876                    ## As if </noscript>
2877                    pop @{$self->{open_elements}};
2878                    !!!parse-error (type => 'in noscript:base');
2879                  
2880                    $self->{insertion_mode} = IN_HEAD_IM;
2881                    ## Reprocess in the "in head" insertion mode...
2882                  }
2883    
2884                  ## NOTE: There is a "as if in head" code clone.
2885                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2886                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2887                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2888                  }
2889                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2890                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2891                  pop @{$self->{open_elements}}
2892                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2893                  !!!next-token;
2894                  redo B;
2895                } elsif ($token->{tag_name} eq 'link') {
2896                  ## NOTE: There is a "as if in head" code clone.
2897                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2898                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2899                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2900                  }
2901                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2902                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2903                  pop @{$self->{open_elements}}
2904                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2905                  !!!next-token;
2906                  redo B;
2907                } elsif ($token->{tag_name} eq 'meta') {
2908                  ## NOTE: There is a "as if in head" code clone.
2909                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2910                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2911                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2912                  }
2913                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2914                  my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2915    
2916                  unless ($self->{confident}) {
2917                    if ($token->{attributes}->{charset}) { ## TODO: And if supported
2918                      $self->{change_encoding}
2919                          ->($self, $token->{attributes}->{charset}->{value});
2920                      
2921                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
2922                          ->set_user_data (manakai_has_reference =>
2923                                               $token->{attributes}->{charset}
2924                                                   ->{has_reference});
2925                    } elsif ($token->{attributes}->{content}) {
2926                      ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
2927                      if ($token->{attributes}->{content}->{value}
2928                          =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=
2929                              [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
2930                              ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
2931                        $self->{change_encoding}
2932                            ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);
2933                        $meta_el->[0]->get_attribute_node_ns (undef, 'content')
2934                            ->set_user_data (manakai_has_reference =>
2935                                                 $token->{attributes}->{content}
2936                                                       ->{has_reference});
2937                      }
2938                    }
2939                  } else {
2940                    if ($token->{attributes}->{charset}) {
2941                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
2942                          ->set_user_data (manakai_has_reference =>
2943                                               $token->{attributes}->{charset}
2944                                                   ->{has_reference});
2945                    }
2946                    if ($token->{attributes}->{content}) {
2947                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
2948                          ->set_user_data (manakai_has_reference =>
2949                                               $token->{attributes}->{content}
2950                                                   ->{has_reference});
2951                    }
2952                  }
2953    
2954                  pop @{$self->{open_elements}}
2955                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2956                  !!!next-token;
2957                  redo B;
2958                } elsif ($token->{tag_name} eq 'title') {
2959                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2960                    ## As if </noscript>
2961                    pop @{$self->{open_elements}};
2962                    !!!parse-error (type => 'in noscript:title');
2963                  
2964                    $self->{insertion_mode} = IN_HEAD_IM;
2965                    ## Reprocess in the "in head" insertion mode...
2966                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2967                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2968                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2969                  }
2970    
2971                  ## NOTE: There is a "as if in head" code clone.
2972                  my $parent = defined $self->{head_element} ? $self->{head_element}
2973                      : $self->{open_elements}->[-1]->[0];
2974                  $parse_rcdata->(RCDATA_CONTENT_MODEL,
2975                                  sub { $parent->append_child ($_[0]) });
2976                  pop @{$self->{open_elements}}
2977                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2978                  redo B;
2979                } elsif ($token->{tag_name} eq 'style') {
2980                  ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
2981                  ## insertion mode IN_HEAD_IM)
2982                  ## NOTE: There is a "as if in head" code clone.
2983                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2984                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2985                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2986                  }
2987                  $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
2988                  pop @{$self->{open_elements}}
2989                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2990                  redo B;
2991                } elsif ($token->{tag_name} eq 'noscript') {
2992                  if ($self->{insertion_mode} == IN_HEAD_IM) {
2993                    ## NOTE: and scripting is disalbed
2994                    !!!insert-element ($token->{tag_name}, $token->{attributes});
2995                    $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
2996                    !!!next-token;
2997                    redo B;
2998                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2999                    !!!parse-error (type => 'in noscript:noscript');
3000                    ## Ignore the token
3001                    !!!next-token;
3002                    redo B;
3003                  } else {
3004                    #
3005                  }
3006                } elsif ($token->{tag_name} eq 'script') {
3007                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3008                    ## As if </noscript>
3009                    pop @{$self->{open_elements}};
3010                    !!!parse-error (type => 'in noscript:script');
3011                  
3012                    $self->{insertion_mode} = IN_HEAD_IM;
3013                    ## Reprocess in the "in head" insertion mode...
3014                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3015                    !!!parse-error (type => 'after head:'.$token->{tag_name});
3016                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3017                  }
3018    
3019                  ## NOTE: There is a "as if in head" code clone.
3020                  $script_start_tag->($insert_to_current);
3021                  pop @{$self->{open_elements}}
3022                      if $self->{insertion_mode} == AFTER_HEAD_IM;
3023                  redo B;
3024                } elsif ($token->{tag_name} eq 'body' or
3025                         $token->{tag_name} eq 'frameset') {
3026                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3027                    ## As if </noscript>
3028                    pop @{$self->{open_elements}};
3029                    !!!parse-error (type => 'in noscript:'.$token->{tag_name});
3030                    
3031                    ## Reprocess in the "in head" insertion mode...
3032                    ## As if </head>
3033                    pop @{$self->{open_elements}};
3034                    
3035                    ## Reprocess in the "after head" insertion mode...
3036                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3037                    pop @{$self->{open_elements}};
3038                    
3039                    ## Reprocess in the "after head" insertion mode...
3040                  }
3041    
3042                  ## "after head" insertion mode
3043                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3044                  if ($token->{tag_name} eq 'body') {
3045                    $self->{insertion_mode} = IN_BODY_IM;
3046                  } elsif ($token->{tag_name} eq 'frameset') {
3047                    $self->{insertion_mode} = IN_FRAMESET_IM;
3048                  } else {
3049                    die "$0: tag name: $self->{tag_name}";
3050                  }
3051                  !!!next-token;
3052                  redo B;
3053                } else {
3054                  #
3055                }
3056    
3057                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3058                  ## As if </noscript>
3059                  pop @{$self->{open_elements}};
3060                  !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
3061                  
3062                  ## Reprocess in the "in head" insertion mode...
3063                  ## As if </head>
3064                  pop @{$self->{open_elements}};
3065    
3066                  ## Reprocess in the "after head" insertion mode...
3067                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3068                  ## As if </head>
3069                  pop @{$self->{open_elements}};
3070    
3071                  ## Reprocess in the "after head" insertion mode...
3072                }
3073    
3074                ## "after head" insertion mode
3075                ## As if <body>
3076                !!!insert-element ('body');
3077                $self->{insertion_mode} = IN_BODY_IM;
3078                ## reprocess
3079                redo B;
3080              } elsif ($token->{type} == END_TAG_TOKEN) {
3081                if ($token->{tag_name} eq 'head') {
3082                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3083                    ## As if <head>
3084                    !!!create-element ($self->{head_element}, 'head');
3085                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3086                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3087    
3088                    ## Reprocess in the "in head" insertion mode...
3089                    pop @{$self->{open_elements}};
3090                    $self->{insertion_mode} = AFTER_HEAD_IM;
3091                    !!!next-token;
3092                    redo B;
3093                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3094                    ## As if </noscript>
3095                    pop @{$self->{open_elements}};
3096                    !!!parse-error (type => 'in noscript:script');
3097                    
3098                    ## Reprocess in the "in head" insertion mode...
3099                    pop @{$self->{open_elements}};
3100                    $self->{insertion_mode} = AFTER_HEAD_IM;
3101                    !!!next-token;
3102                    redo B;
3103                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3104                    pop @{$self->{open_elements}};
3105                    $self->{insertion_mode} = AFTER_HEAD_IM;
3106                    !!!next-token;
3107                    redo B;
3108                  } else {
3109                    #
3110                  }
3111                } elsif ($token->{tag_name} eq 'noscript') {
3112                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3113                    pop @{$self->{open_elements}};
3114                    $self->{insertion_mode} = IN_HEAD_IM;
3115                    !!!next-token;
3116                    redo B;
3117                  } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3118                    !!!parse-error (type => 'unmatched end tag:noscript');
3119                    ## Ignore the token ## ISSUE: An issue in the spec.
3120                    !!!next-token;
3121                    redo B;
3122                  } else {
3123                    #
3124                  }
3125                } elsif ({
3126                          body => 1, html => 1,
3127                         }->{$token->{tag_name}}) {
3128                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3129                    ## As if <head>
3130                    !!!create-element ($self->{head_element}, 'head');
3131                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3132                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3133    
3134                    $self->{insertion_mode} = IN_HEAD_IM;
3135                    ## Reprocess in the "in head" insertion mode...
3136                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3137                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3138                    ## Ignore the token
3139                    !!!next-token;
3140                    redo B;
3141                  }
3142                  
3143                  #
3144                } elsif ({
3145                          p => 1, br => 1,
3146                         }->{$token->{tag_name}}) {
3147                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3148                    ## As if <head>
3149                    !!!create-element ($self->{head_element}, 'head');
3150                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3151                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3152    
3153                    $self->{insertion_mode} = IN_HEAD_IM;
3154                    ## Reprocess in the "in head" insertion mode...
3155                  }
3156    
3157                  #
3158                } else {
3159                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3160                    #
3161                  } else {
3162                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3163                    ## Ignore the token
3164                    !!!next-token;
3165                    redo B;
3166                  }
3167                }
3168    
3169                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3170                  ## As if </noscript>
3171                  pop @{$self->{open_elements}};
3172                  !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
3173                  
3174                  ## Reprocess in the "in head" insertion mode...
3175                  ## As if </head>
3176                  pop @{$self->{open_elements}};
3177    
3178                  ## Reprocess in the "after head" insertion mode...
3179                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3180                  ## As if </head>
3181                  pop @{$self->{open_elements}};
3182    
3183                  ## Reprocess in the "after head" insertion mode...
3184                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3185                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3186                  ## Ignore the token ## ISSUE: An issue in the spec.
3187                  !!!next-token;
3188                  redo B;
3189                }
3190    
3191                ## "after head" insertion mode
3192                ## As if <body>
3193                !!!insert-element ('body');
3194                $self->{insertion_mode} = IN_BODY_IM;
3195                ## reprocess
3196                redo B;
3197              } else {
3198                die "$0: $token->{type}: Unknown token type";
3199              }
3200    
3201              ## ISSUE: An issue in the spec.
3202        } elsif ($self->{insertion_mode} & BODY_IMS) {
3203              if ($token->{type} == CHARACTER_TOKEN) {
3204                ## NOTE: There is a code clone of "character in body".
3205                $reconstruct_active_formatting_elements->($insert_to_current);
3206                
3207                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3208    
3209                !!!next-token;
3210                redo B;
3211              } elsif ($token->{type} == START_TAG_TOKEN) {
3212                if ({
3213                     caption => 1, col => 1, colgroup => 1, tbody => 1,
3214                     td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
3215                    }->{$token->{tag_name}}) {
3216                  if ($self->{insertion_mode} == IN_CELL_IM) {
3217                    ## have an element in table scope
3218                    my $tn;
3219                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3220                      my $node = $self->{open_elements}->[$_];
3221                      if ($node->[1] eq 'td' or $node->[1] eq 'th') {
3222                        $tn = $node->[1];
3223                        last INSCOPE;
3224                      } elsif ({
3225                                table => 1, html => 1,
3226                               }->{$node->[1]}) {
3227                        last INSCOPE;
3228                      }
3229                    } # INSCOPE
3230                      unless (defined $tn) {
3231                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3232                        ## Ignore the token
3233                        !!!next-token;
3234                        redo B;
3235                      }
3236                    
3237                    ## Close the cell
3238                    !!!back-token; # <?>
3239                    $token = {type => END_TAG_TOKEN, tag_name => $tn};
3240                    redo B;
3241                  } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3242                    !!!parse-error (type => 'not closed:caption');
3243                    
3244                    ## As if </caption>
3245                    ## have a table element in table scope
3246                    my $i;
3247                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3248                      my $node = $self->{open_elements}->[$_];
3249                      if ($node->[1] eq 'caption') {
3250                        $i = $_;
3251                        last INSCOPE;
3252                      } elsif ({
3253                                table => 1, html => 1,
3254                               }->{$node->[1]}) {
3255                        last INSCOPE;
3256                      }
3257                    } # INSCOPE
3258                      unless (defined $i) {
3259                        !!!parse-error (type => 'unmatched end tag:caption');
3260                        ## Ignore the token
3261                        !!!next-token;
3262                        redo B;
3263                      }
3264                    
3265                    ## generate implied end tags
3266                    if ({
3267                         dd => 1, dt => 1, li => 1, p => 1,
3268                         td => 1, th => 1, tr => 1,
3269                         tbody => 1, tfoot=> 1, thead => 1,
3270                        }->{$self->{open_elements}->[-1]->[1]}) {
3271                      !!!back-token; # <?>
3272                      $token = {type => END_TAG_TOKEN, tag_name => 'caption'};
3273                      !!!back-token;
3274                      $token = {type => END_TAG_TOKEN,
3275                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3276                      redo B;
3277                    }
3278    
3279                    if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3280                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3281                    }
3282                    
3283                    splice @{$self->{open_elements}}, $i;
3284                    
3285                    $clear_up_to_marker->();
3286                    
3287                    $self->{insertion_mode} = IN_TABLE_IM;
3288                    
3289                    ## reprocess
3290                    redo B;
3291                  } else {
3292                    #
3293                  }
3294                } else {
3295                  #
3296                }
3297              } elsif ($token->{type} == END_TAG_TOKEN) {
3298                if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
3299                  if ($self->{insertion_mode} == IN_CELL_IM) {
3300                    ## have an element in table scope
3301                    my $i;
3302                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3303                      my $node = $self->{open_elements}->[$_];
3304                      if ($node->[1] eq $token->{tag_name}) {
3305                        $i = $_;
3306                        last INSCOPE;
3307                      } elsif ({
3308                                table => 1, html => 1,
3309                               }->{$node->[1]}) {
3310                        last INSCOPE;
3311                      }
3312                    } # INSCOPE
3313                      unless (defined $i) {
3314                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3315                        ## Ignore the token
3316                        !!!next-token;
3317                        redo B;
3318                      }
3319                    
3320                    ## generate implied end tags
3321                    if ({
3322                         dd => 1, dt => 1, li => 1, p => 1,
3323                         td => ($token->{tag_name} eq 'th'),
3324                         th => ($token->{tag_name} eq 'td'),
3325                         tr => 1,
3326                         tbody => 1, tfoot=> 1, thead => 1,
3327                        }->{$self->{open_elements}->[-1]->[1]}) {
3328                      !!!back-token;
3329                      $token = {type => END_TAG_TOKEN,
3330                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3331                      redo B;
3332                    }
3333                    
3334                    if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
3335                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3336                    }
3337                    
3338                    splice @{$self->{open_elements}}, $i;
3339                    
3340                    $clear_up_to_marker->();
3341                    
3342                    $self->{insertion_mode} = IN_ROW_IM;
3343                    
3344                    !!!next-token;
3345                    redo B;
3346                  } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3347                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3348                    ## Ignore the token
3349                    !!!next-token;
3350                    redo B;
3351                  } else {
3352                    #
3353                  }
3354                } elsif ($token->{tag_name} eq 'caption') {
3355                  if ($self->{insertion_mode} == IN_CAPTION_IM) {
3356                    ## have a table element in table scope
3357                    my $i;
3358                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3359                      my $node = $self->{open_elements}->[$_];
3360                      if ($node->[1] eq $token->{tag_name}) {
3361                        $i = $_;
3362                        last INSCOPE;
3363                      } elsif ({
3364                                table => 1, html => 1,
3365                               }->{$node->[1]}) {
3366                        last INSCOPE;
3367                      }
3368                    } # INSCOPE
3369                      unless (defined $i) {
3370                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3371                        ## Ignore the token
3372                        !!!next-token;
3373                        redo B;
3374                      }
3375                    
3376                    ## generate implied end tags
3377                    if ({
3378                         dd => 1, dt => 1, li => 1, p => 1,
3379                         td => 1, th => 1, tr => 1,
3380                         tbody => 1, tfoot=> 1, thead => 1,
3381                        }->{$self->{open_elements}->[-1]->[1]}) {
3382                      !!!back-token;
3383                      $token = {type => END_TAG_TOKEN,
3384                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3385                      redo B;
3386                    }
3387                    
3388                    if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3389                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3390                    }
3391                    
3392                    splice @{$self->{open_elements}}, $i;
3393                    
3394                    $clear_up_to_marker->();
3395                    
3396                    $self->{insertion_mode} = IN_TABLE_IM;
3397                    
3398                    !!!next-token;
3399                    redo B;
3400                  } elsif ($self->{insertion_mode} == IN_CELL_IM) {
3401                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3402                    ## Ignore the token
3403                    !!!next-token;
3404                    redo B;
3405                  } else {
3406                    #
3407                  }
3408                } elsif ({
3409                          table => 1, tbody => 1, tfoot => 1,
3410                          thead => 1, tr => 1,
3411                         }->{$token->{tag_name}} and
3412                         $self->{insertion_mode} == IN_CELL_IM) {
3413                  ## have an element in table scope
3414                  my $i;
3415                  my $tn;
3416                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3417                    my $node = $self->{open_elements}->[$_];
3418                    if ($node->[1] eq $token->{tag_name}) {
3419                      $i = $_;
3420                      last INSCOPE;
3421                    } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {
3422                      $tn = $node->[1];
3423                      ## NOTE: There is exactly one |td| or |th| element
3424                      ## in scope in the stack of open elements by definition.
3425                    } elsif ({
3426                              table => 1, html => 1,
3427                             }->{$node->[1]}) {
3428                      last INSCOPE;
3429                    }
3430                  } # INSCOPE
3431                  unless (defined $i) {
3432                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3433                    ## Ignore the token
3434                    !!!next-token;
3435                    redo B;
3436                  }
3437    
3438                  ## Close the cell
3439                  !!!back-token; # </?>
3440                  $token = {type => END_TAG_TOKEN, tag_name => $tn};
3441                  redo B;
3442                } elsif ($token->{tag_name} eq 'table' and
3443                         $self->{insertion_mode} == IN_CAPTION_IM) {
3444                  !!!parse-error (type => 'not closed:caption');
3445    
3446                  ## As if </caption>
3447                  ## have a table element in table scope
3448                  my $i;
3449                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3450                    my $node = $self->{open_elements}->[$_];
3451                    if ($node->[1] eq 'caption') {
3452                      $i = $_;
3453                      last INSCOPE;
3454                    } elsif ({
3455                              table => 1, html => 1,
3456                             }->{$node->[1]}) {
3457                      last INSCOPE;
3458                    }
3459                  } # INSCOPE
3460                  unless (defined $i) {
3461                    !!!parse-error (type => 'unmatched end tag:caption');
3462                    ## Ignore the token
3463                    !!!next-token;
3464                    redo B;
3465                  }
3466                  
3467                  ## generate implied end tags
3468                  if ({
3469                       dd => 1, dt => 1, li => 1, p => 1,
3470                       td => 1, th => 1, tr => 1,
3471                       tbody => 1, tfoot=> 1, thead => 1,
3472                      }->{$self->{open_elements}->[-1]->[1]}) {
3473                    !!!back-token; # </table>
3474                    $token = {type => END_TAG_TOKEN, tag_name => 'caption'};
3475                    !!!back-token;
3476                    $token = {type => END_TAG_TOKEN,
3477                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3478                    redo B;
3479                  }
3480    
3481                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3482                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3483                  }
3484    
3485                  splice @{$self->{open_elements}}, $i;
3486    
3487                  $clear_up_to_marker->();
3488    
3489                  $self->{insertion_mode} = IN_TABLE_IM;
3490    
3491                  ## reprocess
3492                  redo B;
3493                } elsif ({
3494                          body => 1, col => 1, colgroup => 1, html => 1,
3495                         }->{$token->{tag_name}}) {
3496                  if ($self->{insertion_mode} & BODY_TABLE_IMS) {
3497                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3498                    ## Ignore the token
3499                    !!!next-token;
3500                    redo B;
3501                  } else {
3502                    #
3503                  }
3504                } elsif ({
3505                          tbody => 1, tfoot => 1,
3506                          thead => 1, tr => 1,
3507                         }->{$token->{tag_name}} and
3508                         $self->{insertion_mode} == IN_CAPTION_IM) {
3509                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3510                  ## Ignore the token
3511                  !!!next-token;
3512                  redo B;
3513                } else {
3514                  #
3515                }
3516          } else {
3517            die "$0: $token->{type}: Unknown token type";
3518          }
3519    
3520          $insert = $insert_to_current;
3521          #
3522        } elsif ($self->{insertion_mode} & TABLE_IMS) {
3523          if ($token->{type} == CHARACTER_TOKEN) {
3524                if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3525                  $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3526                  
3527                  unless (length $token->{data}) {
3528                    !!!next-token;
3529                    redo B;
3530                  }
3531                }
3532    
3533                !!!parse-error (type => 'in table:#character');
3534    
3535                ## As if in body, but insert into foster parent element
3536                ## ISSUE: Spec says that "whenever a node would be inserted
3537                ## into the current node" while characters might not be
3538                ## result in a new Text node.
3539                $reconstruct_active_formatting_elements->($insert_to_foster);
3540                
3541                if ({
3542                     table => 1, tbody => 1, tfoot => 1,
3543                     thead => 1, tr => 1,
3544                    }->{$self->{open_elements}->[-1]->[1]}) {
3545                  # MUST
3546                  my $foster_parent_element;
3547                  my $next_sibling;
3548                  my $prev_sibling;
3549                  OE: for (reverse 0..$#{$self->{open_elements}}) {
3550                    if ($self->{open_elements}->[$_]->[1] eq 'table') {
3551                      my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3552                      if (defined $parent and $parent->node_type == 1) {
3553                        $foster_parent_element = $parent;
3554                        $next_sibling = $self->{open_elements}->[$_]->[0];
3555                        $prev_sibling = $next_sibling->previous_sibling;
3556                      } else {
3557                        $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
3558                        $prev_sibling = $foster_parent_element->last_child;
3559                      }
3560                      last OE;
3561                    }
3562                  } # OE
3563                  $foster_parent_element = $self->{open_elements}->[0]->[0] and
3564                  $prev_sibling = $foster_parent_element->last_child
3565                    unless defined $foster_parent_element;
3566                  if (defined $prev_sibling and
3567                      $prev_sibling->node_type == 3) {
3568                    $prev_sibling->manakai_append_text ($token->{data});
3569                  } else {
3570                    $foster_parent_element->insert_before
3571                      ($self->{document}->create_text_node ($token->{data}),
3572                       $next_sibling);
3573                  }
3574                } else {
3575                  $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3576                }
3577                
3578                !!!next-token;
3579                redo B;
3580          } elsif ($token->{type} == START_TAG_TOKEN) {
3581                if ({
3582                     tr => ($self->{insertion_mode} != IN_ROW_IM),
3583                     th => 1, td => 1,
3584                    }->{$token->{tag_name}}) {
3585                  if ($self->{insertion_mode} == IN_TABLE_IM) {
3586                    ## Clear back to table context
3587                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
3588                           $self->{open_elements}->[-1]->[1] ne 'html') {
3589                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3590                      pop @{$self->{open_elements}};
3591                    }
3592                    
3593                    !!!insert-element ('tbody');
3594                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
3595                    ## reprocess in the "in table body" insertion mode...
3596                  }
3597    
3598                  if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3599                    unless ($token->{tag_name} eq 'tr') {
3600                      !!!parse-error (type => 'missing start tag:tr');
3601                    }
3602                    
3603                    ## Clear back to table body context
3604                    while (not {
3605                      tbody => 1, tfoot => 1, thead => 1, html => 1,
3606                    }->{$self->{open_elements}->[-1]->[1]}) {
3607                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3608                      pop @{$self->{open_elements}};
3609                    }
3610                    
3611                    $self->{insertion_mode} = IN_ROW_IM;
3612                    if ($token->{tag_name} eq 'tr') {
3613                      !!!insert-element ($token->{tag_name}, $token->{attributes});
3614                      !!!next-token;
3615                      redo B;
3616                    } else {
3617                      !!!insert-element ('tr');
3618                      ## reprocess in the "in row" insertion mode
3619                    }
3620                  }
3621    
3622                  ## Clear back to table row context
3623                  while (not {
3624                    tr => 1, html => 1,
3625                  }->{$self->{open_elements}->[-1]->[1]}) {
3626                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3627                    pop @{$self->{open_elements}};
3628                  }
3629                  
3630                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3631                  $self->{insertion_mode} = IN_CELL_IM;
3632    
3633                  push @$active_formatting_elements, ['#marker', ''];
3634                  
3635                  !!!next-token;
3636                  redo B;
3637                } elsif ({
3638                          caption => 1, col => 1, colgroup => 1,
3639                          tbody => 1, tfoot => 1, thead => 1,
3640                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
3641                         }->{$token->{tag_name}}) {
3642                  if ($self->{insertion_mode} == IN_ROW_IM) {
3643                    ## As if </tr>
3644                    ## have an element in table scope
3645                    my $i;
3646                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3647                      my $node = $self->{open_elements}->[$_];
3648                      if ($node->[1] eq 'tr') {
3649                        $i = $_;
3650                        last INSCOPE;
3651                      } elsif ({
3652                                table => 1, html => 1,
3653                               }->{$node->[1]}) {
3654                        last INSCOPE;
3655                      }
3656                    } # INSCOPE
3657                    unless (defined $i) {
3658                      !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});
3659                      ## Ignore the token
3660                      !!!next-token;
3661                      redo B;
3662                    }
3663                    
3664                    ## Clear back to table row context
3665                    while (not {
3666                      tr => 1, html => 1,
3667                    }->{$self->{open_elements}->[-1]->[1]}) {
3668                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3669                      pop @{$self->{open_elements}};
3670                    }
3671                    
3672                    pop @{$self->{open_elements}}; # tr
3673                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
3674                    if ($token->{tag_name} eq 'tr') {
3675                      ## reprocess
3676                      redo B;
3677                    } else {
3678                      ## reprocess in the "in table body" insertion mode...
3679                    }
3680                  }
3681    
3682                  if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3683                    ## have an element in table scope
3684                    my $i;
3685                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3686                      my $node = $self->{open_elements}->[$_];
3687                      if ({
3688                           tbody => 1, thead => 1, tfoot => 1,
3689                          }->{$node->[1]}) {
3690                        $i = $_;
3691                        last INSCOPE;
3692                      } elsif ({
3693                                table => 1, html => 1,
3694                               }->{$node->[1]}) {
3695                        last INSCOPE;
3696                      }
3697                    } # INSCOPE
3698                    unless (defined $i) {
3699                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3700                      ## Ignore the token
3701                      !!!next-token;
3702                      redo B;
3703                    }
3704    
3705                    ## Clear back to table body context
3706                    while (not {
3707                      tbody => 1, tfoot => 1, thead => 1, html => 1,
3708                    }->{$self->{open_elements}->[-1]->[1]}) {
3709                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3710                      pop @{$self->{open_elements}};
3711                    }
3712                    
3713                    ## As if <{current node}>
3714                    ## have an element in table scope
3715                    ## true by definition
3716                    
3717                    ## Clear back to table body context
3718                    ## nop by definition
3719                    
3720                    pop @{$self->{open_elements}};
3721                    $self->{insertion_mode} = IN_TABLE_IM;
3722                    ## reprocess in "in table" insertion mode...
3723                  }
3724    
3725                  if ($token->{tag_name} eq 'col') {
3726                    ## Clear back to table context
3727                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
3728                           $self->{open_elements}->[-1]->[1] ne 'html') {
3729                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3730                      pop @{$self->{open_elements}};
3731                    }
3732                    
3733                    !!!insert-element ('colgroup');
3734                    $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
3735                    ## reprocess
3736                    redo B;
3737                  } elsif ({
3738                            caption => 1,
3739                            colgroup => 1,
3740                            tbody => 1, tfoot => 1, thead => 1,
3741                           }->{$token->{tag_name}}) {
3742                    ## Clear back to table context
3743                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
3744                           $self->{open_elements}->[-1]->[1] ne 'html') {
3745                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3746                      pop @{$self->{open_elements}};
3747                    }
3748                    
3749                    push @$active_formatting_elements, ['#marker', '']
3750                        if $token->{tag_name} eq 'caption';
3751                    
3752                    !!!insert-element ($token->{tag_name}, $token->{attributes});
3753                    $self->{insertion_mode} = {
3754                                               caption => IN_CAPTION_IM,
3755                                               colgroup => IN_COLUMN_GROUP_IM,
3756                                               tbody => IN_TABLE_BODY_IM,
3757                                               tfoot => IN_TABLE_BODY_IM,
3758                                               thead => IN_TABLE_BODY_IM,
3759                                              }->{$token->{tag_name}};
3760                    !!!next-token;
3761                    redo B;
3762                  } else {
3763                    die "$0: in table: <>: $token->{tag_name}";
3764                  }
3765                } elsif ($token->{tag_name} eq 'table') {
3766                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3767    
3768                  ## As if </table>
3769                  ## have a table element in table scope
3770                  my $i;
3771                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3772                    my $node = $self->{open_elements}->[$_];
3773                    if ($node->[1] eq 'table') {
3774                      $i = $_;
3775                      last INSCOPE;
3776                    } elsif ({
3777                              table => 1, html => 1,
3778                             }->{$node->[1]}) {
3779                      last INSCOPE;
3780                    }
3781                  } # INSCOPE
3782                  unless (defined $i) {
3783                    !!!parse-error (type => 'unmatched end tag:table');
3784                    ## Ignore tokens </table><table>
3785                    !!!next-token;
3786                    redo B;
3787                  }
3788                  
3789                  ## generate implied end tags
3790                  if ({
3791                       dd => 1, dt => 1, li => 1, p => 1,
3792                       td => 1, th => 1, tr => 1,
3793                       tbody => 1, tfoot=> 1, thead => 1,
3794                      }->{$self->{open_elements}->[-1]->[1]}) {
3795                    !!!back-token; # <table>
3796                    $token = {type => END_TAG_TOKEN, tag_name => 'table'};
3797                    !!!back-token;
3798                    $token = {type => END_TAG_TOKEN,
3799                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3800                    redo B;
3801                  }
3802    
3803                  if ($self->{open_elements}->[-1]->[1] ne 'table') {
3804                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3805                  }
3806    
3807                  splice @{$self->{open_elements}}, $i;
3808    
3809                  $self->_reset_insertion_mode;
3810    
3811                  ## reprocess
3812                  redo B;
3813            } else {
3814              !!!parse-error (type => 'in table:'.$token->{tag_name});
3815    
3816              $insert = $insert_to_foster;
3817              #
3818            }
3819          } elsif ($token->{type} == END_TAG_TOKEN) {
3820                if ($token->{tag_name} eq 'tr' and
3821                    $self->{insertion_mode} == IN_ROW_IM) {
3822                  ## have an element in table scope
3823                  my $i;
3824                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3825                    my $node = $self->{open_elements}->[$_];
3826                    if ($node->[1] eq $token->{tag_name}) {
3827                      $i = $_;
3828                      last INSCOPE;
3829                    } elsif ({
3830                              table => 1, html => 1,
3831                             }->{$node->[1]}) {
3832                      last INSCOPE;
3833                    }
3834                  } # INSCOPE
3835                  unless (defined $i) {
3836                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3837                    ## Ignore the token
3838                    !!!next-token;
3839                    redo B;
3840                  }
3841    
3842                  ## Clear back to table row context
3843                  while (not {
3844                    tr => 1, html => 1,
3845                  }->{$self->{open_elements}->[-1]->[1]}) {
3846                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3847                    pop @{$self->{open_elements}};
3848                  }
3849    
3850                  pop @{$self->{open_elements}}; # tr
3851                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
3852                  !!!next-token;
3853                  redo B;
3854                } elsif ($token->{tag_name} eq 'table') {
3855                  if ($self->{insertion_mode} == IN_ROW_IM) {
3856                    ## As if </tr>
3857                    ## have an element in table scope
3858                    my $i;
3859                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3860                      my $node = $self->{open_elements}->[$_];
3861                      if ($node->[1] eq 'tr') {
3862                        $i = $_;
3863                        last INSCOPE;
3864                      } elsif ({
3865                                table => 1, html => 1,
3866                               }->{$node->[1]}) {
3867                        last INSCOPE;
3868                      }
3869                    } # INSCOPE
3870                    unless (defined $i) {
3871                      !!!parse-error (type => 'unmatched end tag:'.$token->{type});
3872                      ## Ignore the token
3873                      !!!next-token;
3874                      redo B;
3875                    }
3876                    
3877                    ## Clear back to table row context
3878                    while (not {
3879                      tr => 1, html => 1,
3880                    }->{$self->{open_elements}->[-1]->[1]}) {
3881                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3882                      pop @{$self->{open_elements}};
3883                    }
3884                    
3885                    pop @{$self->{open_elements}}; # tr
3886                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
3887                    ## reprocess in the "in table body" insertion mode...
3888                  }
3889    
3890                  if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3891                    ## have an element in table scope
3892                    my $i;
3893                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3894                      my $node = $self->{open_elements}->[$_];
3895                      if ({
3896                           tbody => 1, thead => 1, tfoot => 1,
3897                          }->{$node->[1]}) {
3898                        $i = $_;
3899                        last INSCOPE;
3900                      } elsif ({
3901                                table => 1, html => 1,
3902                               }->{$node->[1]}) {
3903                        last INSCOPE;
3904                      }
3905                    } # INSCOPE
3906                    unless (defined $i) {
3907                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3908                      ## Ignore the token
3909                      !!!next-token;
3910                      redo B;
3911                    }
3912                    
3913                    ## Clear back to table body context
3914                    while (not {
3915                      tbody => 1, tfoot => 1, thead => 1, html => 1,
3916                    }->{$self->{open_elements}->[-1]->[1]}) {
3917                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3918                      pop @{$self->{open_elements}};
3919                    }
3920                    
3921                    ## As if <{current node}>
3922                    ## have an element in table scope
3923                    ## true by definition
3924                    
3925                    ## Clear back to table body context
3926                    ## nop by definition
3927                    
3928                    pop @{$self->{open_elements}};
3929                    $self->{insertion_mode} = IN_TABLE_IM;
3930                    ## reprocess in the "in table" insertion mode...
3931                  }
3932    
3933                  ## have a table element in table scope
3934                  my $i;
3935                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3936                    my $node = $self->{open_elements}->[$_];
3937                    if ($node->[1] eq $token->{tag_name}) {
3938                      $i = $_;
3939                      last INSCOPE;
3940                    } elsif ({
3941                              table => 1, html => 1,
3942                             }->{$node->[1]}) {
3943                      last INSCOPE;
3944                    }
3945                  } # INSCOPE
3946                  unless (defined $i) {
3947                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3948                    ## Ignore the token
3949                    !!!next-token;
3950                    redo B;
3951                  }
3952    
3953                  ## generate implied end tags
3954                  if ({
3955                       dd => 1, dt => 1, li => 1, p => 1,
3956                       td => 1, th => 1, tr => 1,
3957                       tbody => 1, tfoot=> 1, thead => 1,
3958                      }->{$self->{open_elements}->[-1]->[1]}) {
3959                    !!!back-token;
3960                    $token = {type => END_TAG_TOKEN,
3961                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3962                    redo B;
3963                  }
3964                  
3965                  if ($self->{open_elements}->[-1]->[1] ne 'table') {
3966                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3967                  }
3968                    
3969                  splice @{$self->{open_elements}}, $i;
3970                  
3971                  $self->_reset_insertion_mode;
3972                  
3973                  !!!next-token;
3974                  redo B;
3975                } elsif ({
3976                          tbody => 1, tfoot => 1, thead => 1,
3977                         }->{$token->{tag_name}} and
3978                         $self->{insertion_mode} & ROW_IMS) {
3979                  if ($self->{insertion_mode} == IN_ROW_IM) {
3980                    ## have an element in table scope
3981                    my $i;
3982                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3983                      my $node = $self->{open_elements}->[$_];
3984                      if ($node->[1] eq $token->{tag_name}) {
3985                        $i = $_;
3986                        last INSCOPE;
3987                      } elsif ({
3988                                table => 1, html => 1,
3989                               }->{$node->[1]}) {
3990                        last INSCOPE;
3991                      }
3992                    } # INSCOPE
3993                      unless (defined $i) {
3994                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3995                        ## Ignore the token
3996                        !!!next-token;
3997                        redo B;
3998                      }
3999                    
4000                    ## As if </tr>
4001                    ## have an element in table scope
4002                    my $i;
4003                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4004                      my $node = $self->{open_elements}->[$_];
4005                      if ($node->[1] eq 'tr') {
4006                        $i = $_;
4007                        last INSCOPE;
4008                      } elsif ({
4009                                table => 1, html => 1,
4010                               }->{$node->[1]}) {
4011                        last INSCOPE;
4012                      }
4013                    } # INSCOPE
4014                      unless (defined $i) {
4015                        !!!parse-error (type => 'unmatched end tag:tr');
4016                        ## Ignore the token
4017                        !!!next-token;
4018                        redo B;
4019                      }
4020                    
4021                    ## Clear back to table row context
4022                    while (not {
4023                      tr => 1, html => 1,
4024                    }->{$self->{open_elements}->[-1]->[1]}) {
4025                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4026                      pop @{$self->{open_elements}};
4027                    }
4028                    
4029                    pop @{$self->{open_elements}}; # tr
4030                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
4031                    ## reprocess in the "in table body" insertion mode...
4032                  }
4033    
4034                  ## have an element in table scope
4035                  my $i;
4036                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4037                    my $node = $self->{open_elements}->[$_];
4038                    if ($node->[1] eq $token->{tag_name}) {
4039                      $i = $_;
4040                      last INSCOPE;
4041                    } elsif ({
4042                              table => 1, html => 1,
4043                             }->{$node->[1]}) {
4044                      last INSCOPE;
4045                    }
4046                  } # INSCOPE
4047                  unless (defined $i) {
4048                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4049                    ## Ignore the token
4050                    !!!next-token;
4051                    redo B;
4052                  }
4053    
4054                  ## Clear back to table body context
4055                  while (not {
4056                    tbody => 1, tfoot => 1, thead => 1, html => 1,
4057                  }->{$self->{open_elements}->[-1]->[1]}) {
4058                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4059                    pop @{$self->{open_elements}};
4060                  }
4061    
4062                  pop @{$self->{open_elements}};
4063                  $self->{insertion_mode} = IN_TABLE_IM;
4064                  !!!next-token;
4065                  redo B;
4066                } elsif ({
4067                          body => 1, caption => 1, col => 1, colgroup => 1,
4068                          html => 1, td => 1, th => 1,
4069                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
4070                          tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
4071                         }->{$token->{tag_name}}) {
4072                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4073                  ## Ignore the token
4074                  !!!next-token;
4075                  redo B;
4076            } else {
4077              !!!parse-error (type => 'in table:/'.$token->{tag_name});
4078    
4079              $insert = $insert_to_foster;
4080              #
4081            }
4082          } else {
4083            die "$0: $token->{type}: Unknown token type";
4084          }
4085        } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
4086              if ($token->{type} == CHARACTER_TOKEN) {
4087                if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4088                  $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4089                  unless (length $token->{data}) {
4090                    !!!next-token;
4091                    redo B;
4092                  }
4093                }
4094                
4095                #
4096              } elsif ($token->{type} == START_TAG_TOKEN) {
4097                if ($token->{tag_name} eq 'col') {
4098                  !!!insert-element ($token->{tag_name}, $token->{attributes});
4099                  pop @{$self->{open_elements}};
4100                  !!!next-token;
4101                  redo B;
4102                } else {
4103                  #
4104                }
4105              } elsif ($token->{type} == END_TAG_TOKEN) {
4106                if ($token->{tag_name} eq 'colgroup') {
4107                  if ($self->{open_elements}->[-1]->[1] eq 'html') {
4108                    !!!parse-error (type => 'unmatched end tag:colgroup');
4109                    ## Ignore the token
4110                    !!!next-token;
4111                    redo B;
4112                  } else {
4113                    pop @{$self->{open_elements}}; # colgroup
4114                    $self->{insertion_mode} = IN_TABLE_IM;
4115                    !!!next-token;
4116                    redo B;            
4117                  }
4118                } elsif ($token->{tag_name} eq 'col') {
4119                  !!!parse-error (type => 'unmatched end tag:col');
4120                  ## Ignore the token
4121                  !!!next-token;
4122                  redo B;
4123                } else {
4124                  #
4125                }
4126              } else {
4127                #
4128              }
4129    
4130              ## As if </colgroup>
4131              if ($self->{open_elements}->[-1]->[1] eq 'html') {
4132                !!!parse-error (type => 'unmatched end tag:colgroup');
4133                ## Ignore the token
4134                !!!next-token;
4135                redo B;
4136              } else {
4137                pop @{$self->{open_elements}}; # colgroup
4138                $self->{insertion_mode} = IN_TABLE_IM;
4139                ## reprocess
4140                redo B;
4141              }
4142        } elsif ($self->{insertion_mode} == IN_SELECT_IM) {
4143          if ($token->{type} == CHARACTER_TOKEN) {
4144            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4145            !!!next-token;
4146            redo B;
4147          } elsif ($token->{type} == START_TAG_TOKEN) {
4148                if ($token->{tag_name} eq 'option') {
4149                  if ($self->{open_elements}->[-1]->[1] eq 'option') {
4150                    ## As if </option>
4151                    pop @{$self->{open_elements}};
4152                  }
4153    
4154                  !!!insert-element ($token->{tag_name}, $token->{attributes});
4155                  !!!next-token;
4156                  redo B;
4157                } elsif ($token->{tag_name} eq 'optgroup') {
4158                  if ($self->{open_elements}->[-1]->[1] eq 'option') {
4159                    ## As if </option>
4160                    pop @{$self->{open_elements}};
4161                  }
4162    
4163                  if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {
4164                    ## As if </optgroup>
4165                    pop @{$self->{open_elements}};
4166                  }
4167    
4168                  !!!insert-element ($token->{tag_name}, $token->{attributes});
4169                  !!!next-token;
4170                  redo B;
4171                } elsif ($token->{tag_name} eq 'select') {
4172                  !!!parse-error (type => 'not closed:select');
4173                  ## As if </select> instead
4174                  ## have an element in table scope
4175                  my $i;
4176                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4177                    my $node = $self->{open_elements}->[$_];
4178                    if ($node->[1] eq $token->{tag_name}) {
4179                      $i = $_;
4180                      last INSCOPE;
4181                    } elsif ({
4182                              table => 1, html => 1,
4183                             }->{$node->[1]}) {
4184                      last INSCOPE;
4185                    }
4186                  } # INSCOPE
4187                  unless (defined $i) {
4188                    !!!parse-error (type => 'unmatched end tag:select');
4189                    ## Ignore the token
4190                    !!!next-token;
4191                    redo B;
4192                  }
4193                  
4194                  splice @{$self->{open_elements}}, $i;
4195    
4196                  $self->_reset_insertion_mode;
4197    
4198                  !!!next-token;
4199                  redo B;
4200            } else {
4201              !!!parse-error (type => 'in select:'.$token->{tag_name});
4202              ## Ignore the token
4203              !!!next-token;
4204              redo B;
4205            }
4206          } elsif ($token->{type} == END_TAG_TOKEN) {
4207                if ($token->{tag_name} eq 'optgroup') {
4208                  if ($self->{open_elements}->[-1]->[1] eq 'option' and
4209                      $self->{open_elements}->[-2]->[1] eq 'optgroup') {
4210                    ## As if </option>
4211                    splice @{$self->{open_elements}}, -2;
4212                  } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {
4213                    pop @{$self->{open_elements}};
4214                  } else {
4215                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4216                    ## Ignore the token
4217                  }
4218                  !!!next-token;
4219                  redo B;
4220                } elsif ($token->{tag_name} eq 'option') {
4221                  if ($self->{open_elements}->[-1]->[1] eq 'option') {
4222                    pop @{$self->{open_elements}};
4223                  } else {
4224                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4225                    ## Ignore the token
4226                  }
4227                  !!!next-token;
4228                  redo B;
4229                } elsif ($token->{tag_name} eq 'select') {
4230                  ## have an element in table scope
4231                  my $i;
4232                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4233                    my $node = $self->{open_elements}->[$_];
4234                    if ($node->[1] eq $token->{tag_name}) {
4235                      $i = $_;
4236                      last INSCOPE;
4237                    } elsif ({
4238                              table => 1, html => 1,
4239                             }->{$node->[1]}) {
4240                      last INSCOPE;
4241                    }
4242                  } # INSCOPE
4243                  unless (defined $i) {
4244                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4245                    ## Ignore the token
4246                    !!!next-token;
4247                    redo B;
4248                  }
4249                  
4250                  splice @{$self->{open_elements}}, $i;
4251    
4252                  $self->_reset_insertion_mode;
4253    
4254                  !!!next-token;
4255                  redo B;
4256                } elsif ({
4257                          caption => 1, table => 1, tbody => 1,
4258                          tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
4259                         }->{$token->{tag_name}}) {
4260                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4261                  
4262                  ## have an element in table scope
4263                  my $i;
4264                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4265                    my $node = $self->{open_elements}->[$_];
4266                    if ($node->[1] eq $token->{tag_name}) {
4267                      $i = $_;
4268                      last INSCOPE;
4269                    } elsif ({
4270                              table => 1, html => 1,
4271                             }->{$node->[1]}) {
4272                      last INSCOPE;
4273                    }
4274                  } # INSCOPE
4275                  unless (defined $i) {
4276                    ## Ignore the token
4277                    !!!next-token;
4278                    redo B;
4279                  }
4280                  
4281                  ## As if </select>
4282                  ## have an element in table scope
4283                  undef $i;
4284                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4285                    my $node = $self->{open_elements}->[$_];
4286                    if ($node->[1] eq 'select') {
4287                      $i = $_;
4288                      last INSCOPE;
4289                    } elsif ({
4290                              table => 1, html => 1,
4291                             }->{$node->[1]}) {
4292                      last INSCOPE;
4293                    }
4294                  } # INSCOPE
4295                  unless (defined $i) {
4296                    !!!parse-error (type => 'unmatched end tag:select');
4297                    ## Ignore the </select> token
4298                    !!!next-token; ## TODO: ok?
4299                    redo B;
4300                  }
4301                  
4302                  splice @{$self->{open_elements}}, $i;
4303    
4304                  $self->_reset_insertion_mode;
4305    
4306                  ## reprocess
4307                  redo B;
4308            } else {
4309              !!!parse-error (type => 'in select:/'.$token->{tag_name});
4310              ## Ignore the token
4311              !!!next-token;
4312              redo B;
4313            }
4314          } else {
4315            die "$0: $token->{type}: Unknown token type";
4316          }
4317        } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
4318          if ($token->{type} == CHARACTER_TOKEN) {
4319            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4320              my $data = $1;
4321              ## As if in body
4322              $reconstruct_active_formatting_elements->($insert_to_current);
4323                  
4324              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4325              
4326              unless (length $token->{data}) {
4327                !!!next-token;
4328                redo B;
4329              }
4330            }
4331            
4332            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4333              !!!parse-error (type => 'after html:#character');
4334    
4335              ## Reprocess in the "main" phase, "after body" insertion mode...
4336            }
4337            
4338            ## "after body" insertion mode
4339            !!!parse-error (type => 'after body:#character');
4340    
4341            $self->{insertion_mode} = IN_BODY_IM;
4342            ## reprocess
4343            redo B;
4344          } elsif ($token->{type} == START_TAG_TOKEN) {
4345            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4346              !!!parse-error (type => 'after html:'.$token->{tag_name});
4347              
4348              ## Reprocess in the "main" phase, "after body" insertion mode...
4349            }
4350    
4351            ## "after body" insertion mode
4352            !!!parse-error (type => 'after body:'.$token->{tag_name});
4353    
4354            $self->{insertion_mode} = IN_BODY_IM;
4355            ## reprocess
4356            redo B;
4357          } elsif ($token->{type} == END_TAG_TOKEN) {
4358            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4359              !!!parse-error (type => 'after html:/'.$token->{tag_name});
4360              
4361              $self->{insertion_mode} = AFTER_BODY_IM;
4362              ## Reprocess in the "main" phase, "after body" insertion mode...
4363            }
4364    
4365            ## "after body" insertion mode
4366            if ($token->{tag_name} eq 'html') {
4367              if (defined $self->{inner_html_node}) {
4368                !!!parse-error (type => 'unmatched end tag:html');
4369                ## Ignore the token
4370                !!!next-token;
4371                redo B;
4372              } else {
4373                $self->{insertion_mode} = AFTER_HTML_BODY_IM;
4374                !!!next-token;
4375                redo B;
4376              }
4377            } else {
4378              !!!parse-error (type => 'after body:/'.$token->{tag_name});
4379    
4380              $self->{insertion_mode} = IN_BODY_IM;
4381              ## reprocess
4382              redo B;
4383            }
4384          } else {
4385            die "$0: $token->{type}: Unknown token type";
4386          }
4387        } elsif ($self->{insertion_mode} & FRAME_IMS) {
4388          if ($token->{type} == CHARACTER_TOKEN) {
4389            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4390              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4391              
4392              unless (length $token->{data}) {
4393                !!!next-token;
4394                redo B;
4395              }
4396            }
4397            
4398            if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
4399              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4400                !!!parse-error (type => 'in frameset:#character');
4401              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
4402                !!!parse-error (type => 'after frameset:#character');
4403              } else { # "after html frameset"
4404                !!!parse-error (type => 'after html:#character');
4405    
4406                $self->{insertion_mode} = AFTER_FRAMESET_IM;
4407                ## Reprocess in the "main" phase, "after frameset"...
4408                !!!parse-error (type => 'after frameset:#character');
4409              }
4410              
4411              ## Ignore the token.
4412              if (length $token->{data}) {
4413                ## reprocess the rest of characters
4414              } else {
4415                !!!next-token;
4416              }
4417              redo B;
4418            }
4419            
4420            die qq[$0: Character "$token->{data}"];
4421          } elsif ($token->{type} == START_TAG_TOKEN) {
4422            if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4423              !!!parse-error (type => 'after html:'.$token->{tag_name});
4424    
4425              $self->{insertion_mode} = AFTER_FRAMESET_IM;
4426              ## Process in the "main" phase, "after frameset" insertion mode...
4427            }
4428    
4429            if ($token->{tag_name} eq 'frameset' and
4430                $self->{insertion_mode} == IN_FRAMESET_IM) {
4431              !!!insert-element ($token->{tag_name}, $token->{attributes});
4432              !!!next-token;
4433              redo B;
4434            } elsif ($token->{tag_name} eq 'frame' and
4435                     $self->{insertion_mode} == IN_FRAMESET_IM) {
4436              !!!insert-element ($token->{tag_name}, $token->{attributes});
4437              pop @{$self->{open_elements}};
4438              !!!next-token;
4439              redo B;
4440            } elsif ($token->{tag_name} eq 'noframes') {
4441              ## NOTE: As if in body.
4442              $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
4443              redo B;
4444            } else {
4445              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4446                !!!parse-error (type => 'in frameset:'.$token->{tag_name});
4447              } else {
4448                !!!parse-error (type => 'after frameset:'.$token->{tag_name});
4449              }
4450              ## Ignore the token
4451              !!!next-token;
4452              redo B;
4453            }
4454          } elsif ($token->{type} == END_TAG_TOKEN) {
4455            if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4456              !!!parse-error (type => 'after html:/'.$token->{tag_name});
4457    
4458              $self->{insertion_mode} = AFTER_FRAMESET_IM;
4459              ## Process in the "main" phase, "after frameset" insertion mode...
4460            }
4461    
4462            if ($token->{tag_name} eq 'frameset' and
4463                $self->{insertion_mode} == IN_FRAMESET_IM) {
4464              if ($self->{open_elements}->[-1]->[1] eq 'html' and
4465                  @{$self->{open_elements}} == 1) {
4466                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4467                ## Ignore the token
4468                !!!next-token;
4469              } else {
4470                pop @{$self->{open_elements}};
4471                !!!next-token;
4472              }
4473    
4474              if (not defined $self->{inner_html_node} and
4475                  $self->{open_elements}->[-1]->[1] ne 'frameset') {
4476                $self->{insertion_mode} = AFTER_FRAMESET_IM;
4477              }
4478              redo B;
4479            } elsif ($token->{tag_name} eq 'html' and
4480                     $self->{insertion_mode} == AFTER_FRAMESET_IM) {
4481              $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
4482              !!!next-token;
4483              redo B;
4484            } else {
4485              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4486                !!!parse-error (type => 'in frameset:/'.$token->{tag_name});
4487              } else {
4488                !!!parse-error (type => 'after frameset:/'.$token->{tag_name});
4489              }
4490              ## Ignore the token
4491              !!!next-token;
4492              redo B;
4493            }
4494          } else {
4495            die "$0: $token->{type}: Unknown token type";
4496          }
4497    
4498          ## ISSUE: An issue in spec here
4499        } else {
4500          die "$0: $self->{insertion_mode}: Unknown insertion mode";
4501        }
4502    
4503        ## "in body" insertion mode
4504        if ($token->{type} == START_TAG_TOKEN) {
4505        if ($token->{tag_name} eq 'script') {        if ($token->{tag_name} eq 'script') {
4506          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
4507          $script_start_tag->($insert);          $script_start_tag->($insert);
4508          return;          redo B;
4509        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
4510          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
4511          $parse_rcdata->('CDATA', $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4512          return;          redo B;
4513        } elsif ({        } elsif ({
4514                  base => 1, link => 1, meta => 1,                  base => 1, link => 1,
4515                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
4516          ## NOTE: This is an "as if in head" code clone, only "-t" differs          ## NOTE: This is an "as if in head" code clone, only "-t" differs
4517          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4518          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4519          !!!next-token;          !!!next-token;
4520          return;          redo B;
4521          } elsif ($token->{tag_name} eq 'meta') {
4522            ## NOTE: This is an "as if in head" code clone, only "-t" differs
4523            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4524            my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4525    
4526            unless ($self->{confident}) {
4527              if ($token->{attributes}->{charset}) { ## TODO: And if supported
4528                $self->{change_encoding}
4529                    ->($self, $token->{attributes}->{charset}->{value});
4530                
4531                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4532                    ->set_user_data (manakai_has_reference =>
4533                                         $token->{attributes}->{charset}
4534                                             ->{has_reference});
4535              } elsif ($token->{attributes}->{content}) {
4536                ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
4537                if ($token->{attributes}->{content}->{value}
4538                    =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=
4539                        [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4540                        ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
4541                  $self->{change_encoding}
4542                      ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);
4543                  $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4544                      ->set_user_data (manakai_has_reference =>
4545                                           $token->{attributes}->{content}
4546                                                 ->{has_reference});
4547                }
4548              }
4549            } else {
4550              if ($token->{attributes}->{charset}) {
4551                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4552                    ->set_user_data (manakai_has_reference =>
4553                                         $token->{attributes}->{charset}
4554                                             ->{has_reference});
4555              }
4556              if ($token->{attributes}->{content}) {
4557                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4558                    ->set_user_data (manakai_has_reference =>
4559                                         $token->{attributes}->{content}
4560                                             ->{has_reference});
4561              }
4562            }
4563    
4564            !!!next-token;
4565            redo B;
4566        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
4567          !!!parse-error (type => 'in body:title');          !!!parse-error (type => 'in body:title');
4568          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
4569          $parse_rcdata->('RCDATA', $insert);          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {
4570          return;            if (defined $self->{head_element}) {
4571                $self->{head_element}->append_child ($_[0]);
4572              } else {
4573                $insert->($_[0]);
4574              }
4575            });
4576            redo B;
4577        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
4578          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body:body');
4579                                
# Line 2474  sub _tree_construction_main ($) { Line 4591  sub _tree_construction_main ($) {
4591            }            }
4592          }          }
4593          !!!next-token;          !!!next-token;
4594          return;          redo B;
4595        } elsif ({        } elsif ({
4596                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
4597                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1, listing => 1,
# Line 2485  sub _tree_construction_main ($) { Line 4602  sub _tree_construction_main ($) {
4602          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4603            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4604              !!!back-token;              !!!back-token;
4605              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4606              return;              redo B;
4607            } elsif ({            } elsif ({
4608                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4609                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2498  sub _tree_construction_main ($) { Line 4615  sub _tree_construction_main ($) {
4615          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4616          if ($token->{tag_name} eq 'pre') {          if ($token->{tag_name} eq 'pre') {
4617            !!!next-token;            !!!next-token;
4618            if ($token->{type} eq 'character') {            if ($token->{type} == CHARACTER_TOKEN) {
4619              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
4620              unless (length $token->{data}) {              unless (length $token->{data}) {
4621                !!!next-token;                !!!next-token;
# Line 2507  sub _tree_construction_main ($) { Line 4624  sub _tree_construction_main ($) {
4624          } else {          } else {
4625            !!!next-token;            !!!next-token;
4626          }          }
4627          return;          redo B;
4628        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
4629          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
4630            !!!parse-error (type => 'in form:form');            !!!parse-error (type => 'in form:form');
4631            ## Ignore the token            ## Ignore the token
4632            !!!next-token;            !!!next-token;
4633            return;            redo B;
4634          } else {          } else {
4635            ## has a p element in scope            ## has a p element in scope
4636            INSCOPE: for (reverse @{$self->{open_elements}}) {            INSCOPE: for (reverse @{$self->{open_elements}}) {
4637              if ($_->[1] eq 'p') {              if ($_->[1] eq 'p') {
4638                !!!back-token;                !!!back-token;
4639                $token = {type => 'end tag', tag_name => 'p'};                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4640                return;                redo B;
4641              } elsif ({              } elsif ({
4642                        table => 1, caption => 1, td => 1, th => 1,                        table => 1, caption => 1, td => 1, th => 1,
4643                        button => 1, marquee => 1, object => 1, html => 1,                        button => 1, marquee => 1, object => 1, html => 1,
# Line 2532  sub _tree_construction_main ($) { Line 4649  sub _tree_construction_main ($) {
4649            !!!insert-element-t ($token->{tag_name}, $token->{attributes});            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4650            $self->{form_element} = $self->{open_elements}->[-1]->[0];            $self->{form_element} = $self->{open_elements}->[-1]->[0];
4651            !!!next-token;            !!!next-token;
4652            return;            redo B;
4653          }          }
4654        } elsif ($token->{tag_name} eq 'li') {        } elsif ($token->{tag_name} eq 'li') {
4655          ## has a p element in scope          ## has a p element in scope
4656          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4657            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4658              !!!back-token;              !!!back-token;
4659              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4660              return;              redo B;
4661            } elsif ({            } elsif ({
4662                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4663                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2558  sub _tree_construction_main ($) { Line 4675  sub _tree_construction_main ($) {
4675              if ($i != -1) {              if ($i != -1) {
4676                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'end tag missing:'.
4677                                $self->{open_elements}->[-1]->[1]);                                $self->{open_elements}->[-1]->[1]);
               ## TODO: test  
4678              }              }
4679              splice @{$self->{open_elements}}, $i;              splice @{$self->{open_elements}}, $i;
4680              last LI;              last LI;
# Line 2581  sub _tree_construction_main ($) { Line 4697  sub _tree_construction_main ($) {
4697                        
4698          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4699          !!!next-token;          !!!next-token;
4700          return;          redo B;
4701        } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {        } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {
4702          ## has a p element in scope          ## has a p element in scope
4703          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4704            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4705              !!!back-token;              !!!back-token;
4706              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4707              return;              redo B;
4708            } elsif ({            } elsif ({
4709                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4710                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2606  sub _tree_construction_main ($) { Line 4722  sub _tree_construction_main ($) {
4722              if ($i != -1) {              if ($i != -1) {
4723                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'end tag missing:'.
4724                                $self->{open_elements}->[-1]->[1]);                                $self->{open_elements}->[-1]->[1]);
               ## TODO: test  
4725              }              }
4726              splice @{$self->{open_elements}}, $i;              splice @{$self->{open_elements}}, $i;
4727              last LI;              last LI;
# Line 2629  sub _tree_construction_main ($) { Line 4744  sub _tree_construction_main ($) {
4744                        
4745          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4746          !!!next-token;          !!!next-token;
4747          return;          redo B;
4748        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
4749          ## has a p element in scope          ## has a p element in scope
4750          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4751            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4752              !!!back-token;              !!!back-token;
4753              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4754              return;              redo B;
4755            } elsif ({            } elsif ({
4756                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4757                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2647  sub _tree_construction_main ($) { Line 4762  sub _tree_construction_main ($) {
4762                        
4763          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4764                        
4765          $self->{content_model_flag} = 'PLAINTEXT';          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
4766                        
4767          !!!next-token;          !!!next-token;
4768          return;          redo B;
4769        } elsif ({        } elsif ({
4770                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
4771                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 2659  sub _tree_construction_main ($) { Line 4774  sub _tree_construction_main ($) {
4774            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
4775            if ($node->[1] eq 'p') {            if ($node->[1] eq 'p') {
4776              !!!back-token;              !!!back-token;
4777              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4778              return;              redo B;
4779            } elsif ({            } elsif ({
4780                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4781                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2695  sub _tree_construction_main ($) { Line 4810  sub _tree_construction_main ($) {
4810          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4811                        
4812          !!!next-token;          !!!next-token;
4813          return;          redo B;
4814        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
4815          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
4816            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
# Line 2703  sub _tree_construction_main ($) { Line 4818  sub _tree_construction_main ($) {
4818              !!!parse-error (type => 'in a:a');              !!!parse-error (type => 'in a:a');
4819                            
4820              !!!back-token;              !!!back-token;
4821              $token = {type => 'end tag', tag_name => 'a'};              $token = {type => END_TAG_TOKEN, tag_name => 'a'};
4822              $formatting_end_tag->($token->{tag_name});              $formatting_end_tag->($token->{tag_name});
4823                            
4824              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
# Line 2730  sub _tree_construction_main ($) { Line 4845  sub _tree_construction_main ($) {
4845          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
4846    
4847          !!!next-token;          !!!next-token;
4848          return;          redo B;
4849        } elsif ({        } elsif ({
4850                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
4851                  s => 1, small => 1, strile => 1,                  s => 1, small => 1, strile => 1,
# Line 2742  sub _tree_construction_main ($) { Line 4857  sub _tree_construction_main ($) {
4857          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
4858                    
4859          !!!next-token;          !!!next-token;
4860          return;          redo B;
4861        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
4862          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
4863    
# Line 2750  sub _tree_construction_main ($) { Line 4865  sub _tree_construction_main ($) {
4865          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4866            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
4867            if ($node->[1] eq 'nobr') {            if ($node->[1] eq 'nobr') {
4868                !!!parse-error (type => 'in nobr:nobr');
4869              !!!back-token;              !!!back-token;
4870              $token = {type => 'end tag', tag_name => 'nobr'};              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};
4871              return;              redo B;
4872            } elsif ({            } elsif ({
4873                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4874                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2765  sub _tree_construction_main ($) { Line 4881  sub _tree_construction_main ($) {
4881          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
4882                    
4883          !!!next-token;          !!!next-token;
4884          return;          redo B;
4885        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
4886          ## has a button element in scope          ## has a button element in scope
4887          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 2773  sub _tree_construction_main ($) { Line 4889  sub _tree_construction_main ($) {
4889            if ($node->[1] eq 'button') {            if ($node->[1] eq 'button') {
4890              !!!parse-error (type => 'in button:button');              !!!parse-error (type => 'in button:button');
4891              !!!back-token;              !!!back-token;
4892              $token = {type => 'end tag', tag_name => 'button'};              $token = {type => END_TAG_TOKEN, tag_name => 'button'};
4893              return;              redo B;
4894            } elsif ({            } elsif ({
4895                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4896                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2789  sub _tree_construction_main ($) { Line 4905  sub _tree_construction_main ($) {
4905          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
4906    
4907          !!!next-token;          !!!next-token;
4908          return;          redo B;
4909        } elsif ($token->{tag_name} eq 'marquee' or        } elsif ($token->{tag_name} eq 'marquee' or
4910                 $token->{tag_name} eq 'object') {                 $token->{tag_name} eq 'object') {
4911          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
# Line 2798  sub _tree_construction_main ($) { Line 4914  sub _tree_construction_main ($) {
4914          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
4915                    
4916          !!!next-token;          !!!next-token;
4917          return;          redo B;
4918        } elsif ($token->{tag_name} eq 'xmp') {        } elsif ($token->{tag_name} eq 'xmp') {
4919          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
4920          $parse_rcdata->('CDATA', $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4921          return;          redo B;
4922        } elsif ($token->{tag_name} eq 'table') {        } elsif ($token->{tag_name} eq 'table') {
4923          ## has a p element in scope          ## has a p element in scope
4924          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4925            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4926              !!!back-token;              !!!back-token;
4927              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4928              return;              redo B;
4929            } elsif ({            } elsif ({
4930                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4931                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2820  sub _tree_construction_main ($) { Line 4936  sub _tree_construction_main ($) {
4936                        
4937          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4938                        
4939          $self->{insertion_mode} = 'in table';          $self->{insertion_mode} = IN_TABLE_IM;
4940                        
4941          !!!next-token;          !!!next-token;
4942          return;          redo B;
4943        } elsif ({        } elsif ({
4944                  area => 1, basefont => 1, bgsound => 1, br => 1,                  area => 1, basefont => 1, bgsound => 1, br => 1,
4945                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
# Line 2833  sub _tree_construction_main ($) { Line 4949  sub _tree_construction_main ($) {
4949            !!!parse-error (type => 'image');            !!!parse-error (type => 'image');
4950            $token->{tag_name} = 'img';            $token->{tag_name} = 'img';
4951          }          }
4952            
4953            ## NOTE: There is an "as if <br>" code clone.
4954          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
4955                    
4956          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4957          pop @{$self->{open_elements}};          pop @{$self->{open_elements}};
4958                    
4959          !!!next-token;          !!!next-token;
4960          return;          redo B;
4961        } elsif ($token->{tag_name} eq 'hr') {        } elsif ($token->{tag_name} eq 'hr') {
4962          ## has a p element in scope          ## has a p element in scope
4963          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4964            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4965              !!!back-token;              !!!back-token;
4966              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4967              return;              redo B;
4968            } elsif ({            } elsif ({
4969                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4970                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2860  sub _tree_construction_main ($) { Line 4977  sub _tree_construction_main ($) {
4977          pop @{$self->{open_elements}};          pop @{$self->{open_elements}};
4978                        
4979          !!!next-token;          !!!next-token;
4980          return;          redo B;
4981        } elsif ($token->{tag_name} eq 'input') {        } elsif ($token->{tag_name} eq 'input') {
4982          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
4983                    
# Line 2869  sub _tree_construction_main ($) { Line 4986  sub _tree_construction_main ($) {
4986          pop @{$self->{open_elements}};          pop @{$self->{open_elements}};
4987                    
4988          !!!next-token;          !!!next-token;
4989          return;          redo B;
4990        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
4991          !!!parse-error (type => 'isindex');          !!!parse-error (type => 'isindex');
4992                    
4993          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
4994            ## Ignore the token            ## Ignore the token
4995            !!!next-token;            !!!next-token;
4996            return;            redo B;
4997          } else {          } else {
4998            my $at = $token->{attributes};            my $at = $token->{attributes};
4999            my $form_attrs;            my $form_attrs;
# Line 2886  sub _tree_construction_main ($) { Line 5003  sub _tree_construction_main ($) {
5003            delete $at->{action};            delete $at->{action};
5004            delete $at->{prompt};            delete $at->{prompt};
5005            my @tokens = (            my @tokens = (
5006                          {type => 'start tag', tag_name => 'form',                          {type => START_TAG_TOKEN, tag_name => 'form',
5007                           attributes => $form_attrs},                           attributes => $form_attrs},
5008                          {type => 'start tag', tag_name => 'hr'},                          {type => START_TAG_TOKEN, tag_name => 'hr'},
5009                          {type => 'start tag', tag_name => 'p'},                          {type => START_TAG_TOKEN, tag_name => 'p'},
5010                          {type => 'start tag', tag_name => 'label'},                          {type => START_TAG_TOKEN, tag_name => 'label'},
5011                         );                         );
5012            if ($prompt_attr) {            if ($prompt_attr) {
5013              push @tokens, {type => 'character', data => $prompt_attr->{value}};              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};
5014            } else {            } else {
5015              push @tokens, {type => 'character',              push @tokens, {type => CHARACTER_TOKEN,
5016                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD
5017              ## TODO: make this configurable              ## TODO: make this configurable
5018            }            }
5019            push @tokens,            push @tokens,
5020                          {type => 'start tag', tag_name => 'input', attributes => $at},                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},
5021                          #{type => 'character', data => ''}, # SHOULD                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
5022                          {type => 'end tag', tag_name => 'label'},                          {type => END_TAG_TOKEN, tag_name => 'label'},
5023                          {type => 'end tag', tag_name => 'p'},                          {type => END_TAG_TOKEN, tag_name => 'p'},
5024                          {type => 'start tag', tag_name => 'hr'},                          {type => START_TAG_TOKEN, tag_name => 'hr'},
5025                          {type => 'end tag', tag_name => 'form'};                          {type => END_TAG_TOKEN, tag_name => 'form'};
5026            $token = shift @tokens;            $token = shift @tokens;
5027            !!!back-token (@tokens);            !!!back-token (@tokens);
5028            return;            redo B;
5029          }          }
5030        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
5031          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
# Line 2916  sub _tree_construction_main ($) { Line 5033  sub _tree_construction_main ($) {
5033          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $token->{tag_name}, $token->{attributes});
5034                    
5035          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
5036          $self->{content_model_flag} = 'RCDATA';          $self->{content_model} = RCDATA_CONTENT_MODEL;
5037          delete $self->{escape}; # MUST          delete $self->{escape}; # MUST
5038                    
5039          $insert->($el);          $insert->($el);
5040                    
5041          my $text = '';          my $text = '';
5042          !!!next-token;          !!!next-token;
5043          if ($token->{type} eq 'character') {          if ($token->{type} == CHARACTER_TOKEN) {
5044            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
5045            unless (length $token->{data}) {            unless (length $token->{data}) {
5046              !!!next-token;              !!!next-token;
5047            }            }
5048          }          }
5049          while ($token->{type} eq 'character') {          while ($token->{type} == CHARACTER_TOKEN) {
5050            $text .= $token->{data};            $text .= $token->{data};
5051            !!!next-token;            !!!next-token;
5052          }          }
# Line 2937  sub _tree_construction_main ($) { Line 5054  sub _tree_construction_main ($) {
5054            $el->manakai_append_text ($text);            $el->manakai_append_text ($text);
5055          }          }
5056                    
5057          $self->{content_model_flag} = 'PCDATA';          $self->{content_model} = PCDATA_CONTENT_MODEL;
5058                    
5059          if ($token->{type} eq 'end tag' and          if ($token->{type} == END_TAG_TOKEN and
5060              $token->{tag_name} eq $tag_name) {              $token->{tag_name} eq $tag_name) {
5061            ## Ignore the token            ## Ignore the token
5062          } else {          } else {
5063            !!!parse-error (type => 'in RCDATA:#'.$token->{type});            !!!parse-error (type => 'in RCDATA:#'.$token->{type});
5064          }          }
5065          !!!next-token;          !!!next-token;
5066          return;          redo B;
5067        } elsif ({        } elsif ({
5068                  iframe => 1,                  iframe => 1,
5069                  noembed => 1,                  noembed => 1,
5070                  noframes => 1,                  noframes => 1,
5071                  noscript => 0, ## TODO: 1 if scripting is enabled                  noscript => 0, ## TODO: 1 if scripting is enabled
5072                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5073          $parse_rcdata->('CDATA', $insert);          ## NOTE: There is an "as if in body" code clone.
5074          return;          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
5075            redo B;
5076        } elsif ($token->{tag_name} eq 'select') {        } elsif ($token->{tag_name} eq 'select') {
5077          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5078                    
5079          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5080                    
5081          $self->{insertion_mode} = 'in select';          $self->{insertion_mode} = IN_SELECT_IM;
5082          !!!next-token;          !!!next-token;
5083          return;          redo B;
5084        } elsif ({        } elsif ({
5085                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
5086                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 2972  sub _tree_construction_main ($) { Line 5090  sub _tree_construction_main ($) {
5090          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!parse-error (type => 'in body:'.$token->{tag_name});
5091          ## Ignore the token          ## Ignore the token
5092          !!!next-token;          !!!next-token;
5093          return;          redo B;
5094                    
5095          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
5096        } else {        } else {
# Line 2981  sub _tree_construction_main ($) { Line 5099  sub _tree_construction_main ($) {
5099          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5100                    
5101          !!!next-token;          !!!next-token;
5102          return;          redo B;
5103        }        }
5104      } elsif ($token->{type} eq 'end tag') {      } elsif ($token->{type} == END_TAG_TOKEN) {
5105        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
5106          if (@{$self->{open_elements}} > 1 and          if (@{$self->{open_elements}} > 1 and
5107              $self->{open_elements}->[1]->[1] eq 'body') {              $self->{open_elements}->[1]->[1] eq 'body') {
# Line 2991  sub _tree_construction_main ($) { Line 5109  sub _tree_construction_main ($) {
5109              unless ({              unless ({
5110                         dd => 1, dt => 1, li => 1, p => 1, td => 1,                         dd => 1, dt => 1, li => 1, p => 1, td => 1,
5111                         th => 1, tr => 1, body => 1, html => 1,                         th => 1, tr => 1, body => 1, html => 1,
5112                         tbody => 1, tfoot => 1, thead => 1,
5113                      }->{$_->[1]}) {                      }->{$_->[1]}) {
5114                !!!parse-error (type => 'not closed:'.$_->[1]);                !!!parse-error (type => 'not closed:'.$_->[1]);
5115              }              }
5116            }            }
5117    
5118            $self->{insertion_mode} = 'after body';            $self->{insertion_mode} = AFTER_BODY_IM;
5119            !!!next-token;            !!!next-token;
5120            return;            redo B;
5121          } else {          } else {
5122            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5123            ## Ignore the token            ## Ignore the token
5124            !!!next-token;            !!!next-token;
5125            return;            redo B;
5126          }          }
5127        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
5128          if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {          if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {
# Line 3011  sub _tree_construction_main ($) { Line 5130  sub _tree_construction_main ($) {
5130            if ($self->{open_elements}->[-1]->[1] ne 'body') {            if ($self->{open_elements}->[-1]->[1] ne 'body') {
5131              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);
5132            }            }
5133            $self->{insertion_mode} = 'after body';            $self->{insertion_mode} = AFTER_BODY_IM;
5134            ## reprocess            ## reprocess
5135            return;            redo B;
5136          } else {          } else {
5137            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5138            ## Ignore the token            ## Ignore the token
5139            !!!next-token;            !!!next-token;
5140            return;            redo B;
5141          }          }
5142        } elsif ({        } elsif ({
5143                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
# Line 3040  sub _tree_construction_main ($) { Line 5159  sub _tree_construction_main ($) {
5159                   li => ($token->{tag_name} ne 'li'),                   li => ($token->{tag_name} ne 'li'),
5160                   p => ($token->{tag_name} ne 'p'),                   p => ($token->{tag_name} ne 'p'),
5161                   td => 1, th => 1, tr => 1,                   td => 1, th => 1, tr => 1,
5162                     tbody => 1, tfoot=> 1, thead => 1,
5163                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
5164                !!!back-token;                !!!back-token;
5165                $token = {type => 'end tag',                $token = {type => END_TAG_TOKEN,
5166                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5167                return;                redo B;
5168              }              }
5169              $i = $_;              $i = $_;
5170              last INSCOPE unless $token->{tag_name} eq 'p';              last INSCOPE unless $token->{tag_name} eq 'p';
# Line 3057  sub _tree_construction_main ($) { Line 5177  sub _tree_construction_main ($) {
5177          } # INSCOPE          } # INSCOPE
5178                    
5179          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
5180            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            if (defined $i) {
5181                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
5182              } else {
5183                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5184              }
5185          }          }
5186                    
5187          splice @{$self->{open_elements}}, $i if defined $i;          if (defined $i) {
5188              splice @{$self->{open_elements}}, $i;
5189            } elsif ($token->{tag_name} eq 'p') {
5190              ## As if <p>, then reprocess the current token
5191              my $el;
5192              !!!create-element ($el, 'p');
5193              $insert->($el);
5194            }
5195          $clear_up_to_marker->()          $clear_up_to_marker->()
5196            if {            if {
5197              button => 1, marquee => 1, object => 1,              button => 1, marquee => 1, object => 1,
5198            }->{$token->{tag_name}};            }->{$token->{tag_name}};
5199          !!!next-token;          !!!next-token;
5200          return;          redo B;
5201        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
5202          ## has an element in scope          ## has an element in scope
5203          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 3076  sub _tree_construction_main ($) { Line 5207  sub _tree_construction_main ($) {
5207              if ({              if ({
5208                   dd => 1, dt => 1, li => 1, p => 1,                   dd => 1, dt => 1, li => 1, p => 1,
5209                   td => 1, th => 1, tr => 1,                   td => 1, th => 1, tr => 1,
5210                     tbody => 1, tfoot=> 1, thead => 1,
5211                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
5212                !!!back-token;                !!!back-token;
5213                $token = {type => 'end tag',                $token = {type => END_TAG_TOKEN,
5214                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5215                return;                redo B;
5216              }              }
5217              last INSCOPE;              last INSCOPE;
5218            } elsif ({            } elsif ({
# Line 3094  sub _tree_construction_main ($) { Line 5226  sub _tree_construction_main ($) {
5226          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {
5227            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
5228          } else {          } else {
5229            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5230          }          }
5231    
5232          undef $self->{form_element};          undef $self->{form_element};
5233          !!!next-token;          !!!next-token;
5234          return;          redo B;
5235        } elsif ({        } elsif ({
5236                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5237                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 3114  sub _tree_construction_main ($) { Line 5246  sub _tree_construction_main ($) {
5246              if ({              if ({
5247                   dd => 1, dt => 1, li => 1, p => 1,                   dd => 1, dt => 1, li => 1, p => 1,
5248                   td => 1, th => 1, tr => 1,                   td => 1, th => 1, tr => 1,
5249                     tbody => 1, tfoot=> 1, thead => 1,
5250                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
5251                !!!back-token;                !!!back-token;
5252                $token = {type => 'end tag',                $token = {type => END_TAG_TOKEN,
5253                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5254                return;                redo B;
5255              }              }
5256              $i = $_;              $i = $_;
5257              last INSCOPE;              last INSCOPE;
# Line 3131  sub _tree_construction_main ($) { Line 5264  sub _tree_construction_main ($) {
5264          } # INSCOPE          } # INSCOPE
5265                    
5266          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
5267            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5268          }          }
5269                    
5270          splice @{$self->{open_elements}}, $i if defined $i;          splice @{$self->{open_elements}}, $i if defined $i;
5271          !!!next-token;          !!!next-token;
5272          return;          redo B;
5273        } elsif ({        } elsif ({
5274                  a => 1,                  a => 1,
5275                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
# Line 3144  sub _tree_construction_main ($) { Line 5277  sub _tree_construction_main ($) {
5277                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
5278                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5279          $formatting_end_tag->($token->{tag_name});          $formatting_end_tag->($token->{tag_name});
5280  ## TODO: <http://html5.org/tools/web-apps-tracker?from=883&to=884>          redo B;
5281          return;        } elsif ($token->{tag_name} eq 'br') {
5282            !!!parse-error (type => 'unmatched end tag:br');
5283    
5284            ## As if <br>
5285            $reconstruct_active_formatting_elements->($insert_to_current);
5286            
5287            my $el;
5288            !!!create-element ($el, 'br');
5289            $insert->($el);
5290            
5291            ## Ignore the token.
5292            !!!next-token;
5293            redo B;
5294        } elsif ({        } elsif ({
5295                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
5296                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
5297                  tbody => 1, td => 1, tfoot => 1, th => 1,                  tbody => 1, td => 1, tfoot => 1, th => 1,
5298                  thead => 1, tr => 1,                  thead => 1, tr => 1,
5299                  area => 1, basefont => 1, bgsound => 1, br => 1,                  area => 1, basefont => 1, bgsound => 1,
5300                  embed => 1, hr => 1, iframe => 1, image => 1,                  embed => 1, hr => 1, iframe => 1, image => 1,
5301                  img => 1, input => 1, isindex => 1, noembed => 1,                  img => 1, input => 1, isindex => 1, noembed => 1,
5302                  noframes => 1, param => 1, select => 1, spacer => 1,                  noframes => 1, param => 1, select => 1, spacer => 1,
# Line 3161  sub _tree_construction_main ($) { Line 5306  sub _tree_construction_main ($) {
5306          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5307          ## Ignore the token          ## Ignore the token
5308          !!!next-token;          !!!next-token;
5309          return;          redo B;
5310                    
5311          ## ISSUE: Issue on HTML5 new elements in spec          ## ISSUE: Issue on HTML5 new elements in spec
5312                    
# Line 3178  sub _tree_construction_main ($) { Line 5323  sub _tree_construction_main ($) {
5323              if ({              if ({
5324                   dd => 1, dt => 1, li => 1, p => 1,                   dd => 1, dt => 1, li => 1, p => 1,
5325                   td => 1, th => 1, tr => 1,                   td => 1, th => 1, tr => 1,
5326                     tbody => 1, tfoot => 1, thead => 1,
5327                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
5328                !!!back-token;                !!!back-token;
5329                $token = {type => 'end tag',                $token = {type => END_TAG_TOKEN,
5330                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5331                return;                redo B;
5332              }              }
5333                    
5334              ## Step 2              ## Step 2
5335              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {
5336                  ## NOTE: <x><y></x>
5337                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
5338              }              }
5339                            
# Line 3215  sub _tree_construction_main ($) { Line 5362  sub _tree_construction_main ($) {
5362            ## Step 5;            ## Step 5;
5363            redo S2;            redo S2;
5364          } # S2          } # S2
5365          return;          redo B;
       }  
     }  
   }; # $in_body  
   
   B: {  
     if ($phase eq 'main') {  
       if ($token->{type} eq 'DOCTYPE') {  
         !!!parse-error (type => 'in html:#DOCTYPE');  
         ## Ignore the token  
         ## Stay in the phase  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'start tag' and  
                $token->{tag_name} eq 'html') {  
         ## TODO: unless it is the first start tag token, parse-error  
         my $top_el = $self->{open_elements}->[0]->[0];  
         for my $attr_name (keys %{$token->{attributes}}) {  
           unless ($top_el->has_attribute_ns (undef, $attr_name)) {  
             $top_el->set_attribute_ns  
               (undef, [undef, $attr_name],  
                $token->{attributes}->{$attr_name}->{value});  
           }  
         }  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'end-of-file') {  
         ## Generate implied end tags  
         if ({  
              dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,  
             }->{$self->{open_elements}->[-1]->[1]}) {  
           !!!back-token;  
           $token = {type => 'end tag', tag_name => $self->{open_elements}->[-1]->[1]};  
           redo B;  
         }  
           
         if (@{$self->{open_elements}} > 2 or  
             (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         } elsif (defined $self->{inner_html_node} and  
                  @{$self->{open_elements}} > 1 and  
                  $self->{open_elements}->[1]->[1] ne 'body') {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         }  
   
         ## Stop parsing  
         last B;  
   
         ## ISSUE: There is an issue in the spec.  
       } else {  
         if ($self->{insertion_mode} eq 'before head') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
             ## As if <head>  
             !!!create-element ($self->{head_element}, 'head');  
             $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});  
             push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
             $self->{insertion_mode} = 'in head';  
             ## reprocess  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             my $attr = $token->{tag_name} eq 'head' ? $token->{attributes} : {};  
             !!!create-element ($self->{head_element}, 'head', $attr);  
             $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});  
             push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
             $self->{insertion_mode} = 'in head';  
             if ($token->{tag_name} eq 'head') {  
               !!!next-token;  
             #} elsif ({  
             #          base => 1, link => 1, meta => 1,  
             #          script => 1, style => 1, title => 1,  
             #         }->{$token->{tag_name}}) {  
             #  ## reprocess  
             } else {  
               ## reprocess  
             }  
             redo B;  
           } elsif ($token->{type} eq 'end tag') {  
             if ({head => 1, body => 1, html => 1}->{$token->{tag_name}}) {  
               ## As if <head>  
               !!!create-element ($self->{head_element}, 'head');  
               $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});  
               push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
               $self->{insertion_mode} = 'in head';  
               ## reprocess  
               redo B;  
             } else {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token ## ISSUE: An issue in the spec.  
               !!!next-token;  
               redo B;  
             }  
           } else {  
             die "$0: $token->{type}: Unknown type";  
           }  
         } elsif ($self->{insertion_mode} eq 'in head' or  
                  $self->{insertion_mode} eq 'in head noscript' or  
                  $self->{insertion_mode} eq 'after head') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
               
             #  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ({base => ($self->{insertion_mode} eq 'in head' or  
                           $self->{insertion_mode} eq 'after head'),  
                  link => 1, meta => 1}->{$token->{tag_name}}) {  
               ## NOTE: There is a "as if in head" code clone.  
               if ($self->{insertion_mode} eq 'after head') {  
                 !!!parse-error (type => 'after head:'.$token->{tag_name});  
                 push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
               }  
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.  
               pop @{$self->{open_elements}}  
                   if $self->{insertion_mode} eq 'after head';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'title' and  
                      $self->{insertion_mode} eq 'in head') {  
               ## NOTE: There is a "as if in head" code clone.  
               if ($self->{insertion_mode} eq 'after head') {  
                 !!!parse-error (type => 'after head:'.$token->{tag_name});  
                 push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
               }  
               $parse_rcdata->('RCDATA', $insert_to_current);  
               pop @{$self->{open_elements}}  
                   if $self->{insertion_mode} eq 'after head';  
               redo B;  
             } elsif ($token->{tag_name} eq 'style') {  
               ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and  
               ## insertion mode 'in head')  
               ## NOTE: There is a "as if in head" code clone.  
               if ($self->{insertion_mode} eq 'after head') {  
                 !!!parse-error (type => 'after head:'.$token->{tag_name});  
                 push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
               }  
               $parse_rcdata->('CDATA', $insert_to_current);  
               pop @{$self->{open_elements}}  
                   if $self->{insertion_mode} eq 'after head';  
               redo B;  
             } elsif ($token->{tag_name} eq 'noscript') {  
               if ($self->{insertion_mode} eq 'in head') {  
                 ## NOTE: and scripting is disalbed  
                 !!!insert-element ($token->{tag_name}, $token->{attributes});  
                 $self->{insertion_mode} = 'in head noscript';  
                 !!!next-token;  
                 redo B;  
               } elsif ($self->{insertion_mode} eq 'in head noscript') {  
                 !!!parse-error (type => 'noscript in noscript');  
                 ## Ignore the token  
                 redo B;  
               } else {  
                 #  
               }  
             } elsif ($token->{tag_name} eq 'head' and  
                      $self->{insertion_mode} ne 'after head') {  
               !!!parse-error (type => 'in head:head'); # or in head noscript  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } elsif ($self->{insertion_mode} ne 'in head noscript' and  
                      $token->{tag_name} eq 'script') {  
               if ($self->{insertion_mode} eq 'after head') {  
                 !!!parse-error (type => 'after head:'.$token->{tag_name});  
                 push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
               }  
               ## NOTE: There is a "as if in head" code clone.  
               $script_start_tag->($insert_to_current);  
               pop @{$self->{open_elements}}  
                   if $self->{insertion_mode} eq 'after head';  
               redo B;  
             } elsif ($self->{insertion_mode} eq 'after head' and  
                      $token->{tag_name} eq 'body') {  
               !!!insert-element ('body', $token->{attributes});  
               $self->{insertion_mode} = 'in body';  
               !!!next-token;  
               redo B;  
             } elsif ($self->{insertion_mode} eq 'after head' and  
                      $token->{tag_name} eq 'frameset') {  
               !!!insert-element ('frameset', $token->{attributes});  
               $self->{insertion_mode} = 'in frameset';  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($self->{insertion_mode} eq 'in head' and  
                 $token->{tag_name} eq 'head') {  
               pop @{$self->{open_elements}};  
               $self->{insertion_mode} = 'after head';  
               !!!next-token;  
               redo B;  
             } elsif ($self->{insertion_mode} eq 'in head noscript' and  
                 $token->{tag_name} eq 'noscript') {  
               pop @{$self->{open_elements}};  
               $self->{insertion_mode} = 'in head';  
               !!!next-token;  
               redo B;  
             } elsif ($self->{insertion_mode} eq 'in head' and  
                      ($token->{tag_name} eq 'body' or  
                       $token->{tag_name} eq 'html')) {  
               #  
             } elsif ($self->{insertion_mode} ne 'after head') {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
   
           ## As if </head> or </noscript> or <body>  
           if ($self->{insertion_mode} eq 'in head') {  
             pop @{$self->{open_elements}};  
             $self->{insertion_mode} = 'after head';  
           } elsif ($self->{insertion_mode} eq 'in head noscript') {  
             pop @{$self->{open_elements}};  
             !!!parse-error (type => 'in noscript:'.(defined $token->{tag_name} ? ($token->{type} eq 'end tag' ? '/' : '') . $token->{tag_name} : '#' . $token->{type}));  
             $self->{insertion_mode} = 'in head';  
           } else { # 'after head'  
             !!!insert-element ('body');  
             $self->{insertion_mode} = 'in body';  
           }  
           ## reprocess  
           redo B;  
   
           ## ISSUE: An issue in the spec.  
         } elsif ($self->{insertion_mode} eq 'in body') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: There is a code clone of "character in body".  
             $reconstruct_active_formatting_elements->($insert_to_current);  
               
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
   
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             ## NOTE: There is a code clone of "comment in body".  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } else {  
             $in_body->($insert_to_current);  
             redo B;  
           }  
         } elsif ($self->{insertion_mode} eq 'in table') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: There are "character in table" code clones.  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
                 
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
   
             !!!parse-error (type => 'in table:#character');  
   
             ## As if in body, but insert into foster parent element  
             ## ISSUE: Spec says that "whenever a node would be inserted  
             ## into the current node" while characters might not be  
             ## result in a new Text node.  
             $reconstruct_active_formatting_elements->($insert_to_foster);  
               
             if ({  
                  table => 1, tbody => 1, tfoot => 1,  
                  thead => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               # MUST  
               my $foster_parent_element;  
               my $next_sibling;  
               my $prev_sibling;  
               OE: for (reverse 0..$#{$self->{open_elements}}) {  
                 if ($self->{open_elements}->[$_]->[1] eq 'table') {  
                   my $parent = $self->{open_elements}->[$_]->[0]->parent_node;  
                   if (defined $parent and $parent->node_type == 1) {  
                     $foster_parent_element = $parent;  
                     $next_sibling = $self->{open_elements}->[$_]->[0];  
                     $prev_sibling = $next_sibling->previous_sibling;  
                   } else {  
                     $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];  
                     $prev_sibling = $foster_parent_element->last_child;  
                   }  
                   last OE;  
                 }  
               } # OE  
               $foster_parent_element = $self->{open_elements}->[0]->[0] and  
               $prev_sibling = $foster_parent_element->last_child  
                 unless defined $foster_parent_element;  
               if (defined $prev_sibling and  
                   $prev_sibling->node_type == 3) {  
                 $prev_sibling->manakai_append_text ($token->{data});  
               } else {  
                 $foster_parent_element->insert_before  
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
               }  
             } else {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
             }  
               
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ({  
                  caption => 1,  
                  colgroup => 1,  
                  tbody => 1, tfoot => 1, thead => 1,  
                 }->{$token->{tag_name}}) {  
               ## Clear back to table context  
               while ($self->{open_elements}->[-1]->[1] ne 'table' and  
                      $self->{open_elements}->[-1]->[1] ne 'html') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               push @$active_formatting_elements, ['#marker', '']  
                 if $token->{tag_name} eq 'caption';  
   
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               $self->{insertion_mode} = {  
                                  caption => 'in caption',  
                                  colgroup => 'in column group',  
                                  tbody => 'in table body',  
                                  tfoot => 'in table body',  
                                  thead => 'in table body',  
                                 }->{$token->{tag_name}};  
               !!!next-token;  
               redo B;  
             } elsif ({  
                       col => 1,  
                       td => 1, th => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               ## Clear back to table context  
               while ($self->{open_elements}->[-1]->[1] ne 'table' and  
                      $self->{open_elements}->[-1]->[1] ne 'html') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               !!!insert-element ($token->{tag_name} eq 'col' ? 'colgroup' : 'tbody');  
               $self->{insertion_mode} = $token->{tag_name} eq 'col'  
                 ? 'in column group' : 'in table body';  
               ## reprocess  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## NOTE: There are code clones for this "table in table"  
               !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
   
               ## As if </table>  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'table') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:table');  
                 ## Ignore tokens </table><table>  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # <table>  
                 $token = {type => 'end tag', tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'table') {  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               !!!next-token;  
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1, colgroup => 1,  
                       html => 1, tbody => 1, td => 1, tfoot => 1, th => 1,  
                       thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
   
           !!!parse-error (type => 'in table:'.$token->{tag_name});  
           $in_body->($insert_to_foster);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in caption') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a code clone of "character in body".  
             $reconstruct_active_formatting_elements->($insert_to_current);  
               
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
   
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             ## NOTE: This is a code clone of "comment in body".  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ({  
                  caption => 1, col => 1, colgroup => 1, tbody => 1,  
                  td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,  
                 }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'not closed:caption');  
   
               ## As if </caption>  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'caption') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:caption');  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # <?>  
                 $token = {type => 'end tag', tag_name => 'caption'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'caption') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
   
               $self->{insertion_mode} = 'in table';  
   
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'caption') {  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'caption') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
   
               $self->{insertion_mode} = 'in table';  
   
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               !!!parse-error (type => 'not closed:caption');  
   
               ## As if </caption>  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'caption') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:caption');  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # </table>  
                 $token = {type => 'end tag', tag_name => 'caption'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'caption') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
   
               $self->{insertion_mode} = 'in table';  
   
               ## reprocess  
               redo B;  
             } elsif ({  
                       body => 1, col => 1, colgroup => 1,  
                       html => 1, tbody => 1, td => 1, tfoot => 1,  
                       th => 1, thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
                 
           $in_body->($insert_to_current);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in column group') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
               
             #  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'col') {  
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               pop @{$self->{open_elements}};  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'colgroup') {  
               if ($self->{open_elements}->[-1]->[1] eq 'html') {  
                 !!!parse-error (type => 'unmatched end tag:colgroup');  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               } else {  
                 pop @{$self->{open_elements}}; # colgroup  
                 $self->{insertion_mode} = 'in table';  
                 !!!next-token;  
                 redo B;              
               }  
             } elsif ($token->{tag_name} eq 'col') {  
               !!!parse-error (type => 'unmatched end tag:col');  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
   
           ## As if </colgroup>  
           if ($self->{open_elements}->[-1]->[1] eq 'html') {  
             !!!parse-error (type => 'unmatched end tag:colgroup');  
             ## Ignore the token  
             !!!next-token;  
             redo B;  
           } else {  
             pop @{$self->{open_elements}}; # colgroup  
             $self->{insertion_mode} = 'in table';  
             ## reprocess  
             redo B;  
           }  
         } elsif ($self->{insertion_mode} eq 'in table body') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a "character in table" code clone.  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
                 
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
   
             !!!parse-error (type => 'in table:#character');  
   
             ## As if in body, but insert into foster parent element  
             ## ISSUE: Spec says that "whenever a node would be inserted  
             ## into the current node" while characters might not be  
             ## result in a new Text node.  
             $reconstruct_active_formatting_elements->($insert_to_foster);  
   
             if ({  
                  table => 1, tbody => 1, tfoot => 1,  
                  thead => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               # MUST  
               my $foster_parent_element;  
               my $next_sibling;  
               my $prev_sibling;  
               OE: for (reverse 0..$#{$self->{open_elements}}) {  
                 if ($self->{open_elements}->[$_]->[1] eq 'table') {  
                   my $parent = $self->{open_elements}->[$_]->[0]->parent_node;  
                   if (defined $parent and $parent->node_type == 1) {  
                     $foster_parent_element = $parent;  
                     $next_sibling = $self->{open_elements}->[$_]->[0];  
                     $prev_sibling = $next_sibling->previous_sibling;  
                   } else {  
                     $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];  
                     $prev_sibling = $foster_parent_element->last_child;  
                   }  
                   last OE;  
                 }  
               } # OE  
               $foster_parent_element = $self->{open_elements}->[0]->[0] and  
               $prev_sibling = $foster_parent_element->last_child  
                 unless defined $foster_parent_element;  
               if (defined $prev_sibling and  
                   $prev_sibling->node_type == 3) {  
                 $prev_sibling->manakai_append_text ($token->{data});  
               } else {  
                 $foster_parent_element->insert_before  
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
               }  
             } else {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
             }  
               
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             ## Copied from 'in table'  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ({  
                  tr => 1,  
                  th => 1, td => 1,  
                 }->{$token->{tag_name}}) {  
               unless ($token->{tag_name} eq 'tr') {  
                 !!!parse-error (type => 'missing start tag:tr');  
               }  
   
               ## Clear back to table body context  
               while (not {  
                 tbody => 1, tfoot => 1, thead => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
                 
               $self->{insertion_mode} = 'in row';  
               if ($token->{tag_name} eq 'tr') {  
                 !!!insert-element ($token->{tag_name}, $token->{attributes});  
                 !!!next-token;  
               } else {  
                 !!!insert-element ('tr');  
                 ## reprocess  
               }  
               redo B;  
             } elsif ({  
                       caption => 1, col => 1, colgroup => 1,  
                       tbody => 1, tfoot => 1, thead => 1,  
                      }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ({  
                      tbody => 1, thead => 1, tfoot => 1,  
                     }->{$node->[1]}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table body context  
               while (not {  
                 tbody => 1, tfoot => 1, thead => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               ## As if <{current node}>  
               ## have an element in table scope  
               ## true by definition  
   
               ## Clear back to table body context  
               ## nop by definition  
   
               pop @{$self->{open_elements}};  
               $self->{insertion_mode} = 'in table';  
               ## reprocess  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## NOTE: This is a code clone of "table in table"  
               !!!parse-error (type => 'not closed:table');  
   
               ## As if </table>  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'table') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:table');  
                 ## Ignore tokens </table><table>  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # <table>  
                 $token = {type => 'end tag', tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ({  
                  tbody => 1, tfoot => 1, thead => 1,  
                 }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table body context  
               while (not {  
                 tbody => 1, tfoot => 1, thead => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               pop @{$self->{open_elements}};  
               $self->{insertion_mode} = 'in table';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ({  
                      tbody => 1, thead => 1, tfoot => 1,  
                     }->{$node->[1]}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table body context  
               while (not {  
                 tbody => 1, tfoot => 1, thead => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               ## As if <{current node}>  
               ## have an element in table scope  
               ## true by definition  
   
               ## Clear back to table body context  
               ## nop by definition  
   
               pop @{$self->{open_elements}};  
               $self->{insertion_mode} = 'in table';  
               ## reprocess  
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1, colgroup => 1,  
                       html => 1, td => 1, th => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
             
           ## As if in table  
           !!!parse-error (type => 'in table:'.$token->{tag_name});  
           $in_body->($insert_to_foster);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in row') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a "character in table" code clone.  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
                 
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
   
             !!!parse-error (type => 'in table:#character');  
   
             ## As if in body, but insert into foster parent element  
             ## ISSUE: Spec says that "whenever a node would be inserted  
             ## into the current node" while characters might not be  
             ## result in a new Text node.  
             $reconstruct_active_formatting_elements->($insert_to_foster);  
               
             if ({  
                  table => 1, tbody => 1, tfoot => 1,  
                  thead => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               # MUST  
               my $foster_parent_element;  
               my $next_sibling;  
               my $prev_sibling;  
               OE: for (reverse 0..$#{$self->{open_elements}}) {  
                 if ($self->{open_elements}->[$_]->[1] eq 'table') {  
                   my $parent = $self->{open_elements}->[$_]->[0]->parent_node;  
                   if (defined $parent and $parent->node_type == 1) {  
                     $foster_parent_element = $parent;  
                     $next_sibling = $self->{open_elements}->[$_]->[0];  
                     $prev_sibling = $next_sibling->previous_sibling;  
                   } else {  
                     $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];  
                     $prev_sibling = $foster_parent_element->last_child;  
                   }  
                   last OE;  
                 }  
               } # OE  
               $foster_parent_element = $self->{open_elements}->[0]->[0] and  
               $prev_sibling = $foster_parent_element->last_child  
                 unless defined $foster_parent_element;  
               if (defined $prev_sibling and  
                   $prev_sibling->node_type == 3) {  
                 $prev_sibling->manakai_append_text ($token->{data});  
               } else {  
                 $foster_parent_element->insert_before  
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
               }  
             } else {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
             }  
               
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             ## Copied from 'in table'  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'th' or  
                 $token->{tag_name} eq 'td') {  
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
                 
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               $self->{insertion_mode} = 'in cell';  
   
               push @$active_formatting_elements, ['#marker', ''];  
                 
               !!!next-token;  
               redo B;  
             } elsif ({  
                       caption => 1, col => 1, colgroup => 1,  
                       tbody => 1, tfoot => 1, thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               ## As if </tr>  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'tr') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               ## reprocess  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## NOTE: This is a code clone of "table in table"  
               !!!parse-error (type => 'not closed:table');  
   
               ## As if </table>  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'table') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:table');  
                 ## Ignore tokens </table><table>  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # <table>  
                 $token = {type => 'end tag', tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'tr') {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## As if </tr>  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'tr') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{type});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               ## reprocess  
               redo B;  
             } elsif ({  
                       tbody => 1, tfoot => 1, thead => 1,  
                      }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## As if </tr>  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'tr') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:tr');  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               ## reprocess  
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1,  
                       colgroup => 1, html => 1, td => 1, th => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
   
           ## As if in table  
           !!!parse-error (type => 'in table:'.$token->{tag_name});  
           $in_body->($insert_to_foster);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in cell') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a code clone of "character in body".  
             $reconstruct_active_formatting_elements->($insert_to_current);  
               
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
   
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             ## NOTE: This is a code clone of "comment in body".  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ({  
                  caption => 1, col => 1, colgroup => 1,  
                  tbody => 1, td => 1, tfoot => 1, th => 1,  
                  thead => 1, tr => 1,  
                 }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $tn;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'td' or $node->[1] eq 'th') {  
                   $tn = $node->[1];  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $tn) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Close the cell  
               !!!back-token; # <?>  
               $token = {type => 'end tag', tag_name => $tn};  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => ($token->{tag_name} eq 'th'),  
                    th => ($token->{tag_name} eq 'td'),  
                    tr => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
   
               $self->{insertion_mode} = 'in row';  
   
               !!!next-token;  
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1,  
                       colgroup => 1, html => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } elsif ({  
                       table => 1, tbody => 1, tfoot => 1,  
                       thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $i;  
               my $tn;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {  
                   $tn = $node->[1];  
                   ## NOTE: There is exactly one |td| or |th| element  
                   ## in scope in the stack of open elements by definition.  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => 'end tag', tag_name => $tn};  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
             
           $in_body->($insert_to_current);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in select') {  
           if ($token->{type} eq 'character') {  
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'option') {  
               if ($self->{open_elements}->[-1]->[1] eq 'option') {  
                 ## As if </option>  
                 pop @{$self->{open_elements}};  
               }  
   
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'optgroup') {  
               if ($self->{open_elements}->[-1]->[1] eq 'option') {  
                 ## As if </option>  
                 pop @{$self->{open_elements}};  
               }  
   
               if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {  
                 ## As if </optgroup>  
                 pop @{$self->{open_elements}};  
               }  
   
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'select') {  
               !!!parse-error (type => 'not closed:select');  
               ## As if </select> instead  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:select');  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'optgroup') {  
               if ($self->{open_elements}->[-1]->[1] eq 'option' and  
                   $self->{open_elements}->[-2]->[1] eq 'optgroup') {  
                 ## As if </option>  
                 splice @{$self->{open_elements}}, -2;  
               } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {  
                 pop @{$self->{open_elements}};  
               } else {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
               }  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'option') {  
               if ($self->{open_elements}->[-1]->[1] eq 'option') {  
                 pop @{$self->{open_elements}};  
               } else {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
               }  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'select') {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               !!!next-token;  
               redo B;  
             } elsif ({  
                       caption => 1, table => 1, tbody => 1,  
                       tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## As if </select>  
               ## have an element in table scope  
               undef $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'select') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:select');  
                 ## Ignore the </select> token  
                 !!!next-token; ## TODO: ok?  
                 redo B;  
               }  
                 
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
   
           !!!parse-error (type => 'in select:'.$token->{tag_name});  
           ## Ignore the token  
           !!!next-token;  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'after body') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               ## As if in body  
               $reconstruct_active_formatting_elements->($insert_to_current);  
                 
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
   
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
               
             #  
             !!!parse-error (type => 'after body:#'.$token->{type});  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[0]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             !!!parse-error (type => 'after body:'.$token->{tag_name});  
             #  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'html') {  
               if (defined $self->{inner_html_node}) {  
                 !!!parse-error (type => 'unmatched end tag:html');  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               } else {  
                 $phase = 'trailing end';  
                 !!!next-token;  
                 redo B;  
               }  
             } else {  
               !!!parse-error (type => 'after body:/'.$token->{tag_name});  
             }  
           } else {  
             !!!parse-error (type => 'after body:#'.$token->{type});  
           }  
   
           $self->{insertion_mode} = 'in body';  
           ## reprocess  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in frameset') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
   
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
   
             #  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'frameset') {  
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'frame') {  
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               pop @{$self->{open_elements}};  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'noframes') {  
               $in_body->($insert_to_current);  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'frameset') {  
               if ($self->{open_elements}->[-1]->[1] eq 'html' and  
                   @{$self->{open_elements}} == 1) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
               } else {  
                 pop @{$self->{open_elements}};  
                 !!!next-token;  
               }  
                 
               ## if not inner_html and  
               if ($self->{open_elements}->[-1]->[1] ne 'frameset') {  
                 $self->{insertion_mode} = 'after frameset';  
               }  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
             
           if (defined $token->{tag_name}) {  
             !!!parse-error (type => 'in frameset:'.$token->{tag_name});  
           } else {  
             !!!parse-error (type => 'in frameset:#'.$token->{type});  
           }  
           ## Ignore the token  
           !!!next-token;  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'after frameset') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
   
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
   
             #  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'noframes') {  
               $in_body->($insert_to_current);  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'html') {  
               $phase = 'trailing end';  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
             
           if (defined $token->{tag_name}) {  
             !!!parse-error (type => 'after frameset:'.$token->{tag_name});  
           } else {  
             !!!parse-error (type => 'after frameset:#'.$token->{type});  
           }  
           ## Ignore the token  
           !!!next-token;  
           redo B;  
   
           ## ISSUE: An issue in spec there  
         } else {  
           die "$0: $self->{insertion_mode}: Unknown insertion mode";  
         }  
       }  
     } elsif ($phase eq 'trailing end') {  
       ## states in the main stage is preserved yet # MUST  
         
       if ($token->{type} eq 'DOCTYPE') {  
         !!!parse-error (type => 'after html:#DOCTYPE');  
         ## Ignore the token  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'comment') {  
         my $comment = $self->{document}->create_comment ($token->{data});  
         $self->{document}->append_child ($comment);  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'character') {  
         if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
           my $data = $1;  
           ## As if in the main phase.  
           ## NOTE: The insertion mode in the main phase  
           ## just before the phase has been changed to the trailing  
           ## end phase is either "after body" or "after frameset".  
           $reconstruct_active_formatting_elements->($insert_to_current)  
             if $phase eq 'main';  
             
           $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);  
             
           unless (length $token->{data}) {  
             !!!next-token;  
             redo B;  
           }  
         }  
   
         !!!parse-error (type => 'after html:#character');  
         $phase = 'main';  
         ## reprocess  
         redo B;  
       } elsif ($token->{type} eq 'start tag' or  
                $token->{type} eq 'end tag') {  
         !!!parse-error (type => 'after html:'.$token->{tag_name});  
         $phase = 'main';  
         ## reprocess  
         redo B;  
       } elsif ($token->{type} eq 'end-of-file') {  
         ## Stop parsing  
         last B;  
       } else {  
         die "$0: $token->{type}: Unknown token";  
5366        }        }
5367      }      }
5368        redo B;
5369    } # B    } # B
5370    
5371      ## NOTE: The "trailing end" phase in HTML5 is split into
5372      ## two insertion modes: "after html body" and "after html frameset".
5373      ## NOTE: States in the main stage is preserved while
5374      ## the parser stays in the trailing end phase. # MUST
5375    
5376    ## Stop parsing # MUST    ## Stop parsing # MUST
5377        
5378    ## TODO: script stuffs    ## TODO: script stuffs
# Line 5094  sub set_inner_html ($$$) { Line 5384  sub set_inner_html ($$$) {
5384    my $s = \$_[0];    my $s = \$_[0];
5385    my $onerror = $_[1];    my $onerror = $_[1];
5386    
5387      ## ISSUE: Should {confident} be true?
5388    
5389    my $nt = $node->node_type;    my $nt = $node->node_type;
5390    if ($nt == 9) {    if ($nt == 9) {
5391      # MUST      # MUST
# Line 5167  sub set_inner_html ($$$) { Line 5459  sub set_inner_html ($$$) {
5459    
5460      ## Step 2      ## Step 2
5461      my $node_ln = $node->local_name;      my $node_ln = $node->local_name;
5462      $p->{content_model_flag} = {      $p->{content_model} = {
5463        title => 'RCDATA',        title => RCDATA_CONTENT_MODEL,
5464        textarea => 'RCDATA',        textarea => RCDATA_CONTENT_MODEL,
5465        style => 'CDATA',        style => CDATA_CONTENT_MODEL,
5466        script => 'CDATA',        script => CDATA_CONTENT_MODEL,
5467        xmp => 'CDATA',        xmp => CDATA_CONTENT_MODEL,
5468        iframe => 'CDATA',        iframe => CDATA_CONTENT_MODEL,
5469        noembed => 'CDATA',        noembed => CDATA_CONTENT_MODEL,
5470        noframes => 'CDATA',        noframes => CDATA_CONTENT_MODEL,
5471        noscript => 'CDATA',        noscript => CDATA_CONTENT_MODEL,
5472        plaintext => 'PLAINTEXT',        plaintext => PLAINTEXT_CONTENT_MODEL,
5473      }->{$node_ln} || 'PCDATA';      }->{$node_ln};
5474         ## ISSUE: What is "the name of the element"? local name?      $p->{content_model} = PCDATA_CONTENT_MODEL
5475            unless defined $p->{content_model};
5476            ## ISSUE: What is "the name of the element"? local name?
5477    
5478      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $node_ln];
5479    
# Line 5244  sub set_inner_html ($$$) { Line 5538  sub set_inner_html ($$$) {
5538    
5539  } # tree construction stage  } # tree construction stage
5540    
5541  sub get_inner_html ($$$) {  package Whatpm::HTML::RestartParser;
5542    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 = lc $child->tag_name; ## ISSUE: Definition of "lowercase"  
       $s .= '<' . $tag_name;  
   
       ## ISSUE: Non-html elements  
   
       my @attrs = @{$child->attributes}; # sort order MUST be stable  
       for my $attr (@attrs) { # order is implementation dependent  
         my $attr_name = lc $attr->name; ## ISSUE: Definition of "lowercase"  
         $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,  
       }->{$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  
5543    
5544  1;  1;
5545  # $Date$  # $Date$

Legend:
Removed from v.1.25  
changed lines
  Added in v.1.69

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24