/[suikacvs]/markup/html/whatpm/Whatpm/HTML.pm.src
Suika

Diff of /markup/html/whatpm/Whatpm/HTML.pm.src

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.35 by wakaba, Mon Jul 16 03:21:04 2007 UTC revision 1.79 by wakaba, Mon Mar 3 13:15:54 2008 UTC
# Line 1  Line 1 
1  package Whatpm::HTML;  package Whatpm::HTML;
2  use strict;  use strict;
3  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
4    use Error qw(:try);
5    
6  ## ISSUE:  ## ISSUE:
7  ## var doc = implementation.createDocument (null, null, null);  ## var doc = implementation.createDocument (null, null, null);
8  ## doc.write ('');  ## doc.write ('');
9  ## alert (doc.compatMode);  ## alert (doc.compatMode);
10    
11  ## ISSUE: HTML5 revision 967 says that the encoding layer MUST NOT  ## TODO: Control charcters and noncharacters are not allowed (HTML5 revision 1263)
12  ## strip BOM and the HTML layer MUST ignore it.  Whether we can do it  ## TODO: 1252 parse error (revision 1264)
13  ## is not yet clear.  ## TODO: 8859-11 = 874 (revision 1271)
 ## "{U+FEFF}..." in UTF-16BE/UTF-16LE is three or four characters?  
 ## "{U+FEFF}..." in GB18030?  
