/[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.24 by wakaba, Sat Jun 23 16:42:43 2007 UTC revision 1.63 by wakaba, Sun Nov 11 06:54:36 2007 UTC
# Line 1  Line 1 
1  package Whatpm::HTML;  package Whatpm::HTML;
2  use strict;  use strict;
3  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
4    use Error qw(:try);
5    
6  ## ISSUE:  ## ISSUE:
7  ## var doc = implementation.createDocument (null, null, null);  ## var doc = implementation.createDocument (null, null, null);
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;
96        $s = \ (Encode::decode ($charset, $$bytes_s));
97        $self->{input_encoding} = lc $charset; ## TODO: normalize name ## TODO: set $doc->input_encoding
98        $self->{confident} = 1;
99      } else {
100        $s = ref $_[0] ? $_[0] : \($_[0]);
101        $self->{confident} = 0;
102      }
103    
104      $self->{change_encoding} = sub {
105        my $self = shift;
106        my $charset = lc shift;
107        ## TODO: if $charset is supported
108        ## TODO: normalize charset name
109    
110        ## "Change the encoding" algorithm:
111    
112        ## Step 1    
113        if ($charset eq 'utf-16') { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?
114          $charset = 'utf-8';
115        }
116    
117        ## Step 2
118        if (defined $self->{input_encoding} and
119            $self->{input_encoding} eq $charset) {
120          $self->{confident} = 1;
121          return;
122        }
123    
124        !!!parse-error (type => 'charset label detected', level => 'w');
125    
126        ## Step 3
127        # if (can) {
128          ## change the encoding on the fly.
129          #$self->{confident} = 1;
130          #return;
131        # }
132    
133        ## Step 4
134        throw Whatpm::HTML::RestartParser (charset => $charset);
135      }; # $self->{change_encoding}
136    
137      my @args = @_; shift @args; # $s
138      my $return;
139      try {
140        $return = $self->parse_char_string ($s, @args);  
141      } catch Whatpm::HTML::RestartParser with {
142        my $charset = shift->{charset};
143        $s = \ (Encode::decode ($charset, $$bytes_s));    
144        $self->{input_encoding} = $charset; ## TODO: $doc->input_encoding;
145        $self->{confident} = 1;
146        $return = $self->parse_char_string ($s, @args);
147      };
148      return $return;
149    } # parse_byte_string
150    
151    *parse_char_string = \&parse_string;
152    
153  sub parse_string ($$$;$) {  sub parse_string ($$$;$) {
154    my $self = shift->new;    my $self = ref $_[0] ? shift : shift->new;
155    my $s = \$_[0];    my $s = ref $_[0] ? $_[0] : \($_[0]);
156    $self->{document} = $_[1];    $self->{document} = $_[1];
157      @{$self->{document}->child_nodes} = ();
158    
159    ## NOTE: |set_inner_html| copies most of this method's code    ## NOTE: |set_inner_html| copies most of this method's code
160    
161      $self->{confident} = 1 unless exists $self->{confident};
162    
163    my $i = 0;    my $i = 0;
164    my $line = 1;    my $line = 1;
165    my $column = 0;    my $column = 0;
# Line 141  sub new ($) { Line 216  sub new ($) {
216    $self->{parse_error} = sub {    $self->{parse_error} = sub {
217      #      #
218    };    };
219      $self->{change_encoding} = sub {
220        # if ($_[0] is a supported encoding) {
221        #   run "change the encoding" algorithm;
222        #   throw Whatpm::HTML::RestartParser (charset => $new_encoding);
223        # }
224      };
225      $self->{application_cache_selection} = sub {
226        #
227      };
228    return $self;    return $self;
229  } # new  } # new
230    
231    sub CM_ENTITY () { 0b001 } # & markup in data
232    sub CM_LIMITED_MARKUP () { 0b010 } # < markup in data (limited)
233    sub CM_FULL_MARKUP () { 0b100 } # < markup in data (any)
234    
235    sub PLAINTEXT_CONTENT_MODEL () { 0 }
236    sub CDATA_CONTENT_MODEL () { CM_LIMITED_MARKUP }
237    sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }
238    sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
239    
240    sub DATA_STATE () { 0 }
241    sub ENTITY_DATA_STATE () { 1 }
242    sub TAG_OPEN_STATE () { 2 }
243    sub CLOSE_TAG_OPEN_STATE () { 3 }
244    sub TAG_NAME_STATE () { 4 }
245    sub BEFORE_ATTRIBUTE_NAME_STATE () { 5 }
246    sub ATTRIBUTE_NAME_STATE () { 6 }
247    sub AFTER_ATTRIBUTE_NAME_STATE () { 7 }
248    sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 }
249    sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
250    sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
251    sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
252    sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
253    sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
254    sub COMMENT_START_STATE () { 14 }
255    sub COMMENT_START_DASH_STATE () { 15 }
256    sub COMMENT_STATE () { 16 }
257    sub COMMENT_END_STATE () { 17 }
258    sub COMMENT_END_DASH_STATE () { 18 }
259    sub BOGUS_COMMENT_STATE () { 19 }
260    sub DOCTYPE_STATE () { 20 }
261    sub BEFORE_DOCTYPE_NAME_STATE () { 21 }
262    sub DOCTYPE_NAME_STATE () { 22 }
263    sub AFTER_DOCTYPE_NAME_STATE () { 23 }
264    sub BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 24 }
265    sub DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE () { 25 }
266    sub DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE () { 26 }
267    sub AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 27 }
268    sub BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 28 }
269    sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE () { 29 }
270    sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }
271    sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
272    sub BOGUS_DOCTYPE_STATE () { 32 }
273    
274    sub DOCTYPE_TOKEN () { 1 }
275    sub COMMENT_TOKEN () { 2 }
276    sub START_TAG_TOKEN () { 3 }
277    sub END_TAG_TOKEN () { 4 }
278    sub END_OF_FILE_TOKEN () { 5 }
279    sub CHARACTER_TOKEN () { 6 }
280    
281    sub AFTER_HTML_IMS () { 0b100 }
282    sub HEAD_IMS ()       { 0b1000 }
283    sub BODY_IMS ()       { 0b10000 }
284    sub BODY_TABLE_IMS () { 0b100000 }
285    sub TABLE_IMS ()      { 0b1000000 }
286    sub ROW_IMS ()        { 0b10000000 }
287    sub BODY_AFTER_IMS () { 0b100000000 }
288    sub FRAME_IMS ()      { 0b1000000000 }
289    
290    sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
291    sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
292    sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
293    sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
294    sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
295    sub BEFORE_HEAD_IM () { HEAD_IMS | 0b11 }
296    sub IN_BODY_IM () { BODY_IMS }
297    sub IN_CELL_IM () { BODY_IMS | BODY_TABLE_IMS | 0b01 }
298    sub IN_CAPTION_IM () { BODY_IMS | BODY_TABLE_IMS | 0b10 }
299    sub IN_ROW_IM () { TABLE_IMS | ROW_IMS | 0b01 }
300    sub IN_TABLE_BODY_IM () { TABLE_IMS | ROW_IMS | 0b10 }
301    sub IN_TABLE_IM () { TABLE_IMS }
302    sub AFTER_BODY_IM () { BODY_AFTER_IMS }
303    sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
304    sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
305    sub IN_SELECT_IM () { 0b01 }
306    sub IN_COLUMN_GROUP_IM () { 0b10 }
307    
308  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
309    
310  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
311    my $self = shift;    my $self = shift;
312    $self->{state} = 'data'; # MUST    $self->{state} = DATA_STATE; # MUST
313    $self->{content_model_flag} = 'PCDATA'; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
314    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE
315    undef $self->{current_attribute};    undef $self->{current_attribute};
316    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
# Line 162  sub _initialize_tokenizer ($) { Line 323  sub _initialize_tokenizer ($) {
323  } # _initialize_tokenizer  } # _initialize_tokenizer
324    
325  ## A token has:  ## A token has:
326  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',  ##   ->{type} == DOCTYPE_TOKEN, START_TAG_TOKEN, END_TAG_TOKEN, COMMENT_TOKEN,
327  ##       'character', or 'end-of-file'  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN
328  ##   ->{name} (DOCTYPE, start tag (tag name), end tag (tag name))  ##   ->{name} (DOCTYPE_TOKEN)
329  ##   ->{public_identifier} (DOCTYPE)  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
330  ##   ->{system_identifier} (DOCTYPE)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
331  ##   ->{correct} == 1 or 0 (DOCTYPE)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
332  ##   ->{attributes} isa HASH (start tag, end tag)  ##   ->{correct} == 1 or 0 (DOCTYPE_TOKEN)
333  ##   ->{data} (comment, character)  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
334    ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
335    
336  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
337    
# Line 179  sub _initialize_tokenizer ($) { Line 341  sub _initialize_tokenizer ($) {
341  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
342  ## and removed from the list.  ## and removed from the list.
343    
344    ## NOTE: HTML5 "Writing HTML documents" section, applied to
345    ## documents and not to user agents and conformance checkers,
346    ## contains some requirements that are not detected by the
347    ## parsing algorithm:
348    ## - Some requirements on character encoding declarations. ## TODO
349    ## - "Elements MUST NOT contain content that their content model disallows."
350    ##   ... Some are parse error, some are not (will be reported by c.c.).
351    ## - Polytheistic slash SHOULD NOT be used. (Applied only to atheists.) ## TODO
352    ## - Text (in elements, attributes, and comments) SHOULD NOT contain
353    ##   control characters other than space characters. ## TODO: (what is control character? C0, C1 and DEL?  Unicode control character?)
354    
355    ## TODO: HTML5 poses authors two SHOULD-level requirements that cannot
356    ## be detected by the HTML5 parsing algorithm:
357    ## - Text,
358    
359  sub _get_next_token ($) {  sub _get_next_token ($) {
360    my $self = shift;    my $self = shift;
361    if (@{$self->{token}}) {    if (@{$self->{token}}) {
# Line 186  sub _get_next_token ($) { Line 363  sub _get_next_token ($) {
363    }    }
364    
365    A: {    A: {
366      if ($self->{state} eq 'data') {      if ($self->{state} == DATA_STATE) {
367        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_input_character} == 0x0026) { # &
368          if ($self->{content_model_flag} eq 'PCDATA' or          if ($self->{content_model} & CM_ENTITY) { # PCDATA | RCDATA
369              $self->{content_model_flag} eq 'RCDATA') {            $self->{state} = ENTITY_DATA_STATE;
           $self->{state} = 'entity data';  
370            !!!next-input-character;            !!!next-input-character;
371            redo A;            redo A;
372          } else {          } else {
373            #            #
374          }          }
375        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_input_character} == 0x002D) { # -
376          if ($self->{content_model_flag} eq 'RCDATA' or          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
             $self->{content_model_flag} eq 'CDATA') {  
377            unless ($self->{escape}) {            unless ($self->{escape}) {
378              if ($self->{prev_input_character}->[0] == 0x002D and # -              if ($self->{prev_input_character}->[0] == 0x002D and # -
379                  $self->{prev_input_character}->[1] == 0x0021 and # !                  $self->{prev_input_character}->[1] == 0x0021 and # !
# Line 210  sub _get_next_token ($) { Line 385  sub _get_next_token ($) {
385                    
386          #          #
387        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_input_character} == 0x003C) { # <
388          if ($self->{content_model_flag} eq 'PCDATA' or          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
389              (($self->{content_model_flag} eq 'CDATA' or              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
               $self->{content_model_flag} eq 'RCDATA') and  
390               not $self->{escape})) {               not $self->{escape})) {
391            $self->{state} = 'tag open';            $self->{state} = TAG_OPEN_STATE;
392            !!!next-input-character;            !!!next-input-character;
393            redo A;            redo A;
394          } else {          } else {
# Line 222  sub _get_next_token ($) { Line 396  sub _get_next_token ($) {
396          }          }
397        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
398          if ($self->{escape} and          if ($self->{escape} and
399              ($self->{content_model_flag} eq 'RCDATA' or              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
              $self->{content_model_flag} eq 'CDATA')) {  
400            if ($self->{prev_input_character}->[0] == 0x002D and # -            if ($self->{prev_input_character}->[0] == 0x002D and # -
401                $self->{prev_input_character}->[1] == 0x002D) { # -                $self->{prev_input_character}->[1] == 0x002D) { # -
402              delete $self->{escape};              delete $self->{escape};
# Line 232  sub _get_next_token ($) { Line 405  sub _get_next_token ($) {
405                    
406          #          #
407        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
408          !!!emit ({type => 'end-of-file'});          !!!emit ({type => END_OF_FILE_TOKEN});
409          last A; ## TODO: ok?          last A; ## TODO: ok?
410        }        }
411        # Anything else        # Anything else
412        my $token = {type => 'character',        my $token = {type => CHARACTER_TOKEN,
413                     data => chr $self->{next_input_character}};                     data => chr $self->{next_input_character}};
414        ## Stay in the data state        ## Stay in the data state
415        !!!next-input-character;        !!!next-input-character;
# Line 244  sub _get_next_token ($) { Line 417  sub _get_next_token ($) {
417        !!!emit ($token);        !!!emit ($token);
418    
419        redo A;        redo A;
420      } elsif ($self->{state} eq 'entity data') {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
421        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
422                
423        my $token = $self->_tokenize_attempt_to_consume_an_entity;        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);
424    
425        $self->{state} = 'data';        $self->{state} = DATA_STATE;
426        # next-input-character is already done        # next-input-character is already done
427    
428        unless (defined $token) {        unless (defined $token) {
429          !!!emit ({type => 'character', data => '&'});          !!!emit ({type => CHARACTER_TOKEN, data => '&'});
430        } else {        } else {
431          !!!emit ($token);          !!!emit ($token);
432        }        }
433    
434        redo A;        redo A;
435      } elsif ($self->{state} eq 'tag open') {      } elsif ($self->{state} == TAG_OPEN_STATE) {
436        if ($self->{content_model_flag} eq 'RCDATA' or        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
           $self->{content_model_flag} eq 'CDATA') {  
437          if ($self->{next_input_character} == 0x002F) { # /          if ($self->{next_input_character} == 0x002F) { # /
438            !!!next-input-character;            !!!next-input-character;
439            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
440            redo A;            redo A;
441          } else {          } else {
442            ## reconsume            ## reconsume
443            $self->{state} = 'data';            $self->{state} = DATA_STATE;
444    
445            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<'});
446    
447            redo A;            redo A;
448          }          }
449        } elsif ($self->{content_model_flag} eq 'PCDATA') {        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
450          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_input_character} == 0x0021) { # !
451            $self->{state} = 'markup declaration open';            $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
452            !!!next-input-character;            !!!next-input-character;
453            redo A;            redo A;
454          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{next_input_character} == 0x002F) { # /
455            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
456            !!!next-input-character;            !!!next-input-character;
457            redo A;            redo A;
458          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_input_character} and
459                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{next_input_character} <= 0x005A) { # A..Z
460            $self->{current_token}            $self->{current_token}
461              = {type => 'start tag',              = {type => START_TAG_TOKEN,
462                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_input_character} + 0x0020)};
463            $self->{state} = 'tag name';            $self->{state} = TAG_NAME_STATE;
464            !!!next-input-character;            !!!next-input-character;
465            redo A;            redo A;
466          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_input_character} and
467                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_input_character} <= 0x007A) { # a..z
468            $self->{current_token} = {type => 'start tag',            $self->{current_token} = {type => START_TAG_TOKEN,
469                              tag_name => chr ($self->{next_input_character})};                              tag_name => chr ($self->{next_input_character})};
470            $self->{state} = 'tag name';            $self->{state} = TAG_NAME_STATE;
471            !!!next-input-character;            !!!next-input-character;
472            redo A;            redo A;
473          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_input_character} == 0x003E) { # >
474            !!!parse-error (type => 'empty start tag');            !!!parse-error (type => 'empty start tag');
475            $self->{state} = 'data';            $self->{state} = DATA_STATE;
476            !!!next-input-character;            !!!next-input-character;
477    
478            !!!emit ({type => 'character', data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});
479    
480            redo A;            redo A;
481          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_input_character} == 0x003F) { # ?
482            !!!parse-error (type => 'pio');            !!!parse-error (type => 'pio');
483            $self->{state} = 'bogus comment';            $self->{state} = BOGUS_COMMENT_STATE;
484            ## $self->{next_input_character} is intentionally left as is            ## $self->{next_input_character} is intentionally left as is
485            redo A;            redo A;
486          } else {          } else {
487            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago');
488            $self->{state} = 'data';            $self->{state} = DATA_STATE;
489            ## reconsume            ## reconsume
490    
491            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<'});
492    
493            redo A;            redo A;
494          }          }
495        } else {        } else {
496          die "$0: $self->{content_model_flag}: Unknown content model flag";          die "$0: $self->{content_model} in tag open";
497        }        }
498      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
499        if ($self->{content_model_flag} eq 'RCDATA' or        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
           $self->{content_model_flag} eq 'CDATA') {  
500          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
501              ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
502            my @next_char;            my @next_char;
503            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++) {
504              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_input_character};
# Line 338  sub _get_next_token ($) { Line 510  sub _get_next_token ($) {
510              } else {              } else {
511                $self->{next_input_character} = shift @next_char; # reconsume                $self->{next_input_character} = shift @next_char; # reconsume
512                !!!back-next-input-character (@next_char);                !!!back-next-input-character (@next_char);
513                $self->{state} = 'data';                $self->{state} = DATA_STATE;
514    
515                !!!emit ({type => 'character', data => '</'});                !!!emit ({type => CHARACTER_TOKEN, data => '</'});
516        
517                redo A;                redo A;
518              }              }
# Line 357  sub _get_next_token ($) { Line 529  sub _get_next_token ($) {
529                    $self->{next_input_character} == -1) {                    $self->{next_input_character} == -1) {
530              $self->{next_input_character} = shift @next_char; # reconsume              $self->{next_input_character} = shift @next_char; # reconsume
531              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
532              $self->{state} = 'data';              $self->{state} = DATA_STATE;
533              !!!emit ({type => 'character', data => '</'});              !!!emit ({type => CHARACTER_TOKEN, data => '</'});
534              redo A;              redo A;
535            } else {            } else {
536              $self->{next_input_character} = shift @next_char;              $self->{next_input_character} = shift @next_char;
# Line 368  sub _get_next_token ($) { Line 540  sub _get_next_token ($) {
540          } else {          } else {
541            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
542            # next-input-character is already done            # next-input-character is already done
543            $self->{state} = 'data';            $self->{state} = DATA_STATE;
544            !!!emit ({type => 'character', data => '</'});            !!!emit ({type => CHARACTER_TOKEN, data => '</'});
545            redo A;            redo A;
546          }          }
547        }        }
548                
549        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_input_character} and
550            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_input_character} <= 0x005A) { # A..Z
551          $self->{current_token} = {type => 'end tag',          $self->{current_token} = {type => END_TAG_TOKEN,
552                            tag_name => chr ($self->{next_input_character} + 0x0020)};                            tag_name => chr ($self->{next_input_character} + 0x0020)};
553          $self->{state} = 'tag name';          $self->{state} = TAG_NAME_STATE;
554          !!!next-input-character;          !!!next-input-character;
555          redo A;          redo A;
556        } elsif (0x0061 <= $self->{next_input_character} and        } elsif (0x0061 <= $self->{next_input_character} and
557                 $self->{next_input_character} <= 0x007A) { # a..z                 $self->{next_input_character} <= 0x007A) { # a..z
558          $self->{current_token} = {type => 'end tag',          $self->{current_token} = {type => END_TAG_TOKEN,
559                            tag_name => chr ($self->{next_input_character})};                            tag_name => chr ($self->{next_input_character})};
560          $self->{state} = 'tag name';          $self->{state} = TAG_NAME_STATE;
561          !!!next-input-character;          !!!next-input-character;
562          redo A;          redo A;
563        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
564          !!!parse-error (type => 'empty end tag');          !!!parse-error (type => 'empty end tag');
565          $self->{state} = 'data';          $self->{state} = DATA_STATE;
566          !!!next-input-character;          !!!next-input-character;
567          redo A;          redo A;
568        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
569          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
570          $self->{state} = 'data';          $self->{state} = DATA_STATE;
571          # reconsume          # reconsume
572    
573          !!!emit ({type => 'character', data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</'});
574    
575          redo A;          redo A;
576        } else {        } else {
577          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
578          $self->{state} = 'bogus comment';          $self->{state} = BOGUS_COMMENT_STATE;
579          ## $self->{next_input_character} is intentionally left as is          ## $self->{next_input_character} is intentionally left as is
580          redo A;          redo A;
581        }        }
582      } elsif ($self->{state} eq 'tag name') {      } elsif ($self->{state} == TAG_NAME_STATE) {
583        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
584            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
585            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
586            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
587            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
588          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
589          !!!next-input-character;          !!!next-input-character;
590          redo A;          redo A;
591        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
592          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
593              $self->{current_token}->{first_start_tag}
594                  = not defined $self->{last_emitted_start_tag_name};
595            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
596          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
597            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
598            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
599              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
600            }            }
601          } else {          } else {
602            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
603          }          }
604          $self->{state} = 'data';          $self->{state} = DATA_STATE;
605          !!!next-input-character;          !!!next-input-character;
606    
607          !!!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 616  sub _get_next_token ($) {
616          redo A;          redo A;
617        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
618          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
619          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
620              $self->{current_token}->{first_start_tag}
621                  = not defined $self->{last_emitted_start_tag_name};
622            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
623          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
624            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
625            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
626              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
627            }            }
628          } else {          } else {
629            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
630          }          }
631          $self->{state} = 'data';          $self->{state} = DATA_STATE;
632          # reconsume          # reconsume
633    
634          !!!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 637  sub _get_next_token ($) {
637        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
638          !!!next-input-character;          !!!next-input-character;
639          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
640              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
641              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
642            # permitted slash            # permitted slash
643            #            #
644          } else {          } else {
645            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
646          }          }
647          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
648          # next-input-character is already done          # next-input-character is already done
649          redo A;          redo A;
650        } else {        } else {
# Line 478  sub _get_next_token ($) { Line 654  sub _get_next_token ($) {
654          !!!next-input-character;          !!!next-input-character;
655          redo A;          redo A;
656        }        }
657      } elsif ($self->{state} eq 'before attribute name') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
658        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
659            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
660            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 488  sub _get_next_token ($) { Line 664  sub _get_next_token ($) {
664          !!!next-input-character;          !!!next-input-character;
665          redo A;          redo A;
666        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
667          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
668              $self->{current_token}->{first_start_tag}
669                  = not defined $self->{last_emitted_start_tag_name};
670            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
671          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
672            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
673            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
674              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
675            }            }
676          } else {          } else {
677            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
678          }          }
679          $self->{state} = 'data';          $self->{state} = DATA_STATE;
680          !!!next-input-character;          !!!next-input-character;
681    
682          !!!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 686  sub _get_next_token ($) {
686                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_input_character} <= 0x005A) { # A..Z
687          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),
688                                value => ''};                                value => ''};
689          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
690          !!!next-input-character;          !!!next-input-character;
691          redo A;          redo A;
692        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
693          !!!next-input-character;          !!!next-input-character;
694          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
695              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
696              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
697            # permitted slash            # permitted slash
698            #            #
# Line 526  sub _get_next_token ($) { Line 704  sub _get_next_token ($) {
704          redo A;          redo A;
705        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
706          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
707          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
708              $self->{current_token}->{first_start_tag}
709                  = not defined $self->{last_emitted_start_tag_name};
710            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
711          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
712            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
713            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
714              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
715            }            }
716          } else {          } else {
717            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
718          }          }
719          $self->{state} = 'data';          $self->{state} = DATA_STATE;
720          # reconsume          # reconsume
721    
722          !!!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 725  sub _get_next_token ($) {
725        } else {        } else {
726          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          $self->{current_attribute} = {name => chr ($self->{next_input_character}),
727                                value => ''};                                value => ''};
728          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
729          !!!next-input-character;          !!!next-input-character;
730          redo A;          redo A;
731        }        }
732      } elsif ($self->{state} eq 'attribute name') {      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {
733        my $before_leave = sub {        my $before_leave = sub {
734          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
735              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
736            !!!parse-error (type => 'dupulicate attribute');            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});
737            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
738          } else {          } else {
739            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
# Line 567  sub _get_next_token ($) { Line 747  sub _get_next_token ($) {
747            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
748            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
749          $before_leave->();          $before_leave->();
750          $self->{state} = 'after attribute name';          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
751          !!!next-input-character;          !!!next-input-character;
752          redo A;          redo A;
753        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_input_character} == 0x003D) { # =
754          $before_leave->();          $before_leave->();
755          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
756          !!!next-input-character;          !!!next-input-character;
757          redo A;          redo A;
758        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
759          $before_leave->();          $before_leave->();
760          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
761              $self->{current_token}->{first_start_tag}
762                  = not defined $self->{last_emitted_start_tag_name};
763            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
764          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
765            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
766            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
767              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
768            }            }
769          } else {          } else {
770            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
771          }          }
772          $self->{state} = 'data';          $self->{state} = DATA_STATE;
773          !!!next-input-character;          !!!next-input-character;
774    
775          !!!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 785  sub _get_next_token ($) {
785          $before_leave->();          $before_leave->();
786          !!!next-input-character;          !!!next-input-character;
787          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
788              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
789              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
790            # permitted slash            # permitted slash
791            #            #
792          } else {          } else {
793            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
794          }          }
795          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
796          # next-input-character is already done          # next-input-character is already done
797          redo A;          redo A;
798        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
799          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
800          $before_leave->();          $before_leave->();
801          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
802              $self->{current_token}->{first_start_tag}
803                  = not defined $self->{last_emitted_start_tag_name};
804            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
805          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
806            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
807            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
808              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
809            }            }
810          } else {          } else {
811            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
812          }          }
813          $self->{state} = 'data';          $self->{state} = DATA_STATE;
814          # reconsume          # reconsume
815    
816          !!!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 822  sub _get_next_token ($) {
822          !!!next-input-character;          !!!next-input-character;
823          redo A;          redo A;
824        }        }
825      } elsif ($self->{state} eq 'after attribute name') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
826        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
827            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
828            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 648  sub _get_next_token ($) { Line 832  sub _get_next_token ($) {
832          !!!next-input-character;          !!!next-input-character;
833          redo A;          redo A;
834        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_input_character} == 0x003D) { # =
835          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
836          !!!next-input-character;          !!!next-input-character;
837          redo A;          redo A;
838        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
839          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
840              $self->{current_token}->{first_start_tag}
841                  = not defined $self->{last_emitted_start_tag_name};
842            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
843          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
844            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
845            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
846              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
847            }            }
848          } else {          } else {
849            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
850          }          }
851          $self->{state} = 'data';          $self->{state} = DATA_STATE;
852          !!!next-input-character;          !!!next-input-character;
853    
854          !!!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 858  sub _get_next_token ($) {
858                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_input_character} <= 0x005A) { # A..Z
859          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),
860                                value => ''};                                value => ''};
861          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
862          !!!next-input-character;          !!!next-input-character;
863          redo A;          redo A;
864        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
865          !!!next-input-character;          !!!next-input-character;
866          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
867              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
868              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
869            # permitted slash            # permitted slash
870            #            #
871          } else {          } else {
872            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
873              ## TODO: Different error type for <aa / bb> than <aa/>
874          }          }
875          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
876          # next-input-character is already done          # next-input-character is already done
877          redo A;          redo A;
878        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
879          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
880          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
881              $self->{current_token}->{first_start_tag}
882                  = not defined $self->{last_emitted_start_tag_name};
883            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
884          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
885            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
886            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
887              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
888            }            }
889          } else {          } else {
890            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
891          }          }
892          $self->{state} = 'data';          $self->{state} = DATA_STATE;
893          # reconsume          # reconsume
894    
895          !!!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 898  sub _get_next_token ($) {
898        } else {        } else {
899          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          $self->{current_attribute} = {name => chr ($self->{next_input_character}),
900                                value => ''};                                value => ''};
901          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
902          !!!next-input-character;          !!!next-input-character;
903          redo A;                  redo A;        
904        }        }
905      } elsif ($self->{state} eq 'before attribute value') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
906        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
907            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
908            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 723  sub _get_next_token ($) { Line 912  sub _get_next_token ($) {
912          !!!next-input-character;          !!!next-input-character;
913          redo A;          redo A;
914        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_input_character} == 0x0022) { # "
915          $self->{state} = 'attribute value (double-quoted)';          $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
916          !!!next-input-character;          !!!next-input-character;
917          redo A;          redo A;
918        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
919          $self->{state} = 'attribute value (unquoted)';          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
920          ## reconsume          ## reconsume
921          redo A;          redo A;
922        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_input_character} == 0x0027) { # '
923          $self->{state} = 'attribute value (single-quoted)';          $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
924          !!!next-input-character;          !!!next-input-character;
925          redo A;          redo A;
926        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
927          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
928              $self->{current_token}->{first_start_tag}
929                  = not defined $self->{last_emitted_start_tag_name};
930            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
931          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
932            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
933            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
934              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
935            }            }
936          } else {          } else {
937            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
938          }          }
939          $self->{state} = 'data';          $self->{state} = DATA_STATE;
940          !!!next-input-character;          !!!next-input-character;
941    
942          !!!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 944  sub _get_next_token ($) {
944          redo A;          redo A;
945        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
946          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
947          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
948              $self->{current_token}->{first_start_tag}
949                  = not defined $self->{last_emitted_start_tag_name};
950            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
951          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
952            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
953            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
954              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
955            }            }
956          } else {          } else {
957            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
958          }          }
959          $self->{state} = 'data';          $self->{state} = DATA_STATE;
960          ## reconsume          ## reconsume
961    
962          !!!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 964  sub _get_next_token ($) {
964          redo A;          redo A;
965        } else {        } else {
966          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});
967          $self->{state} = 'attribute value (unquoted)';          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
968          !!!next-input-character;          !!!next-input-character;
969          redo A;          redo A;
970        }        }
971      } elsif ($self->{state} eq 'attribute value (double-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
972        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_input_character} == 0x0022) { # "
973          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
974          !!!next-input-character;          !!!next-input-character;
975          redo A;          redo A;
976        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
977          $self->{last_attribute_value_state} = 'attribute value (double-quoted)';          $self->{last_attribute_value_state} = $self->{state};
978          $self->{state} = 'entity in attribute value';          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
979          !!!next-input-character;          !!!next-input-character;
980          redo A;          redo A;
981        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
982          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
983          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
984              $self->{current_token}->{first_start_tag}
985                  = not defined $self->{last_emitted_start_tag_name};
986            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
987          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
988            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
989            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
990              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
991            }            }
992          } else {          } else {
993            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
994          }          }
995          $self->{state} = 'data';          $self->{state} = DATA_STATE;
996          ## reconsume          ## reconsume
997    
998          !!!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 1004  sub _get_next_token ($) {
1004          !!!next-input-character;          !!!next-input-character;
1005          redo A;          redo A;
1006        }        }
1007      } elsif ($self->{state} eq 'attribute value (single-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1008        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_input_character} == 0x0027) { # '
1009          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1010          !!!next-input-character;          !!!next-input-character;
1011          redo A;          redo A;
1012        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
1013          $self->{last_attribute_value_state} = 'attribute value (single-quoted)';          $self->{last_attribute_value_state} = $self->{state};
1014          $self->{state} = 'entity in attribute value';          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1015          !!!next-input-character;          !!!next-input-character;
1016          redo A;          redo A;
1017        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1018          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1019          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1020              $self->{current_token}->{first_start_tag}
1021                  = not defined $self->{last_emitted_start_tag_name};
1022            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1023          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1024            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1025            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1026              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1027            }            }
1028          } else {          } else {
1029            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1030          }          }
1031          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1032          ## reconsume          ## reconsume
1033    
1034          !!!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 1040  sub _get_next_token ($) {
1040          !!!next-input-character;          !!!next-input-character;
1041          redo A;          redo A;
1042        }        }
1043      } elsif ($self->{state} eq 'attribute value (unquoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1044        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1045            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1046            $self->{next_input_character} == 0x000B or # HT            $self->{next_input_character} == 0x000B or # HT
1047            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1048            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
1049          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1050          !!!next-input-character;          !!!next-input-character;
1051          redo A;          redo A;
1052        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
1053          $self->{last_attribute_value_state} = 'attribute value (unquoted)';          $self->{last_attribute_value_state} = $self->{state};
1054          $self->{state} = 'entity in attribute value';          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1055          !!!next-input-character;          !!!next-input-character;
1056          redo A;          redo A;
1057        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1058          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1059              $self->{current_token}->{first_start_tag}
1060                  = not defined $self->{last_emitted_start_tag_name};
1061            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1062          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1063            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1064            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1065              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1066            }            }
1067          } else {          } else {
1068            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1069          }          }
1070          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1071          !!!next-input-character;          !!!next-input-character;
1072    
1073          !!!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 1075  sub _get_next_token ($) {
1075          redo A;          redo A;
1076        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1077          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1078          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1079              $self->{current_token}->{first_start_tag}
1080                  = not defined $self->{last_emitted_start_tag_name};
1081            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1082          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1083            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1084            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1085              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1086            }            }
1087          } else {          } else {
1088            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1089          }          }
1090          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1091          ## reconsume          ## reconsume
1092    
1093          !!!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 1099  sub _get_next_token ($) {
1099          !!!next-input-character;          !!!next-input-character;
1100          redo A;          redo A;
1101        }        }
1102      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {
1103        my $token = $self->_tokenize_attempt_to_consume_an_entity;        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);
1104    
1105        unless (defined $token) {        unless (defined $token) {
1106          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
# Line 911  sub _get_next_token ($) { Line 1112  sub _get_next_token ($) {
1112        $self->{state} = $self->{last_attribute_value_state};        $self->{state} = $self->{last_attribute_value_state};
1113        # next-input-character is already done        # next-input-character is already done
1114        redo A;        redo A;
1115      } elsif ($self->{state} eq 'bogus comment') {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1116        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1117                
1118        my $token = {type => 'comment', data => ''};        my $token = {type => COMMENT_TOKEN, data => ''};
1119    
1120        BC: {        BC: {
1121          if ($self->{next_input_character} == 0x003E) { # >          if ($self->{next_input_character} == 0x003E) { # >
1122            $self->{state} = 'data';            $self->{state} = DATA_STATE;
1123            !!!next-input-character;            !!!next-input-character;
1124    
1125            !!!emit ($token);            !!!emit ($token);
1126    
1127            redo A;            redo A;
1128          } elsif ($self->{next_input_character} == -1) {          } elsif ($self->{next_input_character} == -1) {
1129            $self->{state} = 'data';            $self->{state} = DATA_STATE;
1130            ## reconsume            ## reconsume
1131    
1132            !!!emit ($token);            !!!emit ($token);
# Line 937  sub _get_next_token ($) { Line 1138  sub _get_next_token ($) {
1138            redo BC;            redo BC;
1139          }          }
1140        } # BC        } # BC
1141      } elsif ($self->{state} eq 'markup declaration open') {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1142        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1143    
1144        my @next_char;        my @next_char;
# Line 947  sub _get_next_token ($) { Line 1148  sub _get_next_token ($) {
1148          !!!next-input-character;          !!!next-input-character;
1149          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_input_character};
1150          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_input_character} == 0x002D) { # -
1151            $self->{current_token} = {type => 'comment', data => ''};            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};
1152            $self->{state} = 'comment start';            $self->{state} = COMMENT_START_STATE;
1153            !!!next-input-character;            !!!next-input-character;
1154            redo A;            redo A;
1155          }          }
# Line 979  sub _get_next_token ($) { Line 1180  sub _get_next_token ($) {
1180                    if ($self->{next_input_character} == 0x0045 or # E                    if ($self->{next_input_character} == 0x0045 or # E
1181                        $self->{next_input_character} == 0x0065) { # e                        $self->{next_input_character} == 0x0065) { # e
1182                      ## ISSUE: What a stupid code this is!                      ## ISSUE: What a stupid code this is!
1183                      $self->{state} = 'DOCTYPE';                      $self->{state} = DOCTYPE_STATE;
1184                      !!!next-input-character;                      !!!next-input-character;
1185                      redo A;                      redo A;
1186                    }                    }
# Line 990  sub _get_next_token ($) { Line 1191  sub _get_next_token ($) {
1191          }          }
1192        }        }
1193    
1194        !!!parse-error (type => 'bogus comment open');        !!!parse-error (type => 'bogus comment');
1195        $self->{next_input_character} = shift @next_char;        $self->{next_input_character} = shift @next_char;
1196        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
1197        $self->{state} = 'bogus comment';        $self->{state} = BOGUS_COMMENT_STATE;
1198        redo A;        redo A;
1199                
1200        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
1201        ## 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?
1202      } elsif ($self->{state} eq 'comment start') {      } elsif ($self->{state} == COMMENT_START_STATE) {
1203        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1204          $self->{state} = 'comment start dash';          $self->{state} = COMMENT_START_DASH_STATE;
1205          !!!next-input-character;          !!!next-input-character;
1206          redo A;          redo A;
1207        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1208          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
1209          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1210          !!!next-input-character;          !!!next-input-character;
1211    
1212          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1013  sub _get_next_token ($) { Line 1214  sub _get_next_token ($) {
1214          redo A;          redo A;
1215        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1216          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1217          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1218          ## reconsume          ## reconsume
1219    
1220          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1022  sub _get_next_token ($) { Line 1223  sub _get_next_token ($) {
1223        } else {        } else {
1224          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
1225              .= chr ($self->{next_input_character});              .= chr ($self->{next_input_character});
1226          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1227          !!!next-input-character;          !!!next-input-character;
1228          redo A;          redo A;
1229        }        }
1230      } elsif ($self->{state} eq 'comment start dash') {      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
1231        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1232          $self->{state} = 'comment end';          $self->{state} = COMMENT_END_STATE;
1233          !!!next-input-character;          !!!next-input-character;
1234          redo A;          redo A;
1235        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1236          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
1237          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1238          !!!next-input-character;          !!!next-input-character;
1239    
1240          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1041  sub _get_next_token ($) { Line 1242  sub _get_next_token ($) {
1242          redo A;          redo A;
1243        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1244          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1245          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1246          ## reconsume          ## reconsume
1247    
1248          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1049  sub _get_next_token ($) { Line 1250  sub _get_next_token ($) {
1250          redo A;          redo A;
1251        } else {        } else {
1252          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
1253              .= chr ($self->{next_input_character});              .= '-' . chr ($self->{next_input_character});
1254          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1255          !!!next-input-character;          !!!next-input-character;
1256          redo A;          redo A;
1257        }        }
1258      } elsif ($self->{state} eq 'comment') {      } elsif ($self->{state} == COMMENT_STATE) {
1259        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1260          $self->{state} = 'comment end dash';          $self->{state} = COMMENT_END_DASH_STATE;
1261          !!!next-input-character;          !!!next-input-character;
1262          redo A;          redo A;
1263        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1264          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1265          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1266          ## reconsume          ## reconsume
1267    
1268          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1073  sub _get_next_token ($) { Line 1274  sub _get_next_token ($) {
1274          !!!next-input-character;          !!!next-input-character;
1275          redo A;          redo A;
1276        }        }
1277      } elsif ($self->{state} eq 'comment end dash') {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
1278        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1279          $self->{state} = 'comment end';          $self->{state} = COMMENT_END_STATE;
1280          !!!next-input-character;          !!!next-input-character;
1281          redo A;          redo A;
1282        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1283          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1284          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1285          ## reconsume          ## reconsume
1286    
1287          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1088  sub _get_next_token ($) { Line 1289  sub _get_next_token ($) {
1289          redo A;          redo A;
1290        } else {        } else {
1291          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment
1292          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1293          !!!next-input-character;          !!!next-input-character;
1294          redo A;          redo A;
1295        }        }
1296      } elsif ($self->{state} eq 'comment end') {      } elsif ($self->{state} == COMMENT_END_STATE) {
1297        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_input_character} == 0x003E) { # >
1298          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1299          !!!next-input-character;          !!!next-input-character;
1300    
1301          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1108  sub _get_next_token ($) { Line 1309  sub _get_next_token ($) {
1309          redo A;          redo A;
1310        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1311          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1312          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1313          ## reconsume          ## reconsume
1314    
1315          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1117  sub _get_next_token ($) { Line 1318  sub _get_next_token ($) {
1318        } else {        } else {
1319          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment');
1320          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment
1321          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1322          !!!next-input-character;          !!!next-input-character;
1323          redo A;          redo A;
1324        }        }
1325      } elsif ($self->{state} eq 'DOCTYPE') {      } elsif ($self->{state} == DOCTYPE_STATE) {
1326        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1327            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1328            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
1329            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1330            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
1331          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1332          !!!next-input-character;          !!!next-input-character;
1333          redo A;          redo A;
1334        } else {        } else {
1335          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
1336          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1337          ## reconsume          ## reconsume
1338          redo A;          redo A;
1339        }        }
1340      } elsif ($self->{state} eq 'before DOCTYPE name') {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
1341        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1342            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1343            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 1147  sub _get_next_token ($) { Line 1348  sub _get_next_token ($) {
1348          redo A;          redo A;
1349        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1350          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1351          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1352          !!!next-input-character;          !!!next-input-character;
1353    
1354          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect
1355    
1356          redo A;          redo A;
1357        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1358          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1359          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1360          ## reconsume          ## reconsume
1361    
1362          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect
1363    
1364          redo A;          redo A;
1365        } else {        } else {
1366          $self->{current_token}          $self->{current_token}
1367              = {type => 'DOCTYPE',              = {type => DOCTYPE_TOKEN,
1368                 name => chr ($self->{next_input_character}),                 name => chr ($self->{next_input_character}),
1369                 correct => 1};                 correct => 1};
1370  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
1371          $self->{state} = 'DOCTYPE name';          $self->{state} = DOCTYPE_NAME_STATE;
1372          !!!next-input-character;          !!!next-input-character;
1373          redo A;          redo A;
1374        }        }
1375      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
1376  ## ISSUE: Redundant "First," in the spec.  ## ISSUE: Redundant "First," in the spec.
1377        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1378            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1379            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
1380            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1381            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
1382          $self->{state} = 'after DOCTYPE name';          $self->{state} = AFTER_DOCTYPE_NAME_STATE;
1383          !!!next-input-character;          !!!next-input-character;
1384          redo A;          redo A;
1385        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1386          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1387          !!!next-input-character;          !!!next-input-character;
1388    
1389          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1190  sub _get_next_token ($) { Line 1391  sub _get_next_token ($) {
1391          redo A;          redo A;
1392        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1393          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1394          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1395          ## reconsume          ## reconsume
1396    
1397          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1204  sub _get_next_token ($) { Line 1405  sub _get_next_token ($) {
1405          !!!next-input-character;          !!!next-input-character;
1406          redo A;          redo A;
1407        }        }
1408      } elsif ($self->{state} eq 'after DOCTYPE name') {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
1409        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1410            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1411            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 1214  sub _get_next_token ($) { Line 1415  sub _get_next_token ($) {
1415          !!!next-input-character;          !!!next-input-character;
1416          redo A;          redo A;
1417        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1418          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1419          !!!next-input-character;          !!!next-input-character;
1420    
1421          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1222  sub _get_next_token ($) { Line 1423  sub _get_next_token ($) {
1423          redo A;          redo A;
1424        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1425          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1426          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1427          ## reconsume          ## reconsume
1428    
1429          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1246  sub _get_next_token ($) { Line 1447  sub _get_next_token ($) {
1447                  !!!next-input-character;                  !!!next-input-character;
1448                  if ($self->{next_input_character} == 0x0043 or # C                  if ($self->{next_input_character} == 0x0043 or # C
1449                      $self->{next_input_character} == 0x0063) { # c                      $self->{next_input_character} == 0x0063) { # c
1450                    $self->{state} = 'before DOCTYPE public identifier';                    $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1451                    !!!next-input-character;                    !!!next-input-character;
1452                    redo A;                    redo A;
1453                  }                  }
# Line 1273  sub _get_next_token ($) { Line 1474  sub _get_next_token ($) {
1474                  !!!next-input-character;                  !!!next-input-character;
1475                  if ($self->{next_input_character} == 0x004D or # M                  if ($self->{next_input_character} == 0x004D or # M
1476                      $self->{next_input_character} == 0x006D) { # m                      $self->{next_input_character} == 0x006D) { # m
1477                    $self->{state} = 'before DOCTYPE system identifier';                    $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1478                    !!!next-input-character;                    !!!next-input-character;
1479                    redo A;                    redo A;
1480                  }                  }
# Line 1289  sub _get_next_token ($) { Line 1490  sub _get_next_token ($) {
1490        }        }
1491    
1492        !!!parse-error (type => 'string after DOCTYPE name');        !!!parse-error (type => 'string after DOCTYPE name');
1493        $self->{state} = 'bogus DOCTYPE';        $self->{state} = BOGUS_DOCTYPE_STATE;
1494        # next-input-character is already done        # next-input-character is already done
1495        redo A;        redo A;
1496      } elsif ($self->{state} eq 'before DOCTYPE public identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
1497        if ({        if ({
1498              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1499              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1302  sub _get_next_token ($) { Line 1503  sub _get_next_token ($) {
1503          redo A;          redo A;
1504        } elsif ($self->{next_input_character} eq 0x0022) { # "        } elsif ($self->{next_input_character} eq 0x0022) { # "
1505          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1506          $self->{state} = 'DOCTYPE public identifier (double-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
1507          !!!next-input-character;          !!!next-input-character;
1508          redo A;          redo A;
1509        } elsif ($self->{next_input_character} eq 0x0027) { # '        } elsif ($self->{next_input_character} eq 0x0027) { # '
1510          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1511          $self->{state} = 'DOCTYPE public identifier (single-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
1512          !!!next-input-character;          !!!next-input-character;
1513          redo A;          redo A;
1514        } elsif ($self->{next_input_character} eq 0x003E) { # >        } elsif ($self->{next_input_character} eq 0x003E) { # >
1515          !!!parse-error (type => 'no PUBLIC literal');          !!!parse-error (type => 'no PUBLIC literal');
1516    
1517          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1518          !!!next-input-character;          !!!next-input-character;
1519    
1520          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1323  sub _get_next_token ($) { Line 1524  sub _get_next_token ($) {
1524        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1525          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1526    
1527          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1528          ## reconsume          ## reconsume
1529    
1530          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1332  sub _get_next_token ($) { Line 1533  sub _get_next_token ($) {
1533          redo A;          redo A;
1534        } else {        } else {
1535          !!!parse-error (type => 'string after PUBLIC');          !!!parse-error (type => 'string after PUBLIC');
1536          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1537          !!!next-input-character;          !!!next-input-character;
1538          redo A;          redo A;
1539        }        }
1540      } elsif ($self->{state} eq 'DOCTYPE public identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
1541        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_input_character} == 0x0022) { # "
1542          $self->{state} = 'after DOCTYPE public identifier';          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1543          !!!next-input-character;          !!!next-input-character;
1544          redo A;          redo A;
1545        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1546          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
1547    
1548          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1549          ## reconsume          ## reconsume
1550    
1551          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1358  sub _get_next_token ($) { Line 1559  sub _get_next_token ($) {
1559          !!!next-input-character;          !!!next-input-character;
1560          redo A;          redo A;
1561        }        }
1562      } elsif ($self->{state} eq 'DOCTYPE public identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
1563        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_input_character} == 0x0027) { # '
1564          $self->{state} = 'after DOCTYPE public identifier';          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1565          !!!next-input-character;          !!!next-input-character;
1566          redo A;          redo A;
1567        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1568          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
1569    
1570          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1571          ## reconsume          ## reconsume
1572    
1573          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1380  sub _get_next_token ($) { Line 1581  sub _get_next_token ($) {
1581          !!!next-input-character;          !!!next-input-character;
1582          redo A;          redo A;
1583        }        }
1584      } elsif ($self->{state} eq 'after DOCTYPE public identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
1585        if ({        if ({
1586              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1587              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1390  sub _get_next_token ($) { Line 1591  sub _get_next_token ($) {
1591          redo A;          redo A;
1592        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_input_character} == 0x0022) { # "
1593          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1594          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
1595          !!!next-input-character;          !!!next-input-character;
1596          redo A;          redo A;
1597        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_input_character} == 0x0027) { # '
1598          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1599          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
1600          !!!next-input-character;          !!!next-input-character;
1601          redo A;          redo A;
1602        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1603          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1604          !!!next-input-character;          !!!next-input-character;
1605    
1606          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1408  sub _get_next_token ($) { Line 1609  sub _get_next_token ($) {
1609        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1610          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1611    
1612          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1613          ## recomsume          ## reconsume
1614    
1615          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
1616          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1417  sub _get_next_token ($) { Line 1618  sub _get_next_token ($) {
1618          redo A;          redo A;
1619        } else {        } else {
1620          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after PUBLIC literal');
1621          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1622          !!!next-input-character;          !!!next-input-character;
1623          redo A;          redo A;
1624        }        }
1625      } elsif ($self->{state} eq 'before DOCTYPE system identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
1626        if ({        if ({
1627              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1628              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1431  sub _get_next_token ($) { Line 1632  sub _get_next_token ($) {
1632          redo A;          redo A;
1633        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_input_character} == 0x0022) { # "
1634          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1635          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
1636          !!!next-input-character;          !!!next-input-character;
1637          redo A;          redo A;
1638        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_input_character} == 0x0027) { # '
1639          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1640          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
1641          !!!next-input-character;          !!!next-input-character;
1642          redo A;          redo A;
1643        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1644          !!!parse-error (type => 'no SYSTEM literal');          !!!parse-error (type => 'no SYSTEM literal');
1645          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1646          !!!next-input-character;          !!!next-input-character;
1647    
1648          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1451  sub _get_next_token ($) { Line 1652  sub _get_next_token ($) {
1652        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1653          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1654    
1655          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1656          ## recomsume          ## reconsume
1657    
1658          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
1659          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1660    
1661          redo A;          redo A;
1662        } else {        } else {
1663          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after SYSTEM');
1664          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1665          !!!next-input-character;          !!!next-input-character;
1666          redo A;          redo A;
1667        }        }
1668      } elsif ($self->{state} eq 'DOCTYPE system identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
1669        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_input_character} == 0x0022) { # "
1670          $self->{state} = 'after DOCTYPE system identifier';          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1671          !!!next-input-character;          !!!next-input-character;
1672          redo A;          redo A;
1673        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1674          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
1675    
1676          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1677          ## reconsume          ## reconsume
1678    
1679          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1486  sub _get_next_token ($) { Line 1687  sub _get_next_token ($) {
1687          !!!next-input-character;          !!!next-input-character;
1688          redo A;          redo A;
1689        }        }
1690      } elsif ($self->{state} eq 'DOCTYPE system identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
1691        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_input_character} == 0x0027) { # '
1692          $self->{state} = 'after DOCTYPE system identifier';          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1693          !!!next-input-character;          !!!next-input-character;
1694          redo A;          redo A;
1695        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1696          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
1697    
1698          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1699          ## reconsume          ## reconsume
1700    
1701          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1508  sub _get_next_token ($) { Line 1709  sub _get_next_token ($) {
1709          !!!next-input-character;          !!!next-input-character;
1710          redo A;          redo A;
1711        }        }
1712      } elsif ($self->{state} eq 'after DOCTYPE system identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
1713        if ({        if ({
1714              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1715              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1517  sub _get_next_token ($) { Line 1718  sub _get_next_token ($) {
1718          !!!next-input-character;          !!!next-input-character;
1719          redo A;          redo A;
1720        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1721          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1722          !!!next-input-character;          !!!next-input-character;
1723    
1724          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1526  sub _get_next_token ($) { Line 1727  sub _get_next_token ($) {
1727        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1728          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1729    
1730          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1731          ## recomsume          ## reconsume
1732    
1733          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
1734          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1535  sub _get_next_token ($) { Line 1736  sub _get_next_token ($) {
1736          redo A;          redo A;
1737        } else {        } else {
1738          !!!parse-error (type => 'string after SYSTEM literal');          !!!parse-error (type => 'string after SYSTEM literal');
1739          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1740          !!!next-input-character;          !!!next-input-character;
1741          redo A;          redo A;
1742        }        }
1743      } elsif ($self->{state} eq 'bogus DOCTYPE') {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
1744        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_input_character} == 0x003E) { # >
1745          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1746          !!!next-input-character;          !!!next-input-character;
1747    
1748          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1550  sub _get_next_token ($) { Line 1751  sub _get_next_token ($) {
1751          redo A;          redo A;
1752        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1753          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1754          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1755          ## reconsume          ## reconsume
1756    
1757          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1570  sub _get_next_token ($) { Line 1771  sub _get_next_token ($) {
1771    die "$0: _get_next_token: unexpected case";    die "$0: _get_next_token: unexpected case";
1772  } # _get_next_token  } # _get_next_token
1773    
1774  sub _tokenize_attempt_to_consume_an_entity ($) {  sub _tokenize_attempt_to_consume_an_entity ($$) {
1775    my $self = shift;    my ($self, $in_attr) = @_;
1776    
1777    if ({    if ({
1778         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 1785  sub _tokenize_attempt_to_consume_an_enti
1785      !!!next-input-character;      !!!next-input-character;
1786      if ($self->{next_input_character} == 0x0078 or # x      if ($self->{next_input_character} == 0x0078 or # x
1787          $self->{next_input_character} == 0x0058) { # X          $self->{next_input_character} == 0x0058) { # X
1788        my $num;        my $code;
1789        X: {        X: {
1790          my $x_char = $self->{next_input_character};          my $x_char = $self->{next_input_character};
1791          !!!next-input-character;          !!!next-input-character;
1792          if (0x0030 <= $self->{next_input_character} and          if (0x0030 <= $self->{next_input_character} and
1793              $self->{next_input_character} <= 0x0039) { # 0..9              $self->{next_input_character} <= 0x0039) { # 0..9
1794            $num ||= 0;            $code ||= 0;
1795            $num *= 0x10;            $code *= 0x10;
1796            $num += $self->{next_input_character} - 0x0030;            $code += $self->{next_input_character} - 0x0030;
1797            redo X;            redo X;
1798          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_input_character} and
1799                   $self->{next_input_character} <= 0x0066) { # a..f                   $self->{next_input_character} <= 0x0066) { # a..f
1800            ## ISSUE: the spec says U+0078, which is apparently incorrect            $code ||= 0;
1801            $num ||= 0;            $code *= 0x10;
1802            $num *= 0x10;            $code += $self->{next_input_character} - 0x0060 + 9;
           $num += $self->{next_input_character} - 0x0060 + 9;  
1803            redo X;            redo X;
1804          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_input_character} and
1805                   $self->{next_input_character} <= 0x0046) { # A..F                   $self->{next_input_character} <= 0x0046) { # A..F
1806            ## ISSUE: the spec says U+0058, which is apparently incorrect            $code ||= 0;
1807            $num ||= 0;            $code *= 0x10;
1808            $num *= 0x10;            $code += $self->{next_input_character} - 0x0040 + 9;
           $num += $self->{next_input_character} - 0x0040 + 9;  
1809            redo X;            redo X;
1810          } elsif (not defined $num) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
1811            !!!parse-error (type => 'bare hcro');            !!!parse-error (type => 'bare hcro');
1812              !!!back-next-input-character ($x_char, $self->{next_input_character});
1813            $self->{next_input_character} = 0x0023; # #            $self->{next_input_character} = 0x0023; # #
           !!!back-next-input-character ($x_char);  
1814            return undef;            return undef;
1815          } elsif ($self->{next_input_character} == 0x003B) { # ;          } elsif ($self->{next_input_character} == 0x003B) { # ;
1816            !!!next-input-character;            !!!next-input-character;
# Line 1619  sub _tokenize_attempt_to_consume_an_enti Line 1818  sub _tokenize_attempt_to_consume_an_enti
1818            !!!parse-error (type => 'no refc');            !!!parse-error (type => 'no refc');
1819          }          }
1820    
1821          ## TODO: check the definition for |a valid Unicode character|.          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
1822          ## <http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2006-December/thread.html#8189>            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);
1823          if ($num > 1114111 or $num == 0) {            $code = 0xFFFD;
1824            $num = 0xFFFD; # REPLACEMENT CHARACTER          } elsif ($code > 0x10FFFF) {
1825            ## ISSUE: Why this is not an error?            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);
1826          } elsif (0x80 <= $num and $num <= 0x9F) {            $code = 0xFFFD;
1827            !!!parse-error (type => sprintf 'c1 entity:U+%04X', $num);          } elsif ($code == 0x000D) {
1828            $num = $c1_entity_char->{$num};            !!!parse-error (type => 'CR character reference');
1829              $code = 0x000A;
1830            } elsif (0x80 <= $code and $code <= 0x9F) {
1831              !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);
1832              $code = $c1_entity_char->{$code};
1833          }          }
1834    
1835          return {type => 'character', data => chr $num};          return {type => CHARACTER_TOKEN, data => chr $code};
1836        } # X        } # X
1837      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_input_character} and
1838               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_input_character} <= 0x0039) { # 0..9
# Line 1650  sub _tokenize_attempt_to_consume_an_enti Line 1853  sub _tokenize_attempt_to_consume_an_enti
1853          !!!parse-error (type => 'no refc');          !!!parse-error (type => 'no refc');
1854        }        }
1855    
1856        ## TODO: check the definition for |a valid Unicode character|.        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
1857        if ($code > 1114111 or $code == 0) {          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);
1858          $code = 0xFFFD; # REPLACEMENT CHARACTER          $code = 0xFFFD;
1859          ## ISSUE: Why this is not an error?        } elsif ($code > 0x10FFFF) {
1860            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);
1861            $code = 0xFFFD;
1862          } elsif ($code == 0x000D) {
1863            !!!parse-error (type => 'CR character reference');
1864            $code = 0x000A;
1865        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
1866          !!!parse-error (type => sprintf 'c1 entity:U+%04X', $code);          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);
1867          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
1868        }        }
1869                
1870        return {type => 'character', data => chr $code};        return {type => CHARACTER_TOKEN, data => chr $code};
1871      } else {      } else {
1872        !!!parse-error (type => 'bare nero');        !!!parse-error (type => 'bare nero');
1873        !!!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 1882  sub _tokenize_attempt_to_consume_an_enti
1882      !!!next-input-character;      !!!next-input-character;
1883    
1884      my $value = $entity_name;      my $value = $entity_name;
1885      my $match;      my $match = 0;
1886      require Whatpm::_NamedEntityList;      require Whatpm::_NamedEntityList;
1887      our $EntityChar;      our $EntityChar;
1888    
# Line 1689  sub _tokenize_attempt_to_consume_an_enti Line 1897  sub _tokenize_attempt_to_consume_an_enti
1897              $self->{next_input_character} == 0x003B)) { # ;              $self->{next_input_character} == 0x003B)) { # ;
1898        $entity_name .= chr $self->{next_input_character};        $entity_name .= chr $self->{next_input_character};
1899        if (defined $EntityChar->{$entity_name}) {        if (defined $EntityChar->{$entity_name}) {
         $value = $EntityChar->{$entity_name};  
1900          if ($self->{next_input_character} == 0x003B) { # ;          if ($self->{next_input_character} == 0x003B) { # ;
1901              $value = $EntityChar->{$entity_name};
1902            $match = 1;            $match = 1;
1903            !!!next-input-character;            !!!next-input-character;
1904            last;            last;
1905          } else {          } else {
1906              $value = $EntityChar->{$entity_name};
1907            $match = -1;            $match = -1;
1908              !!!next-input-character;
1909          }          }
1910        } else {        } else {
1911          $value .= chr $self->{next_input_character};          $value .= chr $self->{next_input_character};
1912            $match *= 2;
1913            !!!next-input-character;
1914        }        }
       !!!next-input-character;  
1915      }      }
1916            
1917      if ($match > 0) {      if ($match > 0) {
1918        return {type => 'character', data => $value};        return {type => CHARACTER_TOKEN, data => $value};
1919      } elsif ($match < 0) {      } elsif ($match < 0) {
1920        !!!parse-error (type => 'refc');        !!!parse-error (type => 'no refc');
1921        return {type => 'character', data => $value};        if ($in_attr and $match < -1) {
1922            return {type => CHARACTER_TOKEN, data => '&'.$entity_name};
1923          } else {
1924            return {type => CHARACTER_TOKEN, data => $value};
1925          }
1926      } else {      } else {
1927        !!!parse-error (type => 'bare ero');        !!!parse-error (type => 'bare ero');
1928        ## NOTE: No characters are consumed in the spec.        ## NOTE: No characters are consumed in the spec.
1929        !!!back-token ({type => 'character', data => $value});        return {type => CHARACTER_TOKEN, data => '&'.$value};
       return undef;  
1930      }      }
1931    } else {    } else {
1932      ## no characters are consumed      ## no characters are consumed
# Line 1754  sub _construct_tree ($) { Line 1968  sub _construct_tree ($) {
1968        
1969    !!!next-token;    !!!next-token;
1970    
1971    $self->{insertion_mode} = 'before head';    $self->{insertion_mode} = BEFORE_HEAD_IM;
1972    undef $self->{form_element};    undef $self->{form_element};
1973    undef $self->{head_element};    undef $self->{head_element};
1974    $self->{open_elements} = [];    $self->{open_elements} = [];
# Line 1768  sub _construct_tree ($) { Line 1982  sub _construct_tree ($) {
1982  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
1983    my $self = shift;    my $self = shift;
1984    INITIAL: {    INITIAL: {
1985      if ($token->{type} eq 'DOCTYPE') {      if ($token->{type} == DOCTYPE_TOKEN) {
1986        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
1987        ## error, switch to a conformance checking mode for another        ## error, switch to a conformance checking mode for another
1988        ## language.        ## language.
# Line 1895  sub _tree_construction_initial ($) { Line 2109  sub _tree_construction_initial ($) {
2109        !!!next-token;        !!!next-token;
2110        return;        return;
2111      } elsif ({      } elsif ({
2112                'start tag' => 1,                START_TAG_TOKEN, 1,
2113                'end tag' => 1,                END_TAG_TOKEN, 1,
2114                'end-of-file' => 1,                END_OF_FILE_TOKEN, 1,
2115               }->{$token->{type}}) {               }->{$token->{type}}) {
2116        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE');
2117        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
2118        ## Go to the root element phase        ## Go to the root element phase
2119        ## reprocess        ## reprocess
2120        return;        return;
2121      } elsif ($token->{type} eq 'character') {      } elsif ($token->{type} == CHARACTER_TOKEN) {
2122        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2123          ## Ignore the token          ## Ignore the token
2124    
2125          unless (length $token->{data}) {          unless (length $token->{data}) {
2126            ## Stay in the phase            ## Stay in the phase
2127            !!!next-token;            !!!next-token;
# Line 1919  sub _tree_construction_initial ($) { Line 2134  sub _tree_construction_initial ($) {
2134        ## Go to the root element phase        ## Go to the root element phase
2135        ## reprocess        ## reprocess
2136        return;        return;
2137      } elsif ($token->{type} eq 'comment') {      } elsif ($token->{type} == COMMENT_TOKEN) {
2138        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
2139        $self->{document}->append_child ($comment);        $self->{document}->append_child ($comment);
2140                
# Line 1927  sub _tree_construction_initial ($) { Line 2142  sub _tree_construction_initial ($) {
2142        !!!next-token;        !!!next-token;
2143        redo INITIAL;        redo INITIAL;
2144      } else {      } else {
2145        die "$0: $token->{type}: Unknown token";        die "$0: $token->{type}: Unknown token type";
2146      }      }
2147    } # INITIAL    } # INITIAL
2148  } # _tree_construction_initial  } # _tree_construction_initial
# Line 1936  sub _tree_construction_root_element ($) Line 2151  sub _tree_construction_root_element ($)
2151    my $self = shift;    my $self = shift;
2152        
2153    B: {    B: {
2154        if ($token->{type} eq 'DOCTYPE') {        if ($token->{type} == DOCTYPE_TOKEN) {
2155          !!!parse-error (type => 'in html:#DOCTYPE');          !!!parse-error (type => 'in html:#DOCTYPE');
2156          ## Ignore the token          ## Ignore the token
2157          ## Stay in the phase          ## Stay in the phase
2158          !!!next-token;          !!!next-token;
2159          redo B;          redo B;
2160        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{type} == COMMENT_TOKEN) {
2161          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
2162          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
2163          ## Stay in the phase          ## Stay in the phase
2164          !!!next-token;          !!!next-token;
2165          redo B;          redo B;
2166        } elsif ($token->{type} eq 'character') {        } elsif ($token->{type} == CHARACTER_TOKEN) {
2167          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2168            $self->{document}->manakai_append_text ($1);            ## Ignore the token.
2169            ## ISSUE: DOM3 Core does not allow Document > Text  
2170            unless (length $token->{data}) {            unless (length $token->{data}) {
2171              ## Stay in the phase              ## Stay in the phase
2172              !!!next-token;              !!!next-token;
2173              redo B;              redo B;
2174            }            }
2175          }          }
2176    
2177            $self->{application_cache_selection}->(undef);
2178    
2179            #
2180          } elsif ($token->{type} == START_TAG_TOKEN) {
2181            if ($token->{tag_name} eq 'html' and
2182                $token->{attributes}->{manifest}) { ## ISSUE: Spec spells as "application"
2183              $self->{application_cache_selection}
2184                   ->($token->{attributes}->{manifest}->{value});
2185              ## ISSUE: No relative reference resolution?
2186            } else {
2187              $self->{application_cache_selection}->(undef);
2188            }
2189    
2190            ## ISSUE: There is an issue in the spec
2191          #          #
2192        } elsif ({        } elsif ({
2193                  'start tag' => 1,                  END_TAG_TOKEN, 1,
2194                  'end tag' => 1,                  END_OF_FILE_TOKEN, 1,
                 'end-of-file' => 1,  
2195                 }->{$token->{type}}) {                 }->{$token->{type}}) {
2196            $self->{application_cache_selection}->(undef);
2197    
2198          ## ISSUE: There is an issue in the spec          ## ISSUE: There is an issue in the spec
2199          #          #
2200        } else {        } else {
2201          die "$0: $token->{type}: Unknown token";          die "$0: $token->{type}: Unknown token type";
2202        }        }
2203    
2204        my $root_element; !!!create-element ($root_element, 'html');        my $root_element; !!!create-element ($root_element, 'html');
2205        $self->{document}->append_child ($root_element);        $self->{document}->append_child ($root_element);
2206        push @{$self->{open_elements}}, [$root_element, 'html'];        push @{$self->{open_elements}}, [$root_element, 'html'];
       #$phase = 'main';  
2207        ## reprocess        ## reprocess
2208        #redo B;        #redo B;
2209        return;        return; ## Go to the main phase.
2210    } # B    } # B
2211  } # _tree_construction_root_element  } # _tree_construction_root_element
2212    
# Line 1991  sub _reset_insertion_mode ($) { Line 2222  sub _reset_insertion_mode ($) {
2222            
2223      ## Step 3      ## Step 3
2224      S3: {      S3: {
2225        $last = 1 if $self->{open_elements}->[0]->[0] eq $node->[0];        ## ISSUE: Oops! "If node is the first node in the stack of open
2226        if (defined $self->{inner_html_node}) {        ## elements, then set last to true. If the context element of the
2227          if ($self->{inner_html_node}->[1] eq 'td' or        ## HTML fragment parsing algorithm is neither a td element nor a
2228              $self->{inner_html_node}->[1] eq 'th') {        ## th element, then set node to the context element. (fragment case)":
2229            #        ## The second "if" is in the scope of the first "if"!?
2230          } else {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
2231            $node = $self->{inner_html_node};          $last = 1;
2232            if (defined $self->{inner_html_node}) {
2233              if ($self->{inner_html_node}->[1] eq 'td' or
2234                  $self->{inner_html_node}->[1] eq 'th') {
2235                #
2236              } else {
2237                $node = $self->{inner_html_node};
2238              }
2239          }          }
2240        }        }
2241            
2242        ## Step 4..13        ## Step 4..13
2243        my $new_mode = {        my $new_mode = {
2244                        select => 'in select',                        select => IN_SELECT_IM,
2245                        td => 'in cell',                        td => IN_CELL_IM,
2246                        th => 'in cell',                        th => IN_CELL_IM,
2247                        tr => 'in row',                        tr => IN_ROW_IM,
2248                        tbody => 'in table body',                        tbody => IN_TABLE_BODY_IM,
2249                        thead => 'in table head',                        thead => IN_TABLE_BODY_IM,
2250                        tfoot => 'in table foot',                        tfoot => IN_TABLE_BODY_IM,
2251                        caption => 'in caption',                        caption => IN_CAPTION_IM,
2252                        colgroup => 'in column group',                        colgroup => IN_COLUMN_GROUP_IM,
2253                        table => 'in table',                        table => IN_TABLE_IM,
2254                        head => 'in body', # not in head!                        head => IN_BODY_IM, # not in head!
2255                        body => 'in body',                        body => IN_BODY_IM,
2256                        frameset => 'in frameset',                        frameset => IN_FRAMESET_IM,
2257                       }->{$node->[1]};                       }->{$node->[1]};
2258        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
2259                
2260        ## Step 14        ## Step 14
2261        if ($node->[1] eq 'html') {        if ($node->[1] eq 'html') {
2262          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
2263            $self->{insertion_mode} = 'before head';            $self->{insertion_mode} = BEFORE_HEAD_IM;
2264          } else {          } else {
2265            $self->{insertion_mode} = 'after head';            $self->{insertion_mode} = AFTER_HEAD_IM;
2266          }          }
2267          return;          return;
2268        }        }
2269                
2270        ## Step 15        ## Step 15
2271        $self->{insertion_mode} = 'in body' and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
2272                
2273        ## Step 16        ## Step 16
2274        $i--;        $i--;
# Line 2044  sub _reset_insertion_mode ($) { Line 2282  sub _reset_insertion_mode ($) {
2282  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
2283    my $self = shift;    my $self = shift;
2284    
   my $phase = 'main';  
   
2285    my $active_formatting_elements = [];    my $active_formatting_elements = [];
2286    
2287    my $reconstruct_active_formatting_elements = sub { # MUST    my $reconstruct_active_formatting_elements = sub { # MUST
# Line 2128  sub _tree_construction_main ($) { Line 2364  sub _tree_construction_main ($) {
2364      }      }
2365    }; # $clear_up_to_marker    }; # $clear_up_to_marker
2366    
2367    my $style_start_tag = sub {    my $parse_rcdata = sub ($$) {
2368      my $style_el; !!!create-element ($style_el, 'style', $token->{attributes});      my ($content_model_flag, $insert) = @_;
2369      ## $self->{insertion_mode} eq 'in head' and ... (always true)  
2370      (($self->{insertion_mode} eq 'in head' and defined $self->{head_element})      ## Step 1
2371       ? $self->{head_element} : $self->{open_elements}->[-1]->[0])      my $start_tag_name = $token->{tag_name};
2372        ->append_child ($style_el);      my $el;
2373      $self->{content_model_flag} = 'CDATA';      !!!create-element ($el, $start_tag_name, $token->{attributes});
2374    
2375        ## Step 2
2376        $insert->($el); # /context node/->append_child ($el)
2377    
2378        ## Step 3
2379        $self->{content_model} = $content_model_flag; # CDATA or RCDATA
2380      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
2381                  
2382        ## Step 4
2383      my $text = '';      my $text = '';
2384      !!!next-token;      !!!next-token;
2385      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
2386        $text .= $token->{data};        $text .= $token->{data};
2387        !!!next-token;        !!!next-token;
2388      } # stop if non-character token or tokenizer stops tokenising      }
2389    
2390        ## Step 5
2391      if (length $text) {      if (length $text) {
2392        $style_el->manakai_append_text ($text);        my $text = $self->{document}->create_text_node ($text);
2393          $el->append_child ($text);
2394      }      }
2395        
2396      $self->{content_model_flag} = 'PCDATA';      ## Step 6
2397                      $self->{content_model} = PCDATA_CONTENT_MODEL;
2398      if ($token->{type} eq 'end tag' and $token->{tag_name} eq 'style') {  
2399        ## Step 7
2400        if ($token->{type} == END_TAG_TOKEN and $token->{tag_name} eq $start_tag_name) {
2401        ## Ignore the token        ## Ignore the token
2402      } else {      } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {
2403        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!parse-error (type => 'in CDATA:#'.$token->{type});
2404        ## ISSUE: And ignore?      } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
2405          !!!parse-error (type => 'in RCDATA:#'.$token->{type});
2406        } else {
2407          die "$0: $content_model_flag in parse_rcdata";
2408      }      }
2409      !!!next-token;      !!!next-token;
2410    }; # $style_start_tag    }; # $parse_rcdata
2411    
2412    my $script_start_tag = sub {    my $script_start_tag = sub ($) {
2413        my $insert = $_[0];
2414      my $script_el;      my $script_el;
2415      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, 'script', $token->{attributes});
2416      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
2417    
2418      $self->{content_model_flag} = 'CDATA';      $self->{content_model} = CDATA_CONTENT_MODEL;
2419      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
2420            
2421      my $text = '';      my $text = '';
2422      !!!next-token;      !!!next-token;
2423      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) {
2424        $text .= $token->{data};        $text .= $token->{data};
2425        !!!next-token;        !!!next-token;
2426      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
# Line 2176  sub _tree_construction_main ($) { Line 2428  sub _tree_construction_main ($) {
2428        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
2429      }      }
2430                                
2431      $self->{content_model_flag} = 'PCDATA';      $self->{content_model} = PCDATA_CONTENT_MODEL;
2432    
2433      if ($token->{type} eq 'end tag' and      if ($token->{type} == END_TAG_TOKEN and
2434          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
2435        ## Ignore the token        ## Ignore the token
2436      } else {      } else {
# Line 2192  sub _tree_construction_main ($) { Line 2444  sub _tree_construction_main ($) {
2444      } else {      } else {
2445        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
2446        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
2447          
2448        (($self->{insertion_mode} eq 'in head' and defined $self->{head_element})        $insert->($script_el);
        ? $self->{head_element} : $self->{open_elements}->[-1]->[0])->append_child ($script_el);  
2449                
2450        ## TODO: insertion point = $old_insertion_point (might be "undefined")        ## TODO: insertion point = $old_insertion_point (might be "undefined")
2451                
# Line 2388  sub _tree_construction_main ($) { Line 2639  sub _tree_construction_main ($) {
2639    }; # $formatting_end_tag    }; # $formatting_end_tag
2640    
2641    my $insert_to_current = sub {    my $insert_to_current = sub {
2642      $self->{open_elements}->[-1]->[0]->append_child (shift);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
2643    }; # $insert_to_current    }; # $insert_to_current
2644    
2645    my $insert_to_foster = sub {    my $insert_to_foster = sub {
# Line 2422  sub _tree_construction_main ($) { Line 2673  sub _tree_construction_main ($) {
2673                         }                         }
2674    }; # $insert_to_foster    }; # $insert_to_foster
2675    
2676    my $in_body = sub {    my $insert;
2677      my $insert = shift;  
2678      if ($token->{type} eq 'start tag') {    B: {
2679        if ($token->{tag_name} eq 'script') {      if ($token->{type} == DOCTYPE_TOKEN) {
2680          $script_start_tag->();        !!!parse-error (type => 'DOCTYPE in the middle');
2681          return;        ## Ignore the token
2682        } elsif ($token->{tag_name} eq 'style') {        ## Stay in the phase
2683          $style_start_tag->();        !!!next-token;
2684          return;        redo B;
2685        } elsif ({      } elsif ($token->{type} == END_OF_FILE_TOKEN) {
2686                  base => 1, link => 1, meta => 1,        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
2687                 }->{$token->{tag_name}}) {          #
2688          ## NOTE: This is an "as if in head" code clone        } else {
2689          my $el;          ## Generate implied end tags
2690          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          if ({
2691          if ($self->{insertion_mode} eq 'in head' and               dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,
2692              defined $self->{head_element}) {               tbody => 1, tfoot=> 1, thead => 1,
2693            $self->{head_element}->append_child ($el);              }->{$self->{open_elements}->[-1]->[1]}) {
2694          } else {            !!!back-token;
2695            $insert->($el);            $token = {type => END_TAG_TOKEN, tag_name => $self->{open_elements}->[-1]->[1]};
2696              redo B;
2697          }          }
2698                    
2699            if (@{$self->{open_elements}} > 2 or
2700                (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {
2701              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
2702            } elsif (defined $self->{inner_html_node} and
2703                     @{$self->{open_elements}} > 1 and
2704                     $self->{open_elements}->[1]->[1] ne 'body') {
2705              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
2706            }
2707    
2708            ## ISSUE: There is an issue in the spec.
2709          }
2710    
2711          ## Stop parsing
2712          last B;
2713        } elsif ($token->{type} == START_TAG_TOKEN and
2714                 $token->{tag_name} eq 'html') {
2715          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
2716            ## Turn into the main phase
2717            !!!parse-error (type => 'after html:html');
2718            $self->{insertion_mode} = AFTER_BODY_IM;
2719          } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
2720            ## Turn into the main phase
2721            !!!parse-error (type => 'after html:html');
2722            $self->{insertion_mode} = AFTER_FRAMESET_IM;
2723          }
2724    
2725    ## ISSUE: "aa<html>" is not a parse error.
2726    ## ISSUE: "<html>" in fragment is not a parse error.
2727          unless ($token->{first_start_tag}) {
2728            !!!parse-error (type => 'not first start tag');
2729          }
2730          my $top_el = $self->{open_elements}->[0]->[0];
2731          for my $attr_name (keys %{$token->{attributes}}) {
2732            unless ($top_el->has_attribute_ns (undef, $attr_name)) {
2733              $top_el->set_attribute_ns
2734                (undef, [undef, $attr_name],
2735                 $token->{attributes}->{$attr_name}->{value});
2736            }
2737          }
2738          !!!next-token;
2739          redo B;
2740        } elsif ($token->{type} == COMMENT_TOKEN) {
2741          my $comment = $self->{document}->create_comment ($token->{data});
2742          if ($self->{insertion_mode} & AFTER_HTML_IMS) {
2743            $self->{document}->append_child ($comment);
2744          } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
2745            $self->{open_elements}->[0]->[0]->append_child ($comment);
2746          } else {
2747            $self->{open_elements}->[-1]->[0]->append_child ($comment);
2748          }
2749          !!!next-token;
2750          redo B;
2751        } elsif ($self->{insertion_mode} & HEAD_IMS) {
2752          if ($token->{type} == CHARACTER_TOKEN) {
2753            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
2754              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
2755              unless (length $token->{data}) {
2756                !!!next-token;
2757                redo B;
2758              }
2759            }
2760    
2761            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2762              ## As if <head>
2763              !!!create-element ($self->{head_element}, 'head');
2764              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2765              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2766    
2767              ## Reprocess in the "in head" insertion mode...
2768              pop @{$self->{open_elements}};
2769    
2770              ## Reprocess in the "after head" insertion mode...
2771            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2772              ## As if </noscript>
2773              pop @{$self->{open_elements}};
2774              !!!parse-error (type => 'in noscript:#character');
2775              
2776              ## Reprocess in the "in head" insertion mode...
2777              ## As if </head>
2778              pop @{$self->{open_elements}};
2779    
2780              ## Reprocess in the "after head" insertion mode...
2781            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2782              pop @{$self->{open_elements}};
2783    
2784              ## Reprocess in the "after head" insertion mode...
2785            }
2786    
2787                ## "after head" insertion mode
2788                ## As if <body>
2789                !!!insert-element ('body');
2790                $self->{insertion_mode} = IN_BODY_IM;
2791                ## reprocess
2792                redo B;
2793              } elsif ($token->{type} == START_TAG_TOKEN) {
2794                if ($token->{tag_name} eq 'head') {
2795                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2796                    !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});
2797                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2798                    push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];
2799                    $self->{insertion_mode} = IN_HEAD_IM;
2800                    !!!next-token;
2801                    redo B;
2802                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2803                    #
2804                  } else {
2805                    !!!parse-error (type => 'in head:head'); # or in head noscript
2806                    ## Ignore the token
2807                    !!!next-token;
2808                    redo B;
2809                  }
2810                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2811                  ## As if <head>
2812                  !!!create-element ($self->{head_element}, 'head');
2813                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2814                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2815    
2816                  $self->{insertion_mode} = IN_HEAD_IM;
2817                  ## Reprocess in the "in head" insertion mode...
2818                }
2819    
2820                if ($token->{tag_name} eq 'base') {
2821                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2822                    ## As if </noscript>
2823                    pop @{$self->{open_elements}};
2824                    !!!parse-error (type => 'in noscript:base');
2825                  
2826                    $self->{insertion_mode} = IN_HEAD_IM;
2827                    ## Reprocess in the "in head" insertion mode...
2828                  }
2829    
2830                  ## NOTE: There is a "as if in head" code clone.
2831                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2832                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2833                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2834                  }
2835                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2836                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2837                  pop @{$self->{open_elements}}
2838                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2839                  !!!next-token;
2840                  redo B;
2841                } elsif ($token->{tag_name} eq 'link') {
2842                  ## NOTE: There is a "as if in head" code clone.
2843                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2844                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2845                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2846                  }
2847                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2848                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2849                  pop @{$self->{open_elements}}
2850                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2851                  !!!next-token;
2852                  redo B;
2853                } elsif ($token->{tag_name} eq 'meta') {
2854                  ## NOTE: There is a "as if in head" code clone.
2855                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2856                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2857                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2858                  }
2859                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2860                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2861    
2862                  unless ($self->{confident}) {
2863                    if ($token->{attributes}->{charset}) { ## TODO: And if supported
2864                      $self->{change_encoding}
2865                          ->($self, $token->{attributes}->{charset}->{value});
2866                    } elsif ($token->{attributes}->{content}) {
2867                      ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
2868                      if ($token->{attributes}->{content}->{value}
2869                          =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=
2870                              [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
2871                              ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
2872                        $self->{change_encoding}
2873                            ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);
2874                      }
2875                    }
2876                  }
2877    
2878                  pop @{$self->{open_elements}}
2879                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2880                  !!!next-token;
2881                  redo B;
2882                } elsif ($token->{tag_name} eq 'title') {
2883                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2884                    ## As if </noscript>
2885                    pop @{$self->{open_elements}};
2886                    !!!parse-error (type => 'in noscript:title');
2887                  
2888                    $self->{insertion_mode} = IN_HEAD_IM;
2889                    ## Reprocess in the "in head" insertion mode...
2890                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2891                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2892                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2893                  }
2894    
2895                  ## NOTE: There is a "as if in head" code clone.
2896                  my $parent = defined $self->{head_element} ? $self->{head_element}
2897                      : $self->{open_elements}->[-1]->[0];
2898                  $parse_rcdata->(RCDATA_CONTENT_MODEL,
2899                                  sub { $parent->append_child ($_[0]) });
2900                  pop @{$self->{open_elements}}
2901                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2902                  redo B;
2903                } elsif ($token->{tag_name} eq 'style') {
2904                  ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
2905                  ## insertion mode IN_HEAD_IM)
2906                  ## NOTE: There is a "as if in head" code clone.
2907                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2908                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2909                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2910                  }
2911                  $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
2912                  pop @{$self->{open_elements}}
2913                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2914                  redo B;
2915                } elsif ($token->{tag_name} eq 'noscript') {
2916                  if ($self->{insertion_mode} == IN_HEAD_IM) {
2917                    ## NOTE: and scripting is disalbed
2918                    !!!insert-element ($token->{tag_name}, $token->{attributes});
2919                    $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
2920                    !!!next-token;
2921                    redo B;
2922                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2923                    !!!parse-error (type => 'in noscript:noscript');
2924                    ## Ignore the token
2925                    !!!next-token;
2926                    redo B;
2927                  } else {
2928                    #
2929                  }
2930                } elsif ($token->{tag_name} eq 'script') {
2931                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2932                    ## As if </noscript>
2933                    pop @{$self->{open_elements}};
2934                    !!!parse-error (type => 'in noscript:script');
2935                  
2936                    $self->{insertion_mode} = IN_HEAD_IM;
2937                    ## Reprocess in the "in head" insertion mode...
2938                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2939                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2940                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2941                  }
2942    
2943                  ## NOTE: There is a "as if in head" code clone.
2944                  $script_start_tag->($insert_to_current);
2945                  pop @{$self->{open_elements}}
2946                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2947                  redo B;
2948                } elsif ($token->{tag_name} eq 'body' or
2949                         $token->{tag_name} eq 'frameset') {
2950                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2951                    ## As if </noscript>
2952                    pop @{$self->{open_elements}};
2953                    !!!parse-error (type => 'in noscript:'.$token->{tag_name});
2954                    
2955                    ## Reprocess in the "in head" insertion mode...
2956                    ## As if </head>
2957                    pop @{$self->{open_elements}};
2958                    
2959                    ## Reprocess in the "after head" insertion mode...
2960                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2961                    pop @{$self->{open_elements}};
2962                    
2963                    ## Reprocess in the "after head" insertion mode...
2964                  }
2965    
2966                  ## "after head" insertion mode
2967                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2968                  if ($token->{tag_name} eq 'body') {
2969                    $self->{insertion_mode} = IN_BODY_IM;
2970                  } elsif ($token->{tag_name} eq 'frameset') {
2971                    $self->{insertion_mode} = IN_FRAMESET_IM;
2972                  } else {
2973                    die "$0: tag name: $self->{tag_name}";
2974                  }
2975                  !!!next-token;
2976                  redo B;
2977                } else {
2978                  #
2979                }
2980    
2981                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2982                  ## As if </noscript>
2983                  pop @{$self->{open_elements}};
2984                  !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
2985                  
2986                  ## Reprocess in the "in head" insertion mode...
2987                  ## As if </head>
2988                  pop @{$self->{open_elements}};
2989    
2990                  ## Reprocess in the "after head" insertion mode...
2991                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2992                  ## As if </head>
2993                  pop @{$self->{open_elements}};
2994    
2995                  ## Reprocess in the "after head" insertion mode...
2996                }
2997    
2998                ## "after head" insertion mode
2999                ## As if <body>
3000                !!!insert-element ('body');
3001                $self->{insertion_mode} = IN_BODY_IM;
3002                ## reprocess
3003                redo B;
3004              } elsif ($token->{type} == END_TAG_TOKEN) {
3005                if ($token->{tag_name} eq 'head') {
3006                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3007                    ## As if <head>
3008                    !!!create-element ($self->{head_element}, 'head');
3009                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3010                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3011    
3012                    ## Reprocess in the "in head" insertion mode...
3013                    pop @{$self->{open_elements}};
3014                    $self->{insertion_mode} = AFTER_HEAD_IM;
3015                    !!!next-token;
3016                    redo B;
3017                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3018                    ## As if </noscript>
3019                    pop @{$self->{open_elements}};
3020                    !!!parse-error (type => 'in noscript:script');
3021                    
3022                    ## Reprocess in the "in head" insertion mode...
3023                    pop @{$self->{open_elements}};
3024                    $self->{insertion_mode} = AFTER_HEAD_IM;
3025                    !!!next-token;
3026                    redo B;
3027                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3028                    pop @{$self->{open_elements}};
3029                    $self->{insertion_mode} = AFTER_HEAD_IM;
3030                    !!!next-token;
3031                    redo B;
3032                  } else {
3033                    #
3034                  }
3035                } elsif ($token->{tag_name} eq 'noscript') {
3036                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3037                    pop @{$self->{open_elements}};
3038                    $self->{insertion_mode} = IN_HEAD_IM;
3039                    !!!next-token;
3040                    redo B;
3041                  } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3042                    !!!parse-error (type => 'unmatched end tag:noscript');
3043                    ## Ignore the token ## ISSUE: An issue in the spec.
3044                    !!!next-token;
3045                    redo B;
3046                  } else {
3047                    #
3048                  }
3049                } elsif ({
3050                          body => 1, html => 1,
3051                         }->{$token->{tag_name}}) {
3052                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3053                    ## As if <head>
3054                    !!!create-element ($self->{head_element}, 'head');
3055                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3056                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3057    
3058                    $self->{insertion_mode} = IN_HEAD_IM;
3059                    ## Reprocess in the "in head" insertion mode...
3060                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3061                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3062                    ## Ignore the token
3063                    !!!next-token;
3064                    redo B;
3065                  }
3066                  
3067                  #
3068                } elsif ({
3069                          p => 1, br => 1,
3070                         }->{$token->{tag_name}}) {
3071                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3072                    ## As if <head>
3073                    !!!create-element ($self->{head_element}, 'head');
3074                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3075                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3076    
3077                    $self->{insertion_mode} = IN_HEAD_IM;
3078                    ## Reprocess in the "in head" insertion mode...
3079                  }
3080    
3081                  #
3082                } else {
3083                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3084                    #
3085                  } else {
3086                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3087                    ## Ignore the token
3088                    !!!next-token;
3089                    redo B;
3090                  }
3091                }
3092    
3093                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3094                  ## As if </noscript>
3095                  pop @{$self->{open_elements}};
3096                  !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
3097                  
3098                  ## Reprocess in the "in head" insertion mode...
3099                  ## As if </head>
3100                  pop @{$self->{open_elements}};
3101    
3102                  ## Reprocess in the "after head" insertion mode...
3103                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3104                  ## As if </head>
3105                  pop @{$self->{open_elements}};
3106    
3107                  ## Reprocess in the "after head" insertion mode...
3108                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3109                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3110                  ## Ignore the token ## ISSUE: An issue in the spec.
3111                  !!!next-token;
3112                  redo B;
3113                }
3114    
3115                ## "after head" insertion mode
3116                ## As if <body>
3117                !!!insert-element ('body');
3118                $self->{insertion_mode} = IN_BODY_IM;
3119                ## reprocess
3120                redo B;
3121              } else {
3122                die "$0: $token->{type}: Unknown token type";
3123              }
3124    
3125              ## ISSUE: An issue in the spec.
3126        } elsif ($self->{insertion_mode} & BODY_IMS) {
3127              if ($token->{type} == CHARACTER_TOKEN) {
3128                ## NOTE: There is a code clone of "character in body".
3129                $reconstruct_active_formatting_elements->($insert_to_current);
3130                
3131                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3132    
3133                !!!next-token;
3134                redo B;
3135              } elsif ($token->{type} == START_TAG_TOKEN) {
3136                if ({
3137                     caption => 1, col => 1, colgroup => 1, tbody => 1,
3138                     td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
3139                    }->{$token->{tag_name}}) {
3140                  if ($self->{insertion_mode} == IN_CELL_IM) {
3141                    ## have an element in table scope
3142                    my $tn;
3143                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3144                      my $node = $self->{open_elements}->[$_];
3145                      if ($node->[1] eq 'td' or $node->[1] eq 'th') {
3146                        $tn = $node->[1];
3147                        last INSCOPE;
3148                      } elsif ({
3149                                table => 1, html => 1,
3150                               }->{$node->[1]}) {
3151                        last INSCOPE;
3152                      }
3153                    } # INSCOPE
3154                      unless (defined $tn) {
3155                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3156                        ## Ignore the token
3157                        !!!next-token;
3158                        redo B;
3159                      }
3160                    
3161                    ## Close the cell
3162                    !!!back-token; # <?>
3163                    $token = {type => END_TAG_TOKEN, tag_name => $tn};
3164                    redo B;
3165                  } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3166                    !!!parse-error (type => 'not closed:caption');
3167                    
3168                    ## As if </caption>
3169                    ## have a table element in table scope
3170                    my $i;
3171                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3172                      my $node = $self->{open_elements}->[$_];
3173                      if ($node->[1] eq 'caption') {
3174                        $i = $_;
3175                        last INSCOPE;
3176                      } elsif ({
3177                                table => 1, html => 1,
3178                               }->{$node->[1]}) {
3179                        last INSCOPE;
3180                      }
3181                    } # INSCOPE
3182                      unless (defined $i) {
3183                        !!!parse-error (type => 'unmatched end tag:caption');
3184                        ## Ignore the token
3185                        !!!next-token;
3186                        redo B;
3187                      }
3188                    
3189                    ## generate implied end tags
3190                    if ({
3191                         dd => 1, dt => 1, li => 1, p => 1,
3192                         td => 1, th => 1, tr => 1,
3193                         tbody => 1, tfoot=> 1, thead => 1,
3194                        }->{$self->{open_elements}->[-1]->[1]}) {
3195                      !!!back-token; # <?>
3196                      $token = {type => END_TAG_TOKEN, tag_name => 'caption'};
3197                      !!!back-token;
3198                      $token = {type => END_TAG_TOKEN,
3199                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3200                      redo B;
3201                    }
3202    
3203                    if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3204                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3205                    }
3206                    
3207                    splice @{$self->{open_elements}}, $i;
3208                    
3209                    $clear_up_to_marker->();
3210                    
3211                    $self->{insertion_mode} = IN_TABLE_IM;
3212                    
3213                    ## reprocess
3214                    redo B;
3215                  } else {
3216                    #
3217                  }
3218                } else {
3219                  #
3220                }
3221              } elsif ($token->{type} == END_TAG_TOKEN) {
3222                if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
3223                  if ($self->{insertion_mode} == IN_CELL_IM) {
3224                    ## have an element in table scope
3225                    my $i;
3226                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3227                      my $node = $self->{open_elements}->[$_];
3228                      if ($node->[1] eq $token->{tag_name}) {
3229                        $i = $_;
3230                        last INSCOPE;
3231                      } elsif ({
3232                                table => 1, html => 1,
3233                               }->{$node->[1]}) {
3234                        last INSCOPE;
3235                      }
3236                    } # INSCOPE
3237                      unless (defined $i) {
3238                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3239                        ## Ignore the token
3240                        !!!next-token;
3241                        redo B;
3242                      }
3243                    
3244                    ## generate implied end tags
3245                    if ({
3246                         dd => 1, dt => 1, li => 1, p => 1,
3247                         td => ($token->{tag_name} eq 'th'),
3248                         th => ($token->{tag_name} eq 'td'),
3249                         tr => 1,
3250                         tbody => 1, tfoot=> 1, thead => 1,
3251                        }->{$self->{open_elements}->[-1]->[1]}) {
3252                      !!!back-token;
3253                      $token = {type => END_TAG_TOKEN,
3254                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3255                      redo B;
3256                    }
3257                    
3258                    if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
3259                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3260                    }
3261                    
3262                    splice @{$self->{open_elements}}, $i;
3263                    
3264                    $clear_up_to_marker->();
3265                    
3266                    $self->{insertion_mode} = IN_ROW_IM;
3267                    
3268                    !!!next-token;
3269                    redo B;
3270                  } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3271                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3272                    ## Ignore the token
3273                    !!!next-token;
3274                    redo B;
3275                  } else {
3276                    #
3277                  }
3278                } elsif ($token->{tag_name} eq 'caption') {
3279                  if ($self->{insertion_mode} == IN_CAPTION_IM) {
3280                    ## have a table element in table scope
3281                    my $i;
3282                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3283                      my $node = $self->{open_elements}->[$_];
3284                      if ($node->[1] eq $token->{tag_name}) {
3285                        $i = $_;
3286                        last INSCOPE;
3287                      } elsif ({
3288                                table => 1, html => 1,
3289                               }->{$node->[1]}) {
3290                        last INSCOPE;
3291                      }
3292                    } # INSCOPE
3293                      unless (defined $i) {
3294                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3295                        ## Ignore the token
3296                        !!!next-token;
3297                        redo B;
3298                      }
3299                    
3300                    ## generate implied end tags
3301                    if ({
3302                         dd => 1, dt => 1, li => 1, p => 1,
3303                         td => 1, th => 1, tr => 1,
3304                         tbody => 1, tfoot=> 1, thead => 1,
3305                        }->{$self->{open_elements}->[-1]->[1]}) {
3306                      !!!back-token;
3307                      $token = {type => END_TAG_TOKEN,
3308                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3309                      redo B;
3310                    }
3311                    
3312                    if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3313                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3314                    }
3315                    
3316                    splice @{$self->{open_elements}}, $i;
3317                    
3318                    $clear_up_to_marker->();
3319                    
3320                    $self->{insertion_mode} = IN_TABLE_IM;
3321                    
3322                    !!!next-token;
3323                    redo B;
3324                  } elsif ($self->{insertion_mode} == IN_CELL_IM) {
3325                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3326                    ## Ignore the token
3327                    !!!next-token;
3328                    redo B;
3329                  } else {
3330                    #
3331                  }
3332                } elsif ({
3333                          table => 1, tbody => 1, tfoot => 1,
3334                          thead => 1, tr => 1,
3335                         }->{$token->{tag_name}} and
3336                         $self->{insertion_mode} == IN_CELL_IM) {
3337                  ## have an element in table scope
3338                  my $i;
3339                  my $tn;
3340                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3341                    my $node = $self->{open_elements}->[$_];
3342                    if ($node->[1] eq $token->{tag_name}) {
3343                      $i = $_;
3344                      last INSCOPE;
3345                    } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {
3346                      $tn = $node->[1];
3347                      ## NOTE: There is exactly one |td| or |th| element
3348                      ## in scope in the stack of open elements by definition.
3349                    } elsif ({
3350                              table => 1, html => 1,
3351                             }->{$node->[1]}) {
3352                      last INSCOPE;
3353                    }
3354                  } # INSCOPE
3355                  unless (defined $i) {
3356                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3357                    ## Ignore the token
3358                    !!!next-token;
3359                    redo B;
3360                  }
3361    
3362                  ## Close the cell
3363                  !!!back-token; # </?>
3364                  $token = {type => END_TAG_TOKEN, tag_name => $tn};
3365                  redo B;
3366                } elsif ($token->{tag_name} eq 'table' and
3367                         $self->{insertion_mode} == IN_CAPTION_IM) {
3368                  !!!parse-error (type => 'not closed:caption');
3369    
3370                  ## As if </caption>
3371                  ## have a table element in table scope
3372                  my $i;
3373                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3374                    my $node = $self->{open_elements}->[$_];
3375                    if ($node->[1] eq 'caption') {
3376                      $i = $_;
3377                      last INSCOPE;
3378                    } elsif ({
3379                              table => 1, html => 1,
3380                             }->{$node->[1]}) {
3381                      last INSCOPE;
3382                    }
3383                  } # INSCOPE
3384                  unless (defined $i) {
3385                    !!!parse-error (type => 'unmatched end tag:caption');
3386                    ## Ignore the token
3387                    !!!next-token;
3388                    redo B;
3389                  }
3390                  
3391                  ## generate implied end tags
3392                  if ({
3393                       dd => 1, dt => 1, li => 1, p => 1,
3394                       td => 1, th => 1, tr => 1,
3395                       tbody => 1, tfoot=> 1, thead => 1,
3396                      }->{$self->{open_elements}->[-1]->[1]}) {
3397                    !!!back-token; # </table>
3398                    $token = {type => END_TAG_TOKEN, tag_name => 'caption'};
3399                    !!!back-token;
3400                    $token = {type => END_TAG_TOKEN,
3401                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3402                    redo B;
3403                  }
3404    
3405                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3406                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3407                  }
3408    
3409                  splice @{$self->{open_elements}}, $i;
3410    
3411                  $clear_up_to_marker->();
3412    
3413                  $self->{insertion_mode} = IN_TABLE_IM;
3414    
3415                  ## reprocess
3416                  redo B;
3417                } elsif ({
3418                          body => 1, col => 1, colgroup => 1, html => 1,
3419                         }->{$token->{tag_name}}) {
3420                  if ($self->{insertion_mode} & BODY_TABLE_IMS) {
3421                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3422                    ## Ignore the token
3423                    !!!next-token;
3424                    redo B;
3425                  } else {
3426                    #
3427                  }
3428                } elsif ({
3429                          tbody => 1, tfoot => 1,
3430                          thead => 1, tr => 1,
3431                         }->{$token->{tag_name}} and
3432                         $self->{insertion_mode} == IN_CAPTION_IM) {
3433                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3434                  ## Ignore the token
3435                  !!!next-token;
3436                  redo B;
3437                } else {
3438                  #
3439                }
3440          } else {
3441            die "$0: $token->{type}: Unknown token type";
3442          }
3443    
3444          $insert = $insert_to_current;
3445          #
3446        } elsif ($self->{insertion_mode} & TABLE_IMS) {
3447          if ($token->{type} == CHARACTER_TOKEN) {
3448                if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3449                  $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3450                  
3451                  unless (length $token->{data}) {
3452                    !!!next-token;
3453                    redo B;
3454                  }
3455                }
3456    
3457                !!!parse-error (type => 'in table:#character');
3458    
3459                ## As if in body, but insert into foster parent element
3460                ## ISSUE: Spec says that "whenever a node would be inserted
3461                ## into the current node" while characters might not be
3462                ## result in a new Text node.
3463                $reconstruct_active_formatting_elements->($insert_to_foster);
3464                
3465                if ({
3466                     table => 1, tbody => 1, tfoot => 1,
3467                     thead => 1, tr => 1,
3468                    }->{$self->{open_elements}->[-1]->[1]}) {
3469                  # MUST
3470                  my $foster_parent_element;
3471                  my $next_sibling;
3472                  my $prev_sibling;
3473                  OE: for (reverse 0..$#{$self->{open_elements}}) {
3474                    if ($self->{open_elements}->[$_]->[1] eq 'table') {
3475                      my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3476                      if (defined $parent and $parent->node_type == 1) {
3477                        $foster_parent_element = $parent;
3478                        $next_sibling = $self->{open_elements}->[$_]->[0];
3479                        $prev_sibling = $next_sibling->previous_sibling;
3480                      } else {
3481                        $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
3482                        $prev_sibling = $foster_parent_element->last_child;
3483                      }
3484                      last OE;
3485                    }
3486                  } # OE
3487                  $foster_parent_element = $self->{open_elements}->[0]->[0] and
3488                  $prev_sibling = $foster_parent_element->last_child
3489                    unless defined $foster_parent_element;
3490                  if (defined $prev_sibling and
3491                      $prev_sibling->node_type == 3) {
3492                    $prev_sibling->manakai_append_text ($token->{data});
3493                  } else {
3494                    $foster_parent_element->insert_before
3495                      ($self->{document}->create_text_node ($token->{data}),
3496                       $next_sibling);
3497                  }
3498                } else {
3499                  $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3500                }
3501                
3502                !!!next-token;
3503                redo B;
3504          } elsif ($token->{type} == START_TAG_TOKEN) {
3505                if ({
3506                     tr => ($self->{insertion_mode} != IN_ROW_IM),
3507                     th => 1, td => 1,
3508                    }->{$token->{tag_name}}) {
3509                  if ($self->{insertion_mode} == IN_TABLE_IM) {
3510                    ## Clear back to table context
3511                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
3512                           $self->{open_elements}->[-1]->[1] ne 'html') {
3513                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3514                      pop @{$self->{open_elements}};
3515                    }
3516                    
3517                    !!!insert-element ('tbody');
3518                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
3519                    ## reprocess in the "in table body" insertion mode...
3520                  }
3521    
3522                  if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3523                    unless ($token->{tag_name} eq 'tr') {
3524                      !!!parse-error (type => 'missing start tag:tr');
3525                    }
3526                    
3527                    ## Clear back to table body context
3528                    while (not {
3529                      tbody => 1, tfoot => 1, thead => 1, html => 1,
3530                    }->{$self->{open_elements}->[-1]->[1]}) {
3531                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3532                      pop @{$self->{open_elements}};
3533                    }
3534                    
3535                    $self->{insertion_mode} = IN_ROW_IM;
3536                    if ($token->{tag_name} eq 'tr') {
3537                      !!!insert-element ($token->{tag_name}, $token->{attributes});
3538                      !!!next-token;
3539                      redo B;
3540                    } else {
3541                      !!!insert-element ('tr');
3542                      ## reprocess in the "in row" insertion mode
3543                    }
3544                  }
3545    
3546                  ## Clear back to table row context
3547                  while (not {
3548                    tr => 1, html => 1,
3549                  }->{$self->{open_elements}->[-1]->[1]}) {
3550                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3551                    pop @{$self->{open_elements}};
3552                  }
3553                  
3554                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3555                  $self->{insertion_mode} = IN_CELL_IM;
3556    
3557                  push @$active_formatting_elements, ['#marker', ''];
3558                  
3559                  !!!next-token;
3560                  redo B;
3561                } elsif ({
3562                          caption => 1, col => 1, colgroup => 1,
3563                          tbody => 1, tfoot => 1, thead => 1,
3564                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
3565                         }->{$token->{tag_name}}) {
3566                  if ($self->{insertion_mode} == IN_ROW_IM) {
3567                    ## As if </tr>
3568                    ## have an element in table scope
3569                    my $i;
3570                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3571                      my $node = $self->{open_elements}->[$_];
3572                      if ($node->[1] eq 'tr') {
3573                        $i = $_;
3574                        last INSCOPE;
3575                      } elsif ({
3576                                table => 1, html => 1,
3577                               }->{$node->[1]}) {
3578                        last INSCOPE;
3579                      }
3580                    } # INSCOPE
3581                    unless (defined $i) {
3582                      !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});
3583                      ## Ignore the token
3584                      !!!next-token;
3585                      redo B;
3586                    }
3587                    
3588                    ## Clear back to table row context
3589                    while (not {
3590                      tr => 1, html => 1,
3591                    }->{$self->{open_elements}->[-1]->[1]}) {
3592                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3593                      pop @{$self->{open_elements}};
3594                    }
3595                    
3596                    pop @{$self->{open_elements}}; # tr
3597                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
3598                    if ($token->{tag_name} eq 'tr') {
3599                      ## reprocess
3600                      redo B;
3601                    } else {
3602                      ## reprocess in the "in table body" insertion mode...
3603                    }
3604                  }
3605    
3606                  if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3607                    ## have an element in table scope
3608                    my $i;
3609                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3610                      my $node = $self->{open_elements}->[$_];
3611                      if ({
3612                           tbody => 1, thead => 1, tfoot => 1,
3613                          }->{$node->[1]}) {
3614                        $i = $_;
3615                        last INSCOPE;
3616                      } elsif ({
3617                                table => 1, html => 1,
3618                               }->{$node->[1]}) {
3619                        last INSCOPE;
3620                      }
3621                    } # INSCOPE
3622                    unless (defined $i) {
3623                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3624                      ## Ignore the token
3625                      !!!next-token;
3626                      redo B;
3627                    }
3628    
3629                    ## Clear back to table body context
3630                    while (not {
3631                      tbody => 1, tfoot => 1, thead => 1, html => 1,
3632                    }->{$self->{open_elements}->[-1]->[1]}) {
3633                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3634                      pop @{$self->{open_elements}};
3635                    }
3636                    
3637                    ## As if <{current node}>
3638                    ## have an element in table scope
3639                    ## true by definition
3640                    
3641                    ## Clear back to table body context
3642                    ## nop by definition
3643                    
3644                    pop @{$self->{open_elements}};
3645                    $self->{insertion_mode} = IN_TABLE_IM;
3646                    ## reprocess in "in table" insertion mode...
3647                  }
3648    
3649                  if ($token->{tag_name} eq 'col') {
3650                    ## Clear back to table context
3651                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
3652                           $self->{open_elements}->[-1]->[1] ne 'html') {
3653                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3654                      pop @{$self->{open_elements}};
3655                    }
3656                    
3657                    !!!insert-element ('colgroup');
3658                    $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
3659                    ## reprocess
3660                    redo B;
3661                  } elsif ({
3662                            caption => 1,
3663                            colgroup => 1,
3664                            tbody => 1, tfoot => 1, thead => 1,
3665                           }->{$token->{tag_name}}) {
3666                    ## Clear back to table context
3667                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
3668                           $self->{open_elements}->[-1]->[1] ne 'html') {
3669                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3670                      pop @{$self->{open_elements}};
3671                    }
3672                    
3673                    push @$active_formatting_elements, ['#marker', '']
3674                        if $token->{tag_name} eq 'caption';
3675                    
3676                    !!!insert-element ($token->{tag_name}, $token->{attributes});
3677                    $self->{insertion_mode} = {
3678                                               caption => IN_CAPTION_IM,
3679                                               colgroup => IN_COLUMN_GROUP_IM,
3680                                               tbody => IN_TABLE_BODY_IM,
3681                                               tfoot => IN_TABLE_BODY_IM,
3682                                               thead => IN_TABLE_BODY_IM,
3683                                              }->{$token->{tag_name}};
3684                    !!!next-token;
3685                    redo B;
3686                  } else {
3687                    die "$0: in table: <>: $token->{tag_name}";
3688                  }
3689                } elsif ($token->{tag_name} eq 'table') {
3690                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3691    
3692                  ## As if </table>
3693                  ## have a table element in table scope
3694                  my $i;
3695                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3696                    my $node = $self->{open_elements}->[$_];
3697                    if ($node->[1] eq 'table') {
3698                      $i = $_;
3699                      last INSCOPE;
3700                    } elsif ({
3701                              table => 1, html => 1,
3702                             }->{$node->[1]}) {
3703                      last INSCOPE;
3704                    }
3705                  } # INSCOPE
3706                  unless (defined $i) {
3707                    !!!parse-error (type => 'unmatched end tag:table');
3708                    ## Ignore tokens </table><table>
3709                    !!!next-token;
3710                    redo B;
3711                  }
3712                  
3713                  ## generate implied end tags
3714                  if ({
3715                       dd => 1, dt => 1, li => 1, p => 1,
3716                       td => 1, th => 1, tr => 1,
3717                       tbody => 1, tfoot=> 1, thead => 1,
3718                      }->{$self->{open_elements}->[-1]->[1]}) {
3719                    !!!back-token; # <table>
3720                    $token = {type => END_TAG_TOKEN, tag_name => 'table'};
3721                    !!!back-token;
3722                    $token = {type => END_TAG_TOKEN,
3723                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3724                    redo B;
3725                  }
3726    
3727                  if ($self->{open_elements}->[-1]->[1] ne 'table') {
3728                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3729                  }
3730    
3731                  splice @{$self->{open_elements}}, $i;
3732    
3733                  $self->_reset_insertion_mode;
3734    
3735                  ## reprocess
3736                  redo B;
3737            } else {
3738              !!!parse-error (type => 'in table:'.$token->{tag_name});
3739    
3740              $insert = $insert_to_foster;
3741              #
3742            }
3743          } elsif ($token->{type} == END_TAG_TOKEN) {
3744                if ($token->{tag_name} eq 'tr' and
3745                    $self->{insertion_mode} == IN_ROW_IM) {
3746                  ## have an element in table scope
3747                  my $i;
3748                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3749                    my $node = $self->{open_elements}->[$_];
3750                    if ($node->[1] eq $token->{tag_name}) {
3751                      $i = $_;
3752                      last INSCOPE;
3753                    } elsif ({
3754                              table => 1, html => 1,
3755                             }->{$node->[1]}) {
3756                      last INSCOPE;
3757                    }
3758                  } # INSCOPE
3759                  unless (defined $i) {
3760                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3761                    ## Ignore the token
3762                    !!!next-token;
3763                    redo B;
3764                  }
3765    
3766                  ## Clear back to table row context
3767                  while (not {
3768                    tr => 1, html => 1,
3769                  }->{$self->{open_elements}->[-1]->[1]}) {
3770                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3771                    pop @{$self->{open_elements}};
3772                  }
3773    
3774                  pop @{$self->{open_elements}}; # tr
3775                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
3776                  !!!next-token;
3777                  redo B;
3778                } elsif ($token->{tag_name} eq 'table') {
3779                  if ($self->{insertion_mode} == IN_ROW_IM) {
3780                    ## As if </tr>
3781                    ## have an element in table scope
3782                    my $i;
3783                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3784                      my $node = $self->{open_elements}->[$_];
3785                      if ($node->[1] eq 'tr') {
3786                        $i = $_;
3787                        last INSCOPE;
3788                      } elsif ({
3789                                table => 1, html => 1,
3790                               }->{$node->[1]}) {
3791                        last INSCOPE;
3792                      }
3793                    } # INSCOPE
3794                    unless (defined $i) {
3795                      !!!parse-error (type => 'unmatched end tag:'.$token->{type});
3796                      ## Ignore the token
3797                      !!!next-token;
3798                      redo B;
3799                    }
3800                    
3801                    ## Clear back to table row context
3802                    while (not {
3803                      tr => 1, html => 1,
3804                    }->{$self->{open_elements}->[-1]->[1]}) {
3805                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3806                      pop @{$self->{open_elements}};
3807                    }
3808                    
3809                    pop @{$self->{open_elements}}; # tr
3810                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
3811                    ## reprocess in the "in table body" insertion mode...
3812                  }
3813    
3814                  if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3815                    ## have an element in table scope
3816                    my $i;
3817                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3818                      my $node = $self->{open_elements}->[$_];
3819                      if ({
3820                           tbody => 1, thead => 1, tfoot => 1,
3821                          }->{$node->[1]}) {
3822                        $i = $_;
3823                        last INSCOPE;
3824                      } elsif ({
3825                                table => 1, html => 1,
3826                               }->{$node->[1]}) {
3827                        last INSCOPE;
3828                      }
3829                    } # INSCOPE
3830                    unless (defined $i) {
3831                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3832                      ## Ignore the token
3833                      !!!next-token;
3834                      redo B;
3835                    }
3836                    
3837                    ## Clear back to table body context
3838                    while (not {
3839                      tbody => 1, tfoot => 1, thead => 1, html => 1,
3840                    }->{$self->{open_elements}->[-1]->[1]}) {
3841                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3842                      pop @{$self->{open_elements}};
3843                    }
3844                    
3845                    ## As if <{current node}>
3846                    ## have an element in table scope
3847                    ## true by definition
3848                    
3849                    ## Clear back to table body context
3850                    ## nop by definition
3851                    
3852                    pop @{$self->{open_elements}};
3853                    $self->{insertion_mode} = IN_TABLE_IM;
3854                    ## reprocess in the "in table" insertion mode...
3855                  }
3856    
3857                  ## have a table 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 $token->{tag_name}) {
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->{tag_name});
3872                    ## Ignore the token
3873                    !!!next-token;
3874                    redo B;
3875                  }
3876    
3877                  ## generate implied end tags
3878                  if ({
3879                       dd => 1, dt => 1, li => 1, p => 1,
3880                       td => 1, th => 1, tr => 1,
3881                       tbody => 1, tfoot=> 1, thead => 1,
3882                      }->{$self->{open_elements}->[-1]->[1]}) {
3883                    !!!back-token;
3884                    $token = {type => END_TAG_TOKEN,
3885                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3886                    redo B;
3887                  }
3888                  
3889                  if ($self->{open_elements}->[-1]->[1] ne 'table') {
3890                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3891                  }
3892                    
3893                  splice @{$self->{open_elements}}, $i;
3894                  
3895                  $self->_reset_insertion_mode;
3896                  
3897                  !!!next-token;
3898                  redo B;
3899                } elsif ({
3900                          tbody => 1, tfoot => 1, thead => 1,
3901                         }->{$token->{tag_name}} and
3902                         $self->{insertion_mode} & ROW_IMS) {
3903                  if ($self->{insertion_mode} == IN_ROW_IM) {
3904                    ## have an element in table scope
3905                    my $i;
3906                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3907                      my $node = $self->{open_elements}->[$_];
3908                      if ($node->[1] eq $token->{tag_name}) {
3909                        $i = $_;
3910                        last INSCOPE;
3911                      } elsif ({
3912                                table => 1, html => 1,
3913                               }->{$node->[1]}) {
3914                        last INSCOPE;
3915                      }
3916                    } # INSCOPE
3917                      unless (defined $i) {
3918                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3919                        ## Ignore the token
3920                        !!!next-token;
3921                        redo B;
3922                      }
3923                    
3924                    ## As if </tr>
3925                    ## have an element in table scope
3926                    my $i;
3927                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3928                      my $node = $self->{open_elements}->[$_];
3929                      if ($node->[1] eq 'tr') {
3930                        $i = $_;
3931                        last INSCOPE;
3932                      } elsif ({
3933                                table => 1, html => 1,
3934                               }->{$node->[1]}) {
3935                        last INSCOPE;
3936                      }
3937                    } # INSCOPE
3938                      unless (defined $i) {
3939                        !!!parse-error (type => 'unmatched end tag:tr');
3940                        ## Ignore the token
3941                        !!!next-token;
3942                        redo B;
3943                      }
3944                    
3945                    ## Clear back to table row context
3946                    while (not {
3947                      tr => 1, html => 1,
3948                    }->{$self->{open_elements}->[-1]->[1]}) {
3949                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3950                      pop @{$self->{open_elements}};
3951                    }
3952                    
3953                    pop @{$self->{open_elements}}; # tr
3954                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
3955                    ## reprocess in the "in table body" insertion mode...
3956                  }
3957    
3958                  ## have an element in table scope
3959                  my $i;
3960                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3961                    my $node = $self->{open_elements}->[$_];
3962                    if ($node->[1] eq $token->{tag_name}) {
3963                      $i = $_;
3964                      last INSCOPE;
3965                    } elsif ({
3966                              table => 1, html => 1,
3967                             }->{$node->[1]}) {
3968                      last INSCOPE;
3969                    }
3970                  } # INSCOPE
3971                  unless (defined $i) {
3972                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3973                    ## Ignore the token
3974                    !!!next-token;
3975                    redo B;
3976                  }
3977    
3978                  ## Clear back to table body context
3979                  while (not {
3980                    tbody => 1, tfoot => 1, thead => 1, html => 1,
3981                  }->{$self->{open_elements}->[-1]->[1]}) {
3982                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3983                    pop @{$self->{open_elements}};
3984                  }
3985    
3986                  pop @{$self->{open_elements}};
3987                  $self->{insertion_mode} = IN_TABLE_IM;
3988                  !!!next-token;
3989                  redo B;
3990                } elsif ({
3991                          body => 1, caption => 1, col => 1, colgroup => 1,
3992                          html => 1, td => 1, th => 1,
3993                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
3994                          tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
3995                         }->{$token->{tag_name}}) {
3996                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3997                  ## Ignore the token
3998                  !!!next-token;
3999                  redo B;
4000            } else {
4001              !!!parse-error (type => 'in table:/'.$token->{tag_name});
4002    
4003              $insert = $insert_to_foster;
4004              #
4005            }
4006          } else {
4007            die "$0: $token->{type}: Unknown token type";
4008          }
4009        } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
4010              if ($token->{type} == CHARACTER_TOKEN) {
4011                if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4012                  $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4013                  unless (length $token->{data}) {
4014                    !!!next-token;
4015                    redo B;
4016                  }
4017                }
4018                
4019                #
4020              } elsif ($token->{type} == START_TAG_TOKEN) {
4021                if ($token->{tag_name} eq 'col') {
4022                  !!!insert-element ($token->{tag_name}, $token->{attributes});
4023                  pop @{$self->{open_elements}};
4024                  !!!next-token;
4025                  redo B;
4026                } else {
4027                  #
4028                }
4029              } elsif ($token->{type} == END_TAG_TOKEN) {
4030                if ($token->{tag_name} eq 'colgroup') {
4031                  if ($self->{open_elements}->[-1]->[1] eq 'html') {
4032                    !!!parse-error (type => 'unmatched end tag:colgroup');
4033                    ## Ignore the token
4034                    !!!next-token;
4035                    redo B;
4036                  } else {
4037                    pop @{$self->{open_elements}}; # colgroup
4038                    $self->{insertion_mode} = IN_TABLE_IM;
4039                    !!!next-token;
4040                    redo B;            
4041                  }
4042                } elsif ($token->{tag_name} eq 'col') {
4043                  !!!parse-error (type => 'unmatched end tag:col');
4044                  ## Ignore the token
4045                  !!!next-token;
4046                  redo B;
4047                } else {
4048                  #
4049                }
4050              } else {
4051                #
4052              }
4053    
4054              ## As if </colgroup>
4055              if ($self->{open_elements}->[-1]->[1] eq 'html') {
4056                !!!parse-error (type => 'unmatched end tag:colgroup');
4057                ## Ignore the token
4058                !!!next-token;
4059                redo B;
4060              } else {
4061                pop @{$self->{open_elements}}; # colgroup
4062                $self->{insertion_mode} = IN_TABLE_IM;
4063                ## reprocess
4064                redo B;
4065              }
4066        } elsif ($self->{insertion_mode} == IN_SELECT_IM) {
4067          if ($token->{type} == CHARACTER_TOKEN) {
4068            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4069          !!!next-token;          !!!next-token;
4070          return;          redo B;
4071        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{type} == START_TAG_TOKEN) {
4072          !!!parse-error (type => 'in body:title');              if ($token->{tag_name} eq 'option') {
4073          ## NOTE: There is an "as if in head" code clone                if ($self->{open_elements}->[-1]->[1] eq 'option') {
4074          my $title_el;                  ## As if </option>
4075          !!!create-element ($title_el, 'title', $token->{attributes});                  pop @{$self->{open_elements}};
4076          (defined $self->{head_element} ? $self->{head_element} : $self->{open_elements}->[-1]->[0])                }
4077            ->append_child ($title_el);  
4078          $self->{content_model_flag} = 'RCDATA';                !!!insert-element ($token->{tag_name}, $token->{attributes});
4079          delete $self->{escape}; # MUST                !!!next-token;
4080                          redo B;
4081          my $text = '';              } elsif ($token->{tag_name} eq 'optgroup') {
4082          !!!next-token;                if ($self->{open_elements}->[-1]->[1] eq 'option') {
4083          while ($token->{type} eq 'character') {                  ## As if </option>
4084            $text .= $token->{data};                  pop @{$self->{open_elements}};
4085                  }
4086    
4087                  if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {
4088                    ## As if </optgroup>
4089                    pop @{$self->{open_elements}};
4090                  }
4091    
4092                  !!!insert-element ($token->{tag_name}, $token->{attributes});
4093                  !!!next-token;
4094                  redo B;
4095                } elsif ($token->{tag_name} eq 'select') {
4096                  !!!parse-error (type => 'not closed:select');
4097                  ## As if </select> instead
4098                  ## have an element in table scope
4099                  my $i;
4100                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4101                    my $node = $self->{open_elements}->[$_];
4102                    if ($node->[1] eq $token->{tag_name}) {
4103                      $i = $_;
4104                      last INSCOPE;
4105                    } elsif ({
4106                              table => 1, html => 1,
4107                             }->{$node->[1]}) {
4108                      last INSCOPE;
4109                    }
4110                  } # INSCOPE
4111                  unless (defined $i) {
4112                    !!!parse-error (type => 'unmatched end tag:select');
4113                    ## Ignore the token
4114                    !!!next-token;
4115                    redo B;
4116                  }
4117                  
4118                  splice @{$self->{open_elements}}, $i;
4119    
4120                  $self->_reset_insertion_mode;
4121    
4122                  !!!next-token;
4123                  redo B;
4124            } else {
4125              !!!parse-error (type => 'in select:'.$token->{tag_name});
4126              ## Ignore the token
4127            !!!next-token;            !!!next-token;
4128              redo B;
4129          }          }
4130          if (length $text) {        } elsif ($token->{type} == END_TAG_TOKEN) {
4131            $title_el->manakai_append_text ($text);              if ($token->{tag_name} eq 'optgroup') {
4132                  if ($self->{open_elements}->[-1]->[1] eq 'option' and
4133                      $self->{open_elements}->[-2]->[1] eq 'optgroup') {
4134                    ## As if </option>
4135                    splice @{$self->{open_elements}}, -2;
4136                  } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {
4137                    pop @{$self->{open_elements}};
4138                  } else {
4139                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4140                    ## Ignore the token
4141                  }
4142                  !!!next-token;
4143                  redo B;
4144                } elsif ($token->{tag_name} eq 'option') {
4145                  if ($self->{open_elements}->[-1]->[1] eq 'option') {
4146                    pop @{$self->{open_elements}};
4147                  } else {
4148                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4149                    ## Ignore the token
4150                  }
4151                  !!!next-token;
4152                  redo B;
4153                } elsif ($token->{tag_name} eq 'select') {
4154                  ## have an element in table scope
4155                  my $i;
4156                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4157                    my $node = $self->{open_elements}->[$_];
4158                    if ($node->[1] eq $token->{tag_name}) {
4159                      $i = $_;
4160                      last INSCOPE;
4161                    } elsif ({
4162                              table => 1, html => 1,
4163                             }->{$node->[1]}) {
4164                      last INSCOPE;
4165                    }
4166                  } # INSCOPE
4167                  unless (defined $i) {
4168                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4169                    ## Ignore the token
4170                    !!!next-token;
4171                    redo B;
4172                  }
4173                  
4174                  splice @{$self->{open_elements}}, $i;
4175    
4176                  $self->_reset_insertion_mode;
4177    
4178                  !!!next-token;
4179                  redo B;
4180                } elsif ({
4181                          caption => 1, table => 1, tbody => 1,
4182                          tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
4183                         }->{$token->{tag_name}}) {
4184                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4185                  
4186                  ## have an element in table scope
4187                  my $i;
4188                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4189                    my $node = $self->{open_elements}->[$_];
4190                    if ($node->[1] eq $token->{tag_name}) {
4191                      $i = $_;
4192                      last INSCOPE;
4193                    } elsif ({
4194                              table => 1, html => 1,
4195                             }->{$node->[1]}) {
4196                      last INSCOPE;
4197                    }
4198                  } # INSCOPE
4199                  unless (defined $i) {
4200                    ## Ignore the token
4201                    !!!next-token;
4202                    redo B;
4203                  }
4204                  
4205                  ## As if </select>
4206                  ## have an element in table scope
4207                  undef $i;
4208                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4209                    my $node = $self->{open_elements}->[$_];
4210                    if ($node->[1] eq 'select') {
4211                      $i = $_;
4212                      last INSCOPE;
4213                    } elsif ({
4214                              table => 1, html => 1,
4215                             }->{$node->[1]}) {
4216                      last INSCOPE;
4217                    }
4218                  } # INSCOPE
4219                  unless (defined $i) {
4220                    !!!parse-error (type => 'unmatched end tag:select');
4221                    ## Ignore the </select> token
4222                    !!!next-token; ## TODO: ok?
4223                    redo B;
4224                  }
4225                  
4226                  splice @{$self->{open_elements}}, $i;
4227    
4228                  $self->_reset_insertion_mode;
4229    
4230                  ## reprocess
4231                  redo B;
4232            } else {
4233              !!!parse-error (type => 'in select:/'.$token->{tag_name});
4234              ## Ignore the token
4235              !!!next-token;
4236              redo B;
4237            }
4238          } else {
4239            die "$0: $token->{type}: Unknown token type";
4240          }
4241        } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
4242          if ($token->{type} == CHARACTER_TOKEN) {
4243            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4244              my $data = $1;
4245              ## As if in body
4246              $reconstruct_active_formatting_elements->($insert_to_current);
4247                  
4248              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4249              
4250              unless (length $token->{data}) {
4251                !!!next-token;
4252                redo B;
4253              }
4254          }          }
4255                    
4256          $self->{content_model_flag} = 'PCDATA';          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4257              !!!parse-error (type => 'after html:#character');
4258    
4259              ## Reprocess in the "main" phase, "after body" insertion mode...
4260            }
4261                    
4262          if ($token->{type} eq 'end tag' and          ## "after body" insertion mode
4263              $token->{tag_name} eq 'title') {          !!!parse-error (type => 'after body:#character');
4264    
4265            $self->{insertion_mode} = IN_BODY_IM;
4266            ## reprocess
4267            redo B;
4268          } elsif ($token->{type} == START_TAG_TOKEN) {
4269            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4270              !!!parse-error (type => 'after html:'.$token->{tag_name});
4271              
4272              ## Reprocess in the "main" phase, "after body" insertion mode...
4273            }
4274    
4275            ## "after body" insertion mode
4276            !!!parse-error (type => 'after body:'.$token->{tag_name});
4277    
4278            $self->{insertion_mode} = IN_BODY_IM;
4279            ## reprocess
4280            redo B;
4281          } elsif ($token->{type} == END_TAG_TOKEN) {
4282            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4283              !!!parse-error (type => 'after html:/'.$token->{tag_name});
4284              
4285              $self->{insertion_mode} = AFTER_BODY_IM;
4286              ## Reprocess in the "main" phase, "after body" insertion mode...
4287            }
4288    
4289            ## "after body" insertion mode
4290            if ($token->{tag_name} eq 'html') {
4291              if (defined $self->{inner_html_node}) {
4292                !!!parse-error (type => 'unmatched end tag:html');
4293                ## Ignore the token
4294                !!!next-token;
4295                redo B;
4296              } else {
4297                $self->{insertion_mode} = AFTER_HTML_BODY_IM;
4298                !!!next-token;
4299                redo B;
4300              }
4301            } else {
4302              !!!parse-error (type => 'after body:/'.$token->{tag_name});
4303    
4304              $self->{insertion_mode} = IN_BODY_IM;
4305              ## reprocess
4306              redo B;
4307            }
4308          } else {
4309            die "$0: $token->{type}: Unknown token type";
4310          }
4311        } elsif ($self->{insertion_mode} & FRAME_IMS) {
4312          if ($token->{type} == CHARACTER_TOKEN) {
4313            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4314              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4315              
4316              unless (length $token->{data}) {
4317                !!!next-token;
4318                redo B;
4319              }
4320            }
4321            
4322            if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
4323              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4324                !!!parse-error (type => 'in frameset:#character');
4325              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
4326                !!!parse-error (type => 'after frameset:#character');
4327              } else { # "after html frameset"
4328                !!!parse-error (type => 'after html:#character');
4329    
4330                $self->{insertion_mode} = AFTER_FRAMESET_IM;
4331                ## Reprocess in the "main" phase, "after frameset"...
4332                !!!parse-error (type => 'after frameset:#character');
4333              }
4334              
4335              ## Ignore the token.
4336              if (length $token->{data}) {
4337                ## reprocess the rest of characters
4338              } else {
4339                !!!next-token;
4340              }
4341              redo B;
4342            }
4343            
4344            die qq[$0: Character "$token->{data}"];
4345          } elsif ($token->{type} == START_TAG_TOKEN) {
4346            if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4347              !!!parse-error (type => 'after html:'.$token->{tag_name});
4348    
4349              $self->{insertion_mode} = AFTER_FRAMESET_IM;
4350              ## Process in the "main" phase, "after frameset" insertion mode...
4351            }
4352    
4353            if ($token->{tag_name} eq 'frameset' and
4354                $self->{insertion_mode} == IN_FRAMESET_IM) {
4355              !!!insert-element ($token->{tag_name}, $token->{attributes});
4356              !!!next-token;
4357              redo B;
4358            } elsif ($token->{tag_name} eq 'frame' and
4359                     $self->{insertion_mode} == IN_FRAMESET_IM) {
4360              !!!insert-element ($token->{tag_name}, $token->{attributes});
4361              pop @{$self->{open_elements}};
4362              !!!next-token;
4363              redo B;
4364            } elsif ($token->{tag_name} eq 'noframes') {
4365              ## NOTE: As if in body.
4366              $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
4367              redo B;
4368            } else {
4369              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4370                !!!parse-error (type => 'in frameset:'.$token->{tag_name});
4371              } else {
4372                !!!parse-error (type => 'after frameset:'.$token->{tag_name});
4373              }
4374            ## Ignore the token            ## Ignore the token
4375              !!!next-token;
4376              redo B;
4377            }
4378          } elsif ($token->{type} == END_TAG_TOKEN) {
4379            if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4380              !!!parse-error (type => 'after html:/'.$token->{tag_name});
4381    
4382              $self->{insertion_mode} = AFTER_FRAMESET_IM;
4383              ## Process in the "main" phase, "after frameset" insertion mode...
4384            }
4385    
4386            if ($token->{tag_name} eq 'frameset' and
4387                $self->{insertion_mode} == IN_FRAMESET_IM) {
4388              if ($self->{open_elements}->[-1]->[1] eq 'html' and
4389                  @{$self->{open_elements}} == 1) {
4390                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4391                ## Ignore the token
4392                !!!next-token;
4393              } else {
4394                pop @{$self->{open_elements}};
4395                !!!next-token;
4396              }
4397    
4398              if (not defined $self->{inner_html_node} and
4399                  $self->{open_elements}->[-1]->[1] ne 'frameset') {
4400                $self->{insertion_mode} = AFTER_FRAMESET_IM;
4401              }
4402              redo B;
4403            } elsif ($token->{tag_name} eq 'html' and
4404                     $self->{insertion_mode} == AFTER_FRAMESET_IM) {
4405              $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
4406              !!!next-token;
4407              redo B;
4408          } else {          } else {
4409            !!!parse-error (type => 'in RCDATA:#'.$token->{type});            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4410            ## ISSUE: And ignore?              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});
4411              } else {
4412                !!!parse-error (type => 'after frameset:/'.$token->{tag_name});
4413              }
4414              ## Ignore the token
4415              !!!next-token;
4416              redo B;
4417          }          }
4418          } else {
4419            die "$0: $token->{type}: Unknown token type";
4420          }
4421    
4422          ## ISSUE: An issue in spec here
4423        } else {
4424          die "$0: $self->{insertion_mode}: Unknown insertion mode";
4425        }
4426    
4427        ## "in body" insertion mode
4428        if ($token->{type} == START_TAG_TOKEN) {
4429          if ($token->{tag_name} eq 'script') {
4430            ## NOTE: This is an "as if in head" code clone
4431            $script_start_tag->($insert);
4432            redo B;
4433          } elsif ($token->{tag_name} eq 'style') {
4434            ## NOTE: This is an "as if in head" code clone
4435            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4436            redo B;
4437          } elsif ({
4438                    base => 1, link => 1,
4439                   }->{$token->{tag_name}}) {
4440            ## NOTE: This is an "as if in head" code clone, only "-t" differs
4441            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4442            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4443          !!!next-token;          !!!next-token;
4444          return;          redo B;
4445          } elsif ($token->{tag_name} eq 'meta') {
4446            ## NOTE: This is an "as if in head" code clone, only "-t" differs
4447            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4448            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4449    
4450            unless ($self->{confident}) {
4451              if ($token->{attributes}->{charset}) { ## TODO: And if supported
4452                $self->{change_encoding}
4453                    ->($self, $token->{attributes}->{charset}->{value});
4454              } elsif ($token->{attributes}->{content}) {
4455                ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
4456                if ($token->{attributes}->{content}->{value}
4457                    =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=
4458                        [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4459                        ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
4460                  $self->{change_encoding}
4461                      ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);
4462                }
4463              }
4464            }
4465    
4466            !!!next-token;
4467            redo B;
4468          } elsif ($token->{tag_name} eq 'title') {
4469            !!!parse-error (type => 'in body:title');
4470            ## NOTE: This is an "as if in head" code clone
4471            $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {
4472              if (defined $self->{head_element}) {
4473                $self->{head_element}->append_child ($_[0]);
4474              } else {
4475                $insert->($_[0]);
4476              }
4477            });
4478            redo B;
4479        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
4480          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body:body');
4481                                
# Line 2494  sub _tree_construction_main ($) { Line 4493  sub _tree_construction_main ($) {
4493            }            }
4494          }          }
4495          !!!next-token;          !!!next-token;
4496          return;          redo B;
4497        } elsif ({        } elsif ({
4498                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
4499                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1, listing => 1,
# Line 2505  sub _tree_construction_main ($) { Line 4504  sub _tree_construction_main ($) {
4504          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4505            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4506              !!!back-token;              !!!back-token;
4507              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4508              return;              redo B;
4509            } elsif ({            } elsif ({
4510                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4511                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2518  sub _tree_construction_main ($) { Line 4517  sub _tree_construction_main ($) {
4517          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4518          if ($token->{tag_name} eq 'pre') {          if ($token->{tag_name} eq 'pre') {
4519            !!!next-token;            !!!next-token;
4520            if ($token->{type} eq 'character') {            if ($token->{type} == CHARACTER_TOKEN) {
4521              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
4522              unless (length $token->{data}) {              unless (length $token->{data}) {
4523                !!!next-token;                !!!next-token;
# Line 2527  sub _tree_construction_main ($) { Line 4526  sub _tree_construction_main ($) {
4526          } else {          } else {
4527            !!!next-token;            !!!next-token;
4528          }          }
4529          return;          redo B;
4530        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
4531          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
4532            !!!parse-error (type => 'in form:form');            !!!parse-error (type => 'in form:form');
4533            ## Ignore the token            ## Ignore the token
4534            !!!next-token;            !!!next-token;
4535            return;            redo B;
4536          } else {          } else {
4537            ## has a p element in scope            ## has a p element in scope
4538            INSCOPE: for (reverse @{$self->{open_elements}}) {            INSCOPE: for (reverse @{$self->{open_elements}}) {
4539              if ($_->[1] eq 'p') {              if ($_->[1] eq 'p') {
4540                !!!back-token;                !!!back-token;
4541                $token = {type => 'end tag', tag_name => 'p'};                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4542                return;                redo B;
4543              } elsif ({              } elsif ({
4544                        table => 1, caption => 1, td => 1, th => 1,                        table => 1, caption => 1, td => 1, th => 1,
4545                        button => 1, marquee => 1, object => 1, html => 1,                        button => 1, marquee => 1, object => 1, html => 1,
# Line 2552  sub _tree_construction_main ($) { Line 4551  sub _tree_construction_main ($) {
4551            !!!insert-element-t ($token->{tag_name}, $token->{attributes});            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4552            $self->{form_element} = $self->{open_elements}->[-1]->[0];            $self->{form_element} = $self->{open_elements}->[-1]->[0];
4553            !!!next-token;            !!!next-token;
4554            return;            redo B;
4555          }          }
4556        } elsif ($token->{tag_name} eq 'li') {        } elsif ($token->{tag_name} eq 'li') {
4557          ## has a p element in scope          ## has a p element in scope
4558          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4559            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4560              !!!back-token;              !!!back-token;
4561              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4562              return;              redo B;
4563            } elsif ({            } elsif ({
4564                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4565                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2578  sub _tree_construction_main ($) { Line 4577  sub _tree_construction_main ($) {
4577              if ($i != -1) {              if ($i != -1) {
4578                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'end tag missing:'.
4579                                $self->{open_elements}->[-1]->[1]);                                $self->{open_elements}->[-1]->[1]);
               ## TODO: test  
4580              }              }
4581              splice @{$self->{open_elements}}, $i;              splice @{$self->{open_elements}}, $i;
4582              last LI;              last LI;
# Line 2601  sub _tree_construction_main ($) { Line 4599  sub _tree_construction_main ($) {
4599                        
4600          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4601          !!!next-token;          !!!next-token;
4602          return;          redo B;
4603        } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {        } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {
4604          ## has a p element in scope          ## has a p element in scope
4605          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4606            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4607              !!!back-token;              !!!back-token;
4608              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4609              return;              redo B;
4610            } elsif ({            } elsif ({
4611                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4612                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2626  sub _tree_construction_main ($) { Line 4624  sub _tree_construction_main ($) {
4624              if ($i != -1) {              if ($i != -1) {
4625                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'end tag missing:'.
4626                                $self->{open_elements}->[-1]->[1]);                                $self->{open_elements}->[-1]->[1]);
               ## TODO: test  
4627              }              }
4628              splice @{$self->{open_elements}}, $i;              splice @{$self->{open_elements}}, $i;
4629              last LI;              last LI;
# Line 2649  sub _tree_construction_main ($) { Line 4646  sub _tree_construction_main ($) {
4646                        
4647          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4648          !!!next-token;          !!!next-token;
4649          return;          redo B;
4650        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
4651          ## has a p element in scope          ## has a p element in scope
4652          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4653            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4654              !!!back-token;              !!!back-token;
4655              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4656              return;              redo B;
4657            } elsif ({            } elsif ({
4658                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4659                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2667  sub _tree_construction_main ($) { Line 4664  sub _tree_construction_main ($) {
4664                        
4665          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4666                        
4667          $self->{content_model_flag} = 'PLAINTEXT';          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
4668                        
4669          !!!next-token;          !!!next-token;
4670          return;          redo B;
4671        } elsif ({        } elsif ({
4672                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
4673                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 2679  sub _tree_construction_main ($) { Line 4676  sub _tree_construction_main ($) {
4676            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
4677            if ($node->[1] eq 'p') {            if ($node->[1] eq 'p') {
4678              !!!back-token;              !!!back-token;
4679              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4680              return;              redo B;
4681            } elsif ({            } elsif ({
4682                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4683                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2715  sub _tree_construction_main ($) { Line 4712  sub _tree_construction_main ($) {
4712          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4713                        
4714          !!!next-token;          !!!next-token;
4715          return;          redo B;
4716        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
4717          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
4718            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
# Line 2723  sub _tree_construction_main ($) { Line 4720  sub _tree_construction_main ($) {
4720              !!!parse-error (type => 'in a:a');              !!!parse-error (type => 'in a:a');
4721                            
4722              !!!back-token;              !!!back-token;
4723              $token = {type => 'end tag', tag_name => 'a'};              $token = {type => END_TAG_TOKEN, tag_name => 'a'};
4724              $formatting_end_tag->($token->{tag_name});              $formatting_end_tag->($token->{tag_name});
4725                            
4726              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
# Line 2750  sub _tree_construction_main ($) { Line 4747  sub _tree_construction_main ($) {
4747          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
4748    
4749          !!!next-token;          !!!next-token;
4750          return;          redo B;
4751        } elsif ({        } elsif ({
4752                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
4753                  s => 1, small => 1, strile => 1,                  s => 1, small => 1, strile => 1,
# Line 2762  sub _tree_construction_main ($) { Line 4759  sub _tree_construction_main ($) {
4759          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
4760                    
4761          !!!next-token;          !!!next-token;
4762          return;          redo B;
4763        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
4764          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
4765    
# Line 2770  sub _tree_construction_main ($) { Line 4767  sub _tree_construction_main ($) {
4767          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4768            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
4769            if ($node->[1] eq 'nobr') {            if ($node->[1] eq 'nobr') {
4770                !!!parse-error (type => 'in nobr:nobr');
4771              !!!back-token;              !!!back-token;
4772              $token = {type => 'end tag', tag_name => 'nobr'};              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};
4773              return;              redo B;
4774            } elsif ({            } elsif ({
4775                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4776                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2785  sub _tree_construction_main ($) { Line 4783  sub _tree_construction_main ($) {
4783          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
4784                    
4785          !!!next-token;          !!!next-token;
4786          return;          redo B;
4787        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
4788          ## has a button element in scope          ## has a button element in scope
4789          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 2793  sub _tree_construction_main ($) { Line 4791  sub _tree_construction_main ($) {
4791            if ($node->[1] eq 'button') {            if ($node->[1] eq 'button') {
4792              !!!parse-error (type => 'in button:button');              !!!parse-error (type => 'in button:button');
4793              !!!back-token;              !!!back-token;
4794              $token = {type => 'end tag', tag_name => 'button'};              $token = {type => END_TAG_TOKEN, tag_name => 'button'};
4795              return;              redo B;
4796            } elsif ({            } elsif ({
4797                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4798                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2809  sub _tree_construction_main ($) { Line 4807  sub _tree_construction_main ($) {
4807          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
4808    
4809          !!!next-token;          !!!next-token;
4810          return;          redo B;
4811        } elsif ($token->{tag_name} eq 'marquee' or        } elsif ($token->{tag_name} eq 'marquee' or
4812                 $token->{tag_name} eq 'object') {                 $token->{tag_name} eq 'object') {
4813          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
# Line 2818  sub _tree_construction_main ($) { Line 4816  sub _tree_construction_main ($) {
4816          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
4817                    
4818          !!!next-token;          !!!next-token;
4819          return;          redo B;
4820        } elsif ($token->{tag_name} eq 'xmp') {        } elsif ($token->{tag_name} eq 'xmp') {
4821          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
4822                    $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4823          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          redo B;
           
         $self->{content_model_flag} = 'CDATA';  
         delete $self->{escape}; # MUST  
           
         !!!next-token;  
         return;  
4824        } elsif ($token->{tag_name} eq 'table') {        } elsif ($token->{tag_name} eq 'table') {
4825          ## has a p element in scope          ## has a p element in scope
4826          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4827            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4828              !!!back-token;              !!!back-token;
4829              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4830              return;              redo B;
4831            } elsif ({            } elsif ({
4832                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4833                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2846  sub _tree_construction_main ($) { Line 4838  sub _tree_construction_main ($) {
4838                        
4839          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4840                        
4841          $self->{insertion_mode} = 'in table';          $self->{insertion_mode} = IN_TABLE_IM;
4842                        
4843          !!!next-token;          !!!next-token;
4844          return;          redo B;
4845        } elsif ({        } elsif ({
4846                  area => 1, basefont => 1, bgsound => 1, br => 1,                  area => 1, basefont => 1, bgsound => 1, br => 1,
4847                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
# Line 2859  sub _tree_construction_main ($) { Line 4851  sub _tree_construction_main ($) {
4851            !!!parse-error (type => 'image');            !!!parse-error (type => 'image');
4852            $token->{tag_name} = 'img';            $token->{tag_name} = 'img';
4853          }          }
4854            
4855            ## NOTE: There is an "as if <br>" code clone.
4856          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
4857                    
4858          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4859          pop @{$self->{open_elements}};          pop @{$self->{open_elements}};
4860                    
4861          !!!next-token;          !!!next-token;
4862          return;          redo B;
4863        } elsif ($token->{tag_name} eq 'hr') {        } elsif ($token->{tag_name} eq 'hr') {
4864          ## has a p element in scope          ## has a p element in scope
4865          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4866            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4867              !!!back-token;              !!!back-token;
4868              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4869              return;              redo B;
4870            } elsif ({            } elsif ({
4871                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4872                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2886  sub _tree_construction_main ($) { Line 4879  sub _tree_construction_main ($) {
4879          pop @{$self->{open_elements}};          pop @{$self->{open_elements}};
4880                        
4881          !!!next-token;          !!!next-token;
4882          return;          redo B;
4883        } elsif ($token->{tag_name} eq 'input') {        } elsif ($token->{tag_name} eq 'input') {
4884          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
4885                    
# Line 2895  sub _tree_construction_main ($) { Line 4888  sub _tree_construction_main ($) {
4888          pop @{$self->{open_elements}};          pop @{$self->{open_elements}};
4889                    
4890          !!!next-token;          !!!next-token;
4891          return;          redo B;
4892        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
4893          !!!parse-error (type => 'isindex');          !!!parse-error (type => 'isindex');
4894                    
4895          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
4896            ## Ignore the token            ## Ignore the token
4897            !!!next-token;            !!!next-token;
4898            return;            redo B;
4899          } else {          } else {
4900            my $at = $token->{attributes};            my $at = $token->{attributes};
4901            my $form_attrs;            my $form_attrs;
# Line 2912  sub _tree_construction_main ($) { Line 4905  sub _tree_construction_main ($) {
4905            delete $at->{action};            delete $at->{action};
4906            delete $at->{prompt};            delete $at->{prompt};
4907            my @tokens = (            my @tokens = (
4908                          {type => 'start tag', tag_name => 'form',                          {type => START_TAG_TOKEN, tag_name => 'form',
4909                           attributes => $form_attrs},                           attributes => $form_attrs},
4910                          {type => 'start tag', tag_name => 'hr'},                          {type => START_TAG_TOKEN, tag_name => 'hr'},
4911                          {type => 'start tag', tag_name => 'p'},                          {type => START_TAG_TOKEN, tag_name => 'p'},
4912                          {type => 'start tag', tag_name => 'label'},                          {type => START_TAG_TOKEN, tag_name => 'label'},
4913                         );                         );
4914            if ($prompt_attr) {            if ($prompt_attr) {
4915              push @tokens, {type => 'character', data => $prompt_attr->{value}};              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};
4916            } else {            } else {
4917              push @tokens, {type => 'character',              push @tokens, {type => CHARACTER_TOKEN,
4918                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD
4919              ## TODO: make this configurable              ## TODO: make this configurable
4920            }            }
4921            push @tokens,            push @tokens,
4922                          {type => 'start tag', tag_name => 'input', attributes => $at},                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},
4923                          #{type => 'character', data => ''}, # SHOULD                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
4924                          {type => 'end tag', tag_name => 'label'},                          {type => END_TAG_TOKEN, tag_name => 'label'},
4925                          {type => 'end tag', tag_name => 'p'},                          {type => END_TAG_TOKEN, tag_name => 'p'},
4926                          {type => 'start tag', tag_name => 'hr'},                          {type => START_TAG_TOKEN, tag_name => 'hr'},
4927                          {type => 'end tag', tag_name => 'form'};                          {type => END_TAG_TOKEN, tag_name => 'form'};
4928            $token = shift @tokens;            $token = shift @tokens;
4929            !!!back-token (@tokens);            !!!back-token (@tokens);
4930            return;            redo B;
4931          }          }
4932        } elsif ({        } elsif ($token->{tag_name} eq 'textarea') {
                 textarea => 1,  
                 iframe => 1,  
                 noembed => 1,  
                 noframes => 1,  
                 noscript => 0, ## TODO: 1 if scripting is enabled  
                }->{$token->{tag_name}}) {  
4933          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
4934          my $el;          my $el;
4935          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $token->{tag_name}, $token->{attributes});
4936                    
4937          if ($token->{tag_name} eq 'textarea') {          ## TODO: $self->{form_element} if defined
4938            ## TODO: $self->{form_element} if defined          $self->{content_model} = RCDATA_CONTENT_MODEL;
           $self->{content_model_flag} = 'RCDATA';  
         } else {  
           $self->{content_model_flag} = 'CDATA';  
         }  
4939          delete $self->{escape}; # MUST          delete $self->{escape}; # MUST
4940                    
4941          $insert->($el);          $insert->($el);
4942                    
4943          my $text = '';          my $text = '';
4944          if ($token->{tag_name} eq 'textarea') {          !!!next-token;
4945            !!!next-token;          if ($token->{type} == CHARACTER_TOKEN) {
4946            if ($token->{type} eq 'character') {            $token->{data} =~ s/^\x0A//;
4947              $token->{data} =~ s/^\x0A//;            unless (length $token->{data}) {
4948              unless (length $token->{data}) {              !!!next-token;
               !!!next-token;  
             }  
4949            }            }
         } else {  
           !!!next-token;  
4950          }          }
4951          while ($token->{type} eq 'character') {          while ($token->{type} == CHARACTER_TOKEN) {
4952            $text .= $token->{data};            $text .= $token->{data};
4953            !!!next-token;            !!!next-token;
4954          }          }
# Line 2977  sub _tree_construction_main ($) { Line 4956  sub _tree_construction_main ($) {
4956            $el->manakai_append_text ($text);            $el->manakai_append_text ($text);
4957          }          }
4958                    
4959          $self->{content_model_flag} = 'PCDATA';          $self->{content_model} = PCDATA_CONTENT_MODEL;
4960                    
4961          if ($token->{type} eq 'end tag' and          if ($token->{type} == END_TAG_TOKEN and
4962              $token->{tag_name} eq $tag_name) {              $token->{tag_name} eq $tag_name) {
4963            ## Ignore the token            ## Ignore the token
4964          } else {          } else {
4965            if ($token->{tag_name} eq 'textarea') {            !!!parse-error (type => 'in RCDATA:#'.$token->{type});
             !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
           } else {  
             !!!parse-error (type => 'in CDATA:#'.$token->{type});  
           }  
           ## ISSUE: And ignore?  
4966          }          }
4967          !!!next-token;          !!!next-token;
4968          return;          redo B;
4969          } elsif ({
4970                    iframe => 1,
4971                    noembed => 1,
4972                    noframes => 1,
4973                    noscript => 0, ## TODO: 1 if scripting is enabled
4974                   }->{$token->{tag_name}}) {
4975            ## NOTE: There is an "as if in body" code clone.
4976            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4977            redo B;
4978        } elsif ($token->{tag_name} eq 'select') {        } elsif ($token->{tag_name} eq 'select') {
4979          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
4980                    
4981          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4982                    
4983          $self->{insertion_mode} = 'in select';          $self->{insertion_mode} = IN_SELECT_IM;
4984          !!!next-token;          !!!next-token;
4985          return;          redo B;
4986        } elsif ({        } elsif ({
4987                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
4988                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 3009  sub _tree_construction_main ($) { Line 4992  sub _tree_construction_main ($) {
4992          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!parse-error (type => 'in body:'.$token->{tag_name});
4993          ## Ignore the token          ## Ignore the token
4994          !!!next-token;          !!!next-token;
4995          return;          redo B;
4996                    
4997          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
4998        } else {        } else {
# Line 3018  sub _tree_construction_main ($) { Line 5001  sub _tree_construction_main ($) {
5001          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5002                    
5003          !!!next-token;          !!!next-token;
5004          return;          redo B;
5005        }        }
5006      } elsif ($token->{type} eq 'end tag') {      } elsif ($token->{type} == END_TAG_TOKEN) {
5007        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
5008          if (@{$self->{open_elements}} > 1 and          if (@{$self->{open_elements}} > 1 and
5009              $self->{open_elements}->[1]->[1] eq 'body') {              $self->{open_elements}->[1]->[1] eq 'body') {
# Line 3028  sub _tree_construction_main ($) { Line 5011  sub _tree_construction_main ($) {
5011              unless ({              unless ({
5012                         dd => 1, dt => 1, li => 1, p => 1, td => 1,                         dd => 1, dt => 1, li => 1, p => 1, td => 1,
5013                         th => 1, tr => 1, body => 1, html => 1,                         th => 1, tr => 1, body => 1, html => 1,
5014                         tbody => 1, tfoot => 1, thead => 1,
5015                      }->{$_->[1]}) {                      }->{$_->[1]}) {
5016                !!!parse-error (type => 'not closed:'.$_->[1]);                !!!parse-error (type => 'not closed:'.$_->[1]);
5017              }              }
5018            }            }
5019    
5020            $self->{insertion_mode} = 'after body';            $self->{insertion_mode} = AFTER_BODY_IM;
5021            !!!next-token;            !!!next-token;
5022            return;            redo B;
5023          } else {          } else {
5024            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5025            ## Ignore the token            ## Ignore the token
5026            !!!next-token;            !!!next-token;
5027            return;            redo B;
5028          }          }
5029        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
5030          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 3048  sub _tree_construction_main ($) { Line 5032  sub _tree_construction_main ($) {
5032            if ($self->{open_elements}->[-1]->[1] ne 'body') {            if ($self->{open_elements}->[-1]->[1] ne 'body') {
5033              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);
5034            }            }
5035            $self->{insertion_mode} = 'after body';            $self->{insertion_mode} = AFTER_BODY_IM;
5036            ## reprocess            ## reprocess
5037            return;            redo B;
5038          } else {          } else {
5039            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5040            ## Ignore the token            ## Ignore the token
5041            !!!next-token;            !!!next-token;
5042            return;            redo B;
5043          }          }
5044        } elsif ({        } elsif ({
5045                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
# Line 3077  sub _tree_construction_main ($) { Line 5061  sub _tree_construction_main ($) {
5061                   li => ($token->{tag_name} ne 'li'),                   li => ($token->{tag_name} ne 'li'),
5062                   p => ($token->{tag_name} ne 'p'),                   p => ($token->{tag_name} ne 'p'),
5063                   td => 1, th => 1, tr => 1,                   td => 1, th => 1, tr => 1,
5064                     tbody => 1, tfoot=> 1, thead => 1,
5065                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
5066                !!!back-token;                !!!back-token;
5067                $token = {type => 'end tag',                $token = {type => END_TAG_TOKEN,
5068                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5069                return;                redo B;
5070              }              }
5071              $i = $_;              $i = $_;
5072              last INSCOPE unless $token->{tag_name} eq 'p';              last INSCOPE unless $token->{tag_name} eq 'p';
# Line 3094  sub _tree_construction_main ($) { Line 5079  sub _tree_construction_main ($) {
5079          } # INSCOPE          } # INSCOPE
5080                    
5081          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
5082            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            if (defined $i) {
5083                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
5084              } else {
5085                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5086              }
5087          }          }
5088                    
5089          splice @{$self->{open_elements}}, $i if defined $i;          if (defined $i) {
5090              splice @{$self->{open_elements}}, $i;
5091            } elsif ($token->{tag_name} eq 'p') {
5092              ## As if <p>, then reprocess the current token
5093              my $el;
5094              !!!create-element ($el, 'p');
5095              $insert->($el);
5096            }
5097          $clear_up_to_marker->()          $clear_up_to_marker->()
5098            if {            if {
5099              button => 1, marquee => 1, object => 1,              button => 1, marquee => 1, object => 1,
5100            }->{$token->{tag_name}};            }->{$token->{tag_name}};
5101          !!!next-token;          !!!next-token;
5102          return;          redo B;
5103        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
5104          ## has an element in scope          ## has an element in scope
5105          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 3113  sub _tree_construction_main ($) { Line 5109  sub _tree_construction_main ($) {
5109              if ({              if ({
5110                   dd => 1, dt => 1, li => 1, p => 1,                   dd => 1, dt => 1, li => 1, p => 1,
5111                   td => 1, th => 1, tr => 1,                   td => 1, th => 1, tr => 1,
5112                     tbody => 1, tfoot=> 1, thead => 1,
5113                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
5114                !!!back-token;                !!!back-token;
5115                $token = {type => 'end tag',                $token = {type => END_TAG_TOKEN,
5116                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5117                return;                redo B;
5118              }              }
5119              last INSCOPE;              last INSCOPE;
5120            } elsif ({            } elsif ({
# Line 3131  sub _tree_construction_main ($) { Line 5128  sub _tree_construction_main ($) {
5128          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {
5129            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
5130          } else {          } else {
5131            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5132          }          }
5133    
5134          undef $self->{form_element};          undef $self->{form_element};
5135          !!!next-token;          !!!next-token;
5136          return;          redo B;
5137        } elsif ({        } elsif ({
5138                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5139                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 3151  sub _tree_construction_main ($) { Line 5148  sub _tree_construction_main ($) {
5148              if ({              if ({
5149                   dd => 1, dt => 1, li => 1, p => 1,                   dd => 1, dt => 1, li => 1, p => 1,
5150                   td => 1, th => 1, tr => 1,                   td => 1, th => 1, tr => 1,
5151                     tbody => 1, tfoot=> 1, thead => 1,
5152                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
5153                !!!back-token;                !!!back-token;
5154                $token = {type => 'end tag',                $token = {type => END_TAG_TOKEN,
5155                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5156                return;                redo B;
5157              }              }
5158              $i = $_;              $i = $_;
5159              last INSCOPE;              last INSCOPE;
# Line 3168  sub _tree_construction_main ($) { Line 5166  sub _tree_construction_main ($) {
5166          } # INSCOPE          } # INSCOPE
5167                    
5168          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
5169            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5170          }          }
5171                    
5172          splice @{$self->{open_elements}}, $i if defined $i;          splice @{$self->{open_elements}}, $i if defined $i;
5173          !!!next-token;          !!!next-token;
5174          return;          redo B;
5175        } elsif ({        } elsif ({
5176                  a => 1,                  a => 1,
5177                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
# Line 3181  sub _tree_construction_main ($) { Line 5179  sub _tree_construction_main ($) {
5179                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
5180                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5181          $formatting_end_tag->($token->{tag_name});          $formatting_end_tag->($token->{tag_name});
5182  ## TODO: <http://html5.org/tools/web-apps-tracker?from=883&to=884>          redo B;
5183          return;        } elsif ($token->{tag_name} eq 'br') {
5184            !!!parse-error (type => 'unmatched end tag:br');
5185    
5186            ## As if <br>
5187            $reconstruct_active_formatting_elements->($insert_to_current);
5188            
5189            my $el;
5190            !!!create-element ($el, 'br');
5191            $insert->($el);
5192            
5193            ## Ignore the token.
5194            !!!next-token;
5195            redo B;
5196        } elsif ({        } elsif ({
5197                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
5198                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
5199                  tbody => 1, td => 1, tfoot => 1, th => 1,                  tbody => 1, td => 1, tfoot => 1, th => 1,
5200                  thead => 1, tr => 1,                  thead => 1, tr => 1,
5201                  area => 1, basefont => 1, bgsound => 1, br => 1,                  area => 1, basefont => 1, bgsound => 1,
5202                  embed => 1, hr => 1, iframe => 1, image => 1,                  embed => 1, hr => 1, iframe => 1, image => 1,
5203                  img => 1, input => 1, isindex => 1, noembed => 1,                  img => 1, input => 1, isindex => 1, noembed => 1,
5204                  noframes => 1, param => 1, select => 1, spacer => 1,                  noframes => 1, param => 1, select => 1, spacer => 1,
# Line 3198  sub _tree_construction_main ($) { Line 5208  sub _tree_construction_main ($) {
5208          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5209          ## Ignore the token          ## Ignore the token
5210          !!!next-token;          !!!next-token;
5211          return;          redo B;
5212                    
5213          ## ISSUE: Issue on HTML5 new elements in spec          ## ISSUE: Issue on HTML5 new elements in spec
5214                    
# Line 3215  sub _tree_construction_main ($) { Line 5225  sub _tree_construction_main ($) {
5225              if ({              if ({
5226                   dd => 1, dt => 1, li => 1, p => 1,                   dd => 1, dt => 1, li => 1, p => 1,
5227                   td => 1, th => 1, tr => 1,                   td => 1, th => 1, tr => 1,
5228                     tbody => 1, tfoot => 1, thead => 1,
5229                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
5230                !!!back-token;                !!!back-token;
5231                $token = {type => 'end tag',                $token = {type => END_TAG_TOKEN,
5232                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5233                return;                redo B;
5234              }              }
5235                    
5236              ## Step 2              ## Step 2
5237              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {
5238                  ## NOTE: <x><y></x>
5239                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
5240              }              }
5241                            
# Line 3238  sub _tree_construction_main ($) { Line 5250  sub _tree_construction_main ($) {
5250                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
5251                  ($special_category->{$node->[1]} or                  ($special_category->{$node->[1]} or
5252                   $scoping_category->{$node->[1]})) {                   $scoping_category->{$node->[1]})) {
5253                !!!parse-error (type => 'not closed:'.$node->[1]);                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5254                ## Ignore the token                ## Ignore the token
5255                !!!next-token;                !!!next-token;
5256                last S2;                last S2;
# Line 3252  sub _tree_construction_main ($) { Line 5264  sub _tree_construction_main ($) {
5264            ## Step 5;            ## Step 5;
5265            redo S2;            redo S2;
5266          } # S2          } # S2
5267          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') {  
           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 'title') {  
               ## NOTE: There is an "as if in head" code clone  
               my $title_el;  
               !!!create-element ($title_el, 'title', $token->{attributes});  
               (defined $self->{head_element} ? $self->{head_element} : $self->{open_elements}->[-1]->[0])  
                 ->append_child ($title_el);  
               $self->{content_model_flag} = 'RCDATA';  
               delete $self->{escape}; # MUST  
   
               my $text = '';  
               !!!next-token;  
               while ($token->{type} eq 'character') {  
                 $text .= $token->{data};  
                 !!!next-token;  
               }  
               if (length $text) {  
                 $title_el->manakai_append_text ($text);  
               }  
                 
               $self->{content_model_flag} = 'PCDATA';  
                 
               if ($token->{type} eq 'end tag' and  
                   $token->{tag_name} eq 'title') {  
                 ## Ignore the token  
               } else {  
                 !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
                 ## ISSUE: And ignore?  
               }  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'style') {  
               $style_start_tag->();  
               redo B;  
             } elsif ($token->{tag_name} eq 'script') {  
               $script_start_tag->();  
               redo B;  
             } elsif ({base => 1, link => 1, meta => 1}->{$token->{tag_name}}) {  
               ## NOTE: There are "as if in head" code clones  
               my $el;  
               !!!create-element ($el, $token->{tag_name}, $token->{attributes});  
               if ($self->{insertion_mode} eq 'in head' and  
                   defined $self->{head_element}) {  
                 $self->{head_element}->append_child ($el);  
               } else {  
                 $self->{open_elements}->[-1]->[0]->append_child ($el);  
               }  
   
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'head') {  
               !!!parse-error (type => 'in head:head');  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'head') {  
               if ($self->{open_elements}->[-1]->[1] eq 'head') {  
                 pop @{$self->{open_elements}};  
               } else {  
                 !!!parse-error (type => 'unmatched end tag:head');  
               }  
               $self->{insertion_mode} = 'after head';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'body' or  
                      $token->{tag_name} eq 'html') {  
               #  
             } else {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             }  
           } else {  
             #  
           }  
   
           if ($self->{open_elements}->[-1]->[1] eq 'head') {  
             ## As if </head>  
             pop @{$self->{open_elements}};  
           }  
           $self->{insertion_mode} = 'after head';  
           ## reprocess  
           redo B;  
   
           ## ISSUE: An issue in the spec.  
         } elsif ($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 ($token->{tag_name} eq 'body') {  
               !!!insert-element ('body', $token->{attributes});  
               $self->{insertion_mode} = 'in body';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'frameset') {  
               !!!insert-element ('frameset', $token->{attributes});  
               $self->{insertion_mode} = 'in frameset';  
               !!!next-token;  
               redo B;  
             } elsif ({  
                       base => 1, link => 1, meta => 1,  
                       script => 1, style => 1, title => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'after head:'.$token->{tag_name});  
               $self->{insertion_mode} = 'in head';  
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
             
           ## As if <body>  
           !!!insert-element ('body');  
           $self->{insertion_mode} = 'in body';  
           ## reprocess  
           redo B;  
         } 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";  
5268        }        }
5269      }      }
5270        redo B;
5271    } # B    } # B
5272    
5273      ## NOTE: The "trailing end" phase in HTML5 is split into
5274      ## two insertion modes: "after html body" and "after html frameset".
5275      ## NOTE: States in the main stage is preserved while
5276      ## the parser stays in the trailing end phase. # MUST
5277    
5278    ## Stop parsing # MUST    ## Stop parsing # MUST
5279        
5280    ## TODO: script stuffs    ## TODO: script stuffs
# Line 5137  sub set_inner_html ($$$) { Line 5286  sub set_inner_html ($$$) {
5286    my $s = \$_[0];    my $s = \$_[0];
5287    my $onerror = $_[1];    my $onerror = $_[1];
5288    
5289      ## ISSUE: Should {confident} be true?
5290    
5291    my $nt = $node->node_type;    my $nt = $node->node_type;
5292    if ($nt == 9) {    if ($nt == 9) {
5293      # MUST      # MUST
# Line 5210  sub set_inner_html ($$$) { Line 5361  sub set_inner_html ($$$) {
5361    
5362      ## Step 2      ## Step 2
5363      my $node_ln = $node->local_name;      my $node_ln = $node->local_name;
5364      $p->{content_model_flag} = {      $p->{content_model} = {
5365        title => 'RCDATA',        title => RCDATA_CONTENT_MODEL,
5366        textarea => 'RCDATA',        textarea => RCDATA_CONTENT_MODEL,
5367        style => 'CDATA',        style => CDATA_CONTENT_MODEL,
5368        script => 'CDATA',        script => CDATA_CONTENT_MODEL,
5369        xmp => 'CDATA',        xmp => CDATA_CONTENT_MODEL,
5370        iframe => 'CDATA',        iframe => CDATA_CONTENT_MODEL,
5371        noembed => 'CDATA',        noembed => CDATA_CONTENT_MODEL,
5372        noframes => 'CDATA',        noframes => CDATA_CONTENT_MODEL,
5373        noscript => 'CDATA',        noscript => CDATA_CONTENT_MODEL,
5374        plaintext => 'PLAINTEXT',        plaintext => PLAINTEXT_CONTENT_MODEL,
5375      }->{$node_ln} || 'PCDATA';      }->{$node_ln};
5376         ## ISSUE: What is "the name of the element"? local name?      $p->{content_model} = PCDATA_CONTENT_MODEL
5377            unless defined $p->{content_model};
5378            ## ISSUE: What is "the name of the element"? local name?
5379    
5380      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $node_ln];
5381    
# Line 5287  sub set_inner_html ($$$) { Line 5440  sub set_inner_html ($$$) {
5440    
5441  } # tree construction stage  } # tree construction stage
5442    
5443  sub get_inner_html ($$$) {  package Whatpm::HTML::RestartParser;
5444    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  
5445    
5446  1;  1;
5447  # $Date$  # $Date$

Legend:
Removed from v.1.24  
changed lines
  Added in v.1.63

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24