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

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

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

revision 1.35 by wakaba, Mon Jul 16 03:21:04 2007 UTC revision 1.57 by wakaba, Sat Aug 11 08:08:12 2007 UTC
# Line 150  sub new ($) { Line 150  sub new ($) {
150    return $self;    return $self;
151  } # new  } # new
152    
153    sub CM_ENTITY () { 0b001 } # & markup in data
154    sub CM_LIMITED_MARKUP () { 0b010 } # < markup in data (limited)
155    sub CM_FULL_MARKUP () { 0b100 } # < markup in data (any)
156    
157    sub PLAINTEXT_CONTENT_MODEL () { 0 }
158    sub CDATA_CONTENT_MODEL () { CM_LIMITED_MARKUP }
159    sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }
160    sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
161    
162    sub DATA_STATE () { 0 }
163    sub ENTITY_DATA_STATE () { 1 }
164    sub TAG_OPEN_STATE () { 2 }
165    sub CLOSE_TAG_OPEN_STATE () { 3 }
166    sub TAG_NAME_STATE () { 4 }
167    sub BEFORE_ATTRIBUTE_NAME_STATE () { 5 }
168    sub ATTRIBUTE_NAME_STATE () { 6 }
169    sub AFTER_ATTRIBUTE_NAME_STATE () { 7 }
170    sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 }
171    sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
172    sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
173    sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
174    sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
175    sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
176    sub COMMENT_START_STATE () { 14 }
177    sub COMMENT_START_DASH_STATE () { 15 }
178    sub COMMENT_STATE () { 16 }
179    sub COMMENT_END_STATE () { 17 }
180    sub COMMENT_END_DASH_STATE () { 18 }
181    sub BOGUS_COMMENT_STATE () { 19 }
182    sub DOCTYPE_STATE () { 20 }
183    sub BEFORE_DOCTYPE_NAME_STATE () { 21 }
184    sub DOCTYPE_NAME_STATE () { 22 }
185    sub AFTER_DOCTYPE_NAME_STATE () { 23 }
186    sub BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 24 }
187    sub DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE () { 25 }
188    sub DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE () { 26 }
189    sub AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 27 }
190    sub BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 28 }
191    sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE () { 29 }
192    sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }
193    sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
194    sub BOGUS_DOCTYPE_STATE () { 32 }
195    
196    sub DOCTYPE_TOKEN () { 1 }
197    sub COMMENT_TOKEN () { 2 }
198    sub START_TAG_TOKEN () { 3 }
199    sub END_TAG_TOKEN () { 4 }
200    sub END_OF_FILE_TOKEN () { 5 }
201    sub CHARACTER_TOKEN () { 6 }
202    
203    sub AFTER_HTML_IMS () { 0b100 }
204    sub HEAD_IMS ()       { 0b1000 }
205    sub BODY_IMS ()       { 0b10000 }
206    sub BODY_TABLE_IMS () { 0b100000 }
207    sub TABLE_IMS ()      { 0b1000000 }
208    sub ROW_IMS ()        { 0b10000000 }
209    sub BODY_AFTER_IMS () { 0b100000000 }
210    sub FRAME_IMS ()      { 0b1000000000 }
211    
212    sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
213    sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
214    sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
215    sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
216    sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
217    sub BEFORE_HEAD_IM () { HEAD_IMS | 0b11 }
218    sub IN_BODY_IM () { BODY_IMS }
219    sub IN_CELL_IM () { BODY_IMS | BODY_TABLE_IMS | 0b01 }
220    sub IN_CAPTION_IM () { BODY_IMS | BODY_TABLE_IMS | 0b10 }
221    sub IN_ROW_IM () { TABLE_IMS | ROW_IMS | 0b01 }
222    sub IN_TABLE_BODY_IM () { TABLE_IMS | ROW_IMS | 0b10 }
223    sub IN_TABLE_IM () { TABLE_IMS }
224    sub AFTER_BODY_IM () { BODY_AFTER_IMS }
225    sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
226    sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
227    sub IN_SELECT_IM () { 0b01 }
228    sub IN_COLUMN_GROUP_IM () { 0b10 }
229    
230  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
231    
232  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
233    my $self = shift;    my $self = shift;
234    $self->{state} = 'data'; # MUST    $self->{state} = DATA_STATE; # MUST
235    $self->{content_model_flag} = 'PCDATA'; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
236    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE
237    undef $self->{current_attribute};    undef $self->{current_attribute};
238    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
# Line 168  sub _initialize_tokenizer ($) { Line 245  sub _initialize_tokenizer ($) {
245  } # _initialize_tokenizer  } # _initialize_tokenizer
246    
247  ## A token has:  ## A token has:
248  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',  ##   ->{type} == DOCTYPE_TOKEN, START_TAG_TOKEN, END_TAG_TOKEN, COMMENT_TOKEN,
249  ##       'character', or 'end-of-file'  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN
250  ##   ->{name} (DOCTYPE, start tag (tag name), end tag (tag name))  ##   ->{name} (DOCTYPE_TOKEN)
251  ##   ->{public_identifier} (DOCTYPE)  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
252  ##   ->{system_identifier} (DOCTYPE)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
253  ##   ->{correct} == 1 or 0 (DOCTYPE)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
254  ##   ->{attributes} isa HASH (start tag, end tag)  ##   ->{correct} == 1 or 0 (DOCTYPE_TOKEN)
255  ##   ->{data} (comment, character)  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
256    ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
257    
258  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
259    
# Line 192  sub _get_next_token ($) { Line 270  sub _get_next_token ($) {
270    }    }
271    
272    A: {    A: {
273      if ($self->{state} eq 'data') {      if ($self->{state} == DATA_STATE) {
274        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_input_character} == 0x0026) { # &
275          if ($self->{content_model_flag} eq 'PCDATA' or          if ($self->{content_model} & CM_ENTITY) { # PCDATA | RCDATA
276              $self->{content_model_flag} eq 'RCDATA') {            $self->{state} = ENTITY_DATA_STATE;
           $self->{state} = 'entity data';  
277            !!!next-input-character;            !!!next-input-character;
278            redo A;            redo A;
279          } else {          } else {
280            #            #
281          }          }
282        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_input_character} == 0x002D) { # -
283          if ($self->{content_model_flag} eq 'RCDATA' or          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
             $self->{content_model_flag} eq 'CDATA') {  
284            unless ($self->{escape}) {            unless ($self->{escape}) {
285              if ($self->{prev_input_character}->[0] == 0x002D and # -              if ($self->{prev_input_character}->[0] == 0x002D and # -
286                  $self->{prev_input_character}->[1] == 0x0021 and # !                  $self->{prev_input_character}->[1] == 0x0021 and # !
# Line 216  sub _get_next_token ($) { Line 292  sub _get_next_token ($) {
292                    
293          #          #
294        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_input_character} == 0x003C) { # <
295          if ($self->{content_model_flag} eq 'PCDATA' or          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
296              (($self->{content_model_flag} eq 'CDATA' or              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
               $self->{content_model_flag} eq 'RCDATA') and  
297               not $self->{escape})) {               not $self->{escape})) {
298            $self->{state} = 'tag open';            $self->{state} = TAG_OPEN_STATE;
299            !!!next-input-character;            !!!next-input-character;
300            redo A;            redo A;
301          } else {          } else {
# Line 228  sub _get_next_token ($) { Line 303  sub _get_next_token ($) {
303          }          }
304        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
305          if ($self->{escape} and          if ($self->{escape} and
306              ($self->{content_model_flag} eq 'RCDATA' or              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
              $self->{content_model_flag} eq 'CDATA')) {  
307            if ($self->{prev_input_character}->[0] == 0x002D and # -            if ($self->{prev_input_character}->[0] == 0x002D and # -
308                $self->{prev_input_character}->[1] == 0x002D) { # -                $self->{prev_input_character}->[1] == 0x002D) { # -
309              delete $self->{escape};              delete $self->{escape};
# Line 238  sub _get_next_token ($) { Line 312  sub _get_next_token ($) {
312                    
313          #          #
314        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
315          !!!emit ({type => 'end-of-file'});          !!!emit ({type => END_OF_FILE_TOKEN});
316          last A; ## TODO: ok?          last A; ## TODO: ok?
317        }        }
318        # Anything else        # Anything else
319        my $token = {type => 'character',        my $token = {type => CHARACTER_TOKEN,
320                     data => chr $self->{next_input_character}};                     data => chr $self->{next_input_character}};
321        ## Stay in the data state        ## Stay in the data state
322        !!!next-input-character;        !!!next-input-character;
# Line 250  sub _get_next_token ($) { Line 324  sub _get_next_token ($) {
324        !!!emit ($token);        !!!emit ($token);
325    
326        redo A;        redo A;
327      } elsif ($self->{state} eq 'entity data') {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
328        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
329                
330        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);
331    
332        $self->{state} = 'data';        $self->{state} = DATA_STATE;
333        # next-input-character is already done        # next-input-character is already done
334    
335        unless (defined $token) {        unless (defined $token) {
336          !!!emit ({type => 'character', data => '&'});          !!!emit ({type => CHARACTER_TOKEN, data => '&'});
337        } else {        } else {
338          !!!emit ($token);          !!!emit ($token);
339        }        }
340    
341        redo A;        redo A;
342      } elsif ($self->{state} eq 'tag open') {      } elsif ($self->{state} == TAG_OPEN_STATE) {
343        if ($self->{content_model_flag} eq 'RCDATA' or        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
           $self->{content_model_flag} eq 'CDATA') {  
344          if ($self->{next_input_character} == 0x002F) { # /          if ($self->{next_input_character} == 0x002F) { # /
345            !!!next-input-character;            !!!next-input-character;
346            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
347            redo A;            redo A;
348          } else {          } else {
349            ## reconsume            ## reconsume
350            $self->{state} = 'data';            $self->{state} = DATA_STATE;
351    
352            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<'});
353    
354            redo A;            redo A;
355          }          }
356        } elsif ($self->{content_model_flag} eq 'PCDATA') {        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
357          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_input_character} == 0x0021) { # !
358            $self->{state} = 'markup declaration open';            $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
359            !!!next-input-character;            !!!next-input-character;
360            redo A;            redo A;
361          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{next_input_character} == 0x002F) { # /
362            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
363            !!!next-input-character;            !!!next-input-character;
364            redo A;            redo A;
365          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_input_character} and
366                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{next_input_character} <= 0x005A) { # A..Z
367            $self->{current_token}            $self->{current_token}
368              = {type => 'start tag',              = {type => START_TAG_TOKEN,
369                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_input_character} + 0x0020)};
370            $self->{state} = 'tag name';            $self->{state} = TAG_NAME_STATE;
371            !!!next-input-character;            !!!next-input-character;
372            redo A;            redo A;
373          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_input_character} and
374                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_input_character} <= 0x007A) { # a..z
375            $self->{current_token} = {type => 'start tag',            $self->{current_token} = {type => START_TAG_TOKEN,
376                              tag_name => chr ($self->{next_input_character})};                              tag_name => chr ($self->{next_input_character})};
377            $self->{state} = 'tag name';            $self->{state} = TAG_NAME_STATE;
378            !!!next-input-character;            !!!next-input-character;
379            redo A;            redo A;
380          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_input_character} == 0x003E) { # >
381            !!!parse-error (type => 'empty start tag');            !!!parse-error (type => 'empty start tag');
382            $self->{state} = 'data';            $self->{state} = DATA_STATE;
383            !!!next-input-character;            !!!next-input-character;
384    
385            !!!emit ({type => 'character', data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});
386    
387            redo A;            redo A;
388          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_input_character} == 0x003F) { # ?
389            !!!parse-error (type => 'pio');            !!!parse-error (type => 'pio');
390            $self->{state} = 'bogus comment';            $self->{state} = BOGUS_COMMENT_STATE;
391            ## $self->{next_input_character} is intentionally left as is            ## $self->{next_input_character} is intentionally left as is
392            redo A;            redo A;
393          } else {          } else {
394            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago');
395            $self->{state} = 'data';            $self->{state} = DATA_STATE;
396            ## reconsume            ## reconsume
397    
398            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<'});
399    
400            redo A;            redo A;
401          }          }
402        } else {        } else {
403          die "$0: $self->{content_model_flag}: Unknown content model flag";          die "$0: $self->{content_model} in tag open";
404        }        }
405      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
406        if ($self->{content_model_flag} eq 'RCDATA' or        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
           $self->{content_model_flag} eq 'CDATA') {  
407          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
408            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
409            my @next_char;            my @next_char;
# Line 345  sub _get_next_token ($) { Line 417  sub _get_next_token ($) {
417              } else {              } else {
418                $self->{next_input_character} = shift @next_char; # reconsume                $self->{next_input_character} = shift @next_char; # reconsume
419                !!!back-next-input-character (@next_char);                !!!back-next-input-character (@next_char);
420                $self->{state} = 'data';                $self->{state} = DATA_STATE;
421    
422                !!!emit ({type => 'character', data => '</'});                !!!emit ({type => CHARACTER_TOKEN, data => '</'});
423        
424                redo A;                redo A;
425              }              }
# Line 364  sub _get_next_token ($) { Line 436  sub _get_next_token ($) {
436                    $self->{next_input_character} == -1) {                    $self->{next_input_character} == -1) {
437              $self->{next_input_character} = shift @next_char; # reconsume              $self->{next_input_character} = shift @next_char; # reconsume
438              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
439              $self->{state} = 'data';              $self->{state} = DATA_STATE;
440              !!!emit ({type => 'character', data => '</'});              !!!emit ({type => CHARACTER_TOKEN, data => '</'});
441              redo A;              redo A;
442            } else {            } else {
443              $self->{next_input_character} = shift @next_char;              $self->{next_input_character} = shift @next_char;
# Line 375  sub _get_next_token ($) { Line 447  sub _get_next_token ($) {
447          } else {          } else {
448            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
449            # next-input-character is already done            # next-input-character is already done
450            $self->{state} = 'data';            $self->{state} = DATA_STATE;
451            !!!emit ({type => 'character', data => '</'});            !!!emit ({type => CHARACTER_TOKEN, data => '</'});
452            redo A;            redo A;
453          }          }
454        }        }
455                
456        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_input_character} and
457            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_input_character} <= 0x005A) { # A..Z
458          $self->{current_token} = {type => 'end tag',          $self->{current_token} = {type => END_TAG_TOKEN,
459                            tag_name => chr ($self->{next_input_character} + 0x0020)};                            tag_name => chr ($self->{next_input_character} + 0x0020)};
460          $self->{state} = 'tag name';          $self->{state} = TAG_NAME_STATE;
461          !!!next-input-character;          !!!next-input-character;
462          redo A;          redo A;
463        } elsif (0x0061 <= $self->{next_input_character} and        } elsif (0x0061 <= $self->{next_input_character} and
464                 $self->{next_input_character} <= 0x007A) { # a..z                 $self->{next_input_character} <= 0x007A) { # a..z
465          $self->{current_token} = {type => 'end tag',          $self->{current_token} = {type => END_TAG_TOKEN,
466                            tag_name => chr ($self->{next_input_character})};                            tag_name => chr ($self->{next_input_character})};
467          $self->{state} = 'tag name';          $self->{state} = TAG_NAME_STATE;
468          !!!next-input-character;          !!!next-input-character;
469          redo A;          redo A;
470        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
471          !!!parse-error (type => 'empty end tag');          !!!parse-error (type => 'empty end tag');
472          $self->{state} = 'data';          $self->{state} = DATA_STATE;
473          !!!next-input-character;          !!!next-input-character;
474          redo A;          redo A;
475        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
476          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
477          $self->{state} = 'data';          $self->{state} = DATA_STATE;
478          # reconsume          # reconsume
479    
480          !!!emit ({type => 'character', data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</'});
481    
482          redo A;          redo A;
483        } else {        } else {
484          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
485          $self->{state} = 'bogus comment';          $self->{state} = BOGUS_COMMENT_STATE;
486          ## $self->{next_input_character} is intentionally left as is          ## $self->{next_input_character} is intentionally left as is
487          redo A;          redo A;
488        }        }
489      } elsif ($self->{state} eq 'tag name') {      } elsif ($self->{state} == TAG_NAME_STATE) {
490        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
491            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
492            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
493            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
494            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
495          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
496          !!!next-input-character;          !!!next-input-character;
497          redo A;          redo A;
498        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
499          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
500            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
501                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
502            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
503          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
504            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
505            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
506              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
507            }            }
508          } else {          } else {
509            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
510          }          }
511          $self->{state} = 'data';          $self->{state} = DATA_STATE;
512          !!!next-input-character;          !!!next-input-character;
513    
514          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 451  sub _get_next_token ($) { Line 523  sub _get_next_token ($) {
523          redo A;          redo A;
524        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
525          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
526          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
527            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
528                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
529            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
530          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
531            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
532            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
533              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
534            }            }
535          } else {          } else {
536            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
537          }          }
538          $self->{state} = 'data';          $self->{state} = DATA_STATE;
539          # reconsume          # reconsume
540    
541          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 472  sub _get_next_token ($) { Line 544  sub _get_next_token ($) {
544        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
545          !!!next-input-character;          !!!next-input-character;
546          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
547              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
548              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
549            # permitted slash            # permitted slash
550            #            #
551          } else {          } else {
552            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
553          }          }
554          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
555          # next-input-character is already done          # next-input-character is already done
556          redo A;          redo A;
557        } else {        } else {
# Line 489  sub _get_next_token ($) { Line 561  sub _get_next_token ($) {
561          !!!next-input-character;          !!!next-input-character;
562          redo A;          redo A;
563        }        }
564      } elsif ($self->{state} eq 'before attribute name') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
565        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
566            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
567            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 499  sub _get_next_token ($) { Line 571  sub _get_next_token ($) {
571          !!!next-input-character;          !!!next-input-character;
572          redo A;          redo A;
573        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
574          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
575            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
576                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
577            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
578          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
579            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
580            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
581              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
582            }            }
583          } else {          } else {
584            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
585          }          }
586          $self->{state} = 'data';          $self->{state} = DATA_STATE;
587          !!!next-input-character;          !!!next-input-character;
588    
589          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 521  sub _get_next_token ($) { Line 593  sub _get_next_token ($) {
593                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_input_character} <= 0x005A) { # A..Z
594          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),
595                                value => ''};                                value => ''};
596          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
597          !!!next-input-character;          !!!next-input-character;
598          redo A;          redo A;
599        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
600          !!!next-input-character;          !!!next-input-character;
601          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
602              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
603              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
604            # permitted slash            # permitted slash
605            #            #
# Line 539  sub _get_next_token ($) { Line 611  sub _get_next_token ($) {
611          redo A;          redo A;
612        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
613          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
614          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
615            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
616                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
617            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
618          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
619            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
620            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
621              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
622            }            }
623          } else {          } else {
624            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
625          }          }
626          $self->{state} = 'data';          $self->{state} = DATA_STATE;
627          # reconsume          # reconsume
628    
629          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 560  sub _get_next_token ($) { Line 632  sub _get_next_token ($) {
632        } else {        } else {
633          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          $self->{current_attribute} = {name => chr ($self->{next_input_character}),
634                                value => ''};                                value => ''};
635          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
636          !!!next-input-character;          !!!next-input-character;
637          redo A;          redo A;
638        }        }
639      } elsif ($self->{state} eq 'attribute name') {      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {
640        my $before_leave = sub {        my $before_leave = sub {
641          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
642              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
643            !!!parse-error (type => 'dupulicate attribute');            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});
644            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
645          } else {          } else {
646            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
# Line 582  sub _get_next_token ($) { Line 654  sub _get_next_token ($) {
654            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
655            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
656          $before_leave->();          $before_leave->();
657          $self->{state} = 'after attribute name';          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
658          !!!next-input-character;          !!!next-input-character;
659          redo A;          redo A;
660        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_input_character} == 0x003D) { # =
661          $before_leave->();          $before_leave->();
662          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
663          !!!next-input-character;          !!!next-input-character;
664          redo A;          redo A;
665        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
666          $before_leave->();          $before_leave->();
667          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
668            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
669                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
670            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
671          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
672            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
673            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
674              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
675            }            }
676          } else {          } else {
677            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
678          }          }
679          $self->{state} = 'data';          $self->{state} = DATA_STATE;
680          !!!next-input-character;          !!!next-input-character;
681    
682          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 620  sub _get_next_token ($) { Line 692  sub _get_next_token ($) {
692          $before_leave->();          $before_leave->();
693          !!!next-input-character;          !!!next-input-character;
694          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
695              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
696              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
697            # permitted slash            # permitted slash
698            #            #
699          } else {          } else {
700            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
701          }          }
702          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
703          # next-input-character is already done          # next-input-character is already done
704          redo A;          redo A;
705        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
706          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
707          $before_leave->();          $before_leave->();
708          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
709            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
710                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
711            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
712          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
713            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
714            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
715              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
716            }            }
717          } else {          } else {
718            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
719          }          }
720          $self->{state} = 'data';          $self->{state} = DATA_STATE;
721          # reconsume          # reconsume
722    
723          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 657  sub _get_next_token ($) { Line 729  sub _get_next_token ($) {
729          !!!next-input-character;          !!!next-input-character;
730          redo A;          redo A;
731        }        }
732      } elsif ($self->{state} eq 'after attribute name') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
733        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
734            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
735            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 667  sub _get_next_token ($) { Line 739  sub _get_next_token ($) {
739          !!!next-input-character;          !!!next-input-character;
740          redo A;          redo A;
741        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_input_character} == 0x003D) { # =
742          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
743          !!!next-input-character;          !!!next-input-character;
744          redo A;          redo A;
745        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
746          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
747            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
748                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
749            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
750          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
751            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
752            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
753              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
754            }            }
755          } else {          } else {
756            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
757          }          }
758          $self->{state} = 'data';          $self->{state} = DATA_STATE;
759          !!!next-input-character;          !!!next-input-character;
760    
761          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 693  sub _get_next_token ($) { Line 765  sub _get_next_token ($) {
765                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_input_character} <= 0x005A) { # A..Z
766          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),
767                                value => ''};                                value => ''};
768          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
769          !!!next-input-character;          !!!next-input-character;
770          redo A;          redo A;
771        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
772          !!!next-input-character;          !!!next-input-character;
773          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
774              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
775              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
776            # permitted slash            # permitted slash
777            #            #
# Line 707  sub _get_next_token ($) { Line 779  sub _get_next_token ($) {
779            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
780            ## TODO: Different error type for <aa / bb> than <aa/>            ## TODO: Different error type for <aa / bb> than <aa/>
781          }          }
782          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_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_input_character} == -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            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
789                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
790            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
791          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
792            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
793            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
794              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
795            }            }
796          } else {          } else {
797            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
798          }          }
799          $self->{state} = 'data';          $self->{state} = DATA_STATE;
800          # reconsume          # reconsume
801    
802          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 733  sub _get_next_token ($) { Line 805  sub _get_next_token ($) {
805        } else {        } else {
806          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          $self->{current_attribute} = {name => chr ($self->{next_input_character}),
807                                value => ''};                                value => ''};
808          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
809          !!!next-input-character;          !!!next-input-character;
810          redo A;                  redo A;        
811        }        }
812      } elsif ($self->{state} eq 'before attribute value') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
813        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
814            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
815            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 747  sub _get_next_token ($) { Line 819  sub _get_next_token ($) {
819          !!!next-input-character;          !!!next-input-character;
820          redo A;          redo A;
821        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_input_character} == 0x0022) { # "
822          $self->{state} = 'attribute value (double-quoted)';          $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
823          !!!next-input-character;          !!!next-input-character;
824          redo A;          redo A;
825        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
826          $self->{state} = 'attribute value (unquoted)';          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
827          ## reconsume          ## reconsume
828          redo A;          redo A;
829        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_input_character} == 0x0027) { # '
830          $self->{state} = 'attribute value (single-quoted)';          $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
831          !!!next-input-character;          !!!next-input-character;
832          redo A;          redo A;
833        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
834          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
835            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
836                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
837            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
838          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
839            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
840            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
841              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
842            }            }
843          } else {          } else {
844            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
845          }          }
846          $self->{state} = 'data';          $self->{state} = DATA_STATE;
847          !!!next-input-character;          !!!next-input-character;
848    
849          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 779  sub _get_next_token ($) { Line 851  sub _get_next_token ($) {
851          redo A;          redo A;
852        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
853          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
854          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
855            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
856                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
857            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
858          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
859            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
860            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
861              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
862            }            }
863          } else {          } else {
864            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
865          }          }
866          $self->{state} = 'data';          $self->{state} = DATA_STATE;
867          ## reconsume          ## reconsume
868    
869          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 799  sub _get_next_token ($) { Line 871  sub _get_next_token ($) {
871          redo A;          redo A;
872        } else {        } else {
873          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});
874          $self->{state} = 'attribute value (unquoted)';          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
875          !!!next-input-character;          !!!next-input-character;
876          redo A;          redo A;
877        }        }
878      } elsif ($self->{state} eq 'attribute value (double-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
879        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_input_character} == 0x0022) { # "
880          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
881          !!!next-input-character;          !!!next-input-character;
882          redo A;          redo A;
883        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
884          $self->{last_attribute_value_state} = 'attribute value (double-quoted)';          $self->{last_attribute_value_state} = $self->{state};
885          $self->{state} = 'entity in attribute value';          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
886          !!!next-input-character;          !!!next-input-character;
887          redo A;          redo A;
888        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
889          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
890          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
891            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
892                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
893            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
894          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
895            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
896            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
897              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
898            }            }
899          } else {          } else {
900            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
901          }          }
902          $self->{state} = 'data';          $self->{state} = DATA_STATE;
903          ## reconsume          ## reconsume
904    
905          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 839  sub _get_next_token ($) { Line 911  sub _get_next_token ($) {
911          !!!next-input-character;          !!!next-input-character;
912          redo A;          redo A;
913        }        }
914      } elsif ($self->{state} eq 'attribute value (single-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
915        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_input_character} == 0x0027) { # '
916          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
917          !!!next-input-character;          !!!next-input-character;
918          redo A;          redo A;
919        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
920          $self->{last_attribute_value_state} = 'attribute value (single-quoted)';          $self->{last_attribute_value_state} = $self->{state};
921          $self->{state} = 'entity in attribute value';          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
922          !!!next-input-character;          !!!next-input-character;
923          redo A;          redo A;
924        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
925          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
926          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
927            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
928                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
929            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
930          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
931            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
932            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
933              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
934            }            }
935          } else {          } else {
936            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
937          }          }
938          $self->{state} = 'data';          $self->{state} = DATA_STATE;
939          ## reconsume          ## reconsume
940    
941          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 875  sub _get_next_token ($) { Line 947  sub _get_next_token ($) {
947          !!!next-input-character;          !!!next-input-character;
948          redo A;          redo A;
949        }        }
950      } elsif ($self->{state} eq 'attribute value (unquoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
951        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
952            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
953            $self->{next_input_character} == 0x000B or # HT            $self->{next_input_character} == 0x000B or # HT
954            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
955            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
956          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
957          !!!next-input-character;          !!!next-input-character;
958          redo A;          redo A;
959        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
960          $self->{last_attribute_value_state} = 'attribute value (unquoted)';          $self->{last_attribute_value_state} = $self->{state};
961          $self->{state} = 'entity in attribute value';          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
962          !!!next-input-character;          !!!next-input-character;
963          redo A;          redo A;
964        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
965          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
966            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
967                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
968            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
969          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
970            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
971            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
972              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
973            }            }
974          } else {          } else {
975            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
976          }          }
977          $self->{state} = 'data';          $self->{state} = DATA_STATE;
978          !!!next-input-character;          !!!next-input-character;
979    
980          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 910  sub _get_next_token ($) { Line 982  sub _get_next_token ($) {
982          redo A;          redo A;
983        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
984          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
985          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
986            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
987                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
988            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
989          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
990            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
991            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
992              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
993            }            }
994          } else {          } else {
995            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
996          }          }
997          $self->{state} = 'data';          $self->{state} = DATA_STATE;
998          ## reconsume          ## reconsume
999    
1000          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 934  sub _get_next_token ($) { Line 1006  sub _get_next_token ($) {
1006          !!!next-input-character;          !!!next-input-character;
1007          redo A;          redo A;
1008        }        }
1009      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {
1010        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);
1011    
1012        unless (defined $token) {        unless (defined $token) {
# Line 947  sub _get_next_token ($) { Line 1019  sub _get_next_token ($) {
1019        $self->{state} = $self->{last_attribute_value_state};        $self->{state} = $self->{last_attribute_value_state};
1020        # next-input-character is already done        # next-input-character is already done
1021        redo A;        redo A;
1022      } elsif ($self->{state} eq 'bogus comment') {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1023        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1024                
1025        my $token = {type => 'comment', data => ''};        my $token = {type => COMMENT_TOKEN, data => ''};
1026    
1027        BC: {        BC: {
1028          if ($self->{next_input_character} == 0x003E) { # >          if ($self->{next_input_character} == 0x003E) { # >
1029            $self->{state} = 'data';            $self->{state} = DATA_STATE;
1030            !!!next-input-character;            !!!next-input-character;
1031    
1032            !!!emit ($token);            !!!emit ($token);
1033    
1034            redo A;            redo A;
1035          } elsif ($self->{next_input_character} == -1) {          } elsif ($self->{next_input_character} == -1) {
1036            $self->{state} = 'data';            $self->{state} = DATA_STATE;
1037            ## reconsume            ## reconsume
1038    
1039            !!!emit ($token);            !!!emit ($token);
# Line 973  sub _get_next_token ($) { Line 1045  sub _get_next_token ($) {
1045            redo BC;            redo BC;
1046          }          }
1047        } # BC        } # BC
1048      } elsif ($self->{state} eq 'markup declaration open') {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1049        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1050    
1051        my @next_char;        my @next_char;
# Line 983  sub _get_next_token ($) { Line 1055  sub _get_next_token ($) {
1055          !!!next-input-character;          !!!next-input-character;
1056          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_input_character};
1057          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_input_character} == 0x002D) { # -
1058            $self->{current_token} = {type => 'comment', data => ''};            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};
1059            $self->{state} = 'comment start';            $self->{state} = COMMENT_START_STATE;
1060            !!!next-input-character;            !!!next-input-character;
1061            redo A;            redo A;
1062          }          }
# Line 1015  sub _get_next_token ($) { Line 1087  sub _get_next_token ($) {
1087                    if ($self->{next_input_character} == 0x0045 or # E                    if ($self->{next_input_character} == 0x0045 or # E
1088                        $self->{next_input_character} == 0x0065) { # e                        $self->{next_input_character} == 0x0065) { # e
1089                      ## ISSUE: What a stupid code this is!                      ## ISSUE: What a stupid code this is!
1090                      $self->{state} = 'DOCTYPE';                      $self->{state} = DOCTYPE_STATE;
1091                      !!!next-input-character;                      !!!next-input-character;
1092                      redo A;                      redo A;
1093                    }                    }
# Line 1029  sub _get_next_token ($) { Line 1101  sub _get_next_token ($) {
1101        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'bogus comment');
1102        $self->{next_input_character} = shift @next_char;        $self->{next_input_character} = shift @next_char;
1103        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
1104        $self->{state} = 'bogus comment';        $self->{state} = BOGUS_COMMENT_STATE;
1105        redo A;        redo A;
1106                
1107        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
1108        ## 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?
1109      } elsif ($self->{state} eq 'comment start') {      } elsif ($self->{state} == COMMENT_START_STATE) {
1110        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1111          $self->{state} = 'comment start dash';          $self->{state} = COMMENT_START_DASH_STATE;
1112          !!!next-input-character;          !!!next-input-character;
1113          redo A;          redo A;
1114        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1115          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
1116          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1117          !!!next-input-character;          !!!next-input-character;
1118    
1119          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1049  sub _get_next_token ($) { Line 1121  sub _get_next_token ($) {
1121          redo A;          redo A;
1122        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1123          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1124          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1125          ## reconsume          ## reconsume
1126    
1127          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1058  sub _get_next_token ($) { Line 1130  sub _get_next_token ($) {
1130        } else {        } else {
1131          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
1132              .= chr ($self->{next_input_character});              .= chr ($self->{next_input_character});
1133          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1134          !!!next-input-character;          !!!next-input-character;
1135          redo A;          redo A;
1136        }        }
1137      } elsif ($self->{state} eq 'comment start dash') {      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
1138        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1139          $self->{state} = 'comment end';          $self->{state} = COMMENT_END_STATE;
1140          !!!next-input-character;          !!!next-input-character;
1141          redo A;          redo A;
1142        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1143          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
1144          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1145          !!!next-input-character;          !!!next-input-character;
1146    
1147          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1077  sub _get_next_token ($) { Line 1149  sub _get_next_token ($) {
1149          redo A;          redo A;
1150        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1151          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1152          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1153          ## reconsume          ## reconsume
1154    
1155          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1086  sub _get_next_token ($) { Line 1158  sub _get_next_token ($) {
1158        } else {        } else {
1159          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
1160              .= '-' . chr ($self->{next_input_character});              .= '-' . chr ($self->{next_input_character});
1161          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1162          !!!next-input-character;          !!!next-input-character;
1163          redo A;          redo A;
1164        }        }
1165      } elsif ($self->{state} eq 'comment') {      } elsif ($self->{state} == COMMENT_STATE) {
1166        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1167          $self->{state} = 'comment end dash';          $self->{state} = COMMENT_END_DASH_STATE;
1168          !!!next-input-character;          !!!next-input-character;
1169          redo A;          redo A;
1170        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1171          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1172          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1173          ## reconsume          ## reconsume
1174    
1175          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1109  sub _get_next_token ($) { Line 1181  sub _get_next_token ($) {
1181          !!!next-input-character;          !!!next-input-character;
1182          redo A;          redo A;
1183        }        }
1184      } elsif ($self->{state} eq 'comment end dash') {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
1185        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1186          $self->{state} = 'comment end';          $self->{state} = COMMENT_END_STATE;
1187          !!!next-input-character;          !!!next-input-character;
1188          redo A;          redo A;
1189        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1190          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1191          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1192          ## reconsume          ## reconsume
1193    
1194          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1124  sub _get_next_token ($) { Line 1196  sub _get_next_token ($) {
1196          redo A;          redo A;
1197        } else {        } else {
1198          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment
1199          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1200          !!!next-input-character;          !!!next-input-character;
1201          redo A;          redo A;
1202        }        }
1203      } elsif ($self->{state} eq 'comment end') {      } elsif ($self->{state} == COMMENT_END_STATE) {
1204        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_input_character} == 0x003E) { # >
1205          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1206          !!!next-input-character;          !!!next-input-character;
1207    
1208          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1144  sub _get_next_token ($) { Line 1216  sub _get_next_token ($) {
1216          redo A;          redo A;
1217        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1218          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1219          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1220          ## reconsume          ## reconsume
1221    
1222          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1153  sub _get_next_token ($) { Line 1225  sub _get_next_token ($) {
1225        } else {        } else {
1226          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment');
1227          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment
1228          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1229          !!!next-input-character;          !!!next-input-character;
1230          redo A;          redo A;
1231        }        }
1232      } elsif ($self->{state} eq 'DOCTYPE') {      } elsif ($self->{state} == DOCTYPE_STATE) {
1233        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1234            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1235            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
1236            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1237            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
1238          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1239          !!!next-input-character;          !!!next-input-character;
1240          redo A;          redo A;
1241        } else {        } else {
1242          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
1243          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1244          ## reconsume          ## reconsume
1245          redo A;          redo A;
1246        }        }
1247      } elsif ($self->{state} eq 'before DOCTYPE name') {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
1248        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1249            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1250            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 1183  sub _get_next_token ($) { Line 1255  sub _get_next_token ($) {
1255          redo A;          redo A;
1256        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1257          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1258          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1259          !!!next-input-character;          !!!next-input-character;
1260    
1261          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect
1262    
1263          redo A;          redo A;
1264        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1265          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1266          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1267          ## reconsume          ## reconsume
1268    
1269          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect
1270    
1271          redo A;          redo A;
1272        } else {        } else {
1273          $self->{current_token}          $self->{current_token}
1274              = {type => 'DOCTYPE',              = {type => DOCTYPE_TOKEN,
1275                 name => chr ($self->{next_input_character}),                 name => chr ($self->{next_input_character}),
1276                 correct => 1};                 correct => 1};
1277  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
1278          $self->{state} = 'DOCTYPE name';          $self->{state} = DOCTYPE_NAME_STATE;
1279          !!!next-input-character;          !!!next-input-character;
1280          redo A;          redo A;
1281        }        }
1282      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
1283  ## ISSUE: Redundant "First," in the spec.  ## ISSUE: Redundant "First," in the spec.
1284        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1285            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1286            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
1287            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1288            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
1289          $self->{state} = 'after DOCTYPE name';          $self->{state} = AFTER_DOCTYPE_NAME_STATE;
1290          !!!next-input-character;          !!!next-input-character;
1291          redo A;          redo A;
1292        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1293          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1294          !!!next-input-character;          !!!next-input-character;
1295    
1296          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1226  sub _get_next_token ($) { Line 1298  sub _get_next_token ($) {
1298          redo A;          redo A;
1299        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1300          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1301          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1302          ## reconsume          ## reconsume
1303    
1304          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1240  sub _get_next_token ($) { Line 1312  sub _get_next_token ($) {
1312          !!!next-input-character;          !!!next-input-character;
1313          redo A;          redo A;
1314        }        }
1315      } elsif ($self->{state} eq 'after DOCTYPE name') {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
1316        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1317            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1318            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 1250  sub _get_next_token ($) { Line 1322  sub _get_next_token ($) {
1322          !!!next-input-character;          !!!next-input-character;
1323          redo A;          redo A;
1324        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1325          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1326          !!!next-input-character;          !!!next-input-character;
1327    
1328          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1258  sub _get_next_token ($) { Line 1330  sub _get_next_token ($) {
1330          redo A;          redo A;
1331        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1332          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1333          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1334          ## reconsume          ## reconsume
1335    
1336          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1282  sub _get_next_token ($) { Line 1354  sub _get_next_token ($) {
1354                  !!!next-input-character;                  !!!next-input-character;
1355                  if ($self->{next_input_character} == 0x0043 or # C                  if ($self->{next_input_character} == 0x0043 or # C
1356                      $self->{next_input_character} == 0x0063) { # c                      $self->{next_input_character} == 0x0063) { # c
1357                    $self->{state} = 'before DOCTYPE public identifier';                    $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1358                    !!!next-input-character;                    !!!next-input-character;
1359                    redo A;                    redo A;
1360                  }                  }
# Line 1309  sub _get_next_token ($) { Line 1381  sub _get_next_token ($) {
1381                  !!!next-input-character;                  !!!next-input-character;
1382                  if ($self->{next_input_character} == 0x004D or # M                  if ($self->{next_input_character} == 0x004D or # M
1383                      $self->{next_input_character} == 0x006D) { # m                      $self->{next_input_character} == 0x006D) { # m
1384                    $self->{state} = 'before DOCTYPE system identifier';                    $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1385                    !!!next-input-character;                    !!!next-input-character;
1386                    redo A;                    redo A;
1387                  }                  }
# Line 1325  sub _get_next_token ($) { Line 1397  sub _get_next_token ($) {
1397        }        }
1398    
1399        !!!parse-error (type => 'string after DOCTYPE name');        !!!parse-error (type => 'string after DOCTYPE name');
1400        $self->{state} = 'bogus DOCTYPE';        $self->{state} = BOGUS_DOCTYPE_STATE;
1401        # next-input-character is already done        # next-input-character is already done
1402        redo A;        redo A;
1403      } elsif ($self->{state} eq 'before DOCTYPE public identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
1404        if ({        if ({
1405              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1406              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1338  sub _get_next_token ($) { Line 1410  sub _get_next_token ($) {
1410          redo A;          redo A;
1411        } elsif ($self->{next_input_character} eq 0x0022) { # "        } elsif ($self->{next_input_character} eq 0x0022) { # "
1412          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1413          $self->{state} = 'DOCTYPE public identifier (double-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
1414          !!!next-input-character;          !!!next-input-character;
1415          redo A;          redo A;
1416        } elsif ($self->{next_input_character} eq 0x0027) { # '        } elsif ($self->{next_input_character} eq 0x0027) { # '
1417          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1418          $self->{state} = 'DOCTYPE public identifier (single-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
1419          !!!next-input-character;          !!!next-input-character;
1420          redo A;          redo A;
1421        } elsif ($self->{next_input_character} eq 0x003E) { # >        } elsif ($self->{next_input_character} eq 0x003E) { # >
1422          !!!parse-error (type => 'no PUBLIC literal');          !!!parse-error (type => 'no PUBLIC literal');
1423    
1424          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1425          !!!next-input-character;          !!!next-input-character;
1426    
1427          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1359  sub _get_next_token ($) { Line 1431  sub _get_next_token ($) {
1431        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1432          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1433    
1434          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1435          ## reconsume          ## reconsume
1436    
1437          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1368  sub _get_next_token ($) { Line 1440  sub _get_next_token ($) {
1440          redo A;          redo A;
1441        } else {        } else {
1442          !!!parse-error (type => 'string after PUBLIC');          !!!parse-error (type => 'string after PUBLIC');
1443          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1444          !!!next-input-character;          !!!next-input-character;
1445          redo A;          redo A;
1446        }        }
1447      } elsif ($self->{state} eq 'DOCTYPE public identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
1448        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_input_character} == 0x0022) { # "
1449          $self->{state} = 'after DOCTYPE public identifier';          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1450          !!!next-input-character;          !!!next-input-character;
1451          redo A;          redo A;
1452        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1453          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
1454    
1455          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1456          ## reconsume          ## reconsume
1457    
1458          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1394  sub _get_next_token ($) { Line 1466  sub _get_next_token ($) {
1466          !!!next-input-character;          !!!next-input-character;
1467          redo A;          redo A;
1468        }        }
1469      } elsif ($self->{state} eq 'DOCTYPE public identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
1470        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_input_character} == 0x0027) { # '
1471          $self->{state} = 'after DOCTYPE public identifier';          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1472          !!!next-input-character;          !!!next-input-character;
1473          redo A;          redo A;
1474        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1475          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
1476    
1477          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1478          ## reconsume          ## reconsume
1479    
1480          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1416  sub _get_next_token ($) { Line 1488  sub _get_next_token ($) {
1488          !!!next-input-character;          !!!next-input-character;
1489          redo A;          redo A;
1490        }        }
1491      } elsif ($self->{state} eq 'after DOCTYPE public identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
1492        if ({        if ({
1493              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1494              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1426  sub _get_next_token ($) { Line 1498  sub _get_next_token ($) {
1498          redo A;          redo A;
1499        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_input_character} == 0x0022) { # "
1500          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1501          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
1502          !!!next-input-character;          !!!next-input-character;
1503          redo A;          redo A;
1504        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_input_character} == 0x0027) { # '
1505          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1506          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
1507          !!!next-input-character;          !!!next-input-character;
1508          redo A;          redo A;
1509        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1510          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1511          !!!next-input-character;          !!!next-input-character;
1512    
1513          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1444  sub _get_next_token ($) { Line 1516  sub _get_next_token ($) {
1516        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1517          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1518    
1519          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1520          ## reconsume          ## reconsume
1521    
1522          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1453  sub _get_next_token ($) { Line 1525  sub _get_next_token ($) {
1525          redo A;          redo A;
1526        } else {        } else {
1527          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after PUBLIC literal');
1528          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1529          !!!next-input-character;          !!!next-input-character;
1530          redo A;          redo A;
1531        }        }
1532      } elsif ($self->{state} eq 'before DOCTYPE system identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
1533        if ({        if ({
1534              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1535              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1467  sub _get_next_token ($) { Line 1539  sub _get_next_token ($) {
1539          redo A;          redo A;
1540        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_input_character} == 0x0022) { # "
1541          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1542          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
1543          !!!next-input-character;          !!!next-input-character;
1544          redo A;          redo A;
1545        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_input_character} == 0x0027) { # '
1546          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1547          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
1548          !!!next-input-character;          !!!next-input-character;
1549          redo A;          redo A;
1550        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1551          !!!parse-error (type => 'no SYSTEM literal');          !!!parse-error (type => 'no SYSTEM literal');
1552          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1553          !!!next-input-character;          !!!next-input-character;
1554    
1555          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1487  sub _get_next_token ($) { Line 1559  sub _get_next_token ($) {
1559        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1560          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1561    
1562          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1563          ## reconsume          ## reconsume
1564    
1565          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1496  sub _get_next_token ($) { Line 1568  sub _get_next_token ($) {
1568          redo A;          redo A;
1569        } else {        } else {
1570          !!!parse-error (type => 'string after SYSTEM');          !!!parse-error (type => 'string after SYSTEM');
1571          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1572          !!!next-input-character;          !!!next-input-character;
1573          redo A;          redo A;
1574        }        }
1575      } elsif ($self->{state} eq 'DOCTYPE system identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
1576        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_input_character} == 0x0022) { # "
1577          $self->{state} = 'after DOCTYPE system identifier';          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1578          !!!next-input-character;          !!!next-input-character;
1579          redo A;          redo A;
1580        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1581          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
1582    
1583          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1584          ## reconsume          ## reconsume
1585    
1586          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1522  sub _get_next_token ($) { Line 1594  sub _get_next_token ($) {
1594          !!!next-input-character;          !!!next-input-character;
1595          redo A;          redo A;
1596        }        }
1597      } elsif ($self->{state} eq 'DOCTYPE system identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
1598        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_input_character} == 0x0027) { # '
1599          $self->{state} = 'after DOCTYPE system identifier';          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1600          !!!next-input-character;          !!!next-input-character;
1601          redo A;          redo A;
1602        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1603          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
1604    
1605          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1606          ## reconsume          ## reconsume
1607    
1608          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1544  sub _get_next_token ($) { Line 1616  sub _get_next_token ($) {
1616          !!!next-input-character;          !!!next-input-character;
1617          redo A;          redo A;
1618        }        }
1619      } elsif ($self->{state} eq 'after DOCTYPE system identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
1620        if ({        if ({
1621              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1622              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1553  sub _get_next_token ($) { Line 1625  sub _get_next_token ($) {
1625          !!!next-input-character;          !!!next-input-character;
1626          redo A;          redo A;
1627        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1628          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1629          !!!next-input-character;          !!!next-input-character;
1630    
1631          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1562  sub _get_next_token ($) { Line 1634  sub _get_next_token ($) {
1634        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1635          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1636    
1637          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1638          ## reconsume          ## reconsume
1639    
1640          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1571  sub _get_next_token ($) { Line 1643  sub _get_next_token ($) {
1643          redo A;          redo A;
1644        } else {        } else {
1645          !!!parse-error (type => 'string after SYSTEM literal');          !!!parse-error (type => 'string after SYSTEM literal');
1646          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1647          !!!next-input-character;          !!!next-input-character;
1648          redo A;          redo A;
1649        }        }
1650      } elsif ($self->{state} eq 'bogus DOCTYPE') {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
1651        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_input_character} == 0x003E) { # >
1652          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1653          !!!next-input-character;          !!!next-input-character;
1654    
1655          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1586  sub _get_next_token ($) { Line 1658  sub _get_next_token ($) {
1658          redo A;          redo A;
1659        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1660          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1661          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1662          ## reconsume          ## reconsume
1663    
1664          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1644  sub _tokenize_attempt_to_consume_an_enti Line 1716  sub _tokenize_attempt_to_consume_an_enti
1716            redo X;            redo X;
1717          } elsif (not defined $code) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
1718            !!!parse-error (type => 'bare hcro');            !!!parse-error (type => 'bare hcro');
1719              !!!back-next-input-character ($x_char, $self->{next_input_character});
1720            $self->{next_input_character} = 0x0023; # #            $self->{next_input_character} = 0x0023; # #
           !!!back-next-input-character ($x_char);  
1721            return undef;            return undef;
1722          } elsif ($self->{next_input_character} == 0x003B) { # ;          } elsif ($self->{next_input_character} == 0x003B) { # ;
1723            !!!next-input-character;            !!!next-input-character;
# Line 1667  sub _tokenize_attempt_to_consume_an_enti Line 1739  sub _tokenize_attempt_to_consume_an_enti
1739            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
1740          }          }
1741    
1742          return {type => 'character', data => chr $code};          return {type => CHARACTER_TOKEN, data => chr $code};
1743        } # X        } # X
1744      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_input_character} and
1745               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_input_character} <= 0x0039) { # 0..9
# Line 1702  sub _tokenize_attempt_to_consume_an_enti Line 1774  sub _tokenize_attempt_to_consume_an_enti
1774          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
1775        }        }
1776                
1777        return {type => 'character', data => chr $code};        return {type => CHARACTER_TOKEN, data => chr $code};
1778      } else {      } else {
1779        !!!parse-error (type => 'bare nero');        !!!parse-error (type => 'bare nero');
1780        !!!back-next-input-character ($self->{next_input_character});        !!!back-next-input-character ($self->{next_input_character});
# Line 1717  sub _tokenize_attempt_to_consume_an_enti Line 1789  sub _tokenize_attempt_to_consume_an_enti
1789      !!!next-input-character;      !!!next-input-character;
1790    
1791      my $value = $entity_name;      my $value = $entity_name;
1792      my $match;      my $match = 0;
1793      require Whatpm::_NamedEntityList;      require Whatpm::_NamedEntityList;
1794      our $EntityChar;      our $EntityChar;
1795    
# Line 1737  sub _tokenize_attempt_to_consume_an_enti Line 1809  sub _tokenize_attempt_to_consume_an_enti
1809            $match = 1;            $match = 1;
1810            !!!next-input-character;            !!!next-input-character;
1811            last;            last;
1812          } elsif (not $in_attr) {          } else {
1813            $value = $EntityChar->{$entity_name};            $value = $EntityChar->{$entity_name};
1814            $match = -1;            $match = -1;
1815          } else {            !!!next-input-character;
           $value .= chr $self->{next_input_character};  
1816          }          }
1817        } else {        } else {
1818          $value .= chr $self->{next_input_character};          $value .= chr $self->{next_input_character};
1819            $match *= 2;
1820            !!!next-input-character;
1821        }        }
       !!!next-input-character;  
1822      }      }
1823            
1824      if ($match > 0) {      if ($match > 0) {
1825        return {type => 'character', data => $value};        return {type => CHARACTER_TOKEN, data => $value};
1826      } elsif ($match < 0) {      } elsif ($match < 0) {
1827        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc');
1828        return {type => 'character', data => $value};        if ($in_attr and $match < -1) {
1829            return {type => CHARACTER_TOKEN, data => '&'.$entity_name};
1830          } else {
1831            return {type => CHARACTER_TOKEN, data => $value};
1832          }
1833      } else {      } else {
1834        !!!parse-error (type => 'bare ero');        !!!parse-error (type => 'bare ero');
1835        ## NOTE: No characters are consumed in the spec.        ## NOTE: No characters are consumed in the spec.
1836        return {type => 'character', data => '&'.$value};        return {type => CHARACTER_TOKEN, data => '&'.$value};
1837      }      }
1838    } else {    } else {
1839      ## no characters are consumed      ## no characters are consumed
# Line 1799  sub _construct_tree ($) { Line 1875  sub _construct_tree ($) {
1875        
1876    !!!next-token;    !!!next-token;
1877    
1878    $self->{insertion_mode} = 'before head';    $self->{insertion_mode} = BEFORE_HEAD_IM;
1879    undef $self->{form_element};    undef $self->{form_element};
1880    undef $self->{head_element};    undef $self->{head_element};
1881    $self->{open_elements} = [];    $self->{open_elements} = [];
# Line 1813  sub _construct_tree ($) { Line 1889  sub _construct_tree ($) {
1889  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
1890    my $self = shift;    my $self = shift;
1891    INITIAL: {    INITIAL: {
1892      if ($token->{type} eq 'DOCTYPE') {      if ($token->{type} == DOCTYPE_TOKEN) {
1893        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
1894        ## error, switch to a conformance checking mode for another        ## error, switch to a conformance checking mode for another
1895        ## language.        ## language.
# Line 1940  sub _tree_construction_initial ($) { Line 2016  sub _tree_construction_initial ($) {
2016        !!!next-token;        !!!next-token;
2017        return;        return;
2018      } elsif ({      } elsif ({
2019                'start tag' => 1,                START_TAG_TOKEN, 1,
2020                'end tag' => 1,                END_TAG_TOKEN, 1,
2021                'end-of-file' => 1,                END_OF_FILE_TOKEN, 1,
2022               }->{$token->{type}}) {               }->{$token->{type}}) {
2023        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE');
2024        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
2025        ## Go to the root element phase        ## Go to the root element phase
2026        ## reprocess        ## reprocess
2027        return;        return;
2028      } elsif ($token->{type} eq 'character') {      } elsif ($token->{type} == CHARACTER_TOKEN) {
2029        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2030          ## Ignore the token          ## Ignore the token
2031    
# Line 1965  sub _tree_construction_initial ($) { Line 2041  sub _tree_construction_initial ($) {
2041        ## Go to the root element phase        ## Go to the root element phase
2042        ## reprocess        ## reprocess
2043        return;        return;
2044      } elsif ($token->{type} eq 'comment') {      } elsif ($token->{type} == COMMENT_TOKEN) {
2045        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
2046        $self->{document}->append_child ($comment);        $self->{document}->append_child ($comment);
2047                
# Line 1973  sub _tree_construction_initial ($) { Line 2049  sub _tree_construction_initial ($) {
2049        !!!next-token;        !!!next-token;
2050        redo INITIAL;        redo INITIAL;
2051      } else {      } else {
2052        die "$0: $token->{type}: Unknown token";        die "$0: $token->{type}: Unknown token type";
2053      }      }
2054    } # INITIAL    } # INITIAL
2055  } # _tree_construction_initial  } # _tree_construction_initial
# Line 1982  sub _tree_construction_root_element ($) Line 2058  sub _tree_construction_root_element ($)
2058    my $self = shift;    my $self = shift;
2059        
2060    B: {    B: {
2061        if ($token->{type} eq 'DOCTYPE') {        if ($token->{type} == DOCTYPE_TOKEN) {
2062          !!!parse-error (type => 'in html:#DOCTYPE');          !!!parse-error (type => 'in html:#DOCTYPE');
2063          ## Ignore the token          ## Ignore the token
2064          ## Stay in the phase          ## Stay in the phase
2065          !!!next-token;          !!!next-token;
2066          redo B;          redo B;
2067        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{type} == COMMENT_TOKEN) {
2068          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
2069          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
2070          ## Stay in the phase          ## Stay in the phase
2071          !!!next-token;          !!!next-token;
2072          redo B;          redo B;
2073        } elsif ($token->{type} eq 'character') {        } elsif ($token->{type} == CHARACTER_TOKEN) {
2074          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2075            ## Ignore the token.            ## Ignore the token.
2076    
# Line 2006  sub _tree_construction_root_element ($) Line 2082  sub _tree_construction_root_element ($)
2082          }          }
2083          #          #
2084        } elsif ({        } elsif ({
2085                  'start tag' => 1,                  START_TAG_TOKEN, 1,
2086                  'end tag' => 1,                  END_TAG_TOKEN, 1,
2087                  'end-of-file' => 1,                  END_OF_FILE_TOKEN, 1,
2088                 }->{$token->{type}}) {                 }->{$token->{type}}) {
2089          ## ISSUE: There is an issue in the spec          ## ISSUE: There is an issue in the spec
2090          #          #
2091        } else {        } else {
2092          die "$0: $token->{type}: Unknown token";          die "$0: $token->{type}: Unknown token type";
2093        }        }
2094        my $root_element; !!!create-element ($root_element, 'html');        my $root_element; !!!create-element ($root_element, 'html');
2095        $self->{document}->append_child ($root_element);        $self->{document}->append_child ($root_element);
# Line 2055  sub _reset_insertion_mode ($) { Line 2131  sub _reset_insertion_mode ($) {
2131            
2132        ## Step 4..13        ## Step 4..13
2133        my $new_mode = {        my $new_mode = {
2134                        select => 'in select',                        select => IN_SELECT_IM,
2135                        td => 'in cell',                        td => IN_CELL_IM,
2136                        th => 'in cell',                        th => IN_CELL_IM,
2137                        tr => 'in row',                        tr => IN_ROW_IM,
2138                        tbody => 'in table body',                        tbody => IN_TABLE_BODY_IM,
2139                        thead => 'in table head',                        thead => IN_TABLE_BODY_IM,
2140                        tfoot => 'in table foot',                        tfoot => IN_TABLE_BODY_IM,
2141                        caption => 'in caption',                        caption => IN_CAPTION_IM,
2142                        colgroup => 'in column group',                        colgroup => IN_COLUMN_GROUP_IM,
2143                        table => 'in table',                        table => IN_TABLE_IM,
2144                        head => 'in body', # not in head!                        head => IN_BODY_IM, # not in head!
2145                        body => 'in body',                        body => IN_BODY_IM,
2146                        frameset => 'in frameset',                        frameset => IN_FRAMESET_IM,
2147                       }->{$node->[1]};                       }->{$node->[1]};
2148        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
2149                
2150        ## Step 14        ## Step 14
2151        if ($node->[1] eq 'html') {        if ($node->[1] eq 'html') {
2152          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
2153            $self->{insertion_mode} = 'before head';            $self->{insertion_mode} = BEFORE_HEAD_IM;
2154          } else {          } else {
2155            $self->{insertion_mode} = 'after head';            $self->{insertion_mode} = AFTER_HEAD_IM;
2156          }          }
2157          return;          return;
2158        }        }
2159                
2160        ## Step 15        ## Step 15
2161        $self->{insertion_mode} = 'in body' and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
2162                
2163        ## Step 16        ## Step 16
2164        $i--;        $i--;
# Line 2096  sub _reset_insertion_mode ($) { Line 2172  sub _reset_insertion_mode ($) {
2172  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
2173    my $self = shift;    my $self = shift;
2174    
   my $previous_insertion_mode;  
   
2175    my $active_formatting_elements = [];    my $active_formatting_elements = [];
2176    
2177    my $reconstruct_active_formatting_elements = sub { # MUST    my $reconstruct_active_formatting_elements = sub { # MUST
# Line 2192  sub _tree_construction_main ($) { Line 2266  sub _tree_construction_main ($) {
2266      $insert->($el); # /context node/->append_child ($el)      $insert->($el); # /context node/->append_child ($el)
2267    
2268      ## Step 3      ## Step 3
2269      $self->{content_model_flag} = $content_model_flag; # CDATA or RCDATA      $self->{content_model} = $content_model_flag; # CDATA or RCDATA
2270      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
2271    
2272      ## Step 4      ## Step 4
2273      my $text = '';      my $text = '';
2274      !!!next-token;      !!!next-token;
2275      while ($token->{type} eq 'character') { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
2276        $text .= $token->{data};        $text .= $token->{data};
2277        !!!next-token;        !!!next-token;
2278      }      }
# Line 2210  sub _tree_construction_main ($) { Line 2284  sub _tree_construction_main ($) {
2284      }      }
2285    
2286      ## Step 6      ## Step 6
2287      $self->{content_model_flag} = 'PCDATA';      $self->{content_model} = PCDATA_CONTENT_MODEL;
2288    
2289      ## Step 7      ## Step 7
2290      if ($token->{type} eq 'end tag' and $token->{tag_name} eq $start_tag_name) {      if ($token->{type} == END_TAG_TOKEN and $token->{tag_name} eq $start_tag_name) {
2291        ## Ignore the token        ## Ignore the token
2292        } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {
2293          !!!parse-error (type => 'in CDATA:#'.$token->{type});
2294        } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
2295          !!!parse-error (type => 'in RCDATA:#'.$token->{type});
2296      } else {      } else {
2297        !!!parse-error (type => 'in '.$content_model_flag.':#'.$token->{type});        die "$0: $content_model_flag in parse_rcdata";
2298      }      }
2299      !!!next-token;      !!!next-token;
2300    }; # $parse_rcdata    }; # $parse_rcdata
# Line 2227  sub _tree_construction_main ($) { Line 2305  sub _tree_construction_main ($) {
2305      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, 'script', $token->{attributes});
2306      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
2307    
2308      $self->{content_model_flag} = 'CDATA';      $self->{content_model} = CDATA_CONTENT_MODEL;
2309      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
2310            
2311      my $text = '';      my $text = '';
2312      !!!next-token;      !!!next-token;
2313      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) {
2314        $text .= $token->{data};        $text .= $token->{data};
2315        !!!next-token;        !!!next-token;
2316      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
# Line 2240  sub _tree_construction_main ($) { Line 2318  sub _tree_construction_main ($) {
2318        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
2319      }      }
2320                                
2321      $self->{content_model_flag} = 'PCDATA';      $self->{content_model} = PCDATA_CONTENT_MODEL;
2322    
2323      if ($token->{type} eq 'end tag' and      if ($token->{type} == END_TAG_TOKEN and
2324          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
2325        ## Ignore the token        ## Ignore the token
2326      } else {      } else {
# Line 2485  sub _tree_construction_main ($) { Line 2563  sub _tree_construction_main ($) {
2563                         }                         }
2564    }; # $insert_to_foster    }; # $insert_to_foster
2565    
2566    my $in_body = sub {    my $insert;
2567      my $insert = shift;  
2568      if ($token->{type} eq 'start tag') {    B: {
2569        if ($token->{type} == DOCTYPE_TOKEN) {
2570          !!!parse-error (type => 'DOCTYPE in the middle');
2571          ## Ignore the token
2572          ## Stay in the phase
2573          !!!next-token;
2574          redo B;
2575        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
2576          if ($self->{insertion_mode} & AFTER_HTML_IMS) {
2577            #
2578          } else {
2579            ## Generate implied end tags
2580            if ({
2581                 dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,
2582                 tbody => 1, tfoot=> 1, thead => 1,
2583                }->{$self->{open_elements}->[-1]->[1]}) {
2584              !!!back-token;
2585              $token = {type => END_TAG_TOKEN, tag_name => $self->{open_elements}->[-1]->[1]};
2586              redo B;
2587            }
2588            
2589            if (@{$self->{open_elements}} > 2 or
2590                (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {
2591              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
2592            } elsif (defined $self->{inner_html_node} and
2593                     @{$self->{open_elements}} > 1 and
2594                     $self->{open_elements}->[1]->[1] ne 'body') {
2595              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
2596            }
2597    
2598            ## ISSUE: There is an issue in the spec.
2599          }
2600    
2601          ## Stop parsing
2602          last B;
2603        } elsif ($token->{type} == START_TAG_TOKEN and
2604                 $token->{tag_name} eq 'html') {
2605          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
2606            ## Turn into the main phase
2607            !!!parse-error (type => 'after html:html');
2608            $self->{insertion_mode} = AFTER_BODY_IM;
2609          } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
2610            ## Turn into the main phase
2611            !!!parse-error (type => 'after html:html');
2612            $self->{insertion_mode} = AFTER_FRAMESET_IM;
2613          }
2614    
2615    ## ISSUE: "aa<html>" is not a parse error.
2616    ## ISSUE: "<html>" in fragment is not a parse error.
2617          unless ($token->{first_start_tag}) {
2618            !!!parse-error (type => 'not first start tag');
2619          }
2620          my $top_el = $self->{open_elements}->[0]->[0];
2621          for my $attr_name (keys %{$token->{attributes}}) {
2622            unless ($top_el->has_attribute_ns (undef, $attr_name)) {
2623              $top_el->set_attribute_ns
2624                (undef, [undef, $attr_name],
2625                 $token->{attributes}->{$attr_name}->{value});
2626            }
2627          }
2628          !!!next-token;
2629          redo B;
2630        } elsif ($token->{type} == COMMENT_TOKEN) {
2631          my $comment = $self->{document}->create_comment ($token->{data});
2632          if ($self->{insertion_mode} & AFTER_HTML_IMS) {
2633            $self->{document}->append_child ($comment);
2634          } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
2635            $self->{open_elements}->[0]->[0]->append_child ($comment);
2636          } else {
2637            $self->{open_elements}->[-1]->[0]->append_child ($comment);
2638          }
2639          !!!next-token;
2640          redo B;
2641        } elsif ($self->{insertion_mode} & HEAD_IMS) {
2642          if ($token->{type} == CHARACTER_TOKEN) {
2643            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
2644              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
2645              unless (length $token->{data}) {
2646                !!!next-token;
2647                redo B;
2648              }
2649            }
2650    
2651            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2652              ## As if <head>
2653              !!!create-element ($self->{head_element}, 'head');
2654              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2655              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2656    
2657              ## Reprocess in the "in head" insertion mode...
2658              pop @{$self->{open_elements}};
2659    
2660              ## Reprocess in the "after head" insertion mode...
2661            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2662              ## As if </noscript>
2663              pop @{$self->{open_elements}};
2664              !!!parse-error (type => 'in noscript:#character');
2665              
2666              ## Reprocess in the "in head" insertion mode...
2667              ## As if </head>
2668              pop @{$self->{open_elements}};
2669    
2670              ## Reprocess in the "after head" insertion mode...
2671            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2672              pop @{$self->{open_elements}};
2673    
2674              ## Reprocess in the "after head" insertion mode...
2675            }
2676    
2677                ## "after head" insertion mode
2678                ## As if <body>
2679                !!!insert-element ('body');
2680                $self->{insertion_mode} = IN_BODY_IM;
2681                ## reprocess
2682                redo B;
2683              } elsif ($token->{type} == START_TAG_TOKEN) {
2684                if ($token->{tag_name} eq 'head') {
2685                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2686                    !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});
2687                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2688                    push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];
2689                    $self->{insertion_mode} = IN_HEAD_IM;
2690                    !!!next-token;
2691                    redo B;
2692                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2693                    #
2694                  } else {
2695                    !!!parse-error (type => 'in head:head'); # or in head noscript
2696                    ## Ignore the token
2697                    !!!next-token;
2698                    redo B;
2699                  }
2700                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2701                  ## As if <head>
2702                  !!!create-element ($self->{head_element}, 'head');
2703                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2704                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2705    
2706                  $self->{insertion_mode} = IN_HEAD_IM;
2707                  ## Reprocess in the "in head" insertion mode...
2708                }
2709    
2710                if ($token->{tag_name} eq 'base') {
2711                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2712                    ## As if </noscript>
2713                    pop @{$self->{open_elements}};
2714                    !!!parse-error (type => 'in noscript:base');
2715                  
2716                    $self->{insertion_mode} = IN_HEAD_IM;
2717                    ## Reprocess in the "in head" insertion mode...
2718                  }
2719    
2720                  ## NOTE: There is a "as if in head" code clone.
2721                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2722                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2723                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2724                  }
2725                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2726                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2727                  pop @{$self->{open_elements}}
2728                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2729                  !!!next-token;
2730                  redo B;
2731                } elsif ($token->{tag_name} eq 'link') {
2732                  ## NOTE: There is a "as if in head" code clone.
2733                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2734                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2735                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2736                  }
2737                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2738                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2739                  pop @{$self->{open_elements}}
2740                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2741                  !!!next-token;
2742                  redo B;
2743                } elsif ($token->{tag_name} eq 'meta') {
2744                  ## NOTE: There is a "as if in head" code clone.
2745                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2746                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2747                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2748                  }
2749                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2750                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2751    
2752                  unless ($self->{confident}) {
2753                    my $charset;
2754                    if ($token->{attributes}->{charset}) { ## TODO: And if supported
2755                      $charset = $token->{attributes}->{charset}->{value};
2756                    }
2757                    if ($token->{attributes}->{'http-equiv'}) {
2758                      ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
2759                      if ($token->{attributes}->{'http-equiv'}->{value}
2760                          =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=
2761                              [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
2762                              ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
2763                        $charset = defined $1 ? $1 : defined $2 ? $2 : $3;
2764                      } ## TODO: And if supported
2765                    }
2766                    ## TODO: Change the encoding
2767                  }
2768    
2769                  ## TODO: Extracting |charset| from |meta|.
2770                  pop @{$self->{open_elements}}
2771                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2772                  !!!next-token;
2773                  redo B;
2774                } elsif ($token->{tag_name} eq 'title') {
2775                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2776                    ## As if </noscript>
2777                    pop @{$self->{open_elements}};
2778                    !!!parse-error (type => 'in noscript:title');
2779                  
2780                    $self->{insertion_mode} = IN_HEAD_IM;
2781                    ## Reprocess in the "in head" insertion mode...
2782                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2783                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2784                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2785                  }
2786    
2787                  ## NOTE: There is a "as if in head" code clone.
2788                  my $parent = defined $self->{head_element} ? $self->{head_element}
2789                      : $self->{open_elements}->[-1]->[0];
2790                  $parse_rcdata->(RCDATA_CONTENT_MODEL,
2791                                  sub { $parent->append_child ($_[0]) });
2792                  pop @{$self->{open_elements}}
2793                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2794                  redo B;
2795                } elsif ($token->{tag_name} eq 'style') {
2796                  ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
2797                  ## insertion mode IN_HEAD_IM)
2798                  ## NOTE: There is a "as if in head" code clone.
2799                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2800                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2801                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2802                  }
2803                  $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
2804                  pop @{$self->{open_elements}}
2805                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2806                  redo B;
2807                } elsif ($token->{tag_name} eq 'noscript') {
2808                  if ($self->{insertion_mode} == IN_HEAD_IM) {
2809                    ## NOTE: and scripting is disalbed
2810                    !!!insert-element ($token->{tag_name}, $token->{attributes});
2811                    $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
2812                    !!!next-token;
2813                    redo B;
2814                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2815                    !!!parse-error (type => 'in noscript:noscript');
2816                    ## Ignore the token
2817                    !!!next-token;
2818                    redo B;
2819                  } else {
2820                    #
2821                  }
2822                } elsif ($token->{tag_name} eq 'script') {
2823                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2824                    ## As if </noscript>
2825                    pop @{$self->{open_elements}};
2826                    !!!parse-error (type => 'in noscript:script');
2827                  
2828                    $self->{insertion_mode} = IN_HEAD_IM;
2829                    ## Reprocess in the "in head" insertion mode...
2830                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2831                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2832                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2833                  }
2834    
2835                  ## NOTE: There is a "as if in head" code clone.
2836                  $script_start_tag->($insert_to_current);
2837                  pop @{$self->{open_elements}}
2838                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2839                  redo B;
2840                } elsif ($token->{tag_name} eq 'body' or
2841                         $token->{tag_name} eq 'frameset') {
2842                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2843                    ## As if </noscript>
2844                    pop @{$self->{open_elements}};
2845                    !!!parse-error (type => 'in noscript:'.$token->{tag_name});
2846                    
2847                    ## Reprocess in the "in head" insertion mode...
2848                    ## As if </head>
2849                    pop @{$self->{open_elements}};
2850                    
2851                    ## Reprocess in the "after head" insertion mode...
2852                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2853                    pop @{$self->{open_elements}};
2854                    
2855                    ## Reprocess in the "after head" insertion mode...
2856                  }
2857    
2858                  ## "after head" insertion mode
2859                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2860                  if ($token->{tag_name} eq 'body') {
2861                    $self->{insertion_mode} = IN_BODY_IM;
2862                  } elsif ($token->{tag_name} eq 'frameset') {
2863                    $self->{insertion_mode} = IN_FRAMESET_IM;
2864                  } else {
2865                    die "$0: tag name: $self->{tag_name}";
2866                  }
2867                  !!!next-token;
2868                  redo B;
2869                } else {
2870                  #
2871                }
2872    
2873                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2874                  ## As if </noscript>
2875                  pop @{$self->{open_elements}};
2876                  !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
2877                  
2878                  ## Reprocess in the "in head" insertion mode...
2879                  ## As if </head>
2880                  pop @{$self->{open_elements}};
2881    
2882                  ## Reprocess in the "after head" insertion mode...
2883                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2884                  ## As if </head>
2885                  pop @{$self->{open_elements}};
2886    
2887                  ## Reprocess in the "after head" insertion mode...
2888                }
2889    
2890                ## "after head" insertion mode
2891                ## As if <body>
2892                !!!insert-element ('body');
2893                $self->{insertion_mode} = IN_BODY_IM;
2894                ## reprocess
2895                redo B;
2896              } elsif ($token->{type} == END_TAG_TOKEN) {
2897                if ($token->{tag_name} eq 'head') {
2898                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2899                    ## As if <head>
2900                    !!!create-element ($self->{head_element}, 'head');
2901                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2902                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2903    
2904                    ## Reprocess in the "in head" insertion mode...
2905                    pop @{$self->{open_elements}};
2906                    $self->{insertion_mode} = AFTER_HEAD_IM;
2907                    !!!next-token;
2908                    redo B;
2909                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2910                    ## As if </noscript>
2911                    pop @{$self->{open_elements}};
2912                    !!!parse-error (type => 'in noscript:script');
2913                    
2914                    ## Reprocess in the "in head" insertion mode...
2915                    pop @{$self->{open_elements}};
2916                    $self->{insertion_mode} = AFTER_HEAD_IM;
2917                    !!!next-token;
2918                    redo B;
2919                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2920                    pop @{$self->{open_elements}};
2921                    $self->{insertion_mode} = AFTER_HEAD_IM;
2922                    !!!next-token;
2923                    redo B;
2924                  } else {
2925                    #
2926                  }
2927                } elsif ($token->{tag_name} eq 'noscript') {
2928                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2929                    pop @{$self->{open_elements}};
2930                    $self->{insertion_mode} = IN_HEAD_IM;
2931                    !!!next-token;
2932                    redo B;
2933                  } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2934                    !!!parse-error (type => 'unmatched end tag:noscript');
2935                    ## Ignore the token ## ISSUE: An issue in the spec.
2936                    !!!next-token;
2937                    redo B;
2938                  } else {
2939                    #
2940                  }
2941                } elsif ({
2942                          body => 1, html => 1,
2943                         }->{$token->{tag_name}}) {
2944                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2945                    ## As if <head>
2946                    !!!create-element ($self->{head_element}, 'head');
2947                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2948                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2949    
2950                    $self->{insertion_mode} = IN_HEAD_IM;
2951                    ## Reprocess in the "in head" insertion mode...
2952                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2953                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
2954                    ## Ignore the token
2955                    !!!next-token;
2956                    redo B;
2957                  }
2958                  
2959                  #
2960                } elsif ({
2961                          p => 1, br => 1,
2962                         }->{$token->{tag_name}}) {
2963                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2964                    ## As if <head>
2965                    !!!create-element ($self->{head_element}, 'head');
2966                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2967                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2968    
2969                    $self->{insertion_mode} = IN_HEAD_IM;
2970                    ## Reprocess in the "in head" insertion mode...
2971                  }
2972    
2973                  #
2974                } else {
2975                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2976                    #
2977                  } else {
2978                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
2979                    ## Ignore the token
2980                    !!!next-token;
2981                    redo B;
2982                  }
2983                }
2984    
2985                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2986                  ## As if </noscript>
2987                  pop @{$self->{open_elements}};
2988                  !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
2989                  
2990                  ## Reprocess in the "in head" insertion mode...
2991                  ## As if </head>
2992                  pop @{$self->{open_elements}};
2993    
2994                  ## Reprocess in the "after head" insertion mode...
2995                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2996                  ## As if </head>
2997                  pop @{$self->{open_elements}};
2998    
2999                  ## Reprocess in the "after head" insertion mode...
3000                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3001                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3002                  ## Ignore the token ## ISSUE: An issue in the spec.
3003                  !!!next-token;
3004                  redo B;
3005                }
3006    
3007                ## "after head" insertion mode
3008                ## As if <body>
3009                !!!insert-element ('body');
3010                $self->{insertion_mode} = IN_BODY_IM;
3011                ## reprocess
3012                redo B;
3013              } else {
3014                die "$0: $token->{type}: Unknown token type";
3015              }
3016    
3017              ## ISSUE: An issue in the spec.
3018        } elsif ($self->{insertion_mode} & BODY_IMS) {
3019              if ($token->{type} == CHARACTER_TOKEN) {
3020                ## NOTE: There is a code clone of "character in body".
3021                $reconstruct_active_formatting_elements->($insert_to_current);
3022                
3023                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3024    
3025                !!!next-token;
3026                redo B;
3027              } elsif ($token->{type} == START_TAG_TOKEN) {
3028                if ({
3029                     caption => 1, col => 1, colgroup => 1, tbody => 1,
3030                     td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
3031                    }->{$token->{tag_name}}) {
3032                  if ($self->{insertion_mode} == IN_CELL_IM) {
3033                    ## have an element in table scope
3034                    my $tn;
3035                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3036                      my $node = $self->{open_elements}->[$_];
3037                      if ($node->[1] eq 'td' or $node->[1] eq 'th') {
3038                        $tn = $node->[1];
3039                        last INSCOPE;
3040                      } elsif ({
3041                                table => 1, html => 1,
3042                               }->{$node->[1]}) {
3043                        last INSCOPE;
3044                      }
3045                    } # INSCOPE
3046                      unless (defined $tn) {
3047                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3048                        ## Ignore the token
3049                        !!!next-token;
3050                        redo B;
3051                      }
3052                    
3053                    ## Close the cell
3054                    !!!back-token; # <?>
3055                    $token = {type => END_TAG_TOKEN, tag_name => $tn};
3056                    redo B;
3057                  } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3058                    !!!parse-error (type => 'not closed:caption');
3059                    
3060                    ## As if </caption>
3061                    ## have a table element in table scope
3062                    my $i;
3063                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3064                      my $node = $self->{open_elements}->[$_];
3065                      if ($node->[1] eq 'caption') {
3066                        $i = $_;
3067                        last INSCOPE;
3068                      } elsif ({
3069                                table => 1, html => 1,
3070                               }->{$node->[1]}) {
3071                        last INSCOPE;
3072                      }
3073                    } # INSCOPE
3074                      unless (defined $i) {
3075                        !!!parse-error (type => 'unmatched end tag:caption');
3076                        ## Ignore the token
3077                        !!!next-token;
3078                        redo B;
3079                      }
3080                    
3081                    ## generate implied end tags
3082                    if ({
3083                         dd => 1, dt => 1, li => 1, p => 1,
3084                         td => 1, th => 1, tr => 1,
3085                         tbody => 1, tfoot=> 1, thead => 1,
3086                        }->{$self->{open_elements}->[-1]->[1]}) {
3087                      !!!back-token; # <?>
3088                      $token = {type => END_TAG_TOKEN, tag_name => 'caption'};
3089                      !!!back-token;
3090                      $token = {type => END_TAG_TOKEN,
3091                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3092                      redo B;
3093                    }
3094    
3095                    if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3096                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3097                    }
3098                    
3099                    splice @{$self->{open_elements}}, $i;
3100                    
3101                    $clear_up_to_marker->();
3102                    
3103                    $self->{insertion_mode} = IN_TABLE_IM;
3104                    
3105                    ## reprocess
3106                    redo B;
3107                  } else {
3108                    #
3109                  }
3110                } else {
3111                  #
3112                }
3113              } elsif ($token->{type} == END_TAG_TOKEN) {
3114                if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
3115                  if ($self->{insertion_mode} == IN_CELL_IM) {
3116                    ## have an element in table scope
3117                    my $i;
3118                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3119                      my $node = $self->{open_elements}->[$_];
3120                      if ($node->[1] eq $token->{tag_name}) {
3121                        $i = $_;
3122                        last INSCOPE;
3123                      } elsif ({
3124                                table => 1, html => 1,
3125                               }->{$node->[1]}) {
3126                        last INSCOPE;
3127                      }
3128                    } # INSCOPE
3129                      unless (defined $i) {
3130                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3131                        ## Ignore the token
3132                        !!!next-token;
3133                        redo B;
3134                      }
3135                    
3136                    ## generate implied end tags
3137                    if ({
3138                         dd => 1, dt => 1, li => 1, p => 1,
3139                         td => ($token->{tag_name} eq 'th'),
3140                         th => ($token->{tag_name} eq 'td'),
3141                         tr => 1,
3142                         tbody => 1, tfoot=> 1, thead => 1,
3143                        }->{$self->{open_elements}->[-1]->[1]}) {
3144                      !!!back-token;
3145                      $token = {type => END_TAG_TOKEN,
3146                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3147                      redo B;
3148                    }
3149                    
3150                    if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
3151                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3152                    }
3153                    
3154                    splice @{$self->{open_elements}}, $i;
3155                    
3156                    $clear_up_to_marker->();
3157                    
3158                    $self->{insertion_mode} = IN_ROW_IM;
3159                    
3160                    !!!next-token;
3161                    redo B;
3162                  } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3163                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3164                    ## Ignore the token
3165                    !!!next-token;
3166                    redo B;
3167                  } else {
3168                    #
3169                  }
3170                } elsif ($token->{tag_name} eq 'caption') {
3171                  if ($self->{insertion_mode} == IN_CAPTION_IM) {
3172                    ## have a table element in table scope
3173                    my $i;
3174                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3175                      my $node = $self->{open_elements}->[$_];
3176                      if ($node->[1] eq $token->{tag_name}) {
3177                        $i = $_;
3178                        last INSCOPE;
3179                      } elsif ({
3180                                table => 1, html => 1,
3181                               }->{$node->[1]}) {
3182                        last INSCOPE;
3183                      }
3184                    } # INSCOPE
3185                      unless (defined $i) {
3186                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3187                        ## Ignore the token
3188                        !!!next-token;
3189                        redo B;
3190                      }
3191                    
3192                    ## generate implied end tags
3193                    if ({
3194                         dd => 1, dt => 1, li => 1, p => 1,
3195                         td => 1, th => 1, tr => 1,
3196                         tbody => 1, tfoot=> 1, thead => 1,
3197                        }->{$self->{open_elements}->[-1]->[1]}) {
3198                      !!!back-token;
3199                      $token = {type => END_TAG_TOKEN,
3200                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3201                      redo B;
3202                    }
3203                    
3204                    if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3205                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3206                    }
3207                    
3208                    splice @{$self->{open_elements}}, $i;
3209                    
3210                    $clear_up_to_marker->();
3211                    
3212                    $self->{insertion_mode} = IN_TABLE_IM;
3213                    
3214                    !!!next-token;
3215                    redo B;
3216                  } elsif ($self->{insertion_mode} == IN_CELL_IM) {
3217                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3218                    ## Ignore the token
3219                    !!!next-token;
3220                    redo B;
3221                  } else {
3222                    #
3223                  }
3224                } elsif ({
3225                          table => 1, tbody => 1, tfoot => 1,
3226                          thead => 1, tr => 1,
3227                         }->{$token->{tag_name}} and
3228                         $self->{insertion_mode} == IN_CELL_IM) {
3229                  ## have an element in table scope
3230                  my $i;
3231                  my $tn;
3232                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3233                    my $node = $self->{open_elements}->[$_];
3234                    if ($node->[1] eq $token->{tag_name}) {
3235                      $i = $_;
3236                      last INSCOPE;
3237                    } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {
3238                      $tn = $node->[1];
3239                      ## NOTE: There is exactly one |td| or |th| element
3240                      ## in scope in the stack of open elements by definition.
3241                    } elsif ({
3242                              table => 1, html => 1,
3243                             }->{$node->[1]}) {
3244                      last INSCOPE;
3245                    }
3246                  } # INSCOPE
3247                  unless (defined $i) {
3248                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3249                    ## Ignore the token
3250                    !!!next-token;
3251                    redo B;
3252                  }
3253    
3254                  ## Close the cell
3255                  !!!back-token; # </?>
3256                  $token = {type => END_TAG_TOKEN, tag_name => $tn};
3257                  redo B;
3258                } elsif ($token->{tag_name} eq 'table' and
3259                         $self->{insertion_mode} == IN_CAPTION_IM) {
3260                  !!!parse-error (type => 'not closed:caption');
3261    
3262                  ## As if </caption>
3263                  ## have a table element in table scope
3264                  my $i;
3265                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3266                    my $node = $self->{open_elements}->[$_];
3267                    if ($node->[1] eq 'caption') {
3268                      $i = $_;
3269                      last INSCOPE;
3270                    } elsif ({
3271                              table => 1, html => 1,
3272                             }->{$node->[1]}) {
3273                      last INSCOPE;
3274                    }
3275                  } # INSCOPE
3276                  unless (defined $i) {
3277                    !!!parse-error (type => 'unmatched end tag:caption');
3278                    ## Ignore the token
3279                    !!!next-token;
3280                    redo B;
3281                  }
3282                  
3283                  ## generate implied end tags
3284                  if ({
3285                       dd => 1, dt => 1, li => 1, p => 1,
3286                       td => 1, th => 1, tr => 1,
3287                       tbody => 1, tfoot=> 1, thead => 1,
3288                      }->{$self->{open_elements}->[-1]->[1]}) {
3289                    !!!back-token; # </table>
3290                    $token = {type => END_TAG_TOKEN, tag_name => 'caption'};
3291                    !!!back-token;
3292                    $token = {type => END_TAG_TOKEN,
3293                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3294                    redo B;
3295                  }
3296    
3297                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3298                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3299                  }
3300    
3301                  splice @{$self->{open_elements}}, $i;
3302    
3303                  $clear_up_to_marker->();
3304    
3305                  $self->{insertion_mode} = IN_TABLE_IM;
3306    
3307                  ## reprocess
3308                  redo B;
3309                } elsif ({
3310                          body => 1, col => 1, colgroup => 1, html => 1,
3311                         }->{$token->{tag_name}}) {
3312                  if ($self->{insertion_mode} & BODY_TABLE_IMS) {
3313                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3314                    ## Ignore the token
3315                    !!!next-token;
3316                    redo B;
3317                  } else {
3318                    #
3319                  }
3320                } elsif ({
3321                          tbody => 1, tfoot => 1,
3322                          thead => 1, tr => 1,
3323                         }->{$token->{tag_name}} and
3324                         $self->{insertion_mode} == IN_CAPTION_IM) {
3325                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3326                  ## Ignore the token
3327                  !!!next-token;
3328                  redo B;
3329                } else {
3330                  #
3331                }
3332          } else {
3333            die "$0: $token->{type}: Unknown token type";
3334          }
3335    
3336          $insert = $insert_to_current;
3337          #
3338        } elsif ($self->{insertion_mode} & TABLE_IMS) {
3339              if ($token->{type} == CHARACTER_TOKEN) {
3340                ## NOTE: There are "character in table" code clones.
3341                if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3342                  $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3343                  
3344                  unless (length $token->{data}) {
3345                    !!!next-token;
3346                    redo B;
3347                  }
3348                }
3349    
3350                !!!parse-error (type => 'in table:#character');
3351    
3352                ## As if in body, but insert into foster parent element
3353                ## ISSUE: Spec says that "whenever a node would be inserted
3354                ## into the current node" while characters might not be
3355                ## result in a new Text node.
3356                $reconstruct_active_formatting_elements->($insert_to_foster);
3357                
3358                if ({
3359                     table => 1, tbody => 1, tfoot => 1,
3360                     thead => 1, tr => 1,
3361                    }->{$self->{open_elements}->[-1]->[1]}) {
3362                  # MUST
3363                  my $foster_parent_element;
3364                  my $next_sibling;
3365                  my $prev_sibling;
3366                  OE: for (reverse 0..$#{$self->{open_elements}}) {
3367                    if ($self->{open_elements}->[$_]->[1] eq 'table') {
3368                      my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3369                      if (defined $parent and $parent->node_type == 1) {
3370                        $foster_parent_element = $parent;
3371                        $next_sibling = $self->{open_elements}->[$_]->[0];
3372                        $prev_sibling = $next_sibling->previous_sibling;
3373                      } else {
3374                        $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
3375                        $prev_sibling = $foster_parent_element->last_child;
3376                      }
3377                      last OE;
3378                    }
3379                  } # OE
3380                  $foster_parent_element = $self->{open_elements}->[0]->[0] and
3381                  $prev_sibling = $foster_parent_element->last_child
3382                    unless defined $foster_parent_element;
3383                  if (defined $prev_sibling and
3384                      $prev_sibling->node_type == 3) {
3385                    $prev_sibling->manakai_append_text ($token->{data});
3386                  } else {
3387                    $foster_parent_element->insert_before
3388                      ($self->{document}->create_text_node ($token->{data}),
3389                       $next_sibling);
3390                  }
3391                } else {
3392                  $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3393                }
3394                
3395                !!!next-token;
3396                redo B;
3397              } elsif ($token->{type} == START_TAG_TOKEN) {
3398                if ({
3399                     tr => ($self->{insertion_mode} != IN_ROW_IM),
3400                     th => 1, td => 1,
3401                    }->{$token->{tag_name}}) {
3402                  if ($self->{insertion_mode} == IN_TABLE_IM) {
3403                    ## Clear back to table context
3404                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
3405                           $self->{open_elements}->[-1]->[1] ne 'html') {
3406                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3407                      pop @{$self->{open_elements}};
3408                    }
3409                    
3410                    !!!insert-element ('tbody');
3411                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
3412                    ## reprocess in the "in table body" insertion mode...
3413                  }
3414    
3415                  if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3416                    unless ($token->{tag_name} eq 'tr') {
3417                      !!!parse-error (type => 'missing start tag:tr');
3418                    }
3419                    
3420                    ## Clear back to table body context
3421                    while (not {
3422                      tbody => 1, tfoot => 1, thead => 1, html => 1,
3423                    }->{$self->{open_elements}->[-1]->[1]}) {
3424                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3425                      pop @{$self->{open_elements}};
3426                    }
3427                    
3428                    $self->{insertion_mode} = IN_ROW_IM;
3429                    if ($token->{tag_name} eq 'tr') {
3430                      !!!insert-element ($token->{tag_name}, $token->{attributes});
3431                      !!!next-token;
3432                      redo B;
3433                    } else {
3434                      !!!insert-element ('tr');
3435                      ## reprocess in the "in row" insertion mode
3436                    }
3437                  }
3438    
3439                  ## Clear back to table row context
3440                  while (not {
3441                    tr => 1, html => 1,
3442                  }->{$self->{open_elements}->[-1]->[1]}) {
3443                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3444                    pop @{$self->{open_elements}};
3445                  }
3446                  
3447                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3448                  $self->{insertion_mode} = IN_CELL_IM;
3449    
3450                  push @$active_formatting_elements, ['#marker', ''];
3451                  
3452                  !!!next-token;
3453                  redo B;
3454                } elsif ({
3455                          caption => 1, col => 1, colgroup => 1,
3456                          tbody => 1, tfoot => 1, thead => 1,
3457                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
3458                         }->{$token->{tag_name}}) {
3459                  if ($self->{insertion_mode} == IN_ROW_IM) {
3460                    ## As if </tr>
3461                    ## have an element in table scope
3462                    my $i;
3463                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3464                      my $node = $self->{open_elements}->[$_];
3465                      if ($node->[1] eq 'tr') {
3466                        $i = $_;
3467                        last INSCOPE;
3468                      } elsif ({
3469                                table => 1, html => 1,
3470                               }->{$node->[1]}) {
3471                        last INSCOPE;
3472                      }
3473                    } # INSCOPE
3474                    unless (defined $i) {
3475                      !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});
3476                      ## Ignore the token
3477                      !!!next-token;
3478                      redo B;
3479                    }
3480                    
3481                    ## Clear back to table row context
3482                    while (not {
3483                      tr => 1, html => 1,
3484                    }->{$self->{open_elements}->[-1]->[1]}) {
3485                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3486                      pop @{$self->{open_elements}};
3487                    }
3488                    
3489                    pop @{$self->{open_elements}}; # tr
3490                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
3491                    if ($token->{tag_name} eq 'tr') {
3492                      ## reprocess
3493                      redo B;
3494                    } else {
3495                      ## reprocess in the "in table body" insertion mode...
3496                    }
3497                  }
3498    
3499                  if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3500                    ## have an element in table scope
3501                    my $i;
3502                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3503                      my $node = $self->{open_elements}->[$_];
3504                      if ({
3505                           tbody => 1, thead => 1, tfoot => 1,
3506                          }->{$node->[1]}) {
3507                        $i = $_;
3508                        last INSCOPE;
3509                      } elsif ({
3510                                table => 1, html => 1,
3511                               }->{$node->[1]}) {
3512                        last INSCOPE;
3513                      }
3514                    } # INSCOPE
3515                    unless (defined $i) {
3516                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3517                      ## Ignore the token
3518                      !!!next-token;
3519                      redo B;
3520                    }
3521    
3522                    ## Clear back to table body context
3523                    while (not {
3524                      tbody => 1, tfoot => 1, thead => 1, html => 1,
3525                    }->{$self->{open_elements}->[-1]->[1]}) {
3526                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3527                      pop @{$self->{open_elements}};
3528                    }
3529                    
3530                    ## As if <{current node}>
3531                    ## have an element in table scope
3532                    ## true by definition
3533                    
3534                    ## Clear back to table body context
3535                    ## nop by definition
3536                    
3537                    pop @{$self->{open_elements}};
3538                    $self->{insertion_mode} = IN_TABLE_IM;
3539                    ## reprocess in "in table" insertion mode...
3540                  }
3541    
3542                  if ($token->{tag_name} eq 'col') {
3543                    ## Clear back to table context
3544                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
3545                           $self->{open_elements}->[-1]->[1] ne 'html') {
3546                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3547                      pop @{$self->{open_elements}};
3548                    }
3549                    
3550                    !!!insert-element ('colgroup');
3551                    $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
3552                    ## reprocess
3553                    redo B;
3554                  } elsif ({
3555                            caption => 1,
3556                            colgroup => 1,
3557                            tbody => 1, tfoot => 1, thead => 1,
3558                           }->{$token->{tag_name}}) {
3559                    ## Clear back to table context
3560                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
3561                           $self->{open_elements}->[-1]->[1] ne 'html') {
3562                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3563                      pop @{$self->{open_elements}};
3564                    }
3565                    
3566                    push @$active_formatting_elements, ['#marker', '']
3567                        if $token->{tag_name} eq 'caption';
3568                    
3569                    !!!insert-element ($token->{tag_name}, $token->{attributes});
3570                    $self->{insertion_mode} = {
3571                                               caption => IN_CAPTION_IM,
3572                                               colgroup => IN_COLUMN_GROUP_IM,
3573                                               tbody => IN_TABLE_BODY_IM,
3574                                               tfoot => IN_TABLE_BODY_IM,
3575                                               thead => IN_TABLE_BODY_IM,
3576                                              }->{$token->{tag_name}};
3577                    !!!next-token;
3578                    redo B;
3579                  } else {
3580                    die "$0: in table: <>: $token->{tag_name}";
3581                  }
3582                } elsif ($token->{tag_name} eq 'table') {
3583                  ## NOTE: There are code clones for this "table in table"
3584                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3585    
3586                  ## As if </table>
3587                  ## have a table element in table scope
3588                  my $i;
3589                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3590                    my $node = $self->{open_elements}->[$_];
3591                    if ($node->[1] eq 'table') {
3592                      $i = $_;
3593                      last INSCOPE;
3594                    } elsif ({
3595                              table => 1, html => 1,
3596                             }->{$node->[1]}) {
3597                      last INSCOPE;
3598                    }
3599                  } # INSCOPE
3600                  unless (defined $i) {
3601                    !!!parse-error (type => 'unmatched end tag:table');
3602                    ## Ignore tokens </table><table>
3603                    !!!next-token;
3604                    redo B;
3605                  }
3606                  
3607                  ## generate implied end tags
3608                  if ({
3609                       dd => 1, dt => 1, li => 1, p => 1,
3610                       td => 1, th => 1, tr => 1,
3611                       tbody => 1, tfoot=> 1, thead => 1,
3612                      }->{$self->{open_elements}->[-1]->[1]}) {
3613                    !!!back-token; # <table>
3614                    $token = {type => END_TAG_TOKEN, tag_name => 'table'};
3615                    !!!back-token;
3616                    $token = {type => END_TAG_TOKEN,
3617                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3618                    redo B;
3619                  }
3620    
3621                  if ($self->{open_elements}->[-1]->[1] ne 'table') {
3622                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3623                  }
3624    
3625                  splice @{$self->{open_elements}}, $i;
3626    
3627                  $self->_reset_insertion_mode;
3628    
3629                  ## reprocess
3630                  redo B;
3631                } else {
3632                  #
3633                }
3634              } elsif ($token->{type} == END_TAG_TOKEN) {
3635                if ($token->{tag_name} eq 'tr' and
3636                    $self->{insertion_mode} == IN_ROW_IM) {
3637                  ## have an element in table scope
3638                  my $i;
3639                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3640                    my $node = $self->{open_elements}->[$_];
3641                    if ($node->[1] eq $token->{tag_name}) {
3642                      $i = $_;
3643                      last INSCOPE;
3644                    } elsif ({
3645                              table => 1, html => 1,
3646                             }->{$node->[1]}) {
3647                      last INSCOPE;
3648                    }
3649                  } # INSCOPE
3650                  unless (defined $i) {
3651                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3652                    ## Ignore the token
3653                    !!!next-token;
3654                    redo B;
3655                  }
3656    
3657                  ## Clear back to table row context
3658                  while (not {
3659                    tr => 1, html => 1,
3660                  }->{$self->{open_elements}->[-1]->[1]}) {
3661                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3662                    pop @{$self->{open_elements}};
3663                  }
3664    
3665                  pop @{$self->{open_elements}}; # tr
3666                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
3667                  !!!next-token;
3668                  redo B;
3669                } elsif ($token->{tag_name} eq 'table') {
3670                  if ($self->{insertion_mode} == IN_ROW_IM) {
3671                    ## As if </tr>
3672                    ## have an element in table scope
3673                    my $i;
3674                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3675                      my $node = $self->{open_elements}->[$_];
3676                      if ($node->[1] eq 'tr') {
3677                        $i = $_;
3678                        last INSCOPE;
3679                      } elsif ({
3680                                table => 1, html => 1,
3681                               }->{$node->[1]}) {
3682                        last INSCOPE;
3683                      }
3684                    } # INSCOPE
3685                    unless (defined $i) {
3686                      !!!parse-error (type => 'unmatched end tag:'.$token->{type});
3687                      ## Ignore the token
3688                      !!!next-token;
3689                      redo B;
3690                    }
3691                    
3692                    ## Clear back to table row context
3693                    while (not {
3694                      tr => 1, html => 1,
3695                    }->{$self->{open_elements}->[-1]->[1]}) {
3696                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3697                      pop @{$self->{open_elements}};
3698                    }
3699                    
3700                    pop @{$self->{open_elements}}; # tr
3701                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
3702                    ## reprocess in the "in table body" insertion mode...
3703                  }
3704    
3705                  if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3706                    ## have an element in table scope
3707                    my $i;
3708                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3709                      my $node = $self->{open_elements}->[$_];
3710                      if ({
3711                           tbody => 1, thead => 1, tfoot => 1,
3712                          }->{$node->[1]}) {
3713                        $i = $_;
3714                        last INSCOPE;
3715                      } elsif ({
3716                                table => 1, html => 1,
3717                               }->{$node->[1]}) {
3718                        last INSCOPE;
3719                      }
3720                    } # INSCOPE
3721                    unless (defined $i) {
3722                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3723                      ## Ignore the token
3724                      !!!next-token;
3725                      redo B;
3726                    }
3727                    
3728                    ## Clear back to table body context
3729                    while (not {
3730                      tbody => 1, tfoot => 1, thead => 1, html => 1,
3731                    }->{$self->{open_elements}->[-1]->[1]}) {
3732                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3733                      pop @{$self->{open_elements}};
3734                    }
3735                    
3736                    ## As if <{current node}>
3737                    ## have an element in table scope
3738                    ## true by definition
3739                    
3740                    ## Clear back to table body context
3741                    ## nop by definition
3742                    
3743                    pop @{$self->{open_elements}};
3744                    $self->{insertion_mode} = IN_TABLE_IM;
3745                    ## reprocess in the "in table" insertion mode...
3746                  }
3747    
3748                  ## have a table element in table scope
3749                  my $i;
3750                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3751                    my $node = $self->{open_elements}->[$_];
3752                    if ($node->[1] eq $token->{tag_name}) {
3753                      $i = $_;
3754                      last INSCOPE;
3755                    } elsif ({
3756                              table => 1, html => 1,
3757                             }->{$node->[1]}) {
3758                      last INSCOPE;
3759                    }
3760                  } # INSCOPE
3761                  unless (defined $i) {
3762                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3763                    ## Ignore the token
3764                    !!!next-token;
3765                    redo B;
3766                  }
3767    
3768                  ## generate implied end tags
3769                  if ({
3770                       dd => 1, dt => 1, li => 1, p => 1,
3771                       td => 1, th => 1, tr => 1,
3772                       tbody => 1, tfoot=> 1, thead => 1,
3773                      }->{$self->{open_elements}->[-1]->[1]}) {
3774                    !!!back-token;
3775                    $token = {type => END_TAG_TOKEN,
3776                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3777                    redo B;
3778                  }
3779                  
3780                  if ($self->{open_elements}->[-1]->[1] ne 'table') {
3781                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3782                  }
3783                    
3784                  splice @{$self->{open_elements}}, $i;
3785                  
3786                  $self->_reset_insertion_mode;
3787                  
3788                  !!!next-token;
3789                  redo B;
3790                } elsif ({
3791                          tbody => 1, tfoot => 1, thead => 1,
3792                         }->{$token->{tag_name}} and
3793                         $self->{insertion_mode} & ROW_IMS) {
3794                  if ($self->{insertion_mode} == IN_ROW_IM) {
3795                    ## have an element in table scope
3796                    my $i;
3797                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3798                      my $node = $self->{open_elements}->[$_];
3799                      if ($node->[1] eq $token->{tag_name}) {
3800                        $i = $_;
3801                        last INSCOPE;
3802                      } elsif ({
3803                                table => 1, html => 1,
3804                               }->{$node->[1]}) {
3805                        last INSCOPE;
3806                      }
3807                    } # INSCOPE
3808                      unless (defined $i) {
3809                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3810                        ## Ignore the token
3811                        !!!next-token;
3812                        redo B;
3813                      }
3814                    
3815                    ## As if </tr>
3816                    ## have an element in table scope
3817                    my $i;
3818                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3819                      my $node = $self->{open_elements}->[$_];
3820                      if ($node->[1] eq 'tr') {
3821                        $i = $_;
3822                        last INSCOPE;
3823                      } elsif ({
3824                                table => 1, html => 1,
3825                               }->{$node->[1]}) {
3826                        last INSCOPE;
3827                      }
3828                    } # INSCOPE
3829                      unless (defined $i) {
3830                        !!!parse-error (type => 'unmatched end tag:tr');
3831                        ## Ignore the token
3832                        !!!next-token;
3833                        redo B;
3834                      }
3835                    
3836                    ## Clear back to table row context
3837                    while (not {
3838                      tr => 1, html => 1,
3839                    }->{$self->{open_elements}->[-1]->[1]}) {
3840                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3841                      pop @{$self->{open_elements}};
3842                    }
3843                    
3844                    pop @{$self->{open_elements}}; # tr
3845                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
3846                    ## reprocess in the "in table body" insertion mode...
3847                  }
3848    
3849                  ## have an element in table scope
3850                  my $i;
3851                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3852                    my $node = $self->{open_elements}->[$_];
3853                    if ($node->[1] eq $token->{tag_name}) {
3854                      $i = $_;
3855                      last INSCOPE;
3856                    } elsif ({
3857                              table => 1, html => 1,
3858                             }->{$node->[1]}) {
3859                      last INSCOPE;
3860                    }
3861                  } # INSCOPE
3862                  unless (defined $i) {
3863                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3864                    ## Ignore the token
3865                    !!!next-token;
3866                    redo B;
3867                  }
3868    
3869                  ## Clear back to table body context
3870                  while (not {
3871                    tbody => 1, tfoot => 1, thead => 1, html => 1,
3872                  }->{$self->{open_elements}->[-1]->[1]}) {
3873                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3874                    pop @{$self->{open_elements}};
3875                  }
3876    
3877                  pop @{$self->{open_elements}};
3878                  $self->{insertion_mode} = IN_TABLE_IM;
3879                  !!!next-token;
3880                  redo B;
3881                } elsif ({
3882                          body => 1, caption => 1, col => 1, colgroup => 1,
3883                          html => 1, td => 1, th => 1,
3884                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
3885                          tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
3886                         }->{$token->{tag_name}}) {
3887                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3888                  ## Ignore the token
3889                  !!!next-token;
3890                  redo B;
3891                } else {
3892                  #
3893                }
3894              } else {
3895                die "$0: $token->{type}: Unknown token type";
3896              }
3897    
3898          !!!parse-error (type => 'in table:'.$token->{tag_name});
3899    
3900          $insert = $insert_to_foster;
3901          #
3902        } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
3903              if ($token->{type} == CHARACTER_TOKEN) {
3904                if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3905                  $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3906                  unless (length $token->{data}) {
3907                    !!!next-token;
3908                    redo B;
3909                  }
3910                }
3911                
3912                #
3913              } elsif ($token->{type} == START_TAG_TOKEN) {
3914                if ($token->{tag_name} eq 'col') {
3915                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3916                  pop @{$self->{open_elements}};
3917                  !!!next-token;
3918                  redo B;
3919                } else {
3920                  #
3921                }
3922              } elsif ($token->{type} == END_TAG_TOKEN) {
3923                if ($token->{tag_name} eq 'colgroup') {
3924                  if ($self->{open_elements}->[-1]->[1] eq 'html') {
3925                    !!!parse-error (type => 'unmatched end tag:colgroup');
3926                    ## Ignore the token
3927                    !!!next-token;
3928                    redo B;
3929                  } else {
3930                    pop @{$self->{open_elements}}; # colgroup
3931                    $self->{insertion_mode} = IN_TABLE_IM;
3932                    !!!next-token;
3933                    redo B;            
3934                  }
3935                } elsif ($token->{tag_name} eq 'col') {
3936                  !!!parse-error (type => 'unmatched end tag:col');
3937                  ## Ignore the token
3938                  !!!next-token;
3939                  redo B;
3940                } else {
3941                  #
3942                }
3943              } else {
3944                #
3945              }
3946    
3947              ## As if </colgroup>
3948              if ($self->{open_elements}->[-1]->[1] eq 'html') {
3949                !!!parse-error (type => 'unmatched end tag:colgroup');
3950                ## Ignore the token
3951                !!!next-token;
3952                redo B;
3953              } else {
3954                pop @{$self->{open_elements}}; # colgroup
3955                $self->{insertion_mode} = IN_TABLE_IM;
3956                ## reprocess
3957                redo B;
3958              }
3959        } elsif ($self->{insertion_mode} == IN_SELECT_IM) {
3960              if ($token->{type} == CHARACTER_TOKEN) {
3961                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3962                !!!next-token;
3963                redo B;
3964              } elsif ($token->{type} == START_TAG_TOKEN) {
3965                if ($token->{tag_name} eq 'option') {
3966                  if ($self->{open_elements}->[-1]->[1] eq 'option') {
3967                    ## As if </option>
3968                    pop @{$self->{open_elements}};
3969                  }
3970    
3971                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3972                  !!!next-token;
3973                  redo B;
3974                } elsif ($token->{tag_name} eq 'optgroup') {
3975                  if ($self->{open_elements}->[-1]->[1] eq 'option') {
3976                    ## As if </option>
3977                    pop @{$self->{open_elements}};
3978                  }
3979    
3980                  if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {
3981                    ## As if </optgroup>
3982                    pop @{$self->{open_elements}};
3983                  }
3984    
3985                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3986                  !!!next-token;
3987                  redo B;
3988                } elsif ($token->{tag_name} eq 'select') {
3989                  !!!parse-error (type => 'not closed:select');
3990                  ## As if </select> instead
3991                  ## have an element in table scope
3992                  my $i;
3993                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3994                    my $node = $self->{open_elements}->[$_];
3995                    if ($node->[1] eq $token->{tag_name}) {
3996                      $i = $_;
3997                      last INSCOPE;
3998                    } elsif ({
3999                              table => 1, html => 1,
4000                             }->{$node->[1]}) {
4001                      last INSCOPE;
4002                    }
4003                  } # INSCOPE
4004                  unless (defined $i) {
4005                    !!!parse-error (type => 'unmatched end tag:select');
4006                    ## Ignore the token
4007                    !!!next-token;
4008                    redo B;
4009                  }
4010                  
4011                  splice @{$self->{open_elements}}, $i;
4012    
4013                  $self->_reset_insertion_mode;
4014    
4015                  !!!next-token;
4016                  redo B;
4017                } else {
4018                  #
4019                }
4020              } elsif ($token->{type} == END_TAG_TOKEN) {
4021                if ($token->{tag_name} eq 'optgroup') {
4022                  if ($self->{open_elements}->[-1]->[1] eq 'option' and
4023                      $self->{open_elements}->[-2]->[1] eq 'optgroup') {
4024                    ## As if </option>
4025                    splice @{$self->{open_elements}}, -2;
4026                  } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {
4027                    pop @{$self->{open_elements}};
4028                  } else {
4029                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4030                    ## Ignore the token
4031                  }
4032                  !!!next-token;
4033                  redo B;
4034                } elsif ($token->{tag_name} eq 'option') {
4035                  if ($self->{open_elements}->[-1]->[1] eq 'option') {
4036                    pop @{$self->{open_elements}};
4037                  } else {
4038                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4039                    ## Ignore the token
4040                  }
4041                  !!!next-token;
4042                  redo B;
4043                } elsif ($token->{tag_name} eq 'select') {
4044                  ## have an element in table scope
4045                  my $i;
4046                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4047                    my $node = $self->{open_elements}->[$_];
4048                    if ($node->[1] eq $token->{tag_name}) {
4049                      $i = $_;
4050                      last INSCOPE;
4051                    } elsif ({
4052                              table => 1, html => 1,
4053                             }->{$node->[1]}) {
4054                      last INSCOPE;
4055                    }
4056                  } # INSCOPE
4057                  unless (defined $i) {
4058                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4059                    ## Ignore the token
4060                    !!!next-token;
4061                    redo B;
4062                  }
4063                  
4064                  splice @{$self->{open_elements}}, $i;
4065    
4066                  $self->_reset_insertion_mode;
4067    
4068                  !!!next-token;
4069                  redo B;
4070                } elsif ({
4071                          caption => 1, table => 1, tbody => 1,
4072                          tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
4073                         }->{$token->{tag_name}}) {
4074                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4075                  
4076                  ## have an element in table scope
4077                  my $i;
4078                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4079                    my $node = $self->{open_elements}->[$_];
4080                    if ($node->[1] eq $token->{tag_name}) {
4081                      $i = $_;
4082                      last INSCOPE;
4083                    } elsif ({
4084                              table => 1, html => 1,
4085                             }->{$node->[1]}) {
4086                      last INSCOPE;
4087                    }
4088                  } # INSCOPE
4089                  unless (defined $i) {
4090                    ## Ignore the token
4091                    !!!next-token;
4092                    redo B;
4093                  }
4094                  
4095                  ## As if </select>
4096                  ## have an element in table scope
4097                  undef $i;
4098                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4099                    my $node = $self->{open_elements}->[$_];
4100                    if ($node->[1] eq 'select') {
4101                      $i = $_;
4102                      last INSCOPE;
4103                    } elsif ({
4104                              table => 1, html => 1,
4105                             }->{$node->[1]}) {
4106                      last INSCOPE;
4107                    }
4108                  } # INSCOPE
4109                  unless (defined $i) {
4110                    !!!parse-error (type => 'unmatched end tag:select');
4111                    ## Ignore the </select> token
4112                    !!!next-token; ## TODO: ok?
4113                    redo B;
4114                  }
4115                  
4116                  splice @{$self->{open_elements}}, $i;
4117    
4118                  $self->_reset_insertion_mode;
4119    
4120                  ## reprocess
4121                  redo B;
4122                } else {
4123                  #
4124                }
4125              } else {
4126                #
4127              }
4128    
4129              !!!parse-error (type => 'in select:'.$token->{tag_name});
4130              ## Ignore the token
4131              !!!next-token;
4132              redo B;
4133        } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
4134          if ($token->{type} == CHARACTER_TOKEN) {
4135            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4136              my $data = $1;
4137              ## As if in body
4138              $reconstruct_active_formatting_elements->($insert_to_current);
4139                  
4140              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4141              
4142              unless (length $token->{data}) {
4143                !!!next-token;
4144                redo B;
4145              }
4146            }
4147            
4148            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4149              !!!parse-error (type => 'after html:#character');
4150    
4151              ## Reprocess in the "main" phase, "after body" insertion mode...
4152            }
4153            
4154            ## "after body" insertion mode
4155            !!!parse-error (type => 'after body:#character');
4156    
4157            $self->{insertion_mode} = IN_BODY_IM;
4158            ## reprocess
4159            redo B;
4160          } elsif ($token->{type} == START_TAG_TOKEN) {
4161            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4162              !!!parse-error (type => 'after html:'.$token->{tag_name});
4163              
4164              ## Reprocess in the "main" phase, "after body" insertion mode...
4165            }
4166    
4167            ## "after body" insertion mode
4168            !!!parse-error (type => 'after body:'.$token->{tag_name});
4169    
4170            $self->{insertion_mode} = IN_BODY_IM;
4171            ## reprocess
4172            redo B;
4173          } elsif ($token->{type} == END_TAG_TOKEN) {
4174            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4175              !!!parse-error (type => 'after html:/'.$token->{tag_name});
4176              
4177              $self->{insertion_mode} = AFTER_BODY_IM;
4178              ## Reprocess in the "main" phase, "after body" insertion mode...
4179            }
4180    
4181            ## "after body" insertion mode
4182            if ($token->{tag_name} eq 'html') {
4183              if (defined $self->{inner_html_node}) {
4184                !!!parse-error (type => 'unmatched end tag:html');
4185                ## Ignore the token
4186                !!!next-token;
4187                redo B;
4188              } else {
4189                $self->{insertion_mode} = AFTER_HTML_BODY_IM;
4190                !!!next-token;
4191                redo B;
4192              }
4193            } else {
4194              !!!parse-error (type => 'after body:/'.$token->{tag_name});
4195    
4196              $self->{insertion_mode} = IN_BODY_IM;
4197              ## reprocess
4198              redo B;
4199            }
4200          } else {
4201            die "$0: $token->{type}: Unknown token type";
4202          }
4203        } elsif ($self->{insertion_mode} & FRAME_IMS) {
4204          if ($token->{type} == CHARACTER_TOKEN) {
4205            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4206              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4207              
4208              unless (length $token->{data}) {
4209                !!!next-token;
4210                redo B;
4211              }
4212            }
4213            
4214            if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
4215              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4216                !!!parse-error (type => 'in frameset:#character');
4217              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
4218                !!!parse-error (type => 'after frameset:#character');
4219              } else { # "after html frameset"
4220                !!!parse-error (type => 'after html:#character');
4221    
4222                $self->{insertion_mode} = AFTER_FRAMESET_IM;
4223                ## Reprocess in the "main" phase, "after frameset"...
4224                !!!parse-error (type => 'after frameset:#character');
4225              }
4226              
4227              ## Ignore the token.
4228              if (length $token->{data}) {
4229                ## reprocess the rest of characters
4230              } else {
4231                !!!next-token;
4232              }
4233              redo B;
4234            }
4235            
4236            die qq[$0: Character "$token->{data}"];
4237          } elsif ($token->{type} == START_TAG_TOKEN) {
4238            if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4239              !!!parse-error (type => 'after html:'.$token->{tag_name});
4240    
4241              $self->{insertion_mode} = AFTER_FRAMESET_IM;
4242              ## Process in the "main" phase, "after frameset" insertion mode...
4243            }
4244    
4245            if ($token->{tag_name} eq 'frameset' and
4246                $self->{insertion_mode} == IN_FRAMESET_IM) {
4247              !!!insert-element ($token->{tag_name}, $token->{attributes});
4248              !!!next-token;
4249              redo B;
4250            } elsif ($token->{tag_name} eq 'frame' and
4251                     $self->{insertion_mode} == IN_FRAMESET_IM) {
4252              !!!insert-element ($token->{tag_name}, $token->{attributes});
4253              pop @{$self->{open_elements}};
4254              !!!next-token;
4255              redo B;
4256            } elsif ($token->{tag_name} eq 'noframes') {
4257              ## NOTE: As if in body.
4258              $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
4259              redo B;
4260            } else {
4261              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4262                !!!parse-error (type => 'in frameset:'.$token->{tag_name});
4263              } else {
4264                !!!parse-error (type => 'after frameset:'.$token->{tag_name});
4265              }
4266              ## Ignore the token
4267              !!!next-token;
4268              redo B;
4269            }
4270          } elsif ($token->{type} == END_TAG_TOKEN) {
4271            if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4272              !!!parse-error (type => 'after html:/'.$token->{tag_name});
4273    
4274              $self->{insertion_mode} = AFTER_FRAMESET_IM;
4275              ## Process in the "main" phase, "after frameset" insertion mode...
4276            }
4277    
4278            if ($token->{tag_name} eq 'frameset' and
4279                $self->{insertion_mode} == IN_FRAMESET_IM) {
4280              if ($self->{open_elements}->[-1]->[1] eq 'html' and
4281                  @{$self->{open_elements}} == 1) {
4282                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4283                ## Ignore the token
4284                !!!next-token;
4285              } else {
4286                pop @{$self->{open_elements}};
4287                !!!next-token;
4288              }
4289    
4290              if (not defined $self->{inner_html_node} and
4291                  $self->{open_elements}->[-1]->[1] ne 'frameset') {
4292                $self->{insertion_mode} = AFTER_FRAMESET_IM;
4293              }
4294              redo B;
4295            } elsif ($token->{tag_name} eq 'html' and
4296                     $self->{insertion_mode} == AFTER_FRAMESET_IM) {
4297              $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
4298              !!!next-token;
4299              redo B;
4300            } else {
4301              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4302                !!!parse-error (type => 'in frameset:/'.$token->{tag_name});
4303              } else {
4304                !!!parse-error (type => 'after frameset:/'.$token->{tag_name});
4305              }
4306              ## Ignore the token
4307              !!!next-token;
4308              redo B;
4309            }
4310          } else {
4311            die "$0: $token->{type}: Unknown token type";
4312          }
4313    
4314          ## ISSUE: An issue in spec here
4315        } else {
4316          die "$0: $self->{insertion_mode}: Unknown insertion mode";
4317        }
4318    
4319        ## "in body" insertion mode
4320        if ($token->{type} == START_TAG_TOKEN) {
4321        if ($token->{tag_name} eq 'script') {        if ($token->{tag_name} eq 'script') {
4322          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
4323          $script_start_tag->($insert);          $script_start_tag->($insert);
4324          return;          redo B;
4325        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
4326          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
4327          $parse_rcdata->('CDATA', $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4328          return;          redo B;
4329        } elsif ({        } elsif ({
4330                  base => 1, link => 1,                  base => 1, link => 1,
4331                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 2503  sub _tree_construction_main ($) { Line 4333  sub _tree_construction_main ($) {
4333          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4334          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4335          !!!next-token;          !!!next-token;
4336          return;          redo B;
4337        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
4338          ## NOTE: This is an "as if in head" code clone, only "-t" differs          ## NOTE: This is an "as if in head" code clone, only "-t" differs
4339          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
# Line 2527  sub _tree_construction_main ($) { Line 4357  sub _tree_construction_main ($) {
4357          }          }
4358    
4359          !!!next-token;          !!!next-token;
4360          return;          redo B;
4361        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
4362          !!!parse-error (type => 'in body:title');          !!!parse-error (type => 'in body:title');
4363          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
4364          $parse_rcdata->('RCDATA', sub {          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {
4365            if (defined $self->{head_element}) {            if (defined $self->{head_element}) {
4366              $self->{head_element}->append_child ($_[0]);              $self->{head_element}->append_child ($_[0]);
4367            } else {            } else {
4368              $insert->($_[0]);              $insert->($_[0]);
4369            }            }
4370          });          });
4371          return;          redo B;
4372        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
4373          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body:body');
4374                                
# Line 2556  sub _tree_construction_main ($) { Line 4386  sub _tree_construction_main ($) {
4386            }            }
4387          }          }
4388          !!!next-token;          !!!next-token;
4389          return;          redo B;
4390        } elsif ({        } elsif ({
4391                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
4392                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1, listing => 1,
# Line 2567  sub _tree_construction_main ($) { Line 4397  sub _tree_construction_main ($) {
4397          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4398            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4399              !!!back-token;              !!!back-token;
4400              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4401              return;              redo B;
4402            } elsif ({            } elsif ({
4403                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4404                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2580  sub _tree_construction_main ($) { Line 4410  sub _tree_construction_main ($) {
4410          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4411          if ($token->{tag_name} eq 'pre') {          if ($token->{tag_name} eq 'pre') {
4412            !!!next-token;            !!!next-token;
4413            if ($token->{type} eq 'character') {            if ($token->{type} == CHARACTER_TOKEN) {
4414              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
4415              unless (length $token->{data}) {              unless (length $token->{data}) {
4416                !!!next-token;                !!!next-token;
# Line 2589  sub _tree_construction_main ($) { Line 4419  sub _tree_construction_main ($) {
4419          } else {          } else {
4420            !!!next-token;            !!!next-token;
4421          }          }
4422          return;          redo B;
4423        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
4424          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
4425            !!!parse-error (type => 'in form:form');            !!!parse-error (type => 'in form:form');
4426            ## Ignore the token            ## Ignore the token
4427            !!!next-token;            !!!next-token;
4428            return;            redo B;
4429          } else {          } else {
4430            ## has a p element in scope            ## has a p element in scope
4431            INSCOPE: for (reverse @{$self->{open_elements}}) {            INSCOPE: for (reverse @{$self->{open_elements}}) {
4432              if ($_->[1] eq 'p') {              if ($_->[1] eq 'p') {
4433                !!!back-token;                !!!back-token;
4434                $token = {type => 'end tag', tag_name => 'p'};                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4435                return;                redo B;
4436              } elsif ({              } elsif ({
4437                        table => 1, caption => 1, td => 1, th => 1,                        table => 1, caption => 1, td => 1, th => 1,
4438                        button => 1, marquee => 1, object => 1, html => 1,                        button => 1, marquee => 1, object => 1, html => 1,
# Line 2614  sub _tree_construction_main ($) { Line 4444  sub _tree_construction_main ($) {
4444            !!!insert-element-t ($token->{tag_name}, $token->{attributes});            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4445            $self->{form_element} = $self->{open_elements}->[-1]->[0];            $self->{form_element} = $self->{open_elements}->[-1]->[0];
4446            !!!next-token;            !!!next-token;
4447            return;            redo B;
4448          }          }
4449        } elsif ($token->{tag_name} eq 'li') {        } elsif ($token->{tag_name} eq 'li') {
4450          ## has a p element in scope          ## has a p element in scope
4451          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4452            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4453              !!!back-token;              !!!back-token;
4454              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4455              return;              redo B;
4456            } elsif ({            } elsif ({
4457                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4458                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2662  sub _tree_construction_main ($) { Line 4492  sub _tree_construction_main ($) {
4492                        
4493          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4494          !!!next-token;          !!!next-token;
4495          return;          redo B;
4496        } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {        } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {
4497          ## has a p element in scope          ## has a p element in scope
4498          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4499            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4500              !!!back-token;              !!!back-token;
4501              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4502              return;              redo B;
4503            } elsif ({            } elsif ({
4504                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4505                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2709  sub _tree_construction_main ($) { Line 4539  sub _tree_construction_main ($) {
4539                        
4540          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4541          !!!next-token;          !!!next-token;
4542          return;          redo B;
4543        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
4544          ## has a p element in scope          ## has a p element in scope
4545          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4546            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4547              !!!back-token;              !!!back-token;
4548              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4549              return;              redo B;
4550            } elsif ({            } elsif ({
4551                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4552                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2727  sub _tree_construction_main ($) { Line 4557  sub _tree_construction_main ($) {
4557                        
4558          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4559                        
4560          $self->{content_model_flag} = 'PLAINTEXT';          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
4561                        
4562          !!!next-token;          !!!next-token;
4563          return;          redo B;
4564        } elsif ({        } elsif ({
4565                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
4566                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 2739  sub _tree_construction_main ($) { Line 4569  sub _tree_construction_main ($) {
4569            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
4570            if ($node->[1] eq 'p') {            if ($node->[1] eq 'p') {
4571              !!!back-token;              !!!back-token;
4572              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4573              return;              redo B;
4574            } elsif ({            } elsif ({
4575                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4576                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2775  sub _tree_construction_main ($) { Line 4605  sub _tree_construction_main ($) {
4605          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4606                        
4607          !!!next-token;          !!!next-token;
4608          return;          redo B;
4609        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
4610          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
4611            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
# Line 2783  sub _tree_construction_main ($) { Line 4613  sub _tree_construction_main ($) {
4613              !!!parse-error (type => 'in a:a');              !!!parse-error (type => 'in a:a');
4614                            
4615              !!!back-token;              !!!back-token;
4616              $token = {type => 'end tag', tag_name => 'a'};              $token = {type => END_TAG_TOKEN, tag_name => 'a'};
4617              $formatting_end_tag->($token->{tag_name});              $formatting_end_tag->($token->{tag_name});
4618                            
4619              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
# Line 2810  sub _tree_construction_main ($) { Line 4640  sub _tree_construction_main ($) {
4640          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
4641    
4642          !!!next-token;          !!!next-token;
4643          return;          redo B;
4644        } elsif ({        } elsif ({
4645                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
4646                  s => 1, small => 1, strile => 1,                  s => 1, small => 1, strile => 1,
# Line 2822  sub _tree_construction_main ($) { Line 4652  sub _tree_construction_main ($) {
4652          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
4653                    
4654          !!!next-token;          !!!next-token;
4655          return;          redo B;
4656        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
4657          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
4658    
# Line 2832  sub _tree_construction_main ($) { Line 4662  sub _tree_construction_main ($) {
4662            if ($node->[1] eq 'nobr') {            if ($node->[1] eq 'nobr') {
4663              !!!parse-error (type => 'not closed:nobr');              !!!parse-error (type => 'not closed:nobr');
4664              !!!back-token;              !!!back-token;
4665              $token = {type => 'end tag', tag_name => 'nobr'};              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};
4666              return;              redo B;
4667            } elsif ({            } elsif ({
4668                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4669                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2846  sub _tree_construction_main ($) { Line 4676  sub _tree_construction_main ($) {
4676          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
4677                    
4678          !!!next-token;          !!!next-token;
4679          return;          redo B;
4680        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
4681          ## has a button element in scope          ## has a button element in scope
4682          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 2854  sub _tree_construction_main ($) { Line 4684  sub _tree_construction_main ($) {
4684            if ($node->[1] eq 'button') {            if ($node->[1] eq 'button') {
4685              !!!parse-error (type => 'in button:button');              !!!parse-error (type => 'in button:button');
4686              !!!back-token;              !!!back-token;
4687              $token = {type => 'end tag', tag_name => 'button'};              $token = {type => END_TAG_TOKEN, tag_name => 'button'};
4688              return;              redo B;
4689            } elsif ({            } elsif ({
4690                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4691                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2870  sub _tree_construction_main ($) { Line 4700  sub _tree_construction_main ($) {
4700          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
4701    
4702          !!!next-token;          !!!next-token;
4703          return;          redo B;
4704        } elsif ($token->{tag_name} eq 'marquee' or        } elsif ($token->{tag_name} eq 'marquee' or
4705                 $token->{tag_name} eq 'object') {                 $token->{tag_name} eq 'object') {
4706          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
# Line 2879  sub _tree_construction_main ($) { Line 4709  sub _tree_construction_main ($) {
4709          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
4710                    
4711          !!!next-token;          !!!next-token;
4712          return;          redo B;
4713        } elsif ($token->{tag_name} eq 'xmp') {        } elsif ($token->{tag_name} eq 'xmp') {
4714          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
4715          $parse_rcdata->('CDATA', $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4716          return;          redo B;
4717        } elsif ($token->{tag_name} eq 'table') {        } elsif ($token->{tag_name} eq 'table') {
4718          ## has a p element in scope          ## has a p element in scope
4719          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4720            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4721              !!!back-token;              !!!back-token;
4722              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4723              return;              redo B;
4724            } elsif ({            } elsif ({
4725                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4726                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2901  sub _tree_construction_main ($) { Line 4731  sub _tree_construction_main ($) {
4731                        
4732          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4733                        
4734          $self->{insertion_mode} = 'in table';          $self->{insertion_mode} = IN_TABLE_IM;
4735                        
4736          !!!next-token;          !!!next-token;
4737          return;          redo B;
4738        } elsif ({        } elsif ({
4739                  area => 1, basefont => 1, bgsound => 1, br => 1,                  area => 1, basefont => 1, bgsound => 1, br => 1,
4740                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
# Line 2922  sub _tree_construction_main ($) { Line 4752  sub _tree_construction_main ($) {
4752          pop @{$self->{open_elements}};          pop @{$self->{open_elements}};
4753                    
4754          !!!next-token;          !!!next-token;
4755          return;          redo B;
4756        } elsif ($token->{tag_name} eq 'hr') {        } elsif ($token->{tag_name} eq 'hr') {
4757          ## has a p element in scope          ## has a p element in scope
4758          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4759            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4760              !!!back-token;              !!!back-token;
4761              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4762              return;              redo B;
4763            } elsif ({            } elsif ({
4764                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4765                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2942  sub _tree_construction_main ($) { Line 4772  sub _tree_construction_main ($) {
4772          pop @{$self->{open_elements}};          pop @{$self->{open_elements}};
4773                        
4774          !!!next-token;          !!!next-token;
4775          return;          redo B;
4776        } elsif ($token->{tag_name} eq 'input') {        } elsif ($token->{tag_name} eq 'input') {
4777          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
4778                    
# Line 2951  sub _tree_construction_main ($) { Line 4781  sub _tree_construction_main ($) {
4781          pop @{$self->{open_elements}};          pop @{$self->{open_elements}};
4782                    
4783          !!!next-token;          !!!next-token;
4784          return;          redo B;
4785        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
4786          !!!parse-error (type => 'isindex');          !!!parse-error (type => 'isindex');
4787                    
4788          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
4789            ## Ignore the token            ## Ignore the token
4790            !!!next-token;            !!!next-token;
4791            return;            redo B;
4792          } else {          } else {
4793            my $at = $token->{attributes};            my $at = $token->{attributes};
4794            my $form_attrs;            my $form_attrs;
# Line 2968  sub _tree_construction_main ($) { Line 4798  sub _tree_construction_main ($) {
4798            delete $at->{action};            delete $at->{action};
4799            delete $at->{prompt};            delete $at->{prompt};
4800            my @tokens = (            my @tokens = (
4801                          {type => 'start tag', tag_name => 'form',                          {type => START_TAG_TOKEN, tag_name => 'form',
4802                           attributes => $form_attrs},                           attributes => $form_attrs},
4803                          {type => 'start tag', tag_name => 'hr'},                          {type => START_TAG_TOKEN, tag_name => 'hr'},
4804                          {type => 'start tag', tag_name => 'p'},                          {type => START_TAG_TOKEN, tag_name => 'p'},
4805                          {type => 'start tag', tag_name => 'label'},                          {type => START_TAG_TOKEN, tag_name => 'label'},
4806                         );                         );
4807            if ($prompt_attr) {            if ($prompt_attr) {
4808              push @tokens, {type => 'character', data => $prompt_attr->{value}};              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};
4809            } else {            } else {
4810              push @tokens, {type => 'character',              push @tokens, {type => CHARACTER_TOKEN,
4811                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD
4812              ## TODO: make this configurable              ## TODO: make this configurable
4813            }            }
4814            push @tokens,            push @tokens,
4815                          {type => 'start tag', tag_name => 'input', attributes => $at},                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},
4816                          #{type => 'character', data => ''}, # SHOULD                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
4817                          {type => 'end tag', tag_name => 'label'},                          {type => END_TAG_TOKEN, tag_name => 'label'},
4818                          {type => 'end tag', tag_name => 'p'},                          {type => END_TAG_TOKEN, tag_name => 'p'},
4819                          {type => 'start tag', tag_name => 'hr'},                          {type => START_TAG_TOKEN, tag_name => 'hr'},
4820                          {type => 'end tag', tag_name => 'form'};                          {type => END_TAG_TOKEN, tag_name => 'form'};
4821            $token = shift @tokens;            $token = shift @tokens;
4822            !!!back-token (@tokens);            !!!back-token (@tokens);
4823            return;            redo B;
4824          }          }
4825        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
4826          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
# Line 2998  sub _tree_construction_main ($) { Line 4828  sub _tree_construction_main ($) {
4828          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $token->{tag_name}, $token->{attributes});
4829                    
4830          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
4831          $self->{content_model_flag} = 'RCDATA';          $self->{content_model} = RCDATA_CONTENT_MODEL;
4832          delete $self->{escape}; # MUST          delete $self->{escape}; # MUST
4833                    
4834          $insert->($el);          $insert->($el);
4835                    
4836          my $text = '';          my $text = '';
4837          !!!next-token;          !!!next-token;
4838          if ($token->{type} eq 'character') {          if ($token->{type} == CHARACTER_TOKEN) {
4839            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
4840            unless (length $token->{data}) {            unless (length $token->{data}) {
4841              !!!next-token;              !!!next-token;
4842            }            }
4843          }          }
4844          while ($token->{type} eq 'character') {          while ($token->{type} == CHARACTER_TOKEN) {
4845            $text .= $token->{data};            $text .= $token->{data};
4846            !!!next-token;            !!!next-token;
4847          }          }
# Line 3019  sub _tree_construction_main ($) { Line 4849  sub _tree_construction_main ($) {
4849            $el->manakai_append_text ($text);            $el->manakai_append_text ($text);
4850          }          }
4851                    
4852          $self->{content_model_flag} = 'PCDATA';          $self->{content_model} = PCDATA_CONTENT_MODEL;
4853                    
4854          if ($token->{type} eq 'end tag' and          if ($token->{type} == END_TAG_TOKEN and
4855              $token->{tag_name} eq $tag_name) {              $token->{tag_name} eq $tag_name) {
4856            ## Ignore the token            ## Ignore the token
4857          } else {          } else {
4858            !!!parse-error (type => 'in RCDATA:#'.$token->{type});            !!!parse-error (type => 'in RCDATA:#'.$token->{type});
4859          }          }
4860          !!!next-token;          !!!next-token;
4861          return;          redo B;
4862        } elsif ({        } elsif ({
4863                  iframe => 1,                  iframe => 1,
4864                  noembed => 1,                  noembed => 1,
4865                  noframes => 1,                  noframes => 1,
4866                  noscript => 0, ## TODO: 1 if scripting is enabled                  noscript => 0, ## TODO: 1 if scripting is enabled
4867                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
4868          $parse_rcdata->('CDATA', $insert);          ## NOTE: There are two "as if in body" code clones.
4869          return;          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4870            redo B;
4871        } elsif ($token->{tag_name} eq 'select') {        } elsif ($token->{tag_name} eq 'select') {
4872          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
4873                    
4874          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4875                    
4876          $self->{insertion_mode} = 'in select';          $self->{insertion_mode} = IN_SELECT_IM;
4877          !!!next-token;          !!!next-token;
4878          return;          redo B;
4879        } elsif ({        } elsif ({
4880                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
4881                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 3054  sub _tree_construction_main ($) { Line 4885  sub _tree_construction_main ($) {
4885          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!parse-error (type => 'in body:'.$token->{tag_name});
4886          ## Ignore the token          ## Ignore the token
4887          !!!next-token;          !!!next-token;
4888          return;          redo B;
4889                    
4890          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
4891        } else {        } else {
# Line 3063  sub _tree_construction_main ($) { Line 4894  sub _tree_construction_main ($) {
4894          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4895                    
4896          !!!next-token;          !!!next-token;
4897          return;          redo B;
4898        }        }
4899      } elsif ($token->{type} eq 'end tag') {      } elsif ($token->{type} == END_TAG_TOKEN) {
4900        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
4901          if (@{$self->{open_elements}} > 1 and          if (@{$self->{open_elements}} > 1 and
4902              $self->{open_elements}->[1]->[1] eq 'body') {              $self->{open_elements}->[1]->[1] eq 'body') {
# Line 3079  sub _tree_construction_main ($) { Line 4910  sub _tree_construction_main ($) {
4910              }              }
4911            }            }
4912    
4913            $self->{insertion_mode} = 'after body';            $self->{insertion_mode} = AFTER_BODY_IM;
4914            !!!next-token;            !!!next-token;
4915            return;            redo B;
4916          } else {          } else {
4917            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4918            ## Ignore the token            ## Ignore the token
4919            !!!next-token;            !!!next-token;
4920            return;            redo B;
4921          }          }
4922        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
4923          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 3094  sub _tree_construction_main ($) { Line 4925  sub _tree_construction_main ($) {
4925            if ($self->{open_elements}->[-1]->[1] ne 'body') {            if ($self->{open_elements}->[-1]->[1] ne 'body') {
4926              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);
4927            }            }
4928            $self->{insertion_mode} = 'after body';            $self->{insertion_mode} = AFTER_BODY_IM;
4929            ## reprocess            ## reprocess
4930            return;            redo B;
4931          } else {          } else {
4932            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4933            ## Ignore the token            ## Ignore the token
4934            !!!next-token;            !!!next-token;
4935            return;            redo B;
4936          }          }
4937        } elsif ({        } elsif ({
4938                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
# Line 3126  sub _tree_construction_main ($) { Line 4957  sub _tree_construction_main ($) {
4957                   tbody => 1, tfoot=> 1, thead => 1,                   tbody => 1, tfoot=> 1, thead => 1,
4958                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4959                !!!back-token;                !!!back-token;
4960                $token = {type => 'end tag',                $token = {type => END_TAG_TOKEN,
4961                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
4962                return;                redo B;
4963              }              }
4964              $i = $_;              $i = $_;
4965              last INSCOPE unless $token->{tag_name} eq 'p';              last INSCOPE unless $token->{tag_name} eq 'p';
# Line 3161  sub _tree_construction_main ($) { Line 4992  sub _tree_construction_main ($) {
4992              button => 1, marquee => 1, object => 1,              button => 1, marquee => 1, object => 1,
4993            }->{$token->{tag_name}};            }->{$token->{tag_name}};
4994          !!!next-token;          !!!next-token;
4995          return;          redo B;
4996        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
4997          ## has an element in scope          ## has an element in scope
4998          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 3174  sub _tree_construction_main ($) { Line 5005  sub _tree_construction_main ($) {
5005                   tbody => 1, tfoot=> 1, thead => 1,                   tbody => 1, tfoot=> 1, thead => 1,
5006                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
5007                !!!back-token;                !!!back-token;
5008                $token = {type => 'end tag',                $token = {type => END_TAG_TOKEN,
5009                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5010                return;                redo B;
5011              }              }
5012              last INSCOPE;              last INSCOPE;
5013            } elsif ({            } elsif ({
# Line 3195  sub _tree_construction_main ($) { Line 5026  sub _tree_construction_main ($) {
5026    
5027          undef $self->{form_element};          undef $self->{form_element};
5028          !!!next-token;          !!!next-token;
5029          return;          redo B;
5030        } elsif ({        } elsif ({
5031                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5032                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 3213  sub _tree_construction_main ($) { Line 5044  sub _tree_construction_main ($) {
5044                   tbody => 1, tfoot=> 1, thead => 1,                   tbody => 1, tfoot=> 1, thead => 1,
5045                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
5046                !!!back-token;                !!!back-token;
5047                $token = {type => 'end tag',                $token = {type => END_TAG_TOKEN,
5048                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5049                return;                redo B;
5050              }              }
5051              $i = $_;              $i = $_;
5052              last INSCOPE;              last INSCOPE;
# Line 3233  sub _tree_construction_main ($) { Line 5064  sub _tree_construction_main ($) {
5064                    
5065          splice @{$self->{open_elements}}, $i if defined $i;          splice @{$self->{open_elements}}, $i if defined $i;
5066          !!!next-token;          !!!next-token;
5067          return;          redo B;
5068        } elsif ({        } elsif ({
5069                  a => 1,                  a => 1,
5070                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
# Line 3241  sub _tree_construction_main ($) { Line 5072  sub _tree_construction_main ($) {
5072                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
5073                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5074          $formatting_end_tag->($token->{tag_name});          $formatting_end_tag->($token->{tag_name});
5075          return;          redo B;
5076        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
5077          !!!parse-error (type => 'unmatched end tag:br');          !!!parse-error (type => 'unmatched end tag:br');
5078    
# Line 3254  sub _tree_construction_main ($) { Line 5085  sub _tree_construction_main ($) {
5085                    
5086          ## Ignore the token.          ## Ignore the token.
5087          !!!next-token;          !!!next-token;
5088          return;          redo B;
5089        } elsif ({        } elsif ({
5090                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
5091                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 3270  sub _tree_construction_main ($) { Line 5101  sub _tree_construction_main ($) {
5101          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5102          ## Ignore the token          ## Ignore the token
5103          !!!next-token;          !!!next-token;
5104          return;          redo B;
5105                    
5106          ## ISSUE: Issue on HTML5 new elements in spec          ## ISSUE: Issue on HTML5 new elements in spec
5107                    
# Line 3287  sub _tree_construction_main ($) { Line 5118  sub _tree_construction_main ($) {
5118              if ({              if ({
5119                   dd => 1, dt => 1, li => 1, p => 1,                   dd => 1, dt => 1, li => 1, p => 1,
5120                   td => 1, th => 1, tr => 1,                   td => 1, th => 1, tr => 1,
5121                   tbody => 1, tfoot=> 1, thead => 1,                   tbody => 1, tfoot => 1, thead => 1,
5122                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
5123                !!!back-token;                !!!back-token;
5124                $token = {type => 'end tag',                $token = {type => END_TAG_TOKEN,
5125                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5126                return;                redo B;
5127              }              }
5128                    
5129              ## Step 2              ## Step 2
# Line 3325  sub _tree_construction_main ($) { Line 5156  sub _tree_construction_main ($) {
5156            ## Step 5;            ## Step 5;
5157            redo S2;            redo S2;
5158          } # S2          } # S2
5159          return;          redo B;
       }  
     }  
   }; # $in_body  
   
   B: {  
     if ($self->{insertion_mode} ne 'trailing end') {  
       if ($token->{type} eq 'DOCTYPE') {  
         !!!parse-error (type => 'in html:#DOCTYPE');  
         ## Ignore the token  
         ## Stay in the phase  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'start tag' and  
                $token->{tag_name} eq 'html') {  
 ## ISSUE: "aa<html>" is not a parse error.  
 ## ISSUE: "<html>" in fragment is not a parse error.  
         unless ($token->{first_start_tag}) {  
           !!!parse-error (type => 'not first start tag');  
         }  
         my $top_el = $self->{open_elements}->[0]->[0];  
         for my $attr_name (keys %{$token->{attributes}}) {  
           unless ($top_el->has_attribute_ns (undef, $attr_name)) {  
             $top_el->set_attribute_ns  
               (undef, [undef, $attr_name],  
                $token->{attributes}->{$attr_name}->{value});  
           }  
         }  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'end-of-file') {  
         ## Generate implied end tags  
         if ({  
              dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,  
              tbody => 1, tfoot=> 1, thead => 1,  
             }->{$self->{open_elements}->[-1]->[1]}) {  
           !!!back-token;  
           $token = {type => 'end tag', tag_name => $self->{open_elements}->[-1]->[1]};  
           redo B;  
         }  
           
         if (@{$self->{open_elements}} > 2 or  
             (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         } elsif (defined $self->{inner_html_node} and  
                  @{$self->{open_elements}} > 1 and  
                  $self->{open_elements}->[1]->[1] ne 'body') {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         }  
   
         ## Stop parsing  
         last B;  
   
         ## ISSUE: There is an issue in the spec.  
       } else {  
         if ($self->{insertion_mode} eq 'before head') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
             ## As if <head>  
             !!!create-element ($self->{head_element}, 'head');  
             $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});  
             push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
             $self->{insertion_mode} = 'in head';  
             ## reprocess  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             my $attr = $token->{tag_name} eq 'head' ? $token->{attributes} : {};  
             !!!create-element ($self->{head_element}, 'head', $attr);  
             $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});  
             push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
             $self->{insertion_mode} = 'in head';  
             if ($token->{tag_name} eq 'head') {  
               !!!next-token;  
             #} elsif ({  
             #          base => 1, link => 1, meta => 1,  
             #          script => 1, style => 1, title => 1,  
             #         }->{$token->{tag_name}}) {  
             #  ## reprocess  
             } else {  
               ## reprocess  
             }  
             redo B;  
           } elsif ($token->{type} eq 'end tag') {  
             if ({  
                  head => 1, body => 1, html => 1,  
                  p => 1, br => 1,  
                 }->{$token->{tag_name}}) {  
               ## As if <head>  
               !!!create-element ($self->{head_element}, 'head');  
               $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});  
               push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
               $self->{insertion_mode} = 'in head';  
               ## reprocess  
               redo B;  
             } else {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token ## ISSUE: An issue in the spec.  
               !!!next-token;  
               redo B;  
             }  
           } else {  
             die "$0: $token->{type}: Unknown type";  
           }  
         } elsif ($self->{insertion_mode} eq 'in head' or  
                  $self->{insertion_mode} eq 'in head noscript' or  
                  $self->{insertion_mode} eq 'after head') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
               
             #  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ({base => ($self->{insertion_mode} eq 'in head' or  
                           $self->{insertion_mode} eq 'after head'),  
                  link => 1}->{$token->{tag_name}}) {  
               ## NOTE: There is a "as if in head" code clone.  
               if ($self->{insertion_mode} eq 'after head') {  
                 !!!parse-error (type => 'after head:'.$token->{tag_name});  
                 push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
               }  
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.  
               pop @{$self->{open_elements}}  
                   if $self->{insertion_mode} eq 'after head';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'meta') {  
               ## NOTE: There is a "as if in head" code clone.  
               if ($self->{insertion_mode} eq 'after head') {  
                 !!!parse-error (type => 'after head:'.$token->{tag_name});  
                 push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
               }  
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.  
   
               unless ($self->{confident}) {  
                 my $charset;  
                 if ($token->{attributes}->{charset}) { ## TODO: And if supported  
                   $charset = $token->{attributes}->{charset}->{value};  
                 }  
                 if ($token->{attributes}->{'http-equiv'}) {  
                   ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
                   if ($token->{attributes}->{'http-equiv'}->{value}  
                       =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=  
                           [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|  
                           ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {  
                     $charset = defined $1 ? $1 : defined $2 ? $2 : $3;  
                   } ## TODO: And if supported  
                 }  
                 ## TODO: Change the encoding  
               }  
   
               ## TODO: Extracting |charset| from |meta|.  
               pop @{$self->{open_elements}}  
                   if $self->{insertion_mode} eq 'after head';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'title' and  
                      $self->{insertion_mode} eq 'in head') {  
               ## NOTE: There is a "as if in head" code clone.  
               if ($self->{insertion_mode} eq 'after head') {  
                 !!!parse-error (type => 'after head:'.$token->{tag_name});  
                 push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
               }  
               my $parent = defined $self->{head_element} ? $self->{head_element}  
                   : $self->{open_elements}->[-1]->[0];  
               $parse_rcdata->('RCDATA', sub { $parent->append_child ($_[0]) });  
               pop @{$self->{open_elements}}  
                   if $self->{insertion_mode} eq 'after head';  
               redo B;  
             } elsif ($token->{tag_name} eq 'style') {  
               ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and  
               ## insertion mode 'in head')  
               ## NOTE: There is a "as if in head" code clone.  
               if ($self->{insertion_mode} eq 'after head') {  
                 !!!parse-error (type => 'after head:'.$token->{tag_name});  
                 push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
               }  
               $parse_rcdata->('CDATA', $insert_to_current);  
               pop @{$self->{open_elements}}  
                   if $self->{insertion_mode} eq 'after head';  
               redo B;  
             } elsif ($token->{tag_name} eq 'noscript') {  
               if ($self->{insertion_mode} eq 'in head') {  
                 ## NOTE: and scripting is disalbed  
                 !!!insert-element ($token->{tag_name}, $token->{attributes});  
                 $self->{insertion_mode} = 'in head noscript';  
                 !!!next-token;  
                 redo B;  
               } elsif ($self->{insertion_mode} eq 'in head noscript') {  
                 !!!parse-error (type => 'in noscript:noscript');  
                 ## Ignore the token  
                 redo B;  
               } else {  
                 #  
               }  
             } elsif ($token->{tag_name} eq 'head' and  
                      $self->{insertion_mode} ne 'after head') {  
               !!!parse-error (type => 'in head:head'); # or in head noscript  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } elsif ($self->{insertion_mode} ne 'in head noscript' and  
                      $token->{tag_name} eq 'script') {  
               if ($self->{insertion_mode} eq 'after head') {  
                 !!!parse-error (type => 'after head:'.$token->{tag_name});  
                 push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
               }  
               ## NOTE: There is a "as if in head" code clone.  
               $script_start_tag->($insert_to_current);  
               pop @{$self->{open_elements}}  
                   if $self->{insertion_mode} eq 'after head';  
               redo B;  
             } elsif ($self->{insertion_mode} eq 'after head' and  
                      $token->{tag_name} eq 'body') {  
               !!!insert-element ('body', $token->{attributes});  
               $self->{insertion_mode} = 'in body';  
               !!!next-token;  
               redo B;  
             } elsif ($self->{insertion_mode} eq 'after head' and  
                      $token->{tag_name} eq 'frameset') {  
               !!!insert-element ('frameset', $token->{attributes});  
               $self->{insertion_mode} = 'in frameset';  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($self->{insertion_mode} eq 'in head' and  
                 $token->{tag_name} eq 'head') {  
               pop @{$self->{open_elements}};  
               $self->{insertion_mode} = 'after head';  
               !!!next-token;  
               redo B;  
             } elsif ($self->{insertion_mode} eq 'in head noscript' and  
                 $token->{tag_name} eq 'noscript') {  
               pop @{$self->{open_elements}};  
               $self->{insertion_mode} = 'in head';  
               !!!next-token;  
               redo B;  
             } elsif ($self->{insertion_mode} eq 'in head' and  
                      {  
                       body => 1, html => 1,  
                       p => 1, br => 1,  
                      }->{$token->{tag_name}}) {  
               #  
             } elsif ($self->{insertion_mode} eq 'in head noscript' and  
                      {  
                       p => 1, br => 1,  
                      }->{$token->{tag_name}}) {  
               #  
             } elsif ($self->{insertion_mode} ne 'after head') {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
   
           ## As if </head> or </noscript> or <body>  
           if ($self->{insertion_mode} eq 'in head') {  
             pop @{$self->{open_elements}};  
             $self->{insertion_mode} = 'after head';  
           } elsif ($self->{insertion_mode} eq 'in head noscript') {  
             pop @{$self->{open_elements}};  
             !!!parse-error (type => 'in noscript:'.(defined $token->{tag_name} ? ($token->{type} eq 'end tag' ? '/' : '') . $token->{tag_name} : '#' . $token->{type}));  
             $self->{insertion_mode} = 'in head';  
           } else { # 'after head'  
             !!!insert-element ('body');  
             $self->{insertion_mode} = 'in body';  
           }  
           ## reprocess  
           redo B;  
   
           ## ISSUE: An issue in the spec.  
         } elsif ($self->{insertion_mode} eq 'in body') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: There is a code clone of "character in body".  
             $reconstruct_active_formatting_elements->($insert_to_current);  
               
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
   
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             ## NOTE: There is a code clone of "comment in body".  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } else {  
             $in_body->($insert_to_current);  
             redo B;  
           }  
         } elsif ($self->{insertion_mode} eq 'in table') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: There are "character in table" code clones.  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
                 
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
   
             !!!parse-error (type => 'in table:#character');  
   
             ## As if in body, but insert into foster parent element  
             ## ISSUE: Spec says that "whenever a node would be inserted  
             ## into the current node" while characters might not be  
             ## result in a new Text node.  
             $reconstruct_active_formatting_elements->($insert_to_foster);  
               
             if ({  
                  table => 1, tbody => 1, tfoot => 1,  
                  thead => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               # MUST  
               my $foster_parent_element;  
               my $next_sibling;  
               my $prev_sibling;  
               OE: for (reverse 0..$#{$self->{open_elements}}) {  
                 if ($self->{open_elements}->[$_]->[1] eq 'table') {  
                   my $parent = $self->{open_elements}->[$_]->[0]->parent_node;  
                   if (defined $parent and $parent->node_type == 1) {  
                     $foster_parent_element = $parent;  
                     $next_sibling = $self->{open_elements}->[$_]->[0];  
                     $prev_sibling = $next_sibling->previous_sibling;  
                   } else {  
                     $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];  
                     $prev_sibling = $foster_parent_element->last_child;  
                   }  
                   last OE;  
                 }  
               } # OE  
               $foster_parent_element = $self->{open_elements}->[0]->[0] and  
               $prev_sibling = $foster_parent_element->last_child  
                 unless defined $foster_parent_element;  
               if (defined $prev_sibling and  
                   $prev_sibling->node_type == 3) {  
                 $prev_sibling->manakai_append_text ($token->{data});  
               } else {  
                 $foster_parent_element->insert_before  
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
               }  
             } else {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
             }  
               
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ({  
                  caption => 1,  
                  colgroup => 1,  
                  tbody => 1, tfoot => 1, thead => 1,  
                 }->{$token->{tag_name}}) {  
               ## Clear back to table context  
               while ($self->{open_elements}->[-1]->[1] ne 'table' and  
                      $self->{open_elements}->[-1]->[1] ne 'html') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               push @$active_formatting_elements, ['#marker', '']  
                 if $token->{tag_name} eq 'caption';  
   
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               $self->{insertion_mode} = {  
                                  caption => 'in caption',  
                                  colgroup => 'in column group',  
                                  tbody => 'in table body',  
                                  tfoot => 'in table body',  
                                  thead => 'in table body',  
                                 }->{$token->{tag_name}};  
               !!!next-token;  
               redo B;  
             } elsif ({  
                       col => 1,  
                       td => 1, th => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               ## Clear back to table context  
               while ($self->{open_elements}->[-1]->[1] ne 'table' and  
                      $self->{open_elements}->[-1]->[1] ne 'html') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               !!!insert-element ($token->{tag_name} eq 'col' ? 'colgroup' : 'tbody');  
               $self->{insertion_mode} = $token->{tag_name} eq 'col'  
                 ? 'in column group' : 'in table body';  
               ## reprocess  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## NOTE: There are code clones for this "table in table"  
               !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
   
               ## As if </table>  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'table') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:table');  
                 ## Ignore tokens </table><table>  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # <table>  
                 $token = {type => 'end tag', tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'table') {  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               !!!next-token;  
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1, colgroup => 1,  
                       html => 1, tbody => 1, td => 1, tfoot => 1, th => 1,  
                       thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
   
           !!!parse-error (type => 'in table:'.$token->{tag_name});  
           $in_body->($insert_to_foster);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in caption') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a code clone of "character in body".  
             $reconstruct_active_formatting_elements->($insert_to_current);  
               
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
   
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             ## NOTE: This is a code clone of "comment in body".  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ({  
                  caption => 1, col => 1, colgroup => 1, tbody => 1,  
                  td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,  
                 }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'not closed:caption');  
   
               ## As if </caption>  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'caption') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:caption');  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # <?>  
                 $token = {type => 'end tag', tag_name => 'caption'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'caption') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
   
               $self->{insertion_mode} = 'in table';  
   
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'caption') {  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'caption') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
   
               $self->{insertion_mode} = 'in table';  
   
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               !!!parse-error (type => 'not closed:caption');  
   
               ## As if </caption>  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'caption') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:caption');  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # </table>  
                 $token = {type => 'end tag', tag_name => 'caption'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'caption') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
   
               $self->{insertion_mode} = 'in table';  
   
               ## reprocess  
               redo B;  
             } elsif ({  
                       body => 1, col => 1, colgroup => 1,  
                       html => 1, tbody => 1, td => 1, tfoot => 1,  
                       th => 1, thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
                 
           $in_body->($insert_to_current);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in column group') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
               
             #  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'col') {  
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               pop @{$self->{open_elements}};  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'colgroup') {  
               if ($self->{open_elements}->[-1]->[1] eq 'html') {  
                 !!!parse-error (type => 'unmatched end tag:colgroup');  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               } else {  
                 pop @{$self->{open_elements}}; # colgroup  
                 $self->{insertion_mode} = 'in table';  
                 !!!next-token;  
                 redo B;              
               }  
             } elsif ($token->{tag_name} eq 'col') {  
               !!!parse-error (type => 'unmatched end tag:col');  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
   
           ## As if </colgroup>  
           if ($self->{open_elements}->[-1]->[1] eq 'html') {  
             !!!parse-error (type => 'unmatched end tag:colgroup');  
             ## Ignore the token  
             !!!next-token;  
             redo B;  
           } else {  
             pop @{$self->{open_elements}}; # colgroup  
             $self->{insertion_mode} = 'in table';  
             ## reprocess  
             redo B;  
           }  
         } elsif ($self->{insertion_mode} eq 'in table body') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a "character in table" code clone.  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
                 
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
   
             !!!parse-error (type => 'in table:#character');  
   
             ## As if in body, but insert into foster parent element  
             ## ISSUE: Spec says that "whenever a node would be inserted  
             ## into the current node" while characters might not be  
             ## result in a new Text node.  
             $reconstruct_active_formatting_elements->($insert_to_foster);  
   
             if ({  
                  table => 1, tbody => 1, tfoot => 1,  
                  thead => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               # MUST  
               my $foster_parent_element;  
               my $next_sibling;  
               my $prev_sibling;  
               OE: for (reverse 0..$#{$self->{open_elements}}) {  
                 if ($self->{open_elements}->[$_]->[1] eq 'table') {  
                   my $parent = $self->{open_elements}->[$_]->[0]->parent_node;  
                   if (defined $parent and $parent->node_type == 1) {  
                     $foster_parent_element = $parent;  
                     $next_sibling = $self->{open_elements}->[$_]->[0];  
                     $prev_sibling = $next_sibling->previous_sibling;  
                   } else {  
                     $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];  
                     $prev_sibling = $foster_parent_element->last_child;  
                   }  
                   last OE;  
                 }  
               } # OE  
               $foster_parent_element = $self->{open_elements}->[0]->[0] and  
               $prev_sibling = $foster_parent_element->last_child  
                 unless defined $foster_parent_element;  
               if (defined $prev_sibling and  
                   $prev_sibling->node_type == 3) {  
                 $prev_sibling->manakai_append_text ($token->{data});  
               } else {  
                 $foster_parent_element->insert_before  
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
               }  
             } else {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
             }  
               
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             ## Copied from 'in table'  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ({  
                  tr => 1,  
                  th => 1, td => 1,  
                 }->{$token->{tag_name}}) {  
               unless ($token->{tag_name} eq 'tr') {  
                 !!!parse-error (type => 'missing start tag:tr');  
               }  
   
               ## Clear back to table body context  
               while (not {  
                 tbody => 1, tfoot => 1, thead => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
                 
               $self->{insertion_mode} = 'in row';  
               if ($token->{tag_name} eq 'tr') {  
                 !!!insert-element ($token->{tag_name}, $token->{attributes});  
                 !!!next-token;  
               } else {  
                 !!!insert-element ('tr');  
                 ## reprocess  
               }  
               redo B;  
             } elsif ({  
                       caption => 1, col => 1, colgroup => 1,  
                       tbody => 1, tfoot => 1, thead => 1,  
                      }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ({  
                      tbody => 1, thead => 1, tfoot => 1,  
                     }->{$node->[1]}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table body context  
               while (not {  
                 tbody => 1, tfoot => 1, thead => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               ## As if <{current node}>  
               ## have an element in table scope  
               ## true by definition  
   
               ## Clear back to table body context  
               ## nop by definition  
   
               pop @{$self->{open_elements}};  
               $self->{insertion_mode} = 'in table';  
               ## reprocess  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## NOTE: This is a code clone of "table in table"  
               !!!parse-error (type => 'not closed:table');  
   
               ## As if </table>  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'table') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:table');  
                 ## Ignore tokens </table><table>  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # <table>  
                 $token = {type => 'end tag', tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ({  
                  tbody => 1, tfoot => 1, thead => 1,  
                 }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table body context  
               while (not {  
                 tbody => 1, tfoot => 1, thead => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               pop @{$self->{open_elements}};  
               $self->{insertion_mode} = 'in table';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ({  
                      tbody => 1, thead => 1, tfoot => 1,  
                     }->{$node->[1]}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table body context  
               while (not {  
                 tbody => 1, tfoot => 1, thead => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               ## As if <{current node}>  
               ## have an element in table scope  
               ## true by definition  
   
               ## Clear back to table body context  
               ## nop by definition  
   
               pop @{$self->{open_elements}};  
               $self->{insertion_mode} = 'in table';  
               ## reprocess  
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1, colgroup => 1,  
                       html => 1, td => 1, th => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
             
           ## As if in table  
           !!!parse-error (type => 'in table:'.$token->{tag_name});  
           $in_body->($insert_to_foster);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in row') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a "character in table" code clone.  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
                 
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
   
             !!!parse-error (type => 'in table:#character');  
   
             ## As if in body, but insert into foster parent element  
             ## ISSUE: Spec says that "whenever a node would be inserted  
             ## into the current node" while characters might not be  
             ## result in a new Text node.  
             $reconstruct_active_formatting_elements->($insert_to_foster);  
               
             if ({  
                  table => 1, tbody => 1, tfoot => 1,  
                  thead => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               # MUST  
               my $foster_parent_element;  
               my $next_sibling;  
               my $prev_sibling;  
               OE: for (reverse 0..$#{$self->{open_elements}}) {  
                 if ($self->{open_elements}->[$_]->[1] eq 'table') {  
                   my $parent = $self->{open_elements}->[$_]->[0]->parent_node;  
                   if (defined $parent and $parent->node_type == 1) {  
                     $foster_parent_element = $parent;  
                     $next_sibling = $self->{open_elements}->[$_]->[0];  
                     $prev_sibling = $next_sibling->previous_sibling;  
                   } else {  
                     $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];  
                     $prev_sibling = $foster_parent_element->last_child;  
                   }  
                   last OE;  
                 }  
               } # OE  
               $foster_parent_element = $self->{open_elements}->[0]->[0] and  
               $prev_sibling = $foster_parent_element->last_child  
                 unless defined $foster_parent_element;  
               if (defined $prev_sibling and  
                   $prev_sibling->node_type == 3) {  
                 $prev_sibling->manakai_append_text ($token->{data});  
               } else {  
                 $foster_parent_element->insert_before  
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
               }  
             } else {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
             }  
               
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             ## Copied from 'in table'  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'th' or  
                 $token->{tag_name} eq 'td') {  
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
                 
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               $self->{insertion_mode} = 'in cell';  
   
               push @$active_formatting_elements, ['#marker', ''];  
                 
               !!!next-token;  
               redo B;  
             } elsif ({  
                       caption => 1, col => 1, colgroup => 1,  
                       tbody => 1, tfoot => 1, thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               ## As if </tr>  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'tr') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               ## reprocess  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## NOTE: This is a code clone of "table in table"  
               !!!parse-error (type => 'not closed:table');  
   
               ## As if </table>  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'table') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:table');  
                 ## Ignore tokens </table><table>  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # <table>  
                 $token = {type => 'end tag', tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'tr') {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## As if </tr>  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'tr') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{type});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               ## reprocess  
               redo B;  
             } elsif ({  
                       tbody => 1, tfoot => 1, thead => 1,  
                      }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## As if </tr>  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'tr') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:tr');  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               ## reprocess  
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1,  
                       colgroup => 1, html => 1, td => 1, th => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
   
           ## As if in table  
           !!!parse-error (type => 'in table:'.$token->{tag_name});  
           $in_body->($insert_to_foster);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in cell') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a code clone of "character in body".  
             $reconstruct_active_formatting_elements->($insert_to_current);  
               
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
   
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             ## NOTE: This is a code clone of "comment in body".  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ({  
                  caption => 1, col => 1, colgroup => 1,  
                  tbody => 1, td => 1, tfoot => 1, th => 1,  
                  thead => 1, tr => 1,  
                 }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $tn;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'td' or $node->[1] eq 'th') {  
                   $tn = $node->[1];  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $tn) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Close the cell  
               !!!back-token; # <?>  
               $token = {type => 'end tag', tag_name => $tn};  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => ($token->{tag_name} eq 'th'),  
                    th => ($token->{tag_name} eq 'td'),  
                    tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
   
               $self->{insertion_mode} = 'in row';  
   
               !!!next-token;  
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1,  
                       colgroup => 1, html => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } elsif ({  
                       table => 1, tbody => 1, tfoot => 1,  
                       thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $i;  
               my $tn;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {  
                   $tn = $node->[1];  
                   ## NOTE: There is exactly one |td| or |th| element  
                   ## in scope in the stack of open elements by definition.  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => 'end tag', tag_name => $tn};  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
             
           $in_body->($insert_to_current);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in select') {  
           if ($token->{type} eq 'character') {  
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'option') {  
               if ($self->{open_elements}->[-1]->[1] eq 'option') {  
                 ## As if </option>  
                 pop @{$self->{open_elements}};  
               }  
   
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'optgroup') {  
               if ($self->{open_elements}->[-1]->[1] eq 'option') {  
                 ## As if </option>  
                 pop @{$self->{open_elements}};  
               }  
   
               if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {  
                 ## As if </optgroup>  
                 pop @{$self->{open_elements}};  
               }  
   
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'select') {  
               !!!parse-error (type => 'not closed:select');  
               ## As if </select> instead  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:select');  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'optgroup') {  
               if ($self->{open_elements}->[-1]->[1] eq 'option' and  
                   $self->{open_elements}->[-2]->[1] eq 'optgroup') {  
                 ## As if </option>  
                 splice @{$self->{open_elements}}, -2;  
               } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {  
                 pop @{$self->{open_elements}};  
               } else {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
               }  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'option') {  
               if ($self->{open_elements}->[-1]->[1] eq 'option') {  
                 pop @{$self->{open_elements}};  
               } else {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
               }  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'select') {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               !!!next-token;  
               redo B;  
             } elsif ({  
                       caption => 1, table => 1, tbody => 1,  
                       tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## As if </select>  
               ## have an element in table scope  
               undef $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'select') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:select');  
                 ## Ignore the </select> token  
                 !!!next-token; ## TODO: ok?  
                 redo B;  
               }  
                 
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
   
           !!!parse-error (type => 'in select:'.$token->{tag_name});  
           ## Ignore the token  
           !!!next-token;  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'after body') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               my $data = $1;  
               ## As if in body  
               $reconstruct_active_formatting_elements->($insert_to_current);  
                 
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
   
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
               
             #  
             !!!parse-error (type => 'after body:#'.$token->{type});  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[0]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             !!!parse-error (type => 'after body:'.$token->{tag_name});  
             #  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'html') {  
               if (defined $self->{inner_html_node}) {  
                 !!!parse-error (type => 'unmatched end tag:html');  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               } else {  
                 $previous_insertion_mode = $self->{insertion_mode};  
                 $self->{insertion_mode} = 'trailing end';  
                 !!!next-token;  
                 redo B;  
               }  
             } else {  
               !!!parse-error (type => 'after body:/'.$token->{tag_name});  
             }  
           } else {  
             !!!parse-error (type => 'after body:#'.$token->{type});  
           }  
   
           $self->{insertion_mode} = 'in body';  
           ## reprocess  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in frameset') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
   
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
   
             #  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'frameset') {  
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'frame') {  
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               pop @{$self->{open_elements}};  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'noframes') {  
               $in_body->($insert_to_current);  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'frameset') {  
               if ($self->{open_elements}->[-1]->[1] eq 'html' and  
                   @{$self->{open_elements}} == 1) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
               } else {  
                 pop @{$self->{open_elements}};  
                 !!!next-token;  
               }  
                 
               ## if not inner_html and  
               if ($self->{open_elements}->[-1]->[1] ne 'frameset') {  
                 $self->{insertion_mode} = 'after frameset';  
               }  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
             
           if (defined $token->{tag_name}) {  
             !!!parse-error (type => 'in frameset:'.($token->{type} eq 'end tag' ? '/' : '').$token->{tag_name});  
           } else {  
             !!!parse-error (type => 'in frameset:#'.$token->{type});  
           }  
           ## Ignore the token  
           !!!next-token;  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'after frameset') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
   
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
   
             if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {  
               !!!parse-error (type => 'after frameset:#character');  
   
               ## Ignore the token.  
               if (length $token->{data}) {  
                 ## reprocess the rest of characters  
               } else {  
                 !!!next-token;  
               }  
               redo B;  
             }  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'noframes') {  
               $in_body->($insert_to_current);  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'html') {  
               $previous_insertion_mode = $self->{insertion_mode};  
               $self->{insertion_mode} = 'trailing end';  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             die "$0: $token->{type}: Unknown token type";  
           }  
             
           !!!parse-error (type => 'after frameset:'.($token->{tag_name} eq 'end tag' ? '/' : '').$token->{tag_name});  
           ## Ignore the token  
           !!!next-token;  
           redo B;  
   
           ## ISSUE: An issue in spec there  
         } else {  
           die "$0: $self->{insertion_mode}: Unknown insertion mode";  
         }  
       }  
     } elsif ($self->{insertion_mode} eq 'trailing end') {  
       ## states in the main stage is preserved yet # MUST  
         
       if ($token->{type} eq 'DOCTYPE') {  
         !!!parse-error (type => 'after html:#DOCTYPE');  
         ## Ignore the token  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'comment') {  
         my $comment = $self->{document}->create_comment ($token->{data});  
         $self->{document}->append_child ($comment);  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'character') {  
         if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
           my $data = $1;  
           ## As if in the main phase.  
           ## NOTE: The insertion mode in the main phase  
           ## just before the phase has been changed to the trailing  
           ## end phase is either "after body" or "after frameset".  
           $reconstruct_active_formatting_elements->($insert_to_current);  
             
           $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);  
             
           unless (length $token->{data}) {  
             !!!next-token;  
             redo B;  
           }  
         }  
   
         !!!parse-error (type => 'after html:#character');  
         $self->{insertion_mode} = $previous_insertion_mode;  
         ## reprocess  
         redo B;  
       } elsif ($token->{type} eq 'start tag' or  
                $token->{type} eq 'end tag') {  
         !!!parse-error (type => 'after html:'.($token->{type} eq 'end tag' ? '/' : '').$token->{tag_name});  
         $self->{insertion_mode} = $previous_insertion_mode;  
         ## reprocess  
         redo B;  
       } elsif ($token->{type} eq 'end-of-file') {  
         ## Stop parsing  
         last B;  
       } else {  
         die "$0: $token->{type}: Unknown token";  
5160        }        }
5161      }      }
5162        redo B;
5163    } # B    } # B
5164    
5165      ## NOTE: The "trailing end" phase in HTML5 is split into
5166      ## two insertion modes: "after html body" and "after html frameset".
5167      ## NOTE: States in the main stage is preserved while
5168      ## the parser stays in the trailing end phase. # MUST
5169    
5170    ## Stop parsing # MUST    ## Stop parsing # MUST
5171        
5172    ## TODO: script stuffs    ## TODO: script stuffs
# Line 5341  sub set_inner_html ($$$) { Line 5251  sub set_inner_html ($$$) {
5251    
5252      ## Step 2      ## Step 2
5253      my $node_ln = $node->local_name;      my $node_ln = $node->local_name;
5254      $p->{content_model_flag} = {      $p->{content_model} = {
5255        title => 'RCDATA',        title => RCDATA_CONTENT_MODEL,
5256        textarea => 'RCDATA',        textarea => RCDATA_CONTENT_MODEL,
5257        style => 'CDATA',        style => CDATA_CONTENT_MODEL,
5258        script => 'CDATA',        script => CDATA_CONTENT_MODEL,
5259        xmp => 'CDATA',        xmp => CDATA_CONTENT_MODEL,
5260        iframe => 'CDATA',        iframe => CDATA_CONTENT_MODEL,
5261        noembed => 'CDATA',        noembed => CDATA_CONTENT_MODEL,
5262        noframes => 'CDATA',        noframes => CDATA_CONTENT_MODEL,
5263        noscript => 'CDATA',        noscript => CDATA_CONTENT_MODEL,
5264        plaintext => 'PLAINTEXT',        plaintext => PLAINTEXT_CONTENT_MODEL,
5265      }->{$node_ln} || 'PCDATA';      }->{$node_ln};
5266         ## ISSUE: What is "the name of the element"? local name?      $p->{content_model} = PCDATA_CONTENT_MODEL
5267            unless defined $p->{content_model};
5268            ## ISSUE: What is "the name of the element"? local name?
5269    
5270      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $node_ln];
5271    

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24