14    
15  my $permitted_slash_tag_name = {  my $permitted_slash_tag_name = {
16    base => 1,    base => 1,
# Line 19  my $permitted_slash_tag_name = { Line 18  my $permitted_slash_tag_name = {
18    meta => 1,    meta => 1,
19    hr => 1,    hr => 1,
20    br => 1,    br => 1,
21    img=> 1,    img => 1,
22    embed => 1,    embed => 1,
23    param => 1,    param => 1,
24    area => 1,    area => 1,
# Line 84  my $formatting_category = { Line 83  my $formatting_category = {
83  };  };
84  # $phrasing_category: all other elements  # $phrasing_category: all other elements
85    
86    sub parse_byte_string ($$$$;$) {
87      my $self = ref $_[0] ? shift : shift->new;
88      my $charset = shift;
89      my $bytes_s = ref $_[0] ? $_[0] : \($_[0]);
90      my $s;
91      
92      if (defined $charset) {
93        require Encode; ## TODO: decode(utf8) don't delete BOM
94        $s = \ (Encode::decode ($charset, $$bytes_s));
95        $self->{input_encoding} = lc $charset; ## TODO: normalize name
96        $self->{confident} = 1;
97      } else {
98        ## TODO: Implement HTML5 detection algorithm
99        require Whatpm::Charset::UniversalCharDet;
100        $charset = Whatpm::Charset::UniversalCharDet->detect_byte_string
101            (substr ($$bytes_s, 0, 1024));
102        $charset ||= 'windows-1252';
103        $s = \ (Encode::decode ($charset, $$bytes_s));
104        $self->{input_encoding} = $charset;
105        $self->{confident} = 0;
106      }
107    
108      $self->{change_encoding} = sub {
109        my $self = shift;
110        my $charset = lc shift;
111        ## TODO: if $charset is supported
112        ## TODO: normalize charset name
113    
114        ## "Change the encoding" algorithm:
115    
116        ## Step 1    
117        if ($charset eq 'utf-16') { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?
118          $charset = 'utf-8';
119        }
120    
121        ## Step 2
122        if (defined $self->{input_encoding} and
123            $self->{input_encoding} eq $charset) {
124          $self->{confident} = 1;
125          return;
126        }
127    
128        !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.
129            ':'.$charset, level => 'w');
130    
131        ## Step 3
132        # if (can) {
133          ## change the encoding on the fly.
134          #$self->{confident} = 1;
135          #return;
136        # }
137    
138        ## Step 4
139        throw Whatpm::HTML::RestartParser (charset => $charset);
140      }; # $self->{change_encoding}
141    
142      my @args = @_; shift @args; # $s
143      my $return;
144      try {
145        $return = $self->parse_char_string ($s, @args);  
146      } catch Whatpm::HTML::RestartParser with {
147        my $charset = shift->{charset};
148        $s = \ (Encode::decode ($charset, $$bytes_s));    
149        $self->{input_encoding} = $charset; ## TODO: normalize
150        $self->{confident} = 1;
151        $return = $self->parse_char_string ($s, @args);
152      };
153      return $return;
154    } # parse_byte_string
155    
156    ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
157    ## and the HTML layer MUST ignore it.  However, we does strip BOM in
158    ## the encoding layer and the HTML layer does not ignore any U+FEFF,
159    ## because the core part of our HTML parser expects a string of character,
160    ## not a string of bytes or code units or anything which might contain a BOM.
161    ## Therefore, any parser interface that accepts a string of bytes,
162    ## such as |parse_byte_string| in this module, must ensure that it does
163    ## strip the BOM and never strip any ZWNBSP.
164    
165    *parse_char_string = \&parse_string;
166    
167  sub parse_string ($$$;$) {  sub parse_string ($$$;$) {
168    my $self = shift->new;    my $self = ref $_[0] ? shift : shift->new;
169    my $s = \$_[0];    my $s = ref $_[0] ? $_[0] : \($_[0]);
170    $self->{document} = $_[1];    $self->{document} = $_[1];
171      @{$self->{document}->child_nodes} = ();
172    
173    ## NOTE: |set_inner_html| copies most of this method's code    ## NOTE: |set_inner_html| copies most of this method's code
174    
175      $self->{confident} = 1 unless exists $self->{confident};
176      $self->{document}->input_encoding ($self->{input_encoding})
177          if defined $self->{input_encoding};
178    
179    my $i = 0;    my $i = 0;
180    my $line = 1;    my $line = 1;
181    my $column = 0;    my $column = 0;
182    $self->{set_next_input_character} = sub {    $self->{set_next_char} = sub {
183      my $self = shift;      my $self = shift;
184    
185      pop @{$self->{prev_input_character}};      pop @{$self->{prev_char}};
186      unshift @{$self->{prev_input_character}}, $self->{next_input_character};      unshift @{$self->{prev_char}}, $self->{next_char};
187    
188      $self->{next_input_character} = -1 and return if $i >= length $$s;      $self->{next_char} = -1 and return if $i >= length $$s;
189      $self->{next_input_character} = ord substr $$s, $i++, 1;      $self->{next_char} = ord substr $$s, $i++, 1;
190      $column++;      $column++;
191            
192      if ($self->{next_input_character} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
193        $line++;        $line++;
194        $column = 0;        $column = 0;
195      } elsif ($self->{next_input_character} == 0x000D) { # CR      } elsif ($self->{next_char} == 0x000D) { # CR
196        $i++ if substr ($$s, $i, 1) eq "\x0A";        $i++ if substr ($$s, $i, 1) eq "\x0A";
197        $self->{next_input_character} = 0x000A; # LF # MUST        $self->{next_char} = 0x000A; # LF # MUST
198        $line++;        $line++;
199        $column = 0;        $column = 0;
200      } elsif ($self->{next_input_character} > 0x10FFFF) {      } elsif ($self->{next_char} > 0x10FFFF) {
201        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
202      } elsif ($self->{next_input_character} == 0x0000) { # NULL      } elsif ($self->{next_char} == 0x0000) { # NULL
203        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
204        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
205      }      }
206    };    };
207    $self->{prev_input_character} = [-1, -1, -1];    $self->{prev_char} = [-1, -1, -1];
208    $self->{next_input_character} = -1;    $self->{next_char} = -1;
209    
210    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
211      my (%opt) = @_;      my (%opt) = @_;
# Line 141  sub parse_string ($$$;$) { Line 226  sub parse_string ($$$;$) {
226  sub new ($) {  sub new ($) {
227    my $class = shift;    my $class = shift;
228    my $self = bless {}, $class;    my $self = bless {}, $class;
229    $self->{set_next_input_character} = sub {    $self->{set_next_char} = sub {
230      $self->{next_input_character} = -1;      $self->{next_char} = -1;
231    };    };
232    $self->{parse_error} = sub {    $self->{parse_error} = sub {
233      #      #
234    };    };
235      $self->{change_encoding} = sub {
236        # if ($_[0] is a supported encoding) {
237        #   run "change the encoding" algorithm;
238        #   throw Whatpm::HTML::RestartParser (charset => $new_encoding);
239        # }
240      };
241      $self->{application_cache_selection} = sub {
242        #
243      };
244    return $self;    return $self;
245  } # new  } # new
246    
247    sub CM_ENTITY () { 0b001 } # & markup in data
248    sub CM_LIMITED_MARKUP () { 0b010 } # < markup in data (limited)
249    sub CM_FULL_MARKUP () { 0b100 } # < markup in data (any)
250    
251    sub PLAINTEXT_CONTENT_MODEL () { 0 }
252    sub CDATA_CONTENT_MODEL () { CM_LIMITED_MARKUP }
253    sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }
254    sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
255    
256    sub DATA_STATE () { 0 }
257    sub ENTITY_DATA_STATE () { 1 }
258    sub TAG_OPEN_STATE () { 2 }
259    sub CLOSE_TAG_OPEN_STATE () { 3 }
260    sub TAG_NAME_STATE () { 4 }
261    sub BEFORE_ATTRIBUTE_NAME_STATE () { 5 }
262    sub ATTRIBUTE_NAME_STATE () { 6 }
263    sub AFTER_ATTRIBUTE_NAME_STATE () { 7 }
264    sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 }
265    sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
266    sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
267    sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
268    sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
269    sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
270    sub COMMENT_START_STATE () { 14 }
271    sub COMMENT_START_DASH_STATE () { 15 }
272    sub COMMENT_STATE () { 16 }
273    sub COMMENT_END_STATE () { 17 }
274    sub COMMENT_END_DASH_STATE () { 18 }
275    sub BOGUS_COMMENT_STATE () { 19 }
276    sub DOCTYPE_STATE () { 20 }
277    sub BEFORE_DOCTYPE_NAME_STATE () { 21 }
278    sub DOCTYPE_NAME_STATE () { 22 }
279    sub AFTER_DOCTYPE_NAME_STATE () { 23 }
280    sub BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 24 }
281    sub DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE () { 25 }
282    sub DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE () { 26 }
283    sub AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 27 }
284    sub BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 28 }
285    sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE () { 29 }
286    sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }
287    sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
288    sub BOGUS_DOCTYPE_STATE () { 32 }
289    sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
290    
291    sub DOCTYPE_TOKEN () { 1 }
292    sub COMMENT_TOKEN () { 2 }
293    sub START_TAG_TOKEN () { 3 }
294    sub END_TAG_TOKEN () { 4 }
295    sub END_OF_FILE_TOKEN () { 5 }
296    sub CHARACTER_TOKEN () { 6 }
297    
298    sub AFTER_HTML_IMS () { 0b100 }
299    sub HEAD_IMS ()       { 0b1000 }
300    sub BODY_IMS ()       { 0b10000 }
301    sub BODY_TABLE_IMS () { 0b100000 }
302    sub TABLE_IMS ()      { 0b1000000 }
303    sub ROW_IMS ()        { 0b10000000 }
304    sub BODY_AFTER_IMS () { 0b100000000 }
305    sub FRAME_IMS ()      { 0b1000000000 }
306    
307    sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
308    sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
309    sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
310    sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
311    sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
312    sub BEFORE_HEAD_IM () { HEAD_IMS | 0b11 }
313    sub IN_BODY_IM () { BODY_IMS }
314    sub IN_CELL_IM () { BODY_IMS | BODY_TABLE_IMS | 0b01 }
315    sub IN_CAPTION_IM () { BODY_IMS | BODY_TABLE_IMS | 0b10 }
316    sub IN_ROW_IM () { TABLE_IMS | ROW_IMS | 0b01 }
317    sub IN_TABLE_BODY_IM () { TABLE_IMS | ROW_IMS | 0b10 }
318    sub IN_TABLE_IM () { TABLE_IMS }
319    sub AFTER_BODY_IM () { BODY_AFTER_IMS }
320    sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
321    sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
322    sub IN_SELECT_IM () { 0b01 }
323    sub IN_COLUMN_GROUP_IM () { 0b10 }
324    
325  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
326    
327  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
328    my $self = shift;    my $self = shift;
329    $self->{state} = 'data'; # MUST    $self->{state} = DATA_STATE; # MUST
330    $self->{content_model_flag} = 'PCDATA'; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
331    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE
332    undef $self->{current_attribute};    undef $self->{current_attribute};
333    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
334    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
335    $self->{char} = [];    $self->{char} = [];
336    # $self->{next_input_character}    # $self->{next_char}
337    !!!next-input-character;    !!!next-input-character;
338    $self->{token} = [];    $self->{token} = [];
339    # $self->{escape}    # $self->{escape}
340  } # _initialize_tokenizer  } # _initialize_tokenizer
341    
342  ## A token has:  ## A token has:
343  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',  ##   ->{type} == DOCTYPE_TOKEN, START_TAG_TOKEN, END_TAG_TOKEN, COMMENT_TOKEN,
344  ##       'character', or 'end-of-file'  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN
345  ##   ->{name} (DOCTYPE, start tag (tag name), end tag (tag name))  ##   ->{name} (DOCTYPE_TOKEN)
346  ##   ->{public_identifier} (DOCTYPE)  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
347  ##   ->{system_identifier} (DOCTYPE)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
348  ##   ->{correct} == 1 or 0 (DOCTYPE)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
349  ##   ->{attributes} isa HASH (start tag, end tag)  ##   ->{quirks} == 1 or 0 (DOCTYPE_TOKEN): "force-quirks" flag
350  ##   ->{data} (comment, character)  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
351    ##        ->{name}
352    ##        ->{value}
353    ##        ->{has_reference} == 1 or 0
354    ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
355    
356  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
357    
# Line 185  sub _initialize_tokenizer ($) { Line 361  sub _initialize_tokenizer ($) {
361  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
362  ## and removed from the list.  ## and removed from the list.
363    
364    ## NOTE: HTML5 "Writing HTML documents" section, applied to
365    ## documents and not to user agents and conformance checkers,
366    ## contains some requirements that are not detected by the
367    ## parsing algorithm:
368    ## - Some requirements on character encoding declarations. ## TODO
369    ## - "Elements MUST NOT contain content that their content model disallows."
370    ##   ... Some are parse error, some are not (will be reported by c.c.).
371    ## - Polytheistic slash SHOULD NOT be used. (Applied only to atheists.) ## TODO
372    ## - Text (in elements, attributes, and comments) SHOULD NOT contain
373    ##   control characters other than space characters. ## TODO: (what is control character? C0, C1 and DEL?  Unicode control character?)
374    
375    ## TODO: HTML5 poses authors two SHOULD-level requirements that cannot
376    ## be detected by the HTML5 parsing algorithm:
377    ## - Text,
378    
379  sub _get_next_token ($) {  sub _get_next_token ($) {
380    my $self = shift;    my $self = shift;
381    if (@{$self->{token}}) {    if (@{$self->{token}}) {
# Line 192  sub _get_next_token ($) { Line 383  sub _get_next_token ($) {
383    }    }
384    
385    A: {    A: {
386      if ($self->{state} eq 'data') {      if ($self->{state} == DATA_STATE) {
387        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_char} == 0x0026) { # &
388          if ($self->{content_model_flag} eq 'PCDATA' or          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
389              $self->{content_model_flag} eq 'RCDATA') {              not $self->{escape}) {
390            $self->{state} = 'entity data';            !!!cp (1);
391              $self->{state} = ENTITY_DATA_STATE;
392            !!!next-input-character;            !!!next-input-character;
393            redo A;            redo A;
394          } else {          } else {
395              !!!cp (2);
396            #            #
397          }          }
398        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
399          if ($self->{content_model_flag} eq 'RCDATA' or          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
             $self->{content_model_flag} eq 'CDATA') {  
400            unless ($self->{escape}) {            unless ($self->{escape}) {
401              if ($self->{prev_input_character}->[0] == 0x002D and # -              if ($self->{prev_char}->[0] == 0x002D and # -
402                  $self->{prev_input_character}->[1] == 0x0021 and # !                  $self->{prev_char}->[1] == 0x0021 and # !
403                  $self->{prev_input_character}->[2] == 0x003C) { # <                  $self->{prev_char}->[2] == 0x003C) { # <
404                  !!!cp (3);
405                $self->{escape} = 1;                $self->{escape} = 1;
406                } else {
407                  !!!cp (4);
408              }              }
409              } else {
410                !!!cp (5);
411            }            }
412          }          }
413                    
414          #          #
415        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_char} == 0x003C) { # <
416          if ($self->{content_model_flag} eq 'PCDATA' or          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
417              (($self->{content_model_flag} eq 'CDATA' or              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
               $self->{content_model_flag} eq 'RCDATA') and  
418               not $self->{escape})) {               not $self->{escape})) {
419            $self->{state} = 'tag open';            !!!cp (6);
420              $self->{state} = TAG_OPEN_STATE;
421            !!!next-input-character;            !!!next-input-character;
422            redo A;            redo A;
423          } else {          } else {
424              !!!cp (7);
425            #            #
426          }          }
427        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
428          if ($self->{escape} and          if ($self->{escape} and
429              ($self->{content_model_flag} eq 'RCDATA' or              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
430               $self->{content_model_flag} eq 'CDATA')) {            if ($self->{prev_char}->[0] == 0x002D and # -
431            if ($self->{prev_input_character}->[0] == 0x002D and # -                $self->{prev_char}->[1] == 0x002D) { # -
432                $self->{prev_input_character}->[1] == 0x002D) { # -              !!!cp (8);
433              delete $self->{escape};              delete $self->{escape};
434              } else {
435                !!!cp (9);
436            }            }
437            } else {
438              !!!cp (10);
439          }          }
440                    
441          #          #
442        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
443          !!!emit ({type => 'end-of-file'});          !!!cp (11);
444            !!!emit ({type => END_OF_FILE_TOKEN});
445          last A; ## TODO: ok?          last A; ## TODO: ok?
446          } else {
447            !!!cp (12);
448        }        }
449        # Anything else        # Anything else
450        my $token = {type => 'character',        my $token = {type => CHARACTER_TOKEN,
451                     data => chr $self->{next_input_character}};                     data => chr $self->{next_char}};
452        ## Stay in the data state        ## Stay in the data state
453        !!!next-input-character;        !!!next-input-character;
454    
455        !!!emit ($token);        !!!emit ($token);
456    
457        redo A;        redo A;
458      } elsif ($self->{state} eq 'entity data') {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
459        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
460                
461        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);
462    
463        $self->{state} = 'data';        $self->{state} = DATA_STATE;
464        # next-input-character is already done        # next-input-character is already done
465    
466        unless (defined $token) {        unless (defined $token) {
467          !!!emit ({type => 'character', data => '&'});          !!!cp (13);
468            !!!emit ({type => CHARACTER_TOKEN, data => '&'});
469        } else {        } else {
470            !!!cp (14);
471          !!!emit ($token);          !!!emit ($token);
472        }        }
473    
474        redo A;        redo A;
475      } elsif ($self->{state} eq 'tag open') {      } elsif ($self->{state} == TAG_OPEN_STATE) {
476        if ($self->{content_model_flag} eq 'RCDATA' or        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
477            $self->{content_model_flag} eq 'CDATA') {          if ($self->{next_char} == 0x002F) { # /
478          if ($self->{next_input_character} == 0x002F) { # /            !!!cp (15);
479            !!!next-input-character;            !!!next-input-character;
480            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
481            redo A;            redo A;
482          } else {          } else {
483              !!!cp (16);
484            ## reconsume            ## reconsume
485            $self->{state} = 'data';            $self->{state} = DATA_STATE;
486    
487            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<'});
488    
489            redo A;            redo A;
490          }          }
491        } elsif ($self->{content_model_flag} eq 'PCDATA') {        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
492          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_char} == 0x0021) { # !
493            $self->{state} = 'markup declaration open';            !!!cp (17);
494              $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
495            !!!next-input-character;            !!!next-input-character;
496            redo A;            redo A;
497          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{next_char} == 0x002F) { # /
498            $self->{state} = 'close tag open';            !!!cp (18);
499              $self->{state} = CLOSE_TAG_OPEN_STATE;
500            !!!next-input-character;            !!!next-input-character;
501            redo A;            redo A;
502          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
503                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{next_char} <= 0x005A) { # A..Z
504              !!!cp (19);
505            $self->{current_token}            $self->{current_token}
506              = {type => 'start tag',              = {type => START_TAG_TOKEN,
507                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020)};
508            $self->{state} = 'tag name';            $self->{state} = TAG_NAME_STATE;
509            !!!next-input-character;            !!!next-input-character;
510            redo A;            redo A;
511          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
512                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
513            $self->{current_token} = {type => 'start tag',            !!!cp (20);
514                              tag_name => chr ($self->{next_input_character})};            $self->{current_token} = {type => START_TAG_TOKEN,
515            $self->{state} = 'tag name';                              tag_name => chr ($self->{next_char})};
516              $self->{state} = TAG_NAME_STATE;
517            !!!next-input-character;            !!!next-input-character;
518            redo A;            redo A;
519          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
520              !!!cp (21);
521            !!!parse-error (type => 'empty start tag');            !!!parse-error (type => 'empty start tag');
522            $self->{state} = 'data';            $self->{state} = DATA_STATE;
523            !!!next-input-character;            !!!next-input-character;
524    
525            !!!emit ({type => 'character', data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});
526    
527            redo A;            redo A;
528          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
529              !!!cp (22);
530            !!!parse-error (type => 'pio');            !!!parse-error (type => 'pio');
531            $self->{state} = 'bogus comment';            $self->{state} = BOGUS_COMMENT_STATE;
532            ## $self->{next_input_character} is intentionally left as is            ## $self->{next_char} is intentionally left as is
533            redo A;            redo A;
534          } else {          } else {
535              !!!cp (23);
536            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago');
537            $self->{state} = 'data';            $self->{state} = DATA_STATE;
538            ## reconsume            ## reconsume
539    
540            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<'});
541    
542            redo A;            redo A;
543          }          }
544        } else {        } else {
545          die "$0: $self->{content_model_flag}: Unknown content model flag";          die "$0: $self->{content_model} in tag open";
546        }        }
547      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
548        if ($self->{content_model_flag} eq 'RCDATA' or        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
           $self->{content_model_flag} eq 'CDATA') {  
549          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
550            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
551            my @next_char;            my @next_char;
552            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++) {
553              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_char};
554              my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);              my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);
555              my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;              my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;
556              if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {              if ($self->{next_char} == $c or $self->{next_char} == $C) {
557                  !!!cp (24);
558                !!!next-input-character;                !!!next-input-character;
559                next TAGNAME;                next TAGNAME;
560              } else {              } else {
561                $self->{next_input_character} = shift @next_char; # reconsume                !!!cp (25);
562                  $self->{next_char} = shift @next_char; # reconsume
563                !!!back-next-input-character (@next_char);                !!!back-next-input-character (@next_char);
564                $self->{state} = 'data';                $self->{state} = DATA_STATE;
565    
566                !!!emit ({type => 'character', data => '</'});                !!!emit ({type => CHARACTER_TOKEN, data => '</'});
567        
568                redo A;                redo A;
569              }              }
570            }            }
571            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_char};
572                
573            unless ($self->{next_input_character} == 0x0009 or # HT            unless ($self->{next_char} == 0x0009 or # HT
574                    $self->{next_input_character} == 0x000A or # LF                    $self->{next_char} == 0x000A or # LF
575                    $self->{next_input_character} == 0x000B or # VT                    $self->{next_char} == 0x000B or # VT
576                    $self->{next_input_character} == 0x000C or # FF                    $self->{next_char} == 0x000C or # FF
577                    $self->{next_input_character} == 0x0020 or # SP                    $self->{next_char} == 0x0020 or # SP
578                    $self->{next_input_character} == 0x003E or # >                    $self->{next_char} == 0x003E or # >
579                    $self->{next_input_character} == 0x002F or # /                    $self->{next_char} == 0x002F or # /
580                    $self->{next_input_character} == -1) {                    $self->{next_char} == -1) {
581              $self->{next_input_character} = shift @next_char; # reconsume              !!!cp (26);
582                $self->{next_char} = shift @next_char; # reconsume
583              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
584              $self->{state} = 'data';              $self->{state} = DATA_STATE;
585              !!!emit ({type => 'character', data => '</'});              !!!emit ({type => CHARACTER_TOKEN, data => '</'});
586              redo A;              redo A;
587            } else {            } else {
588              $self->{next_input_character} = shift @next_char;              !!!cp (27);
589                $self->{next_char} = shift @next_char;
590              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
591              # and consume...              # and consume...
592            }            }
593          } else {          } else {
594            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
595              !!!cp (28);
596            # next-input-character is already done            # next-input-character is already done
597            $self->{state} = 'data';            $self->{state} = DATA_STATE;
598            !!!emit ({type => 'character', data => '</'});            !!!emit ({type => CHARACTER_TOKEN, data => '</'});
599            redo A;            redo A;
600          }          }
601        }        }
602                
603        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_char} and
604            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
605          $self->{current_token} = {type => 'end tag',          !!!cp (29);
606                            tag_name => chr ($self->{next_input_character} + 0x0020)};          $self->{current_token} = {type => END_TAG_TOKEN,
607          $self->{state} = 'tag name';                            tag_name => chr ($self->{next_char} + 0x0020)};
608          !!!next-input-character;          $self->{state} = TAG_NAME_STATE;
609          redo A;          !!!next-input-character;
610        } elsif (0x0061 <= $self->{next_input_character} and          redo A;
611                 $self->{next_input_character} <= 0x007A) { # a..z        } elsif (0x0061 <= $self->{next_char} and
612          $self->{current_token} = {type => 'end tag',                 $self->{next_char} <= 0x007A) { # a..z
613                            tag_name => chr ($self->{next_input_character})};          !!!cp (30);
614          $self->{state} = 'tag name';          $self->{current_token} = {type => END_TAG_TOKEN,
615                              tag_name => chr ($self->{next_char})};
616            $self->{state} = TAG_NAME_STATE;
617          !!!next-input-character;          !!!next-input-character;
618          redo A;          redo A;
619        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
620            !!!cp (31);
621          !!!parse-error (type => 'empty end tag');          !!!parse-error (type => 'empty end tag');
622          $self->{state} = 'data';          $self->{state} = DATA_STATE;
623          !!!next-input-character;          !!!next-input-character;
624          redo A;          redo A;
625        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
626            !!!cp (32);
627          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
628          $self->{state} = 'data';          $self->{state} = DATA_STATE;
629          # reconsume          # reconsume
630    
631          !!!emit ({type => 'character', data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</'});
632    
633          redo A;          redo A;
634        } else {        } else {
635            !!!cp (33);
636          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
637          $self->{state} = 'bogus comment';          $self->{state} = BOGUS_COMMENT_STATE;
638          ## $self->{next_input_character} is intentionally left as is          ## $self->{next_char} is intentionally left as is
639          redo A;          redo A;
640        }        }
641      } elsif ($self->{state} eq 'tag name') {      } elsif ($self->{state} == TAG_NAME_STATE) {
642        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
643            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
644            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
645            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
646            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
647          $self->{state} = 'before attribute name';          !!!cp (34);
648          !!!next-input-character;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
649          redo A;          !!!next-input-character;
650        } elsif ($self->{next_input_character} == 0x003E) { # >          redo A;
651          if ($self->{current_token}->{type} eq 'start tag') {        } elsif ($self->{next_char} == 0x003E) { # >
652            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
653              !!!cp (35);
654            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
655                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
656            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
657          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
658            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
659            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
660              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This should never be reached.
661            }            #  !!! cp (36);
662              #  !!! parse-error (type => 'end tag attribute');
663              #} else {
664                !!!cp (37);
665              #}
666          } else {          } else {
667            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
668          }          }
669          $self->{state} = 'data';          $self->{state} = DATA_STATE;
670          !!!next-input-character;          !!!next-input-character;
671    
672          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
673    
674          redo A;          redo A;
675        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
676                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
677          $self->{current_token}->{tag_name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (38);
678            $self->{current_token}->{tag_name} .= chr ($self->{next_char} + 0x0020);
679            # start tag or end tag            # start tag or end tag
680          ## Stay in this state          ## Stay in this state
681          !!!next-input-character;          !!!next-input-character;
682          redo A;          redo A;
683        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
684          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
685          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
686              !!!cp (39);
687            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
688                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
689            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
690          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
691            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
692            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
693              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This state should never be reached.
694            }            #  !!! cp (40);
695              #  !!! parse-error (type => 'end tag attribute');
696              #} else {
697                !!!cp (41);
698              #}
699          } else {          } else {
700            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
701          }          }
702          $self->{state} = 'data';          $self->{state} = DATA_STATE;
703          # reconsume          # reconsume
704    
705          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
706    
707          redo A;          redo A;
708        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
709          !!!next-input-character;          !!!next-input-character;
710          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_char} == 0x003E and # >
711              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
712              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
713            # permitted slash            # permitted slash
714              !!!cp (42);
715            #            #
716          } else {          } else {
717              !!!cp (43);
718            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
719          }          }
720          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
721          # next-input-character is already done          # next-input-character is already done
722          redo A;          redo A;
723        } else {        } else {
724          $self->{current_token}->{tag_name} .= chr $self->{next_input_character};          !!!cp (44);
725            $self->{current_token}->{tag_name} .= chr $self->{next_char};
726            # start tag or end tag            # start tag or end tag
727          ## Stay in the state          ## Stay in the state
728          !!!next-input-character;          !!!next-input-character;
729          redo A;          redo A;
730        }        }
731      } elsif ($self->{state} eq 'before attribute name') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
732        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
733            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
734            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
735            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
736            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
737            !!!cp (45);
738          ## Stay in the state          ## Stay in the state
739          !!!next-input-character;          !!!next-input-character;
740          redo A;          redo A;
741        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
742          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
743              !!!cp (46);
744            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
745                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
746            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
747          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
748            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
749            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
750                !!!cp (47);
751              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
752              } else {
753                !!!cp (48);
754            }            }
755          } else {          } else {
756            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
757          }          }
758          $self->{state} = 'data';          $self->{state} = DATA_STATE;
759          !!!next-input-character;          !!!next-input-character;
760    
761          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
762    
763          redo A;          redo A;
764        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
765                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
766          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (49);
767            $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),
768                                value => ''};                                value => ''};
769          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
770          !!!next-input-character;          !!!next-input-character;
771          redo A;          redo A;
772        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
773          !!!next-input-character;          !!!next-input-character;
774          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_char} == 0x003E and # >
775              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
776              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
777            # permitted slash            # permitted slash
778              !!!cp (50);
779            #            #
780          } else {          } else {
781              !!!cp (51);
782            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
783          }          }
784          ## Stay in the state          ## Stay in the state
785          # next-input-character is already done          # next-input-character is already done
786          redo A;          redo A;
787        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
788          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
789          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
790              !!!cp (52);
791            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
792                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
793            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
794          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
795            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
796            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
797                !!!cp (53);
798              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
799              } else {
800                !!!cp (54);
801            }            }
802          } else {          } else {
803            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
804          }          }
805          $self->{state} = 'data';          $self->{state} = DATA_STATE;
806          # reconsume          # reconsume
807    
808          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
809    
810          redo A;          redo A;
811        } else {        } else {
812          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ({
813                 0x0022 => 1, # "
814                 0x0027 => 1, # '
815                 0x003D => 1, # =
816                }->{$self->{next_char}}) {
817              !!!cp (55);
818              !!!parse-error (type => 'bad attribute name');
819            } else {
820              !!!cp (56);
821            }
822            $self->{current_attribute} = {name => chr ($self->{next_char}),
823                                value => ''};                                value => ''};
824          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
825          !!!next-input-character;          !!!next-input-character;
826          redo A;          redo A;
827        }        }
828      } elsif ($self->{state} eq 'attribute name') {      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {
829        my $before_leave = sub {        my $before_leave = sub {
830          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
831              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
832            !!!parse-error (type => 'dupulicate attribute');            !!!cp (57);
833              !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});
834            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
835          } else {          } else {
836              !!!cp (58);
837            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
838              = $self->{current_attribute};              = $self->{current_attribute};
839          }          }
840        }; # $before_leave        }; # $before_leave
841    
842        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
843            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
844            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
845            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
846            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
847            !!!cp (59);
848          $before_leave->();          $before_leave->();
849          $self->{state} = 'after attribute name';          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
850          !!!next-input-character;          !!!next-input-character;
851          redo A;          redo A;
852        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
853            !!!cp (60);
854          $before_leave->();          $before_leave->();
855          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
856          !!!next-input-character;          !!!next-input-character;
857          redo A;          redo A;
858        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
859          $before_leave->();          $before_leave->();
860          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
861              !!!cp (61);
862            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
863                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
864            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
865          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
866            $self->{content_model_flag} = 'PCDATA'; # MUST            !!!cp (62);
867              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
868            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
869              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
870            }            }
871          } else {          } else {
872            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
873          }          }
874          $self->{state} = 'data';          $self->{state} = DATA_STATE;
875          !!!next-input-character;          !!!next-input-character;
876    
877          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
878    
879          redo A;          redo A;
880        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
881                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
882          $self->{current_attribute}->{name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (63);
883            $self->{current_attribute}->{name} .= chr ($self->{next_char} + 0x0020);
884          ## Stay in the state          ## Stay in the state
885          !!!next-input-character;          !!!next-input-character;
886          redo A;          redo A;
887        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
888          $before_leave->();          $before_leave->();
889          !!!next-input-character;          !!!next-input-character;
890          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_char} == 0x003E and # >
891              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
892              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
893            # permitted slash            # permitted slash
894              !!!cp (64);
895            #            #
896          } else {          } else {
897              !!!cp (65);
898            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
899          }          }
900          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
901          # next-input-character is already done          # next-input-character is already done
902          redo A;          redo A;
903        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
904          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
905          $before_leave->();          $before_leave->();
906          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
907              !!!cp (66);
908            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
909                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
910            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
911          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
912            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
913            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
914                !!!cp (67);
915              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
916              } else {
917                ## NOTE: This state should never be reached.
918                !!!cp (68);
919            }            }
920          } else {          } else {
921            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
922          }          }
923          $self->{state} = 'data';          $self->{state} = DATA_STATE;
924          # reconsume          # reconsume
925    
926          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
927    
928          redo A;          redo A;
929        } else {        } else {
930          $self->{current_attribute}->{name} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x0022 or # "
931                $self->{next_char} == 0x0027) { # '
932              !!!cp (69);
933              !!!parse-error (type => 'bad attribute name');
934            } else {
935              !!!cp (70);
936            }
937            $self->{current_attribute}->{name} .= chr ($self->{next_char});
938          ## Stay in the state          ## Stay in the state
939          !!!next-input-character;          !!!next-input-character;
940          redo A;          redo A;
941        }        }
942      } elsif ($self->{state} eq 'after attribute name') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
943        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
944            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
945            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
946            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
947            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
948            !!!cp (71);
949          ## Stay in the state          ## Stay in the state
950          !!!next-input-character;          !!!next-input-character;
951          redo A;          redo A;
952        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
953          $self->{state} = 'before attribute value';          !!!cp (72);
954            $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
955          !!!next-input-character;          !!!next-input-character;
956          redo A;          redo A;
957        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
958          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
959              !!!cp (73);
960            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
961                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
962            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
963          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
964            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
965            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
966                !!!cp (74);
967              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
968              } else {
969                ## NOTE: This state should never be reached.
970                !!!cp (75);
971            }            }
972          } else {          } else {
973            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
974          }          }
975          $self->{state} = 'data';          $self->{state} = DATA_STATE;
976          !!!next-input-character;          !!!next-input-character;
977    
978          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
979    
980          redo A;          redo A;
981        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
982                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
983          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (76);
984            $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),
985                                value => ''};                                value => ''};
986          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
987          !!!next-input-character;          !!!next-input-character;
988          redo A;          redo A;
989        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
990          !!!next-input-character;          !!!next-input-character;
991          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_char} == 0x003E and # >
992              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
993              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
994            # permitted slash            # permitted slash
995              !!!cp (77);
996            #            #
997          } else {          } else {
998              !!!cp (78);
999            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
1000            ## TODO: Different error type for <aa / bb> than <aa/>            ## TODO: Different error type for <aa / bb> than <aa/>
1001          }          }
1002          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1003          # next-input-character is already done          # next-input-character is already done
1004          redo A;          redo A;
1005        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1006          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1007          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1008              !!!cp (79);
1009            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
1010                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
1011            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1012          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1013            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1014            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1015                !!!cp (80);
1016              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1017              } else {
1018                ## NOTE: This state should never be reached.
1019                !!!cp (81);
1020            }            }
1021          } else {          } else {
1022            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1023          }          }
1024          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1025          # reconsume          # reconsume
1026    
1027          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1028    
1029          redo A;          redo A;
1030        } else {        } else {
1031          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          !!!cp (82);
1032            $self->{current_attribute} = {name => chr ($self->{next_char}),
1033                                value => ''};                                value => ''};
1034          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
1035          !!!next-input-character;          !!!next-input-character;
1036          redo A;                  redo A;        
1037        }        }
1038      } elsif ($self->{state} eq 'before attribute value') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
1039        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1040            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1041            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1042            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1043            $self->{next_input_character} == 0x0020) { # SP                  $self->{next_char} == 0x0020) { # SP      
1044            !!!cp (83);
1045          ## Stay in the state          ## Stay in the state
1046          !!!next-input-character;          !!!next-input-character;
1047          redo A;          redo A;
1048        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
1049          $self->{state} = 'attribute value (double-quoted)';          !!!cp (84);
1050            $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
1051          !!!next-input-character;          !!!next-input-character;
1052          redo A;          redo A;
1053        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1054          $self->{state} = 'attribute value (unquoted)';          !!!cp (85);
1055            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1056          ## reconsume          ## reconsume
1057          redo A;          redo A;
1058        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
1059          $self->{state} = 'attribute value (single-quoted)';          !!!cp (86);
1060            $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
1061          !!!next-input-character;          !!!next-input-character;
1062          redo A;          redo A;
1063        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1064          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1065              !!!cp (87);
1066            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
1067                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
1068            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1069          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1070            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1071            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1072                !!!cp (88);
1073              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1074              } else {
1075                ## NOTE: This state should never be reached.
1076                !!!cp (89);
1077            }            }
1078          } else {          } else {
1079            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1080          }          }
1081          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1082          !!!next-input-character;          !!!next-input-character;
1083    
1084          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1085    
1086          redo A;          redo A;
1087        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1088          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1089          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1090              !!!cp (90);
1091            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
1092                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
1093            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1094          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1095            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1096            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1097                !!!cp (91);
1098              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1099              } else {
1100                ## NOTE: This state should never be reached.
1101                !!!cp (92);
1102            }            }
1103          } else {          } else {
1104            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1105          }          }
1106          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1107          ## reconsume          ## reconsume
1108    
1109          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1110    
1111          redo A;          redo A;
1112        } else {        } else {
1113          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x003D) { # =
1114          $self->{state} = 'attribute value (unquoted)';            !!!cp (93);
1115              !!!parse-error (type => 'bad attribute value');
1116            } else {
1117              !!!cp (94);
1118            }
1119            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1120            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1121          !!!next-input-character;          !!!next-input-character;
1122          redo A;          redo A;
1123        }        }
1124      } elsif ($self->{state} eq 'attribute value (double-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
1125        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
1126          $self->{state} = 'before attribute name';          !!!cp (95);
1127            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1128          !!!next-input-character;          !!!next-input-character;
1129          redo A;          redo A;
1130        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1131          $self->{last_attribute_value_state} = 'attribute value (double-quoted)';          !!!cp (96);
1132          $self->{state} = 'entity in attribute value';          $self->{last_attribute_value_state} = $self->{state};
1133            $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1134          !!!next-input-character;          !!!next-input-character;
1135          redo A;          redo A;
1136        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1137          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1138          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1139              !!!cp (97);
1140            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
1141                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
1142            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1143          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1144            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1145            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1146                !!!cp (98);
1147              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1148              } else {
1149                ## NOTE: This state should never be reached.
1150                !!!cp (99);
1151            }            }
1152          } else {          } else {
1153            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1154          }          }
1155          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1156          ## reconsume          ## reconsume
1157    
1158          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1159    
1160          redo A;          redo A;
1161        } else {        } else {
1162          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (100);
1163            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1164          ## Stay in the state          ## Stay in the state
1165          !!!next-input-character;          !!!next-input-character;
1166          redo A;          redo A;
1167        }        }
1168      } elsif ($self->{state} eq 'attribute value (single-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1169        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
1170          $self->{state} = 'before attribute name';          !!!cp (101);
1171            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1172          !!!next-input-character;          !!!next-input-character;
1173          redo A;          redo A;
1174        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1175          $self->{last_attribute_value_state} = 'attribute value (single-quoted)';          !!!cp (102);
1176          $self->{state} = 'entity in attribute value';          $self->{last_attribute_value_state} = $self->{state};
1177            $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1178          !!!next-input-character;          !!!next-input-character;
1179          redo A;          redo A;
1180        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1181          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1182          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1183              !!!cp (103);
1184            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
1185                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
1186            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1187          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1188            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1189            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1190                !!!cp (104);
1191              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1192              } else {
1193                ## NOTE: This state should never be reached.
1194                !!!cp (105);
1195            }            }
1196          } else {          } else {
1197            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1198          }          }
1199          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1200          ## reconsume          ## reconsume
1201    
1202          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1203    
1204          redo A;          redo A;
1205        } else {        } else {
1206          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (106);
1207            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1208          ## Stay in the state          ## Stay in the state
1209          !!!next-input-character;          !!!next-input-character;
1210          redo A;          redo A;
1211        }        }
1212      } elsif ($self->{state} eq 'attribute value (unquoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1213        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1214            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1215            $self->{next_input_character} == 0x000B or # HT            $self->{next_char} == 0x000B or # HT
1216            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1217            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1218          $self->{state} = 'before attribute name';          !!!cp (107);
1219          !!!next-input-character;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1220          redo A;          !!!next-input-character;
1221        } elsif ($self->{next_input_character} == 0x0026) { # &          redo A;
1222          $self->{last_attribute_value_state} = 'attribute value (unquoted)';        } elsif ($self->{next_char} == 0x0026) { # &
1223          $self->{state} = 'entity in attribute value';          !!!cp (108);
1224          !!!next-input-character;          $self->{last_attribute_value_state} = $self->{state};
1225          redo A;          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1226        } elsif ($self->{next_input_character} == 0x003E) { # >          !!!next-input-character;
1227          if ($self->{current_token}->{type} eq 'start tag') {          redo A;
1228          } elsif ($self->{next_char} == 0x003E) { # >
1229            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1230              !!!cp (109);
1231            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
1232                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
1233            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1234          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1235            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1236            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1237                !!!cp (110);
1238              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1239              } else {
1240                ## NOTE: This state should never be reached.
1241                !!!cp (111);
1242            }            }
1243          } else {          } else {
1244            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1245          }          }
1246          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1247          !!!next-input-character;          !!!next-input-character;
1248    
1249          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1250    
1251          redo A;          redo A;
1252        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1253          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1254          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1255              !!!cp (112);
1256            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
1257                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
1258            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1259          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1260            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1261            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1262                !!!cp (113);
1263              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1264              } else {
1265                ## NOTE: This state should never be reached.
1266                !!!cp (114);
1267            }            }
1268          } else {          } else {
1269            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1270          }          }
1271          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1272          ## reconsume          ## reconsume
1273    
1274          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1275    
1276          redo A;          redo A;
1277        } else {        } else {
1278          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ({
1279                 0x0022 => 1, # "
1280                 0x0027 => 1, # '
1281                 0x003D => 1, # =
1282                }->{$self->{next_char}}) {
1283              !!!cp (115);
1284              !!!parse-error (type => 'bad attribute value');
1285            } else {
1286              !!!cp (116);
1287            }
1288            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1289          ## Stay in the state          ## Stay in the state
1290          !!!next-input-character;          !!!next-input-character;
1291          redo A;          redo A;
1292        }        }
1293      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {
1294        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);        my $token = $self->_tokenize_attempt_to_consume_an_entity
1295              (1,
1296               $self->{last_attribute_value_state}
1297                 == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE ? 0x0022 : # "
1298               $self->{last_attribute_value_state}
1299                 == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE ? 0x0027 : # '
1300               -1);
1301    
1302        unless (defined $token) {        unless (defined $token) {
1303            !!!cp (117);
1304          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
1305        } else {        } else {
1306            !!!cp (118);
1307          $self->{current_attribute}->{value} .= $token->{data};          $self->{current_attribute}->{value} .= $token->{data};
1308            $self->{current_attribute}->{has_reference} = $token->{has_reference};
1309          ## ISSUE: spec says "append the returned character token to the current attribute's value"          ## ISSUE: spec says "append the returned character token to the current attribute's value"
1310        }        }
1311    
1312        $self->{state} = $self->{last_attribute_value_state};        $self->{state} = $self->{last_attribute_value_state};
1313        # next-input-character is already done        # next-input-character is already done
1314        redo A;        redo A;
1315      } elsif ($self->{state} eq 'bogus comment') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1316          if ($self->{next_char} == 0x0009 or # HT
1317              $self->{next_char} == 0x000A or # LF
1318              $self->{next_char} == 0x000B or # VT
1319              $self->{next_char} == 0x000C or # FF
1320              $self->{next_char} == 0x0020) { # SP
1321            !!!cp (118);
1322            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1323            !!!next-input-character;
1324            redo A;
1325          } elsif ($self->{next_char} == 0x003E) { # >
1326            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1327              !!!cp (119);
1328              $self->{current_token}->{first_start_tag}
1329                  = not defined $self->{last_emitted_start_tag_name};
1330              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1331            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1332              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1333              if ($self->{current_token}->{attributes}) {
1334                !!!cp (120);
1335                !!!parse-error (type => 'end tag attribute');
1336              } else {
1337                ## NOTE: This state should never be reached.
1338                !!!cp (121);
1339              }
1340            } else {
1341              die "$0: $self->{current_token}->{type}: Unknown token type";
1342            }
1343            $self->{state} = DATA_STATE;
1344            !!!next-input-character;
1345    
1346            !!!emit ($self->{current_token}); # start tag or end tag
1347    
1348            redo A;
1349          } elsif ($self->{next_char} == 0x002F) { # /
1350            !!!next-input-character;
1351            if ($self->{next_char} == 0x003E and # >
1352                $self->{current_token}->{type} == START_TAG_TOKEN and
1353                $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
1354              # permitted slash
1355              !!!cp (122);
1356              #
1357            } else {
1358              !!!cp (123);
1359              !!!parse-error (type => 'nestc');
1360            }
1361            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1362            # next-input-character is already done
1363            redo A;
1364          } else {
1365            !!!cp (124);
1366            !!!parse-error (type => 'no space between attributes');
1367            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1368            ## reconsume
1369            redo A;
1370          }
1371        } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1372        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1373                
1374        my $token = {type => 'comment', data => ''};        my $token = {type => COMMENT_TOKEN, data => ''};
1375    
1376        BC: {        BC: {
1377          if ($self->{next_input_character} == 0x003E) { # >          if ($self->{next_char} == 0x003E) { # >
1378            $self->{state} = 'data';            !!!cp (124);
1379              $self->{state} = DATA_STATE;
1380            !!!next-input-character;            !!!next-input-character;
1381    
1382            !!!emit ($token);            !!!emit ($token);
1383    
1384            redo A;            redo A;
1385          } elsif ($self->{next_input_character} == -1) {          } elsif ($self->{next_char} == -1) {
1386            $self->{state} = 'data';            !!!cp (125);
1387              $self->{state} = DATA_STATE;
1388            ## reconsume            ## reconsume
1389    
1390            !!!emit ($token);            !!!emit ($token);
1391    
1392            redo A;            redo A;
1393          } else {          } else {
1394            $token->{data} .= chr ($self->{next_input_character});            !!!cp (126);
1395              $token->{data} .= chr ($self->{next_char});
1396            !!!next-input-character;            !!!next-input-character;
1397            redo BC;            redo BC;
1398          }          }
1399        } # BC        } # BC
1400      } elsif ($self->{state} eq 'markup declaration open') {  
1401          die "$0: _get_next_token: unexpected case [BC]";
1402        } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1403        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1404    
1405        my @next_char;        my @next_char;
1406        push @next_char, $self->{next_input_character};        push @next_char, $self->{next_char};
1407                
1408        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1409          !!!next-input-character;          !!!next-input-character;
1410          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
1411          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_char} == 0x002D) { # -
1412            $self->{current_token} = {type => 'comment', data => ''};            !!!cp (127);
1413            $self->{state} = 'comment start';            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};
1414              $self->{state} = COMMENT_START_STATE;
1415            !!!next-input-character;            !!!next-input-character;
1416            redo A;            redo A;
1417            } else {
1418              !!!cp (128);
1419          }          }
1420        } elsif ($self->{next_input_character} == 0x0044 or # D        } elsif ($self->{next_char} == 0x0044 or # D
1421                 $self->{next_input_character} == 0x0064) { # d                 $self->{next_char} == 0x0064) { # d
1422          !!!next-input-character;          !!!next-input-character;
1423          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
1424          if ($self->{next_input_character} == 0x004F or # O          if ($self->{next_char} == 0x004F or # O
1425              $self->{next_input_character} == 0x006F) { # o              $self->{next_char} == 0x006F) { # o
1426            !!!next-input-character;            !!!next-input-character;
1427            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_char};
1428            if ($self->{next_input_character} == 0x0043 or # C            if ($self->{next_char} == 0x0043 or # C
1429                $self->{next_input_character} == 0x0063) { # c                $self->{next_char} == 0x0063) { # c
1430              !!!next-input-character;              !!!next-input-character;
1431              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_char};
1432              if ($self->{next_input_character} == 0x0054 or # T              if ($self->{next_char} == 0x0054 or # T
1433                  $self->{next_input_character} == 0x0074) { # t                  $self->{next_char} == 0x0074) { # t
1434                !!!next-input-character;                !!!next-input-character;
1435                push @next_char, $self->{next_input_character};                push @next_char, $self->{next_char};
1436                if ($self->{next_input_character} == 0x0059 or # Y                if ($self->{next_char} == 0x0059 or # Y
1437                    $self->{next_input_character} == 0x0079) { # y                    $self->{next_char} == 0x0079) { # y
1438                  !!!next-input-character;                  !!!next-input-character;
1439                  push @next_char, $self->{next_input_character};                  push @next_char, $self->{next_char};
1440                  if ($self->{next_input_character} == 0x0050 or # P                  if ($self->{next_char} == 0x0050 or # P
1441                      $self->{next_input_character} == 0x0070) { # p                      $self->{next_char} == 0x0070) { # p
1442                    !!!next-input-character;                    !!!next-input-character;
1443                    push @next_char, $self->{next_input_character};                    push @next_char, $self->{next_char};
1444                    if ($self->{next_input_character} == 0x0045 or # E                    if ($self->{next_char} == 0x0045 or # E
1445                        $self->{next_input_character} == 0x0065) { # e                        $self->{next_char} == 0x0065) { # e
1446                      ## ISSUE: What a stupid code this is!                      !!!cp (129);
1447                      $self->{state} = 'DOCTYPE';                      ## TODO: What a stupid code this is!
1448                        $self->{state} = DOCTYPE_STATE;
1449                      !!!next-input-character;                      !!!next-input-character;
1450                      redo A;                      redo A;
1451                      } else {
1452                        !!!cp (130);
1453                    }                    }
1454                    } else {
1455                      !!!cp (131);
1456                  }                  }
1457                  } else {
1458                    !!!cp (132);
1459                }                }
1460                } else {
1461                  !!!cp (133);
1462              }              }
1463              } else {
1464                !!!cp (134);
1465            }            }
1466            } else {
1467              !!!cp (135);
1468          }          }
1469          } else {
1470            !!!cp (136);
1471        }        }
1472    
1473        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'bogus comment');
1474        $self->{next_input_character} = shift @next_char;        $self->{next_char} = shift @next_char;
1475        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
1476        $self->{state} = 'bogus comment';        $self->{state} = BOGUS_COMMENT_STATE;
1477        redo A;        redo A;
1478                
1479        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
1480        ## 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?
1481      } elsif ($self->{state} eq 'comment start') {      } elsif ($self->{state} == COMMENT_START_STATE) {
1482        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1483          $self->{state} = 'comment start dash';          !!!cp (137);
1484            $self->{state} = COMMENT_START_DASH_STATE;
1485          !!!next-input-character;          !!!next-input-character;
1486          redo A;          redo A;
1487        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1488            !!!cp (138);
1489          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
1490          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1491          !!!next-input-character;          !!!next-input-character;
1492    
1493          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1494    
1495          redo A;          redo A;
1496        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1497            !!!cp (139);
1498          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1499          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1500          ## reconsume          ## reconsume
1501    
1502          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1503    
1504          redo A;          redo A;
1505        } else {        } else {
1506            !!!cp (140);
1507          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
1508              .= chr ($self->{next_input_character});              .= chr ($self->{next_char});
1509          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1510          !!!next-input-character;          !!!next-input-character;
1511          redo A;          redo A;
1512        }        }
1513      } elsif ($self->{state} eq 'comment start dash') {      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
1514        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1515          $self->{state} = 'comment end';          !!!cp (141);
1516            $self->{state} = COMMENT_END_STATE;
1517          !!!next-input-character;          !!!next-input-character;
1518          redo A;          redo A;
1519        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1520            !!!cp (142);
1521          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
1522          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1523          !!!next-input-character;          !!!next-input-character;
1524    
1525          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1526    
1527          redo A;          redo A;
1528        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1529            !!!cp (143);
1530          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1531          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1532          ## reconsume          ## reconsume
1533    
1534          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1535    
1536          redo A;          redo A;
1537        } else {        } else {
1538            !!!cp (144);
1539          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
1540              .= '-' . chr ($self->{next_input_character});              .= '-' . chr ($self->{next_char});
1541          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1542          !!!next-input-character;          !!!next-input-character;
1543          redo A;          redo A;
1544        }        }
1545      } elsif ($self->{state} eq 'comment') {      } elsif ($self->{state} == COMMENT_STATE) {
1546        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1547          $self->{state} = 'comment end dash';          !!!cp (145);
1548            $self->{state} = COMMENT_END_DASH_STATE;
1549          !!!next-input-character;          !!!next-input-character;
1550          redo A;          redo A;
1551        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1552            !!!cp (146);
1553          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1554          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1555          ## reconsume          ## reconsume
1556    
1557          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1558    
1559          redo A;          redo A;
1560        } else {        } else {
1561          $self->{current_token}->{data} .= chr ($self->{next_input_character}); # comment          !!!cp (147);
1562            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1563          ## Stay in the state          ## Stay in the state
1564          !!!next-input-character;          !!!next-input-character;
1565          redo A;          redo A;
1566        }        }
1567      } elsif ($self->{state} eq 'comment end dash') {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
1568        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1569          $self->{state} = 'comment end';          !!!cp (148);
1570            $self->{state} = COMMENT_END_STATE;
1571          !!!next-input-character;          !!!next-input-character;
1572          redo A;          redo A;
1573        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1574            !!!cp (149);
1575          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1576          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1577          ## reconsume          ## reconsume
1578    
1579          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1580    
1581          redo A;          redo A;
1582        } else {        } else {
1583          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          !!!cp (150);
1584          $self->{state} = 'comment';          $self->{current_token}->{data} .= '-' . chr ($self->{next_char}); # comment
1585            $self->{state} = COMMENT_STATE;
1586          !!!next-input-character;          !!!next-input-character;
1587          redo A;          redo A;
1588        }        }
1589      } elsif ($self->{state} eq 'comment end') {      } elsif ($self->{state} == COMMENT_END_STATE) {
1590        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
1591          $self->{state} = 'data';          !!!cp (151);
1592            $self->{state} = DATA_STATE;
1593          !!!next-input-character;          !!!next-input-character;
1594    
1595          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1596    
1597          redo A;          redo A;
1598        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
1599            !!!cp (152);
1600          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment');
1601          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
1602          ## Stay in the state          ## Stay in the state
1603          !!!next-input-character;          !!!next-input-character;
1604          redo A;          redo A;
1605        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1606            !!!cp (153);
1607          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1608          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1609          ## reconsume          ## reconsume
1610    
1611          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1612    
1613          redo A;          redo A;
1614        } else {        } else {
1615            !!!cp (154);
1616          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment');
1617          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
1618          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1619          !!!next-input-character;          !!!next-input-character;
1620          redo A;          redo A;
1621        }        }
1622      } elsif ($self->{state} eq 'DOCTYPE') {      } elsif ($self->{state} == DOCTYPE_STATE) {
1623        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1624            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1625            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1626            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1627            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1628          $self->{state} = 'before DOCTYPE name';          !!!cp (155);
1629            $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1630          !!!next-input-character;          !!!next-input-character;
1631          redo A;          redo A;
1632        } else {        } else {
1633            !!!cp (156);
1634          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
1635          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1636          ## reconsume          ## reconsume
1637          redo A;          redo A;
1638        }        }
1639      } elsif ($self->{state} eq 'before DOCTYPE name') {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
1640        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1641            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1642            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1643            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1644            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1645            !!!cp (157);
1646          ## Stay in the state          ## Stay in the state
1647          !!!next-input-character;          !!!next-input-character;
1648          redo A;          redo A;
1649        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1650            !!!cp (158);
1651          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1652          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1653          !!!next-input-character;          !!!next-input-character;
1654    
1655          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});
1656    
1657          redo A;          redo A;
1658        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1659            !!!cp (159);
1660          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1661          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1662          ## reconsume          ## reconsume
1663    
1664          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});
1665    
1666          redo A;          redo A;
1667        } else {        } else {
1668            !!!cp (160);
1669          $self->{current_token}          $self->{current_token}
1670              = {type => 'DOCTYPE',              = {type => DOCTYPE_TOKEN,
1671                 name => chr ($self->{next_input_character}),                 name => chr ($self->{next_char}),
1672                 correct => 1};                 #quirks => 0,
1673                  };
1674  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
1675          $self->{state} = 'DOCTYPE name';          $self->{state} = DOCTYPE_NAME_STATE;
1676          !!!next-input-character;          !!!next-input-character;
1677          redo A;          redo A;
1678        }        }
1679      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
1680  ## ISSUE: Redundant "First," in the spec.  ## ISSUE: Redundant "First," in the spec.
1681        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1682            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1683            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1684            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1685            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1686          $self->{state} = 'after DOCTYPE name';          !!!cp (161);
1687            $self->{state} = AFTER_DOCTYPE_NAME_STATE;
1688          !!!next-input-character;          !!!next-input-character;
1689          redo A;          redo A;
1690        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1691          $self->{state} = 'data';          !!!cp (162);
1692            $self->{state} = DATA_STATE;
1693          !!!next-input-character;          !!!next-input-character;
1694    
1695          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1696    
1697          redo A;          redo A;
1698        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1699            !!!cp (163);
1700          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1701          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1702          ## reconsume          ## reconsume
1703    
1704          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1705          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1706    
1707          redo A;          redo A;
1708        } else {        } else {
1709            !!!cp (164);
1710          $self->{current_token}->{name}          $self->{current_token}->{name}
1711            .= chr ($self->{next_input_character}); # DOCTYPE            .= chr ($self->{next_char}); # DOCTYPE
1712          ## Stay in the state          ## Stay in the state
1713          !!!next-input-character;          !!!next-input-character;
1714          redo A;          redo A;
1715        }        }
1716      } elsif ($self->{state} eq 'after DOCTYPE name') {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
1717        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1718            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1719            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1720            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1721            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1722            !!!cp (165);
1723          ## Stay in the state          ## Stay in the state
1724          !!!next-input-character;          !!!next-input-character;
1725          redo A;          redo A;
1726        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1727          $self->{state} = 'data';          !!!cp (166);
1728            $self->{state} = DATA_STATE;
1729          !!!next-input-character;          !!!next-input-character;
1730    
1731          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1732    
1733          redo A;          redo A;
1734        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1735            !!!cp (167);
1736          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1737          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1738          ## reconsume          ## reconsume
1739    
1740          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1741          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1742    
1743          redo A;          redo A;
1744        } elsif ($self->{next_input_character} == 0x0050 or # P        } elsif ($self->{next_char} == 0x0050 or # P
1745                 $self->{next_input_character} == 0x0070) { # p                 $self->{next_char} == 0x0070) { # p
1746          !!!next-input-character;          !!!next-input-character;
1747          if ($self->{next_input_character} == 0x0055 or # U          if ($self->{next_char} == 0x0055 or # U
1748              $self->{next_input_character} == 0x0075) { # u              $self->{next_char} == 0x0075) { # u
1749            !!!next-input-character;            !!!next-input-character;
1750            if ($self->{next_input_character} == 0x0042 or # B            if ($self->{next_char} == 0x0042 or # B
1751                $self->{next_input_character} == 0x0062) { # b                $self->{next_char} == 0x0062) { # b
1752              !!!next-input-character;              !!!next-input-character;
1753              if ($self->{next_input_character} == 0x004C or # L              if ($self->{next_char} == 0x004C or # L
1754                  $self->{next_input_character} == 0x006C) { # l                  $self->{next_char} == 0x006C) { # l
1755                !!!next-input-character;                !!!next-input-character;
1756                if ($self->{next_input_character} == 0x0049 or # I                if ($self->{next_char} == 0x0049 or # I
1757                    $self->{next_input_character} == 0x0069) { # i                    $self->{next_char} == 0x0069) { # i
1758                  !!!next-input-character;                  !!!next-input-character;
1759                  if ($self->{next_input_character} == 0x0043 or # C                  if ($self->{next_char} == 0x0043 or # C
1760                      $self->{next_input_character} == 0x0063) { # c                      $self->{next_char} == 0x0063) { # c
1761                    $self->{state} = 'before DOCTYPE public identifier';                    !!!cp (168);
1762                      $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1763                    !!!next-input-character;                    !!!next-input-character;
1764                    redo A;                    redo A;
1765                    } else {
1766                      !!!cp (169);
1767                  }                  }
1768                  } else {
1769                    !!!cp (170);
1770                }                }
1771                } else {
1772                  !!!cp (171);
1773              }              }
1774              } else {
1775                !!!cp (172);
1776            }            }
1777            } else {
1778              !!!cp (173);
1779          }          }
1780    
1781          #          #
1782        } elsif ($self->{next_input_character} == 0x0053 or # S        } elsif ($self->{next_char} == 0x0053 or # S
1783                 $self->{next_input_character} == 0x0073) { # s                 $self->{next_char} == 0x0073) { # s
1784          !!!next-input-character;          !!!next-input-character;
1785          if ($self->{next_input_character} == 0x0059 or # Y          if ($self->{next_char} == 0x0059 or # Y
1786              $self->{next_input_character} == 0x0079) { # y              $self->{next_char} == 0x0079) { # y
1787            !!!next-input-character;            !!!next-input-character;
1788            if ($self->{next_input_character} == 0x0053 or # S            if ($self->{next_char} == 0x0053 or # S
1789                $self->{next_input_character} == 0x0073) { # s                $self->{next_char} == 0x0073) { # s
1790              !!!next-input-character;              !!!next-input-character;
1791              if ($self->{next_input_character} == 0x0054 or # T              if ($self->{next_char} == 0x0054 or # T
1792                  $self->{next_input_character} == 0x0074) { # t                  $self->{next_char} == 0x0074) { # t
1793                !!!next-input-character;                !!!next-input-character;
1794                if ($self->{next_input_character} == 0x0045 or # E                if ($self->{next_char} == 0x0045 or # E
1795                    $self->{next_input_character} == 0x0065) { # e                    $self->{next_char} == 0x0065) { # e
1796                  !!!next-input-character;                  !!!next-input-character;
1797                  if ($self->{next_input_character} == 0x004D or # M                  if ($self->{next_char} == 0x004D or # M
1798                      $self->{next_input_character} == 0x006D) { # m                      $self->{next_char} == 0x006D) { # m
1799                    $self->{state} = 'before DOCTYPE system identifier';                    !!!cp (174);
1800                      $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1801                    !!!next-input-character;                    !!!next-input-character;
1802                    redo A;                    redo A;
1803                    } else {
1804                      !!!cp (175);
1805                  }                  }
1806                  } else {
1807                    !!!cp (176);
1808                }                }
1809                } else {
1810                  !!!cp (177);
1811              }              }
1812              } else {
1813                !!!cp (178);
1814            }            }
1815            } else {
1816              !!!cp (179);
1817          }          }
1818    
1819          #          #
1820        } else {        } else {
1821            !!!cp (180);
1822          !!!next-input-character;          !!!next-input-character;
1823          #          #
1824        }        }
1825    
1826        !!!parse-error (type => 'string after DOCTYPE name');        !!!parse-error (type => 'string after DOCTYPE name');
1827        $self->{state} = 'bogus DOCTYPE';        $self->{current_token}->{quirks} = 1;
1828    
1829          $self->{state} = BOGUS_DOCTYPE_STATE;
1830        # next-input-character is already done        # next-input-character is already done
1831        redo A;        redo A;
1832      } elsif ($self->{state} eq 'before DOCTYPE public identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
1833        if ({        if ({
1834              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1835              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
1836            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
1837            !!!cp (181);
1838          ## Stay in the state          ## Stay in the state
1839          !!!next-input-character;          !!!next-input-character;
1840          redo A;          redo A;
1841        } elsif ($self->{next_input_character} eq 0x0022) { # "        } elsif ($self->{next_char} eq 0x0022) { # "
1842            !!!cp (182);
1843          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1844          $self->{state} = 'DOCTYPE public identifier (double-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
1845          !!!next-input-character;          !!!next-input-character;
1846          redo A;          redo A;
1847        } elsif ($self->{next_input_character} eq 0x0027) { # '        } elsif ($self->{next_char} eq 0x0027) { # '
1848            !!!cp (183);
1849          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1850          $self->{state} = 'DOCTYPE public identifier (single-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
1851          !!!next-input-character;          !!!next-input-character;
1852          redo A;          redo A;
1853        } elsif ($self->{next_input_character} eq 0x003E) { # >        } elsif ($self->{next_char} eq 0x003E) { # >
1854            !!!cp (184);
1855          !!!parse-error (type => 'no PUBLIC literal');          !!!parse-error (type => 'no PUBLIC literal');
1856    
1857          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1858          !!!next-input-character;          !!!next-input-character;
1859    
1860          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1861          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1862    
1863          redo A;          redo A;
1864        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1865            !!!cp (185);
1866          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1867    
1868          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1869          ## reconsume          ## reconsume
1870    
1871          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1872          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1873    
1874          redo A;          redo A;
1875        } else {        } else {
1876            !!!cp (186);
1877          !!!parse-error (type => 'string after PUBLIC');          !!!parse-error (type => 'string after PUBLIC');
1878          $self->{state} = 'bogus DOCTYPE';          $self->{current_token}->{quirks} = 1;
1879    
1880            $self->{state} = BOGUS_DOCTYPE_STATE;
1881          !!!next-input-character;          !!!next-input-character;
1882          redo A;          redo A;
1883        }        }
1884      } elsif ($self->{state} eq 'DOCTYPE public identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
1885        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
1886          $self->{state} = 'after DOCTYPE public identifier';          !!!cp (187);
1887            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1888            !!!next-input-character;
1889            redo A;
1890          } elsif ($self->{next_char} == 0x003E) { # >
1891            !!!cp (188);
1892            !!!parse-error (type => 'unclosed PUBLIC literal');
1893    
1894            $self->{state} = DATA_STATE;
1895          !!!next-input-character;          !!!next-input-character;
1896    
1897            $self->{current_token}->{quirks} = 1;
1898            !!!emit ($self->{current_token}); # DOCTYPE
1899    
1900          redo A;          redo A;
1901        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1902            !!!cp (189);
1903          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
1904    
1905          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1906          ## reconsume          ## reconsume
1907    
1908          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1909          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1910    
1911          redo A;          redo A;
1912        } else {        } else {
1913            !!!cp (190);
1914          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
1915              .= chr $self->{next_input_character};              .= chr $self->{next_char};
1916          ## Stay in the state          ## Stay in the state
1917          !!!next-input-character;          !!!next-input-character;
1918          redo A;          redo A;
1919        }        }
1920      } elsif ($self->{state} eq 'DOCTYPE public identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
1921        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
1922          $self->{state} = 'after DOCTYPE public identifier';          !!!cp (191);
1923            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1924            !!!next-input-character;
1925            redo A;
1926          } elsif ($self->{next_char} == 0x003E) { # >
1927            !!!cp (192);
1928            !!!parse-error (type => 'unclosed PUBLIC literal');
1929    
1930            $self->{state} = DATA_STATE;
1931          !!!next-input-character;          !!!next-input-character;
1932    
1933            $self->{current_token}->{quirks} = 1;
1934            !!!emit ($self->{current_token}); # DOCTYPE
1935    
1936          redo A;          redo A;
1937        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1938            !!!cp (193);
1939          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
1940    
1941          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1942          ## reconsume          ## reconsume
1943    
1944          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1945          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1946    
1947          redo A;          redo A;
1948        } else {        } else {
1949            !!!cp (194);
1950          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
1951              .= chr $self->{next_input_character};              .= chr $self->{next_char};
1952          ## Stay in the state          ## Stay in the state
1953          !!!next-input-character;          !!!next-input-character;
1954          redo A;          redo A;
1955        }        }
1956      } elsif ($self->{state} eq 'after DOCTYPE public identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
1957        if ({        if ({
1958              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1959              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
1960            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
1961            !!!cp (195);
1962          ## Stay in the state          ## Stay in the state
1963          !!!next-input-character;          !!!next-input-character;
1964          redo A;          redo A;
1965        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
1966            !!!cp (196);
1967          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1968          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
1969          !!!next-input-character;          !!!next-input-character;
1970          redo A;          redo A;
1971        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
1972            !!!cp (197);
1973          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1974          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
1975          !!!next-input-character;          !!!next-input-character;
1976          redo A;          redo A;
1977        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1978          $self->{state} = 'data';          !!!cp (198);
1979            $self->{state} = DATA_STATE;
1980          !!!next-input-character;          !!!next-input-character;
1981    
1982          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1983    
1984          redo A;          redo A;
1985        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1986            !!!cp (199);
1987          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1988    
1989          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1990          ## reconsume          ## reconsume
1991    
1992          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1993          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1994    
1995          redo A;          redo A;
1996        } else {        } else {
1997            !!!cp (200);
1998          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after PUBLIC literal');
1999          $self->{state} = 'bogus DOCTYPE';          $self->{current_token}->{quirks} = 1;
2000    
2001            $self->{state} = BOGUS_DOCTYPE_STATE;
2002          !!!next-input-character;          !!!next-input-character;
2003          redo A;          redo A;
2004        }        }
2005      } elsif ($self->{state} eq 'before DOCTYPE system identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2006        if ({        if ({
2007              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2008              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2009            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2010            !!!cp (201);
2011          ## Stay in the state          ## Stay in the state
2012          !!!next-input-character;          !!!next-input-character;
2013          redo A;          redo A;
2014        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
2015            !!!cp (202);
2016          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2017          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2018          !!!next-input-character;          !!!next-input-character;
2019          redo A;          redo A;
2020        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
2021            !!!cp (203);
2022          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2023          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2024          !!!next-input-character;          !!!next-input-character;
2025          redo A;          redo A;
2026        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2027            !!!cp (204);
2028          !!!parse-error (type => 'no SYSTEM literal');          !!!parse-error (type => 'no SYSTEM literal');
2029          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2030          !!!next-input-character;          !!!next-input-character;
2031    
2032          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2033          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2034    
2035          redo A;          redo A;
2036        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2037            !!!cp (205);
2038          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2039    
2040          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2041          ## reconsume          ## reconsume
2042    
2043          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2044          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2045    
2046          redo A;          redo A;
2047        } else {        } else {
2048            !!!cp (206);
2049          !!!parse-error (type => 'string after SYSTEM');          !!!parse-error (type => 'string after SYSTEM');
2050          $self->{state} = 'bogus DOCTYPE';          $self->{current_token}->{quirks} = 1;
2051    
2052            $self->{state} = BOGUS_DOCTYPE_STATE;
2053          !!!next-input-character;          !!!next-input-character;
2054          redo A;          redo A;
2055        }        }
2056      } elsif ($self->{state} eq 'DOCTYPE system identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2057        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
2058          $self->{state} = 'after DOCTYPE system identifier';          !!!cp (207);
2059            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2060          !!!next-input-character;          !!!next-input-character;
2061          redo A;          redo A;
2062        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2063            !!!cp (208);
2064            !!!parse-error (type => 'unclosed PUBLIC literal');
2065    
2066            $self->{state} = DATA_STATE;
2067            !!!next-input-character;
2068    
2069            $self->{current_token}->{quirks} = 1;
2070            !!!emit ($self->{current_token}); # DOCTYPE
2071    
2072            redo A;
2073          } elsif ($self->{next_char} == -1) {
2074            !!!cp (209);
2075          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2076    
2077          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2078          ## reconsume          ## reconsume
2079    
2080          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2081          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2082    
2083          redo A;          redo A;
2084        } else {        } else {
2085            !!!cp (210);
2086          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2087              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2088          ## Stay in the state          ## Stay in the state
2089          !!!next-input-character;          !!!next-input-character;
2090          redo A;          redo A;
2091        }        }
2092      } elsif ($self->{state} eq 'DOCTYPE system identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
2093        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
2094          $self->{state} = 'after DOCTYPE system identifier';          !!!cp (211);
2095            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2096            !!!next-input-character;
2097            redo A;
2098          } elsif ($self->{next_char} == 0x003E) { # >
2099            !!!cp (212);
2100            !!!parse-error (type => 'unclosed PUBLIC literal');
2101    
2102            $self->{state} = DATA_STATE;
2103          !!!next-input-character;          !!!next-input-character;
2104    
2105            $self->{current_token}->{quirks} = 1;
2106            !!!emit ($self->{current_token}); # DOCTYPE
2107    
2108          redo A;          redo A;
2109        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2110            !!!cp (213);
2111          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2112    
2113          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2114          ## reconsume          ## reconsume
2115    
2116          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2117          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2118    
2119          redo A;          redo A;
2120        } else {        } else {
2121            !!!cp (214);
2122          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2123              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2124          ## Stay in the state          ## Stay in the state
2125          !!!next-input-character;          !!!next-input-character;
2126          redo A;          redo A;
2127        }        }
2128      } elsif ($self->{state} eq 'after DOCTYPE system identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2129        if ({        if ({
2130              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2131              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2132            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2133            !!!cp (215);
2134          ## Stay in the state          ## Stay in the state
2135          !!!next-input-character;          !!!next-input-character;
2136          redo A;          redo A;
2137        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2138          $self->{state} = 'data';          !!!cp (216);
2139            $self->{state} = DATA_STATE;
2140          !!!next-input-character;          !!!next-input-character;
2141    
2142          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2143    
2144          redo A;          redo A;
2145        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2146            !!!cp (217);
2147          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2148    
2149          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2150          ## reconsume          ## reconsume
2151    
2152          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2153          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2154    
2155          redo A;          redo A;
2156        } else {        } else {
2157            !!!cp (218);
2158          !!!parse-error (type => 'string after SYSTEM literal');          !!!parse-error (type => 'string after SYSTEM literal');
2159          $self->{state} = 'bogus DOCTYPE';          #$self->{current_token}->{quirks} = 1;
2160    
2161            $self->{state} = BOGUS_DOCTYPE_STATE;
2162          !!!next-input-character;          !!!next-input-character;
2163          redo A;          redo A;
2164        }        }
2165      } elsif ($self->{state} eq 'bogus DOCTYPE') {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
2166        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2167          $self->{state} = 'data';          !!!cp (219);
2168            $self->{state} = DATA_STATE;
2169          !!!next-input-character;          !!!next-input-character;
2170    
         delete $self->{current_token}->{correct};  
2171          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2172    
2173          redo A;          redo A;
2174        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2175            !!!cp (220);
2176          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2177          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2178          ## reconsume          ## reconsume
2179    
         delete $self->{current_token}->{correct};  
2180          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2181    
2182          redo A;          redo A;
2183        } else {        } else {
2184            !!!cp (221);
2185          ## Stay in the state          ## Stay in the state
2186          !!!next-input-character;          !!!next-input-character;
2187          redo A;          redo A;
# Line 1606  sub _get_next_token ($) { Line 2194  sub _get_next_token ($) {
2194    die "$0: _get_next_token: unexpected case";    die "$0: _get_next_token: unexpected case";
2195  } # _get_next_token  } # _get_next_token
2196    
2197  sub _tokenize_attempt_to_consume_an_entity ($$) {  sub _tokenize_attempt_to_consume_an_entity ($$$) {
2198    my ($self, $in_attr) = @_;    my ($self, $in_attr, $additional) = @_;
2199    
2200    if ({    if ({
2201         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
2202         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
2203        }->{$self->{next_input_character}}) {         $additional => 1,
2204          }->{$self->{next_char}}) {
2205        !!!cp (1001);
2206      ## Don't consume      ## Don't consume
2207      ## No error      ## No error
2208      return undef;      return undef;
2209    } elsif ($self->{next_input_character} == 0x0023) { # #    } elsif ($self->{next_char} == 0x0023) { # #
2210      !!!next-input-character;      !!!next-input-character;
2211      if ($self->{next_input_character} == 0x0078 or # x      if ($self->{next_char} == 0x0078 or # x
2212          $self->{next_input_character} == 0x0058) { # X          $self->{next_char} == 0x0058) { # X
2213        my $code;        my $code;
2214        X: {        X: {
2215          my $x_char = $self->{next_input_character};          my $x_char = $self->{next_char};
2216          !!!next-input-character;          !!!next-input-character;
2217          if (0x0030 <= $self->{next_input_character} and          if (0x0030 <= $self->{next_char} and
2218              $self->{next_input_character} <= 0x0039) { # 0..9              $self->{next_char} <= 0x0039) { # 0..9
2219              !!!cp (1002);
2220            $code ||= 0;            $code ||= 0;
2221            $code *= 0x10;            $code *= 0x10;
2222            $code += $self->{next_input_character} - 0x0030;            $code += $self->{next_char} - 0x0030;
2223            redo X;            redo X;
2224          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
2225                   $self->{next_input_character} <= 0x0066) { # a..f                   $self->{next_char} <= 0x0066) { # a..f
2226              !!!cp (1003);
2227            $code ||= 0;            $code ||= 0;
2228            $code *= 0x10;            $code *= 0x10;
2229            $code += $self->{next_input_character} - 0x0060 + 9;            $code += $self->{next_char} - 0x0060 + 9;
2230            redo X;            redo X;
2231          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
2232                   $self->{next_input_character} <= 0x0046) { # A..F                   $self->{next_char} <= 0x0046) { # A..F
2233              !!!cp (1004);
2234            $code ||= 0;            $code ||= 0;
2235            $code *= 0x10;            $code *= 0x10;
2236            $code += $self->{next_input_character} - 0x0040 + 9;            $code += $self->{next_char} - 0x0040 + 9;
2237            redo X;            redo X;
2238          } elsif (not defined $code) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
2239              !!!cp (1005);
2240            !!!parse-error (type => 'bare hcro');            !!!parse-error (type => 'bare hcro');
2241            $self->{next_input_character} = 0x0023; # #            !!!back-next-input-character ($x_char, $self->{next_char});
2242            !!!back-next-input-character ($x_char);            $self->{next_char} = 0x0023; # #
2243            return undef;            return undef;
2244          } elsif ($self->{next_input_character} == 0x003B) { # ;          } elsif ($self->{next_char} == 0x003B) { # ;
2245              !!!cp (1006);
2246            !!!next-input-character;            !!!next-input-character;
2247          } else {          } else {
2248              !!!cp (1007);
2249            !!!parse-error (type => 'no refc');            !!!parse-error (type => 'no refc');
2250          }          }
2251    
2252          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2253              !!!cp (1008);
2254            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);
2255            $code = 0xFFFD;            $code = 0xFFFD;
2256          } elsif ($code > 0x10FFFF) {          } elsif ($code > 0x10FFFF) {
2257              !!!cp (1009);
2258            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);
2259            $code = 0xFFFD;            $code = 0xFFFD;
2260          } elsif ($code == 0x000D) {          } elsif ($code == 0x000D) {
2261              !!!cp (1010);
2262            !!!parse-error (type => 'CR character reference');            !!!parse-error (type => 'CR character reference');
2263            $code = 0x000A;            $code = 0x000A;
2264          } elsif (0x80 <= $code and $code <= 0x9F) {          } elsif (0x80 <= $code and $code <= 0x9F) {
2265              !!!cp (1011);
2266            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);
2267            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
2268          }          }
2269    
2270          return {type => 'character', data => chr $code};          return {type => CHARACTER_TOKEN, data => chr $code,
2271                    has_reference => 1};
2272        } # X        } # X
2273      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_char} and
2274               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_char} <= 0x0039) { # 0..9
2275        my $code = $self->{next_input_character} - 0x0030;        my $code = $self->{next_char} - 0x0030;
2276        !!!next-input-character;        !!!next-input-character;
2277                
2278        while (0x0030 <= $self->{next_input_character} and        while (0x0030 <= $self->{next_char} and
2279                  $self->{next_input_character} <= 0x0039) { # 0..9                  $self->{next_char} <= 0x0039) { # 0..9
2280            !!!cp (1012);
2281          $code *= 10;          $code *= 10;
2282          $code += $self->{next_input_character} - 0x0030;          $code += $self->{next_char} - 0x0030;
2283                    
2284          !!!next-input-character;          !!!next-input-character;
2285        }        }
2286    
2287        if ($self->{next_input_character} == 0x003B) { # ;        if ($self->{next_char} == 0x003B) { # ;
2288            !!!cp (1013);
2289          !!!next-input-character;          !!!next-input-character;
2290        } else {        } else {
2291            !!!cp (1014);
2292          !!!parse-error (type => 'no refc');          !!!parse-error (type => 'no refc');
2293        }        }
2294    
2295        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2296            !!!cp (1015);
2297          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);
2298          $code = 0xFFFD;          $code = 0xFFFD;
2299        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
2300            !!!cp (1016);
2301          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);
2302          $code = 0xFFFD;          $code = 0xFFFD;
2303        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
2304            !!!cp (1017);
2305          !!!parse-error (type => 'CR character reference');          !!!parse-error (type => 'CR character reference');
2306          $code = 0x000A;          $code = 0x000A;
2307        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
2308            !!!cp (1018);
2309          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);
2310          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
2311        }        }
2312                
2313        return {type => 'character', data => chr $code};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1};
2314      } else {      } else {
2315          !!!cp (1019);
2316        !!!parse-error (type => 'bare nero');        !!!parse-error (type => 'bare nero');
2317        !!!back-next-input-character ($self->{next_input_character});        !!!back-next-input-character ($self->{next_char});
2318        $self->{next_input_character} = 0x0023; # #        $self->{next_char} = 0x0023; # #
2319        return undef;        return undef;
2320      }      }
2321    } elsif ((0x0041 <= $self->{next_input_character} and    } elsif ((0x0041 <= $self->{next_char} and
2322              $self->{next_input_character} <= 0x005A) or              $self->{next_char} <= 0x005A) or
2323             (0x0061 <= $self->{next_input_character} and             (0x0061 <= $self->{next_char} and
2324              $self->{next_input_character} <= 0x007A)) {              $self->{next_char} <= 0x007A)) {
2325      my $entity_name = chr $self->{next_input_character};      my $entity_name = chr $self->{next_char};
2326      !!!next-input-character;      !!!next-input-character;
2327    
2328      my $value = $entity_name;      my $value = $entity_name;
2329      my $match;      my $match = 0;
2330      require Whatpm::_NamedEntityList;      require Whatpm::_NamedEntityList;
2331      our $EntityChar;      our $EntityChar;
2332    
2333      while (length $entity_name < 10 and      while (length $entity_name < 10 and
2334             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
2335             ((0x0041 <= $self->{next_input_character} and # a             ((0x0041 <= $self->{next_char} and # a
2336               $self->{next_input_character} <= 0x005A) or # x               $self->{next_char} <= 0x005A) or # x
2337              (0x0061 <= $self->{next_input_character} and # a              (0x0061 <= $self->{next_char} and # a
2338               $self->{next_input_character} <= 0x007A) or # z               $self->{next_char} <= 0x007A) or # z
2339              (0x0030 <= $self->{next_input_character} and # 0              (0x0030 <= $self->{next_char} and # 0
2340               $self->{next_input_character} <= 0x0039) or # 9               $self->{next_char} <= 0x0039) or # 9
2341              $self->{next_input_character} == 0x003B)) { # ;              $self->{next_char} == 0x003B)) { # ;
2342        $entity_name .= chr $self->{next_input_character};        $entity_name .= chr $self->{next_char};
2343        if (defined $EntityChar->{$entity_name}) {        if (defined $EntityChar->{$entity_name}) {
2344          if ($self->{next_input_character} == 0x003B) { # ;          if ($self->{next_char} == 0x003B) { # ;
2345              !!!cp (1020);
2346            $value = $EntityChar->{$entity_name};            $value = $EntityChar->{$entity_name};
2347            $match = 1;            $match = 1;
2348            !!!next-input-character;            !!!next-input-character;
2349            last;            last;
2350          } elsif (not $in_attr) {          } else {
2351              !!!cp (1021);
2352            $value = $EntityChar->{$entity_name};            $value = $EntityChar->{$entity_name};
2353            $match = -1;            $match = -1;
2354          } else {            !!!next-input-character;
           $value .= chr $self->{next_input_character};  
2355          }          }
2356        } else {        } else {
2357          $value .= chr $self->{next_input_character};          !!!cp (1022);
2358            $value .= chr $self->{next_char};
2359            $match *= 2;
2360            !!!next-input-character;
2361        }        }
       !!!next-input-character;  
2362      }      }
2363            
2364      if ($match > 0) {      if ($match > 0) {
2365        return {type => 'character', data => $value};        !!!cp (1023);
2366          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};
2367      } elsif ($match < 0) {      } elsif ($match < 0) {
2368        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc');
2369        return {type => 'character', data => $value};        if ($in_attr and $match < -1) {
2370            !!!cp (1024);
2371            return {type => CHARACTER_TOKEN, data => '&'.$entity_name};
2372          } else {
2373            !!!cp (1025);
2374            return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};
2375          }
2376      } else {      } else {
2377          !!!cp (1026);
2378        !!!parse-error (type => 'bare ero');        !!!parse-error (type => 'bare ero');
2379        ## NOTE: No characters are consumed in the spec.        ## NOTE: "No characters are consumed" in the spec.
2380        return {type => 'character', data => '&'.$value};        return {type => CHARACTER_TOKEN, data => '&'.$value};
2381      }      }
2382    } else {    } else {
2383        !!!cp (1027);
2384      ## no characters are consumed      ## no characters are consumed
2385      !!!parse-error (type => 'bare ero');      !!!parse-error (type => 'bare ero');
2386      return undef;      return undef;
# Line 1799  sub _construct_tree ($) { Line 2420  sub _construct_tree ($) {
2420        
2421    !!!next-token;    !!!next-token;
2422    
2423    $self->{insertion_mode} = 'before head';    $self->{insertion_mode} = BEFORE_HEAD_IM;
2424    undef $self->{form_element};    undef $self->{form_element};
2425    undef $self->{head_element};    undef $self->{head_element};
2426    $self->{open_elements} = [];    $self->{open_elements} = [];
# Line 1813  sub _construct_tree ($) { Line 2434  sub _construct_tree ($) {
2434  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
2435    my $self = shift;    my $self = shift;
2436    INITIAL: {    INITIAL: {
2437      if ($token->{type} eq 'DOCTYPE') {      if ($token->{type} == DOCTYPE_TOKEN) {
2438        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
2439        ## error, switch to a conformance checking mode for another        ## error, switch to a conformance checking mode for another
2440        ## language.        ## language.
# Line 1823  sub _tree_construction_initial ($) { Line 2444  sub _tree_construction_initial ($) {
2444        if (not defined $token->{name} or # <!DOCTYPE>        if (not defined $token->{name} or # <!DOCTYPE>
2445            defined $token->{public_identifier} or            defined $token->{public_identifier} or
2446            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
2447            !!!cp ('t1');
2448          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5');
2449        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
2450            !!!cp ('t2');
2451          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)
2452          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5');
2453          } else {
2454            !!!cp ('t3');
2455        }        }
2456                
2457        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
# Line 1839  sub _tree_construction_initial ($) { Line 2464  sub _tree_construction_initial ($) {
2464        ## ISSUE: internalSubset = null??        ## ISSUE: internalSubset = null??
2465        $self->{document}->append_child ($doctype);        $self->{document}->append_child ($doctype);
2466                
2467        if (not $token->{correct} or $doctype_name ne 'HTML') {        if ($token->{quirks} or $doctype_name ne 'HTML') {
2468            !!!cp ('t4');
2469          $self->{document}->manakai_compat_mode ('quirks');          $self->{document}->manakai_compat_mode ('quirks');
2470        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{public_identifier}) {
2471          my $pubid = $token->{public_identifier};          my $pubid = $token->{public_identifier};
# Line 1893  sub _tree_construction_initial ($) { Line 2519  sub _tree_construction_initial ($) {
2519            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,
2520            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,
2521            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,
2522              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//EN" => 1,
2523              "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//EN" => 1,
2524              "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//EN" => 1,
2525            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,
2526            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,
2527            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,
# Line 1915  sub _tree_construction_initial ($) { Line 2544  sub _tree_construction_initial ($) {
2544            "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,            "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,
2545            "HTML" => 1,            "HTML" => 1,
2546          }->{$pubid}) {          }->{$pubid}) {
2547              !!!cp ('t5');
2548            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
2549          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or
2550                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {
2551            if (defined $token->{system_identifier}) {            if (defined $token->{system_identifier}) {
2552                !!!cp ('t6');
2553              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
2554            } else {            } else {
2555                !!!cp ('t7');
2556              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
2557            }            }
2558          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 Frameset//EN" or          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 Frameset//EN" or
2559                   $pubid eq "-//W3C//DTD XHTML 1.0 Transitional//EN") {                   $pubid eq "-//W3C//DTD XHTML 1.0 Transitional//EN") {
2560              !!!cp ('t8');
2561            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
2562            } else {
2563              !!!cp ('t9');
2564          }          }
2565          } else {
2566            !!!cp ('t10');
2567        }        }
2568        if (defined $token->{system_identifier}) {        if (defined $token->{system_identifier}) {
2569          my $sysid = $token->{system_identifier};          my $sysid = $token->{system_identifier};
2570          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
2571          if ($sysid eq "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd") {          if ($sysid eq "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd") {
2572            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
2573              !!!cp ('t11');
2574            } else {
2575              !!!cp ('t12');
2576          }          }
2577          } else {
2578            !!!cp ('t13');
2579        }        }
2580                
2581        ## Go to the root element phase.        ## Go to the root element phase.
2582        !!!next-token;        !!!next-token;
2583        return;        return;
2584      } elsif ({      } elsif ({
2585                'start tag' => 1,                START_TAG_TOKEN, 1,
2586                'end tag' => 1,                END_TAG_TOKEN, 1,
2587                'end-of-file' => 1,                END_OF_FILE_TOKEN, 1,
2588               }->{$token->{type}}) {               }->{$token->{type}}) {
2589          !!!cp ('t14');
2590        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE');
2591        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
2592        ## Go to the root element phase        ## Go to the root element phase
2593        ## reprocess        ## reprocess
2594        return;        return;
2595      } elsif ($token->{type} eq 'character') {      } elsif ($token->{type} == CHARACTER_TOKEN) {
2596        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2597          ## Ignore the token          ## Ignore the token
2598    
2599          unless (length $token->{data}) {          unless (length $token->{data}) {
2600              !!!cp ('t15');
2601            ## Stay in the phase            ## Stay in the phase
2602            !!!next-token;            !!!next-token;
2603            redo INITIAL;            redo INITIAL;
2604            } else {
2605              !!!cp ('t16');
2606          }          }
2607          } else {
2608            !!!cp ('t17');
2609        }        }
2610    
2611        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE');
# Line 1965  sub _tree_construction_initial ($) { Line 2613  sub _tree_construction_initial ($) {
2613        ## Go to the root element phase        ## Go to the root element phase
2614        ## reprocess        ## reprocess
2615        return;        return;
2616      } elsif ($token->{type} eq 'comment') {      } elsif ($token->{type} == COMMENT_TOKEN) {
2617          !!!cp ('t18');
2618        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
2619        $self->{document}->append_child ($comment);        $self->{document}->append_child ($comment);
2620                
# Line 1973  sub _tree_construction_initial ($) { Line 2622  sub _tree_construction_initial ($) {
2622        !!!next-token;        !!!next-token;
2623        redo INITIAL;        redo INITIAL;
2624      } else {      } else {
2625        die "$0: $token->{type}: Unknown token";        die "$0: $token->{type}: Unknown token type";
2626      }      }
2627    } # INITIAL    } # INITIAL
2628    
2629      die "$0: _tree_construction_initial: This should be never reached";
2630  } # _tree_construction_initial  } # _tree_construction_initial
2631    
2632  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
2633    my $self = shift;    my $self = shift;
2634        
2635    B: {    B: {
2636        if ($token->{type} eq 'DOCTYPE') {        if ($token->{type} == DOCTYPE_TOKEN) {
2637            !!!cp ('t19');
2638          !!!parse-error (type => 'in html:#DOCTYPE');          !!!parse-error (type => 'in html:#DOCTYPE');
2639          ## Ignore the token          ## Ignore the token
2640          ## Stay in the phase          ## Stay in the phase
2641          !!!next-token;          !!!next-token;
2642          redo B;          redo B;
2643        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{type} == COMMENT_TOKEN) {
2644            !!!cp ('t20');
2645          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
2646          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
2647          ## Stay in the phase          ## Stay in the phase
2648          !!!next-token;          !!!next-token;
2649          redo B;          redo B;
2650        } elsif ($token->{type} eq 'character') {        } elsif ($token->{type} == CHARACTER_TOKEN) {
2651          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2652            ## Ignore the token.            ## Ignore the token.
2653    
2654            unless (length $token->{data}) {            unless (length $token->{data}) {
2655                !!!cp ('t21');
2656              ## Stay in the phase              ## Stay in the phase
2657              !!!next-token;              !!!next-token;
2658              redo B;              redo B;
2659              } else {
2660                !!!cp ('t22');
2661            }            }
2662            } else {
2663              !!!cp ('t23');
2664            }
2665    
2666            $self->{application_cache_selection}->(undef);
2667    
2668            #
2669          } elsif ($token->{type} == START_TAG_TOKEN) {
2670            if ($token->{tag_name} eq 'html' and
2671                $token->{attributes}->{manifest}) {
2672              !!!cp ('t24');
2673              $self->{application_cache_selection}
2674                   ->($token->{attributes}->{manifest}->{value});
2675              ## ISSUE: No relative reference resolution?
2676            } else {
2677              !!!cp ('t25');
2678              $self->{application_cache_selection}->(undef);
2679          }          }
2680    
2681            ## ISSUE: There is an issue in the spec
2682          #          #
2683        } elsif ({        } elsif ({
2684                  'start tag' => 1,                  END_TAG_TOKEN, 1,
2685                  'end tag' => 1,                  END_OF_FILE_TOKEN, 1,
                 'end-of-file' => 1,  
2686                 }->{$token->{type}}) {                 }->{$token->{type}}) {
2687            !!!cp ('t26');
2688            $self->{application_cache_selection}->(undef);
2689    
2690          ## ISSUE: There is an issue in the spec          ## ISSUE: There is an issue in the spec
2691          #          #
2692        } else {        } else {
2693          die "$0: $token->{type}: Unknown token";          die "$0: $token->{type}: Unknown token type";
2694        }        }
2695    
2696        my $root_element; !!!create-element ($root_element, 'html');        my $root_element; !!!create-element ($root_element, 'html');
2697        $self->{document}->append_child ($root_element);        $self->{document}->append_child ($root_element);
2698        push @{$self->{open_elements}}, [$root_element, 'html'];        push @{$self->{open_elements}}, [$root_element, 'html'];
# Line 2022  sub _tree_construction_root_element ($) Line 2700  sub _tree_construction_root_element ($)
2700        #redo B;        #redo B;
2701        return; ## Go to the main phase.        return; ## Go to the main phase.
2702    } # B    } # B
2703    
2704      die "$0: _tree_construction_root_element: This should never be reached";
2705  } # _tree_construction_root_element  } # _tree_construction_root_element
2706    
2707  sub _reset_insertion_mode ($) {  sub _reset_insertion_mode ($) {
# Line 2046  sub _reset_insertion_mode ($) { Line 2726  sub _reset_insertion_mode ($) {
2726          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
2727            if ($self->{inner_html_node}->[1] eq 'td' or            if ($self->{inner_html_node}->[1] eq 'td' or
2728                $self->{inner_html_node}->[1] eq 'th') {                $self->{inner_html_node}->[1] eq 'th') {
2729                !!!cp ('t27');
2730              #              #
2731            } else {            } else {
2732                !!!cp ('t28');
2733              $node = $self->{inner_html_node};              $node = $self->{inner_html_node};
2734            }            }
2735          }          }
# Line 2055  sub _reset_insertion_mode ($) { Line 2737  sub _reset_insertion_mode ($) {
2737            
2738        ## Step 4..13        ## Step 4..13
2739        my $new_mode = {        my $new_mode = {
2740                        select => 'in select',                        select => IN_SELECT_IM,
2741                        td => 'in cell',                        td => IN_CELL_IM,
2742                        th => 'in cell',                        th => IN_CELL_IM,
2743                        tr => 'in row',                        tr => IN_ROW_IM,
2744                        tbody => 'in table body',                        tbody => IN_TABLE_BODY_IM,
2745                        thead => 'in table head',                        thead => IN_TABLE_BODY_IM,
2746                        tfoot => 'in table foot',                        tfoot => IN_TABLE_BODY_IM,
2747                        caption => 'in caption',                        caption => IN_CAPTION_IM,
2748                        colgroup => 'in column group',                        colgroup => IN_COLUMN_GROUP_IM,
2749                        table => 'in table',                        table => IN_TABLE_IM,
2750                        head => 'in body', # not in head!                        head => IN_BODY_IM, # not in head!
2751                        body => 'in body',                        body => IN_BODY_IM,
2752                        frameset => 'in frameset',                        frameset => IN_FRAMESET_IM,
2753                       }->{$node->[1]};                       }->{$node->[1]};
2754        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
2755                
2756        ## Step 14        ## Step 14
2757        if ($node->[1] eq 'html') {        if ($node->[1] eq 'html') {
2758          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
2759            $self->{insertion_mode} = 'before head';            !!!cp ('t29');
2760              $self->{insertion_mode} = BEFORE_HEAD_IM;
2761          } else {          } else {
2762            $self->{insertion_mode} = 'after head';            !!!cp ('t30');
2763              $self->{insertion_mode} = AFTER_HEAD_IM;
2764          }          }
2765          return;          return;
2766          } else {
2767            !!!cp ('t31');
2768        }        }
2769                
2770        ## Step 15        ## Step 15
2771        $self->{insertion_mode} = 'in body' and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
2772                
2773        ## Step 16        ## Step 16
2774        $i--;        $i--;
# Line 2091  sub _reset_insertion_mode ($) { Line 2777  sub _reset_insertion_mode ($) {
2777        ## Step 17        ## Step 17
2778        redo S3;        redo S3;
2779      } # S3      } # S3
2780    
2781      die "$0: _reset_insertion_mode: This line should never be reached";
2782  } # _reset_insertion_mode  } # _reset_insertion_mode
2783    
2784  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
2785    my $self = shift;    my $self = shift;
2786    
   my $previous_insertion_mode;  
   
