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

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

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

revision 1.24 by wakaba, Sat Jun 23 16:42:43 2007 UTC revision 1.77 by wakaba, Mon Mar 3 10:20:19 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    ## TODO: Control charcters and noncharacters are not allowed (HTML5 revision 1263)
12    ## TODO: 1252 parse error (revision 1264)
13    ## TODO: 8859-11 = 874 (revision 1271)
14    
15  my $permitted_slash_tag_name = {  my $permitted_slash_tag_name = {
16    base => 1,    base => 1,
17    link => 1,    link => 1,
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 78  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 135  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 179  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 186  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;        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>
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}
655                  = 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                !!!cp (36);
661              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
662              } else {
663                !!!cp (37);
664            }            }
665          } else {          } else {
666            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
667          }          }
668          $self->{state} = 'data';          $self->{state} = DATA_STATE;
669          !!!next-input-character;          !!!next-input-character;
670    
671          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
672    
673          redo A;          redo A;
674        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
675                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
676          $self->{current_token}->{tag_name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (38);
677            $self->{current_token}->{tag_name} .= chr ($self->{next_char} + 0x0020);
678            # start tag or end tag            # start tag or end tag
679          ## Stay in this state          ## Stay in this state
680          !!!next-input-character;          !!!next-input-character;
681          redo A;          redo A;
682        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
683          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
684          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
685              !!!cp (39);
686              $self->{current_token}->{first_start_tag}
687                  = not defined $self->{last_emitted_start_tag_name};
688            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
689          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
690            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
691            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
692                !!!cp (40);
693              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
694              } else {
695                !!!cp (41);
696            }            }
697          } else {          } else {
698            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
699          }          }
700          $self->{state} = 'data';          $self->{state} = DATA_STATE;
701          # reconsume          # reconsume
702    
703          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
704    
705          redo A;          redo A;
706        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
707          !!!next-input-character;          !!!next-input-character;
708          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_char} == 0x003E and # >
709              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
710              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
711            # permitted slash            # permitted slash
712              !!!cp (42);
713            #            #
714          } else {          } else {
715              !!!cp (43);
716            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
717          }          }
718          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
719          # next-input-character is already done          # next-input-character is already done
720          redo A;          redo A;
721        } else {        } else {
722          $self->{current_token}->{tag_name} .= chr $self->{next_input_character};          !!!cp (44);
723            $self->{current_token}->{tag_name} .= chr $self->{next_char};
724            # start tag or end tag            # start tag or end tag
725          ## Stay in the state          ## Stay in the state
726          !!!next-input-character;          !!!next-input-character;
727          redo A;          redo A;
728        }        }
729      } elsif ($self->{state} eq 'before attribute name') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
730        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
731            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
732            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
733            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
734            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
735            !!!cp (45);
736          ## Stay in the state          ## Stay in the state
737          !!!next-input-character;          !!!next-input-character;
738          redo A;          redo A;
739        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
740          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
741              !!!cp (46);
742              $self->{current_token}->{first_start_tag}
743                  = not defined $self->{last_emitted_start_tag_name};
744            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
745          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
746            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
747            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
748                !!!cp (47);
749              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
750              } else {
751                !!!cp (48);
752            }            }
753          } else {          } else {
754            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
755          }          }
756          $self->{state} = 'data';          $self->{state} = DATA_STATE;
757          !!!next-input-character;          !!!next-input-character;
758    
759          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
760    
761          redo A;          redo A;
762        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
763                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
764          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (49);
765            $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),
766                                value => ''};                                value => ''};
767          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
768          !!!next-input-character;          !!!next-input-character;
769          redo A;          redo A;
770        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
771          !!!next-input-character;          !!!next-input-character;
772          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_char} == 0x003E and # >
773              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
774              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
775            # permitted slash            # permitted slash
776              !!!cp (50);
777            #            #
778          } else {          } else {
779              !!!cp (51);
780            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
781          }          }
782          ## Stay in the state          ## Stay in the state
783          # next-input-character is already done          # next-input-character is already done
784          redo A;          redo A;
785        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
786          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
787          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
788              !!!cp (52);
789              $self->{current_token}->{first_start_tag}
790                  = not defined $self->{last_emitted_start_tag_name};
791            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
792          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
793            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
794            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
795                !!!cp (53);
796              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
797              } else {
798                !!!cp (54);
799            }            }
800          } else {          } else {
801            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
802          }          }
803          $self->{state} = 'data';          $self->{state} = DATA_STATE;
804          # reconsume          # reconsume
805    
806          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
807    
808          redo A;          redo A;
809        } else {        } else {
810          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ({
811                 0x0022 => 1, # "
812                 0x0027 => 1, # '
813                 0x003D => 1, # =
814                }->{$self->{next_char}}) {
815              !!!cp (55);
816              !!!parse-error (type => 'bad attribute name');
817            } else {
818              !!!cp (56);
819            }
820            $self->{current_attribute} = {name => chr ($self->{next_char}),
821                                value => ''};                                value => ''};
822          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
823          !!!next-input-character;          !!!next-input-character;
824          redo A;          redo A;
825        }        }
826      } elsif ($self->{state} eq 'attribute name') {      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {
827        my $before_leave = sub {        my $before_leave = sub {
828          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
829              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
830            !!!parse-error (type => 'dupulicate attribute');            !!!cp (57);
831              !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});
832            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
833          } else {          } else {
834              !!!cp (58);
835            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
836              = $self->{current_attribute};              = $self->{current_attribute};
837          }          }
838        }; # $before_leave        }; # $before_leave
839    
840        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
841            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
842            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
843            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
844            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
845            !!!cp (59);
846          $before_leave->();          $before_leave->();
847          $self->{state} = 'after attribute name';          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
848          !!!next-input-character;          !!!next-input-character;
849          redo A;          redo A;
850        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
851            !!!cp (60);
852          $before_leave->();          $before_leave->();
853          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
854          !!!next-input-character;          !!!next-input-character;
855          redo A;          redo A;
856        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
857          $before_leave->();          $before_leave->();
858          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
859              !!!cp (61);
860              $self->{current_token}->{first_start_tag}
861                  = not defined $self->{last_emitted_start_tag_name};
862            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
863          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
864            $self->{content_model_flag} = 'PCDATA'; # MUST            !!!cp (62);
865              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
866            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
867              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
868            }            }
869          } else {          } else {
870            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
871          }          }
872          $self->{state} = 'data';          $self->{state} = DATA_STATE;
873          !!!next-input-character;          !!!next-input-character;
874    
875          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
876    
877          redo A;          redo A;
878        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
879                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
880          $self->{current_attribute}->{name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (63);
881            $self->{current_attribute}->{name} .= chr ($self->{next_char} + 0x0020);
882          ## Stay in the state          ## Stay in the state
883          !!!next-input-character;          !!!next-input-character;
884          redo A;          redo A;
885        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
886          $before_leave->();          $before_leave->();
887          !!!next-input-character;          !!!next-input-character;
888          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_char} == 0x003E and # >
889              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
890              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
891            # permitted slash            # permitted slash
892              !!!cp (64);
893            #            #
894          } else {          } else {
895              !!!cp (65);
896            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
897          }          }
898          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
899          # next-input-character is already done          # next-input-character is already done
900          redo A;          redo A;
901        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
902          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
903          $before_leave->();          $before_leave->();
904          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
905              !!!cp (66);
906              $self->{current_token}->{first_start_tag}
907                  = not defined $self->{last_emitted_start_tag_name};
908            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
909          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
910            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
911            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
912                !!!cp (67);
913              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
914              } else {
915                !!!cp (68);
916            }            }
917          } else {          } else {
918            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
919          }          }
920          $self->{state} = 'data';          $self->{state} = DATA_STATE;
921          # reconsume          # reconsume
922    
923          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
924    
925          redo A;          redo A;
926        } else {        } else {
927          $self->{current_attribute}->{name} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x0022 or # "
928                $self->{next_char} == 0x0027) { # '
929              !!!cp (69);
930              !!!parse-error (type => 'bad attribute name');
931            } else {
932              !!!cp (70);
933            }
934            $self->{current_attribute}->{name} .= chr ($self->{next_char});
935          ## Stay in the state          ## Stay in the state
936          !!!next-input-character;          !!!next-input-character;
937          redo A;          redo A;
938        }        }
939      } elsif ($self->{state} eq 'after attribute name') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
940        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
941            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
942            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
943            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
944            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
945            !!!cp (71);
946          ## Stay in the state          ## Stay in the state
947          !!!next-input-character;          !!!next-input-character;
948          redo A;          redo A;
949        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
950          $self->{state} = 'before attribute value';          !!!cp (72);
951            $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
952          !!!next-input-character;          !!!next-input-character;
953          redo A;          redo A;
954        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
955          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
956              !!!cp (73);
957              $self->{current_token}->{first_start_tag}
958                  = not defined $self->{last_emitted_start_tag_name};
959            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
960          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
961            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
962            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
963                !!!cp (74);
964              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
965              } else {
966                !!!cp (75);
967            }            }
968          } else {          } else {
969            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
970          }          }
971          $self->{state} = 'data';          $self->{state} = DATA_STATE;
972          !!!next-input-character;          !!!next-input-character;
973    
974          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
975    
976          redo A;          redo A;
977        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
978                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
979          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (76);
980            $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),
981                                value => ''};                                value => ''};
982          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
983          !!!next-input-character;          !!!next-input-character;
984          redo A;          redo A;
985        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
986          !!!next-input-character;          !!!next-input-character;
987          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_char} == 0x003E and # >
988              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
989              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
990            # permitted slash            # permitted slash
991              !!!cp (77);
992            #            #
993          } else {          } else {
994              !!!cp (78);
995            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
996              ## TODO: Different error type for <aa / bb> than <aa/>
997          }          }
998          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
999          # next-input-character is already done          # next-input-character is already done
1000          redo A;          redo A;
1001        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1002          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1003          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1004              !!!cp (79);
1005              $self->{current_token}->{first_start_tag}
1006                  = not defined $self->{last_emitted_start_tag_name};
1007            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1008          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1009            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1010            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1011                !!!cp (80);
1012              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1013              } else {
1014                !!!cp (81);
1015            }            }
1016          } else {          } else {
1017            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1018          }          }
1019          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1020          # reconsume          # reconsume
1021    
1022          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1023    
1024          redo A;          redo A;
1025        } else {        } else {
1026          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          !!!cp (82);
1027            $self->{current_attribute} = {name => chr ($self->{next_char}),
1028                                value => ''};                                value => ''};
1029          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
1030          !!!next-input-character;          !!!next-input-character;
1031          redo A;                  redo A;        
1032        }        }
1033      } elsif ($self->{state} eq 'before attribute value') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
1034        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1035            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1036            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1037            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1038            $self->{next_input_character} == 0x0020) { # SP                  $self->{next_char} == 0x0020) { # SP      
1039            !!!cp (83);
1040          ## Stay in the state          ## Stay in the state
1041          !!!next-input-character;          !!!next-input-character;
1042          redo A;          redo A;
1043        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
1044          $self->{state} = 'attribute value (double-quoted)';          !!!cp (84);
1045            $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
1046          !!!next-input-character;          !!!next-input-character;
1047          redo A;          redo A;
1048        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1049          $self->{state} = 'attribute value (unquoted)';          !!!cp (85);
1050            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1051          ## reconsume          ## reconsume
1052          redo A;          redo A;
1053        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
1054          $self->{state} = 'attribute value (single-quoted)';          !!!cp (86);
1055            $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
1056          !!!next-input-character;          !!!next-input-character;
1057          redo A;          redo A;
1058        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1059          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1060              !!!cp (87);
1061              $self->{current_token}->{first_start_tag}
1062                  = not defined $self->{last_emitted_start_tag_name};
1063            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1064          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1065            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1066            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1067                !!!cp (88);
1068              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1069              } else {
1070                !!!cp (89);
1071            }            }
1072          } else {          } else {
1073            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1074          }          }
1075          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1076          !!!next-input-character;          !!!next-input-character;
1077    
1078          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1079    
1080          redo A;          redo A;
1081        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1082          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1083          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1084              !!!cp (90);
1085              $self->{current_token}->{first_start_tag}
1086                  = not defined $self->{last_emitted_start_tag_name};
1087            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1088          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1089            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1090            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1091                !!!cp (91);
1092              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1093              } else {
1094                !!!cp (92);
1095            }            }
1096          } else {          } else {
1097            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1098          }          }
1099          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1100          ## reconsume          ## reconsume
1101    
1102          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1103    
1104          redo A;          redo A;
1105        } else {        } else {
1106          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x003D) { # =
1107          $self->{state} = 'attribute value (unquoted)';            !!!cp (93);
1108              !!!parse-error (type => 'bad attribute value');
1109            } else {
1110              !!!cp (94);
1111            }
1112            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1113            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1114          !!!next-input-character;          !!!next-input-character;
1115          redo A;          redo A;
1116        }        }
1117      } elsif ($self->{state} eq 'attribute value (double-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
1118        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
1119          $self->{state} = 'before attribute name';          !!!cp (95);
1120            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1121          !!!next-input-character;          !!!next-input-character;
1122          redo A;          redo A;
1123        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1124          $self->{last_attribute_value_state} = 'attribute value (double-quoted)';          !!!cp (96);
1125          $self->{state} = 'entity in attribute value';          $self->{last_attribute_value_state} = $self->{state};
1126            $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1127          !!!next-input-character;          !!!next-input-character;
1128          redo A;          redo A;
1129        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1130          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1131          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1132              !!!cp (97);
1133              $self->{current_token}->{first_start_tag}
1134                  = not defined $self->{last_emitted_start_tag_name};
1135            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1136          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1137            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1138            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1139                !!!cp (98);
1140              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1141              } else {
1142                !!!cp (99);
1143            }            }
1144          } else {          } else {
1145            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1146          }          }
1147          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1148          ## reconsume          ## reconsume
1149    
1150          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1151    
1152          redo A;          redo A;
1153        } else {        } else {
1154          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (100);
1155            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1156          ## Stay in the state          ## Stay in the state
1157          !!!next-input-character;          !!!next-input-character;
1158          redo A;          redo A;
1159        }        }
1160      } elsif ($self->{state} eq 'attribute value (single-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1161        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
1162          $self->{state} = 'before attribute name';          !!!cp (101);
1163            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1164          !!!next-input-character;          !!!next-input-character;
1165          redo A;          redo A;
1166        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1167          $self->{last_attribute_value_state} = 'attribute value (single-quoted)';          !!!cp (102);
1168          $self->{state} = 'entity in attribute value';          $self->{last_attribute_value_state} = $self->{state};
1169            $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1170          !!!next-input-character;          !!!next-input-character;
1171          redo A;          redo A;
1172        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1173          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1174          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1175              !!!cp (103);
1176              $self->{current_token}->{first_start_tag}
1177                  = not defined $self->{last_emitted_start_tag_name};
1178            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1179          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1180            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1181            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1182                !!!cp (104);
1183              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1184              } else {
1185                !!!cp (105);
1186            }            }
1187          } else {          } else {
1188            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1189          }          }
1190          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1191          ## reconsume          ## reconsume
1192    
1193          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1194    
1195          redo A;          redo A;
1196        } else {        } else {
1197          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (106);
1198            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1199          ## Stay in the state          ## Stay in the state
1200          !!!next-input-character;          !!!next-input-character;
1201          redo A;          redo A;
1202        }        }
1203      } elsif ($self->{state} eq 'attribute value (unquoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1204        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1205            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1206            $self->{next_input_character} == 0x000B or # HT            $self->{next_char} == 0x000B or # HT
1207            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1208            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1209          $self->{state} = 'before attribute name';          !!!cp (107);
1210          !!!next-input-character;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1211          redo A;          !!!next-input-character;
1212        } elsif ($self->{next_input_character} == 0x0026) { # &          redo A;
1213          $self->{last_attribute_value_state} = 'attribute value (unquoted)';        } elsif ($self->{next_char} == 0x0026) { # &
1214          $self->{state} = 'entity in attribute value';          !!!cp (108);
1215          !!!next-input-character;          $self->{last_attribute_value_state} = $self->{state};
1216          redo A;          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1217        } elsif ($self->{next_input_character} == 0x003E) { # >          !!!next-input-character;
1218          if ($self->{current_token}->{type} eq 'start tag') {          redo A;
1219          } elsif ($self->{next_char} == 0x003E) { # >
1220            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1221              !!!cp (109);
1222              $self->{current_token}->{first_start_tag}
1223                  = not defined $self->{last_emitted_start_tag_name};
1224            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1225          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1226            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1227            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1228                !!!cp (110);
1229              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1230              } else {
1231                !!!cp (111);
1232            }            }
1233          } else {          } else {
1234            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1235          }          }
1236          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1237          !!!next-input-character;          !!!next-input-character;
1238    
1239          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1240    
1241          redo A;          redo A;
1242        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1243          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1244          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1245              !!!cp (112);
1246              $self->{current_token}->{first_start_tag}
1247                  = not defined $self->{last_emitted_start_tag_name};
1248            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1249          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1250            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1251            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1252                !!!cp (113);
1253              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1254              } else {
1255                !!!cp (114);
1256            }            }
1257          } else {          } else {
1258            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1259          }          }
1260          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1261          ## reconsume          ## reconsume
1262    
1263          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1264    
1265          redo A;          redo A;
1266        } else {        } else {
1267          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ({
1268                 0x0022 => 1, # "
1269                 0x0027 => 1, # '
1270                 0x003D => 1, # =
1271                }->{$self->{next_char}}) {
1272              !!!cp (115);
1273              !!!parse-error (type => 'bad attribute value');
1274            } else {
1275              !!!cp (116);
1276            }
1277            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1278          ## Stay in the state          ## Stay in the state
1279          !!!next-input-character;          !!!next-input-character;
1280          redo A;          redo A;
1281        }        }
1282      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {
1283        my $token = $self->_tokenize_attempt_to_consume_an_entity;        my $token = $self->_tokenize_attempt_to_consume_an_entity
1284              (1,
1285               $self->{last_attribute_value_state}
1286                 == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE ? 0x0022 : # "
1287               $self->{last_attribute_value_state}
1288                 == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE ? 0x0027 : # '
1289               -1);
1290    
1291        unless (defined $token) {        unless (defined $token) {
1292            !!!cp (117);
1293          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
1294        } else {        } else {
1295            !!!cp (118);
1296          $self->{current_attribute}->{value} .= $token->{data};          $self->{current_attribute}->{value} .= $token->{data};
1297            $self->{current_attribute}->{has_reference} = $token->{has_reference};
1298          ## 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"
1299        }        }
1300    
1301        $self->{state} = $self->{last_attribute_value_state};        $self->{state} = $self->{last_attribute_value_state};
1302        # next-input-character is already done        # next-input-character is already done
1303        redo A;        redo A;
1304      } elsif ($self->{state} eq 'bogus comment') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1305          if ($self->{next_char} == 0x0009 or # HT
1306              $self->{next_char} == 0x000A or # LF
1307              $self->{next_char} == 0x000B or # VT
1308              $self->{next_char} == 0x000C or # FF
1309              $self->{next_char} == 0x0020) { # SP
1310            !!!cp (118);
1311            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1312            !!!next-input-character;
1313            redo A;
1314          } elsif ($self->{next_char} == 0x003E) { # >
1315            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1316              !!!cp (119);
1317              $self->{current_token}->{first_start_tag}
1318                  = not defined $self->{last_emitted_start_tag_name};
1319              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1320            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1321              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1322              if ($self->{current_token}->{attributes}) {
1323                !!!cp (120);
1324                !!!parse-error (type => 'end tag attribute');
1325              } else {
1326                !!!cp (121);
1327              }
1328            } else {
1329              die "$0: $self->{current_token}->{type}: Unknown token type";
1330            }
1331            $self->{state} = DATA_STATE;
1332            !!!next-input-character;
1333    
1334            !!!emit ($self->{current_token}); # start tag or end tag
1335    
1336            redo A;
1337          } elsif ($self->{next_char} == 0x002F) { # /
1338            !!!next-input-character;
1339            if ($self->{next_char} == 0x003E and # >
1340                $self->{current_token}->{type} == START_TAG_TOKEN and
1341                $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
1342              # permitted slash
1343              !!!cp (122);
1344              #
1345            } else {
1346              !!!cp (123);
1347              !!!parse-error (type => 'nestc');
1348            }
1349            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1350            # next-input-character is already done
1351            redo A;
1352          } else {
1353            !!!cp (124);
1354            !!!parse-error (type => 'no space between attributes');
1355            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1356            ## reconsume
1357            redo A;
1358          }
1359        } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1360        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1361                
1362        my $token = {type => 'comment', data => ''};        my $token = {type => COMMENT_TOKEN, data => ''};
1363    
1364        BC: {        BC: {
1365          if ($self->{next_input_character} == 0x003E) { # >          if ($self->{next_char} == 0x003E) { # >
1366            $self->{state} = 'data';            !!!cp (124);
1367              $self->{state} = DATA_STATE;
1368            !!!next-input-character;            !!!next-input-character;
1369    
1370            !!!emit ($token);            !!!emit ($token);
1371    
1372            redo A;            redo A;
1373          } elsif ($self->{next_input_character} == -1) {          } elsif ($self->{next_char} == -1) {
1374            $self->{state} = 'data';            !!!cp (125);
1375              $self->{state} = DATA_STATE;
1376            ## reconsume            ## reconsume
1377    
1378            !!!emit ($token);            !!!emit ($token);
1379    
1380            redo A;            redo A;
1381          } else {          } else {
1382            $token->{data} .= chr ($self->{next_input_character});            !!!cp (126);
1383              $token->{data} .= chr ($self->{next_char});
1384            !!!next-input-character;            !!!next-input-character;
1385            redo BC;            redo BC;
1386          }          }
1387        } # BC        } # BC
1388      } elsif ($self->{state} eq 'markup declaration open') {  
1389          die "$0: _get_next_token: unexpected case [BC]";
1390        } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1391        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1392    
1393        my @next_char;        my @next_char;
1394        push @next_char, $self->{next_input_character};        push @next_char, $self->{next_char};
1395                
1396        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1397          !!!next-input-character;          !!!next-input-character;
1398          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
1399          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_char} == 0x002D) { # -
1400            $self->{current_token} = {type => 'comment', data => ''};            !!!cp (127);
1401            $self->{state} = 'comment start';            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};
1402              $self->{state} = COMMENT_START_STATE;
1403            !!!next-input-character;            !!!next-input-character;
1404            redo A;            redo A;
1405            } else {
1406              !!!cp (128);
1407          }          }
1408        } elsif ($self->{next_input_character} == 0x0044 or # D        } elsif ($self->{next_char} == 0x0044 or # D
1409                 $self->{next_input_character} == 0x0064) { # d                 $self->{next_char} == 0x0064) { # d
1410          !!!next-input-character;          !!!next-input-character;
1411          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
1412          if ($self->{next_input_character} == 0x004F or # O          if ($self->{next_char} == 0x004F or # O
1413              $self->{next_input_character} == 0x006F) { # o              $self->{next_char} == 0x006F) { # o
1414            !!!next-input-character;            !!!next-input-character;
1415            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_char};
1416            if ($self->{next_input_character} == 0x0043 or # C            if ($self->{next_char} == 0x0043 or # C
1417                $self->{next_input_character} == 0x0063) { # c                $self->{next_char} == 0x0063) { # c
1418              !!!next-input-character;              !!!next-input-character;
1419              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_char};
1420              if ($self->{next_input_character} == 0x0054 or # T              if ($self->{next_char} == 0x0054 or # T
1421                  $self->{next_input_character} == 0x0074) { # t                  $self->{next_char} == 0x0074) { # t
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} == 0x0059 or # Y                if ($self->{next_char} == 0x0059 or # Y
1425                    $self->{next_input_character} == 0x0079) { # y                    $self->{next_char} == 0x0079) { # y
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} == 0x0050 or # P                  if ($self->{next_char} == 0x0050 or # P
1429                      $self->{next_input_character} == 0x0070) { # p                      $self->{next_char} == 0x0070) { # p
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} == 0x0045 or # E                    if ($self->{next_char} == 0x0045 or # E
1433                        $self->{next_input_character} == 0x0065) { # e                        $self->{next_char} == 0x0065) { # e
1434                      ## ISSUE: What a stupid code this is!                      !!!cp (129);
1435                      $self->{state} = 'DOCTYPE';                      ## TODO: What a stupid code this is!
1436                        $self->{state} = DOCTYPE_STATE;
1437                      !!!next-input-character;                      !!!next-input-character;
1438                      redo A;                      redo A;
1439                      } else {
1440                        !!!cp (130);
1441                    }                    }
1442                    } else {
1443                      !!!cp (131);
1444                  }                  }
1445                  } else {
1446                    !!!cp (132);
1447                }                }
1448                } else {
1449                  !!!cp (133);
1450              }              }
1451              } else {
1452                !!!cp (134);
1453            }            }
1454            } else {
1455              !!!cp (135);
1456          }          }
1457          } else {
1458            !!!cp (136);
1459        }        }
1460    
1461        !!!parse-error (type => 'bogus comment open');        !!!parse-error (type => 'bogus comment');
1462        $self->{next_input_character} = shift @next_char;        $self->{next_char} = shift @next_char;
1463        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
1464        $self->{state} = 'bogus comment';        $self->{state} = BOGUS_COMMENT_STATE;
1465        redo A;        redo A;
1466                
1467        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
1468        ## 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?
1469      } elsif ($self->{state} eq 'comment start') {      } elsif ($self->{state} == COMMENT_START_STATE) {
1470        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1471          $self->{state} = 'comment start dash';          !!!cp (137);
1472            $self->{state} = COMMENT_START_DASH_STATE;
1473          !!!next-input-character;          !!!next-input-character;
1474          redo A;          redo A;
1475        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1476            !!!cp (138);
1477          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
1478          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1479          !!!next-input-character;          !!!next-input-character;
1480    
1481          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1482    
1483          redo A;          redo A;
1484        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1485            !!!cp (139);
1486          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1487          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1488          ## reconsume          ## reconsume
1489    
1490          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1491    
1492          redo A;          redo A;
1493        } else {        } else {
1494            !!!cp (140);
1495          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
1496              .= chr ($self->{next_input_character});              .= chr ($self->{next_char});
1497          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1498          !!!next-input-character;          !!!next-input-character;
1499          redo A;          redo A;
1500        }        }
1501      } elsif ($self->{state} eq 'comment start dash') {      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
1502        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1503          $self->{state} = 'comment end';          !!!cp (141);
1504            $self->{state} = COMMENT_END_STATE;
1505          !!!next-input-character;          !!!next-input-character;
1506          redo A;          redo A;
1507        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1508            !!!cp (142);
1509          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
1510          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1511          !!!next-input-character;          !!!next-input-character;
1512    
1513          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1514    
1515          redo A;          redo A;
1516        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1517            !!!cp (143);
1518          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1519          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1520          ## reconsume          ## reconsume
1521    
1522          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1523    
1524          redo A;          redo A;
1525        } else {        } else {
1526            !!!cp (144);
1527          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
1528              .= chr ($self->{next_input_character});              .= '-' . chr ($self->{next_char});
1529          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1530          !!!next-input-character;          !!!next-input-character;
1531          redo A;          redo A;
1532        }        }
1533      } elsif ($self->{state} eq 'comment') {      } elsif ($self->{state} == COMMENT_STATE) {
1534        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1535          $self->{state} = 'comment end dash';          !!!cp (145);
1536            $self->{state} = COMMENT_END_DASH_STATE;
1537          !!!next-input-character;          !!!next-input-character;
1538          redo A;          redo A;
1539        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1540            !!!cp (146);
1541          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1542          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1543          ## reconsume          ## reconsume
1544    
1545          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1546    
1547          redo A;          redo A;
1548        } else {        } else {
1549          $self->{current_token}->{data} .= chr ($self->{next_input_character}); # comment          !!!cp (147);
1550            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1551          ## Stay in the state          ## Stay in the state
1552          !!!next-input-character;          !!!next-input-character;
1553          redo A;          redo A;
1554        }        }
1555      } elsif ($self->{state} eq 'comment end dash') {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
1556        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1557          $self->{state} = 'comment end';          !!!cp (148);
1558            $self->{state} = COMMENT_END_STATE;
1559          !!!next-input-character;          !!!next-input-character;
1560          redo A;          redo A;
1561        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1562            !!!cp (149);
1563          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1564          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1565          ## reconsume          ## reconsume
1566    
1567          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1568    
1569          redo A;          redo A;
1570        } else {        } else {
1571          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          !!!cp (150);
1572          $self->{state} = 'comment';          $self->{current_token}->{data} .= '-' . chr ($self->{next_char}); # comment
1573            $self->{state} = COMMENT_STATE;
1574          !!!next-input-character;          !!!next-input-character;
1575          redo A;          redo A;
1576        }        }
1577      } elsif ($self->{state} eq 'comment end') {      } elsif ($self->{state} == COMMENT_END_STATE) {
1578        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
1579          $self->{state} = 'data';          !!!cp (151);
1580            $self->{state} = DATA_STATE;
1581          !!!next-input-character;          !!!next-input-character;
1582    
1583          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1584    
1585          redo A;          redo A;
1586        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
1587            !!!cp (152);
1588          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment');
1589          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
1590          ## Stay in the state          ## Stay in the state
1591          !!!next-input-character;          !!!next-input-character;
1592          redo A;          redo A;
1593        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1594            !!!cp (153);
1595          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1596          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1597          ## reconsume          ## reconsume
1598    
1599          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1600    
1601          redo A;          redo A;
1602        } else {        } else {
1603            !!!cp (154);
1604          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment');
1605          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
1606          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1607          !!!next-input-character;          !!!next-input-character;
1608          redo A;          redo A;
1609        }        }
1610      } elsif ($self->{state} eq 'DOCTYPE') {      } elsif ($self->{state} == DOCTYPE_STATE) {
1611        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1612            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1613            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1614            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1615            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1616          $self->{state} = 'before DOCTYPE name';          !!!cp (155);
1617            $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1618          !!!next-input-character;          !!!next-input-character;
1619          redo A;          redo A;
1620        } else {        } else {
1621            !!!cp (156);
1622          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
1623          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1624          ## reconsume          ## reconsume
1625          redo A;          redo A;
1626        }        }
1627      } elsif ($self->{state} eq 'before DOCTYPE name') {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
1628        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1629            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1630            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1631            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1632            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1633            !!!cp (157);
1634          ## Stay in the state          ## Stay in the state
1635          !!!next-input-character;          !!!next-input-character;
1636          redo A;          redo A;
1637        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1638            !!!cp (158);
1639          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1640          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1641          !!!next-input-character;          !!!next-input-character;
1642    
1643          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});
1644    
1645          redo A;          redo A;
1646        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1647            !!!cp (159);
1648          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1649          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1650          ## reconsume          ## reconsume
1651    
1652          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});
1653    
1654          redo A;          redo A;
1655        } else {        } else {
1656            !!!cp (160);
1657          $self->{current_token}          $self->{current_token}
1658              = {type => 'DOCTYPE',              = {type => DOCTYPE_TOKEN,
1659                 name => chr ($self->{next_input_character}),                 name => chr ($self->{next_char}),
1660                 correct => 1};                 #quirks => 0,
1661                  };
1662  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
1663          $self->{state} = 'DOCTYPE name';          $self->{state} = DOCTYPE_NAME_STATE;
1664          !!!next-input-character;          !!!next-input-character;
1665          redo A;          redo A;
1666        }        }
1667      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
1668  ## ISSUE: Redundant "First," in the spec.  ## ISSUE: Redundant "First," in the spec.
1669        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1670            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1671            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1672            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1673            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1674          $self->{state} = 'after DOCTYPE name';          !!!cp (161);
1675            $self->{state} = AFTER_DOCTYPE_NAME_STATE;
1676          !!!next-input-character;          !!!next-input-character;
1677          redo A;          redo A;
1678        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1679          $self->{state} = 'data';          !!!cp (162);
1680            $self->{state} = DATA_STATE;
1681          !!!next-input-character;          !!!next-input-character;
1682    
1683          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1684    
1685          redo A;          redo A;
1686        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1687            !!!cp (163);
1688          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1689          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1690          ## reconsume          ## reconsume
1691    
1692          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1693          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1694    
1695          redo A;          redo A;
1696        } else {        } else {
1697            !!!cp (164);
1698          $self->{current_token}->{name}          $self->{current_token}->{name}
1699            .= chr ($self->{next_input_character}); # DOCTYPE            .= chr ($self->{next_char}); # DOCTYPE
1700          ## Stay in the state          ## Stay in the state
1701          !!!next-input-character;          !!!next-input-character;
1702          redo A;          redo A;
1703        }        }
1704      } elsif ($self->{state} eq 'after DOCTYPE name') {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
1705        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1706            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1707            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1708            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1709            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1710            !!!cp (165);
1711          ## Stay in the state          ## Stay in the state
1712          !!!next-input-character;          !!!next-input-character;
1713          redo A;          redo A;
1714        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1715          $self->{state} = 'data';          !!!cp (166);
1716            $self->{state} = DATA_STATE;
1717          !!!next-input-character;          !!!next-input-character;
1718    
1719          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1720    
1721          redo A;          redo A;
1722        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1723            !!!cp (167);
1724          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1725          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1726          ## reconsume          ## reconsume
1727    
1728          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1729          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1730    
1731          redo A;          redo A;
1732        } elsif ($self->{next_input_character} == 0x0050 or # P        } elsif ($self->{next_char} == 0x0050 or # P
1733                 $self->{next_input_character} == 0x0070) { # p                 $self->{next_char} == 0x0070) { # p
1734          !!!next-input-character;          !!!next-input-character;
1735          if ($self->{next_input_character} == 0x0055 or # U          if ($self->{next_char} == 0x0055 or # U
1736              $self->{next_input_character} == 0x0075) { # u              $self->{next_char} == 0x0075) { # u
1737            !!!next-input-character;            !!!next-input-character;
1738            if ($self->{next_input_character} == 0x0042 or # B            if ($self->{next_char} == 0x0042 or # B
1739                $self->{next_input_character} == 0x0062) { # b                $self->{next_char} == 0x0062) { # b
1740              !!!next-input-character;              !!!next-input-character;
1741              if ($self->{next_input_character} == 0x004C or # L              if ($self->{next_char} == 0x004C or # L
1742                  $self->{next_input_character} == 0x006C) { # l                  $self->{next_char} == 0x006C) { # l
1743                !!!next-input-character;                !!!next-input-character;
1744                if ($self->{next_input_character} == 0x0049 or # I                if ($self->{next_char} == 0x0049 or # I
1745                    $self->{next_input_character} == 0x0069) { # i                    $self->{next_char} == 0x0069) { # i
1746                  !!!next-input-character;                  !!!next-input-character;
1747                  if ($self->{next_input_character} == 0x0043 or # C                  if ($self->{next_char} == 0x0043 or # C
1748                      $self->{next_input_character} == 0x0063) { # c                      $self->{next_char} == 0x0063) { # c
1749                    $self->{state} = 'before DOCTYPE public identifier';                    !!!cp (168);
1750                      $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1751                    !!!next-input-character;                    !!!next-input-character;
1752                    redo A;                    redo A;
1753                    } else {
1754                      !!!cp (169);
1755                  }                  }
1756                  } else {
1757                    !!!cp (170);
1758                }                }
1759                } else {
1760                  !!!cp (171);
1761              }              }
1762              } else {
1763                !!!cp (172);
1764            }            }
1765            } else {
1766              !!!cp (173);
1767          }          }
1768    
1769          #          #
1770        } elsif ($self->{next_input_character} == 0x0053 or # S        } elsif ($self->{next_char} == 0x0053 or # S
1771                 $self->{next_input_character} == 0x0073) { # s                 $self->{next_char} == 0x0073) { # s
1772          !!!next-input-character;          !!!next-input-character;
1773          if ($self->{next_input_character} == 0x0059 or # Y          if ($self->{next_char} == 0x0059 or # Y
1774              $self->{next_input_character} == 0x0079) { # y              $self->{next_char} == 0x0079) { # y
1775            !!!next-input-character;            !!!next-input-character;
1776            if ($self->{next_input_character} == 0x0053 or # S            if ($self->{next_char} == 0x0053 or # S
1777                $self->{next_input_character} == 0x0073) { # s                $self->{next_char} == 0x0073) { # s
1778              !!!next-input-character;              !!!next-input-character;
1779              if ($self->{next_input_character} == 0x0054 or # T              if ($self->{next_char} == 0x0054 or # T
1780                  $self->{next_input_character} == 0x0074) { # t                  $self->{next_char} == 0x0074) { # t
1781                !!!next-input-character;                !!!next-input-character;
1782                if ($self->{next_input_character} == 0x0045 or # E                if ($self->{next_char} == 0x0045 or # E
1783                    $self->{next_input_character} == 0x0065) { # e                    $self->{next_char} == 0x0065) { # e
1784                  !!!next-input-character;                  !!!next-input-character;
1785                  if ($self->{next_input_character} == 0x004D or # M                  if ($self->{next_char} == 0x004D or # M
1786                      $self->{next_input_character} == 0x006D) { # m                      $self->{next_char} == 0x006D) { # m
1787                    $self->{state} = 'before DOCTYPE system identifier';                    !!!cp (174);
1788                      $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1789                    !!!next-input-character;                    !!!next-input-character;
1790                    redo A;                    redo A;
1791                    } else {
1792                      !!!cp (175);
1793                  }                  }
1794                  } else {
1795                    !!!cp (176);
1796                }                }
1797                } else {
1798                  !!!cp (177);
1799              }              }
1800              } else {
1801                !!!cp (178);
1802            }            }
1803            } else {
1804              !!!cp (179);
1805          }          }
1806    
1807          #          #
1808        } else {        } else {
1809            !!!cp (180);
1810          !!!next-input-character;          !!!next-input-character;
1811          #          #
1812        }        }
1813    
1814        !!!parse-error (type => 'string after DOCTYPE name');        !!!parse-error (type => 'string after DOCTYPE name');
1815        $self->{state} = 'bogus DOCTYPE';        $self->{current_token}->{quirks} = 1;
1816    
1817          $self->{state} = BOGUS_DOCTYPE_STATE;
1818        # next-input-character is already done        # next-input-character is already done
1819        redo A;        redo A;
1820      } elsif ($self->{state} eq 'before DOCTYPE public identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
1821        if ({        if ({
1822              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1823              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
1824            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
1825            !!!cp (181);
1826          ## Stay in the state          ## Stay in the state
1827          !!!next-input-character;          !!!next-input-character;
1828          redo A;          redo A;
1829        } elsif ($self->{next_input_character} eq 0x0022) { # "        } elsif ($self->{next_char} eq 0x0022) { # "
1830            !!!cp (182);
1831          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1832          $self->{state} = 'DOCTYPE public identifier (double-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
1833          !!!next-input-character;          !!!next-input-character;
1834          redo A;          redo A;
1835        } elsif ($self->{next_input_character} eq 0x0027) { # '        } elsif ($self->{next_char} eq 0x0027) { # '
1836            !!!cp (183);
1837          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1838          $self->{state} = 'DOCTYPE public identifier (single-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
1839          !!!next-input-character;          !!!next-input-character;
1840          redo A;          redo A;
1841        } elsif ($self->{next_input_character} eq 0x003E) { # >        } elsif ($self->{next_char} eq 0x003E) { # >
1842            !!!cp (184);
1843          !!!parse-error (type => 'no PUBLIC literal');          !!!parse-error (type => 'no PUBLIC literal');
1844    
1845          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1846          !!!next-input-character;          !!!next-input-character;
1847    
1848          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1849          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1850    
1851          redo A;          redo A;
1852        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1853            !!!cp (185);
1854          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1855    
1856          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1857          ## reconsume          ## reconsume
1858    
1859          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1860          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1861    
1862          redo A;          redo A;
1863        } else {        } else {
1864            !!!cp (186);
1865          !!!parse-error (type => 'string after PUBLIC');          !!!parse-error (type => 'string after PUBLIC');
1866          $self->{state} = 'bogus DOCTYPE';          $self->{current_token}->{quirks} = 1;
1867    
1868            $self->{state} = BOGUS_DOCTYPE_STATE;
1869          !!!next-input-character;          !!!next-input-character;
1870          redo A;          redo A;
1871        }        }
1872      } elsif ($self->{state} eq 'DOCTYPE public identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
1873        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
1874          $self->{state} = 'after DOCTYPE public identifier';          !!!cp (187);
1875            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1876          !!!next-input-character;          !!!next-input-character;
1877          redo A;          redo A;
1878        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
1879            !!!cp (188);
1880          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
1881    
1882          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1883            !!!next-input-character;
1884    
1885            $self->{current_token}->{quirks} = 1;
1886            !!!emit ($self->{current_token}); # DOCTYPE
1887    
1888            redo A;
1889          } elsif ($self->{next_char} == -1) {
1890            !!!cp (189);
1891            !!!parse-error (type => 'unclosed PUBLIC literal');
1892    
1893            $self->{state} = DATA_STATE;
1894          ## reconsume          ## reconsume
1895    
1896          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1897          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1898    
1899          redo A;          redo A;
1900        } else {        } else {
1901            !!!cp (190);
1902          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
1903              .= chr $self->{next_input_character};              .= chr $self->{next_char};
1904          ## Stay in the state          ## Stay in the state
1905          !!!next-input-character;          !!!next-input-character;
1906          redo A;          redo A;
1907        }        }
1908      } elsif ($self->{state} eq 'DOCTYPE public identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
1909        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
1910          $self->{state} = 'after DOCTYPE public identifier';          !!!cp (191);
1911            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1912          !!!next-input-character;          !!!next-input-character;
1913          redo A;          redo A;
1914        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
1915            !!!cp (192);
1916          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
1917    
1918          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1919            !!!next-input-character;
1920    
1921            $self->{current_token}->{quirks} = 1;
1922            !!!emit ($self->{current_token}); # DOCTYPE
1923    
1924            redo A;
1925          } elsif ($self->{next_char} == -1) {
1926            !!!cp (193);
1927            !!!parse-error (type => 'unclosed PUBLIC literal');
1928    
1929            $self->{state} = DATA_STATE;
1930          ## reconsume          ## reconsume
1931    
1932          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1933          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1934    
1935          redo A;          redo A;
1936        } else {        } else {
1937            !!!cp (194);
1938          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
1939              .= chr $self->{next_input_character};              .= chr $self->{next_char};
1940          ## Stay in the state          ## Stay in the state
1941          !!!next-input-character;          !!!next-input-character;
1942          redo A;          redo A;
1943        }        }
1944      } elsif ($self->{state} eq 'after DOCTYPE public identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
1945        if ({        if ({
1946              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1947              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
1948            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
1949            !!!cp (195);
1950          ## Stay in the state          ## Stay in the state
1951          !!!next-input-character;          !!!next-input-character;
1952          redo A;          redo A;
1953        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
1954            !!!cp (196);
1955          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1956          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
1957          !!!next-input-character;          !!!next-input-character;
1958          redo A;          redo A;
1959        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
1960            !!!cp (197);
1961          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1962          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
1963          !!!next-input-character;          !!!next-input-character;
1964          redo A;          redo A;
1965        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1966          $self->{state} = 'data';          !!!cp (198);
1967            $self->{state} = DATA_STATE;
1968          !!!next-input-character;          !!!next-input-character;
1969    
1970          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1971    
1972          redo A;          redo A;
1973        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1974            !!!cp (199);
1975          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1976    
1977          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1978          ## recomsume          ## reconsume
1979    
1980          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1981          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1982    
1983          redo A;          redo A;
1984        } else {        } else {
1985            !!!cp (200);
1986          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after PUBLIC literal');
1987          $self->{state} = 'bogus DOCTYPE';          $self->{current_token}->{quirks} = 1;
1988    
1989            $self->{state} = BOGUS_DOCTYPE_STATE;
1990          !!!next-input-character;          !!!next-input-character;
1991          redo A;          redo A;
1992        }        }
1993      } elsif ($self->{state} eq 'before DOCTYPE system identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
1994        if ({        if ({
1995              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1996              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
1997            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
1998            !!!cp (201);
1999          ## Stay in the state          ## Stay in the state
2000          !!!next-input-character;          !!!next-input-character;
2001          redo A;          redo A;
2002        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
2003            !!!cp (202);
2004          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2005          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2006          !!!next-input-character;          !!!next-input-character;
2007          redo A;          redo A;
2008        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
2009            !!!cp (203);
2010          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2011          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2012          !!!next-input-character;          !!!next-input-character;
2013          redo A;          redo A;
2014        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2015            !!!cp (204);
2016          !!!parse-error (type => 'no SYSTEM literal');          !!!parse-error (type => 'no SYSTEM literal');
2017          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2018          !!!next-input-character;          !!!next-input-character;
2019    
2020          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2021          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2022    
2023          redo A;          redo A;
2024        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2025            !!!cp (205);
2026          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2027    
2028          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2029          ## recomsume          ## reconsume
2030    
2031          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2032          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2033    
2034          redo A;          redo A;
2035        } else {        } else {
2036          !!!parse-error (type => 'string after PUBLIC literal');          !!!cp (206);
2037          $self->{state} = 'bogus DOCTYPE';          !!!parse-error (type => 'string after SYSTEM');
2038            $self->{current_token}->{quirks} = 1;
2039    
2040            $self->{state} = BOGUS_DOCTYPE_STATE;
2041          !!!next-input-character;          !!!next-input-character;
2042          redo A;          redo A;
2043        }        }
2044      } elsif ($self->{state} eq 'DOCTYPE system identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2045        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
2046          $self->{state} = 'after DOCTYPE system identifier';          !!!cp (207);
2047            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2048          !!!next-input-character;          !!!next-input-character;
2049          redo A;          redo A;
2050        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2051            !!!cp (208);
2052            !!!parse-error (type => 'unclosed PUBLIC literal');
2053    
2054            $self->{state} = DATA_STATE;
2055            !!!next-input-character;
2056    
2057            $self->{current_token}->{quirks} = 1;
2058            !!!emit ($self->{current_token}); # DOCTYPE
2059    
2060            redo A;
2061          } elsif ($self->{next_char} == -1) {
2062            !!!cp (209);
2063          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2064    
2065          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2066          ## reconsume          ## reconsume
2067    
2068          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2069          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2070    
2071          redo A;          redo A;
2072        } else {        } else {
2073            !!!cp (210);
2074          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2075              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2076          ## Stay in the state          ## Stay in the state
2077          !!!next-input-character;          !!!next-input-character;
2078          redo A;          redo A;
2079        }        }
2080      } elsif ($self->{state} eq 'DOCTYPE system identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
2081        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
2082          $self->{state} = 'after DOCTYPE system identifier';          !!!cp (211);
2083            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2084          !!!next-input-character;          !!!next-input-character;
2085          redo A;          redo A;
2086        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2087            !!!cp (212);
2088            !!!parse-error (type => 'unclosed PUBLIC literal');
2089    
2090            $self->{state} = DATA_STATE;
2091            !!!next-input-character;
2092    
2093            $self->{current_token}->{quirks} = 1;
2094            !!!emit ($self->{current_token}); # DOCTYPE
2095    
2096            redo A;
2097          } elsif ($self->{next_char} == -1) {
2098            !!!cp (213);
2099          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2100    
2101          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2102          ## reconsume          ## reconsume
2103    
2104          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2105          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2106    
2107          redo A;          redo A;
2108        } else {        } else {
2109            !!!cp (214);
2110          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2111              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2112          ## Stay in the state          ## Stay in the state
2113          !!!next-input-character;          !!!next-input-character;
2114          redo A;          redo A;
2115        }        }
2116      } elsif ($self->{state} eq 'after DOCTYPE system identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2117        if ({        if ({
2118              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2119              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2120            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2121            !!!cp (215);
2122          ## Stay in the state          ## Stay in the state
2123          !!!next-input-character;          !!!next-input-character;
2124          redo A;          redo A;
2125        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2126          $self->{state} = 'data';          !!!cp (216);
2127            $self->{state} = DATA_STATE;
2128          !!!next-input-character;          !!!next-input-character;
2129    
2130          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2131    
2132          redo A;          redo A;
2133        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2134            !!!cp (217);
2135          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2136    
2137          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2138          ## recomsume          ## reconsume
2139    
2140          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2141          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2142    
2143          redo A;          redo A;
2144        } else {        } else {
2145            !!!cp (218);
2146          !!!parse-error (type => 'string after SYSTEM literal');          !!!parse-error (type => 'string after SYSTEM literal');
2147          $self->{state} = 'bogus DOCTYPE';          #$self->{current_token}->{quirks} = 1;
2148    
2149            $self->{state} = BOGUS_DOCTYPE_STATE;
2150          !!!next-input-character;          !!!next-input-character;
2151          redo A;          redo A;
2152        }        }
2153      } elsif ($self->{state} eq 'bogus DOCTYPE') {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
2154        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2155          $self->{state} = 'data';          !!!cp (219);
2156            $self->{state} = DATA_STATE;
2157          !!!next-input-character;          !!!next-input-character;
2158    
         delete $self->{current_token}->{correct};  
2159          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2160    
2161          redo A;          redo A;
2162        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2163            !!!cp (220);
2164          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2165          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2166          ## reconsume          ## reconsume
2167    
         delete $self->{current_token}->{correct};  
2168          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2169    
2170          redo A;          redo A;
2171        } else {        } else {
2172            !!!cp (221);
2173          ## Stay in the state          ## Stay in the state
2174          !!!next-input-character;          !!!next-input-character;
2175          redo A;          redo A;
# Line 1570  sub _get_next_token ($) { Line 2182  sub _get_next_token ($) {
2182    die "$0: _get_next_token: unexpected case";    die "$0: _get_next_token: unexpected case";
2183  } # _get_next_token  } # _get_next_token
2184    
2185  sub _tokenize_attempt_to_consume_an_entity ($) {  sub _tokenize_attempt_to_consume_an_entity ($$$) {
2186    my $self = shift;    my ($self, $in_attr, $additional) = @_;
2187    
2188    if ({    if ({
2189         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
2190         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
2191        }->{$self->{next_input_character}}) {         $additional => 1,
2192          }->{$self->{next_char}}) {
2193      ## Don't consume      ## Don't consume
2194      ## No error      ## No error
2195      return undef;      return undef;
2196    } elsif ($self->{next_input_character} == 0x0023) { # #    } elsif ($self->{next_char} == 0x0023) { # #
2197      !!!next-input-character;      !!!next-input-character;
2198      if ($self->{next_input_character} == 0x0078 or # x      if ($self->{next_char} == 0x0078 or # x
2199          $self->{next_input_character} == 0x0058) { # X          $self->{next_char} == 0x0058) { # X
2200        my $num;        my $code;
2201        X: {        X: {
2202          my $x_char = $self->{next_input_character};          my $x_char = $self->{next_char};
2203          !!!next-input-character;          !!!next-input-character;
2204          if (0x0030 <= $self->{next_input_character} and          if (0x0030 <= $self->{next_char} and
2205              $self->{next_input_character} <= 0x0039) { # 0..9              $self->{next_char} <= 0x0039) { # 0..9
2206            $num ||= 0;            $code ||= 0;
2207            $num *= 0x10;            $code *= 0x10;
2208            $num += $self->{next_input_character} - 0x0030;            $code += $self->{next_char} - 0x0030;
2209            redo X;            redo X;
2210          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
2211                   $self->{next_input_character} <= 0x0066) { # a..f                   $self->{next_char} <= 0x0066) { # a..f
2212            ## ISSUE: the spec says U+0078, which is apparently incorrect            $code ||= 0;
2213            $num ||= 0;            $code *= 0x10;
2214            $num *= 0x10;            $code += $self->{next_char} - 0x0060 + 9;
           $num += $self->{next_input_character} - 0x0060 + 9;  
2215            redo X;            redo X;
2216          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
2217                   $self->{next_input_character} <= 0x0046) { # A..F                   $self->{next_char} <= 0x0046) { # A..F
2218            ## ISSUE: the spec says U+0058, which is apparently incorrect            $code ||= 0;
2219            $num ||= 0;            $code *= 0x10;
2220            $num *= 0x10;            $code += $self->{next_char} - 0x0040 + 9;
           $num += $self->{next_input_character} - 0x0040 + 9;  
2221            redo X;            redo X;
2222          } elsif (not defined $num) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
2223            !!!parse-error (type => 'bare hcro');            !!!parse-error (type => 'bare hcro');
2224            $self->{next_input_character} = 0x0023; # #            !!!back-next-input-character ($x_char, $self->{next_char});
2225            !!!back-next-input-character ($x_char);            $self->{next_char} = 0x0023; # #
2226            return undef;            return undef;
2227          } elsif ($self->{next_input_character} == 0x003B) { # ;          } elsif ($self->{next_char} == 0x003B) { # ;
2228            !!!next-input-character;            !!!next-input-character;
2229          } else {          } else {
2230            !!!parse-error (type => 'no refc');            !!!parse-error (type => 'no refc');
2231          }          }
2232    
2233          ## TODO: check the definition for |a valid Unicode character|.          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2234          ## <http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2006-December/thread.html#8189>            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);
2235          if ($num > 1114111 or $num == 0) {            $code = 0xFFFD;
2236            $num = 0xFFFD; # REPLACEMENT CHARACTER          } elsif ($code > 0x10FFFF) {
2237            ## ISSUE: Why this is not an error?            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);
2238          } elsif (0x80 <= $num and $num <= 0x9F) {            $code = 0xFFFD;
2239            !!!parse-error (type => sprintf 'c1 entity:U+%04X', $num);          } elsif ($code == 0x000D) {
2240            $num = $c1_entity_char->{$num};            !!!parse-error (type => 'CR character reference');
2241              $code = 0x000A;
2242            } elsif (0x80 <= $code and $code <= 0x9F) {
2243              !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);
2244              $code = $c1_entity_char->{$code};
2245          }          }
2246    
2247          return {type => 'character', data => chr $num};          return {type => CHARACTER_TOKEN, data => chr $code,
2248                    has_reference => 1};
2249        } # X        } # X
2250      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_char} and
2251               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_char} <= 0x0039) { # 0..9
2252        my $code = $self->{next_input_character} - 0x0030;        my $code = $self->{next_char} - 0x0030;
2253        !!!next-input-character;        !!!next-input-character;
2254                
2255        while (0x0030 <= $self->{next_input_character} and        while (0x0030 <= $self->{next_char} and
2256                  $self->{next_input_character} <= 0x0039) { # 0..9                  $self->{next_char} <= 0x0039) { # 0..9
2257          $code *= 10;          $code *= 10;
2258          $code += $self->{next_input_character} - 0x0030;          $code += $self->{next_char} - 0x0030;
2259                    
2260          !!!next-input-character;          !!!next-input-character;
2261        }        }
2262    
2263        if ($self->{next_input_character} == 0x003B) { # ;        if ($self->{next_char} == 0x003B) { # ;
2264          !!!next-input-character;          !!!next-input-character;
2265        } else {        } else {
2266          !!!parse-error (type => 'no refc');          !!!parse-error (type => 'no refc');
2267        }        }
2268    
2269        ## TODO: check the definition for |a valid Unicode character|.        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2270        if ($code > 1114111 or $code == 0) {          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);
2271          $code = 0xFFFD; # REPLACEMENT CHARACTER          $code = 0xFFFD;
2272          ## ISSUE: Why this is not an error?        } elsif ($code > 0x10FFFF) {
2273            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);
2274            $code = 0xFFFD;
2275          } elsif ($code == 0x000D) {
2276            !!!parse-error (type => 'CR character reference');
2277            $code = 0x000A;
2278        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
2279          !!!parse-error (type => sprintf 'c1 entity:U+%04X', $code);          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);
2280          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
2281        }        }
2282                
2283        return {type => 'character', data => chr $code};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1};
2284      } else {      } else {
2285        !!!parse-error (type => 'bare nero');        !!!parse-error (type => 'bare nero');
2286        !!!back-next-input-character ($self->{next_input_character});        !!!back-next-input-character ($self->{next_char});
2287        $self->{next_input_character} = 0x0023; # #        $self->{next_char} = 0x0023; # #
2288        return undef;        return undef;
2289      }      }
2290    } elsif ((0x0041 <= $self->{next_input_character} and    } elsif ((0x0041 <= $self->{next_char} and
2291              $self->{next_input_character} <= 0x005A) or              $self->{next_char} <= 0x005A) or
2292             (0x0061 <= $self->{next_input_character} and             (0x0061 <= $self->{next_char} and
2293              $self->{next_input_character} <= 0x007A)) {              $self->{next_char} <= 0x007A)) {
2294      my $entity_name = chr $self->{next_input_character};      my $entity_name = chr $self->{next_char};
2295      !!!next-input-character;      !!!next-input-character;
2296    
2297      my $value = $entity_name;      my $value = $entity_name;
2298      my $match;      my $match = 0;
2299      require Whatpm::_NamedEntityList;      require Whatpm::_NamedEntityList;
2300      our $EntityChar;      our $EntityChar;
2301    
2302      while (length $entity_name < 10 and      while (length $entity_name < 10 and
2303             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
2304             ((0x0041 <= $self->{next_input_character} and # a             ((0x0041 <= $self->{next_char} and # a
2305               $self->{next_input_character} <= 0x005A) or # x               $self->{next_char} <= 0x005A) or # x
2306              (0x0061 <= $self->{next_input_character} and # a              (0x0061 <= $self->{next_char} and # a
2307               $self->{next_input_character} <= 0x007A) or # z               $self->{next_char} <= 0x007A) or # z
2308              (0x0030 <= $self->{next_input_character} and # 0              (0x0030 <= $self->{next_char} and # 0
2309               $self->{next_input_character} <= 0x0039) or # 9               $self->{next_char} <= 0x0039) or # 9
2310              $self->{next_input_character} == 0x003B)) { # ;              $self->{next_char} == 0x003B)) { # ;
2311        $entity_name .= chr $self->{next_input_character};        $entity_name .= chr $self->{next_char};
2312        if (defined $EntityChar->{$entity_name}) {        if (defined $EntityChar->{$entity_name}) {
2313          $value = $EntityChar->{$entity_name};          if ($self->{next_char} == 0x003B) { # ;
2314          if ($self->{next_input_character} == 0x003B) { # ;            $value = $EntityChar->{$entity_name};
2315            $match = 1;            $match = 1;
2316            !!!next-input-character;            !!!next-input-character;
2317            last;            last;
2318          } else {          } else {
2319              $value = $EntityChar->{$entity_name};
2320            $match = -1;            $match = -1;
2321              !!!next-input-character;
2322          }          }
2323        } else {        } else {
2324          $value .= chr $self->{next_input_character};          $value .= chr $self->{next_char};
2325            $match *= 2;
2326            !!!next-input-character;
2327        }        }
       !!!next-input-character;  
2328      }      }
2329            
2330      if ($match > 0) {      if ($match > 0) {
2331        return {type => 'character', data => $value};        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};
2332      } elsif ($match < 0) {      } elsif ($match < 0) {
2333        !!!parse-error (type => 'refc');        !!!parse-error (type => 'no refc');
2334        return {type => 'character', data => $value};        if ($in_attr and $match < -1) {
2335            return {type => CHARACTER_TOKEN, data => '&'.$entity_name};
2336          } else {
2337            return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};
2338          }
2339      } else {      } else {
2340        !!!parse-error (type => 'bare ero');        !!!parse-error (type => 'bare ero');
2341        ## NOTE: No characters are consumed in the spec.        ## NOTE: "No characters are consumed" in the spec.
2342        !!!back-token ({type => 'character', data => $value});        return {type => CHARACTER_TOKEN, data => '&'.$value};
       return undef;  
2343      }      }
2344    } else {    } else {
2345      ## no characters are consumed      ## no characters are consumed
# Line 1754  sub _construct_tree ($) { Line 2381  sub _construct_tree ($) {
2381        
2382    !!!next-token;    !!!next-token;
2383    
2384    $self->{insertion_mode} = 'before head';    $self->{insertion_mode} = BEFORE_HEAD_IM;
2385    undef $self->{form_element};    undef $self->{form_element};
2386    undef $self->{head_element};    undef $self->{head_element};
2387    $self->{open_elements} = [];    $self->{open_elements} = [];
# Line 1768  sub _construct_tree ($) { Line 2395  sub _construct_tree ($) {
2395  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
2396    my $self = shift;    my $self = shift;
2397    INITIAL: {    INITIAL: {
2398      if ($token->{type} eq 'DOCTYPE') {      if ($token->{type} == DOCTYPE_TOKEN) {
2399        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
2400        ## error, switch to a conformance checking mode for another        ## error, switch to a conformance checking mode for another
2401        ## language.        ## language.
# Line 1794  sub _tree_construction_initial ($) { Line 2421  sub _tree_construction_initial ($) {
2421        ## ISSUE: internalSubset = null??        ## ISSUE: internalSubset = null??
2422        $self->{document}->append_child ($doctype);        $self->{document}->append_child ($doctype);
2423                
2424        if (not $token->{correct} or $doctype_name ne 'HTML') {        if ($token->{quirks} or $doctype_name ne 'HTML') {
2425          $self->{document}->manakai_compat_mode ('quirks');          $self->{document}->manakai_compat_mode ('quirks');
2426        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{public_identifier}) {
2427          my $pubid = $token->{public_identifier};          my $pubid = $token->{public_identifier};
# Line 1848  sub _tree_construction_initial ($) { Line 2475  sub _tree_construction_initial ($) {
2475            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,
2476            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,
2477            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,
2478              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//EN" => 1,
2479              "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//EN" => 1,
2480              "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//EN" => 1,
2481            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,
2482            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,
2483            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,
# Line 1895  sub _tree_construction_initial ($) { Line 2525  sub _tree_construction_initial ($) {
2525        !!!next-token;        !!!next-token;
2526        return;        return;
2527      } elsif ({      } elsif ({
2528                'start tag' => 1,                START_TAG_TOKEN, 1,
2529                'end tag' => 1,                END_TAG_TOKEN, 1,
2530                'end-of-file' => 1,                END_OF_FILE_TOKEN, 1,
2531               }->{$token->{type}}) {               }->{$token->{type}}) {
2532        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE');
2533        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
2534        ## Go to the root element phase        ## Go to the root element phase
2535        ## reprocess        ## reprocess
2536        return;        return;
2537      } elsif ($token->{type} eq 'character') {      } elsif ($token->{type} == CHARACTER_TOKEN) {
2538        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2539          ## Ignore the token          ## Ignore the token
2540    
2541          unless (length $token->{data}) {          unless (length $token->{data}) {
2542            ## Stay in the phase            ## Stay in the phase
2543            !!!next-token;            !!!next-token;
# Line 1919  sub _tree_construction_initial ($) { Line 2550  sub _tree_construction_initial ($) {
2550        ## Go to the root element phase        ## Go to the root element phase
2551        ## reprocess        ## reprocess
2552        return;        return;
2553      } elsif ($token->{type} eq 'comment') {      } elsif ($token->{type} == COMMENT_TOKEN) {
2554        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
2555        $self->{document}->append_child ($comment);        $self->{document}->append_child ($comment);
2556                
# Line 1927  sub _tree_construction_initial ($) { Line 2558  sub _tree_construction_initial ($) {
2558        !!!next-token;        !!!next-token;
2559        redo INITIAL;        redo INITIAL;
2560      } else {      } else {
2561        die "$0: $token->{type}: Unknown token";        die "$0: $token->{type}: Unknown token type";
2562      }      }
2563    } # INITIAL    } # INITIAL
2564  } # _tree_construction_initial  } # _tree_construction_initial
# Line 1936  sub _tree_construction_root_element ($) Line 2567  sub _tree_construction_root_element ($)
2567    my $self = shift;    my $self = shift;
2568        
2569    B: {    B: {
2570        if ($token->{type} eq 'DOCTYPE') {        if ($token->{type} == DOCTYPE_TOKEN) {
2571          !!!parse-error (type => 'in html:#DOCTYPE');          !!!parse-error (type => 'in html:#DOCTYPE');
2572          ## Ignore the token          ## Ignore the token
2573          ## Stay in the phase          ## Stay in the phase
2574          !!!next-token;          !!!next-token;
2575          redo B;          redo B;
2576        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{type} == COMMENT_TOKEN) {
2577          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
2578          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
2579          ## Stay in the phase          ## Stay in the phase
2580          !!!next-token;          !!!next-token;
2581          redo B;          redo B;
2582        } elsif ($token->{type} eq 'character') {        } elsif ($token->{type} == CHARACTER_TOKEN) {
2583          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2584            $self->{document}->manakai_append_text ($1);            ## Ignore the token.
2585            ## ISSUE: DOM3 Core does not allow Document > Text  
2586            unless (length $token->{data}) {            unless (length $token->{data}) {
2587              ## Stay in the phase              ## Stay in the phase
2588              !!!next-token;              !!!next-token;
2589              redo B;              redo B;
2590            }            }
2591          }          }
2592    
2593            $self->{application_cache_selection}->(undef);
2594    
2595            #
2596          } elsif ($token->{type} == START_TAG_TOKEN) {
2597            if ($token->{tag_name} eq 'html' and
2598                $token->{attributes}->{manifest}) {
2599              $self->{application_cache_selection}
2600                   ->($token->{attributes}->{manifest}->{value});
2601              ## ISSUE: No relative reference resolution?
2602            } else {
2603              $self->{application_cache_selection}->(undef);
2604            }
2605    
2606            ## ISSUE: There is an issue in the spec
2607          #          #
2608        } elsif ({        } elsif ({
2609                  'start tag' => 1,                  END_TAG_TOKEN, 1,
2610                  'end tag' => 1,                  END_OF_FILE_TOKEN, 1,
                 'end-of-file' => 1,  
2611                 }->{$token->{type}}) {                 }->{$token->{type}}) {
2612            $self->{application_cache_selection}->(undef);
2613    
2614          ## ISSUE: There is an issue in the spec          ## ISSUE: There is an issue in the spec
2615          #          #
2616        } else {        } else {
2617          die "$0: $token->{type}: Unknown token";          die "$0: $token->{type}: Unknown token type";
2618        }        }
2619    
2620        my $root_element; !!!create-element ($root_element, 'html');        my $root_element; !!!create-element ($root_element, 'html');
2621        $self->{document}->append_child ($root_element);        $self->{document}->append_child ($root_element);
2622        push @{$self->{open_elements}}, [$root_element, 'html'];        push @{$self->{open_elements}}, [$root_element, 'html'];
       #$phase = 'main';  
2623        ## reprocess        ## reprocess
2624        #redo B;        #redo B;
2625        return;        return; ## Go to the main phase.
2626    } # B    } # B
2627  } # _tree_construction_root_element  } # _tree_construction_root_element
2628    
# Line 1991  sub _reset_insertion_mode ($) { Line 2638  sub _reset_insertion_mode ($) {
2638            
2639      ## Step 3      ## Step 3
2640      S3: {      S3: {
2641        $last = 1 if $self->{open_elements}->[0]->[0] eq $node->[0];        ## ISSUE: Oops! "If node is the first node in the stack of open
2642        if (defined $self->{inner_html_node}) {        ## elements, then set last to true. If the context element of the
2643          if ($self->{inner_html_node}->[1] eq 'td' or        ## HTML fragment parsing algorithm is neither a td element nor a
2644              $self->{inner_html_node}->[1] eq 'th') {        ## th element, then set node to the context element. (fragment case)":
2645            #        ## The second "if" is in the scope of the first "if"!?
2646          } else {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
2647            $node = $self->{inner_html_node};          $last = 1;
2648            if (defined $self->{inner_html_node}) {
2649              if ($self->{inner_html_node}->[1] eq 'td' or
2650                  $self->{inner_html_node}->[1] eq 'th') {
2651                #
2652              } else {
2653                $node = $self->{inner_html_node};
2654              }
2655          }          }
2656        }        }
2657            
2658        ## Step 4..13        ## Step 4..13
2659        my $new_mode = {        my $new_mode = {
2660                        select => 'in select',                        select => IN_SELECT_IM,
2661                        td => 'in cell',                        td => IN_CELL_IM,
2662                        th => 'in cell',                        th => IN_CELL_IM,
2663                        tr => 'in row',                        tr => IN_ROW_IM,
2664                        tbody => 'in table body',                        tbody => IN_TABLE_BODY_IM,
2665                        thead => 'in table head',                        thead => IN_TABLE_BODY_IM,
2666                        tfoot => 'in table foot',                        tfoot => IN_TABLE_BODY_IM,
2667                        caption => 'in caption',                        caption => IN_CAPTION_IM,
2668                        colgroup => 'in column group',                        colgroup => IN_COLUMN_GROUP_IM,
2669                        table => 'in table',                        table => IN_TABLE_IM,
2670                        head => 'in body', # not in head!                        head => IN_BODY_IM, # not in head!
2671                        body => 'in body',                        body => IN_BODY_IM,
2672                        frameset => 'in frameset',                        frameset => IN_FRAMESET_IM,
2673                       }->{$node->[1]};                       }->{$node->[1]};
2674        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
2675                
2676        ## Step 14        ## Step 14
2677        if ($node->[1] eq 'html') {        if ($node->[1] eq 'html') {
2678          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
2679            $self->{insertion_mode} = 'before head';            $self->{insertion_mode} = BEFORE_HEAD_IM;
2680          } else {          } else {
2681            $self->{insertion_mode} = 'after head';            $self->{insertion_mode} = AFTER_HEAD_IM;
2682          }          }
2683          return;          return;
2684        }        }
2685                
2686        ## Step 15        ## Step 15
2687        $self->{insertion_mode} = 'in body' and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
2688                
2689        ## Step 16        ## Step 16
2690        $i--;        $i--;
# Line 2044  sub _reset_insertion_mode ($) { Line 2698  sub _reset_insertion_mode ($) {
2698  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
2699    my $self = shift;    my $self = shift;
2700    
   my $phase = 'main';  
   
2701    my $active_formatting_elements = [];    my $active_formatting_elements = [];
2702    
2703    my $reconstruct_active_formatting_elements = sub { # MUST    my $reconstruct_active_formatting_elements = sub { # MUST
# Line 2128  sub _tree_construction_main ($) { Line 2780  sub _tree_construction_main ($) {
2780      }      }
2781    }; # $clear_up_to_marker    }; # $clear_up_to_marker
2782    
2783    my $style_start_tag = sub {    my $parse_rcdata = sub ($$) {
2784      my $style_el; !!!create-element ($style_el, 'style', $token->{attributes});      my ($content_model_flag, $insert) = @_;
2785      ## $self->{insertion_mode} eq 'in head' and ... (always true)  
2786      (($self->{insertion_mode} eq 'in head' and defined $self->{head_element})      ## Step 1
2787       ? $self->{head_element} : $self->{open_elements}->[-1]->[0])      my $start_tag_name = $token->{tag_name};
2788        ->append_child ($style_el);      my $el;
2789      $self->{content_model_flag} = 'CDATA';      !!!create-element ($el, $start_tag_name, $token->{attributes});
2790    
2791        ## Step 2
2792        $insert->($el); # /context node/->append_child ($el)
2793    
2794        ## Step 3
2795        $self->{content_model} = $content_model_flag; # CDATA or RCDATA
2796      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
2797                  
2798        ## Step 4
2799      my $text = '';      my $text = '';
2800      !!!next-token;      !!!next-token;
2801      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
2802        $text .= $token->{data};        $text .= $token->{data};
2803        !!!next-token;        !!!next-token;
2804      } # stop if non-character token or tokenizer stops tokenising      }
2805    
2806        ## Step 5
2807      if (length $text) {      if (length $text) {
2808        $style_el->manakai_append_text ($text);        my $text = $self->{document}->create_text_node ($text);
2809          $el->append_child ($text);
2810      }      }
2811        
2812      $self->{content_model_flag} = 'PCDATA';      ## Step 6
2813                      $self->{content_model} = PCDATA_CONTENT_MODEL;
2814      if ($token->{type} eq 'end tag' and $token->{tag_name} eq 'style') {  
2815        ## Step 7
2816        if ($token->{type} == END_TAG_TOKEN and $token->{tag_name} eq $start_tag_name) {
2817        ## Ignore the token        ## Ignore the token
2818      } else {      } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {
2819        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!parse-error (type => 'in CDATA:#'.$token->{type});
2820        ## ISSUE: And ignore?      } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
2821          !!!parse-error (type => 'in RCDATA:#'.$token->{type});
2822        } else {
2823          die "$0: $content_model_flag in parse_rcdata";
2824      }      }
2825      !!!next-token;      !!!next-token;
2826    }; # $style_start_tag    }; # $parse_rcdata
2827    
2828    my $script_start_tag = sub {    my $script_start_tag = sub ($) {
2829        my $insert = $_[0];
2830      my $script_el;      my $script_el;
2831      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, 'script', $token->{attributes});
2832      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
2833    
2834      $self->{content_model_flag} = 'CDATA';      $self->{content_model} = CDATA_CONTENT_MODEL;
2835      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
2836            
2837      my $text = '';      my $text = '';
2838      !!!next-token;      !!!next-token;
2839      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) {
2840        $text .= $token->{data};        $text .= $token->{data};
2841        !!!next-token;        !!!next-token;
2842      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
# Line 2176  sub _tree_construction_main ($) { Line 2844  sub _tree_construction_main ($) {
2844        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
2845      }      }
2846                                
2847      $self->{content_model_flag} = 'PCDATA';      $self->{content_model} = PCDATA_CONTENT_MODEL;
2848    
2849      if ($token->{type} eq 'end tag' and      if ($token->{type} == END_TAG_TOKEN and
2850          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
2851        ## Ignore the token        ## Ignore the token
2852      } else {      } else {
# Line 2192  sub _tree_construction_main ($) { Line 2860  sub _tree_construction_main ($) {
2860      } else {      } else {
2861        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
2862        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
2863          
2864        (($self->{insertion_mode} eq 'in head' and defined $self->{head_element})        $insert->($script_el);
        ? $self->{head_element} : $self->{open_elements}->[-1]->[0])->append_child ($script_el);  
2865                
2866        ## TODO: insertion point = $old_insertion_point (might be "undefined")        ## TODO: insertion point = $old_insertion_point (might be "undefined")
2867                
# Line 2388  sub _tree_construction_main ($) { Line 3055  sub _tree_construction_main ($) {
3055    }; # $formatting_end_tag    }; # $formatting_end_tag
3056    
3057    my $insert_to_current = sub {    my $insert_to_current = sub {
3058      $self->{open_elements}->[-1]->[0]->append_child (shift);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
3059    }; # $insert_to_current    }; # $insert_to_current
3060    
3061    my $insert_to_foster = sub {    my $insert_to_foster = sub {
# Line 2422  sub _tree_construction_main ($) { Line 3089  sub _tree_construction_main ($) {
3089                         }                         }
3090    }; # $insert_to_foster    }; # $insert_to_foster
3091    
3092    my $in_body = sub {    my $insert;
3093      my $insert = shift;  
3094      if ($token->{type} eq 'start tag') {    B: {
3095        if ($token->{tag_name} eq 'script') {      if ($token->{type} == DOCTYPE_TOKEN) {
3096          $script_start_tag->();        !!!parse-error (type => 'DOCTYPE in the middle');
3097          return;        ## Ignore the token
3098        } elsif ($token->{tag_name} eq 'style') {        ## Stay in the phase
3099          $style_start_tag->();        !!!next-token;
3100          return;        redo B;
3101        } elsif ({      } elsif ($token->{type} == END_OF_FILE_TOKEN) {
3102                  base => 1, link => 1, meta => 1,        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
3103                 }->{$token->{tag_name}}) {          #
3104          ## NOTE: This is an "as if in head" code clone        } else {
3105          my $el;          ## Generate implied end tags
3106          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          if ({
3107          if ($self->{insertion_mode} eq 'in head' and               dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,
3108              defined $self->{head_element}) {               tbody => 1, tfoot=> 1, thead => 1,
3109            $self->{head_element}->append_child ($el);              }->{$self->{open_elements}->[-1]->[1]}) {
3110          } else {            !!!back-token;
3111            $insert->($el);            $token = {type => END_TAG_TOKEN, tag_name => $self->{open_elements}->[-1]->[1]};
3112              redo B;
3113          }          }
3114                    
3115            if (@{$self->{open_elements}} > 2 or
3116                (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {
3117              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3118            } elsif (defined $self->{inner_html_node} and
3119                     @{$self->{open_elements}} > 1 and
3120                     $self->{open_elements}->[1]->[1] ne 'body') {
3121              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3122            }
3123    
3124            ## ISSUE: There is an issue in the spec.
3125          }
3126    
3127          ## Stop parsing
3128          last B;
3129        } elsif ($token->{type} == START_TAG_TOKEN and
3130                 $token->{tag_name} eq 'html') {
3131          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
3132            ## Turn into the main phase
3133            !!!parse-error (type => 'after html:html');
3134            $self->{insertion_mode} = AFTER_BODY_IM;
3135          } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
3136            ## Turn into the main phase
3137            !!!parse-error (type => 'after html:html');
3138            $self->{insertion_mode} = AFTER_FRAMESET_IM;
3139          }
3140    
3141    ## ISSUE: "aa<html>" is not a parse error.
3142    ## ISSUE: "<html>" in fragment is not a parse error.
3143          unless ($token->{first_start_tag}) {
3144            !!!parse-error (type => 'not first start tag');
3145          }
3146          my $top_el = $self->{open_elements}->[0]->[0];
3147          for my $attr_name (keys %{$token->{attributes}}) {
3148            unless ($top_el->has_attribute_ns (undef, $attr_name)) {
3149              $top_el->set_attribute_ns
3150                (undef, [undef, $attr_name],
3151                 $token->{attributes}->{$attr_name}->{value});
3152            }
3153          }
3154          !!!next-token;
3155          redo B;
3156        } elsif ($token->{type} == COMMENT_TOKEN) {
3157          my $comment = $self->{document}->create_comment ($token->{data});
3158          if ($self->{insertion_mode} & AFTER_HTML_IMS) {
3159            $self->{document}->append_child ($comment);
3160          } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
3161            $self->{open_elements}->[0]->[0]->append_child ($comment);
3162          } else {
3163            $self->{open_elements}->[-1]->[0]->append_child ($comment);
3164          }
3165          !!!next-token;
3166          redo B;
3167        } elsif ($self->{insertion_mode} & HEAD_IMS) {
3168          if ($token->{type} == CHARACTER_TOKEN) {
3169            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3170              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3171              unless (length $token->{data}) {
3172                !!!next-token;
3173                redo B;
3174              }
3175            }
3176    
3177            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3178              ## As if <head>
3179              !!!create-element ($self->{head_element}, 'head');
3180              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3181              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3182    
3183              ## Reprocess in the "in head" insertion mode...
3184              pop @{$self->{open_elements}};
3185    
3186              ## Reprocess in the "after head" insertion mode...
3187            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3188              ## As if </noscript>
3189              pop @{$self->{open_elements}};
3190              !!!parse-error (type => 'in noscript:#character');
3191              
3192              ## Reprocess in the "in head" insertion mode...
3193              ## As if </head>
3194              pop @{$self->{open_elements}};
3195    
3196              ## Reprocess in the "after head" insertion mode...
3197            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3198              pop @{$self->{open_elements}};
3199    
3200              ## Reprocess in the "after head" insertion mode...
3201            }
3202    
3203                ## "after head" insertion mode
3204                ## As if <body>
3205                !!!insert-element ('body');
3206                $self->{insertion_mode} = IN_BODY_IM;
3207                ## reprocess
3208                redo B;
3209              } elsif ($token->{type} == START_TAG_TOKEN) {
3210                if ($token->{tag_name} eq 'head') {
3211                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3212                    !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});
3213                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3214                    push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];
3215                    $self->{insertion_mode} = IN_HEAD_IM;
3216                    !!!next-token;
3217                    redo B;
3218                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3219                    #
3220                  } else {
3221                    !!!parse-error (type => 'in head:head'); # or in head noscript
3222                    ## Ignore the token
3223                    !!!next-token;
3224                    redo B;
3225                  }
3226                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3227                  ## As if <head>
3228                  !!!create-element ($self->{head_element}, 'head');
3229                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3230                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3231    
3232                  $self->{insertion_mode} = IN_HEAD_IM;
3233                  ## Reprocess in the "in head" insertion mode...
3234                }
3235    
3236                if ($token->{tag_name} eq 'base') {
3237                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3238                    ## As if </noscript>
3239                    pop @{$self->{open_elements}};
3240                    !!!parse-error (type => 'in noscript:base');
3241                  
3242                    $self->{insertion_mode} = IN_HEAD_IM;
3243                    ## Reprocess in the "in head" insertion mode...
3244                  }
3245    
3246                  ## NOTE: There is a "as if in head" code clone.
3247                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3248                    !!!parse-error (type => 'after head:'.$token->{tag_name});
3249                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3250                  }
3251                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3252                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3253                  pop @{$self->{open_elements}}
3254                      if $self->{insertion_mode} == AFTER_HEAD_IM;
3255                  !!!next-token;
3256                  redo B;
3257                } elsif ($token->{tag_name} eq 'link') {
3258                  ## NOTE: There is a "as if in head" code clone.
3259                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3260                    !!!parse-error (type => 'after head:'.$token->{tag_name});
3261                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3262                  }
3263                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3264                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3265                  pop @{$self->{open_elements}}
3266                      if $self->{insertion_mode} == AFTER_HEAD_IM;
3267                  !!!next-token;
3268                  redo B;
3269                } elsif ($token->{tag_name} eq 'meta') {
3270                  ## NOTE: There is a "as if in head" code clone.
3271                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3272                    !!!parse-error (type => 'after head:'.$token->{tag_name});
3273                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3274                  }
3275                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3276                  my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3277    
3278                  unless ($self->{confident}) {
3279                    if ($token->{attributes}->{charset}) { ## TODO: And if supported
3280                      $self->{change_encoding}
3281                          ->($self, $token->{attributes}->{charset}->{value});
3282                      
3283                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
3284                          ->set_user_data (manakai_has_reference =>
3285                                               $token->{attributes}->{charset}
3286                                                   ->{has_reference});
3287                    } elsif ($token->{attributes}->{content}) {
3288                      ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
3289                      if ($token->{attributes}->{content}->{value}
3290                          =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
3291                              [\x09-\x0D\x20]*=
3292                              [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
3293                              ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
3294                        $self->{change_encoding}
3295                            ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);
3296                        $meta_el->[0]->get_attribute_node_ns (undef, 'content')
3297                            ->set_user_data (manakai_has_reference =>
3298                                                 $token->{attributes}->{content}
3299                                                       ->{has_reference});
3300                      }
3301                    }
3302                  } else {
3303                    if ($token->{attributes}->{charset}) {
3304                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
3305                          ->set_user_data (manakai_has_reference =>
3306                                               $token->{attributes}->{charset}
3307                                                   ->{has_reference});
3308                    }
3309                    if ($token->{attributes}->{content}) {
3310                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
3311                          ->set_user_data (manakai_has_reference =>
3312                                               $token->{attributes}->{content}
3313                                                   ->{has_reference});
3314                    }
3315                  }
3316    
3317                  pop @{$self->{open_elements}}
3318                      if $self->{insertion_mode} == AFTER_HEAD_IM;
3319                  !!!next-token;
3320                  redo B;
3321                } elsif ($token->{tag_name} eq 'title') {
3322                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3323                    ## As if </noscript>
3324                    pop @{$self->{open_elements}};
3325                    !!!parse-error (type => 'in noscript:title');
3326                  
3327                    $self->{insertion_mode} = IN_HEAD_IM;
3328                    ## Reprocess in the "in head" insertion mode...
3329                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3330                    !!!parse-error (type => 'after head:'.$token->{tag_name});
3331                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3332                  }
3333    
3334                  ## NOTE: There is a "as if in head" code clone.
3335                  my $parent = defined $self->{head_element} ? $self->{head_element}
3336                      : $self->{open_elements}->[-1]->[0];
3337                  $parse_rcdata->(RCDATA_CONTENT_MODEL,
3338                                  sub { $parent->append_child ($_[0]) });
3339                  pop @{$self->{open_elements}}
3340                      if $self->{insertion_mode} == AFTER_HEAD_IM;
3341                  redo B;
3342                } elsif ($token->{tag_name} eq 'style') {
3343                  ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
3344                  ## insertion mode IN_HEAD_IM)
3345                  ## NOTE: There is a "as if in head" code clone.
3346                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3347                    !!!parse-error (type => 'after head:'.$token->{tag_name});
3348                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3349                  }
3350                  $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
3351                  pop @{$self->{open_elements}}
3352                      if $self->{insertion_mode} == AFTER_HEAD_IM;
3353                  redo B;
3354                } elsif ($token->{tag_name} eq 'noscript') {
3355                  if ($self->{insertion_mode} == IN_HEAD_IM) {
3356                    ## NOTE: and scripting is disalbed
3357                    !!!insert-element ($token->{tag_name}, $token->{attributes});
3358                    $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
3359                    !!!next-token;
3360                    redo B;
3361                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3362                    !!!parse-error (type => 'in noscript:noscript');
3363                    ## Ignore the token
3364                    !!!next-token;
3365                    redo B;
3366                  } else {
3367                    #
3368                  }
3369                } elsif ($token->{tag_name} eq 'script') {
3370                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3371                    ## As if </noscript>
3372                    pop @{$self->{open_elements}};
3373                    !!!parse-error (type => 'in noscript:script');
3374                  
3375                    $self->{insertion_mode} = IN_HEAD_IM;
3376                    ## Reprocess in the "in head" insertion mode...
3377                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3378                    !!!parse-error (type => 'after head:'.$token->{tag_name});
3379                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3380                  }
3381    
3382                  ## NOTE: There is a "as if in head" code clone.
3383                  $script_start_tag->($insert_to_current);
3384                  pop @{$self->{open_elements}}
3385                      if $self->{insertion_mode} == AFTER_HEAD_IM;
3386                  redo B;
3387                } elsif ($token->{tag_name} eq 'body' or
3388                         $token->{tag_name} eq 'frameset') {
3389                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3390                    ## As if </noscript>
3391                    pop @{$self->{open_elements}};
3392                    !!!parse-error (type => 'in noscript:'.$token->{tag_name});
3393                    
3394                    ## Reprocess in the "in head" insertion mode...
3395                    ## As if </head>
3396                    pop @{$self->{open_elements}};
3397                    
3398                    ## Reprocess in the "after head" insertion mode...
3399                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3400                    pop @{$self->{open_elements}};
3401                    
3402                    ## Reprocess in the "after head" insertion mode...
3403                  }
3404    
3405                  ## "after head" insertion mode
3406                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3407                  if ($token->{tag_name} eq 'body') {
3408                    $self->{insertion_mode} = IN_BODY_IM;
3409                  } elsif ($token->{tag_name} eq 'frameset') {
3410                    $self->{insertion_mode} = IN_FRAMESET_IM;
3411                  } else {
3412                    die "$0: tag name: $self->{tag_name}";
3413                  }
3414                  !!!next-token;
3415                  redo B;
3416                } else {
3417                  #
3418                }
3419    
3420                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3421                  ## As if </noscript>
3422                  pop @{$self->{open_elements}};
3423                  !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
3424                  
3425                  ## Reprocess in the "in head" insertion mode...
3426                  ## As if </head>
3427                  pop @{$self->{open_elements}};
3428    
3429                  ## Reprocess in the "after head" insertion mode...
3430                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3431                  ## As if </head>
3432                  pop @{$self->{open_elements}};
3433    
3434                  ## Reprocess in the "after head" insertion mode...
3435                }
3436    
3437                ## "after head" insertion mode
3438                ## As if <body>
3439                !!!insert-element ('body');
3440                $self->{insertion_mode} = IN_BODY_IM;
3441                ## reprocess
3442                redo B;
3443              } elsif ($token->{type} == END_TAG_TOKEN) {
3444                if ($token->{tag_name} eq 'head') {
3445                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3446                    ## As if <head>
3447                    !!!create-element ($self->{head_element}, 'head');
3448                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3449                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3450    
3451                    ## Reprocess in the "in head" insertion mode...
3452                    pop @{$self->{open_elements}};
3453                    $self->{insertion_mode} = AFTER_HEAD_IM;
3454                    !!!next-token;
3455                    redo B;
3456                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3457                    ## As if </noscript>
3458                    pop @{$self->{open_elements}};
3459                    !!!parse-error (type => 'in noscript:script');
3460                    
3461                    ## Reprocess in the "in head" insertion mode...
3462                    pop @{$self->{open_elements}};
3463                    $self->{insertion_mode} = AFTER_HEAD_IM;
3464                    !!!next-token;
3465                    redo B;
3466                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3467                    pop @{$self->{open_elements}};
3468                    $self->{insertion_mode} = AFTER_HEAD_IM;
3469                    !!!next-token;
3470                    redo B;
3471                  } else {
3472                    #
3473                  }
3474                } elsif ($token->{tag_name} eq 'noscript') {
3475                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3476                    pop @{$self->{open_elements}};
3477                    $self->{insertion_mode} = IN_HEAD_IM;
3478                    !!!next-token;
3479                    redo B;
3480                  } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3481                    !!!parse-error (type => 'unmatched end tag:noscript');
3482                    ## Ignore the token ## ISSUE: An issue in the spec.
3483                    !!!next-token;
3484                    redo B;
3485                  } else {
3486                    #
3487                  }
3488                } elsif ({
3489                          body => 1, html => 1,
3490                         }->{$token->{tag_name}}) {
3491                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3492                    ## As if <head>
3493                    !!!create-element ($self->{head_element}, 'head');
3494                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3495                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3496    
3497                    $self->{insertion_mode} = IN_HEAD_IM;
3498                    ## Reprocess in the "in head" insertion mode...
3499                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3500                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3501                    ## Ignore the token
3502                    !!!next-token;
3503                    redo B;
3504                  }
3505                  
3506                  #
3507                } elsif ({
3508                          p => 1, br => 1,
3509                         }->{$token->{tag_name}}) {
3510                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3511                    ## As if <head>
3512                    !!!create-element ($self->{head_element}, 'head');
3513                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3514                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3515    
3516                    $self->{insertion_mode} = IN_HEAD_IM;
3517                    ## Reprocess in the "in head" insertion mode...
3518                  }
3519    
3520                  #
3521                } else {
3522                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3523                    #
3524                  } else {
3525                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3526                    ## Ignore the token
3527                    !!!next-token;
3528                    redo B;
3529                  }
3530                }
3531    
3532                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3533                  ## As if </noscript>
3534                  pop @{$self->{open_elements}};
3535                  !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
3536                  
3537                  ## Reprocess in the "in head" insertion mode...
3538                  ## As if </head>
3539                  pop @{$self->{open_elements}};
3540    
3541                  ## Reprocess in the "after head" insertion mode...
3542                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3543                  ## As if </head>
3544                  pop @{$self->{open_elements}};
3545    
3546                  ## Reprocess in the "after head" insertion mode...
3547                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3548                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3549                  ## Ignore the token ## ISSUE: An issue in the spec.
3550                  !!!next-token;
3551                  redo B;
3552                }
3553    
3554                ## "after head" insertion mode
3555                ## As if <body>
3556                !!!insert-element ('body');
3557                $self->{insertion_mode} = IN_BODY_IM;
3558                ## reprocess
3559                redo B;
3560              } else {
3561                die "$0: $token->{type}: Unknown token type";
3562              }
3563    
3564              ## ISSUE: An issue in the spec.
3565        } elsif ($self->{insertion_mode} & BODY_IMS) {
3566              if ($token->{type} == CHARACTER_TOKEN) {
3567                ## NOTE: There is a code clone of "character in body".
3568                $reconstruct_active_formatting_elements->($insert_to_current);
3569                
3570                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3571    
3572                !!!next-token;
3573                redo B;
3574              } elsif ($token->{type} == START_TAG_TOKEN) {
3575                if ({
3576                     caption => 1, col => 1, colgroup => 1, tbody => 1,
3577                     td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
3578                    }->{$token->{tag_name}}) {
3579                  if ($self->{insertion_mode} == IN_CELL_IM) {
3580                    ## have an element in table scope
3581                    my $tn;
3582                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3583                      my $node = $self->{open_elements}->[$_];
3584                      if ($node->[1] eq 'td' or $node->[1] eq 'th') {
3585                        $tn = $node->[1];
3586                        last INSCOPE;
3587                      } elsif ({
3588                                table => 1, html => 1,
3589                               }->{$node->[1]}) {
3590                        last INSCOPE;
3591                      }
3592                    } # INSCOPE
3593                      unless (defined $tn) {
3594                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3595                        ## Ignore the token
3596                        !!!next-token;
3597                        redo B;
3598                      }
3599                    
3600                    ## Close the cell
3601                    !!!back-token; # <?>
3602                    $token = {type => END_TAG_TOKEN, tag_name => $tn};
3603                    redo B;
3604                  } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3605                    !!!parse-error (type => 'not closed:caption');
3606                    
3607                    ## As if </caption>
3608                    ## have a table element in table scope
3609                    my $i;
3610                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3611                      my $node = $self->{open_elements}->[$_];
3612                      if ($node->[1] eq 'caption') {
3613                        $i = $_;
3614                        last INSCOPE;
3615                      } elsif ({
3616                                table => 1, html => 1,
3617                               }->{$node->[1]}) {
3618                        last INSCOPE;
3619                      }
3620                    } # INSCOPE
3621                      unless (defined $i) {
3622                        !!!parse-error (type => 'unmatched end tag:caption');
3623                        ## Ignore the token
3624                        !!!next-token;
3625                        redo B;
3626                      }
3627                    
3628                    ## generate implied end tags
3629                    if ({
3630                         dd => 1, dt => 1, li => 1, p => 1,
3631                         td => 1, th => 1, tr => 1,
3632                         tbody => 1, tfoot=> 1, thead => 1,
3633                        }->{$self->{open_elements}->[-1]->[1]}) {
3634                      !!!back-token; # <?>
3635                      $token = {type => END_TAG_TOKEN, tag_name => 'caption'};
3636                      !!!back-token;
3637                      $token = {type => END_TAG_TOKEN,
3638                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3639                      redo B;
3640                    }
3641    
3642                    if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3643                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3644                    }
3645                    
3646                    splice @{$self->{open_elements}}, $i;
3647                    
3648                    $clear_up_to_marker->();
3649                    
3650                    $self->{insertion_mode} = IN_TABLE_IM;
3651                    
3652                    ## reprocess
3653                    redo B;
3654                  } else {
3655                    #
3656                  }
3657                } else {
3658                  #
3659                }
3660              } elsif ($token->{type} == END_TAG_TOKEN) {
3661                if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
3662                  if ($self->{insertion_mode} == IN_CELL_IM) {
3663                    ## have an element in table scope
3664                    my $i;
3665                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3666                      my $node = $self->{open_elements}->[$_];
3667                      if ($node->[1] eq $token->{tag_name}) {
3668                        $i = $_;
3669                        last INSCOPE;
3670                      } elsif ({
3671                                table => 1, html => 1,
3672                               }->{$node->[1]}) {
3673                        last INSCOPE;
3674                      }
3675                    } # INSCOPE
3676                      unless (defined $i) {
3677                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3678                        ## Ignore the token
3679                        !!!next-token;
3680                        redo B;
3681                      }
3682                    
3683                    ## generate implied end tags
3684                    if ({
3685                         dd => 1, dt => 1, li => 1, p => 1,
3686                         td => ($token->{tag_name} eq 'th'),
3687                         th => ($token->{tag_name} eq 'td'),
3688                         tr => 1,
3689                         tbody => 1, tfoot=> 1, thead => 1,
3690                        }->{$self->{open_elements}->[-1]->[1]}) {
3691                      !!!back-token;
3692                      $token = {type => END_TAG_TOKEN,
3693                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3694                      redo B;
3695                    }
3696                    
3697                    if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
3698                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3699                    }
3700                    
3701                    splice @{$self->{open_elements}}, $i;
3702                    
3703                    $clear_up_to_marker->();
3704                    
3705                    $self->{insertion_mode} = IN_ROW_IM;
3706                    
3707                    !!!next-token;
3708                    redo B;
3709                  } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3710                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3711                    ## Ignore the token
3712                    !!!next-token;
3713                    redo B;
3714                  } else {
3715                    #
3716                  }
3717                } elsif ($token->{tag_name} eq 'caption') {
3718                  if ($self->{insertion_mode} == IN_CAPTION_IM) {
3719                    ## have a table element in table scope
3720                    my $i;
3721                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3722                      my $node = $self->{open_elements}->[$_];
3723                      if ($node->[1] eq $token->{tag_name}) {
3724                        $i = $_;
3725                        last INSCOPE;
3726                      } elsif ({
3727                                table => 1, html => 1,
3728                               }->{$node->[1]}) {
3729                        last INSCOPE;
3730                      }
3731                    } # INSCOPE
3732                      unless (defined $i) {
3733                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3734                        ## Ignore the token
3735                        !!!next-token;
3736                        redo B;
3737                      }
3738                    
3739                    ## generate implied end tags
3740                    if ({
3741                         dd => 1, dt => 1, li => 1, p => 1,
3742                         td => 1, th => 1, tr => 1,
3743                         tbody => 1, tfoot=> 1, thead => 1,
3744                        }->{$self->{open_elements}->[-1]->[1]}) {
3745                      !!!back-token;
3746                      $token = {type => END_TAG_TOKEN,
3747                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3748                      redo B;
3749                    }
3750                    
3751                    if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3752                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3753                    }
3754                    
3755                    splice @{$self->{open_elements}}, $i;
3756                    
3757                    $clear_up_to_marker->();
3758                    
3759                    $self->{insertion_mode} = IN_TABLE_IM;
3760                    
3761                    !!!next-token;
3762                    redo B;
3763                  } elsif ($self->{insertion_mode} == IN_CELL_IM) {
3764                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3765                    ## Ignore the token
3766                    !!!next-token;
3767                    redo B;
3768                  } else {
3769                    #
3770                  }
3771                } elsif ({
3772                          table => 1, tbody => 1, tfoot => 1,
3773                          thead => 1, tr => 1,
3774                         }->{$token->{tag_name}} and
3775                         $self->{insertion_mode} == IN_CELL_IM) {
3776                  ## have an element in table scope
3777                  my $i;
3778                  my $tn;
3779                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3780                    my $node = $self->{open_elements}->[$_];
3781                    if ($node->[1] eq $token->{tag_name}) {
3782                      $i = $_;
3783                      last INSCOPE;
3784                    } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {
3785                      $tn = $node->[1];
3786                      ## NOTE: There is exactly one |td| or |th| element
3787                      ## in scope in the stack of open elements by definition.
3788                    } elsif ({
3789                              table => 1, html => 1,
3790                             }->{$node->[1]}) {
3791                      last INSCOPE;
3792                    }
3793                  } # INSCOPE
3794                  unless (defined $i) {
3795                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3796                    ## Ignore the token
3797                    !!!next-token;
3798                    redo B;
3799                  }
3800    
3801                  ## Close the cell
3802                  !!!back-token; # </?>
3803                  $token = {type => END_TAG_TOKEN, tag_name => $tn};
3804                  redo B;
3805                } elsif ($token->{tag_name} eq 'table' and
3806                         $self->{insertion_mode} == IN_CAPTION_IM) {
3807                  !!!parse-error (type => 'not closed:caption');
3808    
3809                  ## As if </caption>
3810                  ## have a table element in table scope
3811                  my $i;
3812                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3813                    my $node = $self->{open_elements}->[$_];
3814                    if ($node->[1] eq 'caption') {
3815                      $i = $_;
3816                      last INSCOPE;
3817                    } elsif ({
3818                              table => 1, html => 1,
3819                             }->{$node->[1]}) {
3820                      last INSCOPE;
3821                    }
3822                  } # INSCOPE
3823                  unless (defined $i) {
3824                    !!!parse-error (type => 'unmatched end tag:caption');
3825                    ## Ignore the token
3826                    !!!next-token;
3827                    redo B;
3828                  }
3829                  
3830                  ## generate implied end tags
3831                  if ({
3832                       dd => 1, dt => 1, li => 1, p => 1,
3833                       td => 1, th => 1, tr => 1,
3834                       tbody => 1, tfoot=> 1, thead => 1,
3835                      }->{$self->{open_elements}->[-1]->[1]}) {
3836                    !!!back-token; # </table>
3837                    $token = {type => END_TAG_TOKEN, tag_name => 'caption'};
3838                    !!!back-token;
3839                    $token = {type => END_TAG_TOKEN,
3840                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3841                    redo B;
3842                  }
3843    
3844                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3845                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3846                  }
3847    
3848                  splice @{$self->{open_elements}}, $i;
3849    
3850                  $clear_up_to_marker->();
3851    
3852                  $self->{insertion_mode} = IN_TABLE_IM;
3853    
3854                  ## reprocess
3855                  redo B;
3856                } elsif ({
3857                          body => 1, col => 1, colgroup => 1, html => 1,
3858                         }->{$token->{tag_name}}) {
3859                  if ($self->{insertion_mode} & BODY_TABLE_IMS) {
3860                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3861                    ## Ignore the token
3862                    !!!next-token;
3863                    redo B;
3864                  } else {
3865                    #
3866                  }
3867                } elsif ({
3868                          tbody => 1, tfoot => 1,
3869                          thead => 1, tr => 1,
3870                         }->{$token->{tag_name}} and
3871                         $self->{insertion_mode} == IN_CAPTION_IM) {
3872                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3873                  ## Ignore the token
3874                  !!!next-token;
3875                  redo B;
3876                } else {
3877                  #
3878                }
3879          } else {
3880            die "$0: $token->{type}: Unknown token type";
3881          }
3882    
3883          $insert = $insert_to_current;
3884          #
3885        } elsif ($self->{insertion_mode} & TABLE_IMS) {
3886          if ($token->{type} == CHARACTER_TOKEN) {
3887                if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3888                  $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3889                  
3890                  unless (length $token->{data}) {
3891                    !!!next-token;
3892                    redo B;
3893                  }
3894                }
3895    
3896                !!!parse-error (type => 'in table:#character');
3897    
3898                ## As if in body, but insert into foster parent element
3899                ## ISSUE: Spec says that "whenever a node would be inserted
3900                ## into the current node" while characters might not be
3901                ## result in a new Text node.
3902                $reconstruct_active_formatting_elements->($insert_to_foster);
3903                
3904                if ({
3905                     table => 1, tbody => 1, tfoot => 1,
3906                     thead => 1, tr => 1,
3907                    }->{$self->{open_elements}->[-1]->[1]}) {
3908                  # MUST
3909                  my $foster_parent_element;
3910                  my $next_sibling;
3911                  my $prev_sibling;
3912                  OE: for (reverse 0..$#{$self->{open_elements}}) {
3913                    if ($self->{open_elements}->[$_]->[1] eq 'table') {
3914                      my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3915                      if (defined $parent and $parent->node_type == 1) {
3916                        $foster_parent_element = $parent;
3917                        $next_sibling = $self->{open_elements}->[$_]->[0];
3918                        $prev_sibling = $next_sibling->previous_sibling;
3919                      } else {
3920                        $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
3921                        $prev_sibling = $foster_parent_element->last_child;
3922                      }
3923                      last OE;
3924                    }
3925                  } # OE
3926                  $foster_parent_element = $self->{open_elements}->[0]->[0] and
3927                  $prev_sibling = $foster_parent_element->last_child
3928                    unless defined $foster_parent_element;
3929                  if (defined $prev_sibling and
3930                      $prev_sibling->node_type == 3) {
3931                    $prev_sibling->manakai_append_text ($token->{data});
3932                  } else {
3933                    $foster_parent_element->insert_before
3934                      ($self->{document}->create_text_node ($token->{data}),
3935                       $next_sibling);
3936                  }
3937                } else {
3938                  $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3939                }
3940                
3941                !!!next-token;
3942                redo B;
3943          } elsif ($token->{type} == START_TAG_TOKEN) {
3944                if ({
3945                     tr => ($self->{insertion_mode} != IN_ROW_IM),
3946                     th => 1, td => 1,
3947                    }->{$token->{tag_name}}) {
3948                  if ($self->{insertion_mode} == IN_TABLE_IM) {
3949                    ## Clear back to table context
3950                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
3951                           $self->{open_elements}->[-1]->[1] ne 'html') {
3952                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3953                      pop @{$self->{open_elements}};
3954                    }
3955                    
3956                    !!!insert-element ('tbody');
3957                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
3958                    ## reprocess in the "in table body" insertion mode...
3959                  }
3960    
3961                  if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3962                    unless ($token->{tag_name} eq 'tr') {
3963                      !!!parse-error (type => 'missing start tag:tr');
3964                    }
3965                    
3966                    ## Clear back to table body context
3967                    while (not {
3968                      tbody => 1, tfoot => 1, thead => 1, html => 1,
3969                    }->{$self->{open_elements}->[-1]->[1]}) {
3970                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3971                      pop @{$self->{open_elements}};
3972                    }
3973                    
3974                    $self->{insertion_mode} = IN_ROW_IM;
3975                    if ($token->{tag_name} eq 'tr') {
3976                      !!!insert-element ($token->{tag_name}, $token->{attributes});
3977                      !!!next-token;
3978                      redo B;
3979                    } else {
3980                      !!!insert-element ('tr');
3981                      ## reprocess in the "in row" insertion mode
3982                    }
3983                  }
3984    
3985                  ## Clear back to table row context
3986                  while (not {
3987                    tr => 1, html => 1,
3988                  }->{$self->{open_elements}->[-1]->[1]}) {
3989                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3990                    pop @{$self->{open_elements}};
3991                  }
3992                  
3993                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3994                  $self->{insertion_mode} = IN_CELL_IM;
3995    
3996                  push @$active_formatting_elements, ['#marker', ''];
3997                  
3998                  !!!next-token;
3999                  redo B;
4000                } elsif ({
4001                          caption => 1, col => 1, colgroup => 1,
4002                          tbody => 1, tfoot => 1, thead => 1,
4003                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
4004                         }->{$token->{tag_name}}) {
4005                  if ($self->{insertion_mode} == IN_ROW_IM) {
4006                    ## As if </tr>
4007                    ## have an element in table scope
4008                    my $i;
4009                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4010                      my $node = $self->{open_elements}->[$_];
4011                      if ($node->[1] eq 'tr') {
4012                        $i = $_;
4013                        last INSCOPE;
4014                      } elsif ({
4015                                table => 1, html => 1,
4016                               }->{$node->[1]}) {
4017                        last INSCOPE;
4018                      }
4019                    } # INSCOPE
4020                    unless (defined $i) {
4021                      !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});
4022                      ## Ignore the token
4023                      !!!next-token;
4024                      redo B;
4025                    }
4026                    
4027                    ## Clear back to table row context
4028                    while (not {
4029                      tr => 1, html => 1,
4030                    }->{$self->{open_elements}->[-1]->[1]}) {
4031                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4032                      pop @{$self->{open_elements}};
4033                    }
4034                    
4035                    pop @{$self->{open_elements}}; # tr
4036                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
4037                    if ($token->{tag_name} eq 'tr') {
4038                      ## reprocess
4039                      redo B;
4040                    } else {
4041                      ## reprocess in the "in table body" insertion mode...
4042                    }
4043                  }
4044    
4045                  if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
4046                    ## have an element in table scope
4047                    my $i;
4048                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4049                      my $node = $self->{open_elements}->[$_];
4050                      if ({
4051                           tbody => 1, thead => 1, tfoot => 1,
4052                          }->{$node->[1]}) {
4053                        $i = $_;
4054                        last INSCOPE;
4055                      } elsif ({
4056                                table => 1, html => 1,
4057                               }->{$node->[1]}) {
4058                        last INSCOPE;
4059                      }
4060                    } # INSCOPE
4061                    unless (defined $i) {
4062                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4063                      ## Ignore the token
4064                      !!!next-token;
4065                      redo B;
4066                    }
4067    
4068                    ## Clear back to table body context
4069                    while (not {
4070                      tbody => 1, tfoot => 1, thead => 1, html => 1,
4071                    }->{$self->{open_elements}->[-1]->[1]}) {
4072                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4073                      pop @{$self->{open_elements}};
4074                    }
4075                    
4076                    ## As if <{current node}>
4077                    ## have an element in table scope
4078                    ## true by definition
4079                    
4080                    ## Clear back to table body context
4081                    ## nop by definition
4082                    
4083                    pop @{$self->{open_elements}};
4084                    $self->{insertion_mode} = IN_TABLE_IM;
4085                    ## reprocess in "in table" insertion mode...
4086                  }
4087    
4088                  if ($token->{tag_name} eq 'col') {
4089                    ## Clear back to table context
4090                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
4091                           $self->{open_elements}->[-1]->[1] ne 'html') {
4092                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4093                      pop @{$self->{open_elements}};
4094                    }
4095                    
4096                    !!!insert-element ('colgroup');
4097                    $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
4098                    ## reprocess
4099                    redo B;
4100                  } elsif ({
4101                            caption => 1,
4102                            colgroup => 1,
4103                            tbody => 1, tfoot => 1, thead => 1,
4104                           }->{$token->{tag_name}}) {
4105                    ## Clear back to table context
4106                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
4107                           $self->{open_elements}->[-1]->[1] ne 'html') {
4108                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4109                      pop @{$self->{open_elements}};
4110                    }
4111                    
4112                    push @$active_formatting_elements, ['#marker', '']
4113                        if $token->{tag_name} eq 'caption';
4114                    
4115                    !!!insert-element ($token->{tag_name}, $token->{attributes});
4116                    $self->{insertion_mode} = {
4117                                               caption => IN_CAPTION_IM,
4118                                               colgroup => IN_COLUMN_GROUP_IM,
4119                                               tbody => IN_TABLE_BODY_IM,
4120                                               tfoot => IN_TABLE_BODY_IM,
4121                                               thead => IN_TABLE_BODY_IM,
4122                                              }->{$token->{tag_name}};
4123                    !!!next-token;
4124                    redo B;
4125                  } else {
4126                    die "$0: in table: <>: $token->{tag_name}";
4127                  }
4128                } elsif ($token->{tag_name} eq 'table') {
4129                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4130    
4131                  ## As if </table>
4132                  ## have a table element in table scope
4133                  my $i;
4134                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4135                    my $node = $self->{open_elements}->[$_];
4136                    if ($node->[1] eq 'table') {
4137                      $i = $_;
4138                      last INSCOPE;
4139                    } elsif ({
4140                              table => 1, html => 1,
4141                             }->{$node->[1]}) {
4142                      last INSCOPE;
4143                    }
4144                  } # INSCOPE
4145                  unless (defined $i) {
4146                    !!!parse-error (type => 'unmatched end tag:table');
4147                    ## Ignore tokens </table><table>
4148                    !!!next-token;
4149                    redo B;
4150                  }
4151                  
4152                  ## generate implied end tags
4153                  if ({
4154                       dd => 1, dt => 1, li => 1, p => 1,
4155                       td => 1, th => 1, tr => 1,
4156                       tbody => 1, tfoot=> 1, thead => 1,
4157                      }->{$self->{open_elements}->[-1]->[1]}) {
4158                    !!!back-token; # <table>
4159                    $token = {type => END_TAG_TOKEN, tag_name => 'table'};
4160                    !!!back-token;
4161                    $token = {type => END_TAG_TOKEN,
4162                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
4163                    redo B;
4164                  }
4165    
4166                  if ($self->{open_elements}->[-1]->[1] ne 'table') {
4167                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4168                  }
4169    
4170                  splice @{$self->{open_elements}}, $i;
4171    
4172                  $self->_reset_insertion_mode;
4173    
4174                  ## reprocess
4175                  redo B;
4176            } else {
4177              !!!parse-error (type => 'in table:'.$token->{tag_name});
4178    
4179              $insert = $insert_to_foster;
4180              #
4181            }
4182          } elsif ($token->{type} == END_TAG_TOKEN) {
4183                if ($token->{tag_name} eq 'tr' and
4184                    $self->{insertion_mode} == IN_ROW_IM) {
4185                  ## have an element in table scope
4186                  my $i;
4187                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4188                    my $node = $self->{open_elements}->[$_];
4189                    if ($node->[1] eq $token->{tag_name}) {
4190                      $i = $_;
4191                      last INSCOPE;
4192                    } elsif ({
4193                              table => 1, html => 1,
4194                             }->{$node->[1]}) {
4195                      last INSCOPE;
4196                    }
4197                  } # INSCOPE
4198                  unless (defined $i) {
4199                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4200                    ## Ignore the token
4201                    !!!next-token;
4202                    redo B;
4203                  }
4204    
4205                  ## Clear back to table row context
4206                  while (not {
4207                    tr => 1, html => 1,
4208                  }->{$self->{open_elements}->[-1]->[1]}) {
4209                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4210                    pop @{$self->{open_elements}};
4211                  }
4212    
4213                  pop @{$self->{open_elements}}; # tr
4214                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
4215                  !!!next-token;
4216                  redo B;
4217                } elsif ($token->{tag_name} eq 'table') {
4218                  if ($self->{insertion_mode} == IN_ROW_IM) {
4219                    ## As if </tr>
4220                    ## have an element in table scope
4221                    my $i;
4222                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4223                      my $node = $self->{open_elements}->[$_];
4224                      if ($node->[1] eq 'tr') {
4225                        $i = $_;
4226                        last INSCOPE;
4227                      } elsif ({
4228                                table => 1, html => 1,
4229                               }->{$node->[1]}) {
4230                        last INSCOPE;
4231                      }
4232                    } # INSCOPE
4233                    unless (defined $i) {
4234                      !!!parse-error (type => 'unmatched end tag:'.$token->{type});
4235                      ## Ignore the token
4236                      !!!next-token;
4237                      redo B;
4238                    }
4239                    
4240                    ## Clear back to table row context
4241                    while (not {
4242                      tr => 1, html => 1,
4243                    }->{$self->{open_elements}->[-1]->[1]}) {
4244                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4245                      pop @{$self->{open_elements}};
4246                    }
4247                    
4248                    pop @{$self->{open_elements}}; # tr
4249                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
4250                    ## reprocess in the "in table body" insertion mode...
4251                  }
4252    
4253                  if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
4254                    ## have an element in table scope
4255                    my $i;
4256                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4257                      my $node = $self->{open_elements}->[$_];
4258                      if ({
4259                           tbody => 1, thead => 1, tfoot => 1,
4260                          }->{$node->[1]}) {
4261                        $i = $_;
4262                        last INSCOPE;
4263                      } elsif ({
4264                                table => 1, html => 1,
4265                               }->{$node->[1]}) {
4266                        last INSCOPE;
4267                      }
4268                    } # INSCOPE
4269                    unless (defined $i) {
4270                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4271                      ## Ignore the token
4272                      !!!next-token;
4273                      redo B;
4274                    }
4275                    
4276                    ## Clear back to table body context
4277                    while (not {
4278                      tbody => 1, tfoot => 1, thead => 1, html => 1,
4279                    }->{$self->{open_elements}->[-1]->[1]}) {
4280                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4281                      pop @{$self->{open_elements}};
4282                    }
4283                    
4284                    ## As if <{current node}>
4285                    ## have an element in table scope
4286                    ## true by definition
4287                    
4288                    ## Clear back to table body context
4289                    ## nop by definition
4290                    
4291                    pop @{$self->{open_elements}};
4292                    $self->{insertion_mode} = IN_TABLE_IM;
4293                    ## reprocess in the "in table" insertion mode...
4294                  }
4295    
4296                  ## have a table element in table scope
4297                  my $i;
4298                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4299                    my $node = $self->{open_elements}->[$_];
4300                    if ($node->[1] eq $token->{tag_name}) {
4301                      $i = $_;
4302                      last INSCOPE;
4303                    } elsif ({
4304                              table => 1, html => 1,
4305                             }->{$node->[1]}) {
4306                      last INSCOPE;
4307                    }
4308                  } # INSCOPE
4309                  unless (defined $i) {
4310                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4311                    ## Ignore the token
4312                    !!!next-token;
4313                    redo B;
4314                  }
4315    
4316                  ## generate implied end tags
4317                  if ({
4318                       dd => 1, dt => 1, li => 1, p => 1,
4319                       td => 1, th => 1, tr => 1,
4320                       tbody => 1, tfoot=> 1, thead => 1,
4321                      }->{$self->{open_elements}->[-1]->[1]}) {
4322                    !!!back-token;
4323                    $token = {type => END_TAG_TOKEN,
4324                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
4325                    redo B;
4326                  }
4327                  
4328                  if ($self->{open_elements}->[-1]->[1] ne 'table') {
4329                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4330                  }
4331                    
4332                  splice @{$self->{open_elements}}, $i;
4333                  
4334                  $self->_reset_insertion_mode;
4335                  
4336                  !!!next-token;
4337                  redo B;
4338                } elsif ({
4339                          tbody => 1, tfoot => 1, thead => 1,
4340                         }->{$token->{tag_name}} and
4341                         $self->{insertion_mode} & ROW_IMS) {
4342                  if ($self->{insertion_mode} == IN_ROW_IM) {
4343                    ## have an element in table scope
4344                    my $i;
4345                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4346                      my $node = $self->{open_elements}->[$_];
4347                      if ($node->[1] eq $token->{tag_name}) {
4348                        $i = $_;
4349                        last INSCOPE;
4350                      } elsif ({
4351                                table => 1, html => 1,
4352                               }->{$node->[1]}) {
4353                        last INSCOPE;
4354                      }
4355                    } # INSCOPE
4356                      unless (defined $i) {
4357                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4358                        ## Ignore the token
4359                        !!!next-token;
4360                        redo B;
4361                      }
4362                    
4363                    ## As if </tr>
4364                    ## have an element in table scope
4365                    my $i;
4366                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4367                      my $node = $self->{open_elements}->[$_];
4368                      if ($node->[1] eq 'tr') {
4369                        $i = $_;
4370                        last INSCOPE;
4371                      } elsif ({
4372                                table => 1, html => 1,
4373                               }->{$node->[1]}) {
4374                        last INSCOPE;
4375                      }
4376                    } # INSCOPE
4377                      unless (defined $i) {
4378                        !!!parse-error (type => 'unmatched end tag:tr');
4379                        ## Ignore the token
4380                        !!!next-token;
4381                        redo B;
4382                      }
4383                    
4384                    ## Clear back to table row context
4385                    while (not {
4386                      tr => 1, html => 1,
4387                    }->{$self->{open_elements}->[-1]->[1]}) {
4388                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4389                      pop @{$self->{open_elements}};
4390                    }
4391                    
4392                    pop @{$self->{open_elements}}; # tr
4393                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
4394                    ## reprocess in the "in table body" insertion mode...
4395                  }
4396    
4397                  ## have an element in table scope
4398                  my $i;
4399                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4400                    my $node = $self->{open_elements}->[$_];
4401                    if ($node->[1] eq $token->{tag_name}) {
4402                      $i = $_;
4403                      last INSCOPE;
4404                    } elsif ({
4405                              table => 1, html => 1,
4406                             }->{$node->[1]}) {
4407                      last INSCOPE;
4408                    }
4409                  } # INSCOPE
4410                  unless (defined $i) {
4411                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4412                    ## Ignore the token
4413                    !!!next-token;
4414                    redo B;
4415                  }
4416    
4417                  ## Clear back to table body context
4418                  while (not {
4419                    tbody => 1, tfoot => 1, thead => 1, html => 1,
4420                  }->{$self->{open_elements}->[-1]->[1]}) {
4421                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4422                    pop @{$self->{open_elements}};
4423                  }
4424    
4425                  pop @{$self->{open_elements}};
4426                  $self->{insertion_mode} = IN_TABLE_IM;
4427                  !!!next-token;
4428                  redo B;
4429                } elsif ({
4430                          body => 1, caption => 1, col => 1, colgroup => 1,
4431                          html => 1, td => 1, th => 1,
4432                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
4433                          tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
4434                         }->{$token->{tag_name}}) {
4435                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4436                  ## Ignore the token
4437                  !!!next-token;
4438                  redo B;
4439            } else {
4440              !!!parse-error (type => 'in table:/'.$token->{tag_name});
4441    
4442              $insert = $insert_to_foster;
4443              #
4444            }
4445          } else {
4446            die "$0: $token->{type}: Unknown token type";
4447          }
4448        } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
4449              if ($token->{type} == CHARACTER_TOKEN) {
4450                if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4451                  $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4452                  unless (length $token->{data}) {
4453                    !!!next-token;
4454                    redo B;
4455                  }
4456                }
4457                
4458                #
4459              } elsif ($token->{type} == START_TAG_TOKEN) {
4460                if ($token->{tag_name} eq 'col') {
4461                  !!!insert-element ($token->{tag_name}, $token->{attributes});
4462                  pop @{$self->{open_elements}};
4463                  !!!next-token;
4464                  redo B;
4465                } else {
4466                  #
4467                }
4468              } elsif ($token->{type} == END_TAG_TOKEN) {
4469                if ($token->{tag_name} eq 'colgroup') {
4470                  if ($self->{open_elements}->[-1]->[1] eq 'html') {
4471                    !!!parse-error (type => 'unmatched end tag:colgroup');
4472                    ## Ignore the token
4473                    !!!next-token;
4474                    redo B;
4475                  } else {
4476                    pop @{$self->{open_elements}}; # colgroup
4477                    $self->{insertion_mode} = IN_TABLE_IM;
4478                    !!!next-token;
4479                    redo B;            
4480                  }
4481                } elsif ($token->{tag_name} eq 'col') {
4482                  !!!parse-error (type => 'unmatched end tag:col');
4483                  ## Ignore the token
4484                  !!!next-token;
4485                  redo B;
4486                } else {
4487                  #
4488                }
4489              } else {
4490                #
4491              }
4492    
4493              ## As if </colgroup>
4494              if ($self->{open_elements}->[-1]->[1] eq 'html') {
4495                !!!parse-error (type => 'unmatched end tag:colgroup');
4496                ## Ignore the token
4497                !!!next-token;
4498                redo B;
4499              } else {
4500                pop @{$self->{open_elements}}; # colgroup
4501                $self->{insertion_mode} = IN_TABLE_IM;
4502                ## reprocess
4503                redo B;
4504              }
4505        } elsif ($self->{insertion_mode} == IN_SELECT_IM) {
4506          if ($token->{type} == CHARACTER_TOKEN) {
4507            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4508          !!!next-token;          !!!next-token;
4509          return;          redo B;
4510        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{type} == START_TAG_TOKEN) {
4511          !!!parse-error (type => 'in body:title');              if ($token->{tag_name} eq 'option') {
4512          ## NOTE: There is an "as if in head" code clone                if ($self->{open_elements}->[-1]->[1] eq 'option') {
4513          my $title_el;                  ## As if </option>
4514          !!!create-element ($title_el, 'title', $token->{attributes});                  pop @{$self->{open_elements}};
4515          (defined $self->{head_element} ? $self->{head_element} : $self->{open_elements}->[-1]->[0])                }
4516            ->append_child ($title_el);  
4517          $self->{content_model_flag} = 'RCDATA';                !!!insert-element ($token->{tag_name}, $token->{attributes});
4518          delete $self->{escape}; # MUST                !!!next-token;
4519                          redo B;
4520          my $text = '';              } elsif ($token->{tag_name} eq 'optgroup') {
4521          !!!next-token;                if ($self->{open_elements}->[-1]->[1] eq 'option') {
4522          while ($token->{type} eq 'character') {                  ## As if </option>
4523            $text .= $token->{data};                  pop @{$self->{open_elements}};
4524                  }
4525    
4526                  if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {
4527                    ## As if </optgroup>
4528                    pop @{$self->{open_elements}};
4529                  }
4530    
4531                  !!!insert-element ($token->{tag_name}, $token->{attributes});
4532                  !!!next-token;
4533                  redo B;
4534                } elsif ($token->{tag_name} eq 'select') {
4535                  !!!parse-error (type => 'not closed:select');
4536                  ## As if </select> instead
4537                  ## have an element in table scope
4538                  my $i;
4539                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4540                    my $node = $self->{open_elements}->[$_];
4541                    if ($node->[1] eq $token->{tag_name}) {
4542                      $i = $_;
4543                      last INSCOPE;
4544                    } elsif ({
4545                              table => 1, html => 1,
4546                             }->{$node->[1]}) {
4547                      last INSCOPE;
4548                    }
4549                  } # INSCOPE
4550                  unless (defined $i) {
4551                    !!!parse-error (type => 'unmatched end tag:select');
4552                    ## Ignore the token
4553                    !!!next-token;
4554                    redo B;
4555                  }
4556                  
4557                  splice @{$self->{open_elements}}, $i;
4558    
4559                  $self->_reset_insertion_mode;
4560    
4561                  !!!next-token;
4562                  redo B;
4563            } else {
4564              !!!parse-error (type => 'in select:'.$token->{tag_name});
4565              ## Ignore the token
4566            !!!next-token;            !!!next-token;
4567              redo B;
4568          }          }
4569          if (length $text) {        } elsif ($token->{type} == END_TAG_TOKEN) {
4570            $title_el->manakai_append_text ($text);              if ($token->{tag_name} eq 'optgroup') {
4571                  if ($self->{open_elements}->[-1]->[1] eq 'option' and
4572                      $self->{open_elements}->[-2]->[1] eq 'optgroup') {
4573                    ## As if </option>
4574                    splice @{$self->{open_elements}}, -2;
4575                  } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {
4576                    pop @{$self->{open_elements}};
4577                  } else {
4578                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4579                    ## Ignore the token
4580                  }
4581                  !!!next-token;
4582                  redo B;
4583                } elsif ($token->{tag_name} eq 'option') {
4584                  if ($self->{open_elements}->[-1]->[1] eq 'option') {
4585                    pop @{$self->{open_elements}};
4586                  } else {
4587                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4588                    ## Ignore the token
4589                  }
4590                  !!!next-token;
4591                  redo B;
4592                } elsif ($token->{tag_name} eq 'select') {
4593                  ## have an element in table scope
4594                  my $i;
4595                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4596                    my $node = $self->{open_elements}->[$_];
4597                    if ($node->[1] eq $token->{tag_name}) {
4598                      $i = $_;
4599                      last INSCOPE;
4600                    } elsif ({
4601                              table => 1, html => 1,
4602                             }->{$node->[1]}) {
4603                      last INSCOPE;
4604                    }
4605                  } # INSCOPE
4606                  unless (defined $i) {
4607                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4608                    ## Ignore the token
4609                    !!!next-token;
4610                    redo B;
4611                  }
4612                  
4613                  splice @{$self->{open_elements}}, $i;
4614    
4615                  $self->_reset_insertion_mode;
4616    
4617                  !!!next-token;
4618                  redo B;
4619                } elsif ({
4620                          caption => 1, table => 1, tbody => 1,
4621                          tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
4622                         }->{$token->{tag_name}}) {
4623                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4624                  
4625                  ## have an element in table scope
4626                  my $i;
4627                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4628                    my $node = $self->{open_elements}->[$_];
4629                    if ($node->[1] eq $token->{tag_name}) {
4630                      $i = $_;
4631                      last INSCOPE;
4632                    } elsif ({
4633                              table => 1, html => 1,
4634                             }->{$node->[1]}) {
4635                      last INSCOPE;
4636                    }
4637                  } # INSCOPE
4638                  unless (defined $i) {
4639                    ## Ignore the token
4640                    !!!next-token;
4641                    redo B;
4642                  }
4643                  
4644                  ## As if </select>
4645                  ## have an element in table scope
4646                  undef $i;
4647                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4648                    my $node = $self->{open_elements}->[$_];
4649                    if ($node->[1] eq 'select') {
4650                      $i = $_;
4651                      last INSCOPE;
4652                    } elsif ({
4653                              table => 1, html => 1,
4654                             }->{$node->[1]}) {
4655                      last INSCOPE;
4656                    }
4657                  } # INSCOPE
4658                  unless (defined $i) {
4659                    !!!parse-error (type => 'unmatched end tag:select');
4660                    ## Ignore the </select> token
4661                    !!!next-token; ## TODO: ok?
4662                    redo B;
4663                  }
4664                  
4665                  splice @{$self->{open_elements}}, $i;
4666    
4667                  $self->_reset_insertion_mode;
4668    
4669                  ## reprocess
4670                  redo B;
4671            } else {
4672              !!!parse-error (type => 'in select:/'.$token->{tag_name});
4673              ## Ignore the token
4674              !!!next-token;
4675              redo B;
4676            }
4677          } else {
4678            die "$0: $token->{type}: Unknown token type";
4679          }
4680        } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
4681          if ($token->{type} == CHARACTER_TOKEN) {
4682            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4683              my $data = $1;
4684              ## As if in body
4685              $reconstruct_active_formatting_elements->($insert_to_current);
4686                  
4687              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4688              
4689              unless (length $token->{data}) {
4690                !!!next-token;
4691                redo B;
4692              }
4693            }
4694            
4695            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4696              !!!parse-error (type => 'after html:#character');
4697    
4698              ## Reprocess in the "main" phase, "after body" insertion mode...
4699          }          }
4700                    
4701          $self->{content_model_flag} = 'PCDATA';          ## "after body" insertion mode
4702            !!!parse-error (type => 'after body:#character');
4703    
4704            $self->{insertion_mode} = IN_BODY_IM;
4705            ## reprocess
4706            redo B;
4707          } elsif ($token->{type} == START_TAG_TOKEN) {
4708            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4709              !!!parse-error (type => 'after html:'.$token->{tag_name});
4710              
4711              ## Reprocess in the "main" phase, "after body" insertion mode...
4712            }
4713    
4714            ## "after body" insertion mode
4715            !!!parse-error (type => 'after body:'.$token->{tag_name});
4716    
4717            $self->{insertion_mode} = IN_BODY_IM;
4718            ## reprocess
4719            redo B;
4720          } elsif ($token->{type} == END_TAG_TOKEN) {
4721            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4722              !!!parse-error (type => 'after html:/'.$token->{tag_name});
4723              
4724              $self->{insertion_mode} = AFTER_BODY_IM;
4725              ## Reprocess in the "main" phase, "after body" insertion mode...
4726            }
4727    
4728            ## "after body" insertion mode
4729            if ($token->{tag_name} eq 'html') {
4730              if (defined $self->{inner_html_node}) {
4731                !!!parse-error (type => 'unmatched end tag:html');
4732                ## Ignore the token
4733                !!!next-token;
4734                redo B;
4735              } else {
4736                $self->{insertion_mode} = AFTER_HTML_BODY_IM;
4737                !!!next-token;
4738                redo B;
4739              }
4740            } else {
4741              !!!parse-error (type => 'after body:/'.$token->{tag_name});
4742    
4743              $self->{insertion_mode} = IN_BODY_IM;
4744              ## reprocess
4745              redo B;
4746            }
4747          } else {
4748            die "$0: $token->{type}: Unknown token type";
4749          }
4750        } elsif ($self->{insertion_mode} & FRAME_IMS) {
4751          if ($token->{type} == CHARACTER_TOKEN) {
4752            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4753              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4754              
4755              unless (length $token->{data}) {
4756                !!!next-token;
4757                redo B;
4758              }
4759            }
4760            
4761            if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
4762              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4763                !!!parse-error (type => 'in frameset:#character');
4764              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
4765                !!!parse-error (type => 'after frameset:#character');
4766              } else { # "after html frameset"
4767                !!!parse-error (type => 'after html:#character');
4768    
4769                $self->{insertion_mode} = AFTER_FRAMESET_IM;
4770                ## Reprocess in the "main" phase, "after frameset"...
4771                !!!parse-error (type => 'after frameset:#character');
4772              }
4773              
4774              ## Ignore the token.
4775              if (length $token->{data}) {
4776                ## reprocess the rest of characters
4777              } else {
4778                !!!next-token;
4779              }
4780              redo B;
4781            }
4782                    
4783          if ($token->{type} eq 'end tag' and          die qq[$0: Character "$token->{data}"];
4784              $token->{tag_name} eq 'title') {        } elsif ($token->{type} == START_TAG_TOKEN) {
4785            if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4786              !!!parse-error (type => 'after html:'.$token->{tag_name});
4787    
4788              $self->{insertion_mode} = AFTER_FRAMESET_IM;
4789              ## Process in the "main" phase, "after frameset" insertion mode...
4790            }
4791    
4792            if ($token->{tag_name} eq 'frameset' and
4793                $self->{insertion_mode} == IN_FRAMESET_IM) {
4794              !!!insert-element ($token->{tag_name}, $token->{attributes});
4795              !!!next-token;
4796              redo B;
4797            } elsif ($token->{tag_name} eq 'frame' and
4798                     $self->{insertion_mode} == IN_FRAMESET_IM) {
4799              !!!insert-element ($token->{tag_name}, $token->{attributes});
4800              pop @{$self->{open_elements}};
4801              !!!next-token;
4802              redo B;
4803            } elsif ($token->{tag_name} eq 'noframes') {
4804              ## NOTE: As if in body.
4805              $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
4806              redo B;
4807            } else {
4808              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4809                !!!parse-error (type => 'in frameset:'.$token->{tag_name});
4810              } else {
4811                !!!parse-error (type => 'after frameset:'.$token->{tag_name});
4812              }
4813            ## Ignore the token            ## Ignore the token
4814              !!!next-token;
4815              redo B;
4816            }
4817          } elsif ($token->{type} == END_TAG_TOKEN) {
4818            if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4819              !!!parse-error (type => 'after html:/'.$token->{tag_name});
4820    
4821              $self->{insertion_mode} = AFTER_FRAMESET_IM;
4822              ## Process in the "main" phase, "after frameset" insertion mode...
4823            }
4824    
4825            if ($token->{tag_name} eq 'frameset' and
4826                $self->{insertion_mode} == IN_FRAMESET_IM) {
4827              if ($self->{open_elements}->[-1]->[1] eq 'html' and
4828                  @{$self->{open_elements}} == 1) {
4829                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4830                ## Ignore the token
4831                !!!next-token;
4832              } else {
4833                pop @{$self->{open_elements}};
4834                !!!next-token;
4835              }
4836    
4837              if (not defined $self->{inner_html_node} and
4838                  $self->{open_elements}->[-1]->[1] ne 'frameset') {
4839                $self->{insertion_mode} = AFTER_FRAMESET_IM;
4840              }
4841              redo B;
4842            } elsif ($token->{tag_name} eq 'html' and
4843                     $self->{insertion_mode} == AFTER_FRAMESET_IM) {
4844              $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
4845              !!!next-token;
4846              redo B;
4847          } else {          } else {
4848            !!!parse-error (type => 'in RCDATA:#'.$token->{type});            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4849            ## ISSUE: And ignore?              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});
4850              } else {
4851                !!!parse-error (type => 'after frameset:/'.$token->{tag_name});
4852              }
4853              ## Ignore the token
4854              !!!next-token;
4855              redo B;
4856          }          }
4857          } else {
4858            die "$0: $token->{type}: Unknown token type";
4859          }
4860    
4861          ## ISSUE: An issue in spec here
4862        } else {
4863          die "$0: $self->{insertion_mode}: Unknown insertion mode";
4864        }
4865    
4866        ## "in body" insertion mode
4867        if ($token->{type} == START_TAG_TOKEN) {
4868          if ($token->{tag_name} eq 'script') {
4869            ## NOTE: This is an "as if in head" code clone
4870            $script_start_tag->($insert);
4871            redo B;
4872          } elsif ($token->{tag_name} eq 'style') {
4873            ## NOTE: This is an "as if in head" code clone
4874            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4875            redo B;
4876          } elsif ({
4877                    base => 1, link => 1,
4878                   }->{$token->{tag_name}}) {
4879            ## NOTE: This is an "as if in head" code clone, only "-t" differs
4880            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4881            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4882          !!!next-token;          !!!next-token;
4883          return;          redo B;
4884          } elsif ($token->{tag_name} eq 'meta') {
4885            ## NOTE: This is an "as if in head" code clone, only "-t" differs
4886            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4887            my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4888    
4889            unless ($self->{confident}) {
4890              if ($token->{attributes}->{charset}) { ## TODO: And if supported
4891                $self->{change_encoding}
4892                    ->($self, $token->{attributes}->{charset}->{value});
4893                
4894                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4895                    ->set_user_data (manakai_has_reference =>
4896                                         $token->{attributes}->{charset}
4897                                             ->{has_reference});
4898              } elsif ($token->{attributes}->{content}) {
4899                ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
4900                if ($token->{attributes}->{content}->{value}
4901                    =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4902                        [\x09-\x0D\x20]*=
4903                        [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4904                        ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
4905                  $self->{change_encoding}
4906                      ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);
4907                  $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4908                      ->set_user_data (manakai_has_reference =>
4909                                           $token->{attributes}->{content}
4910                                                 ->{has_reference});
4911                }
4912              }
4913            } else {
4914              if ($token->{attributes}->{charset}) {
4915                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4916                    ->set_user_data (manakai_has_reference =>
4917                                         $token->{attributes}->{charset}
4918                                             ->{has_reference});
4919              }
4920              if ($token->{attributes}->{content}) {
4921                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4922                    ->set_user_data (manakai_has_reference =>
4923                                         $token->{attributes}->{content}
4924                                             ->{has_reference});
4925              }
4926            }
4927    
4928            !!!next-token;
4929            redo B;
4930          } elsif ($token->{tag_name} eq 'title') {
4931            !!!parse-error (type => 'in body:title');
4932            ## NOTE: This is an "as if in head" code clone
4933            $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {
4934              if (defined $self->{head_element}) {
4935                $self->{head_element}->append_child ($_[0]);
4936              } else {
4937                $insert->($_[0]);
4938              }
4939            });
4940            redo B;
4941        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
4942          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body:body');
4943                                
# Line 2494  sub _tree_construction_main ($) { Line 4955  sub _tree_construction_main ($) {
4955            }            }
4956          }          }
4957          !!!next-token;          !!!next-token;
4958          return;          redo B;
4959        } elsif ({        } elsif ({
4960                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
4961                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1, listing => 1,
# Line 2505  sub _tree_construction_main ($) { Line 4966  sub _tree_construction_main ($) {
4966          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4967            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4968              !!!back-token;              !!!back-token;
4969              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4970              return;              redo B;
4971            } elsif ({            } elsif ({
4972                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4973                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2518  sub _tree_construction_main ($) { Line 4979  sub _tree_construction_main ($) {
4979          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4980          if ($token->{tag_name} eq 'pre') {          if ($token->{tag_name} eq 'pre') {
4981            !!!next-token;            !!!next-token;
4982            if ($token->{type} eq 'character') {            if ($token->{type} == CHARACTER_TOKEN) {
4983              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
4984              unless (length $token->{data}) {              unless (length $token->{data}) {
4985                !!!next-token;                !!!next-token;
# Line 2527  sub _tree_construction_main ($) { Line 4988  sub _tree_construction_main ($) {
4988          } else {          } else {
4989            !!!next-token;            !!!next-token;
4990          }          }
4991          return;          redo B;
4992        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
4993          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
4994            !!!parse-error (type => 'in form:form');            !!!parse-error (type => 'in form:form');
4995            ## Ignore the token            ## Ignore the token
4996            !!!next-token;            !!!next-token;
4997            return;            redo B;
4998          } else {          } else {
4999            ## has a p element in scope            ## has a p element in scope
5000            INSCOPE: for (reverse @{$self->{open_elements}}) {            INSCOPE: for (reverse @{$self->{open_elements}}) {
5001              if ($_->[1] eq 'p') {              if ($_->[1] eq 'p') {
5002                !!!back-token;                !!!back-token;
5003                $token = {type => 'end tag', tag_name => 'p'};                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5004                return;                redo B;
5005              } elsif ({              } elsif ({
5006                        table => 1, caption => 1, td => 1, th => 1,                        table => 1, caption => 1, td => 1, th => 1,
5007                        button => 1, marquee => 1, object => 1, html => 1,                        button => 1, marquee => 1, object => 1, html => 1,
# Line 2552  sub _tree_construction_main ($) { Line 5013  sub _tree_construction_main ($) {
5013            !!!insert-element-t ($token->{tag_name}, $token->{attributes});            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5014            $self->{form_element} = $self->{open_elements}->[-1]->[0];            $self->{form_element} = $self->{open_elements}->[-1]->[0];
5015            !!!next-token;            !!!next-token;
5016            return;            redo B;
5017          }          }
5018        } elsif ($token->{tag_name} eq 'li') {        } elsif ($token->{tag_name} eq 'li') {
5019          ## has a p element in scope          ## has a p element in scope
5020          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5021            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
5022              !!!back-token;              !!!back-token;
5023              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5024              return;              redo B;
5025            } elsif ({            } elsif ({
5026                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
5027                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2578  sub _tree_construction_main ($) { Line 5039  sub _tree_construction_main ($) {
5039              if ($i != -1) {              if ($i != -1) {
5040                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'end tag missing:'.
5041                                $self->{open_elements}->[-1]->[1]);                                $self->{open_elements}->[-1]->[1]);
               ## TODO: test  
5042              }              }
5043              splice @{$self->{open_elements}}, $i;              splice @{$self->{open_elements}}, $i;
5044              last LI;              last LI;
# Line 2601  sub _tree_construction_main ($) { Line 5061  sub _tree_construction_main ($) {
5061                        
5062          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5063          !!!next-token;          !!!next-token;
5064          return;          redo B;
5065        } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {        } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {
5066          ## has a p element in scope          ## has a p element in scope
5067          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5068            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
5069              !!!back-token;              !!!back-token;
5070              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5071              return;              redo B;
5072            } elsif ({            } elsif ({
5073                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
5074                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2626  sub _tree_construction_main ($) { Line 5086  sub _tree_construction_main ($) {
5086              if ($i != -1) {              if ($i != -1) {
5087                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'end tag missing:'.
5088                                $self->{open_elements}->[-1]->[1]);                                $self->{open_elements}->[-1]->[1]);
               ## TODO: test  
5089              }              }
5090              splice @{$self->{open_elements}}, $i;              splice @{$self->{open_elements}}, $i;
5091              last LI;              last LI;
# Line 2649  sub _tree_construction_main ($) { Line 5108  sub _tree_construction_main ($) {
5108                        
5109          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5110          !!!next-token;          !!!next-token;
5111          return;          redo B;
5112        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
5113          ## has a p element in scope          ## has a p element in scope
5114          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5115            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
5116              !!!back-token;              !!!back-token;
5117              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5118              return;              redo B;
5119            } elsif ({            } elsif ({
5120                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
5121                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2667  sub _tree_construction_main ($) { Line 5126  sub _tree_construction_main ($) {
5126                        
5127          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5128                        
5129          $self->{content_model_flag} = 'PLAINTEXT';          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
5130                        
5131          !!!next-token;          !!!next-token;
5132          return;          redo B;
5133        } elsif ({        } elsif ({
5134                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5135                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 2679  sub _tree_construction_main ($) { Line 5138  sub _tree_construction_main ($) {
5138            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
5139            if ($node->[1] eq 'p') {            if ($node->[1] eq 'p') {
5140              !!!back-token;              !!!back-token;
5141              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5142              return;              redo B;
5143            } elsif ({            } elsif ({
5144                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
5145                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2715  sub _tree_construction_main ($) { Line 5174  sub _tree_construction_main ($) {
5174          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5175                        
5176          !!!next-token;          !!!next-token;
5177          return;          redo B;
5178        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
5179          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
5180            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
# Line 2723  sub _tree_construction_main ($) { Line 5182  sub _tree_construction_main ($) {
5182              !!!parse-error (type => 'in a:a');              !!!parse-error (type => 'in a:a');
5183                            
5184              !!!back-token;              !!!back-token;
5185              $token = {type => 'end tag', tag_name => 'a'};              $token = {type => END_TAG_TOKEN, tag_name => 'a'};
5186              $formatting_end_tag->($token->{tag_name});              $formatting_end_tag->($token->{tag_name});
5187                            
5188              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
# Line 2750  sub _tree_construction_main ($) { Line 5209  sub _tree_construction_main ($) {
5209          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
5210    
5211          !!!next-token;          !!!next-token;
5212          return;          redo B;
5213        } elsif ({        } elsif ({
5214                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
5215                  s => 1, small => 1, strile => 1,                  s => 1, small => 1, strile => 1,
# Line 2762  sub _tree_construction_main ($) { Line 5221  sub _tree_construction_main ($) {
5221          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
5222                    
5223          !!!next-token;          !!!next-token;
5224          return;          redo B;
5225        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
5226          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5227    
# Line 2770  sub _tree_construction_main ($) { Line 5229  sub _tree_construction_main ($) {
5229          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5230            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
5231            if ($node->[1] eq 'nobr') {            if ($node->[1] eq 'nobr') {
5232                !!!parse-error (type => 'in nobr:nobr');
5233              !!!back-token;              !!!back-token;
5234              $token = {type => 'end tag', tag_name => 'nobr'};              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};
5235              return;              redo B;
5236            } elsif ({            } elsif ({
5237                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
5238                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2785  sub _tree_construction_main ($) { Line 5245  sub _tree_construction_main ($) {
5245          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
5246                    
5247          !!!next-token;          !!!next-token;
5248          return;          redo B;
5249        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
5250          ## has a button element in scope          ## has a button element in scope
5251          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 2793  sub _tree_construction_main ($) { Line 5253  sub _tree_construction_main ($) {
5253            if ($node->[1] eq 'button') {            if ($node->[1] eq 'button') {
5254              !!!parse-error (type => 'in button:button');              !!!parse-error (type => 'in button:button');
5255              !!!back-token;              !!!back-token;
5256              $token = {type => 'end tag', tag_name => 'button'};              $token = {type => END_TAG_TOKEN, tag_name => 'button'};
5257              return;              redo B;
5258            } elsif ({            } elsif ({
5259                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
5260                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2809  sub _tree_construction_main ($) { Line 5269  sub _tree_construction_main ($) {
5269          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
5270    
5271          !!!next-token;          !!!next-token;
5272          return;          redo B;
5273        } elsif ($token->{tag_name} eq 'marquee' or        } elsif ($token->{tag_name} eq 'marquee' or
5274                 $token->{tag_name} eq 'object') {                 $token->{tag_name} eq 'object') {
5275          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
# Line 2818  sub _tree_construction_main ($) { Line 5278  sub _tree_construction_main ($) {
5278          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
5279                    
5280          !!!next-token;          !!!next-token;
5281          return;          redo B;
5282        } elsif ($token->{tag_name} eq 'xmp') {        } elsif ($token->{tag_name} eq 'xmp') {
5283          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5284                    $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
5285          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          redo B;
           
         $self->{content_model_flag} = 'CDATA';  
         delete $self->{escape}; # MUST  
           
         !!!next-token;  
         return;  
5286        } elsif ($token->{tag_name} eq 'table') {        } elsif ($token->{tag_name} eq 'table') {
5287          ## has a p element in scope          ## has a p element in scope
5288          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5289            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
5290              !!!back-token;              !!!back-token;
5291              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5292              return;              redo B;
5293            } elsif ({            } elsif ({
5294                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
5295                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2846  sub _tree_construction_main ($) { Line 5300  sub _tree_construction_main ($) {
5300                        
5301          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5302                        
5303          $self->{insertion_mode} = 'in table';          $self->{insertion_mode} = IN_TABLE_IM;
5304                        
5305          !!!next-token;          !!!next-token;
5306          return;          redo B;
5307        } elsif ({        } elsif ({
5308                  area => 1, basefont => 1, bgsound => 1, br => 1,                  area => 1, basefont => 1, bgsound => 1, br => 1,
5309                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
# Line 2859  sub _tree_construction_main ($) { Line 5313  sub _tree_construction_main ($) {
5313            !!!parse-error (type => 'image');            !!!parse-error (type => 'image');
5314            $token->{tag_name} = 'img';            $token->{tag_name} = 'img';
5315          }          }
5316            
5317            ## NOTE: There is an "as if <br>" code clone.
5318          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5319                    
5320          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5321          pop @{$self->{open_elements}};          pop @{$self->{open_elements}};
5322                    
5323          !!!next-token;          !!!next-token;
5324          return;          redo B;
5325        } elsif ($token->{tag_name} eq 'hr') {        } elsif ($token->{tag_name} eq 'hr') {
5326          ## has a p element in scope          ## has a p element in scope
5327          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5328            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
5329              !!!back-token;              !!!back-token;
5330              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5331              return;              redo B;
5332            } elsif ({            } elsif ({
5333                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
5334                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2886  sub _tree_construction_main ($) { Line 5341  sub _tree_construction_main ($) {
5341          pop @{$self->{open_elements}};          pop @{$self->{open_elements}};
5342                        
5343          !!!next-token;          !!!next-token;
5344          return;          redo B;
5345        } elsif ($token->{tag_name} eq 'input') {        } elsif ($token->{tag_name} eq 'input') {
5346          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5347                    
# Line 2895  sub _tree_construction_main ($) { Line 5350  sub _tree_construction_main ($) {
5350          pop @{$self->{open_elements}};          pop @{$self->{open_elements}};
5351                    
5352          !!!next-token;          !!!next-token;
5353          return;          redo B;
5354        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
5355          !!!parse-error (type => 'isindex');          !!!parse-error (type => 'isindex');
5356                    
5357          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
5358            ## Ignore the token            ## Ignore the token
5359            !!!next-token;            !!!next-token;
5360            return;            redo B;
5361          } else {          } else {
5362            my $at = $token->{attributes};            my $at = $token->{attributes};
5363            my $form_attrs;            my $form_attrs;
# Line 2912  sub _tree_construction_main ($) { Line 5367  sub _tree_construction_main ($) {
5367            delete $at->{action};            delete $at->{action};
5368            delete $at->{prompt};            delete $at->{prompt};
5369            my @tokens = (            my @tokens = (
5370                          {type => 'start tag', tag_name => 'form',                          {type => START_TAG_TOKEN, tag_name => 'form',
5371                           attributes => $form_attrs},                           attributes => $form_attrs},
5372                          {type => 'start tag', tag_name => 'hr'},                          {type => START_TAG_TOKEN, tag_name => 'hr'},
5373                          {type => 'start tag', tag_name => 'p'},                          {type => START_TAG_TOKEN, tag_name => 'p'},
5374                          {type => 'start tag', tag_name => 'label'},                          {type => START_TAG_TOKEN, tag_name => 'label'},
5375                         );                         );
5376            if ($prompt_attr) {            if ($prompt_attr) {
5377              push @tokens, {type => 'character', data => $prompt_attr->{value}};              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};
5378            } else {            } else {
5379              push @tokens, {type => 'character',              push @tokens, {type => CHARACTER_TOKEN,
5380                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD
5381              ## TODO: make this configurable              ## TODO: make this configurable
5382            }            }
5383            push @tokens,            push @tokens,
5384                          {type => 'start tag', tag_name => 'input', attributes => $at},                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},
5385                          #{type => 'character', data => ''}, # SHOULD                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
5386                          {type => 'end tag', tag_name => 'label'},                          {type => END_TAG_TOKEN, tag_name => 'label'},
5387                          {type => 'end tag', tag_name => 'p'},                          {type => END_TAG_TOKEN, tag_name => 'p'},
5388                          {type => 'start tag', tag_name => 'hr'},                          {type => START_TAG_TOKEN, tag_name => 'hr'},
5389                          {type => 'end tag', tag_name => 'form'};                          {type => END_TAG_TOKEN, tag_name => 'form'};
5390            $token = shift @tokens;            $token = shift @tokens;
5391            !!!back-token (@tokens);            !!!back-token (@tokens);
5392            return;            redo B;
5393          }          }
5394        } elsif ({        } elsif ($token->{tag_name} eq 'textarea') {
                 textarea => 1,  
                 iframe => 1,  
                 noembed => 1,  
                 noframes => 1,  
                 noscript => 0, ## TODO: 1 if scripting is enabled  
                }->{$token->{tag_name}}) {  
5395          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
5396          my $el;          my $el;
5397          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $token->{tag_name}, $token->{attributes});
5398                    
5399          if ($token->{tag_name} eq 'textarea') {          ## TODO: $self->{form_element} if defined
5400            ## TODO: $self->{form_element} if defined          $self->{content_model} = RCDATA_CONTENT_MODEL;
           $self->{content_model_flag} = 'RCDATA';  
         } else {  
           $self->{content_model_flag} = 'CDATA';  
         }  
5401          delete $self->{escape}; # MUST          delete $self->{escape}; # MUST
5402                    
5403          $insert->($el);          $insert->($el);
5404                    
5405          my $text = '';          my $text = '';
5406          if ($token->{tag_name} eq 'textarea') {          !!!next-token;
5407            !!!next-token;          if ($token->{type} == CHARACTER_TOKEN) {
5408            if ($token->{type} eq 'character') {            $token->{data} =~ s/^\x0A//;
5409              $token->{data} =~ s/^\x0A//;            unless (length $token->{data}) {
5410              unless (length $token->{data}) {              !!!next-token;
               !!!next-token;  
             }  
5411            }            }
         } else {  
           !!!next-token;  
5412          }          }
5413          while ($token->{type} eq 'character') {          while ($token->{type} == CHARACTER_TOKEN) {
5414            $text .= $token->{data};            $text .= $token->{data};
5415            !!!next-token;            !!!next-token;
5416          }          }
# Line 2977  sub _tree_construction_main ($) { Line 5418  sub _tree_construction_main ($) {
5418            $el->manakai_append_text ($text);            $el->manakai_append_text ($text);
5419          }          }
5420                    
5421          $self->{content_model_flag} = 'PCDATA';          $self->{content_model} = PCDATA_CONTENT_MODEL;
5422                    
5423          if ($token->{type} eq 'end tag' and          if ($token->{type} == END_TAG_TOKEN and
5424              $token->{tag_name} eq $tag_name) {              $token->{tag_name} eq $tag_name) {
5425            ## Ignore the token            ## Ignore the token
5426          } else {          } else {
5427            if ($token->{tag_name} eq 'textarea') {            !!!parse-error (type => 'in RCDATA:#'.$token->{type});
             !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
           } else {  
             !!!parse-error (type => 'in CDATA:#'.$token->{type});  
           }  
           ## ISSUE: And ignore?  
5428          }          }
5429          !!!next-token;          !!!next-token;
5430          return;          redo B;
5431          } elsif ({
5432                    iframe => 1,
5433                    noembed => 1,
5434                    noframes => 1,
5435                    noscript => 0, ## TODO: 1 if scripting is enabled
5436                   }->{$token->{tag_name}}) {
5437            ## NOTE: There is an "as if in body" code clone.
5438            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
5439            redo B;
5440        } elsif ($token->{tag_name} eq 'select') {        } elsif ($token->{tag_name} eq 'select') {
5441          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5442                    
5443          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5444                    
5445          $self->{insertion_mode} = 'in select';          $self->{insertion_mode} = IN_SELECT_IM;
5446          !!!next-token;          !!!next-token;
5447          return;          redo B;
5448        } elsif ({        } elsif ({
5449                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
5450                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 3009  sub _tree_construction_main ($) { Line 5454  sub _tree_construction_main ($) {
5454          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!parse-error (type => 'in body:'.$token->{tag_name});
5455          ## Ignore the token          ## Ignore the token
5456          !!!next-token;          !!!next-token;
5457          return;          redo B;
5458                    
5459          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
5460        } else {        } else {
# Line 3018  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                    
5465          !!!next-token;          !!!next-token;
5466          return;          redo B;
5467        }        }
5468      } elsif ($token->{type} eq 'end tag') {      } elsif ($token->{type} == END_TAG_TOKEN) {
5469        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
5470          if (@{$self->{open_elements}} > 1 and          if (@{$self->{open_elements}} > 1 and
5471              $self->{open_elements}->[1]->[1] eq 'body') {              $self->{open_elements}->[1]->[1] eq 'body') {
# Line 3028  sub _tree_construction_main ($) { Line 5473  sub _tree_construction_main ($) {
5473              unless ({              unless ({
5474                         dd => 1, dt => 1, li => 1, p => 1, td => 1,                         dd => 1, dt => 1, li => 1, p => 1, td => 1,
5475                         th => 1, tr => 1, body => 1, html => 1,                         th => 1, tr => 1, body => 1, html => 1,
5476                         tbody => 1, tfoot => 1, thead => 1,
5477                      }->{$_->[1]}) {                      }->{$_->[1]}) {
5478                !!!parse-error (type => 'not closed:'.$_->[1]);                !!!parse-error (type => 'not closed:'.$_->[1]);
5479              }              }
5480            }            }
5481    
5482            $self->{insertion_mode} = 'after body';            $self->{insertion_mode} = AFTER_BODY_IM;
5483            !!!next-token;            !!!next-token;
5484            return;            redo B;
5485          } else {          } else {
5486            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5487            ## Ignore the token            ## Ignore the token
5488            !!!next-token;            !!!next-token;
5489            return;            redo B;
5490          }          }
5491        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
5492          if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {          if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {
# Line 3048  sub _tree_construction_main ($) { Line 5494  sub _tree_construction_main ($) {
5494            if ($self->{open_elements}->[-1]->[1] ne 'body') {            if ($self->{open_elements}->[-1]->[1] ne 'body') {
5495              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);
5496            }            }
5497            $self->{insertion_mode} = 'after body';            $self->{insertion_mode} = AFTER_BODY_IM;
5498            ## reprocess            ## reprocess
5499            return;            redo B;
5500          } else {          } else {
5501            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5502            ## Ignore the token            ## Ignore the token
5503            !!!next-token;            !!!next-token;
5504            return;            redo B;
5505          }          }
5506        } elsif ({        } elsif ({
5507                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
# Line 3077  sub _tree_construction_main ($) { Line 5523  sub _tree_construction_main ($) {
5523                   li => ($token->{tag_name} ne 'li'),                   li => ($token->{tag_name} ne 'li'),
5524                   p => ($token->{tag_name} ne 'p'),                   p => ($token->{tag_name} ne 'p'),
5525                   td => 1, th => 1, tr => 1,                   td => 1, th => 1, tr => 1,
5526                     tbody => 1, tfoot=> 1, thead => 1,
5527                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
5528                !!!back-token;                !!!back-token;
5529                $token = {type => 'end tag',                $token = {type => END_TAG_TOKEN,
5530                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5531                return;                redo B;
5532              }              }
5533              $i = $_;              $i = $_;
5534              last INSCOPE unless $token->{tag_name} eq 'p';              last INSCOPE unless $token->{tag_name} eq 'p';
# Line 3094  sub _tree_construction_main ($) { Line 5541  sub _tree_construction_main ($) {
5541          } # INSCOPE          } # INSCOPE
5542                    
5543          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
5544            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            if (defined $i) {
5545                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
5546              } else {
5547                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5548              }
5549          }          }
5550                    
5551          splice @{$self->{open_elements}}, $i if defined $i;          if (defined $i) {
5552              splice @{$self->{open_elements}}, $i;
5553            } elsif ($token->{tag_name} eq 'p') {
5554              ## As if <p>, then reprocess the current token
5555              my $el;
5556              !!!create-element ($el, 'p');
5557              $insert->($el);
5558            }
5559          $clear_up_to_marker->()          $clear_up_to_marker->()
5560            if {            if {
5561              button => 1, marquee => 1, object => 1,              button => 1, marquee => 1, object => 1,
5562            }->{$token->{tag_name}};            }->{$token->{tag_name}};
5563          !!!next-token;          !!!next-token;
5564          return;          redo B;
5565        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
5566          ## has an element in scope          ## has an element in scope
5567          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 3113  sub _tree_construction_main ($) { Line 5571  sub _tree_construction_main ($) {
5571              if ({              if ({
5572                   dd => 1, dt => 1, li => 1, p => 1,                   dd => 1, dt => 1, li => 1, p => 1,
5573                   td => 1, th => 1, tr => 1,                   td => 1, th => 1, tr => 1,
5574                     tbody => 1, tfoot=> 1, thead => 1,
5575                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
5576                !!!back-token;                !!!back-token;
5577                $token = {type => 'end tag',                $token = {type => END_TAG_TOKEN,
5578                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5579                return;                redo B;
5580              }              }
5581              last INSCOPE;              last INSCOPE;
5582            } elsif ({            } elsif ({
# Line 3131  sub _tree_construction_main ($) { Line 5590  sub _tree_construction_main ($) {
5590          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {
5591            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
5592          } else {          } else {
5593            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5594          }          }
5595    
5596          undef $self->{form_element};          undef $self->{form_element};
5597          !!!next-token;          !!!next-token;
5598          return;          redo B;
5599        } elsif ({        } elsif ({
5600                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5601                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 3151  sub _tree_construction_main ($) { Line 5610  sub _tree_construction_main ($) {
5610              if ({              if ({
5611                   dd => 1, dt => 1, li => 1, p => 1,                   dd => 1, dt => 1, li => 1, p => 1,
5612                   td => 1, th => 1, tr => 1,                   td => 1, th => 1, tr => 1,
5613                     tbody => 1, tfoot=> 1, thead => 1,
5614                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
5615                !!!back-token;                !!!back-token;
5616                $token = {type => 'end tag',                $token = {type => END_TAG_TOKEN,
5617                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5618                return;                redo B;
5619              }              }
5620              $i = $_;              $i = $_;
5621              last INSCOPE;              last INSCOPE;
# Line 3168  sub _tree_construction_main ($) { Line 5628  sub _tree_construction_main ($) {
5628          } # INSCOPE          } # INSCOPE
5629                    
5630          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
5631            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5632          }          }
5633                    
5634          splice @{$self->{open_elements}}, $i if defined $i;          splice @{$self->{open_elements}}, $i if defined $i;
5635          !!!next-token;          !!!next-token;
5636          return;          redo B;
5637        } elsif ({        } elsif ({
5638                  a => 1,                  a => 1,
5639                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
# Line 3181  sub _tree_construction_main ($) { Line 5641  sub _tree_construction_main ($) {
5641                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
5642                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5643          $formatting_end_tag->($token->{tag_name});          $formatting_end_tag->($token->{tag_name});
5644  ## TODO: <http://html5.org/tools/web-apps-tracker?from=883&to=884>          redo B;
5645          return;        } elsif ($token->{tag_name} eq 'br') {
5646            !!!parse-error (type => 'unmatched end tag:br');
5647    
5648            ## As if <br>
5649            $reconstruct_active_formatting_elements->($insert_to_current);
5650            
5651            my $el;
5652            !!!create-element ($el, 'br');
5653            $insert->($el);
5654            
5655            ## Ignore the token.
5656            !!!next-token;
5657            redo B;
5658        } elsif ({        } elsif ({
5659                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
5660                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
5661                  tbody => 1, td => 1, tfoot => 1, th => 1,                  tbody => 1, td => 1, tfoot => 1, th => 1,
5662                  thead => 1, tr => 1,                  thead => 1, tr => 1,
5663                  area => 1, basefont => 1, bgsound => 1, br => 1,                  area => 1, basefont => 1, bgsound => 1,
5664                  embed => 1, hr => 1, iframe => 1, image => 1,                  embed => 1, hr => 1, iframe => 1, image => 1,
5665                  img => 1, input => 1, isindex => 1, noembed => 1,                  img => 1, input => 1, isindex => 1, noembed => 1,
5666                  noframes => 1, param => 1, select => 1, spacer => 1,                  noframes => 1, param => 1, select => 1, spacer => 1,
# Line 3198  sub _tree_construction_main ($) { Line 5670  sub _tree_construction_main ($) {
5670          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5671          ## Ignore the token          ## Ignore the token
5672          !!!next-token;          !!!next-token;
5673          return;          redo B;
5674                    
5675          ## ISSUE: Issue on HTML5 new elements in spec          ## ISSUE: Issue on HTML5 new elements in spec
5676                    
# Line 3215  sub _tree_construction_main ($) { Line 5687  sub _tree_construction_main ($) {
5687              if ({              if ({
5688                   dd => 1, dt => 1, li => 1, p => 1,                   dd => 1, dt => 1, li => 1, p => 1,
5689                   td => 1, th => 1, tr => 1,                   td => 1, th => 1, tr => 1,
5690                     tbody => 1, tfoot => 1, thead => 1,
5691                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
5692                !!!back-token;                !!!back-token;
5693                $token = {type => 'end tag',                $token = {type => END_TAG_TOKEN,
5694                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5695                return;                redo B;
5696              }              }
5697                    
5698              ## Step 2              ## Step 2
5699              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {
5700                  ## NOTE: <x><y></x>
5701                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
5702              }              }
5703                            
# Line 3238  sub _tree_construction_main ($) { Line 5712  sub _tree_construction_main ($) {
5712                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
5713                  ($special_category->{$node->[1]} or                  ($special_category->{$node->[1]} or
5714                   $scoping_category->{$node->[1]})) {                   $scoping_category->{$node->[1]})) {
5715                !!!parse-error (type => 'not closed:'.$node->[1]);                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5716                ## Ignore the token                ## Ignore the token
5717                !!!next-token;                !!!next-token;
5718                last S2;                last S2;
# Line 3252  sub _tree_construction_main ($) { Line 5726  sub _tree_construction_main ($) {
5726            ## Step 5;            ## Step 5;
5727            redo S2;            redo S2;
5728          } # S2          } # S2
5729          return;          redo B;
       }  
     }  
   }; # $in_body  
   
   B: {  
     if ($phase eq 'main') {  
       if ($token->{type} eq 'DOCTYPE') {  
         !!!parse-error (type => 'in html:#DOCTYPE');  
         ## Ignore the token  
         ## Stay in the phase  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'start tag' and  
                $token->{tag_name} eq 'html') {  
         ## TODO: unless it is the first start tag token, parse-error  
         my $top_el = $self->{open_elements}->[0]->[0];  
         for my $attr_name (keys %{$token->{attributes}}) {  
           unless ($top_el->has_attribute_ns (undef, $attr_name)) {  
             $top_el->set_attribute_ns  
               (undef, [undef, $attr_name],  
                $token->{attributes}->{$attr_name}->{value});  
           }  
         }  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'end-of-file') {  
         ## Generate implied end tags  
         if ({  
              dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,  
             }->{$self->{open_elements}->[-1]->[1]}) {  
           !!!back-token;  
           $token = {type => 'end tag', tag_name => $self->{open_elements}->[-1]->[1]};  
           redo B;  
         }  
           
         if (@{$self->{open_elements}} > 2 or  
             (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         } elsif (defined $self->{inner_html_node} and  
                  @{$self->{open_elements}} > 1 and  
                  $self->{open_elements}->[1]->[1] ne 'body') {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         }  
   
         ## Stop parsing  
         last B;  
   
         ## ISSUE: There is an issue in the spec.  
       } else {  
         if ($self->{insertion_mode} eq 'before head') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
             ## As if <head>  
             !!!create-element ($self->{head_element}, 'head');  
             $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});  
             push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
             $self->{insertion_mode} = 'in head';  
             ## reprocess  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             my $attr = $token->{tag_name} eq 'head' ? $token->{attributes} : {};  
             !!!create-element ($self->{head_element}, 'head', $attr);  
             $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});  
             push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
             $self->{insertion_mode} = 'in head';  
             if ($token->{tag_name} eq 'head') {  
               !!!next-token;  
             #} elsif ({  
             #          base => 1, link => 1, meta => 1,  
             #          script => 1, style => 1, title => 1,  
             #         }->{$token->{tag_name}}) {  
             #  ## reprocess  
             } else {  
               ## reprocess  
             }  
             redo B;  
           } elsif ($token->{type} eq 'end tag') {  
             if ({head => 1, body => 1, html => 1}->{$token->{tag_name}}) {  
               ## As if <head>  
               !!!create-element ($self->{head_element}, 'head');  
               $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});  
               push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
               $self->{insertion_mode} = 'in head';  
               ## reprocess  
               redo B;  
             } else {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token ## ISSUE: An issue in the spec.  
               !!!next-token;  
               redo B;  
             }  
           } else {  
             die "$0: $token->{type}: Unknown type";  
           }  
         } elsif ($self->{insertion_mode} eq 'in head') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
               
             #  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'title') {  
               ## NOTE: There is an "as if in head" code clone  
               my $title_el;  
               !!!create-element ($title_el, 'title', $token->{attributes});  
               (defined $self->{head_element} ? $self->{head_element} : $self->{open_elements}->[-1]->[0])  
                 ->append_child ($title_el);  
               $self->{content_model_flag} = 'RCDATA';  
               delete $self->{escape}; # MUST  
   
               my $text = '';  
               !!!next-token;  
               while ($token->{type} eq 'character') {  
                 $text .= $token->{data};  
                 !!!next-token;  
               }  
               if (length $text) {  
                 $title_el->manakai_append_text ($text);  
               }  
                 
               $self->{content_model_flag} = 'PCDATA';  
                 
               if ($token->{type} eq 'end tag' and  
                   $token->{tag_name} eq 'title') {  
                 ## Ignore the token  
               } else {  
                 !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
                 ## ISSUE: And ignore?  
               }  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'style') {  
               $style_start_tag->();  
               redo B;  
             } elsif ($token->{tag_name} eq 'script') {  
               $script_start_tag->();  
               redo B;  
             } elsif ({base => 1, link => 1, meta => 1}->{$token->{tag_name}}) {  
               ## NOTE: There are "as if in head" code clones  
               my $el;  
               !!!create-element ($el, $token->{tag_name}, $token->{attributes});  
               if ($self->{insertion_mode} eq 'in head' and  
                   defined $self->{head_element}) {  
                 $self->{head_element}->append_child ($el);  
               } else {  
                 $self->{open_elements}->[-1]->[0]->append_child ($el);  
               }  
   
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'head') {  
               !!!parse-error (type => 'in head:head');  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'head') {  
               if ($self->{open_elements}->[-1]->[1] eq 'head') {  
                 pop @{$self->{open_elements}};  
               } else {  
                 !!!parse-error (type => 'unmatched end tag:head');  
               }  
               $self->{insertion_mode} = 'after head';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'body' or  
                      $token->{tag_name} eq 'html') {  
               #  
             } else {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             }  
           } else {  
             #  
           }  
   
           if ($self->{open_elements}->[-1]->[1] eq 'head') {  
             ## As if </head>  
             pop @{$self->{open_elements}};  
           }  
           $self->{insertion_mode} = 'after head';  
           ## reprocess  
           redo B;  
   
           ## ISSUE: An issue in the spec.  
         } elsif ($self->{insertion_mode} eq 'after head') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
               
             #  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'body') {  
               !!!insert-element ('body', $token->{attributes});  
               $self->{insertion_mode} = 'in body';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'frameset') {  
               !!!insert-element ('frameset', $token->{attributes});  
               $self->{insertion_mode} = 'in frameset';  
               !!!next-token;  
               redo B;  
             } elsif ({  
                       base => 1, link => 1, meta => 1,  
                       script => 1, style => 1, title => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'after head:'.$token->{tag_name});  
               $self->{insertion_mode} = 'in head';  
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
             
           ## As if <body>  
           !!!insert-element ('body');  
           $self->{insertion_mode} = 'in body';  
           ## reprocess  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in body') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: There is a code clone of "character in body".  
             $reconstruct_active_formatting_elements->($insert_to_current);  
               
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
   
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             ## NOTE: There is a code clone of "comment in body".  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } else {  
             $in_body->($insert_to_current);  
             redo B;  
           }  
         } elsif ($self->{insertion_mode} eq 'in table') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: There are "character in table" code clones.  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
                 
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
   
             !!!parse-error (type => 'in table:#character');  
   
             ## As if in body, but insert into foster parent element  
             ## ISSUE: Spec says that "whenever a node would be inserted  
             ## into the current node" while characters might not be  
             ## result in a new Text node.  
             $reconstruct_active_formatting_elements->($insert_to_foster);  
               
             if ({  
                  table => 1, tbody => 1, tfoot => 1,  
                  thead => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               # MUST  
               my $foster_parent_element;  
               my $next_sibling;  
               my $prev_sibling;  
               OE: for (reverse 0..$#{$self->{open_elements}}) {  
                 if ($self->{open_elements}->[$_]->[1] eq 'table') {  
                   my $parent = $self->{open_elements}->[$_]->[0]->parent_node;  
                   if (defined $parent and $parent->node_type == 1) {  
                     $foster_parent_element = $parent;  
                     $next_sibling = $self->{open_elements}->[$_]->[0];  
                     $prev_sibling = $next_sibling->previous_sibling;  
                   } else {  
                     $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];  
                     $prev_sibling = $foster_parent_element->last_child;  
                   }  
                   last OE;  
                 }  
               } # OE  
               $foster_parent_element = $self->{open_elements}->[0]->[0] and  
               $prev_sibling = $foster_parent_element->last_child  
                 unless defined $foster_parent_element;  
               if (defined $prev_sibling and  
                   $prev_sibling->node_type == 3) {  
                 $prev_sibling->manakai_append_text ($token->{data});  
               } else {  
                 $foster_parent_element->insert_before  
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
               }  
             } else {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
             }  
               
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ({  
                  caption => 1,  
                  colgroup => 1,  
                  tbody => 1, tfoot => 1, thead => 1,  
                 }->{$token->{tag_name}}) {  
               ## Clear back to table context  
               while ($self->{open_elements}->[-1]->[1] ne 'table' and  
                      $self->{open_elements}->[-1]->[1] ne 'html') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               push @$active_formatting_elements, ['#marker', '']  
                 if $token->{tag_name} eq 'caption';  
   
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               $self->{insertion_mode} = {  
                                  caption => 'in caption',  
                                  colgroup => 'in column group',  
                                  tbody => 'in table body',  
                                  tfoot => 'in table body',  
                                  thead => 'in table body',  
                                 }->{$token->{tag_name}};  
               !!!next-token;  
               redo B;  
             } elsif ({  
                       col => 1,  
                       td => 1, th => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               ## Clear back to table context  
               while ($self->{open_elements}->[-1]->[1] ne 'table' and  
                      $self->{open_elements}->[-1]->[1] ne 'html') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               !!!insert-element ($token->{tag_name} eq 'col' ? 'colgroup' : 'tbody');  
               $self->{insertion_mode} = $token->{tag_name} eq 'col'  
                 ? 'in column group' : 'in table body';  
               ## reprocess  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## NOTE: There are code clones for this "table in table"  
               !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
   
               ## As if </table>  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'table') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:table');  
                 ## Ignore tokens </table><table>  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # <table>  
                 $token = {type => 'end tag', tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'table') {  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               !!!next-token;  
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1, colgroup => 1,  
                       html => 1, tbody => 1, td => 1, tfoot => 1, th => 1,  
                       thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
   
           !!!parse-error (type => 'in table:'.$token->{tag_name});  
           $in_body->($insert_to_foster);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in caption') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a code clone of "character in body".  
             $reconstruct_active_formatting_elements->($insert_to_current);  
               
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
   
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             ## NOTE: This is a code clone of "comment in body".  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ({  
                  caption => 1, col => 1, colgroup => 1, tbody => 1,  
                  td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,  
                 }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'not closed:caption');  
   
               ## As if </caption>  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'caption') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:caption');  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # <?>  
                 $token = {type => 'end tag', tag_name => 'caption'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'caption') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
   
               $self->{insertion_mode} = 'in table';  
   
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'caption') {  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'caption') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
   
               $self->{insertion_mode} = 'in table';  
   
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               !!!parse-error (type => 'not closed:caption');  
   
               ## As if </caption>  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'caption') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:caption');  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # </table>  
                 $token = {type => 'end tag', tag_name => 'caption'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'caption') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
   
               $self->{insertion_mode} = 'in table';  
   
               ## reprocess  
               redo B;  
             } elsif ({  
                       body => 1, col => 1, colgroup => 1,  
                       html => 1, tbody => 1, td => 1, tfoot => 1,  
                       th => 1, thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
                 
           $in_body->($insert_to_current);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in column group') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
               
             #  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'col') {  
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               pop @{$self->{open_elements}};  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'colgroup') {  
               if ($self->{open_elements}->[-1]->[1] eq 'html') {  
                 !!!parse-error (type => 'unmatched end tag:colgroup');  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               } else {  
                 pop @{$self->{open_elements}}; # colgroup  
                 $self->{insertion_mode} = 'in table';  
                 !!!next-token;  
                 redo B;              
               }  
             } elsif ($token->{tag_name} eq 'col') {  
               !!!parse-error (type => 'unmatched end tag:col');  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
   
           ## As if </colgroup>  
           if ($self->{open_elements}->[-1]->[1] eq 'html') {  
             !!!parse-error (type => 'unmatched end tag:colgroup');  
             ## Ignore the token  
             !!!next-token;  
             redo B;  
           } else {  
             pop @{$self->{open_elements}}; # colgroup  
             $self->{insertion_mode} = 'in table';  
             ## reprocess  
             redo B;  
           }  
         } elsif ($self->{insertion_mode} eq 'in table body') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a "character in table" code clone.  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
                 
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
   
             !!!parse-error (type => 'in table:#character');  
   
             ## As if in body, but insert into foster parent element  
             ## ISSUE: Spec says that "whenever a node would be inserted  
             ## into the current node" while characters might not be  
             ## result in a new Text node.  
             $reconstruct_active_formatting_elements->($insert_to_foster);  
   
             if ({  
                  table => 1, tbody => 1, tfoot => 1,  
                  thead => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               # MUST  
               my $foster_parent_element;  
               my $next_sibling;  
               my $prev_sibling;  
               OE: for (reverse 0..$#{$self->{open_elements}}) {  
                 if ($self->{open_elements}->[$_]->[1] eq 'table') {  
                   my $parent = $self->{open_elements}->[$_]->[0]->parent_node;  
                   if (defined $parent and $parent->node_type == 1) {  
                     $foster_parent_element = $parent;  
                     $next_sibling = $self->{open_elements}->[$_]->[0];  
                     $prev_sibling = $next_sibling->previous_sibling;  
                   } else {  
                     $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];  
                     $prev_sibling = $foster_parent_element->last_child;  
                   }  
                   last OE;  
                 }  
               } # OE  
               $foster_parent_element = $self->{open_elements}->[0]->[0] and  
               $prev_sibling = $foster_parent_element->last_child  
                 unless defined $foster_parent_element;  
               if (defined $prev_sibling and  
                   $prev_sibling->node_type == 3) {  
                 $prev_sibling->manakai_append_text ($token->{data});  
               } else {  
                 $foster_parent_element->insert_before  
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
               }  
             } else {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
             }  
               
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             ## Copied from 'in table'  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ({  
                  tr => 1,  
                  th => 1, td => 1,  
                 }->{$token->{tag_name}}) {  
               unless ($token->{tag_name} eq 'tr') {  
                 !!!parse-error (type => 'missing start tag:tr');  
               }  
   
               ## Clear back to table body context  
               while (not {  
                 tbody => 1, tfoot => 1, thead => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
                 
               $self->{insertion_mode} = 'in row';  
               if ($token->{tag_name} eq 'tr') {  
                 !!!insert-element ($token->{tag_name}, $token->{attributes});  
                 !!!next-token;  
               } else {  
                 !!!insert-element ('tr');  
                 ## reprocess  
               }  
               redo B;  
             } elsif ({  
                       caption => 1, col => 1, colgroup => 1,  
                       tbody => 1, tfoot => 1, thead => 1,  
                      }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ({  
                      tbody => 1, thead => 1, tfoot => 1,  
                     }->{$node->[1]}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table body context  
               while (not {  
                 tbody => 1, tfoot => 1, thead => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               ## As if <{current node}>  
               ## have an element in table scope  
               ## true by definition  
   
               ## Clear back to table body context  
               ## nop by definition  
   
               pop @{$self->{open_elements}};  
               $self->{insertion_mode} = 'in table';  
               ## reprocess  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## NOTE: This is a code clone of "table in table"  
               !!!parse-error (type => 'not closed:table');  
   
               ## As if </table>  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'table') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:table');  
                 ## Ignore tokens </table><table>  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # <table>  
                 $token = {type => 'end tag', tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ({  
                  tbody => 1, tfoot => 1, thead => 1,  
                 }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table body context  
               while (not {  
                 tbody => 1, tfoot => 1, thead => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               pop @{$self->{open_elements}};  
               $self->{insertion_mode} = 'in table';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ({  
                      tbody => 1, thead => 1, tfoot => 1,  
                     }->{$node->[1]}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table body context  
               while (not {  
                 tbody => 1, tfoot => 1, thead => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               ## As if <{current node}>  
               ## have an element in table scope  
               ## true by definition  
   
               ## Clear back to table body context  
               ## nop by definition  
   
               pop @{$self->{open_elements}};  
               $self->{insertion_mode} = 'in table';  
               ## reprocess  
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1, colgroup => 1,  
                       html => 1, td => 1, th => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
             
           ## As if in table  
           !!!parse-error (type => 'in table:'.$token->{tag_name});  
           $in_body->($insert_to_foster);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in row') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a "character in table" code clone.  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
                 
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
   
             !!!parse-error (type => 'in table:#character');  
   
             ## As if in body, but insert into foster parent element  
             ## ISSUE: Spec says that "whenever a node would be inserted  
             ## into the current node" while characters might not be  
             ## result in a new Text node.  
             $reconstruct_active_formatting_elements->($insert_to_foster);  
               
             if ({  
                  table => 1, tbody => 1, tfoot => 1,  
                  thead => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               # MUST  
               my $foster_parent_element;  
               my $next_sibling;  
               my $prev_sibling;  
               OE: for (reverse 0..$#{$self->{open_elements}}) {  
                 if ($self->{open_elements}->[$_]->[1] eq 'table') {  
                   my $parent = $self->{open_elements}->[$_]->[0]->parent_node;  
                   if (defined $parent and $parent->node_type == 1) {  
                     $foster_parent_element = $parent;  
                     $next_sibling = $self->{open_elements}->[$_]->[0];  
                     $prev_sibling = $next_sibling->previous_sibling;  
                   } else {  
                     $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];  
                     $prev_sibling = $foster_parent_element->last_child;  
                   }  
                   last OE;  
                 }  
               } # OE  
               $foster_parent_element = $self->{open_elements}->[0]->[0] and  
               $prev_sibling = $foster_parent_element->last_child  
                 unless defined $foster_parent_element;  
               if (defined $prev_sibling and  
                   $prev_sibling->node_type == 3) {  
                 $prev_sibling->manakai_append_text ($token->{data});  
               } else {  
                 $foster_parent_element->insert_before  
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
               }  
             } else {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
             }  
               
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             ## Copied from 'in table'  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'th' or  
                 $token->{tag_name} eq 'td') {  
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
                 
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               $self->{insertion_mode} = 'in cell';  
   
               push @$active_formatting_elements, ['#marker', ''];  
                 
               !!!next-token;  
               redo B;  
             } elsif ({  
                       caption => 1, col => 1, colgroup => 1,  
                       tbody => 1, tfoot => 1, thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               ## As if </tr>  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'tr') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               ## reprocess  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## NOTE: This is a code clone of "table in table"  
               !!!parse-error (type => 'not closed:table');  
   
               ## As if </table>  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'table') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:table');  
                 ## Ignore tokens </table><table>  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # <table>  
                 $token = {type => 'end tag', tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'tr') {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## As if </tr>  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'tr') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{type});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               ## reprocess  
               redo B;  
             } elsif ({  
                       tbody => 1, tfoot => 1, thead => 1,  
                      }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## As if </tr>  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'tr') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:tr');  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               ## reprocess  
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1,  
                       colgroup => 1, html => 1, td => 1, th => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
   
           ## As if in table  
           !!!parse-error (type => 'in table:'.$token->{tag_name});  
           $in_body->($insert_to_foster);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in cell') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a code clone of "character in body".  
             $reconstruct_active_formatting_elements->($insert_to_current);  
               
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
   
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             ## NOTE: This is a code clone of "comment in body".  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ({  
                  caption => 1, col => 1, colgroup => 1,  
                  tbody => 1, td => 1, tfoot => 1, th => 1,  
                  thead => 1, tr => 1,  
                 }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $tn;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'td' or $node->[1] eq 'th') {  
                   $tn = $node->[1];  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $tn) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Close the cell  
               !!!back-token; # <?>  
               $token = {type => 'end tag', tag_name => $tn};  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => ($token->{tag_name} eq 'th'),  
                    th => ($token->{tag_name} eq 'td'),  
                    tr => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
   
               $self->{insertion_mode} = 'in row';  
   
               !!!next-token;  
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1,  
                       colgroup => 1, html => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } elsif ({  
                       table => 1, tbody => 1, tfoot => 1,  
                       thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $i;  
               my $tn;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {  
                   $tn = $node->[1];  
                   ## NOTE: There is exactly one |td| or |th| element  
                   ## in scope in the stack of open elements by definition.  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => 'end tag', tag_name => $tn};  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
             
           $in_body->($insert_to_current);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in select') {  
           if ($token->{type} eq 'character') {  
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'option') {  
               if ($self->{open_elements}->[-1]->[1] eq 'option') {  
                 ## As if </option>  
                 pop @{$self->{open_elements}};  
               }  
   
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'optgroup') {  
               if ($self->{open_elements}->[-1]->[1] eq 'option') {  
                 ## As if </option>  
                 pop @{$self->{open_elements}};  
               }  
   
               if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {  
                 ## As if </optgroup>  
                 pop @{$self->{open_elements}};  
               }  
   
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'select') {  
               !!!parse-error (type => 'not closed:select');  
               ## As if </select> instead  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:select');  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'optgroup') {  
               if ($self->{open_elements}->[-1]->[1] eq 'option' and  
                   $self->{open_elements}->[-2]->[1] eq 'optgroup') {  
                 ## As if </option>  
                 splice @{$self->{open_elements}}, -2;  
               } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {  
                 pop @{$self->{open_elements}};  
               } else {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
               }  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'option') {  
               if ($self->{open_elements}->[-1]->[1] eq 'option') {  
                 pop @{$self->{open_elements}};  
               } else {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
               }  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'select') {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               !!!next-token;  
               redo B;  
             } elsif ({  
                       caption => 1, table => 1, tbody => 1,  
                       tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## As if </select>  
               ## have an element in table scope  
               undef $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'select') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:select');  
                 ## Ignore the </select> token  
                 !!!next-token; ## TODO: ok?  
                 redo B;  
               }  
                 
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
   
           !!!parse-error (type => 'in select:'.$token->{tag_name});  
           ## Ignore the token  
           !!!next-token;  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'after body') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               ## As if in body  
               $reconstruct_active_formatting_elements->($insert_to_current);  
                 
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
   
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
               
             #  
             !!!parse-error (type => 'after body:#'.$token->{type});  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[0]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             !!!parse-error (type => 'after body:'.$token->{tag_name});  
             #  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'html') {  
               if (defined $self->{inner_html_node}) {  
                 !!!parse-error (type => 'unmatched end tag:html');  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               } else {  
                 $phase = 'trailing end';  
                 !!!next-token;  
                 redo B;  
               }  
             } else {  
               !!!parse-error (type => 'after body:/'.$token->{tag_name});  
             }  
           } else {  
             !!!parse-error (type => 'after body:#'.$token->{type});  
           }  
   
           $self->{insertion_mode} = 'in body';  
           ## reprocess  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in frameset') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
   
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
   
             #  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'frameset') {  
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'frame') {  
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               pop @{$self->{open_elements}};  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'noframes') {  
               $in_body->($insert_to_current);  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'frameset') {  
               if ($self->{open_elements}->[-1]->[1] eq 'html' and  
                   @{$self->{open_elements}} == 1) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
               } else {  
                 pop @{$self->{open_elements}};  
                 !!!next-token;  
               }  
                 
               ## if not inner_html and  
               if ($self->{open_elements}->[-1]->[1] ne 'frameset') {  
                 $self->{insertion_mode} = 'after frameset';  
               }  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
             
           if (defined $token->{tag_name}) {  
             !!!parse-error (type => 'in frameset:'.$token->{tag_name});  
           } else {  
             !!!parse-error (type => 'in frameset:#'.$token->{type});  
           }  
           ## Ignore the token  
           !!!next-token;  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'after frameset') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
   
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
   
             #  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'noframes') {  
               $in_body->($insert_to_current);  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'html') {  
               $phase = 'trailing end';  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
             
           if (defined $token->{tag_name}) {  
             !!!parse-error (type => 'after frameset:'.$token->{tag_name});  
           } else {  
             !!!parse-error (type => 'after frameset:#'.$token->{type});  
           }  
           ## Ignore the token  
           !!!next-token;  
           redo B;  
   
           ## ISSUE: An issue in spec there  
         } else {  
           die "$0: $self->{insertion_mode}: Unknown insertion mode";  
         }  
       }  
     } elsif ($phase eq 'trailing end') {  
       ## states in the main stage is preserved yet # MUST  
         
       if ($token->{type} eq 'DOCTYPE') {  
         !!!parse-error (type => 'after html:#DOCTYPE');  
         ## Ignore the token  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'comment') {  
         my $comment = $self->{document}->create_comment ($token->{data});  
         $self->{document}->append_child ($comment);  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'character') {  
         if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
           my $data = $1;  
           ## As if in the main phase.  
           ## NOTE: The insertion mode in the main phase  
           ## just before the phase has been changed to the trailing  
           ## end phase is either "after body" or "after frameset".  
           $reconstruct_active_formatting_elements->($insert_to_current)  
             if $phase eq 'main';  
             
           $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);  
             
           unless (length $token->{data}) {  
             !!!next-token;  
             redo B;  
           }  
         }  
   
         !!!parse-error (type => 'after html:#character');  
         $phase = 'main';  
         ## reprocess  
         redo B;  
       } elsif ($token->{type} eq 'start tag' or  
                $token->{type} eq 'end tag') {  
         !!!parse-error (type => 'after html:'.$token->{tag_name});  
         $phase = 'main';  
         ## reprocess  
         redo B;  
       } elsif ($token->{type} eq 'end-of-file') {  
         ## Stop parsing  
         last B;  
       } else {  
         die "$0: $token->{type}: Unknown token";  
5730        }        }
5731      }      }
5732        redo B;
5733    } # B    } # B
5734    
5735      ## NOTE: The "trailing end" phase in HTML5 is split into
5736      ## two insertion modes: "after html body" and "after html frameset".
5737      ## NOTE: States in the main stage is preserved while
5738      ## the parser stays in the trailing end phase. # MUST
5739    
5740    ## Stop parsing # MUST    ## Stop parsing # MUST
5741        
5742    ## TODO: script stuffs    ## TODO: script stuffs
# Line 5137  sub set_inner_html ($$$) { Line 5748  sub set_inner_html ($$$) {
5748    my $s = \$_[0];    my $s = \$_[0];
5749    my $onerror = $_[1];    my $onerror = $_[1];
5750    
5751      ## ISSUE: Should {confident} be true?
5752    
5753    my $nt = $node->node_type;    my $nt = $node->node_type;
5754    if ($nt == 9) {    if ($nt == 9) {
5755      # MUST      # MUST
# Line 5169  sub set_inner_html ($$$) { Line 5782  sub set_inner_html ($$$) {
5782      my $i = 0;      my $i = 0;
5783      my $line = 1;      my $line = 1;
5784      my $column = 0;      my $column = 0;
5785      $p->{set_next_input_character} = sub {      $p->{set_next_char} = sub {
5786        my $self = shift;        my $self = shift;
5787    
5788        pop @{$self->{prev_input_character}};        pop @{$self->{prev_char}};
5789        unshift @{$self->{prev_input_character}}, $self->{next_input_character};        unshift @{$self->{prev_char}}, $self->{next_char};
5790    
5791        $self->{next_input_character} = -1 and return if $i >= length $$s;        $self->{next_char} = -1 and return if $i >= length $$s;
5792        $self->{next_input_character} = ord substr $$s, $i++, 1;        $self->{next_char} = ord substr $$s, $i++, 1;
5793        $column++;        $column++;
5794    
5795        if ($self->{next_input_character} == 0x000A) { # LF        if ($self->{next_char} == 0x000A) { # LF
5796          $line++;          $line++;
5797          $column = 0;          $column = 0;
5798        } elsif ($self->{next_input_character} == 0x000D) { # CR        } elsif ($self->{next_char} == 0x000D) { # CR
5799          $i++ if substr ($$s, $i, 1) eq "\x0A";          $i++ if substr ($$s, $i, 1) eq "\x0A";
5800          $self->{next_input_character} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
5801          $line++;          $line++;
5802          $column = 0;          $column = 0;
5803        } elsif ($self->{next_input_character} > 0x10FFFF) {        } elsif ($self->{next_char} > 0x10FFFF) {
5804          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
5805        } elsif ($self->{next_input_character} == 0x0000) { # NULL        } elsif ($self->{next_char} == 0x0000) { # NULL
5806          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
5807          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
5808        }        }
5809      };      };
5810      $p->{prev_input_character} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];
5811      $p->{next_input_character} = -1;      $p->{next_char} = -1;
5812            
5813      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
5814        my (%opt) = @_;        my (%opt) = @_;
# Line 5209  sub set_inner_html ($$$) { Line 5822  sub set_inner_html ($$$) {
5822      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
5823    
5824      ## Step 2      ## Step 2
5825      my $node_ln = $node->local_name;      my $node_ln = $node->manakai_local_name;
5826      $p->{content_model_flag} = {      $p->{content_model} = {
5827        title => 'RCDATA',        title => RCDATA_CONTENT_MODEL,
5828        textarea => 'RCDATA',        textarea => RCDATA_CONTENT_MODEL,
5829        style => 'CDATA',        style => CDATA_CONTENT_MODEL,
5830        script => 'CDATA',        script => CDATA_CONTENT_MODEL,
5831        xmp => 'CDATA',        xmp => CDATA_CONTENT_MODEL,
5832        iframe => 'CDATA',        iframe => CDATA_CONTENT_MODEL,
5833        noembed => 'CDATA',        noembed => CDATA_CONTENT_MODEL,
5834        noframes => 'CDATA',        noframes => CDATA_CONTENT_MODEL,
5835        noscript => 'CDATA',        noscript => CDATA_CONTENT_MODEL,
5836        plaintext => 'PLAINTEXT',        plaintext => PLAINTEXT_CONTENT_MODEL,
5837      }->{$node_ln} || 'PCDATA';      }->{$node_ln};
5838         ## ISSUE: What is "the name of the element"? local name?      $p->{content_model} = PCDATA_CONTENT_MODEL
5839            unless defined $p->{content_model};
5840            ## ISSUE: What is "the name of the element"? local name?
5841    
5842      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $node_ln];
5843    
# Line 5247  sub set_inner_html ($$$) { Line 5862  sub set_inner_html ($$$) {
5862        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
5863          my $nsuri = $anode->namespace_uri;          my $nsuri = $anode->namespace_uri;
5864          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
5865            if ($anode->local_name eq 'form') { ## TODO: case?            if ($anode->manakai_local_name eq 'form') {
5866              $p->{form_element} = $anode;              $p->{form_element} = $anode;
5867              last AN;              last AN;
5868            }            }
# Line 5287  sub set_inner_html ($$$) { Line 5902  sub set_inner_html ($$$) {
5902    
5903  } # tree construction stage  } # tree construction stage
5904    
5905  sub get_inner_html ($$$) {  package Whatpm::HTML::RestartParser;
5906    my (undef, $node, $on_error) = @_;  push our @ISA, 'Error';
   
   ## Step 1  
   my $s = '';  
   
   my $in_cdata;  
   my $parent = $node;  
   while (defined $parent) {  
     if ($parent->node_type == 1 and  
         $parent->namespace_uri eq 'http://www.w3.org/1999/xhtml' and  
         {  
           style => 1, script => 1, xmp => 1, iframe => 1,  
           noembed => 1, noframes => 1, noscript => 1,  
         }->{$parent->local_name}) { ## TODO: case thingy  
       $in_cdata = 1;  
     }  
     $parent = $parent->parent_node;  
   }  
   
   ## Step 2  
   my @node = @{$node->child_nodes};  
   C: while (@node) {  
     my $child = shift @node;  
     unless (ref $child) {  
       if ($child eq 'cdata-out') {  
         $in_cdata = 0;  
       } else {  
         $s .= $child; # end tag  
       }  
       next C;  
     }  
       
     my $nt = $child->node_type;  
     if ($nt == 1) { # Element  
       my $tag_name = lc $child->tag_name; ## ISSUE: Definition of "lowercase"  
       $s .= '<' . $tag_name;  
   
       ## ISSUE: Non-html elements  
   
       my @attrs = @{$child->attributes}; # sort order MUST be stable  
       for my $attr (@attrs) { # order is implementation dependent  
         my $attr_name = lc $attr->name; ## ISSUE: Definition of "lowercase"  
         $s .= ' ' . $attr_name . '="';  
         my $attr_value = $attr->value;  
         ## escape  
         $attr_value =~ s/&/&amp;/g;  
         $attr_value =~ s/</&lt;/g;  
         $attr_value =~ s/>/&gt;/g;  
         $attr_value =~ s/"/&quot;/g;  
         $s .= $attr_value . '"';  
       }  
       $s .= '>';  
         
       next C if {  
         area => 1, base => 1, basefont => 1, bgsound => 1,  
         br => 1, col => 1, embed => 1, frame => 1, hr => 1,  
         img => 1, input => 1, link => 1, meta => 1, param => 1,  
         spacer => 1, wbr => 1,  
       }->{$tag_name};  
   
       $s .= "\x0A" if $tag_name eq 'pre' or $tag_name eq 'textarea';  
   
       if (not $in_cdata and {  
         style => 1, script => 1, xmp => 1, iframe => 1,  
         noembed => 1, noframes => 1, noscript => 1,  
       }->{$tag_name}) {  
         unshift @node, 'cdata-out';  
         $in_cdata = 1;  
       }  
   
       unshift @node, @{$child->child_nodes}, '</' . $tag_name . '>';  
     } elsif ($nt == 3 or $nt == 4) {  
       if ($in_cdata) {  
         $s .= $child->data;  
       } else {  
         my $value = $child->data;  
         $value =~ s/&/&amp;/g;  
         $value =~ s/</&lt;/g;  
         $value =~ s/>/&gt;/g;  
         $value =~ s/"/&quot;/g;  
         $s .= $value;  
       }  
     } elsif ($nt == 8) {  
       $s .= '<!--' . $child->data . '-->';  
     } elsif ($nt == 10) {  
       $s .= '<!DOCTYPE ' . $child->name . '>';  
     } elsif ($nt == 5) { # entrefs  
       push @node, @{$child->child_nodes};  
     } else {  
       $on_error->($child) if defined $on_error;  
     }  
     ## ISSUE: This code does not support PIs.  
   } # C  
     
   ## Step 3  
   return \$s;  
 } # get_inner_html  
5907    
5908  1;  1;
5909  # $Date$  # $Date$

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24