2787    my $active_formatting_elements = [];    my $active_formatting_elements = [];
2788    
2789    my $reconstruct_active_formatting_elements = sub { # MUST    my $reconstruct_active_formatting_elements = sub { # MUST
# Line 2114  sub _tree_construction_main ($) { Line 2800  sub _tree_construction_main ($) {
2800      return if $entry->[0] eq '#marker';      return if $entry->[0] eq '#marker';
2801      for (@{$self->{open_elements}}) {      for (@{$self->{open_elements}}) {
2802        if ($entry->[0] eq $_->[0]) {        if ($entry->[0] eq $_->[0]) {
2803            !!!cp ('t32');
2804          return;          return;
2805        }        }
2806      }      }
# Line 2128  sub _tree_construction_main ($) { Line 2815  sub _tree_construction_main ($) {
2815    
2816        ## Step 6        ## Step 6
2817        if ($entry->[0] eq '#marker') {        if ($entry->[0] eq '#marker') {
2818            !!!cp ('t33');
2819          #          #
2820        } else {        } else {
2821          my $in_open_elements;          my $in_open_elements;
2822          OE: for (@{$self->{open_elements}}) {          OE: for (@{$self->{open_elements}}) {
2823            if ($entry->[0] eq $_->[0]) {            if ($entry->[0] eq $_->[0]) {
2824                !!!cp ('t33');
2825              $in_open_elements = 1;              $in_open_elements = 1;
2826              last OE;              last OE;
2827            }            }
2828          }          }
2829          if ($in_open_elements) {          if ($in_open_elements) {
2830              !!!cp ('t34');
2831            #            #
2832          } else {          } else {
2833              !!!cp ('t35');
2834            redo S4;            redo S4;
2835          }          }
2836        }        }
# Line 2162  sub _tree_construction_main ($) { Line 2853  sub _tree_construction_main ($) {
2853    
2854        ## Step 11        ## Step 11
2855        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {
2856            !!!cp ('t36');
2857          ## Step 7'          ## Step 7'
2858          $i++;          $i++;
2859          $entry = $active_formatting_elements->[$i];          $entry = $active_formatting_elements->[$i];
2860                    
2861          redo S7;          redo S7;
2862        }        }
2863    
2864          !!!cp ('t37');
2865      } # S7      } # S7
2866    }; # $reconstruct_active_formatting_elements    }; # $reconstruct_active_formatting_elements
2867    
2868    my $clear_up_to_marker = sub {    my $clear_up_to_marker = sub {
2869      for (reverse 0..$#$active_formatting_elements) {      for (reverse 0..$#$active_formatting_elements) {
2870        if ($active_formatting_elements->[$_]->[0] eq '#marker') {        if ($active_formatting_elements->[$_]->[0] eq '#marker') {
2871            !!!cp ('t38');
2872          splice @$active_formatting_elements, $_;          splice @$active_formatting_elements, $_;
2873          return;          return;
2874        }        }
2875      }      }
2876    
2877        !!!cp ('t39');
2878    }; # $clear_up_to_marker    }; # $clear_up_to_marker
2879    
2880    my $parse_rcdata = sub ($$) {    my $parse_rcdata = sub ($$) {
# Line 2192  sub _tree_construction_main ($) { Line 2889  sub _tree_construction_main ($) {
2889      $insert->($el); # /context node/->append_child ($el)      $insert->($el); # /context node/->append_child ($el)
2890    
2891      ## Step 3      ## Step 3
2892      $self->{content_model_flag} = $content_model_flag; # CDATA or RCDATA      $self->{content_model} = $content_model_flag; # CDATA or RCDATA
2893      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
2894    
2895      ## Step 4      ## Step 4
2896      my $text = '';      my $text = '';
2897      !!!next-token;      !!!next-token;
2898      while ($token->{type} eq 'character') { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
2899          !!!cp ('t40');
2900        $text .= $token->{data};        $text .= $token->{data};
2901        !!!next-token;        !!!next-token;
2902      }      }
2903    
2904      ## Step 5      ## Step 5
2905      if (length $text) {      if (length $text) {
2906          !!!cp ('t41');
2907        my $text = $self->{document}->create_text_node ($text);        my $text = $self->{document}->create_text_node ($text);
2908        $el->append_child ($text);        $el->append_child ($text);
2909      }      }
2910    
2911      ## Step 6      ## Step 6
2912      $self->{content_model_flag} = 'PCDATA';      $self->{content_model} = PCDATA_CONTENT_MODEL;
2913    
2914      ## Step 7      ## Step 7
2915      if ($token->{type} eq 'end tag' and $token->{tag_name} eq $start_tag_name) {      if ($token->{type} == END_TAG_TOKEN and
2916            $token->{tag_name} eq $start_tag_name) {
2917          !!!cp ('t42');
2918        ## Ignore the token        ## Ignore the token
2919        } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {
2920          !!!cp ('t43');
2921          !!!parse-error (type => 'in CDATA:#'.$token->{type});
2922        } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
2923          !!!cp ('t44');
2924          !!!parse-error (type => 'in RCDATA:#'.$token->{type});
2925      } else {      } else {
2926        !!!parse-error (type => 'in '.$content_model_flag.':#'.$token->{type});        die "$0: $content_model_flag in parse_rcdata";
2927      }      }
2928      !!!next-token;      !!!next-token;
2929    }; # $parse_rcdata    }; # $parse_rcdata
# Line 2227  sub _tree_construction_main ($) { Line 2934  sub _tree_construction_main ($) {
2934      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, 'script', $token->{attributes});
2935      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
2936    
2937      $self->{content_model_flag} = 'CDATA';      $self->{content_model} = CDATA_CONTENT_MODEL;
2938      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
2939            
2940      my $text = '';      my $text = '';
2941      !!!next-token;      !!!next-token;
2942      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) {
2943          !!!cp ('t45');
2944        $text .= $token->{data};        $text .= $token->{data};
2945        !!!next-token;        !!!next-token;
2946      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
2947      if (length $text) {      if (length $text) {
2948          !!!cp ('t46');
2949        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
2950      }      }
2951                                
2952      $self->{content_model_flag} = 'PCDATA';      $self->{content_model} = PCDATA_CONTENT_MODEL;
2953    
2954      if ($token->{type} eq 'end tag' and      if ($token->{type} == END_TAG_TOKEN and
2955          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
2956          !!!cp ('t47');
2957        ## Ignore the token        ## Ignore the token
2958      } else {      } else {
2959          !!!cp ('t48');
2960        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!parse-error (type => 'in CDATA:#'.$token->{type});
2961        ## ISSUE: And ignore?        ## ISSUE: And ignore?
2962        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
2963      }      }
2964            
2965      if (defined $self->{inner_html_node}) {      if (defined $self->{inner_html_node}) {
2966          !!!cp ('t49');
2967        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
2968      } else {      } else {
2969          !!!cp ('t50');
2970        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
2971        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
2972    
# Line 2276  sub _tree_construction_main ($) { Line 2989  sub _tree_construction_main ($) {
2989        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
2990        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
2991          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {
2992              !!!cp ('t51');
2993            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
2994            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
2995            last AFE;            last AFE;
2996          } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {          } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {
2997              !!!cp ('t52');
2998            last AFE;            last AFE;
2999          }          }
3000        } # AFE        } # AFE
3001        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3002            !!!cp ('t53');
3003          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!parse-error (type => 'unmatched end tag:'.$tag_name);
3004          ## Ignore the token          ## Ignore the token
3005          !!!next-token;          !!!next-token;
# Line 2296  sub _tree_construction_main ($) { Line 3012  sub _tree_construction_main ($) {
3012          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3013          if ($node->[0] eq $formatting_element->[0]) {          if ($node->[0] eq $formatting_element->[0]) {
3014            if ($in_scope) {            if ($in_scope) {
3015                !!!cp ('t54');
3016              $formatting_element_i_in_open = $_;              $formatting_element_i_in_open = $_;
3017              last INSCOPE;              last INSCOPE;
3018            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3019                !!!cp ('t55');
3020              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3021              ## Ignore the token              ## Ignore the token
3022              !!!next-token;              !!!next-token;
# Line 2308  sub _tree_construction_main ($) { Line 3026  sub _tree_construction_main ($) {
3026                    table => 1, caption => 1, td => 1, th => 1,                    table => 1, caption => 1, td => 1, th => 1,
3027                    button => 1, marquee => 1, object => 1, html => 1,                    button => 1, marquee => 1, object => 1, html => 1,
3028                   }->{$node->[1]}) {                   }->{$node->[1]}) {
3029              !!!cp ('t56');
3030            $in_scope = 0;            $in_scope = 0;
3031          }          }
3032        } # INSCOPE        } # INSCOPE
3033        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3034            !!!cp ('t57');
3035          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3036          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3037          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
3038          return;          return;
3039        }        }
3040        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3041            !!!cp ('t58');
3042          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3043        }        }
3044                
# Line 2330  sub _tree_construction_main ($) { Line 3051  sub _tree_construction_main ($) {
3051              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3052              ($special_category->{$node->[1]} or              ($special_category->{$node->[1]} or
3053               $scoping_category->{$node->[1]})) {               $scoping_category->{$node->[1]})) {
3054              !!!cp ('t59');
3055            $furthest_block = $node;            $furthest_block = $node;
3056            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
3057          } elsif ($node->[0] eq $formatting_element->[0]) {          } elsif ($node->[0] eq $formatting_element->[0]) {
3058              !!!cp ('t60');
3059            last OE;            last OE;
3060          }          }
3061        } # OE        } # OE
3062                
3063        ## Step 3        ## Step 3
3064        unless (defined $furthest_block) { # MUST        unless (defined $furthest_block) { # MUST
3065            !!!cp ('t61');
3066          splice @{$self->{open_elements}}, $formatting_element_i_in_open;          splice @{$self->{open_elements}}, $formatting_element_i_in_open;
3067          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;
3068          !!!next-token;          !!!next-token;
# Line 2351  sub _tree_construction_main ($) { Line 3075  sub _tree_construction_main ($) {
3075        ## Step 5        ## Step 5
3076        my $furthest_block_parent = $furthest_block->[0]->parent_node;        my $furthest_block_parent = $furthest_block->[0]->parent_node;
3077        if (defined $furthest_block_parent) {        if (defined $furthest_block_parent) {
3078            !!!cp ('t62');
3079          $furthest_block_parent->remove_child ($furthest_block->[0]);          $furthest_block_parent->remove_child ($furthest_block->[0]);
3080        }        }
3081                
# Line 2373  sub _tree_construction_main ($) { Line 3098  sub _tree_construction_main ($) {
3098          S7S2: {          S7S2: {
3099            for (reverse 0..$#$active_formatting_elements) {            for (reverse 0..$#$active_formatting_elements) {
3100              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
3101                  !!!cp ('t63');
3102                $node_i_in_active = $_;                $node_i_in_active = $_;
3103                last S7S2;                last S7S2;
3104              }              }
# Line 2386  sub _tree_construction_main ($) { Line 3112  sub _tree_construction_main ($) {
3112                    
3113          ## Step 4          ## Step 4
3114          if ($last_node->[0] eq $furthest_block->[0]) {          if ($last_node->[0] eq $furthest_block->[0]) {
3115              !!!cp ('t64');
3116            $bookmark_prev_el = $node->[0];            $bookmark_prev_el = $node->[0];
3117          }          }
3118                    
3119          ## Step 5          ## Step 5
3120          if ($node->[0]->has_child_nodes ()) {          if ($node->[0]->has_child_nodes ()) {
3121              !!!cp ('t65');
3122            my $clone = [$node->[0]->clone_node (0), $node->[1]];            my $clone = [$node->[0]->clone_node (0), $node->[1]];
3123            $active_formatting_elements->[$node_i_in_active] = $clone;            $active_formatting_elements->[$node_i_in_active] = $clone;
3124            $self->{open_elements}->[$node_i_in_open] = $clone;            $self->{open_elements}->[$node_i_in_open] = $clone;
# Line 2425  sub _tree_construction_main ($) { Line 3153  sub _tree_construction_main ($) {
3153        my $i;        my $i;
3154        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3155          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {
3156              !!!cp ('t66');
3157            splice @$active_formatting_elements, $_, 1;            splice @$active_formatting_elements, $_, 1;
3158            $i-- and last AFE if defined $i;            $i-- and last AFE if defined $i;
3159          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {
3160              !!!cp ('t67');
3161            $i = $_;            $i = $_;
3162          }          }
3163        } # AFE        } # AFE
# Line 2437  sub _tree_construction_main ($) { Line 3167  sub _tree_construction_main ($) {
3167        undef $i;        undef $i;
3168        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3169          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {
3170              !!!cp ('t68');
3171            splice @{$self->{open_elements}}, $_, 1;            splice @{$self->{open_elements}}, $_, 1;
3172            $i-- and last OE if defined $i;            $i-- and last OE if defined $i;
3173          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {
3174              !!!cp ('t69');
3175            $i = $_;            $i = $_;
3176          }          }
3177        } # OE        } # OE
# Line 2467  sub _tree_construction_main ($) { Line 3199  sub _tree_construction_main ($) {
3199                             if ($self->{open_elements}->[$_]->[1] eq 'table') {                             if ($self->{open_elements}->[$_]->[1] eq 'table') {
3200                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3201                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3202                                   !!!cp ('t70');
3203                                 $foster_parent_element = $parent;                                 $foster_parent_element = $parent;
3204                                 $next_sibling = $self->{open_elements}->[$_]->[0];                                 $next_sibling = $self->{open_elements}->[$_]->[0];
3205                               } else {                               } else {
3206                                   !!!cp ('t71');
3207                                 $foster_parent_element                                 $foster_parent_element
3208                                   = $self->{open_elements}->[$_ - 1]->[0];                                   = $self->{open_elements}->[$_ - 1]->[0];
3209                               }                               }
# Line 2481  sub _tree_construction_main ($) { Line 3215  sub _tree_construction_main ($) {
3215                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
3216                             ($child, $next_sibling);                             ($child, $next_sibling);
3217                         } else {                         } else {
3218                             !!!cp ('t72');
3219                           $self->{open_elements}->[-1]->[0]->append_child ($child);                           $self->{open_elements}->[-1]->[0]->append_child ($child);
3220                         }                         }
3221    }; # $insert_to_foster    }; # $insert_to_foster
3222    
3223    my $in_body = sub {    my $insert;
3224      my $insert = shift;  
3225      if ($token->{type} eq 'start tag') {    B: {
3226        if ($token->{type} == DOCTYPE_TOKEN) {
3227          !!!cp ('t73');
3228          !!!parse-error (type => 'DOCTYPE in the middle');
3229          ## Ignore the token
3230          ## Stay in the phase
3231          !!!next-token;
3232          redo B;
3233        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
3234          if ($self->{insertion_mode} & AFTER_HTML_IMS) {
3235            !!!cp ('t74');
3236            #
3237          } else {
3238            ## Generate implied end tags
3239            if ({
3240                 dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,
3241                 tbody => 1, tfoot=> 1, thead => 1,
3242                }->{$self->{open_elements}->[-1]->[1]}) {
3243              !!!cp ('t75');
3244              !!!back-token;
3245              $token = {type => END_TAG_TOKEN, tag_name => $self->{open_elements}->[-1]->[1]};
3246              redo B;
3247            }
3248            
3249            if (@{$self->{open_elements}} > 2 or
3250                (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {
3251              !!!cp ('t76');
3252              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3253            } elsif (defined $self->{inner_html_node} and
3254                     @{$self->{open_elements}} > 1 and
3255                     $self->{open_elements}->[1]->[1] ne 'body') {
3256              !!!cp ('t77');
3257              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3258            } else {
3259              !!!cp ('t78');
3260            }
3261    
3262            ## ISSUE: There is an issue in the spec.
3263          }
3264    
3265          ## Stop parsing
3266          last B;
3267        } elsif ($token->{type} == START_TAG_TOKEN and
3268                 $token->{tag_name} eq 'html') {
3269          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
3270            !!!cp ('t79');
3271            ## Turn into the main phase
3272            !!!parse-error (type => 'after html:html');
3273            $self->{insertion_mode} = AFTER_BODY_IM;
3274          } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
3275            !!!cp ('t80');
3276            ## Turn into the main phase
3277            !!!parse-error (type => 'after html:html');
3278            $self->{insertion_mode} = AFTER_FRAMESET_IM;
3279          } else {
3280            !!!cp ('t81');
3281          }
3282    
3283    ## ISSUE: "aa<html>" is not a parse error.
3284    ## ISSUE: "<html>" in fragment is not a parse error.
3285          unless ($token->{first_start_tag}) {
3286            !!!cp ('t82');
3287            !!!parse-error (type => 'not first start tag');
3288          } else {
3289            !!!cp ('t83');
3290          }
3291          my $top_el = $self->{open_elements}->[0]->[0];
3292          for my $attr_name (keys %{$token->{attributes}}) {
3293            unless ($top_el->has_attribute_ns (undef, $attr_name)) {
3294              !!!cp ('t84');
3295              $top_el->set_attribute_ns
3296                (undef, [undef, $attr_name],
3297                 $token->{attributes}->{$attr_name}->{value});
3298            }
3299          }
3300          !!!next-token;
3301          redo B;
3302        } elsif ($token->{type} == COMMENT_TOKEN) {
3303          my $comment = $self->{document}->create_comment ($token->{data});
3304          if ($self->{insertion_mode} & AFTER_HTML_IMS) {
3305            !!!cp ('t85');
3306            $self->{document}->append_child ($comment);
3307          } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
3308            !!!cp ('t86');
3309            $self->{open_elements}->[0]->[0]->append_child ($comment);
3310          } else {
3311            !!!cp ('t87');
3312            $self->{open_elements}->[-1]->[0]->append_child ($comment);
3313          }
3314          !!!next-token;
3315          redo B;
3316        } elsif ($self->{insertion_mode} & HEAD_IMS) {
3317          if ($token->{type} == CHARACTER_TOKEN) {
3318            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3319              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3320              unless (length $token->{data}) {
3321                !!!cp ('t88');
3322                !!!next-token;
3323                redo B;
3324              }
3325            }
3326    
3327            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3328              !!!cp ('t89');
3329              ## As if <head>
3330              !!!create-element ($self->{head_element}, 'head');
3331              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3332              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3333    
3334              ## Reprocess in the "in head" insertion mode...
3335              pop @{$self->{open_elements}};
3336    
3337              ## Reprocess in the "after head" insertion mode...
3338            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3339              !!!cp ('t90');
3340              ## As if </noscript>
3341              pop @{$self->{open_elements}};
3342              !!!parse-error (type => 'in noscript:#character');
3343              
3344              ## Reprocess in the "in head" insertion mode...
3345              ## As if </head>
3346              pop @{$self->{open_elements}};
3347    
3348              ## Reprocess in the "after head" insertion mode...
3349            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3350              !!!cp ('t91');
3351              pop @{$self->{open_elements}};
3352    
3353              ## Reprocess in the "after head" insertion mode...
3354            } else {
3355              !!!cp ('t92');
3356            }
3357    
3358                ## "after head" insertion mode
3359                ## As if <body>
3360                !!!insert-element ('body');
3361                $self->{insertion_mode} = IN_BODY_IM;
3362                ## reprocess
3363                redo B;
3364              } elsif ($token->{type} == START_TAG_TOKEN) {
3365                if ($token->{tag_name} eq 'head') {
3366                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3367                    !!!cp ('t93');
3368                    !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});
3369                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3370                    push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];
3371                    $self->{insertion_mode} = IN_HEAD_IM;
3372                    !!!next-token;
3373                    redo B;
3374                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3375                    !!!cp ('t94');
3376                    #
3377                  } else {
3378                    !!!cp ('t95');
3379                    !!!parse-error (type => 'in head:head'); # or in head noscript
3380                    ## Ignore the token
3381                    !!!next-token;
3382                    redo B;
3383                  }
3384                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3385                  !!!cp ('t96');
3386                  ## As if <head>
3387                  !!!create-element ($self->{head_element}, 'head');
3388                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3389                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3390    
3391                  $self->{insertion_mode} = IN_HEAD_IM;
3392                  ## Reprocess in the "in head" insertion mode...
3393                } else {
3394                  !!!cp ('t97');
3395                }
3396    
3397                if ($token->{tag_name} eq 'base') {
3398                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3399                    !!!cp ('t98');
3400                    ## As if </noscript>
3401                    pop @{$self->{open_elements}};
3402                    !!!parse-error (type => 'in noscript:base');
3403                  
3404                    $self->{insertion_mode} = IN_HEAD_IM;
3405                    ## Reprocess in the "in head" insertion mode...
3406                  } else {
3407                    !!!cp ('t99');
3408                  }
3409    
3410                  ## NOTE: There is a "as if in head" code clone.
3411                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3412                    !!!cp ('t100');
3413                    !!!parse-error (type => 'after head:'.$token->{tag_name});
3414                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3415                  } else {
3416                    !!!cp ('t101');
3417                  }
3418                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3419                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3420                  pop @{$self->{open_elements}}
3421                      if $self->{insertion_mode} == AFTER_HEAD_IM;
3422                  !!!next-token;
3423                  redo B;
3424                } elsif ($token->{tag_name} eq 'link') {
3425                  ## NOTE: There is a "as if in head" code clone.
3426                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3427                    !!!cp ('t102');
3428                    !!!parse-error (type => 'after head:'.$token->{tag_name});
3429                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3430                  } else {
3431                    !!!cp ('t103');
3432                  }
3433                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3434                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3435                  pop @{$self->{open_elements}}
3436                      if $self->{insertion_mode} == AFTER_HEAD_IM;
3437                  !!!next-token;
3438                  redo B;
3439                } elsif ($token->{tag_name} eq 'meta') {
3440                  ## NOTE: There is a "as if in head" code clone.
3441                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3442                    !!!cp ('t104');
3443                    !!!parse-error (type => 'after head:'.$token->{tag_name});
3444                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3445                  } else {
3446                    !!!cp ('t105');
3447                  }
3448                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3449                  my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3450    
3451                  unless ($self->{confident}) {
3452                    if ($token->{attributes}->{charset}) { ## TODO: And if supported
3453                      !!!cp ('t106');
3454                      $self->{change_encoding}
3455                          ->($self, $token->{attributes}->{charset}->{value});
3456                      
3457                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
3458                          ->set_user_data (manakai_has_reference =>
3459                                               $token->{attributes}->{charset}
3460                                                   ->{has_reference});
3461                    } elsif ($token->{attributes}->{content}) {
3462                      ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
3463                      if ($token->{attributes}->{content}->{value}
3464                          =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
3465                              [\x09-\x0D\x20]*=
3466                              [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
3467                              ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
3468                        !!!cp ('t107');
3469                        $self->{change_encoding}
3470                            ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);
3471                        $meta_el->[0]->get_attribute_node_ns (undef, 'content')
3472                            ->set_user_data (manakai_has_reference =>
3473                                                 $token->{attributes}->{content}
3474                                                       ->{has_reference});
3475                      } else {
3476                        !!!cp ('t108');
3477                      }
3478                    }
3479                  } else {
3480                    if ($token->{attributes}->{charset}) {
3481                      !!!cp ('t109');
3482                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
3483                          ->set_user_data (manakai_has_reference =>
3484                                               $token->{attributes}->{charset}
3485                                                   ->{has_reference});
3486                    }
3487                    if ($token->{attributes}->{content}) {
3488                      !!!cp ('t110');
3489                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
3490                          ->set_user_data (manakai_has_reference =>
3491                                               $token->{attributes}->{content}
3492                                                   ->{has_reference});
3493                    }
3494                  }
3495    
3496                  pop @{$self->{open_elements}}
3497                      if $self->{insertion_mode} == AFTER_HEAD_IM;
3498                  !!!next-token;
3499                  redo B;
3500                } elsif ($token->{tag_name} eq 'title') {
3501                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3502                    !!!cp ('t111');
3503                    ## As if </noscript>
3504                    pop @{$self->{open_elements}};
3505                    !!!parse-error (type => 'in noscript:title');
3506                  
3507                    $self->{insertion_mode} = IN_HEAD_IM;
3508                    ## Reprocess in the "in head" insertion mode...
3509                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3510                    !!!cp ('t112');
3511                    !!!parse-error (type => 'after head:'.$token->{tag_name});
3512                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3513                  } else {
3514                    !!!cp ('t113');
3515                  }
3516    
3517                  ## NOTE: There is a "as if in head" code clone.
3518                  my $parent = defined $self->{head_element} ? $self->{head_element}
3519                      : $self->{open_elements}->[-1]->[0];
3520                  $parse_rcdata->(RCDATA_CONTENT_MODEL,
3521                                  sub { $parent->append_child ($_[0]) });
3522                  pop @{$self->{open_elements}}
3523                      if $self->{insertion_mode} == AFTER_HEAD_IM;
3524                  redo B;
3525                } elsif ($token->{tag_name} eq 'style') {
3526                  ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
3527                  ## insertion mode IN_HEAD_IM)
3528                  ## NOTE: There is a "as if in head" code clone.
3529                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3530                    !!!cp ('t114');
3531                    !!!parse-error (type => 'after head:'.$token->{tag_name});
3532                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3533                  } else {
3534                    !!!cp ('t115');
3535                  }
3536                  $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
3537                  pop @{$self->{open_elements}}
3538                      if $self->{insertion_mode} == AFTER_HEAD_IM;
3539                  redo B;
3540                } elsif ($token->{tag_name} eq 'noscript') {
3541                  if ($self->{insertion_mode} == IN_HEAD_IM) {
3542                    !!!cp ('t116');
3543                    ## NOTE: and scripting is disalbed
3544                    !!!insert-element ($token->{tag_name}, $token->{attributes});
3545                    $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
3546                    !!!next-token;
3547                    redo B;
3548                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3549                    !!!cp ('t117');
3550                    !!!parse-error (type => 'in noscript:noscript');
3551                    ## Ignore the token
3552                    !!!next-token;
3553                    redo B;
3554                  } else {
3555                    !!!cp ('t118');
3556                    #
3557                  }
3558                } elsif ($token->{tag_name} eq 'script') {
3559                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3560                    !!!cp ('t119');
3561                    ## As if </noscript>
3562                    pop @{$self->{open_elements}};
3563                    !!!parse-error (type => 'in noscript:script');
3564                  
3565                    $self->{insertion_mode} = IN_HEAD_IM;
3566                    ## Reprocess in the "in head" insertion mode...
3567                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3568                    !!!cp ('t120');
3569                    !!!parse-error (type => 'after head:'.$token->{tag_name});
3570                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3571                  } else {
3572                    !!!cp ('t121');
3573                  }
3574    
3575                  ## NOTE: There is a "as if in head" code clone.
3576                  $script_start_tag->($insert_to_current);
3577                  pop @{$self->{open_elements}}
3578                      if $self->{insertion_mode} == AFTER_HEAD_IM;
3579                  redo B;
3580                } elsif ($token->{tag_name} eq 'body' or
3581                         $token->{tag_name} eq 'frameset') {
3582                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3583                    !!!cp ('t122');
3584                    ## As if </noscript>
3585                    pop @{$self->{open_elements}};
3586                    !!!parse-error (type => 'in noscript:'.$token->{tag_name});
3587                    
3588                    ## Reprocess in the "in head" insertion mode...
3589                    ## As if </head>
3590                    pop @{$self->{open_elements}};
3591                    
3592                    ## Reprocess in the "after head" insertion mode...
3593                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3594                    !!!cp ('t124');
3595                    pop @{$self->{open_elements}};
3596                    
3597                    ## Reprocess in the "after head" insertion mode...
3598                  } else {
3599                    !!!cp ('t125');
3600                  }
3601    
3602                  ## "after head" insertion mode
3603                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3604                  if ($token->{tag_name} eq 'body') {
3605                    !!!cp ('t126');
3606                    $self->{insertion_mode} = IN_BODY_IM;
3607                  } elsif ($token->{tag_name} eq 'frameset') {
3608                    !!!cp ('t127');
3609                    $self->{insertion_mode} = IN_FRAMESET_IM;
3610                  } else {
3611                    die "$0: tag name: $self->{tag_name}";
3612                  }
3613                  !!!next-token;
3614                  redo B;
3615                } else {
3616                  !!!cp ('t128');
3617                  #
3618                }
3619    
3620                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3621                  !!!cp ('t129');
3622                  ## As if </noscript>
3623                  pop @{$self->{open_elements}};
3624                  !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
3625                  
3626                  ## Reprocess in the "in head" insertion mode...
3627                  ## As if </head>
3628                  pop @{$self->{open_elements}};
3629    
3630                  ## Reprocess in the "after head" insertion mode...
3631                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3632                  !!!cp ('t130');
3633                  ## As if </head>
3634                  pop @{$self->{open_elements}};
3635    
3636                  ## Reprocess in the "after head" insertion mode...
3637                } else {
3638                  !!!cp ('t131');
3639                }
3640    
3641                ## "after head" insertion mode
3642                ## As if <body>
3643                !!!insert-element ('body');
3644                $self->{insertion_mode} = IN_BODY_IM;
3645                ## reprocess
3646                redo B;
3647              } elsif ($token->{type} == END_TAG_TOKEN) {
3648                if ($token->{tag_name} eq 'head') {
3649                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3650                    !!!cp ('t132');
3651                    ## As if <head>
3652                    !!!create-element ($self->{head_element}, 'head');
3653                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3654                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3655    
3656                    ## Reprocess in the "in head" insertion mode...
3657                    pop @{$self->{open_elements}};
3658                    $self->{insertion_mode} = AFTER_HEAD_IM;
3659                    !!!next-token;
3660                    redo B;
3661                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3662                    !!!cp ('t133');
3663                    ## As if </noscript>
3664                    pop @{$self->{open_elements}};
3665                    !!!parse-error (type => 'in noscript:script');
3666                    
3667                    ## Reprocess in the "in head" insertion mode...
3668                    pop @{$self->{open_elements}};
3669                    $self->{insertion_mode} = AFTER_HEAD_IM;
3670                    !!!next-token;
3671                    redo B;
3672                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3673                    !!!cp ('t134');
3674                    pop @{$self->{open_elements}};
3675                    $self->{insertion_mode} = AFTER_HEAD_IM;
3676                    !!!next-token;
3677                    redo B;
3678                  } else {
3679                    !!!cp ('t135');
3680                    #
3681                  }
3682                } elsif ($token->{tag_name} eq 'noscript') {
3683                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3684                    !!!cp ('t136');
3685                    pop @{$self->{open_elements}};
3686                    $self->{insertion_mode} = IN_HEAD_IM;
3687                    !!!next-token;
3688                    redo B;
3689                  } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3690                    !!!cp ('t137');
3691                    !!!parse-error (type => 'unmatched end tag:noscript');
3692                    ## Ignore the token ## ISSUE: An issue in the spec.
3693                    !!!next-token;
3694                    redo B;
3695                  } else {
3696                    !!!cp ('t138');
3697                    #
3698                  }
3699                } elsif ({
3700                          body => 1, html => 1,
3701                         }->{$token->{tag_name}}) {
3702                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3703                    !!!cp ('t139');
3704                    ## As if <head>
3705                    !!!create-element ($self->{head_element}, 'head');
3706                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3707                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3708    
3709                    $self->{insertion_mode} = IN_HEAD_IM;
3710                    ## Reprocess in the "in head" insertion mode...
3711                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3712                    !!!cp ('t140');
3713                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3714                    ## Ignore the token
3715                    !!!next-token;
3716                    redo B;
3717                  } else {
3718                    !!!cp ('t141');
3719                  }
3720                  
3721                  #
3722                } elsif ({
3723                          p => 1, br => 1,
3724                         }->{$token->{tag_name}}) {
3725                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3726                    !!!cp ('t142');
3727                    ## As if <head>
3728                    !!!create-element ($self->{head_element}, 'head');
3729                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3730                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3731    
3732                    $self->{insertion_mode} = IN_HEAD_IM;
3733                    ## Reprocess in the "in head" insertion mode...
3734                  } else {
3735                    !!!cp ('t143');
3736                  }
3737    
3738                  #
3739                } else {
3740                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3741                    !!!cp ('t144');
3742                    #
3743                  } else {
3744                    !!!cp ('t145');
3745                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3746                    ## Ignore the token
3747                    !!!next-token;
3748                    redo B;
3749                  }
3750                }
3751    
3752                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3753                  !!!cp ('t146');
3754                  ## As if </noscript>
3755                  pop @{$self->{open_elements}};
3756                  !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
3757                  
3758                  ## Reprocess in the "in head" insertion mode...
3759                  ## As if </head>
3760                  pop @{$self->{open_elements}};
3761    
3762                  ## Reprocess in the "after head" insertion mode...
3763                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3764                  !!!cp ('t147');
3765                  ## As if </head>
3766                  pop @{$self->{open_elements}};
3767    
3768                  ## Reprocess in the "after head" insertion mode...
3769                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3770                  !!!cp ('t148');
3771                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3772                  ## Ignore the token ## ISSUE: An issue in the spec.
3773                  !!!next-token;
3774                  redo B;
3775                } else {
3776                  !!!cp ('t149');
3777                }
3778    
3779                ## "after head" insertion mode
3780                ## As if <body>
3781                !!!insert-element ('body');
3782                $self->{insertion_mode} = IN_BODY_IM;
3783                ## reprocess
3784                redo B;
3785              } else {
3786                die "$0: $token->{type}: Unknown token type";
3787              }
3788    
3789              ## ISSUE: An issue in the spec.
3790        } elsif ($self->{insertion_mode} & BODY_IMS) {
3791              if ($token->{type} == CHARACTER_TOKEN) {
3792                !!!cp ('t150');
3793                ## NOTE: There is a code clone of "character in body".
3794                $reconstruct_active_formatting_elements->($insert_to_current);
3795                
3796                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3797    
3798                !!!next-token;
3799                redo B;
3800              } elsif ($token->{type} == START_TAG_TOKEN) {
3801                if ({
3802                     caption => 1, col => 1, colgroup => 1, tbody => 1,
3803                     td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
3804                    }->{$token->{tag_name}}) {
3805                  if ($self->{insertion_mode} == IN_CELL_IM) {
3806                    ## have an element in table scope
3807                    my $tn;
3808                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3809                      my $node = $self->{open_elements}->[$_];
3810                      if ($node->[1] eq 'td' or $node->[1] eq 'th') {
3811                        !!!cp ('t151');
3812                        $tn = $node->[1];
3813                        last INSCOPE;
3814                      } elsif ({
3815                                table => 1, html => 1,
3816                               }->{$node->[1]}) {
3817                        !!!cp ('t152');
3818                        last INSCOPE;
3819                      }
3820                    } # INSCOPE
3821                      unless (defined $tn) {
3822                        !!!cp ('t153');
3823                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3824                        ## Ignore the token
3825                        !!!next-token;
3826                        redo B;
3827                      }
3828                    
3829                    !!!cp ('t154');
3830                    ## Close the cell
3831                    !!!back-token; # <?>
3832                    $token = {type => END_TAG_TOKEN, tag_name => $tn};
3833                    redo B;
3834                  } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3835                    !!!parse-error (type => 'not closed:caption');
3836                    
3837                    ## As if </caption>
3838                    ## have a table element in table scope
3839                    my $i;
3840                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3841                      my $node = $self->{open_elements}->[$_];
3842                      if ($node->[1] eq 'caption') {
3843                        !!!cp ('t155');
3844                        $i = $_;
3845                        last INSCOPE;
3846                      } elsif ({
3847                                table => 1, html => 1,
3848                               }->{$node->[1]}) {
3849                        !!!cp ('t156');
3850                        last INSCOPE;
3851                      }
3852                    } # INSCOPE
3853                      unless (defined $i) {
3854                        !!!cp ('t157');
3855                        !!!parse-error (type => 'unmatched end tag:caption');
3856                        ## Ignore the token
3857                        !!!next-token;
3858                        redo B;
3859                      }
3860                    
3861                    ## generate implied end tags
3862                    if ({
3863                         dd => 1, dt => 1, li => 1, p => 1,
3864                         td => 1, th => 1, tr => 1,
3865                         tbody => 1, tfoot=> 1, thead => 1,
3866                        }->{$self->{open_elements}->[-1]->[1]}) {
3867                      !!!cp ('t158');
3868                      !!!back-token; # <?>
3869                      $token = {type => END_TAG_TOKEN, tag_name => 'caption'};
3870                      !!!back-token;
3871                      $token = {type => END_TAG_TOKEN,
3872                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3873                      redo B;
3874                    }
3875    
3876                    if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3877                      !!!cp ('t159');
3878                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3879                    } else {
3880                      !!!cp ('t160');
3881                    }
3882                    
3883                    splice @{$self->{open_elements}}, $i;
3884                    
3885                    $clear_up_to_marker->();
3886                    
3887                    $self->{insertion_mode} = IN_TABLE_IM;
3888                    
3889                    ## reprocess
3890                    redo B;
3891                  } else {
3892                    !!!cp ('t161');
3893                    #
3894                  }
3895                } else {
3896                  !!!cp ('t162');
3897                  #
3898                }
3899              } elsif ($token->{type} == END_TAG_TOKEN) {
3900                if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
3901                  if ($self->{insertion_mode} == IN_CELL_IM) {
3902                    ## have an element in table scope
3903                    my $i;
3904                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3905                      my $node = $self->{open_elements}->[$_];
3906                      if ($node->[1] eq $token->{tag_name}) {
3907                        !!!cp ('t163');
3908                        $i = $_;
3909                        last INSCOPE;
3910                      } elsif ({
3911                                table => 1, html => 1,
3912                               }->{$node->[1]}) {
3913                        !!!cp ('t164');
3914                        last INSCOPE;
3915                      }
3916                    } # INSCOPE
3917                      unless (defined $i) {
3918                        !!!cp ('t165');
3919                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3920                        ## Ignore the token
3921                        !!!next-token;
3922                        redo B;
3923                      }
3924                    
3925                    ## generate implied end tags
3926                    if ({
3927                         dd => 1, dt => 1, li => 1, p => 1,
3928                         td => ($token->{tag_name} eq 'th'),
3929                         th => ($token->{tag_name} eq 'td'),
3930                         tr => 1,
3931                         tbody => 1, tfoot=> 1, thead => 1,
3932                        }->{$self->{open_elements}->[-1]->[1]}) {
3933                      !!!cp ('t166');
3934                      !!!back-token;
3935                      $token = {type => END_TAG_TOKEN,
3936                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3937                      redo B;
3938                    }
3939                    
3940                    if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
3941                      !!!cp ('t167');
3942                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3943                    } else {
3944                      !!!cp ('t168');
3945                    }
3946                    
3947                    splice @{$self->{open_elements}}, $i;
3948                    
3949                    $clear_up_to_marker->();
3950                    
3951                    $self->{insertion_mode} = IN_ROW_IM;
3952                    
3953                    !!!next-token;
3954                    redo B;
3955                  } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3956                    !!!cp ('t169');
3957                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3958                    ## Ignore the token
3959                    !!!next-token;
3960                    redo B;
3961                  } else {
3962                    !!!cp ('t170');
3963                    #
3964                  }
3965                } elsif ($token->{tag_name} eq 'caption') {
3966                  if ($self->{insertion_mode} == IN_CAPTION_IM) {
3967                    ## have a table element in table scope
3968                    my $i;
3969                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3970                      my $node = $self->{open_elements}->[$_];
3971                      if ($node->[1] eq $token->{tag_name}) {
3972                        !!!cp ('t171');
3973                        $i = $_;
3974                        last INSCOPE;
3975                      } elsif ({
3976                                table => 1, html => 1,
3977                               }->{$node->[1]}) {
3978                        !!!cp ('t172');
3979                        last INSCOPE;
3980                      }
3981                    } # INSCOPE
3982                      unless (defined $i) {
3983                        !!!cp ('t173');
3984                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3985                        ## Ignore the token
3986                        !!!next-token;
3987                        redo B;
3988                      }
3989                    
3990                    ## generate implied end tags
3991                    if ({
3992                         dd => 1, dt => 1, li => 1, p => 1,
3993                         td => 1, th => 1, tr => 1,
3994                         tbody => 1, tfoot=> 1, thead => 1,
3995                        }->{$self->{open_elements}->[-1]->[1]}) {
3996                      !!!cp ('t174');
3997                      !!!back-token;
3998                      $token = {type => END_TAG_TOKEN,
3999                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
4000                      redo B;
4001                    }
4002                    
4003                    if ($self->{open_elements}->[-1]->[1] ne 'caption') {
4004                      !!!cp ('t175');
4005                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4006                    } else {
4007                      !!!cp ('t176');
4008                    }
4009                    
4010                    splice @{$self->{open_elements}}, $i;
4011                    
4012                    $clear_up_to_marker->();
4013                    
4014                    $self->{insertion_mode} = IN_TABLE_IM;
4015                    
4016                    !!!next-token;
4017                    redo B;
4018                  } elsif ($self->{insertion_mode} == IN_CELL_IM) {
4019                    !!!cp ('t177');
4020                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4021                    ## Ignore the token
4022                    !!!next-token;
4023                    redo B;
4024                  } else {
4025                    !!!cp ('t178');
4026                    #
4027                  }
4028                } elsif ({
4029                          table => 1, tbody => 1, tfoot => 1,
4030                          thead => 1, tr => 1,
4031                         }->{$token->{tag_name}} and
4032                         $self->{insertion_mode} == IN_CELL_IM) {
4033                  ## have an element in table scope
4034                  my $i;
4035                  my $tn;
4036                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4037                    my $node = $self->{open_elements}->[$_];
4038                    if ($node->[1] eq $token->{tag_name}) {
4039                      !!!cp ('t179');
4040                      $i = $_;
4041                      last INSCOPE;
4042                    } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {
4043                      !!!cp ('t180');
4044                      $tn = $node->[1];
4045                      ## NOTE: There is exactly one |td| or |th| element
4046                      ## in scope in the stack of open elements by definition.
4047                    } elsif ({
4048                              table => 1, html => 1,
4049                             }->{$node->[1]}) {
4050                      !!!cp ('t181');
4051                      last INSCOPE;
4052                    }
4053                  } # INSCOPE
4054                  unless (defined $i) {
4055                    !!!cp ('t182');
4056                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4057                    ## Ignore the token
4058                    !!!next-token;
4059                    redo B;
4060                  } else {
4061                    !!!cp ('t183');
4062                  }
4063    
4064                  ## Close the cell
4065                  !!!back-token; # </?>
4066                  $token = {type => END_TAG_TOKEN, tag_name => $tn};
4067                  redo B;
4068                } elsif ($token->{tag_name} eq 'table' and
4069                         $self->{insertion_mode} == IN_CAPTION_IM) {
4070                  !!!parse-error (type => 'not closed:caption');
4071    
4072                  ## As if </caption>
4073                  ## have a table element in table scope
4074                  my $i;
4075                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4076                    my $node = $self->{open_elements}->[$_];
4077                    if ($node->[1] eq 'caption') {
4078                      !!!cp ('t184');
4079                      $i = $_;
4080                      last INSCOPE;
4081                    } elsif ({
4082                              table => 1, html => 1,
4083                             }->{$node->[1]}) {
4084                      !!!cp ('t185');
4085                      last INSCOPE;
4086                    }
4087                  } # INSCOPE
4088                  unless (defined $i) {
4089                    !!!cp ('t186');
4090                    !!!parse-error (type => 'unmatched end tag:caption');
4091                    ## Ignore the token
4092                    !!!next-token;
4093                    redo B;
4094                  }
4095                  
4096                  ## generate implied end tags
4097                  if ({
4098                       dd => 1, dt => 1, li => 1, p => 1,
4099                       td => 1, th => 1, tr => 1,
4100                       tbody => 1, tfoot=> 1, thead => 1,
4101                      }->{$self->{open_elements}->[-1]->[1]}) {
4102                    !!!cp ('t187');
4103                    !!!back-token; # </table>
4104                    $token = {type => END_TAG_TOKEN, tag_name => 'caption'};
4105                    !!!back-token;
4106                    $token = {type => END_TAG_TOKEN,
4107                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
4108                    redo B;
4109                  }
4110    
4111                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {
4112                    !!!cp ('t188');
4113                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4114                  } else {
4115                    !!!cp ('t189');
4116                  }
4117    
4118                  splice @{$self->{open_elements}}, $i;
4119    
4120                  $clear_up_to_marker->();
4121    
4122                  $self->{insertion_mode} = IN_TABLE_IM;
4123    
4124                  ## reprocess
4125                  redo B;
4126                } elsif ({
4127                          body => 1, col => 1, colgroup => 1, html => 1,
4128                         }->{$token->{tag_name}}) {
4129                  if ($self->{insertion_mode} & BODY_TABLE_IMS) {
4130                    !!!cp ('t190');
4131                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4132                    ## Ignore the token
4133                    !!!next-token;
4134                    redo B;
4135                  } else {
4136                    !!!cp ('t191');
4137                    #
4138                  }
4139                } elsif ({
4140                          tbody => 1, tfoot => 1,
4141                          thead => 1, tr => 1,
4142                         }->{$token->{tag_name}} and
4143                         $self->{insertion_mode} == IN_CAPTION_IM) {
4144                  !!!cp ('t192');
4145                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4146                  ## Ignore the token
4147                  !!!next-token;
4148                  redo B;
4149                } else {
4150                  !!!cp ('t193');
4151                  #
4152                }
4153          } else {
4154            die "$0: $token->{type}: Unknown token type";
4155          }
4156    
4157          $insert = $insert_to_current;
4158          #
4159        } elsif ($self->{insertion_mode} & TABLE_IMS) {
4160          if ($token->{type} == CHARACTER_TOKEN) {
4161                if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4162                  $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4163                  
4164                  unless (length $token->{data}) {
4165                    !!!cp ('t194');
4166                    !!!next-token;
4167                    redo B;
4168                  } else {
4169                    !!!cp ('t195');
4170                  }
4171                }
4172    
4173                !!!parse-error (type => 'in table:#character');
4174    
4175                ## As if in body, but insert into foster parent element
4176                ## ISSUE: Spec says that "whenever a node would be inserted
4177                ## into the current node" while characters might not be
4178                ## result in a new Text node.
4179                $reconstruct_active_formatting_elements->($insert_to_foster);
4180                
4181                if ({
4182                     table => 1, tbody => 1, tfoot => 1,
4183                     thead => 1, tr => 1,
4184                    }->{$self->{open_elements}->[-1]->[1]}) {
4185                  # MUST
4186                  my $foster_parent_element;
4187                  my $next_sibling;
4188                  my $prev_sibling;
4189                  OE: for (reverse 0..$#{$self->{open_elements}}) {
4190                    if ($self->{open_elements}->[$_]->[1] eq 'table') {
4191                      my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4192                      if (defined $parent and $parent->node_type == 1) {
4193                        !!!cp ('t196');
4194                        $foster_parent_element = $parent;
4195                        $next_sibling = $self->{open_elements}->[$_]->[0];
4196                        $prev_sibling = $next_sibling->previous_sibling;
4197                      } else {
4198                        !!!cp ('t197');
4199                        $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
4200                        $prev_sibling = $foster_parent_element->last_child;
4201                      }
4202                      last OE;
4203                    }
4204                  } # OE
4205                  $foster_parent_element = $self->{open_elements}->[0]->[0] and
4206                  $prev_sibling = $foster_parent_element->last_child
4207                    unless defined $foster_parent_element;
4208                  if (defined $prev_sibling and
4209                      $prev_sibling->node_type == 3) {
4210                    !!!cp ('t198');
4211                    $prev_sibling->manakai_append_text ($token->{data});
4212                  } else {
4213                    !!!cp ('t199');
4214                    $foster_parent_element->insert_before
4215                      ($self->{document}->create_text_node ($token->{data}),
4216                       $next_sibling);
4217                  }
4218                } else {
4219                  !!!cp ('t200');
4220                  $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4221                }
4222                
4223                !!!next-token;
4224                redo B;
4225          } elsif ($token->{type} == START_TAG_TOKEN) {
4226                if ({
4227                     tr => ($self->{insertion_mode} != IN_ROW_IM),
4228                     th => 1, td => 1,
4229                    }->{$token->{tag_name}}) {
4230                  if ($self->{insertion_mode} == IN_TABLE_IM) {
4231                    ## Clear back to table context
4232                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
4233                           $self->{open_elements}->[-1]->[1] ne 'html') {
4234                      !!!cp ('t201');
4235                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4236                      pop @{$self->{open_elements}};
4237                    }
4238                    
4239                    !!!insert-element ('tbody');
4240                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
4241                    ## reprocess in the "in table body" insertion mode...
4242                  }
4243    
4244                  if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
4245                    unless ($token->{tag_name} eq 'tr') {
4246                      !!!cp ('t202');
4247                      !!!parse-error (type => 'missing start tag:tr');
4248                    }
4249                    
4250                    ## Clear back to table body context
4251                    while (not {
4252                      tbody => 1, tfoot => 1, thead => 1, html => 1,
4253                    }->{$self->{open_elements}->[-1]->[1]}) {
4254                      !!!cp ('t203');
4255                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4256                      pop @{$self->{open_elements}};
4257                    }
4258                    
4259                    $self->{insertion_mode} = IN_ROW_IM;
4260                    if ($token->{tag_name} eq 'tr') {
4261                      !!!cp ('t204');
4262                      !!!insert-element ($token->{tag_name}, $token->{attributes});
4263                      !!!next-token;
4264                      redo B;
4265                    } else {
4266                      !!!cp ('t205');
4267                      !!!insert-element ('tr');
4268                      ## reprocess in the "in row" insertion mode
4269                    }
4270                  } else {
4271                    !!!cp ('t206');
4272                  }
4273    
4274                  ## Clear back to table row context
4275                  while (not {
4276                    tr => 1, html => 1,
4277                  }->{$self->{open_elements}->[-1]->[1]}) {
4278                    !!!cp ('t207');
4279                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4280                    pop @{$self->{open_elements}};
4281                  }
4282                  
4283                  !!!insert-element ($token->{tag_name}, $token->{attributes});
4284                  $self->{insertion_mode} = IN_CELL_IM;
4285    
4286                  push @$active_formatting_elements, ['#marker', ''];
4287                  
4288                  !!!next-token;
4289                  redo B;
4290                } elsif ({
4291                          caption => 1, col => 1, colgroup => 1,
4292                          tbody => 1, tfoot => 1, thead => 1,
4293                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
4294                         }->{$token->{tag_name}}) {
4295                  if ($self->{insertion_mode} == IN_ROW_IM) {
4296                    ## As if </tr>
4297                    ## have an element in table scope
4298                    my $i;
4299                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4300                      my $node = $self->{open_elements}->[$_];
4301                      if ($node->[1] eq 'tr') {
4302                        !!!cp ('t208');
4303                        $i = $_;
4304                        last INSCOPE;
4305                      } elsif ({
4306                                table => 1, html => 1,
4307                               }->{$node->[1]}) {
4308                        !!!cp ('t209');
4309                        last INSCOPE;
4310                      }
4311                    } # INSCOPE
4312                    unless (defined $i) {
4313                     !!!cp ('t210');
4314                     !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});
4315                      ## Ignore the token
4316                      !!!next-token;
4317                      redo B;
4318                    }
4319                    
4320                    ## Clear back to table row context
4321                    while (not {
4322                      tr => 1, html => 1,
4323                    }->{$self->{open_elements}->[-1]->[1]}) {
4324                      !!!cp ('t211');
4325                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4326                      pop @{$self->{open_elements}};
4327                    }
4328                    
4329                    pop @{$self->{open_elements}}; # tr
4330                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
4331                    if ($token->{tag_name} eq 'tr') {
4332                      !!!cp ('t212');
4333                      ## reprocess
4334                      redo B;
4335                    } else {
4336                      !!!cp ('t213');
4337                      ## reprocess in the "in table body" insertion mode...
4338                    }
4339                  }
4340    
4341                  if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
4342                    ## have an element in table scope
4343                    my $i;
4344                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4345                      my $node = $self->{open_elements}->[$_];
4346                      if ({
4347                           tbody => 1, thead => 1, tfoot => 1,
4348                          }->{$node->[1]}) {
4349                        !!!cp ('t214');
4350                        $i = $_;
4351                        last INSCOPE;
4352                      } elsif ({
4353                                table => 1, html => 1,
4354                               }->{$node->[1]}) {
4355                        !!!cp ('t215');
4356                        last INSCOPE;
4357                      }
4358                    } # INSCOPE
4359                    unless (defined $i) {
4360                      !!!cp ('t216');
4361                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4362                      ## Ignore the token
4363                      !!!next-token;
4364                      redo B;
4365                    }
4366    
4367                    ## Clear back to table body context
4368                    while (not {
4369                      tbody => 1, tfoot => 1, thead => 1, html => 1,
4370                    }->{$self->{open_elements}->[-1]->[1]}) {
4371                      !!!cp ('t217');
4372                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4373                      pop @{$self->{open_elements}};
4374                    }
4375                    
4376                    ## As if <{current node}>
4377                    ## have an element in table scope
4378                    ## true by definition
4379                    
4380                    ## Clear back to table body context
4381                    ## nop by definition
4382                    
4383                    pop @{$self->{open_elements}};
4384                    $self->{insertion_mode} = IN_TABLE_IM;
4385                    ## reprocess in "in table" insertion mode...
4386                  } else {
4387                    !!!cp ('t218');
4388                  }
4389    
4390                  if ($token->{tag_name} eq 'col') {
4391                    ## Clear back to table context
4392                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
4393                           $self->{open_elements}->[-1]->[1] ne 'html') {
4394                      !!!cp ('t219');
4395                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4396                      pop @{$self->{open_elements}};
4397                    }
4398                    
4399                    !!!insert-element ('colgroup');
4400                    $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
4401                    ## reprocess
4402                    redo B;
4403                  } elsif ({
4404                            caption => 1,
4405                            colgroup => 1,
4406                            tbody => 1, tfoot => 1, thead => 1,
4407                           }->{$token->{tag_name}}) {
4408                    ## Clear back to table context
4409                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
4410                           $self->{open_elements}->[-1]->[1] ne 'html') {
4411                      !!!cp ('t220');
4412                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4413                      pop @{$self->{open_elements}};
4414                    }
4415                    
4416                    push @$active_formatting_elements, ['#marker', '']
4417                        if $token->{tag_name} eq 'caption';
4418                    
4419                    !!!insert-element ($token->{tag_name}, $token->{attributes});
4420                    $self->{insertion_mode} = {
4421                                               caption => IN_CAPTION_IM,
4422                                               colgroup => IN_COLUMN_GROUP_IM,
4423                                               tbody => IN_TABLE_BODY_IM,
4424                                               tfoot => IN_TABLE_BODY_IM,
4425                                               thead => IN_TABLE_BODY_IM,
4426                                              }->{$token->{tag_name}};
4427                    !!!next-token;
4428                    redo B;
4429                  } else {
4430                    die "$0: in table: <>: $token->{tag_name}";
4431                  }
4432                } elsif ($token->{tag_name} eq 'table') {
4433                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4434    
4435                  ## As if </table>
4436                  ## have a table element in table scope
4437                  my $i;
4438                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4439                    my $node = $self->{open_elements}->[$_];
4440                    if ($node->[1] eq 'table') {
4441                      !!!cp ('t221');
4442                      $i = $_;
4443                      last INSCOPE;
4444                    } elsif ({
4445                              table => 1, html => 1,
4446                             }->{$node->[1]}) {
4447                      !!!cp ('t222');
4448                      last INSCOPE;
4449                    }
4450                  } # INSCOPE
4451                  unless (defined $i) {
4452                    !!!cp ('t223');
4453                    !!!parse-error (type => 'unmatched end tag:table');
4454                    ## Ignore tokens </table><table>
4455                    !!!next-token;
4456                    redo B;
4457                  }
4458                  
4459                  ## generate implied end tags
4460                  if ({
4461                       dd => 1, dt => 1, li => 1, p => 1,
4462                       td => 1, th => 1, tr => 1,
4463                       tbody => 1, tfoot=> 1, thead => 1,
4464                      }->{$self->{open_elements}->[-1]->[1]}) {
4465                    !!!cp ('t224');
4466                    !!!back-token; # <table>
4467                    $token = {type => END_TAG_TOKEN, tag_name => 'table'};
4468                    !!!back-token;
4469                    $token = {type => END_TAG_TOKEN,
4470                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
4471                    redo B;
4472                  }
4473    
4474                  if ($self->{open_elements}->[-1]->[1] ne 'table') {
4475                    !!!cp ('t225');
4476                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4477                  } else {
4478                    !!!cp ('t226');
4479                  }
4480    
4481                  splice @{$self->{open_elements}}, $i;
4482    
4483                  $self->_reset_insertion_mode;
4484    
4485                  ## reprocess
4486                  redo B;
4487            } else {
4488              !!!cp ('t227');
4489              !!!parse-error (type => 'in table:'.$token->{tag_name});
4490    
4491              $insert = $insert_to_foster;
4492              #
4493            }
4494          } elsif ($token->{type} == END_TAG_TOKEN) {
4495                if ($token->{tag_name} eq 'tr' and
4496                    $self->{insertion_mode} == IN_ROW_IM) {
4497                  ## have an element in table scope
4498                  my $i;
4499                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4500                    my $node = $self->{open_elements}->[$_];
4501                    if ($node->[1] eq $token->{tag_name}) {
4502                      !!!cp ('t228');
4503                      $i = $_;
4504                      last INSCOPE;
4505                    } elsif ({
4506                              table => 1, html => 1,
4507                             }->{$node->[1]}) {
4508                      !!!cp ('t229');
4509                      last INSCOPE;
4510                    }
4511                  } # INSCOPE
4512                  unless (defined $i) {
4513                    !!!cp ('t230');
4514                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4515                    ## Ignore the token
4516                    !!!next-token;
4517                    redo B;
4518                  } else {
4519                    !!!cp ('t232');
4520                  }
4521    
4522                  ## Clear back to table row context
4523                  while (not {
4524                    tr => 1, html => 1,
4525                  }->{$self->{open_elements}->[-1]->[1]}) {
4526                    !!!cp ('t231');
4527                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4528                    pop @{$self->{open_elements}};
4529                  }
4530    
4531                  pop @{$self->{open_elements}}; # tr
4532                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
4533                  !!!next-token;
4534                  redo B;
4535                } elsif ($token->{tag_name} eq 'table') {
4536                  if ($self->{insertion_mode} == IN_ROW_IM) {
4537                    ## As if </tr>
4538                    ## have an element in table scope
4539                    my $i;
4540                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4541                      my $node = $self->{open_elements}->[$_];
4542                      if ($node->[1] eq 'tr') {
4543                        !!!cp ('t233');
4544                        $i = $_;
4545                        last INSCOPE;
4546                      } elsif ({
4547                                table => 1, html => 1,
4548                               }->{$node->[1]}) {
4549                        !!!cp ('t234');
4550                        last INSCOPE;
4551                      }
4552                    } # INSCOPE
4553                    unless (defined $i) {
4554                      !!!cp ('t235');
4555                      !!!parse-error (type => 'unmatched end tag:'.$token->{type});
4556                      ## Ignore the token
4557                      !!!next-token;
4558                      redo B;
4559                    }
4560                    
4561                    ## Clear back to table row context
4562                    while (not {
4563                      tr => 1, html => 1,
4564                    }->{$self->{open_elements}->[-1]->[1]}) {
4565                      !!!cp ('t236');
4566                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4567                      pop @{$self->{open_elements}};
4568                    }
4569                    
4570                    pop @{$self->{open_elements}}; # tr
4571                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
4572                    ## reprocess in the "in table body" insertion mode...
4573                  }
4574    
4575                  if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
4576                    ## have an element in table scope
4577                    my $i;
4578                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4579                      my $node = $self->{open_elements}->[$_];
4580                      if ({
4581                           tbody => 1, thead => 1, tfoot => 1,
4582                          }->{$node->[1]}) {
4583                        !!!cp ('t237');
4584                        $i = $_;
4585                        last INSCOPE;
4586                      } elsif ({
4587                                table => 1, html => 1,
4588                               }->{$node->[1]}) {
4589                        !!!cp ('t238');
4590                        last INSCOPE;
4591                      }
4592                    } # INSCOPE
4593                    unless (defined $i) {
4594                      !!!cp ('t239');
4595                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4596                      ## Ignore the token
4597                      !!!next-token;
4598                      redo B;
4599                    }
4600                    
4601                    ## Clear back to table body context
4602                    while (not {
4603                      tbody => 1, tfoot => 1, thead => 1, html => 1,
4604                    }->{$self->{open_elements}->[-1]->[1]}) {
4605                      !!!cp ('t240');
4606                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4607                      pop @{$self->{open_elements}};
4608                    }
4609                    
4610                    ## As if <{current node}>
4611                    ## have an element in table scope
4612                    ## true by definition
4613                    
4614                    ## Clear back to table body context
4615                    ## nop by definition
4616                    
4617                    pop @{$self->{open_elements}};
4618                    $self->{insertion_mode} = IN_TABLE_IM;
4619                    ## reprocess in the "in table" insertion mode...
4620                  }
4621    
4622                  ## have a table element in table scope
4623                  my $i;
4624                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4625                    my $node = $self->{open_elements}->[$_];
4626                    if ($node->[1] eq $token->{tag_name}) {
4627                      !!!cp ('t241');
4628                      $i = $_;
4629                      last INSCOPE;
4630                    } elsif ({
4631                              table => 1, html => 1,
4632                             }->{$node->[1]}) {
4633                      !!!cp ('t242');
4634                      last INSCOPE;
4635                    }
4636                  } # INSCOPE
4637                  unless (defined $i) {
4638                    !!!cp ('t243');
4639                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4640                    ## Ignore the token
4641                    !!!next-token;
4642                    redo B;
4643                  }
4644    
4645                  ## generate implied end tags
4646                  if ({
4647                       dd => 1, dt => 1, li => 1, p => 1,
4648                       td => 1, th => 1, tr => 1,
4649                       tbody => 1, tfoot=> 1, thead => 1,
4650                      }->{$self->{open_elements}->[-1]->[1]}) {
4651                    !!!cp ('t244');
4652                    !!!back-token;
4653                    $token = {type => END_TAG_TOKEN,
4654                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
4655                    redo B;
4656                  }
4657                  
4658                  if ($self->{open_elements}->[-1]->[1] ne 'table') {
4659                    !!!cp ('t245');
4660                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4661                  } else {
4662                    !!!cp ('t246');
4663                  }
4664                    
4665                  splice @{$self->{open_elements}}, $i;
4666                  
4667                  $self->_reset_insertion_mode;
4668                  
4669                  !!!next-token;
4670                  redo B;
4671                } elsif ({
4672                          tbody => 1, tfoot => 1, thead => 1,
4673                         }->{$token->{tag_name}} and
4674                         $self->{insertion_mode} & ROW_IMS) {
4675                  if ($self->{insertion_mode} == IN_ROW_IM) {
4676                    ## have an element in table scope
4677                    my $i;
4678                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4679                      my $node = $self->{open_elements}->[$_];
4680                      if ($node->[1] eq $token->{tag_name}) {
4681                        !!!cp ('t247');
4682                        $i = $_;
4683                        last INSCOPE;
4684                      } elsif ({
4685                                table => 1, html => 1,
4686                               }->{$node->[1]}) {
4687                        !!!cp ('t248');
4688                        last INSCOPE;
4689                      }
4690                    } # INSCOPE
4691                      unless (defined $i) {
4692                        !!!cp ('t249');
4693                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4694                        ## Ignore the token
4695                        !!!next-token;
4696                        redo B;
4697                      }
4698                    
4699                    ## As if </tr>
4700                    ## have an element in table scope
4701                    my $i;
4702                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4703                      my $node = $self->{open_elements}->[$_];
4704                      if ($node->[1] eq 'tr') {
4705                        !!!cp ('t250');
4706                        $i = $_;
4707                        last INSCOPE;
4708                      } elsif ({
4709                                table => 1, html => 1,
4710                               }->{$node->[1]}) {
4711                        !!!cp ('t251');
4712                        last INSCOPE;
4713                      }
4714                    } # INSCOPE
4715                      unless (defined $i) {
4716                        !!!cp ('t252');
4717                        !!!parse-error (type => 'unmatched end tag:tr');
4718                        ## Ignore the token
4719                        !!!next-token;
4720                        redo B;
4721                      }
4722                    
4723                    ## Clear back to table row context
4724                    while (not {
4725                      tr => 1, html => 1,
4726                    }->{$self->{open_elements}->[-1]->[1]}) {
4727                      !!!cp ('t253');
4728                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4729                      pop @{$self->{open_elements}};
4730                    }
4731                    
4732                    pop @{$self->{open_elements}}; # tr
4733                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
4734                    ## reprocess in the "in table body" insertion mode...
4735                  }
4736    
4737                  ## have an element in table scope
4738                  my $i;
4739                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4740                    my $node = $self->{open_elements}->[$_];
4741                    if ($node->[1] eq $token->{tag_name}) {
4742                      !!!cp ('t254');
4743                      $i = $_;
4744                      last INSCOPE;
4745                    } elsif ({
4746                              table => 1, html => 1,
4747                             }->{$node->[1]}) {
4748                      !!!cp ('t255');
4749                      last INSCOPE;
4750                    }
4751                  } # INSCOPE
4752                  unless (defined $i) {
4753                    !!!cp ('t256');
4754                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4755                    ## Ignore the token
4756                    !!!next-token;
4757                    redo B;
4758                  }
4759    
4760                  ## Clear back to table body context
4761                  while (not {
4762                    tbody => 1, tfoot => 1, thead => 1, html => 1,
4763                  }->{$self->{open_elements}->[-1]->[1]}) {
4764                    !!!cp ('t257');
4765                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4766                    pop @{$self->{open_elements}};
4767                  }
4768    
4769                  pop @{$self->{open_elements}};
4770                  $self->{insertion_mode} = IN_TABLE_IM;
4771                  !!!next-token;
4772                  redo B;
4773                } elsif ({
4774                          body => 1, caption => 1, col => 1, colgroup => 1,
4775                          html => 1, td => 1, th => 1,
4776                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
4777                          tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
4778                         }->{$token->{tag_name}}) {
4779                  !!!cp ('t258');
4780                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4781                  ## Ignore the token
4782                  !!!next-token;
4783                  redo B;
4784            } else {
4785              !!!cp ('t259');
4786              !!!parse-error (type => 'in table:/'.$token->{tag_name});
4787    
4788              $insert = $insert_to_foster;
4789              #
4790            }
4791          } else {
4792            die "$0: $token->{type}: Unknown token type";
4793          }
4794        } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
4795              if ($token->{type} == CHARACTER_TOKEN) {
4796                if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4797                  $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4798                  unless (length $token->{data}) {
4799                    !!!cp ('t260');
4800                    !!!next-token;
4801                    redo B;
4802                  }
4803                }
4804                
4805                !!!cp ('t261');
4806                #
4807              } elsif ($token->{type} == START_TAG_TOKEN) {
4808                if ($token->{tag_name} eq 'col') {
4809                  !!!cp ('t262');
4810                  !!!insert-element ($token->{tag_name}, $token->{attributes});
4811                  pop @{$self->{open_elements}};
4812                  !!!next-token;
4813                  redo B;
4814                } else {
4815                  !!!cp ('t263');
4816                  #
4817                }
4818              } elsif ($token->{type} == END_TAG_TOKEN) {
4819                if ($token->{tag_name} eq 'colgroup') {
4820                  if ($self->{open_elements}->[-1]->[1] eq 'html') {
4821                    !!!cp ('t264');
4822                    !!!parse-error (type => 'unmatched end tag:colgroup');
4823                    ## Ignore the token
4824                    !!!next-token;
4825                    redo B;
4826                  } else {
4827                    !!!cp ('t265');
4828                    pop @{$self->{open_elements}}; # colgroup
4829                    $self->{insertion_mode} = IN_TABLE_IM;
4830                    !!!next-token;
4831                    redo B;            
4832                  }
4833                } elsif ($token->{tag_name} eq 'col') {
4834                  !!!cp ('t266');
4835                  !!!parse-error (type => 'unmatched end tag:col');
4836                  ## Ignore the token
4837                  !!!next-token;
4838                  redo B;
4839                } else {
4840                  !!!cp ('t267');
4841                  #
4842                }
4843              } else {
4844                !!!cp ('t268');
4845                #
4846              }
4847    
4848              ## As if </colgroup>
4849              if ($self->{open_elements}->[-1]->[1] eq 'html') {
4850                !!!cp ('t269');
4851                !!!parse-error (type => 'unmatched end tag:colgroup');
4852                ## Ignore the token
4853                !!!next-token;
4854                redo B;
4855              } else {
4856                !!!cp ('t270');
4857                pop @{$self->{open_elements}}; # colgroup
4858                $self->{insertion_mode} = IN_TABLE_IM;
4859                ## reprocess
4860                redo B;
4861              }
4862        } elsif ($self->{insertion_mode} == IN_SELECT_IM) {
4863          if ($token->{type} == CHARACTER_TOKEN) {
4864            !!!cp ('t271');
4865            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4866            !!!next-token;
4867            redo B;
4868          } elsif ($token->{type} == START_TAG_TOKEN) {
4869                if ($token->{tag_name} eq 'option') {
4870                  if ($self->{open_elements}->[-1]->[1] eq 'option') {
4871                    !!!cp ('t272');
4872                    ## As if </option>
4873                    pop @{$self->{open_elements}};
4874                  } else {
4875                    !!!cp ('t273');
4876                  }
4877    
4878                  !!!insert-element ($token->{tag_name}, $token->{attributes});
4879                  !!!next-token;
4880                  redo B;
4881                } elsif ($token->{tag_name} eq 'optgroup') {
4882                  if ($self->{open_elements}->[-1]->[1] eq 'option') {
4883                    !!!cp ('t274');
4884                    ## As if </option>
4885                    pop @{$self->{open_elements}};
4886                  } else {
4887                    !!!cp ('t275');
4888                  }
4889    
4890                  if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {
4891                    !!!cp ('t276');
4892                    ## As if </optgroup>
4893                    pop @{$self->{open_elements}};
4894                  } else {
4895                    !!!cp ('t277');
4896                  }
4897    
4898                  !!!insert-element ($token->{tag_name}, $token->{attributes});
4899                  !!!next-token;
4900                  redo B;
4901                } elsif ($token->{tag_name} eq 'select') {
4902                  !!!parse-error (type => 'not closed:select');
4903                  ## As if </select> instead
4904                  ## have an element in table scope
4905                  my $i;
4906                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4907                    my $node = $self->{open_elements}->[$_];
4908                    if ($node->[1] eq $token->{tag_name}) {
4909                      !!!cp ('t278');
4910                      $i = $_;
4911                      last INSCOPE;
4912                    } elsif ({
4913                              table => 1, html => 1,
4914                             }->{$node->[1]}) {
4915                      !!!cp ('t279');
4916                      last INSCOPE;
4917                    }
4918                  } # INSCOPE
4919                  unless (defined $i) {
4920                    !!!cp ('t280');
4921                    !!!parse-error (type => 'unmatched end tag:select');
4922                    ## Ignore the token
4923                    !!!next-token;
4924                    redo B;
4925                  }
4926                  
4927                  !!!cp ('t281');
4928                  splice @{$self->{open_elements}}, $i;
4929    
4930                  $self->_reset_insertion_mode;
4931    
4932                  !!!next-token;
4933                  redo B;
4934            } else {
4935              !!!cp ('t282');
4936              !!!parse-error (type => 'in select:'.$token->{tag_name});
4937              ## Ignore the token
4938              !!!next-token;
4939              redo B;
4940            }
4941          } elsif ($token->{type} == END_TAG_TOKEN) {
4942                if ($token->{tag_name} eq 'optgroup') {
4943                  if ($self->{open_elements}->[-1]->[1] eq 'option' and
4944                      $self->{open_elements}->[-2]->[1] eq 'optgroup') {
4945                    !!!cp ('t283');
4946                    ## As if </option>
4947                    splice @{$self->{open_elements}}, -2;
4948                  } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {
4949                    !!!cp ('t284');
4950                    pop @{$self->{open_elements}};
4951                  } else {
4952                    !!!cp ('t285');
4953                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4954                    ## Ignore the token
4955                  }
4956                  !!!next-token;
4957                  redo B;
4958                } elsif ($token->{tag_name} eq 'option') {
4959                  if ($self->{open_elements}->[-1]->[1] eq 'option') {
4960                    !!!cp ('t286');
4961                    pop @{$self->{open_elements}};
4962                  } else {
4963                    !!!cp ('t287');
4964                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4965                    ## Ignore the token
4966                  }
4967                  !!!next-token;
4968                  redo B;
4969                } elsif ($token->{tag_name} eq 'select') {
4970                  ## have an element in table scope
4971                  my $i;
4972                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4973                    my $node = $self->{open_elements}->[$_];
4974                    if ($node->[1] eq $token->{tag_name}) {
4975                      !!!cp ('t288');
4976                      $i = $_;
4977                      last INSCOPE;
4978                    } elsif ({
4979                              table => 1, html => 1,
4980                             }->{$node->[1]}) {
4981                      !!!cp ('t289');
4982                      last INSCOPE;
4983                    }
4984                  } # INSCOPE
4985                  unless (defined $i) {
4986                    !!!cp ('t290');
4987                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4988                    ## Ignore the token
4989                    !!!next-token;
4990                    redo B;
4991                  }
4992                  
4993                  !!!cp ('t291');
4994                  splice @{$self->{open_elements}}, $i;
4995    
4996                  $self->_reset_insertion_mode;
4997    
4998                  !!!next-token;
4999                  redo B;
5000                } elsif ({
5001                          caption => 1, table => 1, tbody => 1,
5002                          tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
5003                         }->{$token->{tag_name}}) {
5004                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5005                  
5006                  ## have an element in table scope
5007                  my $i;
5008                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5009                    my $node = $self->{open_elements}->[$_];
5010                    if ($node->[1] eq $token->{tag_name}) {
5011                      !!!cp ('t292');
5012                      $i = $_;
5013                      last INSCOPE;
5014                    } elsif ({
5015                              table => 1, html => 1,
5016                             }->{$node->[1]}) {
5017                      !!!cp ('t293');
5018                      last INSCOPE;
5019                    }
5020                  } # INSCOPE
5021                  unless (defined $i) {
5022                    !!!cp ('t294');
5023                    ## Ignore the token
5024                    !!!next-token;
5025                    redo B;
5026                  }
5027                  
5028                  ## As if </select>
5029                  ## have an element in table scope
5030                  undef $i;
5031                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5032                    my $node = $self->{open_elements}->[$_];
5033                    if ($node->[1] eq 'select') {
5034                      !!!cp ('t295');
5035                      $i = $_;
5036                      last INSCOPE;
5037                    } elsif ({
5038                              table => 1, html => 1,
5039                             }->{$node->[1]}) {
5040                      !!!cp ('t296');
5041                      last INSCOPE;
5042                    }
5043                  } # INSCOPE
5044                  unless (defined $i) {
5045                    !!!cp ('t297');
5046                    !!!parse-error (type => 'unmatched end tag:select');
5047                    ## Ignore the </select> token
5048                    !!!next-token; ## TODO: ok?
5049                    redo B;
5050                  }
5051                  
5052                  !!!cp ('t298');
5053                  splice @{$self->{open_elements}}, $i;
5054    
5055                  $self->_reset_insertion_mode;
5056    
5057                  ## reprocess
5058                  redo B;
5059            } else {
5060              !!!cp ('t299');
5061              !!!parse-error (type => 'in select:/'.$token->{tag_name});
5062              ## Ignore the token
5063              !!!next-token;
5064              redo B;
5065            }
5066          } else {
5067            die "$0: $token->{type}: Unknown token type";
5068          }
5069        } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
5070          if ($token->{type} == CHARACTER_TOKEN) {
5071            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5072              my $data = $1;
5073              ## As if in body
5074              $reconstruct_active_formatting_elements->($insert_to_current);
5075                  
5076              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5077              
5078              unless (length $token->{data}) {
5079                !!!cp ('t300');
5080                !!!next-token;
5081                redo B;
5082              }
5083            }
5084            
5085            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5086              !!!cp ('t301');
5087              !!!parse-error (type => 'after html:#character');
5088    
5089              ## Reprocess in the "main" phase, "after body" insertion mode...
5090            } else {
5091              !!!cp ('t302');
5092            }
5093            
5094            ## "after body" insertion mode
5095            !!!parse-error (type => 'after body:#character');
5096    
5097            $self->{insertion_mode} = IN_BODY_IM;
5098            ## reprocess
5099            redo B;
5100          } elsif ($token->{type} == START_TAG_TOKEN) {
5101            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5102              !!!cp ('t303');
5103              !!!parse-error (type => 'after html:'.$token->{tag_name});
5104              
5105              ## Reprocess in the "main" phase, "after body" insertion mode...
5106            } else {
5107              !!!cp ('t304');
5108            }
5109    
5110            ## "after body" insertion mode
5111            !!!parse-error (type => 'after body:'.$token->{tag_name});
5112    
5113            $self->{insertion_mode} = IN_BODY_IM;
5114            ## reprocess
5115            redo B;
5116          } elsif ($token->{type} == END_TAG_TOKEN) {
5117            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5118              !!!cp ('t305');
5119              !!!parse-error (type => 'after html:/'.$token->{tag_name});
5120              
5121              $self->{insertion_mode} = AFTER_BODY_IM;
5122              ## Reprocess in the "main" phase, "after body" insertion mode...
5123            } else {
5124              !!!cp ('t306');
5125            }
5126    
5127            ## "after body" insertion mode
5128            if ($token->{tag_name} eq 'html') {
5129              if (defined $self->{inner_html_node}) {
5130                !!!cp ('t307');
5131                !!!parse-error (type => 'unmatched end tag:html');
5132                ## Ignore the token
5133                !!!next-token;
5134                redo B;
5135              } else {
5136                !!!cp ('t308');
5137                $self->{insertion_mode} = AFTER_HTML_BODY_IM;
5138                !!!next-token;
5139                redo B;
5140              }
5141            } else {
5142              !!!cp ('t309');
5143              !!!parse-error (type => 'after body:/'.$token->{tag_name});
5144    
5145              $self->{insertion_mode} = IN_BODY_IM;
5146              ## reprocess
5147              redo B;
5148            }
5149          } else {
5150            die "$0: $token->{type}: Unknown token type";
5151          }
5152        } elsif ($self->{insertion_mode} & FRAME_IMS) {
5153          if ($token->{type} == CHARACTER_TOKEN) {
5154            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5155              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5156              
5157              unless (length $token->{data}) {
5158                !!!cp ('t310');
5159                !!!next-token;
5160                redo B;
5161              }
5162            }
5163            
5164            if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
5165              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5166                !!!cp ('t311');
5167                !!!parse-error (type => 'in frameset:#character');
5168              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
5169                !!!cp ('t312');
5170                !!!parse-error (type => 'after frameset:#character');
5171              } else { # "after html frameset"
5172                !!!cp ('t313');
5173                !!!parse-error (type => 'after html:#character');
5174    
5175                $self->{insertion_mode} = AFTER_FRAMESET_IM;
5176                ## Reprocess in the "main" phase, "after frameset"...
5177                !!!parse-error (type => 'after frameset:#character');
5178              }
5179              
5180              ## Ignore the token.
5181              if (length $token->{data}) {
5182                !!!cp ('t314');
5183                ## reprocess the rest of characters
5184              } else {
5185                !!!cp ('t315');
5186                !!!next-token;
5187              }
5188              redo B;
5189            }
5190            
5191            die qq[$0: Character "$token->{data}"];
5192          } elsif ($token->{type} == START_TAG_TOKEN) {
5193            if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
5194              !!!cp ('t316');
5195              !!!parse-error (type => 'after html:'.$token->{tag_name});
5196    
5197              $self->{insertion_mode} = AFTER_FRAMESET_IM;
5198              ## Process in the "main" phase, "after frameset" insertion mode...
5199            } else {
5200              !!!cp ('t317');
5201            }
5202    
5203            if ($token->{tag_name} eq 'frameset' and
5204                $self->{insertion_mode} == IN_FRAMESET_IM) {
5205              !!!cp ('t318');
5206              !!!insert-element ($token->{tag_name}, $token->{attributes});
5207              !!!next-token;
5208              redo B;
5209            } elsif ($token->{tag_name} eq 'frame' and
5210                     $self->{insertion_mode} == IN_FRAMESET_IM) {
5211              !!!cp ('t319');
5212              !!!insert-element ($token->{tag_name}, $token->{attributes});
5213              pop @{$self->{open_elements}};
5214              !!!next-token;
5215              redo B;
5216            } elsif ($token->{tag_name} eq 'noframes') {
5217              !!!cp ('t320');
5218              ## NOTE: As if in body.
5219              $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
5220              redo B;
5221            } else {
5222              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5223                !!!cp ('t321');
5224                !!!parse-error (type => 'in frameset:'.$token->{tag_name});
5225              } else {
5226                !!!cp ('t322');
5227                !!!parse-error (type => 'after frameset:'.$token->{tag_name});
5228              }
5229              ## Ignore the token
5230              !!!next-token;
5231              redo B;
5232            }
5233          } elsif ($token->{type} == END_TAG_TOKEN) {
5234            if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
5235              !!!cp ('t323');
5236              !!!parse-error (type => 'after html:/'.$token->{tag_name});
5237    
5238              $self->{insertion_mode} = AFTER_FRAMESET_IM;
5239              ## Process in the "main" phase, "after frameset" insertion mode...
5240            } else {
5241              !!!cp ('t324');
5242            }
5243    
5244            if ($token->{tag_name} eq 'frameset' and
5245                $self->{insertion_mode} == IN_FRAMESET_IM) {
5246              if ($self->{open_elements}->[-1]->[1] eq 'html' and
5247                  @{$self->{open_elements}} == 1) {
5248                !!!cp ('t325');
5249                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5250                ## Ignore the token
5251                !!!next-token;
5252              } else {
5253                !!!cp ('t326');
5254                pop @{$self->{open_elements}};
5255                !!!next-token;
5256              }
5257    
5258              if (not defined $self->{inner_html_node} and
5259                  $self->{open_elements}->[-1]->[1] ne 'frameset') {
5260                !!!cp ('t327');
5261                $self->{insertion_mode} = AFTER_FRAMESET_IM;
5262              } else {
5263                !!!cp ('t328');
5264              }
5265              redo B;
5266            } elsif ($token->{tag_name} eq 'html' and
5267                     $self->{insertion_mode} == AFTER_FRAMESET_IM) {
5268              !!!cp ('t329');
5269              $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
5270              !!!next-token;
5271              redo B;
5272            } else {
5273              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5274                !!!cp ('t330');
5275                !!!parse-error (type => 'in frameset:/'.$token->{tag_name});
5276              } else {
5277                !!!cp ('t331');
5278                !!!parse-error (type => 'after frameset:/'.$token->{tag_name});
5279              }
5280              ## Ignore the token
5281              !!!next-token;
5282              redo B;
5283            }
5284          } else {
5285            die "$0: $token->{type}: Unknown token type";
5286          }
5287    
5288          ## ISSUE: An issue in spec here
5289        } else {
5290          die "$0: $self->{insertion_mode}: Unknown insertion mode";
5291        }
5292    
5293        ## "in body" insertion mode
5294        if ($token->{type} == START_TAG_TOKEN) {
5295        if ($token->{tag_name} eq 'script') {        if ($token->{tag_name} eq 'script') {
5296            !!!cp ('t332');
5297          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
5298          $script_start_tag->($insert);          $script_start_tag->($insert);
5299          return;          redo B;
5300        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
5301            !!!cp ('t333');
5302          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
5303          $parse_rcdata->('CDATA', $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
5304          return;          redo B;
5305        } elsif ({        } elsif ({
5306                  base => 1, link => 1,                  base => 1, link => 1,
5307                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5308            !!!cp ('t334');
5309          ## NOTE: This is an "as if in head" code clone, only "-t" differs          ## NOTE: This is an "as if in head" code clone, only "-t" differs
5310          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5311          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
5312          !!!next-token;          !!!next-token;
5313          return;          redo B;
5314        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
5315          ## NOTE: This is an "as if in head" code clone, only "-t" differs          ## NOTE: This is an "as if in head" code clone, only "-t" differs
5316          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5317          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
5318    
5319          unless ($self->{confident}) {          unless ($self->{confident}) {
           my $charset;  
5320            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) { ## TODO: And if supported
5321              $charset = $token->{attributes}->{charset}->{value};              !!!cp ('t335');
5322            }              $self->{change_encoding}
5323            if ($token->{attributes}->{'http-equiv'}) {                  ->($self, $token->{attributes}->{charset}->{value});
5324                
5325                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
5326                    ->set_user_data (manakai_has_reference =>
5327                                         $token->{attributes}->{charset}
5328                                             ->{has_reference});
5329              } elsif ($token->{attributes}->{content}) {
5330              ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.              ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
5331              if ($token->{attributes}->{'http-equiv'}->{value}              if ($token->{attributes}->{content}->{value}
5332                  =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
5333                        [\x09-\x0D\x20]*=
5334                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
5335                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
5336                $charset = defined $1 ? $1 : defined $2 ? $2 : $3;                !!!cp ('t336');
5337              } ## TODO: And if supported                $self->{change_encoding}
5338                      ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);
5339                  $meta_el->[0]->get_attribute_node_ns (undef, 'content')
5340                      ->set_user_data (manakai_has_reference =>
5341                                           $token->{attributes}->{content}
5342                                                 ->{has_reference});
5343                }
5344              }
5345            } else {
5346              if ($token->{attributes}->{charset}) {
5347                !!!cp ('t337');
5348                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
5349                    ->set_user_data (manakai_has_reference =>
5350                                         $token->{attributes}->{charset}
5351                                             ->{has_reference});
5352              }
5353              if ($token->{attributes}->{content}) {
5354                !!!cp ('t338');
5355                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
5356                    ->set_user_data (manakai_has_reference =>
5357                                         $token->{attributes}->{content}
5358                                             ->{has_reference});
5359            }            }
           ## TODO: Change the encoding  
5360          }          }
5361    
5362          !!!next-token;          !!!next-token;
5363          return;          redo B;
5364        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
5365            !!!cp ('t341');
5366          !!!parse-error (type => 'in body:title');          !!!parse-error (type => 'in body:title');
5367          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
5368          $parse_rcdata->('RCDATA', sub {          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {
5369            if (defined $self->{head_element}) {            if (defined $self->{head_element}) {
5370                !!!cp ('t339');
5371              $self->{head_element}->append_child ($_[0]);              $self->{head_element}->append_child ($_[0]);
5372            } else {            } else {
5373                !!!cp ('t340');
5374              $insert->($_[0]);              $insert->($_[0]);
5375            }            }
5376          });          });
5377          return;          redo B;
5378        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
5379          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body:body');
5380                                
5381          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
5382              $self->{open_elements}->[1]->[1] ne 'body') {              $self->{open_elements}->[1]->[1] ne 'body') {
5383              !!!cp ('t342');
5384            ## Ignore the token            ## Ignore the token
5385          } else {          } else {
5386            my $body_el = $self->{open_elements}->[1]->[0];            my $body_el = $self->{open_elements}->[1]->[0];
5387            for my $attr_name (keys %{$token->{attributes}}) {            for my $attr_name (keys %{$token->{attributes}}) {
5388              unless ($body_el->has_attribute_ns (undef, $attr_name)) {              unless ($body_el->has_attribute_ns (undef, $attr_name)) {
5389                  !!!cp ('t343');
5390                $body_el->set_attribute_ns                $body_el->set_attribute_ns
5391                  (undef, [undef, $attr_name],                  (undef, [undef, $attr_name],
5392                   $token->{attributes}->{$attr_name}->{value});                   $token->{attributes}->{$attr_name}->{value});
# Line 2556  sub _tree_construction_main ($) { Line 5394  sub _tree_construction_main ($) {
5394            }            }
5395          }          }
5396          !!!next-token;          !!!next-token;
5397          return;          redo B;
5398        } elsif ({        } elsif ({
5399                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
5400                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1, listing => 1,
# Line 2566  sub _tree_construction_main ($) { Line 5404  sub _tree_construction_main ($) {
5404          ## has a p element in scope          ## has a p element in scope
5405          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5406            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
5407                !!!cp ('t344');
5408              !!!back-token;              !!!back-token;
5409              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5410              return;              redo B;
5411            } elsif ({            } elsif ({
5412                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
5413                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5414                     }->{$_->[1]}) {                     }->{$_->[1]}) {
5415                !!!cp ('t345');
5416              last INSCOPE;              last INSCOPE;
5417            }            }
5418          } # INSCOPE          } # INSCOPE
# Line 2580  sub _tree_construction_main ($) { Line 5420  sub _tree_construction_main ($) {
5420          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5421          if ($token->{tag_name} eq 'pre') {          if ($token->{tag_name} eq 'pre') {
5422            !!!next-token;            !!!next-token;
5423            if ($token->{type} eq 'character') {            if ($token->{type} == CHARACTER_TOKEN) {
5424              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
5425              unless (length $token->{data}) {              unless (length $token->{data}) {
5426                  !!!cp ('t346');
5427                !!!next-token;                !!!next-token;
5428                } else {
5429                  !!!cp ('t349');
5430              }              }
5431              } else {
5432                !!!cp ('t348');
5433            }            }
5434          } else {          } else {
5435              !!!cp ('t347');
5436            !!!next-token;            !!!next-token;
5437          }          }
5438          return;          redo B;
5439        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
5440          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
5441              !!!cp ('t350');
5442            !!!parse-error (type => 'in form:form');            !!!parse-error (type => 'in form:form');
5443            ## Ignore the token            ## Ignore the token
5444            !!!next-token;            !!!next-token;
5445            return;            redo B;
5446          } else {          } else {
5447            ## has a p element in scope            ## has a p element in scope
5448            INSCOPE: for (reverse @{$self->{open_elements}}) {            INSCOPE: for (reverse @{$self->{open_elements}}) {
5449              if ($_->[1] eq 'p') {              if ($_->[1] eq 'p') {
5450                  !!!cp ('t351');
5451                !!!back-token;                !!!back-token;
5452                $token = {type => 'end tag', tag_name => 'p'};                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5453                return;                redo B;
5454              } elsif ({              } elsif ({
5455                        table => 1, caption => 1, td => 1, th => 1,                        table => 1, caption => 1, td => 1, th => 1,
5456                        button => 1, marquee => 1, object => 1, html => 1,                        button => 1, marquee => 1, object => 1, html => 1,
5457                       }->{$_->[1]}) {                       }->{$_->[1]}) {
5458                  !!!cp ('t352');
5459                last INSCOPE;                last INSCOPE;
5460              }              }
5461            } # INSCOPE            } # INSCOPE
# Line 2614  sub _tree_construction_main ($) { Line 5463  sub _tree_construction_main ($) {
5463            !!!insert-element-t ($token->{tag_name}, $token->{attributes});            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5464            $self->{form_element} = $self->{open_elements}->[-1]->[0];            $self->{form_element} = $self->{open_elements}->[-1]->[0];
5465            !!!next-token;            !!!next-token;
5466            return;            redo B;
5467          }          }
5468        } elsif ($token->{tag_name} eq 'li') {        } elsif ($token->{tag_name} eq 'li') {
5469          ## has a p element in scope          ## has a p element in scope
5470          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5471            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
5472                !!!cp ('t353');
5473              !!!back-token;              !!!back-token;
5474              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5475              return;              redo B;
5476            } elsif ({            } elsif ({
5477                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
5478                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5479                     }->{$_->[1]}) {                     }->{$_->[1]}) {
5480                !!!cp ('t354');
5481              last INSCOPE;              last INSCOPE;
5482            }            }
5483          } # INSCOPE          } # INSCOPE
# Line 2638  sub _tree_construction_main ($) { Line 5489  sub _tree_construction_main ($) {
5489            ## Step 2            ## Step 2
5490            if ($node->[1] eq 'li') {            if ($node->[1] eq 'li') {
5491              if ($i != -1) {              if ($i != -1) {
5492                  !!!cp ('t355');
5493                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'end tag missing:'.
5494                                $self->{open_elements}->[-1]->[1]);                                $self->{open_elements}->[-1]->[1]);
5495                } else {
5496                  !!!cp ('t356');
5497              }              }
5498              splice @{$self->{open_elements}}, $i;              splice @{$self->{open_elements}}, $i;
5499              last LI;              last LI;
5500              } else {
5501                !!!cp ('t357');
5502            }            }
5503                        
5504            ## Step 3            ## Step 3
# Line 2651  sub _tree_construction_main ($) { Line 5507  sub _tree_construction_main ($) {
5507                ($special_category->{$node->[1]} or                ($special_category->{$node->[1]} or
5508                 $scoping_category->{$node->[1]}) and                 $scoping_category->{$node->[1]}) and
5509                $node->[1] ne 'address' and $node->[1] ne 'div') {                $node->[1] ne 'address' and $node->[1] ne 'div') {
5510                !!!cp ('t358');
5511              last LI;              last LI;
5512            }            }
5513                        
5514              !!!cp ('t359');
5515            ## Step 4            ## Step 4
5516            $i--;            $i--;
5517            $node = $self->{open_elements}->[$i];            $node = $self->{open_elements}->[$i];
# Line 2662  sub _tree_construction_main ($) { Line 5520  sub _tree_construction_main ($) {
5520                        
5521          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5522          !!!next-token;          !!!next-token;
5523          return;          redo B;
5524        } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {        } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {
5525          ## has a p element in scope          ## has a p element in scope
5526          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5527            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
5528                !!!cp ('t360');
5529              !!!back-token;              !!!back-token;
5530              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5531              return;              redo B;
5532            } elsif ({            } elsif ({
5533                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
5534                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5535                     }->{$_->[1]}) {                     }->{$_->[1]}) {
5536                !!!cp ('t361');
5537              last INSCOPE;              last INSCOPE;
5538            }            }
5539          } # INSCOPE          } # INSCOPE
# Line 2685  sub _tree_construction_main ($) { Line 5545  sub _tree_construction_main ($) {
5545            ## Step 2            ## Step 2
5546            if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {            if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {
5547              if ($i != -1) {              if ($i != -1) {
5548                  !!!cp ('t362');
5549                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'end tag missing:'.
5550                                $self->{open_elements}->[-1]->[1]);                                $self->{open_elements}->[-1]->[1]);
5551                } else {
5552                  !!!cp ('t363');
5553              }              }
5554              splice @{$self->{open_elements}}, $i;              splice @{$self->{open_elements}}, $i;
5555              last LI;              last LI;
5556              } else {
5557                !!!cp ('t364');
5558            }            }
5559                        
5560            ## Step 3            ## Step 3
# Line 2698  sub _tree_construction_main ($) { Line 5563  sub _tree_construction_main ($) {
5563                ($special_category->{$node->[1]} or                ($special_category->{$node->[1]} or
5564                 $scoping_category->{$node->[1]}) and                 $scoping_category->{$node->[1]}) and
5565                $node->[1] ne 'address' and $node->[1] ne 'div') {                $node->[1] ne 'address' and $node->[1] ne 'div') {
5566                !!!cp ('t365');
5567              last LI;              last LI;
5568            }            }
5569                        
5570              !!!cp ('t366');
5571            ## Step 4            ## Step 4
5572            $i--;            $i--;
5573            $node = $self->{open_elements}->[$i];            $node = $self->{open_elements}->[$i];
# Line 2709  sub _tree_construction_main ($) { Line 5576  sub _tree_construction_main ($) {
5576                        
5577          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5578          !!!next-token;          !!!next-token;
5579          return;          redo B;
5580        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
5581          ## has a p element in scope          ## has a p element in scope
5582          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5583            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
5584                !!!cp ('t367');
5585              !!!back-token;              !!!back-token;
5586              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5587              return;              redo B;
5588            } elsif ({            } elsif ({
5589                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
5590                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5591                     }->{$_->[1]}) {                     }->{$_->[1]}) {
5592                !!!cp ('t368');
5593              last INSCOPE;              last INSCOPE;
5594            }            }
5595          } # INSCOPE          } # INSCOPE
5596                        
5597          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5598                        
5599          $self->{content_model_flag} = 'PLAINTEXT';          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
5600                        
5601          !!!next-token;          !!!next-token;
5602          return;          redo B;
5603        } elsif ({        } elsif ({
5604                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5605                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 2738  sub _tree_construction_main ($) { Line 5607  sub _tree_construction_main ($) {
5607          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5608            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
5609            if ($node->[1] eq 'p') {            if ($node->[1] eq 'p') {
5610                !!!cp ('t369');
5611              !!!back-token;              !!!back-token;
5612              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5613              return;              redo B;
5614            } elsif ({            } elsif ({
5615                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
5616                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5617                     }->{$node->[1]}) {                     }->{$node->[1]}) {
5618                !!!cp ('t370');
5619              last INSCOPE;              last INSCOPE;
5620            }            }
5621          } # INSCOPE          } # INSCOPE
# Line 2775  sub _tree_construction_main ($) { Line 5646  sub _tree_construction_main ($) {
5646          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5647                        
5648          !!!next-token;          !!!next-token;
5649          return;          redo B;
5650        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
5651          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
5652            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
5653            if ($node->[1] eq 'a') {            if ($node->[1] eq 'a') {
5654                !!!cp ('t371');
5655              !!!parse-error (type => 'in a:a');              !!!parse-error (type => 'in a:a');
5656                            
5657              !!!back-token;              !!!back-token;
5658              $token = {type => 'end tag', tag_name => 'a'};              $token = {type => END_TAG_TOKEN, tag_name => 'a'};
5659              $formatting_end_tag->($token->{tag_name});              $formatting_end_tag->($token->{tag_name});
5660                            
5661              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
5662                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
5663                    !!!cp ('t372');
5664                  splice @$active_formatting_elements, $_, 1;                  splice @$active_formatting_elements, $_, 1;
5665                  last AFE2;                  last AFE2;
5666                }                }
5667              } # AFE2              } # AFE2
5668              OE: for (reverse 0..$#{$self->{open_elements}}) {              OE: for (reverse 0..$#{$self->{open_elements}}) {
5669                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
5670                    !!!cp ('t373');
5671                  splice @{$self->{open_elements}}, $_, 1;                  splice @{$self->{open_elements}}, $_, 1;
5672                  last OE;                  last OE;
5673                }                }
5674              } # OE              } # OE
5675              last AFE;              last AFE;
5676            } elsif ($node->[0] eq '#marker') {            } elsif ($node->[0] eq '#marker') {
5677                !!!cp ('t374');
5678              last AFE;              last AFE;
5679            }            }
5680          } # AFE          } # AFE
# Line 2810  sub _tree_construction_main ($) { Line 5685  sub _tree_construction_main ($) {
5685          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
5686    
5687          !!!next-token;          !!!next-token;
5688          return;          redo B;
5689        } elsif ({        } elsif ({
5690                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
5691                  s => 1, small => 1, strile => 1,                  s => 1, small => 1, strile => 1,
5692                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
5693                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5694            !!!cp ('t375');
5695          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5696                    
5697          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5698          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
5699                    
5700          !!!next-token;          !!!next-token;
5701          return;          redo B;
5702        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
5703          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5704    
# Line 2830  sub _tree_construction_main ($) { Line 5706  sub _tree_construction_main ($) {
5706          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5707            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
5708            if ($node->[1] eq 'nobr') {            if ($node->[1] eq 'nobr') {
5709              !!!parse-error (type => 'not closed:nobr');              !!!cp ('t376');
5710                !!!parse-error (type => 'in nobr:nobr');
5711              !!!back-token;              !!!back-token;
5712              $token = {type => 'end tag', tag_name => 'nobr'};              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};
5713              return;              redo B;
5714            } elsif ({            } elsif ({
5715                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
5716                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5717                     }->{$node->[1]}) {                     }->{$node->[1]}) {
5718                !!!cp ('t377');
5719              last INSCOPE;              last INSCOPE;
5720            }            }
5721          } # INSCOPE          } # INSCOPE
# Line 2846  sub _tree_construction_main ($) { Line 5724  sub _tree_construction_main ($) {
5724          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
5725                    
5726          !!!next-token;          !!!next-token;
5727          return;          redo B;
5728        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
5729          ## has a button element in scope          ## has a button element in scope
5730          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5731            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
5732            if ($node->[1] eq 'button') {            if ($node->[1] eq 'button') {
5733                !!!cp ('t378');
5734              !!!parse-error (type => 'in button:button');              !!!parse-error (type => 'in button:button');
5735              !!!back-token;              !!!back-token;
5736              $token = {type => 'end tag', tag_name => 'button'};              $token = {type => END_TAG_TOKEN, tag_name => 'button'};
5737              return;              redo B;
5738            } elsif ({            } elsif ({
5739                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
5740                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5741                     }->{$node->[1]}) {                     }->{$node->[1]}) {
5742                !!!cp ('t379');
5743              last INSCOPE;              last INSCOPE;
5744            }            }
5745          } # INSCOPE          } # INSCOPE
# Line 2870  sub _tree_construction_main ($) { Line 5750  sub _tree_construction_main ($) {
5750          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
5751    
5752          !!!next-token;          !!!next-token;
5753          return;          redo B;
5754        } elsif ($token->{tag_name} eq 'marquee' or        } elsif ($token->{tag_name} eq 'marquee' or
5755                 $token->{tag_name} eq 'object') {                 $token->{tag_name} eq 'object') {
5756            !!!cp ('t380');
5757          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5758                    
5759          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5760          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
5761                    
5762          !!!next-token;          !!!next-token;
5763          return;          redo B;
5764        } elsif ($token->{tag_name} eq 'xmp') {        } elsif ($token->{tag_name} eq 'xmp') {
5765            !!!cp ('t381');
5766          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5767          $parse_rcdata->('CDATA', $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
5768          return;          redo B;
5769        } elsif ($token->{tag_name} eq 'table') {        } elsif ($token->{tag_name} eq 'table') {
5770          ## has a p element in scope          ## has a p element in scope
5771          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5772            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
5773                !!!cp ('t382');
5774              !!!back-token;              !!!back-token;
5775              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5776              return;              redo B;
5777            } elsif ({            } elsif ({
5778                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
5779                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5780                     }->{$_->[1]}) {                     }->{$_->[1]}) {
5781                !!!cp ('t383');
5782              last INSCOPE;              last INSCOPE;
5783            }            }
5784          } # INSCOPE          } # INSCOPE
5785                        
5786          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5787                        
5788          $self->{insertion_mode} = 'in table';          $self->{insertion_mode} = IN_TABLE_IM;
5789                        
5790          !!!next-token;          !!!next-token;
5791          return;          redo B;
5792        } elsif ({        } elsif ({
5793                  area => 1, basefont => 1, bgsound => 1, br => 1,                  area => 1, basefont => 1, bgsound => 1, br => 1,
5794                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
5795                  image => 1,                  image => 1,
5796                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5797          if ($token->{tag_name} eq 'image') {          if ($token->{tag_name} eq 'image') {
5798              !!!cp ('t384');
5799            !!!parse-error (type => 'image');            !!!parse-error (type => 'image');
5800            $token->{tag_name} = 'img';            $token->{tag_name} = 'img';
5801            } else {
5802              !!!cp ('t385');
5803          }          }
5804    
5805          ## NOTE: There is an "as if <br>" code clone.          ## NOTE: There is an "as if <br>" code clone.
# Line 2922  sub _tree_construction_main ($) { Line 5809  sub _tree_construction_main ($) {
5809          pop @{$self->{open_elements}};          pop @{$self->{open_elements}};
5810                    
5811          !!!next-token;          !!!next-token;
5812          return;          redo B;
5813        } elsif ($token->{tag_name} eq 'hr') {        } elsif ($token->{tag_name} eq 'hr') {
5814          ## has a p element in scope          ## has a p element in scope
5815          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5816            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
5817                !!!cp ('t386');
5818              !!!back-token;              !!!back-token;
5819              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5820              return;              redo B;
5821            } elsif ({            } elsif ({
5822                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
5823                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5824                     }->{$_->[1]}) {                     }->{$_->[1]}) {
5825                !!!cp ('t387');
5826              last INSCOPE;              last INSCOPE;
5827            }            }
5828          } # INSCOPE          } # INSCOPE
# Line 2942  sub _tree_construction_main ($) { Line 5831  sub _tree_construction_main ($) {
5831          pop @{$self->{open_elements}};          pop @{$self->{open_elements}};
5832                        
5833          !!!next-token;          !!!next-token;
5834          return;          redo B;
5835        } elsif ($token->{tag_name} eq 'input') {        } elsif ($token->{tag_name} eq 'input') {
5836            !!!cp ('t388');
5837          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5838                    
5839          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
# Line 2951  sub _tree_construction_main ($) { Line 5841  sub _tree_construction_main ($) {
5841          pop @{$self->{open_elements}};          pop @{$self->{open_elements}};
5842                    
5843          !!!next-token;          !!!next-token;
5844          return;          redo B;
5845        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
5846          !!!parse-error (type => 'isindex');          !!!parse-error (type => 'isindex');
5847                    
5848          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
5849              !!!cp ('t389');
5850            ## Ignore the token            ## Ignore the token
5851            !!!next-token;            !!!next-token;
5852            return;            redo B;
5853          } else {          } else {
5854            my $at = $token->{attributes};            my $at = $token->{attributes};
5855            my $form_attrs;            my $form_attrs;
# Line 2968  sub _tree_construction_main ($) { Line 5859  sub _tree_construction_main ($) {
5859            delete $at->{action};            delete $at->{action};
5860            delete $at->{prompt};            delete $at->{prompt};
5861            my @tokens = (            my @tokens = (
5862                          {type => 'start tag', tag_name => 'form',                          {type => START_TAG_TOKEN, tag_name => 'form',
5863                           attributes => $form_attrs},                           attributes => $form_attrs},
5864                          {type => 'start tag', tag_name => 'hr'},                          {type => START_TAG_TOKEN, tag_name => 'hr'},
5865                          {type => 'start tag', tag_name => 'p'},                          {type => START_TAG_TOKEN, tag_name => 'p'},
5866                          {type => 'start tag', tag_name => 'label'},                          {type => START_TAG_TOKEN, tag_name => 'label'},
5867                         );                         );
5868            if ($prompt_attr) {            if ($prompt_attr) {
5869              push @tokens, {type => 'character', data => $prompt_attr->{value}};              !!!cp ('t390');
5870                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};
5871            } else {            } else {
5872              push @tokens, {type => 'character',              !!!cp ('t391');
5873                push @tokens, {type => CHARACTER_TOKEN,
5874                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD
5875              ## TODO: make this configurable              ## TODO: make this configurable
5876            }            }
5877            push @tokens,            push @tokens,
5878                          {type => 'start tag', tag_name => 'input', attributes => $at},                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},
5879                          #{type => 'character', data => ''}, # SHOULD                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
5880                          {type => 'end tag', tag_name => 'label'},                          {type => END_TAG_TOKEN, tag_name => 'label'},
5881                          {type => 'end tag', tag_name => 'p'},                          {type => END_TAG_TOKEN, tag_name => 'p'},
5882                          {type => 'start tag', tag_name => 'hr'},                          {type => START_TAG_TOKEN, tag_name => 'hr'},
5883                          {type => 'end tag', tag_name => 'form'};                          {type => END_TAG_TOKEN, tag_name => 'form'};
5884            $token = shift @tokens;            $token = shift @tokens;
5885            !!!back-token (@tokens);            !!!back-token (@tokens);
5886            return;            redo B;
5887          }          }
5888        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
5889          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
# Line 2998  sub _tree_construction_main ($) { Line 5891  sub _tree_construction_main ($) {
5891          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $token->{tag_name}, $token->{attributes});
5892                    
5893          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
5894          $self->{content_model_flag} = 'RCDATA';          $self->{content_model} = RCDATA_CONTENT_MODEL;
5895          delete $self->{escape}; # MUST          delete $self->{escape}; # MUST
5896                    
5897          $insert->($el);          $insert->($el);
5898                    
5899          my $text = '';          my $text = '';
5900          !!!next-token;          !!!next-token;
5901          if ($token->{type} eq 'character') {          if ($token->{type} == CHARACTER_TOKEN) {
5902            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
5903            unless (length $token->{data}) {            unless (length $token->{data}) {
5904                !!!cp ('t392');
5905              !!!next-token;              !!!next-token;
5906              } else {
5907                !!!cp ('t393');
5908            }            }
5909            } else {
5910              !!!cp ('t394');
5911          }          }
5912          while ($token->{type} eq 'character') {          while ($token->{type} == CHARACTER_TOKEN) {
5913              !!!cp ('t395');
5914            $text .= $token->{data};            $text .= $token->{data};
5915            !!!next-token;            !!!next-token;
5916          }          }
5917          if (length $text) {          if (length $text) {
5918              !!!cp ('t396');
5919            $el->manakai_append_text ($text);            $el->manakai_append_text ($text);
5920          }          }
5921                    
5922          $self->{content_model_flag} = 'PCDATA';          $self->{content_model} = PCDATA_CONTENT_MODEL;
5923                    
5924          if ($token->{type} eq 'end tag' and          if ($token->{type} == END_TAG_TOKEN and
5925              $token->{tag_name} eq $tag_name) {              $token->{tag_name} eq $tag_name) {
5926              !!!cp ('t397');
5927            ## Ignore the token            ## Ignore the token
5928          } else {          } else {
5929              !!!cp ('t398');
5930            !!!parse-error (type => 'in RCDATA:#'.$token->{type});            !!!parse-error (type => 'in RCDATA:#'.$token->{type});
5931          }          }
5932          !!!next-token;          !!!next-token;
5933          return;          redo B;
5934        } elsif ({        } elsif ({
5935                  iframe => 1,                  iframe => 1,
5936                  noembed => 1,                  noembed => 1,
5937                  noframes => 1,                  noframes => 1,
5938                  noscript => 0, ## TODO: 1 if scripting is enabled                  noscript => 0, ## TODO: 1 if scripting is enabled
5939                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5940          $parse_rcdata->('CDATA', $insert);          !!!cp ('t399');
5941          return;          ## NOTE: There is an "as if in body" code clone.
5942            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
5943            redo B;
5944        } elsif ($token->{tag_name} eq 'select') {        } elsif ($token->{tag_name} eq 'select') {
5945            !!!cp ('t400');
5946          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5947                    
5948          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5949                    
5950          $self->{insertion_mode} = 'in select';          $self->{insertion_mode} = IN_SELECT_IM;
5951          !!!next-token;          !!!next-token;
5952          return;          redo B;
5953        } elsif ({        } elsif ({
5954                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
5955                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
5956                  tbody => 1, td => 1, tfoot => 1, th => 1,                  tbody => 1, td => 1, tfoot => 1, th => 1,
5957                  thead => 1, tr => 1,                  thead => 1, tr => 1,
5958                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5959            !!!cp ('t401');
5960          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!parse-error (type => 'in body:'.$token->{tag_name});
5961          ## Ignore the token          ## Ignore the token
5962          !!!next-token;          !!!next-token;
5963          return;          redo B;
5964                    
5965          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
5966        } else {        } else {
5967            !!!cp ('t402');
5968          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5969                    
5970          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5971                    
5972          !!!next-token;          !!!next-token;
5973          return;          redo B;
5974        }        }
5975      } elsif ($token->{type} eq 'end tag') {      } elsif ($token->{type} == END_TAG_TOKEN) {
5976        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
5977          if (@{$self->{open_elements}} > 1 and          if (@{$self->{open_elements}} > 1 and
5978              $self->{open_elements}->[1]->[1] eq 'body') {              $self->{open_elements}->[1]->[1] eq 'body') {
# Line 3075  sub _tree_construction_main ($) { Line 5982  sub _tree_construction_main ($) {
5982                         th => 1, tr => 1, body => 1, html => 1,                         th => 1, tr => 1, body => 1, html => 1,
5983                       tbody => 1, tfoot => 1, thead => 1,                       tbody => 1, tfoot => 1, thead => 1,
5984                      }->{$_->[1]}) {                      }->{$_->[1]}) {
5985                  !!!cp ('t403');
5986                !!!parse-error (type => 'not closed:'.$_->[1]);                !!!parse-error (type => 'not closed:'.$_->[1]);
5987                } else {
5988                  !!!cp ('t404');
5989              }              }
5990            }            }
5991    
5992            $self->{insertion_mode} = 'after body';            $self->{insertion_mode} = AFTER_BODY_IM;
5993            !!!next-token;            !!!next-token;
5994            return;            redo B;
5995          } else {          } else {
5996              !!!cp ('t405');
5997            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5998            ## Ignore the token            ## Ignore the token
5999            !!!next-token;            !!!next-token;
6000            return;            redo B;
6001          }          }
6002        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
6003          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') {
6004            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
6005            if ($self->{open_elements}->[-1]->[1] ne 'body') {            if ($self->{open_elements}->[-1]->[1] ne 'body') {
6006                !!!cp ('t406');
6007              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);
6008              } else {
6009                !!!cp ('t407');
6010            }            }
6011            $self->{insertion_mode} = 'after body';            $self->{insertion_mode} = AFTER_BODY_IM;
6012            ## reprocess            ## reprocess
6013            return;            redo B;
6014          } else {          } else {
6015              !!!cp ('t408');
6016            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
6017            ## Ignore the token            ## Ignore the token
6018            !!!next-token;            !!!next-token;
6019            return;            redo B;
6020          }          }
6021        } elsif ({        } elsif ({
6022                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
# Line 3125  sub _tree_construction_main ($) { Line 6040  sub _tree_construction_main ($) {
6040                   td => 1, th => 1, tr => 1,                   td => 1, th => 1, tr => 1,
6041                   tbody => 1, tfoot=> 1, thead => 1,                   tbody => 1, tfoot=> 1, thead => 1,
6042                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
6043                  !!!cp ('t409');
6044                !!!back-token;                !!!back-token;
6045                $token = {type => 'end tag',                $token = {type => END_TAG_TOKEN,
6046                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
6047                return;                redo B;
6048              }              }
6049                
6050                !!!cp ('t410');
6051              $i = $_;              $i = $_;
6052              last INSCOPE unless $token->{tag_name} eq 'p';              last INSCOPE unless $token->{tag_name} eq 'p';
6053            } elsif ({            } elsif ({
6054                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
6055                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
6056                     }->{$node->[1]}) {                     }->{$node->[1]}) {
6057                !!!cp ('t411');
6058              last INSCOPE;              last INSCOPE;
6059            }            }
6060          } # INSCOPE          } # INSCOPE
6061                    
6062          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
6063            if (defined $i) {            if (defined $i) {
6064                !!!cp ('t412');
6065              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
6066            } else {            } else {
6067                !!!cp ('t413');
6068              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
6069            }            }
6070          }          }
6071                    
6072          if (defined $i) {          if (defined $i) {
6073              !!!cp ('t414');
6074            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6075          } elsif ($token->{tag_name} eq 'p') {          } elsif ($token->{tag_name} eq 'p') {
6076              !!!cp ('t415');
6077            ## As if <p>, then reprocess the current token            ## As if <p>, then reprocess the current token
6078            my $el;            my $el;
6079            !!!create-element ($el, 'p');            !!!create-element ($el, 'p');
6080            $insert->($el);            $insert->($el);
6081            } else {
6082              !!!cp ('t416');
6083          }          }
6084          $clear_up_to_marker->()          $clear_up_to_marker->()
6085            if {            if {
6086              button => 1, marquee => 1, object => 1,              button => 1, marquee => 1, object => 1,
6087            }->{$token->{tag_name}};            }->{$token->{tag_name}};
6088          !!!next-token;          !!!next-token;
6089          return;          redo B;
6090        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
6091          ## has an element in scope          ## has an element in scope
6092          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 3173  sub _tree_construction_main ($) { Line 6098  sub _tree_construction_main ($) {
6098                   td => 1, th => 1, tr => 1,                   td => 1, th => 1, tr => 1,
6099                   tbody => 1, tfoot=> 1, thead => 1,                   tbody => 1, tfoot=> 1, thead => 1,
6100                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
6101                  !!!cp ('t417');
6102                !!!back-token;                !!!back-token;
6103                $token = {type => 'end tag',                $token = {type => END_TAG_TOKEN,
6104                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
6105                return;                redo B;
6106              }              }
6107                
6108                !!!cp ('t418');
6109              last INSCOPE;              last INSCOPE;
6110            } elsif ({            } elsif ({
6111                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
6112                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
6113                     }->{$node->[1]}) {                     }->{$node->[1]}) {
6114                !!!cp ('t419');
6115              last INSCOPE;              last INSCOPE;
6116            }            }
6117          } # INSCOPE          } # INSCOPE
6118                    
6119          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {
6120              !!!cp ('t420');
6121            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6122          } else {          } else {
6123            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!cp ('t421');
6124              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
6125          }          }
6126    
6127          undef $self->{form_element};          undef $self->{form_element};
6128          !!!next-token;          !!!next-token;
6129          return;          redo B;
6130        } elsif ({        } elsif ({
6131                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6132                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 3212  sub _tree_construction_main ($) { Line 6143  sub _tree_construction_main ($) {
6143                   td => 1, th => 1, tr => 1,                   td => 1, th => 1, tr => 1,
6144                   tbody => 1, tfoot=> 1, thead => 1,                   tbody => 1, tfoot=> 1, thead => 1,
6145                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
6146                  !!!cp ('t422');
6147                !!!back-token;                !!!back-token;
6148                $token = {type => 'end tag',                $token = {type => END_TAG_TOKEN,
6149                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
6150                return;                redo B;
6151              }              }
6152    
6153                !!!cp ('t423');
6154              $i = $_;              $i = $_;
6155              last INSCOPE;              last INSCOPE;
6156            } elsif ({            } elsif ({
6157                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
6158                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
6159                     }->{$node->[1]}) {                     }->{$node->[1]}) {
6160                !!!cp ('t424');
6161              last INSCOPE;              last INSCOPE;
6162            }            }
6163          } # INSCOPE          } # INSCOPE
6164                    
6165          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
6166            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!cp ('t425');
6167              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
6168            } else {
6169              !!!cp ('t426');
6170          }          }
6171                    
6172          splice @{$self->{open_elements}}, $i if defined $i;          splice @{$self->{open_elements}}, $i if defined $i;
6173          !!!next-token;          !!!next-token;
6174          return;          redo B;
6175        } elsif ({        } elsif ({
6176                  a => 1,                  a => 1,
6177                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
6178                  nobr => 1, s => 1, small => 1, strile => 1,                  nobr => 1, s => 1, small => 1, strile => 1,
6179                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
6180                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6181            !!!cp ('t427');
6182          $formatting_end_tag->($token->{tag_name});          $formatting_end_tag->($token->{tag_name});
6183          return;          redo B;
6184        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
6185            !!!cp ('t428');
6186          !!!parse-error (type => 'unmatched end tag:br');          !!!parse-error (type => 'unmatched end tag:br');
6187    
6188          ## As if <br>          ## As if <br>
# Line 3254  sub _tree_construction_main ($) { Line 6194  sub _tree_construction_main ($) {
6194                    
6195          ## Ignore the token.          ## Ignore the token.
6196          !!!next-token;          !!!next-token;
6197          return;          redo B;
6198        } elsif ({        } elsif ({
6199                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
6200                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 3267  sub _tree_construction_main ($) { Line 6207  sub _tree_construction_main ($) {
6207                  table => 1, textarea => 1, wbr => 1,                  table => 1, textarea => 1, wbr => 1,
6208                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
6209                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6210            !!!cp ('t429');
6211          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
6212          ## Ignore the token          ## Ignore the token
6213          !!!next-token;          !!!next-token;
6214          return;          redo B;
6215                    
6216          ## ISSUE: Issue on HTML5 new elements in spec          ## ISSUE: Issue on HTML5 new elements in spec
6217                    
# Line 3287  sub _tree_construction_main ($) { Line 6228  sub _tree_construction_main ($) {
6228              if ({              if ({
6229                   dd => 1, dt => 1, li => 1, p => 1,                   dd => 1, dt => 1, li => 1, p => 1,
6230                   td => 1, th => 1, tr => 1,                   td => 1, th => 1, tr => 1,
6231                   tbody => 1, tfoot=> 1, thead => 1,                   tbody => 1, tfoot => 1, thead => 1,
6232                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
6233                  !!!cp ('t430');
6234                !!!back-token;                !!!back-token;
6235                $token = {type => 'end tag',                $token = {type => END_TAG_TOKEN,
6236                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
6237                return;                redo B;
6238              }              }
6239                    
6240              ## Step 2              ## Step 2
6241              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {
6242                  !!!cp ('t431');
6243                  ## NOTE: <x><y></x>
6244                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
6245                } else {
6246                  !!!cp ('t432');
6247              }              }
6248                            
6249              ## Step 3              ## Step 3
# Line 3311  sub _tree_construction_main ($) { Line 6257  sub _tree_construction_main ($) {
6257                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
6258                  ($special_category->{$node->[1]} or                  ($special_category->{$node->[1]} or
6259                   $scoping_category->{$node->[1]})) {                   $scoping_category->{$node->[1]})) {
6260                  !!!cp ('t433');
6261                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
6262                ## Ignore the token                ## Ignore the token
6263                !!!next-token;                !!!next-token;
6264                last S2;                last S2;
6265              }              }
6266    
6267                !!!cp ('t434');
6268            }            }
6269                        
6270            ## Step 4            ## Step 4
# Line 3325  sub _tree_construction_main ($) { Line 6274  sub _tree_construction_main ($) {
6274            ## Step 5;            ## Step 5;
6275            redo S2;            redo S2;
6276          } # S2          } # S2
6277          return;          redo B;
       }  
     }  
   }; # $in_body  
   
   B: {  
     if ($self->{insertion_mode} ne 'trailing end') {  
       if ($token->{type} eq 'DOCTYPE') {  
         !!!parse-error (type => 'in html:#DOCTYPE');  
         ## Ignore the token  
         ## Stay in the phase  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'start tag' and  
                $token->{tag_name} eq 'html') {  
 ## ISSUE: "aa<html>" is not a parse error.  
 ## ISSUE: "<html>" in fragment is not a parse error.  
         unless ($token->{first_start_tag}) {  
           !!!parse-error (type => 'not first start tag');  
         }  
         my $top_el = $self->{open_elements}->[0]->[0];  
         for my $attr_name (keys %{$token->{attributes}}) {  
           unless ($top_el->has_attribute_ns (undef, $attr_name)) {  
             $top_el->set_attribute_ns  
               (undef, [undef, $attr_name],  
                $token->{attributes}->{$attr_name}->{value});  
           }  
         }  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'end-of-file') {  
         ## Generate implied end tags  
         if ({  
              dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,  
              tbody => 1, tfoot=> 1, thead => 1,  
             }->{$self->{open_elements}->[-1]->[1]}) {  
           !!!back-token;  
           $token = {type => 'end tag', tag_name => $self->{open_elements}->[-1]->[1]};  
           redo B;  
         }  
           
         if (@{$self->{open_elements}} > 2 or  
             (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         } elsif (defined $self->{inner_html_node} and  
                  @{$self->{open_elements}} > 1 and  
                  $self->{open_elements}->[1]->[1] ne 'body') {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         }  
   
         ## Stop parsing  
         last B;  
   
         ## ISSUE: There is an issue in the spec.  
       } else {  
         if ($self->{insertion_mode} eq 'before head') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
             ## As if <head>  
             !!!create-element ($self->{head_element}, 'head');  
             $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});  
             push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
             $self->{insertion_mode} = 'in head';  
             ## reprocess  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             my $attr = $token->{tag_name} eq 'head' ? $token->{attributes} : {};  
             !!!create-element ($self->{head_element}, 'head', $attr);  
             $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});  
             push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
             $self->{insertion_mode} = 'in head';  
             if ($token->{tag_name} eq 'head') {  
               !!!next-token;  
             #} elsif ({  
             #          base => 1, link => 1, meta => 1,  
             #          script => 1, style => 1, title => 1,  
             #         }->{$token->{tag_name}}) {  
             #  ## reprocess  
             } else {  
               ## reprocess  
             }  
             redo B;  
           } elsif ($token->{type} eq 'end tag') {  
             if ({  
                  head => 1, body => 1, html => 1,  
                  p => 1, br => 1,  
                 }->{$token->{tag_name}}) {  
               ## As if <head>  
               !!!create-element ($self->{head_element}, 'head');  
               $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});  
               push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
               $self->{insertion_mode} = 'in head';  
               ## reprocess  
               redo B;  
             } else {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token ## ISSUE: An issue in the spec.  
               !!!next-token;  
               redo B;  
             }  
           } else {  
             die "$0: $token->{type}: Unknown type";  
           }  
         } elsif ($self->{insertion_mode} eq 'in head' or  
                  $self->{insertion_mode} eq 'in head noscript' or  
                  $self->{insertion_mode} eq 'after head') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
               
             #  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ({base => ($self->{insertion_mode} eq 'in head' or  
                           $self->{insertion_mode} eq 'after head'),  
                  link => 1}->{$token->{tag_name}}) {  
               ## NOTE: There is a "as if in head" code clone.  
               if ($self->{insertion_mode} eq 'after head') {  
                 !!!parse-error (type => 'after head:'.$token->{tag_name});  
                 push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
               }  
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.  
               pop @{$self->{open_elements}}  
                   if $self->{insertion_mode} eq 'after head';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'meta') {  
               ## NOTE: There is a "as if in head" code clone.  
               if ($self->{insertion_mode} eq 'after head') {  
                 !!!parse-error (type => 'after head:'.$token->{tag_name});  
                 push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
               }  
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.  
   
               unless ($self->{confident}) {  
                 my $charset;  
                 if ($token->{attributes}->{charset}) { ## TODO: And if supported  
                   $charset = $token->{attributes}->{charset}->{value};  
                 }  
                 if ($token->{attributes}->{'http-equiv'}) {  
                   ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
                   if ($token->{attributes}->{'http-equiv'}->{value}  
                       =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=  
                           [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|  
                           ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {  
                     $charset = defined $1 ? $1 : defined $2 ? $2 : $3;  
                   } ## TODO: And if supported  
                 }  
                 ## TODO: Change the encoding  
               }  
   
               ## TODO: Extracting |charset| from |meta|.  
               pop @{$self->{open_elements}}  
                   if $self->{insertion_mode} eq 'after head';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'title' and  
                      $self->{insertion_mode} eq 'in head') {  
               ## NOTE: There is a "as if in head" code clone.  
               if ($self->{insertion_mode} eq 'after head') {  
                 !!!parse-error (type => 'after head:'.$token->{tag_name});  
                 push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
               }  
               my $parent = defined $self->{head_element} ? $self->{head_element}  
                   : $self->{open_elements}->[-1]->[0];  
               $parse_rcdata->('RCDATA', sub { $parent->append_child ($_[0]) });  
               pop @{$self->{open_elements}}  
                   if $self->{insertion_mode} eq 'after head';  
               redo B;  
             } elsif ($token->{tag_name} eq 'style') {  
               ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and  
               ## insertion mode 'in head')  
               ## NOTE: There is a "as if in head" code clone.  
               if ($self->{insertion_mode} eq 'after head') {  
                 !!!parse-error (type => 'after head:'.$token->{tag_name});  
                 push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
               }  
               $parse_rcdata->('CDATA', $insert_to_current);  
               pop @{$self->{open_elements}}  
                   if $self->{insertion_mode} eq 'after head';  
               redo B;  
             } elsif ($token->{tag_name} eq 'noscript') {  
               if ($self->{insertion_mode} eq 'in head') {  
                 ## NOTE: and scripting is disalbed  
                 !!!insert-element ($token->{tag_name}, $token->{attributes});  
                 $self->{insertion_mode} = 'in head noscript';  
                 !!!next-token;  
                 redo B;  
               } elsif ($self->{insertion_mode} eq 'in head noscript') {  
                 !!!parse-error (type => 'in noscript:noscript');  
                 ## Ignore the token  
                 redo B;  
               } else {  
                 #  
               }  
             } elsif ($token->{tag_name} eq 'head' and  
                      $self->{insertion_mode} ne 'after head') {  
               !!!parse-error (type => 'in head:head'); # or in head noscript  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } elsif ($self->{insertion_mode} ne 'in head noscript' and  
                      $token->{tag_name} eq 'script') {  
               if ($self->{insertion_mode} eq 'after head') {  
                 !!!parse-error (type => 'after head:'.$token->{tag_name});  
                 push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
               }  
               ## NOTE: There is a "as if in head" code clone.  
               $script_start_tag->($insert_to_current);  
               pop @{$self->{open_elements}}  
                   if $self->{insertion_mode} eq 'after head';  
               redo B;  
             } elsif ($self->{insertion_mode} eq 'after head' and  
                      $token->{tag_name} eq 'body') {  
               !!!insert-element ('body', $token->{attributes});  
               $self->{insertion_mode} = 'in body';  
               !!!next-token;  
               redo B;  
             } elsif ($self->{insertion_mode} eq 'after head' and  
                      $token->{tag_name} eq 'frameset') {  
               !!!insert-element ('frameset', $token->{attributes});  
               $self->{insertion_mode} = 'in frameset';  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($self->{insertion_mode} eq 'in head' and  
                 $token->{tag_name} eq 'head') {  
               pop @{$self->{open_elements}};  
               $self->{insertion_mode} = 'after head';  
               !!!next-token;  
               redo B;  
             } elsif ($self->{insertion_mode} eq 'in head noscript' and  
                 $token->{tag_name} eq 'noscript') {  
               pop @{$self->{open_elements}};  
               $self->{insertion_mode} = 'in head';  
               !!!next-token;  
               redo B;  
             } elsif ($self->{insertion_mode} eq 'in head' and  
                      {  
                       body => 1, html => 1,  
                       p => 1, br => 1,  
                      }->{$token->{tag_name}}) {  
               #  
             } elsif ($self->{insertion_mode} eq 'in head noscript' and  
                      {  
                       p => 1, br => 1,  
                      }->{$token->{tag_name}}) {  
               #  
             } elsif ($self->{insertion_mode} ne 'after head') {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
   
           ## As if </head> or </noscript> or <body>  
           if ($self->{insertion_mode} eq 'in head') {  
             pop @{$self->{open_elements}};  
             $self->{insertion_mode} = 'after head';  
           } elsif ($self->{insertion_mode} eq 'in head noscript') {  
             pop @{$self->{open_elements}};  
             !!!parse-error (type => 'in noscript:'.(defined $token->{tag_name} ? ($token->{type} eq 'end tag' ? '/' : '') . $token->{tag_name} : '#' . $token->{type}));  
             $self->{insertion_mode} = 'in head';  
           } else { # 'after head'  
             !!!insert-element ('body');  
             $self->{insertion_mode} = 'in body';  
           }  
           ## reprocess  
           redo B;  
   
           ## ISSUE: An issue in the spec.  
         } elsif ($self->{insertion_mode} eq 'in body') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: There is a code clone of "character in body".  
             $reconstruct_active_formatting_elements->($insert_to_current);  
               
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
   
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             ## NOTE: There is a code clone of "comment in body".  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } else {  
             $in_body->($insert_to_current);  
             redo B;  
           }  
         } elsif ($self->{insertion_mode} eq 'in table') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: There are "character in table" code clones.  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
                 
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
   
             !!!parse-error (type => 'in table:#character');  
   
             ## As if in body, but insert into foster parent element  
             ## ISSUE: Spec says that "whenever a node would be inserted  
             ## into the current node" while characters might not be  
             ## result in a new Text node.  
             $reconstruct_active_formatting_elements->($insert_to_foster);  
               
             if ({  
                  table => 1, tbody => 1, tfoot => 1,  
                  thead => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               # MUST  
               my $foster_parent_element;  
               my $next_sibling;  
               my $prev_sibling;  
               OE: for (reverse 0..$#{$self->{open_elements}}) {  
                 if ($self->{open_elements}->[$_]->[1] eq 'table') {  
                   my $parent = $self->{open_elements}->[$_]->[0]->parent_node;  
                   if (defined $parent and $parent->node_type == 1) {  
                     $foster_parent_element = $parent;  
                     $next_sibling = $self->{open_elements}->[$_]->[0];  
                     $prev_sibling = $next_sibling->previous_sibling;  
                   } else {  
                     $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];  
                     $prev_sibling = $foster_parent_element->last_child;  
                   }  
                   last OE;  
                 }  
               } # OE  
               $foster_parent_element = $self->{open_elements}->[0]->[0] and  
               $prev_sibling = $foster_parent_element->last_child  
                 unless defined $foster_parent_element;  
               if (defined $prev_sibling and  
                   $prev_sibling->node_type == 3) {  
                 $prev_sibling->manakai_append_text ($token->{data});  
               } else {  
                 $foster_parent_element->insert_before  
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
               }  
             } else {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
             }  
               
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ({  
                  caption => 1,  
                  colgroup => 1,  
                  tbody => 1, tfoot => 1, thead => 1,  
                 }->{$token->{tag_name}}) {  
               ## Clear back to table context  
               while ($self->{open_elements}->[-1]->[1] ne 'table' and  
                      $self->{open_elements}->[-1]->[1] ne 'html') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               push @$active_formatting_elements, ['#marker', '']  
                 if $token->{tag_name} eq 'caption';  
   
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               $self->{insertion_mode} = {  
                                  caption => 'in caption',  
                                  colgroup => 'in column group',  
                                  tbody => 'in table body',  
                                  tfoot => 'in table body',  
                                  thead => 'in table body',  
                                 }->{$token->{tag_name}};  
               !!!next-token;  
               redo B;  
             } elsif ({  
                       col => 1,  
                       td => 1, th => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               ## Clear back to table context  
               while ($self->{open_elements}->[-1]->[1] ne 'table' and  
                      $self->{open_elements}->[-1]->[1] ne 'html') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               !!!insert-element ($token->{tag_name} eq 'col' ? 'colgroup' : 'tbody');  
               $self->{insertion_mode} = $token->{tag_name} eq 'col'  
                 ? 'in column group' : 'in table body';  
               ## reprocess  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## NOTE: There are code clones for this "table in table"  
               !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
   
               ## As if </table>  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'table') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:table');  
                 ## Ignore tokens </table><table>  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # <table>  
                 $token = {type => 'end tag', tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'table') {  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               !!!next-token;  
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1, colgroup => 1,  
                       html => 1, tbody => 1, td => 1, tfoot => 1, th => 1,  
                       thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
   
           !!!parse-error (type => 'in table:'.$token->{tag_name});  
           $in_body->($insert_to_foster);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in caption') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a code clone of "character in body".  
             $reconstruct_active_formatting_elements->($insert_to_current);  
               
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
   
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             ## NOTE: This is a code clone of "comment in body".  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ({  
                  caption => 1, col => 1, colgroup => 1, tbody => 1,  
                  td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,  
                 }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'not closed:caption');  
   
               ## As if </caption>  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'caption') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:caption');  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # <?>  
                 $token = {type => 'end tag', tag_name => 'caption'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'caption') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
   
               $self->{insertion_mode} = 'in table';  
   
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'caption') {  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'caption') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
   
               $self->{insertion_mode} = 'in table';  
   
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               !!!parse-error (type => 'not closed:caption');  
   
               ## As if </caption>  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'caption') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:caption');  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # </table>  
                 $token = {type => 'end tag', tag_name => 'caption'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'caption') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
   
               $self->{insertion_mode} = 'in table';  
   
               ## reprocess  
               redo B;  
             } elsif ({  
                       body => 1, col => 1, colgroup => 1,  
                       html => 1, tbody => 1, td => 1, tfoot => 1,  
                       th => 1, thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
                 
           $in_body->($insert_to_current);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in column group') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
               
             #  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'col') {  
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               pop @{$self->{open_elements}};  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'colgroup') {  
               if ($self->{open_elements}->[-1]->[1] eq 'html') {  
                 !!!parse-error (type => 'unmatched end tag:colgroup');  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               } else {  
                 pop @{$self->{open_elements}}; # colgroup  
                 $self->{insertion_mode} = 'in table';  
                 !!!next-token;  
                 redo B;              
               }  
             } elsif ($token->{tag_name} eq 'col') {  
               !!!parse-error (type => 'unmatched end tag:col');  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
   
           ## As if </colgroup>  
           if ($self->{open_elements}->[-1]->[1] eq 'html') {  
             !!!parse-error (type => 'unmatched end tag:colgroup');  
             ## Ignore the token  
             !!!next-token;  
             redo B;  
           } else {  
             pop @{$self->{open_elements}}; # colgroup  
             $self->{insertion_mode} = 'in table';  
             ## reprocess  
             redo B;  
           }  
         } elsif ($self->{insertion_mode} eq 'in table body') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a "character in table" code clone.  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
                 
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
   
             !!!parse-error (type => 'in table:#character');  
   
             ## As if in body, but insert into foster parent element  
             ## ISSUE: Spec says that "whenever a node would be inserted  
             ## into the current node" while characters might not be  
             ## result in a new Text node.  
             $reconstruct_active_formatting_elements->($insert_to_foster);  
   
             if ({  
                  table => 1, tbody => 1, tfoot => 1,  
                  thead => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               # MUST  
               my $foster_parent_element;  
               my $next_sibling;  
               my $prev_sibling;  
               OE: for (reverse 0..$#{$self->{open_elements}}) {  
                 if ($self->{open_elements}->[$_]->[1] eq 'table') {  
                   my $parent = $self->{open_elements}->[$_]->[0]->parent_node;  
                   if (defined $parent and $parent->node_type == 1) {  
                     $foster_parent_element = $parent;  
                     $next_sibling = $self->{open_elements}->[$_]->[0];  
                     $prev_sibling = $next_sibling->previous_sibling;  
                   } else {  
                     $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];  
                     $prev_sibling = $foster_parent_element->last_child;  
                   }  
                   last OE;  
                 }  
               } # OE  
               $foster_parent_element = $self->{open_elements}->[0]->[0] and  
               $prev_sibling = $foster_parent_element->last_child  
                 unless defined $foster_parent_element;  
               if (defined $prev_sibling and  
                   $prev_sibling->node_type == 3) {  
                 $prev_sibling->manakai_append_text ($token->{data});  
               } else {  
                 $foster_parent_element->insert_before  
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
               }  
             } else {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
             }  
               
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             ## Copied from 'in table'  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ({  
                  tr => 1,  
                  th => 1, td => 1,  
                 }->{$token->{tag_name}}) {  
               unless ($token->{tag_name} eq 'tr') {  
                 !!!parse-error (type => 'missing start tag:tr');  
               }  
   
               ## Clear back to table body context  
               while (not {  
                 tbody => 1, tfoot => 1, thead => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
                 
               $self->{insertion_mode} = 'in row';  
               if ($token->{tag_name} eq 'tr') {  
                 !!!insert-element ($token->{tag_name}, $token->{attributes});  
                 !!!next-token;  
               } else {  
                 !!!insert-element ('tr');  
                 ## reprocess  
               }  
               redo B;  
             } elsif ({  
                       caption => 1, col => 1, colgroup => 1,  
                       tbody => 1, tfoot => 1, thead => 1,  
                      }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ({  
                      tbody => 1, thead => 1, tfoot => 1,  
                     }->{$node->[1]}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table body context  
               while (not {  
                 tbody => 1, tfoot => 1, thead => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               ## As if <{current node}>  
               ## have an element in table scope  
               ## true by definition  
   
               ## Clear back to table body context  
               ## nop by definition  
   
               pop @{$self->{open_elements}};  
               $self->{insertion_mode} = 'in table';  
               ## reprocess  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## NOTE: This is a code clone of "table in table"  
               !!!parse-error (type => 'not closed:table');  
   
               ## As if </table>  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'table') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:table');  
                 ## Ignore tokens </table><table>  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # <table>  
                 $token = {type => 'end tag', tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ({  
                  tbody => 1, tfoot => 1, thead => 1,  
                 }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table body context  
               while (not {  
                 tbody => 1, tfoot => 1, thead => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               pop @{$self->{open_elements}};  
               $self->{insertion_mode} = 'in table';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ({  
                      tbody => 1, thead => 1, tfoot => 1,  
                     }->{$node->[1]}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table body context  
               while (not {  
                 tbody => 1, tfoot => 1, thead => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               ## As if <{current node}>  
               ## have an element in table scope  
               ## true by definition  
   
               ## Clear back to table body context  
               ## nop by definition  
   
               pop @{$self->{open_elements}};  
               $self->{insertion_mode} = 'in table';  
               ## reprocess  
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1, colgroup => 1,  
                       html => 1, td => 1, th => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
             
           ## As if in table  
           !!!parse-error (type => 'in table:'.$token->{tag_name});  
           $in_body->($insert_to_foster);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in row') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a "character in table" code clone.  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
                 
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
   
             !!!parse-error (type => 'in table:#character');  
   
             ## As if in body, but insert into foster parent element  
             ## ISSUE: Spec says that "whenever a node would be inserted  
             ## into the current node" while characters might not be  
             ## result in a new Text node.  
             $reconstruct_active_formatting_elements->($insert_to_foster);  
               
             if ({  
                  table => 1, tbody => 1, tfoot => 1,  
                  thead => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               # MUST  
               my $foster_parent_element;  
               my $next_sibling;  
               my $prev_sibling;  
               OE: for (reverse 0..$#{$self->{open_elements}}) {  
                 if ($self->{open_elements}->[$_]->[1] eq 'table') {  
                   my $parent = $self->{open_elements}->[$_]->[0]->parent_node;  
                   if (defined $parent and $parent->node_type == 1) {  
                     $foster_parent_element = $parent;  
                     $next_sibling = $self->{open_elements}->[$_]->[0];  
                     $prev_sibling = $next_sibling->previous_sibling;  
                   } else {  
                     $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];  
                     $prev_sibling = $foster_parent_element->last_child;  
                   }  
                   last OE;  
                 }  
               } # OE  
               $foster_parent_element = $self->{open_elements}->[0]->[0] and  
               $prev_sibling = $foster_parent_element->last_child  
                 unless defined $foster_parent_element;  
               if (defined $prev_sibling and  
                   $prev_sibling->node_type == 3) {  
                 $prev_sibling->manakai_append_text ($token->{data});  
               } else {  
                 $foster_parent_element->insert_before  
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
               }  
             } else {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
             }  
               
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             ## Copied from 'in table'  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'th' or  
                 $token->{tag_name} eq 'td') {  
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
                 
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               $self->{insertion_mode} = 'in cell';  
   
               push @$active_formatting_elements, ['#marker', ''];  
                 
               !!!next-token;  
               redo B;  
             } elsif ({  
                       caption => 1, col => 1, colgroup => 1,  
                       tbody => 1, tfoot => 1, thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               ## As if </tr>  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'tr') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               ## reprocess  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## NOTE: This is a code clone of "table in table"  
               !!!parse-error (type => 'not closed:table');  
   
               ## As if </table>  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'table') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:table');  
                 ## Ignore tokens </table><table>  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # <table>  
                 $token = {type => 'end tag', tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'tr') {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## As if </tr>  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'tr') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{type});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               ## reprocess  
               redo B;  
             } elsif ({  
                       tbody => 1, tfoot => 1, thead => 1,  
                      }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## As if </tr>  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'tr') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:tr');  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               ## reprocess  
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1,  
                       colgroup => 1, html => 1, td => 1, th => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
   
           ## As if in table  
           !!!parse-error (type => 'in table:'.$token->{tag_name});  
           $in_body->($insert_to_foster);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in cell') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a code clone of "character in body".  
             $reconstruct_active_formatting_elements->($insert_to_current);  
               
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
   
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             ## NOTE: This is a code clone of "comment in body".  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ({  
                  caption => 1, col => 1, colgroup => 1,  
                  tbody => 1, td => 1, tfoot => 1, th => 1,  
                  thead => 1, tr => 1,  
                 }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $tn;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'td' or $node->[1] eq 'th') {  
                   $tn = $node->[1];  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $tn) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Close the cell  
               !!!back-token; # <?>  
               $token = {type => 'end tag', tag_name => $tn};  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => ($token->{tag_name} eq 'th'),  
                    th => ($token->{tag_name} eq 'td'),  
                    tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
   
               $self->{insertion_mode} = 'in row';  
   
               !!!next-token;  
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1,  
                       colgroup => 1, html => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } elsif ({  
                       table => 1, tbody => 1, tfoot => 1,  
                       thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $i;  
               my $tn;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {  
                   $tn = $node->[1];  
                   ## NOTE: There is exactly one |td| or |th| element  
                   ## in scope in the stack of open elements by definition.  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => 'end tag', tag_name => $tn};  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
             
           $in_body->($insert_to_current);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in select') {  
           if ($token->{type} eq 'character') {  
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'option') {  
               if ($self->{open_elements}->[-1]->[1] eq 'option') {  
                 ## As if </option>  
                 pop @{$self->{open_elements}};  
               }  
   
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'optgroup') {  
               if ($self->{open_elements}->[-1]->[1] eq 'option') {  
                 ## As if </option>  
                 pop @{$self->{open_elements}};  
               }  
   
               if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {  
                 ## As if </optgroup>  
                 pop @{$self->{open_elements}};  
               }  
   
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'select') {  
               !!!parse-error (type => 'not closed:select');  
               ## As if </select> instead  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:select');  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'optgroup') {  
               if ($self->{open_elements}->[-1]->[1] eq 'option' and  
                   $self->{open_elements}->[-2]->[1] eq 'optgroup') {  
                 ## As if </option>  
                 splice @{$self->{open_elements}}, -2;  
               } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {  
                 pop @{$self->{open_elements}};  
               } else {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
               }  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'option') {  
               if ($self->{open_elements}->[-1]->[1] eq 'option') {  
                 pop @{$self->{open_elements}};  
               } else {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
               }  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'select') {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               !!!next-token;  
               redo B;  
             } elsif ({  
                       caption => 1, table => 1, tbody => 1,  
                       tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## As if </select>  
               ## have an element in table scope  
               undef $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'select') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:select');  
                 ## Ignore the </select> token  
                 !!!next-token; ## TODO: ok?  
                 redo B;  
               }  
                 
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
   
           !!!parse-error (type => 'in select:'.$token->{tag_name});  
           ## Ignore the token  
           !!!next-token;  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'after body') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               my $data = $1;  
               ## As if in body  
               $reconstruct_active_formatting_elements->($insert_to_current);  
                 
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
   
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
               
             #  
             !!!parse-error (type => 'after body:#'.$token->{type});  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[0]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             !!!parse-error (type => 'after body:'.$token->{tag_name});  
             #  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'html') {  
               if (defined $self->{inner_html_node}) {  
                 !!!parse-error (type => 'unmatched end tag:html');  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               } else {  
                 $previous_insertion_mode = $self->{insertion_mode};  
                 $self->{insertion_mode} = 'trailing end';  
                 !!!next-token;  
                 redo B;  
               }  
             } else {  
               !!!parse-error (type => 'after body:/'.$token->{tag_name});  
             }  
           } else {  
             !!!parse-error (type => 'after body:#'.$token->{type});  
           }  
   
           $self->{insertion_mode} = 'in body';  
           ## reprocess  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in frameset') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
   
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
   
             #  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'frameset') {  
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'frame') {  
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               pop @{$self->{open_elements}};  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'noframes') {  
               $in_body->($insert_to_current);  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'frameset') {  
               if ($self->{open_elements}->[-1]->[1] eq 'html' and  
                   @{$self->{open_elements}} == 1) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
               } else {  
                 pop @{$self->{open_elements}};  
                 !!!next-token;  
               }  
                 
               ## if not inner_html and  
               if ($self->{open_elements}->[-1]->[1] ne 'frameset') {  
                 $self->{insertion_mode} = 'after frameset';  
               }  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
             
           if (defined $token->{tag_name}) {  
             !!!parse-error (type => 'in frameset:'.($token->{type} eq 'end tag' ? '/' : '').$token->{tag_name});  
           } else {  
             !!!parse-error (type => 'in frameset:#'.$token->{type});  
           }  
           ## Ignore the token  
           !!!next-token;  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'after frameset') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
   
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
   
             if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {  
               !!!parse-error (type => 'after frameset:#character');  
   
               ## Ignore the token.  
               if (length $token->{data}) {  
                 ## reprocess the rest of characters  
               } else {  
                 !!!next-token;  
               }  
               redo B;  
             }  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'noframes') {  
               $in_body->($insert_to_current);  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'html') {  
               $previous_insertion_mode = $self->{insertion_mode};  
               $self->{insertion_mode} = 'trailing end';  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             die "$0: $token->{type}: Unknown token type";  
           }  
             
           !!!parse-error (type => 'after frameset:'.($token->{tag_name} eq 'end tag' ? '/' : '').$token->{tag_name});  
           ## Ignore the token  
           !!!next-token;  
           redo B;  
   
           ## ISSUE: An issue in spec there  
         } else {  
           die "$0: $self->{insertion_mode}: Unknown insertion mode";  
         }  
       }  
     } elsif ($self->{insertion_mode} eq 'trailing end') {  
       ## states in the main stage is preserved yet # MUST  
         
       if ($token->{type} eq 'DOCTYPE') {  
         !!!parse-error (type => 'after html:#DOCTYPE');  
         ## Ignore the token  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'comment') {  
         my $comment = $self->{document}->create_comment ($token->{data});  
         $self->{document}->append_child ($comment);  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'character') {  
         if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
           my $data = $1;  
           ## As if in the main phase.  
           ## NOTE: The insertion mode in the main phase  
           ## just before the phase has been changed to the trailing  
           ## end phase is either "after body" or "after frameset".  
           $reconstruct_active_formatting_elements->($insert_to_current);  
             
           $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);  
             
           unless (length $token->{data}) {  
             !!!next-token;  
             redo B;  
           }  
         }  
   
         !!!parse-error (type => 'after html:#character');  
         $self->{insertion_mode} = $previous_insertion_mode;  
         ## reprocess  
         redo B;  
       } elsif ($token->{type} eq 'start tag' or  
                $token->{type} eq 'end tag') {  
         !!!parse-error (type => 'after html:'.($token->{type} eq 'end tag' ? '/' : '').$token->{tag_name});  
         $self->{insertion_mode} = $previous_insertion_mode;  
         ## reprocess  
         redo B;  
       } elsif ($token->{type} eq 'end-of-file') {  
         ## Stop parsing  
         last B;  
       } else {  
         die "$0: $token->{type}: Unknown token";  
6278        }        }
6279      }      }
6280        redo B;
6281    } # B    } # B
6282    
6283      ## NOTE: The "trailing end" phase in HTML5 is split into
6284      ## two insertion modes: "after html body" and "after html frameset".
6285      ## NOTE: States in the main stage is preserved while
6286      ## the parser stays in the trailing end phase. # MUST
6287    
6288    ## Stop parsing # MUST    ## Stop parsing # MUST
6289        
6290    ## TODO: script stuffs    ## TODO: script stuffs
# Line 5268  sub set_inner_html ($$$) { Line 6296  sub set_inner_html ($$$) {
6296    my $s = \$_[0];    my $s = \$_[0];
6297    my $onerror = $_[1];    my $onerror = $_[1];
6298    
6299      ## ISSUE: Should {confident} be true?
6300    
6301    my $nt = $node->node_type;    my $nt = $node->node_type;
6302    if ($nt == 9) {    if ($nt == 9) {
6303      # MUST      # MUST
# Line 5300  sub set_inner_html ($$$) { Line 6330  sub set_inner_html ($$$) {
6330      my $i = 0;      my $i = 0;
6331      my $line = 1;      my $line = 1;
6332      my $column = 0;      my $column = 0;
6333      $p->{set_next_input_character} = sub {      $p->{set_next_char} = sub {
6334        my $self = shift;        my $self = shift;
6335    
6336        pop @{$self->{prev_input_character}};        pop @{$self->{prev_char}};
6337        unshift @{$self->{prev_input_character}}, $self->{next_input_character};        unshift @{$self->{prev_char}}, $self->{next_char};
6338    
6339        $self->{next_input_character} = -1 and return if $i >= length $$s;        $self->{next_char} = -1 and return if $i >= length $$s;
6340        $self->{next_input_character} = ord substr $$s, $i++, 1;        $self->{next_char} = ord substr $$s, $i++, 1;
6341        $column++;        $column++;
6342    
6343        if ($self->{next_input_character} == 0x000A) { # LF        if ($self->{next_char} == 0x000A) { # LF
6344          $line++;          $line++;
6345          $column = 0;          $column = 0;
6346        } elsif ($self->{next_input_character} == 0x000D) { # CR          !!!cp ('i1');
6347          } elsif ($self->{next_char} == 0x000D) { # CR
6348          $i++ if substr ($$s, $i, 1) eq "\x0A";          $i++ if substr ($$s, $i, 1) eq "\x0A";
6349          $self->{next_input_character} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
6350          $line++;          $line++;
6351          $column = 0;          $column = 0;
6352        } elsif ($self->{next_input_character} > 0x10FFFF) {          !!!cp ('i2');
6353          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        } elsif ($self->{next_char} > 0x10FFFF) {
6354        } elsif ($self->{next_input_character} == 0x0000) { # NULL          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
6355            !!!cp ('i3');
6356          } elsif ($self->{next_char} == 0x0000) { # NULL
6357            !!!cp ('i4');
6358          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
6359          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
6360        }        }
6361      };      };
6362      $p->{prev_input_character} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];
6363      $p->{next_input_character} = -1;      $p->{next_char} = -1;
6364            
6365      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
6366        my (%opt) = @_;        my (%opt) = @_;
# Line 5340  sub set_inner_html ($$$) { Line 6374  sub set_inner_html ($$$) {
6374      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
6375    
6376      ## Step 2      ## Step 2
6377      my $node_ln = $node->local_name;      my $node_ln = $node->manakai_local_name;
6378      $p->{content_model_flag} = {      $p->{content_model} = {
6379        title => 'RCDATA',        title => RCDATA_CONTENT_MODEL,
6380        textarea => 'RCDATA',        textarea => RCDATA_CONTENT_MODEL,
6381        style => 'CDATA',        style => CDATA_CONTENT_MODEL,
6382        script => 'CDATA',        script => CDATA_CONTENT_MODEL,
6383        xmp => 'CDATA',        xmp => CDATA_CONTENT_MODEL,
6384        iframe => 'CDATA',        iframe => CDATA_CONTENT_MODEL,
6385        noembed => 'CDATA',        noembed => CDATA_CONTENT_MODEL,
6386        noframes => 'CDATA',        noframes => CDATA_CONTENT_MODEL,
6387        noscript => 'CDATA',        noscript => CDATA_CONTENT_MODEL,
6388        plaintext => 'PLAINTEXT',        plaintext => PLAINTEXT_CONTENT_MODEL,
6389      }->{$node_ln} || 'PCDATA';      }->{$node_ln};
6390         ## ISSUE: What is "the name of the element"? local name?      $p->{content_model} = PCDATA_CONTENT_MODEL
6391            unless defined $p->{content_model};
6392            ## ISSUE: What is "the name of the element"? local name?
6393    
6394      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $node_ln];
6395    
# Line 5378  sub set_inner_html ($$$) { Line 6414  sub set_inner_html ($$$) {
6414        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
6415          my $nsuri = $anode->namespace_uri;          my $nsuri = $anode->namespace_uri;
6416          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
6417            if ($anode->local_name eq 'form') { ## TODO: case?            if ($anode->manakai_local_name eq 'form') {
6418                !!!cp ('i5');
6419              $p->{form_element} = $anode;              $p->{form_element} = $anode;
6420              last AN;              last AN;
6421            }            }
# Line 5418  sub set_inner_html ($$$) { Line 6455  sub set_inner_html ($$$) {
6455    
6456  } # tree construction stage  } # tree construction stage
6457    
6458  sub get_inner_html ($$$) {  package Whatpm::HTML::RestartParser;
6459    my (undef, $node, $on_error) = @_;  push our @ISA, 'Error';
   
   ## Step 1  
   my $s = '';  
   
   my $in_cdata;  
   my $parent = $node;  
   while (defined $parent) {  
     if ($parent->node_type == 1 and  
         $parent->namespace_uri eq 'http://www.w3.org/1999/xhtml' and  
         {  
           style => 1, script => 1, xmp => 1, iframe => 1,  
           noembed => 1, noframes => 1, noscript => 1,  
         }->{$parent->local_name}) { ## TODO: case thingy  
       $in_cdata = 1;  
     }  
     $parent = $parent->parent_node;  
   }  
   
   ## Step 2  
   my @node = @{$node->child_nodes};  
   C: while (@node) {  
     my $child = shift @node;  
     unless (ref $child) {  
       if ($child eq 'cdata-out') {  
         $in_cdata = 0;  
       } else {  
         $s .= $child; # end tag  
       }  
       next C;  
     }  
       
     my $nt = $child->node_type;  
     if ($nt == 1) { # Element  
       my $tag_name = $child->tag_name; ## TODO: manakai_tag_name  
       $s .= '<' . $tag_name;  
       ## NOTE: Non-HTML case:  
       ## <http://permalink.gmane.org/gmane.org.w3c.whatwg.discuss/11191>  
   
       my @attrs = @{$child->attributes}; # sort order MUST be stable  
       for my $attr (@attrs) { # order is implementation dependent  
         my $attr_name = $attr->name; ## TODO: manakai_name  
         $s .= ' ' . $attr_name . '="';  
         my $attr_value = $attr->value;  
         ## escape  
         $attr_value =~ s/&/&amp;/g;  
         $attr_value =~ s/</&lt;/g;  
         $attr_value =~ s/>/&gt;/g;  
         $attr_value =~ s/"/&quot;/g;  
         $s .= $attr_value . '"';  
       }  
       $s .= '>';  
         
       next C if {  
         area => 1, base => 1, basefont => 1, bgsound => 1,  
         br => 1, col => 1, embed => 1, frame => 1, hr => 1,  
         img => 1, input => 1, link => 1, meta => 1, param => 1,  
         spacer => 1, wbr => 1,  
       }->{$tag_name};  
   
       $s .= "\x0A" if $tag_name eq 'pre' or $tag_name eq 'textarea';  
   
       if (not $in_cdata and {  
         style => 1, script => 1, xmp => 1, iframe => 1,  
         noembed => 1, noframes => 1, noscript => 1,  
         plaintext => 1,  
       }->{$tag_name}) {  
         unshift @node, 'cdata-out';  
         $in_cdata = 1;  
       }  
   
       unshift @node, @{$child->child_nodes}, '</' . $tag_name . '>';  
     } elsif ($nt == 3 or $nt == 4) {  
       if ($in_cdata) {  
         $s .= $child->data;  
       } else {  
         my $value = $child->data;  
         $value =~ s/&/&amp;/g;  
         $value =~ s/</&lt;/g;  
         $value =~ s/>/&gt;/g;  
         $value =~ s/"/&quot;/g;  
         $s .= $value;  
       }  
     } elsif ($nt == 8) {  
       $s .= '<!--' . $child->data . '-->';  
     } elsif ($nt == 10) {  
       $s .= '<!DOCTYPE ' . $child->name . '>';  
     } elsif ($nt == 5) { # entrefs  
       push @node, @{$child->child_nodes};  
     } else {  
       $on_error->($child) if defined $on_error;  
     }  
     ## ISSUE: This code does not support PIs.  
   } # C  
     
   ## Step 3  
   return \$s;  
 } # get_inner_html  
6460    
6461  1;  1;
6462  # $Date$  # $Date$

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24