/[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.72 by wakaba, Sun Mar 2 14:32:26 2008 UTC revision 1.118 by wakaba, Thu Mar 20 01:34:00 2008 UTC
# Line 74  my $special_category = { Line 74  my $special_category = {
74    textarea => 1, tfoot => 1, thead => 1, title => 1, tr => 1, ul => 1, wbr => 1,    textarea => 1, tfoot => 1, thead => 1, title => 1, tr => 1, ul => 1, wbr => 1,
75  };  };
76  my $scoping_category = {  my $scoping_category = {
77    button => 1, caption => 1, html => 1, marquee => 1, object => 1,    applet => 1, button => 1, caption => 1, html => 1, marquee => 1, object => 1,
78    table => 1, td => 1, th => 1,    table => 1, td => 1, th => 1,
79  };  };
80  my $formatting_category = {  my $formatting_category = {
# Line 108  sub parse_byte_string ($$$$;$) { Line 108  sub parse_byte_string ($$$$;$) {
108    $self->{change_encoding} = sub {    $self->{change_encoding} = sub {
109      my $self = shift;      my $self = shift;
110      my $charset = lc shift;      my $charset = lc shift;
111        my $token = shift;
112      ## TODO: if $charset is supported      ## TODO: if $charset is supported
113      ## TODO: normalize charset name      ## TODO: normalize charset name
114    
# Line 126  sub parse_byte_string ($$$$;$) { Line 127  sub parse_byte_string ($$$$;$) {
127      }      }
128    
129      !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.      !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.
130          ':'.$charset, level => 'w');          ':'.$charset, level => 'w', token => $token);
131    
132      ## Step 3      ## Step 3
133      # if (can) {      # if (can) {
# Line 177  sub parse_string ($$$;$) { Line 178  sub parse_string ($$$;$) {
178        if defined $self->{input_encoding};        if defined $self->{input_encoding};
179    
180    my $i = 0;    my $i = 0;
181    my $line = 1;    $self->{line_prev} = $self->{line} = 1;
182    my $column = 0;    $self->{column_prev} = $self->{column} = 0;
183    $self->{set_next_input_character} = sub {    $self->{set_next_char} = sub {
184      my $self = shift;      my $self = shift;
185    
186      pop @{$self->{prev_input_character}};      pop @{$self->{prev_char}};
187      unshift @{$self->{prev_input_character}}, $self->{next_input_character};      unshift @{$self->{prev_char}}, $self->{next_char};
188    
189      $self->{next_input_character} = -1 and return if $i >= length $$s;      $self->{next_char} = -1 and return if $i >= length $$s;
190      $self->{next_input_character} = ord substr $$s, $i++, 1;      $self->{next_char} = ord substr $$s, $i++, 1;
191      $column++;  
192        ($self->{line_prev}, $self->{column_prev})
193            = ($self->{line}, $self->{column});
194        $self->{column}++;
195            
196      if ($self->{next_input_character} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
197        $line++;        $self->{line}++;
198        $column = 0;        $self->{column} = 0;
199      } elsif ($self->{next_input_character} == 0x000D) { # CR      } elsif ($self->{next_char} == 0x000D) { # CR
200        $i++ if substr ($$s, $i, 1) eq "\x0A";        $i++ if substr ($$s, $i, 1) eq "\x0A";
201        $self->{next_input_character} = 0x000A; # LF # MUST        $self->{next_char} = 0x000A; # LF # MUST
202        $line++;        $self->{line}++;
203        $column = 0;        $self->{column} = 0;
204      } elsif ($self->{next_input_character} > 0x10FFFF) {      } elsif ($self->{next_char} > 0x10FFFF) {
205        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
206      } elsif ($self->{next_input_character} == 0x0000) { # NULL      } elsif ($self->{next_char} == 0x0000) { # NULL
207        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
208        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
209      }      }
210    };    };
211    $self->{prev_input_character} = [-1, -1, -1];    $self->{prev_char} = [-1, -1, -1];
212    $self->{next_input_character} = -1;    $self->{next_char} = -1;
213    
214    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
215      my (%opt) = @_;      my (%opt) = @_;
216      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
217        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
218        warn "Parse error ($opt{type}) at line $line column $column\n";
219    };    };
220    $self->{parse_error} = sub {    $self->{parse_error} = sub {
221      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
222    };    };
223    
224    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
# Line 220  sub parse_string ($$$;$) { Line 226  sub parse_string ($$$;$) {
226    $self->_construct_tree;    $self->_construct_tree;
227    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
228    
229      delete $self->{parse_error}; # remove loop
230    
231    return $self->{document};    return $self->{document};
232  } # parse_string  } # parse_string
233    
234  sub new ($) {  sub new ($) {
235    my $class = shift;    my $class = shift;
236    my $self = bless {}, $class;    my $self = bless {}, $class;
237    $self->{set_next_input_character} = sub {    $self->{set_next_char} = sub {
238      $self->{next_input_character} = -1;      $self->{next_char} = -1;
239    };    };
240    $self->{parse_error} = sub {    $self->{parse_error} = sub {
241      #      #
# Line 303  sub TABLE_IMS ()      { 0b1000000 } Line 311  sub TABLE_IMS ()      { 0b1000000 }
311  sub ROW_IMS ()        { 0b10000000 }  sub ROW_IMS ()        { 0b10000000 }
312  sub BODY_AFTER_IMS () { 0b100000000 }  sub BODY_AFTER_IMS () { 0b100000000 }
313  sub FRAME_IMS ()      { 0b1000000000 }  sub FRAME_IMS ()      { 0b1000000000 }
314    sub SELECT_IMS ()     { 0b10000000000 }
315    
316    ## NOTE: "initial" and "before html" insertion modes have no constants.
317    
318    ## NOTE: "after after body" insertion mode.
319  sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }  sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
320    
321    ## NOTE: "after after frameset" insertion mode.
322  sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }  sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
323    
324  sub IN_HEAD_IM () { HEAD_IMS | 0b00 }  sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
325  sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }  sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
326  sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }  sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
# Line 319  sub IN_TABLE_IM () { TABLE_IMS } Line 334  sub IN_TABLE_IM () { TABLE_IMS }
334  sub AFTER_BODY_IM () { BODY_AFTER_IMS }  sub AFTER_BODY_IM () { BODY_AFTER_IMS }
335  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
336  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
337  sub IN_SELECT_IM () { 0b01 }  sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
338    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
339  sub IN_COLUMN_GROUP_IM () { 0b10 }  sub IN_COLUMN_GROUP_IM () { 0b10 }
340    
341  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
# Line 333  sub _initialize_tokenizer ($) { Line 349  sub _initialize_tokenizer ($) {
349    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
350    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
351    $self->{char} = [];    $self->{char} = [];
352    # $self->{next_input_character}    # $self->{next_char}
353    !!!next-input-character;    !!!next-input-character;
354    $self->{token} = [];    $self->{token} = [];
355    # $self->{escape}    # $self->{escape}
# Line 346  sub _initialize_tokenizer ($) { Line 362  sub _initialize_tokenizer ($) {
362  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
363  ##   ->{public_identifier} (DOCTYPE_TOKEN)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
364  ##   ->{system_identifier} (DOCTYPE_TOKEN)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
365  ##   ->{correct} == 1 or 0 (DOCTYPE_TOKEN)  ##   ->{quirks} == 1 or 0 (DOCTYPE_TOKEN): "force-quirks" flag
366  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
367  ##        ->{name}  ##        ->{name}
368  ##        ->{value}  ##        ->{value}
# Line 384  sub _get_next_token ($) { Line 400  sub _get_next_token ($) {
400    
401    A: {    A: {
402      if ($self->{state} == DATA_STATE) {      if ($self->{state} == DATA_STATE) {
403        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_char} == 0x0026) { # &
404          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
405              not $self->{escape}) {              not $self->{escape}) {
406              !!!cp (1);
407            $self->{state} = ENTITY_DATA_STATE;            $self->{state} = ENTITY_DATA_STATE;
408            !!!next-input-character;            !!!next-input-character;
409            redo A;            redo A;
410          } else {          } else {
411              !!!cp (2);
412            #            #
413          }          }
414        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
415          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
416            unless ($self->{escape}) {            unless ($self->{escape}) {
417              if ($self->{prev_input_character}->[0] == 0x002D and # -              if ($self->{prev_char}->[0] == 0x002D and # -
418                  $self->{prev_input_character}->[1] == 0x0021 and # !                  $self->{prev_char}->[1] == 0x0021 and # !
419                  $self->{prev_input_character}->[2] == 0x003C) { # <                  $self->{prev_char}->[2] == 0x003C) { # <
420                  !!!cp (3);
421                $self->{escape} = 1;                $self->{escape} = 1;
422                } else {
423                  !!!cp (4);
424              }              }
425              } else {
426                !!!cp (5);
427            }            }
428          }          }
429                    
430          #          #
431        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_char} == 0x003C) { # <
432          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
433              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
434               not $self->{escape})) {               not $self->{escape})) {
435              !!!cp (6);
436            $self->{state} = TAG_OPEN_STATE;            $self->{state} = TAG_OPEN_STATE;
437            !!!next-input-character;            !!!next-input-character;
438            redo A;            redo A;
439          } else {          } else {
440              !!!cp (7);
441            #            #
442          }          }
443        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
444          if ($self->{escape} and          if ($self->{escape} and
445              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
446            if ($self->{prev_input_character}->[0] == 0x002D and # -            if ($self->{prev_char}->[0] == 0x002D and # -
447                $self->{prev_input_character}->[1] == 0x002D) { # -                $self->{prev_char}->[1] == 0x002D) { # -
448                !!!cp (8);
449              delete $self->{escape};              delete $self->{escape};
450              } else {
451                !!!cp (9);
452            }            }
453            } else {
454              !!!cp (10);
455          }          }
456                    
457          #          #
458        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
459          !!!emit ({type => END_OF_FILE_TOKEN});          !!!cp (11);
460            !!!emit ({type => END_OF_FILE_TOKEN,
461                      line => $self->{line}, column => $self->{column}});
462          last A; ## TODO: ok?          last A; ## TODO: ok?
463          } else {
464            !!!cp (12);
465        }        }
466        # Anything else        # Anything else
467        my $token = {type => CHARACTER_TOKEN,        my $token = {type => CHARACTER_TOKEN,
468                     data => chr $self->{next_input_character}};                     data => chr $self->{next_char},
469                       #line => $self->{line}, column => $self->{column},
470                      };
471        ## Stay in the data state        ## Stay in the data state
472        !!!next-input-character;        !!!next-input-character;
473    
# Line 440  sub _get_next_token ($) { Line 476  sub _get_next_token ($) {
476        redo A;        redo A;
477      } elsif ($self->{state} == ENTITY_DATA_STATE) {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
478        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
479    
480          #my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
481                
482        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);
483    
# Line 447  sub _get_next_token ($) { Line 485  sub _get_next_token ($) {
485        # next-input-character is already done        # next-input-character is already done
486    
487        unless (defined $token) {        unless (defined $token) {
488          !!!emit ({type => CHARACTER_TOKEN, data => '&'});          !!!cp (13);
489            !!!emit ({type => CHARACTER_TOKEN, data => '&',
490                      #line => $l, column => $c,
491                     });
492        } else {        } else {
493            !!!cp (14);
494          !!!emit ($token);          !!!emit ($token);
495        }        }
496    
497        redo A;        redo A;
498      } elsif ($self->{state} == TAG_OPEN_STATE) {      } elsif ($self->{state} == TAG_OPEN_STATE) {
499        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
500          if ($self->{next_input_character} == 0x002F) { # /          if ($self->{next_char} == 0x002F) { # /
501              !!!cp (15);
502            !!!next-input-character;            !!!next-input-character;
503            $self->{state} = CLOSE_TAG_OPEN_STATE;            $self->{state} = CLOSE_TAG_OPEN_STATE;
504            redo A;            redo A;
505          } else {          } else {
506              !!!cp (16);
507            ## reconsume            ## reconsume
508            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
509    
510            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
511                        #line => $self->{line_prev},
512                        #column => $self->{column_prev},
513                       });
514    
515            redo A;            redo A;
516          }          }
517        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
518          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_char} == 0x0021) { # !
519              !!!cp (17);
520            $self->{state} = MARKUP_DECLARATION_OPEN_STATE;            $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
521            !!!next-input-character;            !!!next-input-character;
522            redo A;            redo A;
523          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{next_char} == 0x002F) { # /
524              !!!cp (18);
525            $self->{state} = CLOSE_TAG_OPEN_STATE;            $self->{state} = CLOSE_TAG_OPEN_STATE;
526            !!!next-input-character;            !!!next-input-character;
527            redo A;            redo A;
528          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
529                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{next_char} <= 0x005A) { # A..Z
530              !!!cp (19);
531            $self->{current_token}            $self->{current_token}
532              = {type => START_TAG_TOKEN,              = {type => START_TAG_TOKEN,
533                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020),
534                   line => $self->{line_prev},
535                   column => $self->{column_prev}};
536            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
537            !!!next-input-character;            !!!next-input-character;
538            redo A;            redo A;
539          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
540                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
541              !!!cp (20);
542            $self->{current_token} = {type => START_TAG_TOKEN,            $self->{current_token} = {type => START_TAG_TOKEN,
543                              tag_name => chr ($self->{next_input_character})};                                      tag_name => chr ($self->{next_char}),
544                                        line => $self->{line_prev},
545                                        column => $self->{column_prev}};
546            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
547            !!!next-input-character;            !!!next-input-character;
548            redo A;            redo A;
549          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
550            !!!parse-error (type => 'empty start tag');            !!!cp (21);
551              !!!parse-error (type => 'empty start tag',
552                              line => $self->{line_prev},
553                              column => $self->{column_prev});
554            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
555            !!!next-input-character;            !!!next-input-character;
556    
557            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
558                        #line => $self->{line_prev},
559                        #column => $self->{column_prev},
560                       });
561    
562            redo A;            redo A;
563          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
564            !!!parse-error (type => 'pio');            !!!cp (22);
565              !!!parse-error (type => 'pio',
566                              line => $self->{line_prev},
567                              column => $self->{column_prev});
568            $self->{state} = BOGUS_COMMENT_STATE;            $self->{state} = BOGUS_COMMENT_STATE;
569            ## $self->{next_input_character} is intentionally left as is            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
570                                        #line => $self->{line_prev},
571                                        #column => $self->{column_prev},
572                                       };
573              ## $self->{next_char} is intentionally left as is
574            redo A;            redo A;
575          } else {          } else {
576              !!!cp (23);
577            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago');
578            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
579            ## reconsume            ## reconsume
580    
581            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
582                        #line => $self->{line_prev},
583                        #column => $self->{column_prev},
584                       });
585    
586            redo A;            redo A;
587          }          }
# Line 517  sub _get_next_token ($) { Line 589  sub _get_next_token ($) {
589          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
590        }        }
591      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
592          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
593        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
594          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
595    
596            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
597            my @next_char;            my @next_char;
598            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {
599              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_char};
600              my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);              my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);
601              my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;              my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;
602              if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {              if ($self->{next_char} == $c or $self->{next_char} == $C) {
603                  !!!cp (24);
604                !!!next-input-character;                !!!next-input-character;
605                next TAGNAME;                next TAGNAME;
606              } else {              } else {
607                $self->{next_input_character} = shift @next_char; # reconsume                !!!cp (25);
608                  $self->{next_char} = shift @next_char; # reconsume
609                !!!back-next-input-character (@next_char);                !!!back-next-input-character (@next_char);
610                $self->{state} = DATA_STATE;                $self->{state} = DATA_STATE;
611    
612                !!!emit ({type => CHARACTER_TOKEN, data => '</'});                !!!emit ({type => CHARACTER_TOKEN, data => '</',
613                            #line => $l, column => $c,
614                           });
615        
616                redo A;                redo A;
617              }              }
618            }            }
619            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_char};
620                
621            unless ($self->{next_input_character} == 0x0009 or # HT            unless ($self->{next_char} == 0x0009 or # HT
622                    $self->{next_input_character} == 0x000A or # LF                    $self->{next_char} == 0x000A or # LF
623                    $self->{next_input_character} == 0x000B or # VT                    $self->{next_char} == 0x000B or # VT
624                    $self->{next_input_character} == 0x000C or # FF                    $self->{next_char} == 0x000C or # FF
625                    $self->{next_input_character} == 0x0020 or # SP                    $self->{next_char} == 0x0020 or # SP
626                    $self->{next_input_character} == 0x003E or # >                    $self->{next_char} == 0x003E or # >
627                    $self->{next_input_character} == 0x002F or # /                    $self->{next_char} == 0x002F or # /
628                    $self->{next_input_character} == -1) {                    $self->{next_char} == -1) {
629              $self->{next_input_character} = shift @next_char; # reconsume              !!!cp (26);
630                $self->{next_char} = shift @next_char; # reconsume
631              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
632              $self->{state} = DATA_STATE;              $self->{state} = DATA_STATE;
633              !!!emit ({type => CHARACTER_TOKEN, data => '</'});              !!!emit ({type => CHARACTER_TOKEN, data => '</',
634                          #line => $l, column => $c,
635                         });
636              redo A;              redo A;
637            } else {            } else {
638              $self->{next_input_character} = shift @next_char;              !!!cp (27);
639                $self->{next_char} = shift @next_char;
640              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
641              # and consume...              # and consume...
642            }            }
643          } else {          } else {
644            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
645              !!!cp (28);
646            # next-input-character is already done            # next-input-character is already done
647            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
648            !!!emit ({type => CHARACTER_TOKEN, data => '</'});            !!!emit ({type => CHARACTER_TOKEN, data => '</',
649                        #line => $l, column => $c,
650                       });
651            redo A;            redo A;
652          }          }
653        }        }
654                
655        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_char} and
656            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
657          $self->{current_token} = {type => END_TAG_TOKEN,          !!!cp (29);
658                            tag_name => chr ($self->{next_input_character} + 0x0020)};          $self->{current_token}
659                = {type => END_TAG_TOKEN,
660                   tag_name => chr ($self->{next_char} + 0x0020),
661                   line => $l, column => $c};
662          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
663          !!!next-input-character;          !!!next-input-character;
664          redo A;          redo A;
665        } elsif (0x0061 <= $self->{next_input_character} and        } elsif (0x0061 <= $self->{next_char} and
666                 $self->{next_input_character} <= 0x007A) { # a..z                 $self->{next_char} <= 0x007A) { # a..z
667            !!!cp (30);
668          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token} = {type => END_TAG_TOKEN,
669                            tag_name => chr ($self->{next_input_character})};                                    tag_name => chr ($self->{next_char}),
670                                      line => $l, column => $c};
671          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
672          !!!next-input-character;          !!!next-input-character;
673          redo A;          redo A;
674        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
675          !!!parse-error (type => 'empty end tag');          !!!cp (31);
676            !!!parse-error (type => 'empty end tag',
677                            line => $self->{line_prev}, ## "<" in "</>"
678                            column => $self->{column_prev} - 1);
679          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
680          !!!next-input-character;          !!!next-input-character;
681          redo A;          redo A;
682        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
683            !!!cp (32);
684          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
685          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
686          # reconsume          # reconsume
687    
688          !!!emit ({type => CHARACTER_TOKEN, data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
689                      #line => $l, column => $c,
690                     });
691    
692          redo A;          redo A;
693        } else {        } else {
694            !!!cp (33);
695          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
696          $self->{state} = BOGUS_COMMENT_STATE;          $self->{state} = BOGUS_COMMENT_STATE;
697          ## $self->{next_input_character} is intentionally left as is          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
698                                      #line => $self->{line_prev}, # "<" of "</"
699                                      #column => $self->{column_prev} - 1,
700                                     };
701            ## $self->{next_char} is intentionally left as is
702          redo A;          redo A;
703        }        }
704      } elsif ($self->{state} == TAG_NAME_STATE) {      } elsif ($self->{state} == TAG_NAME_STATE) {
705        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
706            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
707            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
708            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
709            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
710            !!!cp (34);
711          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
712          !!!next-input-character;          !!!next-input-character;
713          redo A;          redo A;
714        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
715          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
716            $self->{current_token}->{first_start_tag}            !!!cp (35);
               = not defined $self->{last_emitted_start_tag_name};  
717            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
718          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
719            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
720            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
721              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This should never be reached.
722            }            #  !!! cp (36);
723              #  !!! parse-error (type => 'end tag attribute');
724              #} else {
725                !!!cp (37);
726              #}
727          } else {          } else {
728            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
729          }          }
# Line 628  sub _get_next_token ($) { Line 733  sub _get_next_token ($) {
733          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
734    
735          redo A;          redo A;
736        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
737                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
738          $self->{current_token}->{tag_name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (38);
739            $self->{current_token}->{tag_name} .= chr ($self->{next_char} + 0x0020);
740            # start tag or end tag            # start tag or end tag
741          ## Stay in this state          ## Stay in this state
742          !!!next-input-character;          !!!next-input-character;
743          redo A;          redo A;
744        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
745          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
746          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
747            $self->{current_token}->{first_start_tag}            !!!cp (39);
               = not defined $self->{last_emitted_start_tag_name};  
748            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
749          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
750            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
751            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
752              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This state should never be reached.
753            }            #  !!! cp (40);
754              #  !!! parse-error (type => 'end tag attribute');
755              #} else {
756                !!!cp (41);
757              #}
758          } else {          } else {
759            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
760          }          }
# Line 655  sub _get_next_token ($) { Line 764  sub _get_next_token ($) {
764          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
765    
766          redo A;          redo A;
767        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
768          !!!next-input-character;          !!!next-input-character;
769          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_char} == 0x003E and # >
770              $self->{current_token}->{type} == START_TAG_TOKEN and              $self->{current_token}->{type} == START_TAG_TOKEN and
771              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
772            # permitted slash            # permitted slash
773              !!!cp (42);
774            #            #
775          } else {          } else {
776              !!!cp (43);
777            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
778          }          }
779          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
780          # next-input-character is already done          # next-input-character is already done
781          redo A;          redo A;
782        } else {        } else {
783          $self->{current_token}->{tag_name} .= chr $self->{next_input_character};          !!!cp (44);
784            $self->{current_token}->{tag_name} .= chr $self->{next_char};
785            # start tag or end tag            # start tag or end tag
786          ## Stay in the state          ## Stay in the state
787          !!!next-input-character;          !!!next-input-character;
788          redo A;          redo A;
789        }        }
790      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
791        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
792            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
793            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
794            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
795            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
796            !!!cp (45);
797          ## Stay in the state          ## Stay in the state
798          !!!next-input-character;          !!!next-input-character;
799          redo A;          redo A;
800        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
801          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
802            $self->{current_token}->{first_start_tag}            !!!cp (46);
               = not defined $self->{last_emitted_start_tag_name};  
803            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
804          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
805            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
806            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
807                !!!cp (47);
808              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
809              } else {
810                !!!cp (48);
811            }            }
812          } else {          } else {
813            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 703  sub _get_next_token ($) { Line 818  sub _get_next_token ($) {
818          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
819    
820          redo A;          redo A;
821        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
822                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
823          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (49);
824            $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),
825                                value => ''};                                value => ''};
826          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
827          !!!next-input-character;          !!!next-input-character;
828          redo A;          redo A;
829        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
830          !!!next-input-character;          !!!next-input-character;
831          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_char} == 0x003E and # >
832              $self->{current_token}->{type} == START_TAG_TOKEN and              $self->{current_token}->{type} == START_TAG_TOKEN and
833              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
834            # permitted slash            # permitted slash
835              !!!cp (50);
836            #            #
837          } else {          } else {
838              !!!cp (51);
839            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
840          }          }
841          ## Stay in the state          ## Stay in the state
842          # next-input-character is already done          # next-input-character is already done
843          redo A;          redo A;
844        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
845          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
846          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
847            $self->{current_token}->{first_start_tag}            !!!cp (52);
               = not defined $self->{last_emitted_start_tag_name};  
848            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
849          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
850            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
851            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
852                !!!cp (53);
853              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
854              } else {
855                !!!cp (54);
856            }            }
857          } else {          } else {
858            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 748  sub _get_next_token ($) { Line 868  sub _get_next_token ($) {
868               0x0022 => 1, # "               0x0022 => 1, # "
869               0x0027 => 1, # '               0x0027 => 1, # '
870               0x003D => 1, # =               0x003D => 1, # =
871              }->{$self->{next_input_character}}) {              }->{$self->{next_char}}) {
872              !!!cp (55);
873            !!!parse-error (type => 'bad attribute name');            !!!parse-error (type => 'bad attribute name');
874            } else {
875              !!!cp (56);
876          }          }
877          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          $self->{current_attribute} = {name => chr ($self->{next_char}),
878                                value => ''};                                value => ''};
879          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
880          !!!next-input-character;          !!!next-input-character;
# Line 761  sub _get_next_token ($) { Line 884  sub _get_next_token ($) {
884        my $before_leave = sub {        my $before_leave = sub {
885          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
886              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
887              !!!cp (57);
888            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});
889            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
890          } else {          } else {
891              !!!cp (58);
892            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
893              = $self->{current_attribute};              = $self->{current_attribute};
894          }          }
895        }; # $before_leave        }; # $before_leave
896    
897        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
898            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
899            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
900            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
901            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
902            !!!cp (59);
903          $before_leave->();          $before_leave->();
904          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
905          !!!next-input-character;          !!!next-input-character;
906          redo A;          redo A;
907        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
908            !!!cp (60);
909          $before_leave->();          $before_leave->();
910          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
911          !!!next-input-character;          !!!next-input-character;
912          redo A;          redo A;
913        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
914          $before_leave->();          $before_leave->();
915          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
916            $self->{current_token}->{first_start_tag}            !!!cp (61);
               = not defined $self->{last_emitted_start_tag_name};  
917            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
918          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
919              !!!cp (62);
920            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
921            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
922              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 803  sub _get_next_token ($) { Line 930  sub _get_next_token ($) {
930          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
931    
932          redo A;          redo A;
933        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
934                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
935          $self->{current_attribute}->{name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (63);
936            $self->{current_attribute}->{name} .= chr ($self->{next_char} + 0x0020);
937          ## Stay in the state          ## Stay in the state
938          !!!next-input-character;          !!!next-input-character;
939          redo A;          redo A;
940        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
941          $before_leave->();          $before_leave->();
942          !!!next-input-character;          !!!next-input-character;
943          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_char} == 0x003E and # >
944              $self->{current_token}->{type} == START_TAG_TOKEN and              $self->{current_token}->{type} == START_TAG_TOKEN and
945              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
946            # permitted slash            # permitted slash
947              !!!cp (64);
948            #            #
949          } else {          } else {
950              !!!cp (65);
951            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
952          }          }
953          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
954          # next-input-character is already done          # next-input-character is already done
955          redo A;          redo A;
956        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
957          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
958          $before_leave->();          $before_leave->();
959          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
960            $self->{current_token}->{first_start_tag}            !!!cp (66);
               = not defined $self->{last_emitted_start_tag_name};  
961            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
962          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
963            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
964            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
965                !!!cp (67);
966              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
967              } else {
968                ## NOTE: This state should never be reached.
969                !!!cp (68);
970            }            }
971          } else {          } else {
972            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 845  sub _get_next_token ($) { Line 978  sub _get_next_token ($) {
978    
979          redo A;          redo A;
980        } else {        } else {
981          if ($self->{next_input_character} == 0x0022 or # "          if ($self->{next_char} == 0x0022 or # "
982              $self->{next_input_character} == 0x0027) { # '              $self->{next_char} == 0x0027) { # '
983              !!!cp (69);
984            !!!parse-error (type => 'bad attribute name');            !!!parse-error (type => 'bad attribute name');
985            } else {
986              !!!cp (70);
987          }          }
988          $self->{current_attribute}->{name} .= chr ($self->{next_input_character});          $self->{current_attribute}->{name} .= chr ($self->{next_char});
989          ## Stay in the state          ## Stay in the state
990          !!!next-input-character;          !!!next-input-character;
991          redo A;          redo A;
992        }        }
993      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
994        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
995            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
996            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
997            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
998            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
999            !!!cp (71);
1000          ## Stay in the state          ## Stay in the state
1001          !!!next-input-character;          !!!next-input-character;
1002          redo A;          redo A;
1003        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1004            !!!cp (72);
1005          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1006          !!!next-input-character;          !!!next-input-character;
1007          redo A;          redo A;
1008        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1009          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1010            $self->{current_token}->{first_start_tag}            !!!cp (73);
               = not defined $self->{last_emitted_start_tag_name};  
1011            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1012          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1013            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1014            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1015                !!!cp (74);
1016              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1017              } else {
1018                ## NOTE: This state should never be reached.
1019                !!!cp (75);
1020            }            }
1021          } else {          } else {
1022            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 886  sub _get_next_token ($) { Line 1027  sub _get_next_token ($) {
1027          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1028    
1029          redo A;          redo A;
1030        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1031                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1032          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (76);
1033            $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),
1034                                value => ''};                                value => ''};
1035          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1036          !!!next-input-character;          !!!next-input-character;
1037          redo A;          redo A;
1038        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1039          !!!next-input-character;          !!!next-input-character;
1040          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_char} == 0x003E and # >
1041              $self->{current_token}->{type} == START_TAG_TOKEN and              $self->{current_token}->{type} == START_TAG_TOKEN and
1042              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
1043            # permitted slash            # permitted slash
1044              !!!cp (77);
1045            #            #
1046          } else {          } else {
1047              !!!cp (78);
1048            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
1049            ## TODO: Different error type for <aa / bb> than <aa/>            ## TODO: Different error type for <aa / bb> than <aa/>
1050          }          }
1051          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1052          # next-input-character is already done          # next-input-character is already done
1053          redo A;          redo A;
1054        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1055          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1056          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1057            $self->{current_token}->{first_start_tag}            !!!cp (79);
               = not defined $self->{last_emitted_start_tag_name};  
1058            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1059          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1060            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1061            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1062                !!!cp (80);
1063              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1064              } else {
1065                ## NOTE: This state should never be reached.
1066                !!!cp (81);
1067            }            }
1068          } else {          } else {
1069            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 928  sub _get_next_token ($) { Line 1075  sub _get_next_token ($) {
1075    
1076          redo A;          redo A;
1077        } else {        } else {
1078          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          !!!cp (82);
1079            $self->{current_attribute} = {name => chr ($self->{next_char}),
1080                                value => ''};                                value => ''};
1081          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1082          !!!next-input-character;          !!!next-input-character;
1083          redo A;                  redo A;        
1084        }        }
1085      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
1086        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1087            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1088            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1089            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1090            $self->{next_input_character} == 0x0020) { # SP                  $self->{next_char} == 0x0020) { # SP      
1091            !!!cp (83);
1092          ## Stay in the state          ## Stay in the state
1093          !!!next-input-character;          !!!next-input-character;
1094          redo A;          redo A;
1095        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
1096            !!!cp (84);
1097          $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
1098          !!!next-input-character;          !!!next-input-character;
1099          redo A;          redo A;
1100        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1101            !!!cp (85);
1102          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1103          ## reconsume          ## reconsume
1104          redo A;          redo A;
1105        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
1106            !!!cp (86);
1107          $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
1108          !!!next-input-character;          !!!next-input-character;
1109          redo A;          redo A;
1110        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1111          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1112            $self->{current_token}->{first_start_tag}            !!!cp (87);
               = not defined $self->{last_emitted_start_tag_name};  
1113            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1114          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1115            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1116            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1117                !!!cp (88);
1118              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1119              } else {
1120                ## NOTE: This state should never be reached.
1121                !!!cp (89);
1122            }            }
1123          } else {          } else {
1124            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 974  sub _get_next_token ($) { Line 1129  sub _get_next_token ($) {
1129          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1130    
1131          redo A;          redo A;
1132        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1133          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1134          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1135            $self->{current_token}->{first_start_tag}            !!!cp (90);
               = not defined $self->{last_emitted_start_tag_name};  
1136            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1137          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1138            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1139            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1140                !!!cp (91);
1141              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1142              } else {
1143                ## NOTE: This state should never be reached.
1144                !!!cp (92);
1145            }            }
1146          } else {          } else {
1147            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 995  sub _get_next_token ($) { Line 1153  sub _get_next_token ($) {
1153    
1154          redo A;          redo A;
1155        } else {        } else {
1156          if ($self->{next_input_character} == 0x003D) { # =          if ($self->{next_char} == 0x003D) { # =
1157              !!!cp (93);
1158            !!!parse-error (type => 'bad attribute value');            !!!parse-error (type => 'bad attribute value');
1159            } else {
1160              !!!cp (94);
1161          }          }
1162          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          $self->{current_attribute}->{value} .= chr ($self->{next_char});
1163          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1164          !!!next-input-character;          !!!next-input-character;
1165          redo A;          redo A;
1166        }        }
1167      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
1168        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
1169            !!!cp (95);
1170          $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;          $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1171          !!!next-input-character;          !!!next-input-character;
1172          redo A;          redo A;
1173        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1174            !!!cp (96);
1175          $self->{last_attribute_value_state} = $self->{state};          $self->{last_attribute_value_state} = $self->{state};
1176          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1177          !!!next-input-character;          !!!next-input-character;
1178          redo A;          redo A;
1179        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1180          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1181          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1182            $self->{current_token}->{first_start_tag}            !!!cp (97);
               = not defined $self->{last_emitted_start_tag_name};  
1183            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1184          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1185            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1186            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1187                !!!cp (98);
1188              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1189              } else {
1190                ## NOTE: This state should never be reached.
1191                !!!cp (99);
1192            }            }
1193          } else {          } else {
1194            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 1034  sub _get_next_token ($) { Line 1200  sub _get_next_token ($) {
1200    
1201          redo A;          redo A;
1202        } else {        } else {
1203          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (100);
1204            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1205          ## Stay in the state          ## Stay in the state
1206          !!!next-input-character;          !!!next-input-character;
1207          redo A;          redo A;
1208        }        }
1209      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1210        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
1211            !!!cp (101);
1212          $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;          $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1213          !!!next-input-character;          !!!next-input-character;
1214          redo A;          redo A;
1215        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1216            !!!cp (102);
1217          $self->{last_attribute_value_state} = $self->{state};          $self->{last_attribute_value_state} = $self->{state};
1218          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1219          !!!next-input-character;          !!!next-input-character;
1220          redo A;          redo A;
1221        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1222          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1223          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1224            $self->{current_token}->{first_start_tag}            !!!cp (103);
               = not defined $self->{last_emitted_start_tag_name};  
1225            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1226          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1227            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1228            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1229                !!!cp (104);
1230              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1231              } else {
1232                ## NOTE: This state should never be reached.
1233                !!!cp (105);
1234            }            }
1235          } else {          } else {
1236            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 1070  sub _get_next_token ($) { Line 1242  sub _get_next_token ($) {
1242    
1243          redo A;          redo A;
1244        } else {        } else {
1245          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (106);
1246            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1247          ## Stay in the state          ## Stay in the state
1248          !!!next-input-character;          !!!next-input-character;
1249          redo A;          redo A;
1250        }        }
1251      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1252        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1253            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1254            $self->{next_input_character} == 0x000B or # HT            $self->{next_char} == 0x000B or # HT
1255            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1256            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1257            !!!cp (107);
1258          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1259          !!!next-input-character;          !!!next-input-character;
1260          redo A;          redo A;
1261        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1262            !!!cp (108);
1263          $self->{last_attribute_value_state} = $self->{state};          $self->{last_attribute_value_state} = $self->{state};
1264          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1265          !!!next-input-character;          !!!next-input-character;
1266          redo A;          redo A;
1267        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1268          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1269            $self->{current_token}->{first_start_tag}            !!!cp (109);
               = not defined $self->{last_emitted_start_tag_name};  
1270            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1271          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1272            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1273            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1274                !!!cp (110);
1275              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1276              } else {
1277                ## NOTE: This state should never be reached.
1278                !!!cp (111);
1279            }            }
1280          } else {          } else {
1281            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 1108  sub _get_next_token ($) { Line 1286  sub _get_next_token ($) {
1286          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1287    
1288          redo A;          redo A;
1289        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1290          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1291          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1292            $self->{current_token}->{first_start_tag}            !!!cp (112);
               = not defined $self->{last_emitted_start_tag_name};  
1293            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1294          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1295            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1296            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1297                !!!cp (113);
1298              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1299              } else {
1300                ## NOTE: This state should never be reached.
1301                !!!cp (114);
1302            }            }
1303          } else {          } else {
1304            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 1133  sub _get_next_token ($) { Line 1314  sub _get_next_token ($) {
1314               0x0022 => 1, # "               0x0022 => 1, # "
1315               0x0027 => 1, # '               0x0027 => 1, # '
1316               0x003D => 1, # =               0x003D => 1, # =
1317              }->{$self->{next_input_character}}) {              }->{$self->{next_char}}) {
1318              !!!cp (115);
1319            !!!parse-error (type => 'bad attribute value');            !!!parse-error (type => 'bad attribute value');
1320            } else {
1321              !!!cp (116);
1322          }          }
1323          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          $self->{current_attribute}->{value} .= chr ($self->{next_char});
1324          ## Stay in the state          ## Stay in the state
1325          !!!next-input-character;          !!!next-input-character;
1326          redo A;          redo A;
# Line 1151  sub _get_next_token ($) { Line 1335  sub _get_next_token ($) {
1335             -1);             -1);
1336    
1337        unless (defined $token) {        unless (defined $token) {
1338            !!!cp (117);
1339          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
1340        } else {        } else {
1341            !!!cp (118);
1342          $self->{current_attribute}->{value} .= $token->{data};          $self->{current_attribute}->{value} .= $token->{data};
1343          $self->{current_attribute}->{has_reference} = $token->{has_reference};          $self->{current_attribute}->{has_reference} = $token->{has_reference};
1344          ## ISSUE: spec says "append the returned character token to the current attribute's value"          ## ISSUE: spec says "append the returned character token to the current attribute's value"
# Line 1162  sub _get_next_token ($) { Line 1348  sub _get_next_token ($) {
1348        # next-input-character is already done        # next-input-character is already done
1349        redo A;        redo A;
1350      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1351        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1352            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1353            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1354            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1355            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1356            !!!cp (118);
1357          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1358          !!!next-input-character;          !!!next-input-character;
1359          redo A;          redo A;
1360        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1361          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1362            $self->{current_token}->{first_start_tag}            !!!cp (119);
               = not defined $self->{last_emitted_start_tag_name};  
1363            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1364          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1365            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1366            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1367                !!!cp (120);
1368              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1369              } else {
1370                ## NOTE: This state should never be reached.
1371                !!!cp (121);
1372            }            }
1373          } else {          } else {
1374            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 1189  sub _get_next_token ($) { Line 1379  sub _get_next_token ($) {
1379          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1380    
1381          redo A;          redo A;
1382        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1383          !!!next-input-character;          !!!next-input-character;
1384          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_char} == 0x003E and # >
1385              $self->{current_token}->{type} == START_TAG_TOKEN and              $self->{current_token}->{type} == START_TAG_TOKEN and
1386              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
1387            # permitted slash            # permitted slash
1388              !!!cp (122);
1389            #            #
1390          } else {          } else {
1391              !!!cp (123);
1392            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
1393          }          }
1394          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1395          # next-input-character is already done          # next-input-character is already done
1396          redo A;          redo A;
1397        } else {        } else {
1398            !!!cp (124);
1399          !!!parse-error (type => 'no space between attributes');          !!!parse-error (type => 'no space between attributes');
1400          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1401          ## reconsume          ## reconsume
# Line 1211  sub _get_next_token ($) { Line 1404  sub _get_next_token ($) {
1404      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1405        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1406                
1407        my $token = {type => COMMENT_TOKEN, data => ''};        ## NOTE: Set by the previous state
1408          #my $token = {type => COMMENT_TOKEN, data => ''};
1409    
1410        BC: {        BC: {
1411          if ($self->{next_input_character} == 0x003E) { # >          if ($self->{next_char} == 0x003E) { # >
1412              !!!cp (124);
1413            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1414            !!!next-input-character;            !!!next-input-character;
1415    
1416            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1417    
1418            redo A;            redo A;
1419          } elsif ($self->{next_input_character} == -1) {          } elsif ($self->{next_char} == -1) {
1420              !!!cp (125);
1421            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1422            ## reconsume            ## reconsume
1423    
1424            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1425    
1426            redo A;            redo A;
1427          } else {          } else {
1428            $token->{data} .= chr ($self->{next_input_character});            !!!cp (126);
1429              $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1430            !!!next-input-character;            !!!next-input-character;
1431            redo BC;            redo BC;
1432          }          }
1433        } # BC        } # BC
1434    
1435          die "$0: _get_next_token: unexpected case [BC]";
1436      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1437        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1438    
1439          #my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1);
1440    
1441        my @next_char;        my @next_char;
1442        push @next_char, $self->{next_input_character};        push @next_char, $self->{next_char};
1443                
1444        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1445          !!!next-input-character;          !!!next-input-character;
1446          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
1447          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_char} == 0x002D) { # -
1448            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};            !!!cp (127);
1449              $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1450                                        #line => $l, column => $c,
1451                                       };
1452            $self->{state} = COMMENT_START_STATE;            $self->{state} = COMMENT_START_STATE;
1453            !!!next-input-character;            !!!next-input-character;
1454            redo A;            redo A;
1455            } else {
1456              !!!cp (128);
1457          }          }
1458        } elsif ($self->{next_input_character} == 0x0044 or # D        } elsif ($self->{next_char} == 0x0044 or # D
1459                 $self->{next_input_character} == 0x0064) { # d                 $self->{next_char} == 0x0064) { # d
1460          !!!next-input-character;          !!!next-input-character;
1461          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
1462          if ($self->{next_input_character} == 0x004F or # O          if ($self->{next_char} == 0x004F or # O
1463              $self->{next_input_character} == 0x006F) { # o              $self->{next_char} == 0x006F) { # o
1464            !!!next-input-character;            !!!next-input-character;
1465            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_char};
1466            if ($self->{next_input_character} == 0x0043 or # C            if ($self->{next_char} == 0x0043 or # C
1467                $self->{next_input_character} == 0x0063) { # c                $self->{next_char} == 0x0063) { # c
1468              !!!next-input-character;              !!!next-input-character;
1469              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_char};
1470              if ($self->{next_input_character} == 0x0054 or # T              if ($self->{next_char} == 0x0054 or # T
1471                  $self->{next_input_character} == 0x0074) { # t                  $self->{next_char} == 0x0074) { # t
1472                !!!next-input-character;                !!!next-input-character;
1473                push @next_char, $self->{next_input_character};                push @next_char, $self->{next_char};
1474                if ($self->{next_input_character} == 0x0059 or # Y                if ($self->{next_char} == 0x0059 or # Y
1475                    $self->{next_input_character} == 0x0079) { # y                    $self->{next_char} == 0x0079) { # y
1476                  !!!next-input-character;                  !!!next-input-character;
1477                  push @next_char, $self->{next_input_character};                  push @next_char, $self->{next_char};
1478                  if ($self->{next_input_character} == 0x0050 or # P                  if ($self->{next_char} == 0x0050 or # P
1479                      $self->{next_input_character} == 0x0070) { # p                      $self->{next_char} == 0x0070) { # p
1480                    !!!next-input-character;                    !!!next-input-character;
1481                    push @next_char, $self->{next_input_character};                    push @next_char, $self->{next_char};
1482                    if ($self->{next_input_character} == 0x0045 or # E                    if ($self->{next_char} == 0x0045 or # E
1483                        $self->{next_input_character} == 0x0065) { # e                        $self->{next_char} == 0x0065) { # e
1484                      ## ISSUE: What a stupid code this is!                      !!!cp (129);
1485                        ## TODO: What a stupid code this is!
1486                      $self->{state} = DOCTYPE_STATE;                      $self->{state} = DOCTYPE_STATE;
1487                        $self->{current_token} = {type => DOCTYPE_TOKEN,
1488                                                  quirks => 1,
1489                                                  #line => $l, column => $c,
1490                                                 };
1491                      !!!next-input-character;                      !!!next-input-character;
1492                      redo A;                      redo A;
1493                      } else {
1494                        !!!cp (130);
1495                    }                    }
1496                    } else {
1497                      !!!cp (131);
1498                  }                  }
1499                  } else {
1500                    !!!cp (132);
1501                }                }
1502                } else {
1503                  !!!cp (133);
1504              }              }
1505              } else {
1506                !!!cp (134);
1507            }            }
1508            } else {
1509              !!!cp (135);
1510          }          }
1511          } else {
1512            !!!cp (136);
1513        }        }
1514    
1515        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'bogus comment');
1516        $self->{next_input_character} = shift @next_char;        $self->{next_char} = shift @next_char;
1517        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
1518        $self->{state} = BOGUS_COMMENT_STATE;        $self->{state} = BOGUS_COMMENT_STATE;
1519          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1520                                    #line => $l, column => $c,
1521                                   };
1522        redo A;        redo A;
1523                
1524        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
1525        ## 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?
1526      } elsif ($self->{state} == COMMENT_START_STATE) {      } elsif ($self->{state} == COMMENT_START_STATE) {
1527        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1528            !!!cp (137);
1529          $self->{state} = COMMENT_START_DASH_STATE;          $self->{state} = COMMENT_START_DASH_STATE;
1530          !!!next-input-character;          !!!next-input-character;
1531          redo A;          redo A;
1532        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1533            !!!cp (138);
1534          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
1535          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1536          !!!next-input-character;          !!!next-input-character;
# Line 1308  sub _get_next_token ($) { Line 1538  sub _get_next_token ($) {
1538          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1539    
1540          redo A;          redo A;
1541        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1542            !!!cp (139);
1543          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1544          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1545          ## reconsume          ## reconsume
# Line 1317  sub _get_next_token ($) { Line 1548  sub _get_next_token ($) {
1548    
1549          redo A;          redo A;
1550        } else {        } else {
1551            !!!cp (140);
1552          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
1553              .= chr ($self->{next_input_character});              .= chr ($self->{next_char});
1554          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
1555          !!!next-input-character;          !!!next-input-character;
1556          redo A;          redo A;
1557        }        }
1558      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
1559        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1560            !!!cp (141);
1561          $self->{state} = COMMENT_END_STATE;          $self->{state} = COMMENT_END_STATE;
1562          !!!next-input-character;          !!!next-input-character;
1563          redo A;          redo A;
1564        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1565            !!!cp (142);
1566          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
1567          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1568          !!!next-input-character;          !!!next-input-character;
# Line 1336  sub _get_next_token ($) { Line 1570  sub _get_next_token ($) {
1570          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1571    
1572          redo A;          redo A;
1573        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1574            !!!cp (143);
1575          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1576          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1577          ## reconsume          ## reconsume
# Line 1345  sub _get_next_token ($) { Line 1580  sub _get_next_token ($) {
1580    
1581          redo A;          redo A;
1582        } else {        } else {
1583            !!!cp (144);
1584          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
1585              .= '-' . chr ($self->{next_input_character});              .= '-' . chr ($self->{next_char});
1586          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
1587          !!!next-input-character;          !!!next-input-character;
1588          redo A;          redo A;
1589        }        }
1590      } elsif ($self->{state} == COMMENT_STATE) {      } elsif ($self->{state} == COMMENT_STATE) {
1591        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1592            !!!cp (145);
1593          $self->{state} = COMMENT_END_DASH_STATE;          $self->{state} = COMMENT_END_DASH_STATE;
1594          !!!next-input-character;          !!!next-input-character;
1595          redo A;          redo A;
1596        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1597            !!!cp (146);
1598          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1599          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1600          ## reconsume          ## reconsume
# Line 1365  sub _get_next_token ($) { Line 1603  sub _get_next_token ($) {
1603    
1604          redo A;          redo A;
1605        } else {        } else {
1606          $self->{current_token}->{data} .= chr ($self->{next_input_character}); # comment          !!!cp (147);
1607            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1608          ## Stay in the state          ## Stay in the state
1609          !!!next-input-character;          !!!next-input-character;
1610          redo A;          redo A;
1611        }        }
1612      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
1613        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1614            !!!cp (148);
1615          $self->{state} = COMMENT_END_STATE;          $self->{state} = COMMENT_END_STATE;
1616          !!!next-input-character;          !!!next-input-character;
1617          redo A;          redo A;
1618        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1619            !!!cp (149);
1620          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1621          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1622          ## reconsume          ## reconsume
# Line 1384  sub _get_next_token ($) { Line 1625  sub _get_next_token ($) {
1625    
1626          redo A;          redo A;
1627        } else {        } else {
1628          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          !!!cp (150);
1629            $self->{current_token}->{data} .= '-' . chr ($self->{next_char}); # comment
1630          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
1631          !!!next-input-character;          !!!next-input-character;
1632          redo A;          redo A;
1633        }        }
1634      } elsif ($self->{state} == COMMENT_END_STATE) {      } elsif ($self->{state} == COMMENT_END_STATE) {
1635        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
1636            !!!cp (151);
1637          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1638          !!!next-input-character;          !!!next-input-character;
1639    
1640          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1641    
1642          redo A;          redo A;
1643        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
1644          !!!parse-error (type => 'dash in comment');          !!!cp (152);
1645            !!!parse-error (type => 'dash in comment',
1646                            line => $self->{line_prev},
1647                            column => $self->{column_prev});
1648          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
1649          ## Stay in the state          ## Stay in the state
1650          !!!next-input-character;          !!!next-input-character;
1651          redo A;          redo A;
1652        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1653            !!!cp (153);
1654          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1655          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1656          ## reconsume          ## reconsume
# Line 1412  sub _get_next_token ($) { Line 1659  sub _get_next_token ($) {
1659    
1660          redo A;          redo A;
1661        } else {        } else {
1662          !!!parse-error (type => 'dash in comment');          !!!cp (154);
1663          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          !!!parse-error (type => 'dash in comment',
1664                            line => $self->{line_prev},
1665                            column => $self->{column_prev});
1666            $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
1667          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
1668          !!!next-input-character;          !!!next-input-character;
1669          redo A;          redo A;
1670        }        }
1671      } elsif ($self->{state} == DOCTYPE_STATE) {      } elsif ($self->{state} == DOCTYPE_STATE) {
1672        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1673            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1674            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1675            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1676            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1677            !!!cp (155);
1678          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1679          !!!next-input-character;          !!!next-input-character;
1680          redo A;          redo A;
1681        } else {        } else {
1682            !!!cp (156);
1683          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
1684          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1685          ## reconsume          ## reconsume
1686          redo A;          redo A;
1687        }        }
1688      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
1689        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1690            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1691            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1692            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1693            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1694            !!!cp (157);
1695          ## Stay in the state          ## Stay in the state
1696          !!!next-input-character;          !!!next-input-character;
1697          redo A;          redo A;
1698        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1699            !!!cp (158);
1700          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1701          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1702          !!!next-input-character;          !!!next-input-character;
1703    
1704          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
1705    
1706          redo A;          redo A;
1707        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1708            !!!cp (159);
1709          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1710          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1711          ## reconsume          ## reconsume
1712    
1713          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
1714    
1715          redo A;          redo A;
1716        } else {        } else {
1717          $self->{current_token}          !!!cp (160);
1718              = {type => DOCTYPE_TOKEN,          $self->{current_token}->{name} = chr $self->{next_char};
1719                 name => chr ($self->{next_input_character}),          delete $self->{current_token}->{quirks};
                correct => 1};  
1720  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
1721          $self->{state} = DOCTYPE_NAME_STATE;          $self->{state} = DOCTYPE_NAME_STATE;
1722          !!!next-input-character;          !!!next-input-character;
# Line 1470  sub _get_next_token ($) { Line 1724  sub _get_next_token ($) {
1724        }        }
1725      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
1726  ## ISSUE: Redundant "First," in the spec.  ## ISSUE: Redundant "First," in the spec.
1727        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1728            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1729            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1730            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1731            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1732            !!!cp (161);
1733          $self->{state} = AFTER_DOCTYPE_NAME_STATE;          $self->{state} = AFTER_DOCTYPE_NAME_STATE;
1734          !!!next-input-character;          !!!next-input-character;
1735          redo A;          redo A;
1736        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1737            !!!cp (162);
1738          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1739          !!!next-input-character;          !!!next-input-character;
1740    
1741          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1742    
1743          redo A;          redo A;
1744        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1745            !!!cp (163);
1746          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1747          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1748          ## reconsume          ## reconsume
1749    
1750          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1751          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1752    
1753          redo A;          redo A;
1754        } else {        } else {
1755            !!!cp (164);
1756          $self->{current_token}->{name}          $self->{current_token}->{name}
1757            .= chr ($self->{next_input_character}); # DOCTYPE            .= chr ($self->{next_char}); # DOCTYPE
1758          ## Stay in the state          ## Stay in the state
1759          !!!next-input-character;          !!!next-input-character;
1760          redo A;          redo A;
1761        }        }
1762      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
1763        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1764            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1765            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1766            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1767            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1768            !!!cp (165);
1769          ## Stay in the state          ## Stay in the state
1770          !!!next-input-character;          !!!next-input-character;
1771          redo A;          redo A;
1772        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1773            !!!cp (166);
1774          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1775          !!!next-input-character;          !!!next-input-character;
1776    
1777          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1778    
1779          redo A;          redo A;
1780        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1781            !!!cp (167);
1782          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1783          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1784          ## reconsume          ## reconsume
1785    
1786          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1787          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1788    
1789          redo A;          redo A;
1790        } elsif ($self->{next_input_character} == 0x0050 or # P        } elsif ($self->{next_char} == 0x0050 or # P
1791                 $self->{next_input_character} == 0x0070) { # p                 $self->{next_char} == 0x0070) { # p
1792          !!!next-input-character;          !!!next-input-character;
1793          if ($self->{next_input_character} == 0x0055 or # U          if ($self->{next_char} == 0x0055 or # U
1794              $self->{next_input_character} == 0x0075) { # u              $self->{next_char} == 0x0075) { # u
1795            !!!next-input-character;            !!!next-input-character;
1796            if ($self->{next_input_character} == 0x0042 or # B            if ($self->{next_char} == 0x0042 or # B
1797                $self->{next_input_character} == 0x0062) { # b                $self->{next_char} == 0x0062) { # b
1798              !!!next-input-character;              !!!next-input-character;
1799              if ($self->{next_input_character} == 0x004C or # L              if ($self->{next_char} == 0x004C or # L
1800                  $self->{next_input_character} == 0x006C) { # l                  $self->{next_char} == 0x006C) { # l
1801                !!!next-input-character;                !!!next-input-character;
1802                if ($self->{next_input_character} == 0x0049 or # I                if ($self->{next_char} == 0x0049 or # I
1803                    $self->{next_input_character} == 0x0069) { # i                    $self->{next_char} == 0x0069) { # i
1804                  !!!next-input-character;                  !!!next-input-character;
1805                  if ($self->{next_input_character} == 0x0043 or # C                  if ($self->{next_char} == 0x0043 or # C
1806                      $self->{next_input_character} == 0x0063) { # c                      $self->{next_char} == 0x0063) { # c
1807                      !!!cp (168);
1808                    $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;                    $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1809                    !!!next-input-character;                    !!!next-input-character;
1810                    redo A;                    redo A;
1811                    } else {
1812                      !!!cp (169);
1813                  }                  }
1814                  } else {
1815                    !!!cp (170);
1816                }                }
1817                } else {
1818                  !!!cp (171);
1819              }              }
1820              } else {
1821                !!!cp (172);
1822            }            }
1823            } else {
1824              !!!cp (173);
1825          }          }
1826    
1827          #          #
1828        } elsif ($self->{next_input_character} == 0x0053 or # S        } elsif ($self->{next_char} == 0x0053 or # S
1829                 $self->{next_input_character} == 0x0073) { # s                 $self->{next_char} == 0x0073) { # s
1830          !!!next-input-character;          !!!next-input-character;
1831          if ($self->{next_input_character} == 0x0059 or # Y          if ($self->{next_char} == 0x0059 or # Y
1832              $self->{next_input_character} == 0x0079) { # y              $self->{next_char} == 0x0079) { # y
1833            !!!next-input-character;            !!!next-input-character;
1834            if ($self->{next_input_character} == 0x0053 or # S            if ($self->{next_char} == 0x0053 or # S
1835                $self->{next_input_character} == 0x0073) { # s                $self->{next_char} == 0x0073) { # s
1836              !!!next-input-character;              !!!next-input-character;
1837              if ($self->{next_input_character} == 0x0054 or # T              if ($self->{next_char} == 0x0054 or # T
1838                  $self->{next_input_character} == 0x0074) { # t                  $self->{next_char} == 0x0074) { # t
1839                !!!next-input-character;                !!!next-input-character;
1840                if ($self->{next_input_character} == 0x0045 or # E                if ($self->{next_char} == 0x0045 or # E
1841                    $self->{next_input_character} == 0x0065) { # e                    $self->{next_char} == 0x0065) { # e
1842                  !!!next-input-character;                  !!!next-input-character;
1843                  if ($self->{next_input_character} == 0x004D or # M                  if ($self->{next_char} == 0x004D or # M
1844                      $self->{next_input_character} == 0x006D) { # m                      $self->{next_char} == 0x006D) { # m
1845                      !!!cp (174);
1846                    $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;                    $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1847                    !!!next-input-character;                    !!!next-input-character;
1848                    redo A;                    redo A;
1849                    } else {
1850                      !!!cp (175);
1851                  }                  }
1852                  } else {
1853                    !!!cp (176);
1854                }                }
1855                } else {
1856                  !!!cp (177);
1857              }              }
1858              } else {
1859                !!!cp (178);
1860            }            }
1861            } else {
1862              !!!cp (179);
1863          }          }
1864    
1865          #          #
1866        } else {        } else {
1867            !!!cp (180);
1868          !!!next-input-character;          !!!next-input-character;
1869          #          #
1870        }        }
1871    
1872        !!!parse-error (type => 'string after DOCTYPE name');        !!!parse-error (type => 'string after DOCTYPE name');
1873          $self->{current_token}->{quirks} = 1;
1874    
1875        $self->{state} = BOGUS_DOCTYPE_STATE;        $self->{state} = BOGUS_DOCTYPE_STATE;
1876        # next-input-character is already done        # next-input-character is already done
1877        redo A;        redo A;
# Line 1593  sub _get_next_token ($) { Line 1879  sub _get_next_token ($) {
1879        if ({        if ({
1880              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1881              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
1882            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
1883            !!!cp (181);
1884          ## Stay in the state          ## Stay in the state
1885          !!!next-input-character;          !!!next-input-character;
1886          redo A;          redo A;
1887        } elsif ($self->{next_input_character} eq 0x0022) { # "        } elsif ($self->{next_char} eq 0x0022) { # "
1888            !!!cp (182);
1889          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1890          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
1891          !!!next-input-character;          !!!next-input-character;
1892          redo A;          redo A;
1893        } elsif ($self->{next_input_character} eq 0x0027) { # '        } elsif ($self->{next_char} eq 0x0027) { # '
1894            !!!cp (183);
1895          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1896          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
1897          !!!next-input-character;          !!!next-input-character;
1898          redo A;          redo A;
1899        } elsif ($self->{next_input_character} eq 0x003E) { # >        } elsif ($self->{next_char} eq 0x003E) { # >
1900            !!!cp (184);
1901          !!!parse-error (type => 'no PUBLIC literal');          !!!parse-error (type => 'no PUBLIC literal');
1902    
1903          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1904          !!!next-input-character;          !!!next-input-character;
1905    
1906          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1907          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1908    
1909          redo A;          redo A;
1910        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1911            !!!cp (185);
1912          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1913    
1914          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1915          ## reconsume          ## reconsume
1916    
1917          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1918          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1919    
1920          redo A;          redo A;
1921        } else {        } else {
1922            !!!cp (186);
1923          !!!parse-error (type => 'string after PUBLIC');          !!!parse-error (type => 'string after PUBLIC');
1924            $self->{current_token}->{quirks} = 1;
1925    
1926          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
1927          !!!next-input-character;          !!!next-input-character;
1928          redo A;          redo A;
1929        }        }
1930      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
1931        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
1932            !!!cp (187);
1933          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1934          !!!next-input-character;          !!!next-input-character;
1935          redo A;          redo A;
1936        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1937            !!!cp (188);
1938          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
1939    
1940          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1941          !!!next-input-character;          !!!next-input-character;
1942    
1943          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1944          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1945    
1946          redo A;          redo A;
1947        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1948            !!!cp (189);
1949          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
1950    
1951          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1952          ## reconsume          ## reconsume
1953    
1954          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1955          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1956    
1957          redo A;          redo A;
1958        } else {        } else {
1959            !!!cp (190);
1960          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
1961              .= chr $self->{next_input_character};              .= chr $self->{next_char};
1962          ## Stay in the state          ## Stay in the state
1963          !!!next-input-character;          !!!next-input-character;
1964          redo A;          redo A;
1965        }        }
1966      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
1967        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
1968            !!!cp (191);
1969          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1970          !!!next-input-character;          !!!next-input-character;
1971          redo A;          redo A;
1972        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1973            !!!cp (192);
1974          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
1975    
1976          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1977          !!!next-input-character;          !!!next-input-character;
1978    
1979          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1980          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1981    
1982          redo A;          redo A;
1983        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1984            !!!cp (193);
1985          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
1986    
1987          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1988          ## reconsume          ## reconsume
1989    
1990          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1991          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1992    
1993          redo A;          redo A;
1994        } else {        } else {
1995            !!!cp (194);
1996          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
1997              .= chr $self->{next_input_character};              .= chr $self->{next_char};
1998          ## Stay in the state          ## Stay in the state
1999          !!!next-input-character;          !!!next-input-character;
2000          redo A;          redo A;
# Line 1701  sub _get_next_token ($) { Line 2003  sub _get_next_token ($) {
2003        if ({        if ({
2004              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2005              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2006            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2007            !!!cp (195);
2008          ## Stay in the state          ## Stay in the state
2009          !!!next-input-character;          !!!next-input-character;
2010          redo A;          redo A;
2011        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
2012            !!!cp (196);
2013          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2014          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2015          !!!next-input-character;          !!!next-input-character;
2016          redo A;          redo A;
2017        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
2018            !!!cp (197);
2019          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2020          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2021          !!!next-input-character;          !!!next-input-character;
2022          redo A;          redo A;
2023        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2024            !!!cp (198);
2025          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2026          !!!next-input-character;          !!!next-input-character;
2027    
2028          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2029    
2030          redo A;          redo A;
2031        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2032            !!!cp (199);
2033          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2034    
2035          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2036          ## reconsume          ## reconsume
2037    
2038          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2039          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2040    
2041          redo A;          redo A;
2042        } else {        } else {
2043            !!!cp (200);
2044          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after PUBLIC literal');
2045            $self->{current_token}->{quirks} = 1;
2046    
2047          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2048          !!!next-input-character;          !!!next-input-character;
2049          redo A;          redo A;
# Line 1742  sub _get_next_token ($) { Line 2052  sub _get_next_token ($) {
2052        if ({        if ({
2053              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2054              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2055            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2056            !!!cp (201);
2057          ## Stay in the state          ## Stay in the state
2058          !!!next-input-character;          !!!next-input-character;
2059          redo A;          redo A;
2060        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
2061            !!!cp (202);
2062          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2063          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2064          !!!next-input-character;          !!!next-input-character;
2065          redo A;          redo A;
2066        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
2067            !!!cp (203);
2068          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2069          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2070          !!!next-input-character;          !!!next-input-character;
2071          redo A;          redo A;
2072        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2073            !!!cp (204);
2074          !!!parse-error (type => 'no SYSTEM literal');          !!!parse-error (type => 'no SYSTEM literal');
2075          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2076          !!!next-input-character;          !!!next-input-character;
2077    
2078          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2079          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2080    
2081          redo A;          redo A;
2082        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2083            !!!cp (205);
2084          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2085    
2086          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2087          ## reconsume          ## reconsume
2088    
2089          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2090          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2091    
2092          redo A;          redo A;
2093        } else {        } else {
2094            !!!cp (206);
2095          !!!parse-error (type => 'string after SYSTEM');          !!!parse-error (type => 'string after SYSTEM');
2096            $self->{current_token}->{quirks} = 1;
2097    
2098          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2099          !!!next-input-character;          !!!next-input-character;
2100          redo A;          redo A;
2101        }        }
2102      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2103        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
2104            !!!cp (207);
2105          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2106          !!!next-input-character;          !!!next-input-character;
2107          redo A;          redo A;
2108        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2109            !!!cp (208);
2110          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2111    
2112          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2113          !!!next-input-character;          !!!next-input-character;
2114    
2115          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2116          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2117    
2118          redo A;          redo A;
2119        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2120            !!!cp (209);
2121          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2122    
2123          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2124          ## reconsume          ## reconsume
2125    
2126          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2127          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2128    
2129          redo A;          redo A;
2130        } else {        } else {
2131            !!!cp (210);
2132          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2133              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2134          ## Stay in the state          ## Stay in the state
2135          !!!next-input-character;          !!!next-input-character;
2136          redo A;          redo A;
2137        }        }
2138      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
2139        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
2140            !!!cp (211);
2141          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2142          !!!next-input-character;          !!!next-input-character;
2143          redo A;          redo A;
2144        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2145            !!!cp (212);
2146          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2147    
2148          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2149          !!!next-input-character;          !!!next-input-character;
2150    
2151          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2152          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2153    
2154          redo A;          redo A;
2155        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2156            !!!cp (213);
2157          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2158    
2159          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2160          ## reconsume          ## reconsume
2161    
2162          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2163          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2164    
2165          redo A;          redo A;
2166        } else {        } else {
2167            !!!cp (214);
2168          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2169              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2170          ## Stay in the state          ## Stay in the state
2171          !!!next-input-character;          !!!next-input-character;
2172          redo A;          redo A;
# Line 1849  sub _get_next_token ($) { Line 2175  sub _get_next_token ($) {
2175        if ({        if ({
2176              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2177              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2178            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2179            !!!cp (215);
2180          ## Stay in the state          ## Stay in the state
2181          !!!next-input-character;          !!!next-input-character;
2182          redo A;          redo A;
2183        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2184            !!!cp (216);
2185          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2186          !!!next-input-character;          !!!next-input-character;
2187    
2188          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2189    
2190          redo A;          redo A;
2191        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2192            !!!cp (217);
2193          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2194    
2195          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2196          ## reconsume          ## reconsume
2197    
2198          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2199          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2200    
2201          redo A;          redo A;
2202        } else {        } else {
2203            !!!cp (218);
2204          !!!parse-error (type => 'string after SYSTEM literal');          !!!parse-error (type => 'string after SYSTEM literal');
2205            #$self->{current_token}->{quirks} = 1;
2206    
2207          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2208          !!!next-input-character;          !!!next-input-character;
2209          redo A;          redo A;
2210        }        }
2211      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
2212        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2213            !!!cp (219);
2214          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2215          !!!next-input-character;          !!!next-input-character;
2216    
         delete $self->{current_token}->{correct};  
2217          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2218    
2219          redo A;          redo A;
2220        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2221            !!!cp (220);
2222          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2223          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2224          ## reconsume          ## reconsume
2225    
         delete $self->{current_token}->{correct};  
2226          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2227    
2228          redo A;          redo A;
2229        } else {        } else {
2230            !!!cp (221);
2231          ## Stay in the state          ## Stay in the state
2232          !!!next-input-character;          !!!next-input-character;
2233          redo A;          redo A;
# Line 1910  sub _get_next_token ($) { Line 2243  sub _get_next_token ($) {
2243  sub _tokenize_attempt_to_consume_an_entity ($$$) {  sub _tokenize_attempt_to_consume_an_entity ($$$) {
2244    my ($self, $in_attr, $additional) = @_;    my ($self, $in_attr, $additional) = @_;
2245    
2246      my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
2247    
2248    if ({    if ({
2249         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
2250         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
2251         $additional => 1,         $additional => 1,
2252        }->{$self->{next_input_character}}) {        }->{$self->{next_char}}) {
2253        !!!cp (1001);
2254      ## Don't consume      ## Don't consume
2255      ## No error      ## No error
2256      return undef;      return undef;
2257    } elsif ($self->{next_input_character} == 0x0023) { # #    } elsif ($self->{next_char} == 0x0023) { # #
2258      !!!next-input-character;      !!!next-input-character;
2259      if ($self->{next_input_character} == 0x0078 or # x      if ($self->{next_char} == 0x0078 or # x
2260          $self->{next_input_character} == 0x0058) { # X          $self->{next_char} == 0x0058) { # X
2261        my $code;        my $code;
2262        X: {        X: {
2263          my $x_char = $self->{next_input_character};          my $x_char = $self->{next_char};
2264          !!!next-input-character;          !!!next-input-character;
2265          if (0x0030 <= $self->{next_input_character} and          if (0x0030 <= $self->{next_char} and
2266              $self->{next_input_character} <= 0x0039) { # 0..9              $self->{next_char} <= 0x0039) { # 0..9
2267              !!!cp (1002);
2268            $code ||= 0;            $code ||= 0;
2269            $code *= 0x10;            $code *= 0x10;
2270            $code += $self->{next_input_character} - 0x0030;            $code += $self->{next_char} - 0x0030;
2271            redo X;            redo X;
2272          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
2273                   $self->{next_input_character} <= 0x0066) { # a..f                   $self->{next_char} <= 0x0066) { # a..f
2274              !!!cp (1003);
2275            $code ||= 0;            $code ||= 0;
2276            $code *= 0x10;            $code *= 0x10;
2277            $code += $self->{next_input_character} - 0x0060 + 9;            $code += $self->{next_char} - 0x0060 + 9;
2278            redo X;            redo X;
2279          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
2280                   $self->{next_input_character} <= 0x0046) { # A..F                   $self->{next_char} <= 0x0046) { # A..F
2281              !!!cp (1004);
2282            $code ||= 0;            $code ||= 0;
2283            $code *= 0x10;            $code *= 0x10;
2284            $code += $self->{next_input_character} - 0x0040 + 9;            $code += $self->{next_char} - 0x0040 + 9;
2285            redo X;            redo X;
2286          } elsif (not defined $code) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
2287            !!!parse-error (type => 'bare hcro');            !!!cp (1005);
2288            !!!back-next-input-character ($x_char, $self->{next_input_character});            !!!parse-error (type => 'bare hcro', line => $l, column => $c);
2289            $self->{next_input_character} = 0x0023; # #            !!!back-next-input-character ($x_char, $self->{next_char});
2290              $self->{next_char} = 0x0023; # #
2291            return undef;            return undef;
2292          } elsif ($self->{next_input_character} == 0x003B) { # ;          } elsif ($self->{next_char} == 0x003B) { # ;
2293              !!!cp (1006);
2294            !!!next-input-character;            !!!next-input-character;
2295          } else {          } else {
2296            !!!parse-error (type => 'no refc');            !!!cp (1007);
2297              !!!parse-error (type => 'no refc', line => $l, column => $c);
2298          }          }
2299    
2300          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2301            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);            !!!cp (1008);
2302              !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2303            $code = 0xFFFD;            $code = 0xFFFD;
2304          } elsif ($code > 0x10FFFF) {          } elsif ($code > 0x10FFFF) {
2305            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);            !!!cp (1009);
2306              !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2307            $code = 0xFFFD;            $code = 0xFFFD;
2308          } elsif ($code == 0x000D) {          } elsif ($code == 0x000D) {
2309            !!!parse-error (type => 'CR character reference');            !!!cp (1010);
2310              !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2311            $code = 0x000A;            $code = 0x000A;
2312          } elsif (0x80 <= $code and $code <= 0x9F) {          } elsif (0x80 <= $code and $code <= 0x9F) {
2313            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);            !!!cp (1011);
2314              !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2315            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
2316          }          }
2317    
2318          return {type => CHARACTER_TOKEN, data => chr $code,          return {type => CHARACTER_TOKEN, data => chr $code,
2319                  has_reference => 1};                  has_reference => 1,
2320                    #line => $l, column => $c,
2321                   };
2322        } # X        } # X
2323      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_char} and
2324               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_char} <= 0x0039) { # 0..9
2325        my $code = $self->{next_input_character} - 0x0030;        my $code = $self->{next_char} - 0x0030;
2326        !!!next-input-character;        !!!next-input-character;
2327                
2328        while (0x0030 <= $self->{next_input_character} and        while (0x0030 <= $self->{next_char} and
2329                  $self->{next_input_character} <= 0x0039) { # 0..9                  $self->{next_char} <= 0x0039) { # 0..9
2330            !!!cp (1012);
2331          $code *= 10;          $code *= 10;
2332          $code += $self->{next_input_character} - 0x0030;          $code += $self->{next_char} - 0x0030;
2333                    
2334          !!!next-input-character;          !!!next-input-character;
2335        }        }
2336    
2337        if ($self->{next_input_character} == 0x003B) { # ;        if ($self->{next_char} == 0x003B) { # ;
2338            !!!cp (1013);
2339          !!!next-input-character;          !!!next-input-character;
2340        } else {        } else {
2341          !!!parse-error (type => 'no refc');          !!!cp (1014);
2342            !!!parse-error (type => 'no refc', line => $l, column => $c);
2343        }        }
2344    
2345        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2346          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);          !!!cp (1015);
2347            !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2348          $code = 0xFFFD;          $code = 0xFFFD;
2349        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
2350          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);          !!!cp (1016);
2351            !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2352          $code = 0xFFFD;          $code = 0xFFFD;
2353        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
2354          !!!parse-error (type => 'CR character reference');          !!!cp (1017);
2355            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2356          $code = 0x000A;          $code = 0x000A;
2357        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
2358          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!cp (1018);
2359            !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2360          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
2361        }        }
2362                
2363        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,
2364                  #line => $l, column => $c,
2365                 };
2366      } else {      } else {
2367        !!!parse-error (type => 'bare nero');        !!!cp (1019);
2368        !!!back-next-input-character ($self->{next_input_character});        !!!parse-error (type => 'bare nero', line => $l, column => $c);
2369        $self->{next_input_character} = 0x0023; # #        !!!back-next-input-character ($self->{next_char});
2370          $self->{next_char} = 0x0023; # #
2371        return undef;        return undef;
2372      }      }
2373    } elsif ((0x0041 <= $self->{next_input_character} and    } elsif ((0x0041 <= $self->{next_char} and
2374              $self->{next_input_character} <= 0x005A) or              $self->{next_char} <= 0x005A) or
2375             (0x0061 <= $self->{next_input_character} and             (0x0061 <= $self->{next_char} and
2376              $self->{next_input_character} <= 0x007A)) {              $self->{next_char} <= 0x007A)) {
2377      my $entity_name = chr $self->{next_input_character};      my $entity_name = chr $self->{next_char};
2378      !!!next-input-character;      !!!next-input-character;
2379    
2380      my $value = $entity_name;      my $value = $entity_name;
# Line 2026  sub _tokenize_attempt_to_consume_an_enti Line 2384  sub _tokenize_attempt_to_consume_an_enti
2384    
2385      while (length $entity_name < 10 and      while (length $entity_name < 10 and
2386             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
2387             ((0x0041 <= $self->{next_input_character} and # a             ((0x0041 <= $self->{next_char} and # a
2388               $self->{next_input_character} <= 0x005A) or # x               $self->{next_char} <= 0x005A) or # x
2389              (0x0061 <= $self->{next_input_character} and # a              (0x0061 <= $self->{next_char} and # a
2390               $self->{next_input_character} <= 0x007A) or # z               $self->{next_char} <= 0x007A) or # z
2391              (0x0030 <= $self->{next_input_character} and # 0              (0x0030 <= $self->{next_char} and # 0
2392               $self->{next_input_character} <= 0x0039) or # 9               $self->{next_char} <= 0x0039) or # 9
2393              $self->{next_input_character} == 0x003B)) { # ;              $self->{next_char} == 0x003B)) { # ;
2394        $entity_name .= chr $self->{next_input_character};        $entity_name .= chr $self->{next_char};
2395        if (defined $EntityChar->{$entity_name}) {        if (defined $EntityChar->{$entity_name}) {
2396          if ($self->{next_input_character} == 0x003B) { # ;          if ($self->{next_char} == 0x003B) { # ;
2397              !!!cp (1020);
2398            $value = $EntityChar->{$entity_name};            $value = $EntityChar->{$entity_name};
2399            $match = 1;            $match = 1;
2400            !!!next-input-character;            !!!next-input-character;
2401            last;            last;
2402          } else {          } else {
2403              !!!cp (1021);
2404            $value = $EntityChar->{$entity_name};            $value = $EntityChar->{$entity_name};
2405            $match = -1;            $match = -1;
2406            !!!next-input-character;            !!!next-input-character;
2407          }          }
2408        } else {        } else {
2409          $value .= chr $self->{next_input_character};          !!!cp (1022);
2410            $value .= chr $self->{next_char};
2411          $match *= 2;          $match *= 2;
2412          !!!next-input-character;          !!!next-input-character;
2413        }        }
2414      }      }
2415            
2416      if ($match > 0) {      if ($match > 0) {
2417        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};        !!!cp (1023);
2418          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
2419                  #line => $l, column => $c,
2420                 };
2421      } elsif ($match < 0) {      } elsif ($match < 0) {
2422        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc', line => $l, column => $c);
2423        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
2424          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};          !!!cp (1024);
2425        } else {          return {type => CHARACTER_TOKEN, data => '&'.$entity_name,
2426          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};                  #line => $l, column => $c,
2427                   };
2428          } else {
2429            !!!cp (1025);
2430            return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
2431                    #line => $l, column => $c,
2432                   };
2433        }        }
2434      } else {      } else {
2435        !!!parse-error (type => 'bare ero');        !!!cp (1026);
2436          !!!parse-error (type => 'bare ero', line => $l, column => $c);
2437        ## NOTE: "No characters are consumed" in the spec.        ## NOTE: "No characters are consumed" in the spec.
2438        return {type => CHARACTER_TOKEN, data => '&'.$value};        return {type => CHARACTER_TOKEN, data => '&'.$value,
2439                  #line => $l, column => $c,
2440                 };
2441      }      }
2442    } else {    } else {
2443        !!!cp (1027);
2444      ## no characters are consumed      ## no characters are consumed
2445      !!!parse-error (type => 'bare ero');      !!!parse-error (type => 'bare ero', line => $l, column => $c);
2446      return undef;      return undef;
2447    }    }
2448  } # _tokenize_attempt_to_consume_an_entity  } # _tokenize_attempt_to_consume_an_entity
# Line 2106  sub _construct_tree ($) { Line 2480  sub _construct_tree ($) {
2480        
2481    !!!next-token;    !!!next-token;
2482    
   $self->{insertion_mode} = BEFORE_HEAD_IM;  
2483    undef $self->{form_element};    undef $self->{form_element};
2484    undef $self->{head_element};    undef $self->{head_element};
2485    $self->{open_elements} = [];    $self->{open_elements} = [];
2486    undef $self->{inner_html_node};    undef $self->{inner_html_node};
2487    
2488      ## NOTE: The "initial" insertion mode.
2489    $self->_tree_construction_initial; # MUST    $self->_tree_construction_initial; # MUST
2490    
2491      ## NOTE: The "before html" insertion mode.
2492    $self->_tree_construction_root_element;    $self->_tree_construction_root_element;
2493      $self->{insertion_mode} = BEFORE_HEAD_IM;
2494    
2495      ## NOTE: The "before head" insertion mode and so on.
2496    $self->_tree_construction_main;    $self->_tree_construction_main;
2497  } # _construct_tree  } # _construct_tree
2498    
2499  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
2500    my $self = shift;    my $self = shift;
2501    
2502      ## NOTE: "initial" insertion mode
2503    
2504    INITIAL: {    INITIAL: {
2505      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
2506        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
# Line 2130  sub _tree_construction_initial ($) { Line 2512  sub _tree_construction_initial ($) {
2512        if (not defined $token->{name} or # <!DOCTYPE>        if (not defined $token->{name} or # <!DOCTYPE>
2513            defined $token->{public_identifier} or            defined $token->{public_identifier} or
2514            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
2515          !!!parse-error (type => 'not HTML5');          !!!cp ('t1');
2516            !!!parse-error (type => 'not HTML5', token => $token);
2517        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
2518            !!!cp ('t2');
2519          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)
2520          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
2521          } else {
2522            !!!cp ('t3');
2523        }        }
2524                
2525        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
# Line 2146  sub _tree_construction_initial ($) { Line 2532  sub _tree_construction_initial ($) {
2532        ## ISSUE: internalSubset = null??        ## ISSUE: internalSubset = null??
2533        $self->{document}->append_child ($doctype);        $self->{document}->append_child ($doctype);
2534                
2535        if (not $token->{correct} or $doctype_name ne 'HTML') {        if ($token->{quirks} or $doctype_name ne 'HTML') {
2536            !!!cp ('t4');
2537          $self->{document}->manakai_compat_mode ('quirks');          $self->{document}->manakai_compat_mode ('quirks');
2538        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{public_identifier}) {
2539          my $pubid = $token->{public_identifier};          my $pubid = $token->{public_identifier};
# Line 2225  sub _tree_construction_initial ($) { Line 2612  sub _tree_construction_initial ($) {
2612            "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,            "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,
2613            "HTML" => 1,            "HTML" => 1,
2614          }->{$pubid}) {          }->{$pubid}) {
2615              !!!cp ('t5');
2616            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
2617          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or
2618                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {
2619            if (defined $token->{system_identifier}) {            if (defined $token->{system_identifier}) {
2620                !!!cp ('t6');
2621              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
2622            } else {            } else {
2623                !!!cp ('t7');
2624              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
2625            }            }
2626          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 Frameset//EN" or          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 FRAMESET//EN" or
2627                   $pubid eq "-//W3C//DTD XHTML 1.0 Transitional//EN") {                   $pubid eq "-//W3C//DTD XHTML 1.0 TRANSITIONAL//EN") {
2628              !!!cp ('t8');
2629            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
2630            } else {
2631              !!!cp ('t9');
2632          }          }
2633          } else {
2634            !!!cp ('t10');
2635        }        }
2636        if (defined $token->{system_identifier}) {        if (defined $token->{system_identifier}) {
2637          my $sysid = $token->{system_identifier};          my $sysid = $token->{system_identifier};
2638          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
2639          if ($sysid eq "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd") {          if ($sysid eq "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd") {
2640              ## TODO: Check the spec: PUBLIC "(limited quirks)" "(quirks)"
2641            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
2642              !!!cp ('t11');
2643            } else {
2644              !!!cp ('t12');
2645          }          }
2646          } else {
2647            !!!cp ('t13');
2648        }        }
2649                
2650        ## Go to the root element phase.        ## Go to the "before html" insertion mode.
2651        !!!next-token;        !!!next-token;
2652        return;        return;
2653      } elsif ({      } elsif ({
# Line 2254  sub _tree_construction_initial ($) { Line 2655  sub _tree_construction_initial ($) {
2655                END_TAG_TOKEN, 1,                END_TAG_TOKEN, 1,
2656                END_OF_FILE_TOKEN, 1,                END_OF_FILE_TOKEN, 1,
2657               }->{$token->{type}}) {               }->{$token->{type}}) {
2658        !!!parse-error (type => 'no DOCTYPE');        !!!cp ('t14');
2659          !!!parse-error (type => 'no DOCTYPE', token => $token);
2660        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
2661        ## Go to the root element phase        ## Go to the "before html" insertion mode.
2662        ## reprocess        ## reprocess
2663        return;        return;
2664      } elsif ($token->{type} == CHARACTER_TOKEN) {      } elsif ($token->{type} == CHARACTER_TOKEN) {
# Line 2264  sub _tree_construction_initial ($) { Line 2666  sub _tree_construction_initial ($) {
2666          ## Ignore the token          ## Ignore the token
2667    
2668          unless (length $token->{data}) {          unless (length $token->{data}) {
2669            ## Stay in the phase            !!!cp ('t15');
2670              ## Stay in the insertion mode.
2671            !!!next-token;            !!!next-token;
2672            redo INITIAL;            redo INITIAL;
2673            } else {
2674              !!!cp ('t16');
2675          }          }
2676          } else {
2677            !!!cp ('t17');
2678        }        }
2679    
2680        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
2681        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
2682        ## Go to the root element phase        ## Go to the "before html" insertion mode.
2683        ## reprocess        ## reprocess
2684        return;        return;
2685      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
2686          !!!cp ('t18');
2687        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
2688        $self->{document}->append_child ($comment);        $self->{document}->append_child ($comment);
2689                
2690        ## Stay in the phase.        ## Stay in the insertion mode.
2691        !!!next-token;        !!!next-token;
2692        redo INITIAL;        redo INITIAL;
2693      } else {      } else {
2694        die "$0: $token->{type}: Unknown token type";        die "$0: $token->{type}: Unknown token type";
2695      }      }
2696    } # INITIAL    } # INITIAL
2697    
2698      die "$0: _tree_construction_initial: This should be never reached";
2699  } # _tree_construction_initial  } # _tree_construction_initial
2700    
2701  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
2702    my $self = shift;    my $self = shift;
2703    
2704      ## NOTE: "before html" insertion mode.
2705        
2706    B: {    B: {
2707        if ($token->{type} == DOCTYPE_TOKEN) {        if ($token->{type} == DOCTYPE_TOKEN) {
2708          !!!parse-error (type => 'in html:#DOCTYPE');          !!!cp ('t19');
2709            !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
2710          ## Ignore the token          ## Ignore the token
2711          ## Stay in the phase          ## Stay in the insertion mode.
2712          !!!next-token;          !!!next-token;
2713          redo B;          redo B;
2714        } elsif ($token->{type} == COMMENT_TOKEN) {        } elsif ($token->{type} == COMMENT_TOKEN) {
2715            !!!cp ('t20');
2716          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
2717          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
2718          ## Stay in the phase          ## Stay in the insertion mode.
2719          !!!next-token;          !!!next-token;
2720          redo B;          redo B;
2721        } elsif ($token->{type} == CHARACTER_TOKEN) {        } elsif ($token->{type} == CHARACTER_TOKEN) {
# Line 2309  sub _tree_construction_root_element ($) Line 2723  sub _tree_construction_root_element ($)
2723            ## Ignore the token.            ## Ignore the token.
2724    
2725            unless (length $token->{data}) {            unless (length $token->{data}) {
2726              ## Stay in the phase              !!!cp ('t21');
2727                ## Stay in the insertion mode.
2728              !!!next-token;              !!!next-token;
2729              redo B;              redo B;
2730              } else {
2731                !!!cp ('t22');
2732            }            }
2733            } else {
2734              !!!cp ('t23');
2735          }          }
2736    
2737          $self->{application_cache_selection}->(undef);          $self->{application_cache_selection}->(undef);
2738    
2739          #          #
2740        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
2741          if ($token->{tag_name} eq 'html' and          if ($token->{tag_name} eq 'html') {
2742              $token->{attributes}->{manifest}) {            my $root_element;
2743            $self->{application_cache_selection}            !!!create-element ($root_element, $token->{tag_name}, $token->{attributes}, $token);
2744                 ->($token->{attributes}->{manifest}->{value});            $self->{document}->append_child ($root_element);
2745            ## ISSUE: No relative reference resolution?            push @{$self->{open_elements}}, [$root_element, 'html'];
2746    
2747              if ($token->{attributes}->{manifest}) {
2748                !!!cp ('t24');
2749                $self->{application_cache_selection}
2750                    ->($token->{attributes}->{manifest}->{value});
2751                ## ISSUE: Spec is unclear on relative references.
2752                ## According to Hixie (#whatwg 2008-03-19), it should be
2753                ## resolved against the base URI of the document in HTML
2754                ## or xml:base of the element in XHTML.
2755              } else {
2756                !!!cp ('t25');
2757                $self->{application_cache_selection}->(undef);
2758              }
2759    
2760              !!!next-token;
2761              return; ## Go to the "before head" insertion mode.
2762          } else {          } else {
2763            $self->{application_cache_selection}->(undef);            !!!cp ('t25.1');
2764              #
2765          }          }
   
         ## ISSUE: There is an issue in the spec  
         #  
2766        } elsif ({        } elsif ({
2767                  END_TAG_TOKEN, 1,                  END_TAG_TOKEN, 1,
2768                  END_OF_FILE_TOKEN, 1,                  END_OF_FILE_TOKEN, 1,
2769                 }->{$token->{type}}) {                 }->{$token->{type}}) {
2770          $self->{application_cache_selection}->(undef);          !!!cp ('t26');
   
         ## ISSUE: There is an issue in the spec  
2771          #          #
2772        } else {        } else {
2773          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
2774        }        }
2775    
2776        my $root_element; !!!create-element ($root_element, 'html');      my $root_element; !!!create-element ($root_element, 'html',, $token);
2777        $self->{document}->append_child ($root_element);      $self->{document}->append_child ($root_element);
2778        push @{$self->{open_elements}}, [$root_element, 'html'];      push @{$self->{open_elements}}, [$root_element, 'html'];
2779        ## reprocess  
2780        #redo B;      $self->{application_cache_selection}->(undef);
2781        return; ## Go to the main phase.  
2782        ## NOTE: Reprocess the token.
2783        return; ## Go to the "before head" insertion mode.
2784    
2785        ## ISSUE: There is an issue in the spec
2786    } # B    } # B
2787    
2788      die "$0: _tree_construction_root_element: This should never be reached";
2789  } # _tree_construction_root_element  } # _tree_construction_root_element
2790    
2791  sub _reset_insertion_mode ($) {  sub _reset_insertion_mode ($) {
# Line 2363  sub _reset_insertion_mode ($) { Line 2800  sub _reset_insertion_mode ($) {
2800            
2801      ## Step 3      ## Step 3
2802      S3: {      S3: {
       ## ISSUE: Oops! "If node is the first node in the stack of open  
       ## elements, then set last to true. If the context element of the  
       ## HTML fragment parsing algorithm is neither a td element nor a  
       ## th element, then set node to the context element. (fragment case)":  
       ## The second "if" is in the scope of the first "if"!?  
2803        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
2804          $last = 1;          $last = 1;
2805          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
2806            if ($self->{inner_html_node}->[1] eq 'td' or            if ($self->{inner_html_node}->[1] eq 'td' or
2807                $self->{inner_html_node}->[1] eq 'th') {                $self->{inner_html_node}->[1] eq 'th') {
2808                !!!cp ('t27');
2809              #              #
2810            } else {            } else {
2811                !!!cp ('t28');
2812              $node = $self->{inner_html_node};              $node = $self->{inner_html_node};
2813            }            }
2814          }          }
# Line 2383  sub _reset_insertion_mode ($) { Line 2817  sub _reset_insertion_mode ($) {
2817        ## Step 4..13        ## Step 4..13
2818        my $new_mode = {        my $new_mode = {
2819                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
2820                          ## NOTE: |option| and |optgroup| do not set
2821                          ## insertion mode to "in select" by themselves.
2822                        td => IN_CELL_IM,                        td => IN_CELL_IM,
2823                        th => IN_CELL_IM,                        th => IN_CELL_IM,
2824                        tr => IN_ROW_IM,                        tr => IN_ROW_IM,
# Line 2401  sub _reset_insertion_mode ($) { Line 2837  sub _reset_insertion_mode ($) {
2837        ## Step 14        ## Step 14
2838        if ($node->[1] eq 'html') {        if ($node->[1] eq 'html') {
2839          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
2840              !!!cp ('t29');
2841            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
2842          } else {          } else {
2843              ## ISSUE: Can this state be reached?
2844              !!!cp ('t30');
2845            $self->{insertion_mode} = AFTER_HEAD_IM;            $self->{insertion_mode} = AFTER_HEAD_IM;
2846          }          }
2847          return;          return;
2848          } else {
2849            !!!cp ('t31');
2850        }        }
2851                
2852        ## Step 15        ## Step 15
# Line 2418  sub _reset_insertion_mode ($) { Line 2859  sub _reset_insertion_mode ($) {
2859        ## Step 17        ## Step 17
2860        redo S3;        redo S3;
2861      } # S3      } # S3
2862    
2863      die "$0: _reset_insertion_mode: This line should never be reached";
2864  } # _reset_insertion_mode  } # _reset_insertion_mode
2865    
2866  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
# Line 2439  sub _tree_construction_main ($) { Line 2882  sub _tree_construction_main ($) {
2882      return if $entry->[0] eq '#marker';      return if $entry->[0] eq '#marker';
2883      for (@{$self->{open_elements}}) {      for (@{$self->{open_elements}}) {
2884        if ($entry->[0] eq $_->[0]) {        if ($entry->[0] eq $_->[0]) {
2885            !!!cp ('t32');
2886          return;          return;
2887        }        }
2888      }      }
# Line 2453  sub _tree_construction_main ($) { Line 2897  sub _tree_construction_main ($) {
2897    
2898        ## Step 6        ## Step 6
2899        if ($entry->[0] eq '#marker') {        if ($entry->[0] eq '#marker') {
2900            !!!cp ('t33_1');
2901          #          #
2902        } else {        } else {
2903          my $in_open_elements;          my $in_open_elements;
2904          OE: for (@{$self->{open_elements}}) {          OE: for (@{$self->{open_elements}}) {
2905            if ($entry->[0] eq $_->[0]) {            if ($entry->[0] eq $_->[0]) {
2906                !!!cp ('t33');
2907              $in_open_elements = 1;              $in_open_elements = 1;
2908              last OE;              last OE;
2909            }            }
2910          }          }
2911          if ($in_open_elements) {          if ($in_open_elements) {
2912              !!!cp ('t34');
2913            #            #
2914          } else {          } else {
2915              ## NOTE: <!DOCTYPE HTML><p><b><i><u></p> <p>X
2916              !!!cp ('t35');
2917            redo S4;            redo S4;
2918          }          }
2919        }        }
# Line 2487  sub _tree_construction_main ($) { Line 2936  sub _tree_construction_main ($) {
2936    
2937        ## Step 11        ## Step 11
2938        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {
2939            !!!cp ('t36');
2940          ## Step 7'          ## Step 7'
2941          $i++;          $i++;
2942          $entry = $active_formatting_elements->[$i];          $entry = $active_formatting_elements->[$i];
2943                    
2944          redo S7;          redo S7;
2945        }        }
2946    
2947          !!!cp ('t37');
2948      } # S7      } # S7
2949    }; # $reconstruct_active_formatting_elements    }; # $reconstruct_active_formatting_elements
2950    
2951    my $clear_up_to_marker = sub {    my $clear_up_to_marker = sub {
2952      for (reverse 0..$#$active_formatting_elements) {      for (reverse 0..$#$active_formatting_elements) {
2953        if ($active_formatting_elements->[$_]->[0] eq '#marker') {        if ($active_formatting_elements->[$_]->[0] eq '#marker') {
2954            !!!cp ('t38');
2955          splice @$active_formatting_elements, $_;          splice @$active_formatting_elements, $_;
2956          return;          return;
2957        }        }
2958      }      }
2959    
2960        !!!cp ('t39');
2961    }; # $clear_up_to_marker    }; # $clear_up_to_marker
2962    
2963    my $parse_rcdata = sub ($$) {    my $insert;
2964      my ($content_model_flag, $insert) = @_;  
2965      my $parse_rcdata = sub ($) {
2966        my ($content_model_flag) = @_;
2967    
2968      ## Step 1      ## Step 1
2969      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
2970      my $el;      my $el;
2971      !!!create-element ($el, $start_tag_name, $token->{attributes});      !!!create-element ($el, $start_tag_name, $token->{attributes}, $token);
2972    
2973      ## Step 2      ## Step 2
2974      $insert->($el); # /context node/->append_child ($el)      $insert->($el);
2975    
2976      ## Step 3      ## Step 3
2977      $self->{content_model} = $content_model_flag; # CDATA or RCDATA      $self->{content_model} = $content_model_flag; # CDATA or RCDATA
# Line 2524  sub _tree_construction_main ($) { Line 2981  sub _tree_construction_main ($) {
2981      my $text = '';      my $text = '';
2982      !!!next-token;      !!!next-token;
2983      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
2984          !!!cp ('t40');
2985        $text .= $token->{data};        $text .= $token->{data};
2986        !!!next-token;        !!!next-token;
2987      }      }
2988    
2989      ## Step 5      ## Step 5
2990      if (length $text) {      if (length $text) {
2991          !!!cp ('t41');
2992        my $text = $self->{document}->create_text_node ($text);        my $text = $self->{document}->create_text_node ($text);
2993        $el->append_child ($text);        $el->append_child ($text);
2994      }      }
# Line 2538  sub _tree_construction_main ($) { Line 2997  sub _tree_construction_main ($) {
2997      $self->{content_model} = PCDATA_CONTENT_MODEL;      $self->{content_model} = PCDATA_CONTENT_MODEL;
2998    
2999      ## Step 7      ## Step 7
3000      if ($token->{type} == END_TAG_TOKEN and $token->{tag_name} eq $start_tag_name) {      if ($token->{type} == END_TAG_TOKEN and
3001            $token->{tag_name} eq $start_tag_name) {
3002          !!!cp ('t42');
3003        ## Ignore the token        ## Ignore the token
     } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {  
       !!!parse-error (type => 'in CDATA:#'.$token->{type});  
     } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {  
       !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
3004      } else {      } else {
3005        die "$0: $content_model_flag in parse_rcdata";        ## NOTE: An end-of-file token.
3006          if ($content_model_flag == CDATA_CONTENT_MODEL) {
3007            !!!cp ('t43');
3008            !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3009          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3010            !!!cp ('t44');
3011            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
3012          } else {
3013            die "$0: $content_model_flag in parse_rcdata";
3014          }
3015      }      }
3016      !!!next-token;      !!!next-token;
3017    }; # $parse_rcdata    }; # $parse_rcdata
3018    
3019    my $script_start_tag = sub ($) {    my $script_start_tag = sub () {
     my $insert = $_[0];  
3020      my $script_el;      my $script_el;
3021      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, 'script', $token->{attributes}, $token);
3022      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3023    
3024      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
# Line 2562  sub _tree_construction_main ($) { Line 3027  sub _tree_construction_main ($) {
3027      my $text = '';      my $text = '';
3028      !!!next-token;      !!!next-token;
3029      while ($token->{type} == CHARACTER_TOKEN) {      while ($token->{type} == CHARACTER_TOKEN) {
3030          !!!cp ('t45');
3031        $text .= $token->{data};        $text .= $token->{data};
3032        !!!next-token;        !!!next-token;
3033      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
3034      if (length $text) {      if (length $text) {
3035          !!!cp ('t46');
3036        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
3037      }      }
3038                                
# Line 2573  sub _tree_construction_main ($) { Line 3040  sub _tree_construction_main ($) {
3040    
3041      if ($token->{type} == END_TAG_TOKEN and      if ($token->{type} == END_TAG_TOKEN and
3042          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
3043          !!!cp ('t47');
3044        ## Ignore the token        ## Ignore the token
3045      } else {      } else {
3046        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!cp ('t48');
3047          !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3048        ## ISSUE: And ignore?        ## ISSUE: And ignore?
3049        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3050      }      }
3051            
3052      if (defined $self->{inner_html_node}) {      if (defined $self->{inner_html_node}) {
3053          !!!cp ('t49');
3054        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3055      } else {      } else {
3056          !!!cp ('t50');
3057        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
3058        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
3059    
# Line 2596  sub _tree_construction_main ($) { Line 3067  sub _tree_construction_main ($) {
3067      !!!next-token;      !!!next-token;
3068    }; # $script_start_tag    }; # $script_start_tag
3069    
3070      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
3071      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
3072      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
3073    
3074    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
3075      my $tag_name = shift;      my $end_tag_token = shift;
3076        my $tag_name = $end_tag_token->{tag_name};
3077    
3078        ## NOTE: The adoption agency algorithm (AAA).
3079    
3080      FET: {      FET: {
3081        ## Step 1        ## Step 1
# Line 2605  sub _tree_construction_main ($) { Line 3083  sub _tree_construction_main ($) {
3083        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3084        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3085          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {
3086              !!!cp ('t51');
3087            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3088            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3089            last AFE;            last AFE;
3090          } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {          } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {
3091              !!!cp ('t52');
3092            last AFE;            last AFE;
3093          }          }
3094        } # AFE        } # AFE
3095        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3096          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!cp ('t53');
3097            !!!parse-error (type => 'unmatched end tag:'.$tag_name, token => $end_tag_token);
3098          ## Ignore the token          ## Ignore the token
3099          !!!next-token;          !!!next-token;
3100          return;          return;
# Line 2625  sub _tree_construction_main ($) { Line 3106  sub _tree_construction_main ($) {
3106          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3107          if ($node->[0] eq $formatting_element->[0]) {          if ($node->[0] eq $formatting_element->[0]) {
3108            if ($in_scope) {            if ($in_scope) {
3109                !!!cp ('t54');
3110              $formatting_element_i_in_open = $_;              $formatting_element_i_in_open = $_;
3111              last INSCOPE;              last INSCOPE;
3112            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3113              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t55');
3114                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3115                                token => $end_tag_token);
3116              ## Ignore the token              ## Ignore the token
3117              !!!next-token;              !!!next-token;
3118              return;              return;
3119            }            }
3120          } elsif ({          } elsif ({
3121                    table => 1, caption => 1, td => 1, th => 1,                    applet => 1, table => 1, caption => 1, td => 1, th => 1,
3122                    button => 1, marquee => 1, object => 1, html => 1,                    button => 1, marquee => 1, object => 1, html => 1,
3123                   }->{$node->[1]}) {                   }->{$node->[1]}) {
3124              !!!cp ('t56');
3125            $in_scope = 0;            $in_scope = 0;
3126          }          }
3127        } # INSCOPE        } # INSCOPE
3128        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3129          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t57');
3130            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3131                            token => $end_tag_token);
3132          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3133          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
3134          return;          return;
3135        }        }
3136        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3137          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!cp ('t58');
3138            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1],
3139                            token => $end_tag_token);
3140        }        }
3141                
3142        ## Step 2        ## Step 2
# Line 2658  sub _tree_construction_main ($) { Line 3147  sub _tree_construction_main ($) {
3147          if (not $formatting_category->{$node->[1]} and          if (not $formatting_category->{$node->[1]} and
3148              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3149              ($special_category->{$node->[1]} or              ($special_category->{$node->[1]} or
3150               $scoping_category->{$node->[1]})) {               $scoping_category->{$node->[1]})) { ## Scoping is redundant, maybe
3151              !!!cp ('t59');
3152            $furthest_block = $node;            $furthest_block = $node;
3153            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
3154          } elsif ($node->[0] eq $formatting_element->[0]) {          } elsif ($node->[0] eq $formatting_element->[0]) {
3155              !!!cp ('t60');
3156            last OE;            last OE;
3157          }          }
3158        } # OE        } # OE
3159                
3160        ## Step 3        ## Step 3
3161        unless (defined $furthest_block) { # MUST        unless (defined $furthest_block) { # MUST
3162            !!!cp ('t61');
3163          splice @{$self->{open_elements}}, $formatting_element_i_in_open;          splice @{$self->{open_elements}}, $formatting_element_i_in_open;
3164          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;
3165          !!!next-token;          !!!next-token;
# Line 2680  sub _tree_construction_main ($) { Line 3172  sub _tree_construction_main ($) {
3172        ## Step 5        ## Step 5
3173        my $furthest_block_parent = $furthest_block->[0]->parent_node;        my $furthest_block_parent = $furthest_block->[0]->parent_node;
3174        if (defined $furthest_block_parent) {        if (defined $furthest_block_parent) {
3175            !!!cp ('t62');
3176          $furthest_block_parent->remove_child ($furthest_block->[0]);          $furthest_block_parent->remove_child ($furthest_block->[0]);
3177        }        }
3178                
# Line 2702  sub _tree_construction_main ($) { Line 3195  sub _tree_construction_main ($) {
3195          S7S2: {          S7S2: {
3196            for (reverse 0..$#$active_formatting_elements) {            for (reverse 0..$#$active_formatting_elements) {
3197              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
3198                  !!!cp ('t63');
3199                $node_i_in_active = $_;                $node_i_in_active = $_;
3200                last S7S2;                last S7S2;
3201              }              }
# Line 2715  sub _tree_construction_main ($) { Line 3209  sub _tree_construction_main ($) {
3209                    
3210          ## Step 4          ## Step 4
3211          if ($last_node->[0] eq $furthest_block->[0]) {          if ($last_node->[0] eq $furthest_block->[0]) {
3212              !!!cp ('t64');
3213            $bookmark_prev_el = $node->[0];            $bookmark_prev_el = $node->[0];
3214          }          }
3215                    
3216          ## Step 5          ## Step 5
3217          if ($node->[0]->has_child_nodes ()) {          if ($node->[0]->has_child_nodes ()) {
3218              !!!cp ('t65');
3219            my $clone = [$node->[0]->clone_node (0), $node->[1]];            my $clone = [$node->[0]->clone_node (0), $node->[1]];
3220            $active_formatting_elements->[$node_i_in_active] = $clone;            $active_formatting_elements->[$node_i_in_active] = $clone;
3221            $self->{open_elements}->[$node_i_in_open] = $clone;            $self->{open_elements}->[$node_i_in_open] = $clone;
# Line 2737  sub _tree_construction_main ($) { Line 3233  sub _tree_construction_main ($) {
3233        } # S7          } # S7  
3234                
3235        ## Step 8        ## Step 8
3236        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ({
3237               table => 1, tbody => 1, tfoot => 1, thead => 1, tr => 1,
3238              }->{$common_ancestor_node->[1]}) {
3239            my $foster_parent_element;
3240            my $next_sibling;
3241                             OE: for (reverse 0..$#{$self->{open_elements}}) {
3242                               if ($self->{open_elements}->[$_]->[1] eq 'table') {
3243                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3244                                 if (defined $parent and $parent->node_type == 1) {
3245                                   !!!cp ('t65.1');
3246                                   $foster_parent_element = $parent;
3247                                   $next_sibling = $self->{open_elements}->[$_]->[0];
3248                                 } else {
3249                                   !!!cp ('t65.2');
3250                                   $foster_parent_element
3251                                     = $self->{open_elements}->[$_ - 1]->[0];
3252                                 }
3253                                 last OE;
3254                               }
3255                             } # OE
3256                             $foster_parent_element = $self->{open_elements}->[0]->[0]
3257                               unless defined $foster_parent_element;
3258            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
3259            $open_tables->[-1]->[1] = 1; # tainted
3260          } else {
3261            !!!cp ('t65.3');
3262            $common_ancestor_node->[0]->append_child ($last_node->[0]);
3263          }
3264                
3265        ## Step 9        ## Step 9
3266        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 2754  sub _tree_construction_main ($) { Line 3277  sub _tree_construction_main ($) {
3277        my $i;        my $i;
3278        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3279          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {
3280              !!!cp ('t66');
3281            splice @$active_formatting_elements, $_, 1;            splice @$active_formatting_elements, $_, 1;
3282            $i-- and last AFE if defined $i;            $i-- and last AFE if defined $i;
3283          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {
3284              !!!cp ('t67');
3285            $i = $_;            $i = $_;
3286          }          }
3287        } # AFE        } # AFE
# Line 2766  sub _tree_construction_main ($) { Line 3291  sub _tree_construction_main ($) {
3291        undef $i;        undef $i;
3292        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3293          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {
3294              !!!cp ('t68');
3295            splice @{$self->{open_elements}}, $_, 1;            splice @{$self->{open_elements}}, $_, 1;
3296            $i-- and last OE if defined $i;            $i-- and last OE if defined $i;
3297          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {
3298              !!!cp ('t69');
3299            $i = $_;            $i = $_;
3300          }          }
3301        } # OE        } # OE
# Line 2779  sub _tree_construction_main ($) { Line 3306  sub _tree_construction_main ($) {
3306      } # FET      } # FET
3307    }; # $formatting_end_tag    }; # $formatting_end_tag
3308    
3309    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
3310      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
3311    }; # $insert_to_current    }; # $insert_to_current
3312    
3313    my $insert_to_foster = sub {    my $insert_to_foster = sub {
3314                         my $child = shift;      my $child = shift;
3315                         if ({      if ({
3316                              table => 1, tbody => 1, tfoot => 1,           table => 1, tbody => 1, tfoot => 1, thead => 1, tr => 1,
3317                              thead => 1, tr => 1,          }->{$self->{open_elements}->[-1]->[1]}) {
3318                             }->{$self->{open_elements}->[-1]->[1]}) {        # MUST
3319                           # MUST        my $foster_parent_element;
3320                           my $foster_parent_element;        my $next_sibling;
                          my $next_sibling;  
3321                           OE: for (reverse 0..$#{$self->{open_elements}}) {                           OE: for (reverse 0..$#{$self->{open_elements}}) {
3322                             if ($self->{open_elements}->[$_]->[1] eq 'table') {                             if ($self->{open_elements}->[$_]->[1] eq 'table') {
3323                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3324                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3325                                   !!!cp ('t70');
3326                                 $foster_parent_element = $parent;                                 $foster_parent_element = $parent;
3327                                 $next_sibling = $self->{open_elements}->[$_]->[0];                                 $next_sibling = $self->{open_elements}->[$_]->[0];
3328                               } else {                               } else {
3329                                   !!!cp ('t71');
3330                                 $foster_parent_element                                 $foster_parent_element
3331                                   = $self->{open_elements}->[$_ - 1]->[0];                                   = $self->{open_elements}->[$_ - 1]->[0];
3332                               }                               }
# Line 2809  sub _tree_construction_main ($) { Line 3337  sub _tree_construction_main ($) {
3337                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
3338                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
3339                             ($child, $next_sibling);                             ($child, $next_sibling);
3340                         } else {        $open_tables->[-1]->[1] = 1; # tainted
3341                           $self->{open_elements}->[-1]->[0]->append_child ($child);      } else {
3342                         }        !!!cp ('t72');
3343          $self->{open_elements}->[-1]->[0]->append_child ($child);
3344        }
3345    }; # $insert_to_foster    }; # $insert_to_foster
3346    
   my $insert;  
   
3347    B: {    B: {
3348      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
3349        !!!parse-error (type => 'DOCTYPE in the middle');        !!!cp ('t73');
3350          !!!parse-error (type => 'DOCTYPE in the middle', token => $token);
3351        ## Ignore the token        ## Ignore the token
3352        ## Stay in the phase        ## Stay in the phase
3353        !!!next-token;        !!!next-token;
3354        redo B;        redo B;
     } elsif ($token->{type} == END_OF_FILE_TOKEN) {  
       if ($self->{insertion_mode} & AFTER_HTML_IMS) {  
         #  
       } else {  
         ## 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_TOKEN, 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]);  
         }  
   
         ## ISSUE: There is an issue in the spec.  
       }  
   
       ## Stop parsing  
       last B;  
3355      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
3356               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
3357        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
3358          ## Turn into the main phase          !!!cp ('t79');
3359          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html', token => $token);
3360          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
3361        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
3362          ## Turn into the main phase          !!!cp ('t80');
3363          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html', token => $token);
3364          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
3365          } else {
3366            !!!cp ('t81');
3367        }        }
3368    
3369  ## ISSUE: "aa<html>" is not a parse error.        !!!cp ('t82');
3370  ## ISSUE: "<html>" in fragment is not a parse error.        !!!parse-error (type => 'not first start tag', token => $token);
       unless ($token->{first_start_tag}) {  
         !!!parse-error (type => 'not first start tag');  
       }  
3371        my $top_el = $self->{open_elements}->[0]->[0];        my $top_el = $self->{open_elements}->[0]->[0];
3372        for my $attr_name (keys %{$token->{attributes}}) {        for my $attr_name (keys %{$token->{attributes}}) {
3373          unless ($top_el->has_attribute_ns (undef, $attr_name)) {          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
3374              !!!cp ('t84');
3375            $top_el->set_attribute_ns            $top_el->set_attribute_ns
3376              (undef, [undef, $attr_name],              (undef, [undef, $attr_name],
3377               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
# Line 2881  sub _tree_construction_main ($) { Line 3382  sub _tree_construction_main ($) {
3382      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
3383        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
3384        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
3385            !!!cp ('t85');
3386          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
3387        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
3388            !!!cp ('t86');
3389          $self->{open_elements}->[0]->[0]->append_child ($comment);          $self->{open_elements}->[0]->[0]->append_child ($comment);
3390        } else {        } else {
3391            !!!cp ('t87');
3392          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
3393        }        }
3394        !!!next-token;        !!!next-token;
# Line 2892  sub _tree_construction_main ($) { Line 3396  sub _tree_construction_main ($) {
3396      } elsif ($self->{insertion_mode} & HEAD_IMS) {      } elsif ($self->{insertion_mode} & HEAD_IMS) {
3397        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
3398          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3399            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3400                !!!cp ('t88.2');
3401                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3402              } else {
3403                !!!cp ('t88.1');
3404                ## Ignore the token.
3405                !!!next-token;
3406                redo B;
3407              }
3408            unless (length $token->{data}) {            unless (length $token->{data}) {
3409                !!!cp ('t88');
3410              !!!next-token;              !!!next-token;
3411              redo B;              redo B;
3412            }            }
3413          }          }
3414    
3415          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3416              !!!cp ('t89');
3417            ## As if <head>            ## As if <head>
3418            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, 'head',, $token);
3419            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3420            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3421    
# Line 2910  sub _tree_construction_main ($) { Line 3424  sub _tree_construction_main ($) {
3424    
3425            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
3426          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3427              !!!cp ('t90');
3428            ## As if </noscript>            ## As if </noscript>
3429            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
3430            !!!parse-error (type => 'in noscript:#character');            !!!parse-error (type => 'in noscript:#character', token => $token);
3431                        
3432            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
3433            ## As if </head>            ## As if </head>
# Line 2920  sub _tree_construction_main ($) { Line 3435  sub _tree_construction_main ($) {
3435    
3436            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
3437          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3438              !!!cp ('t91');
3439            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
3440    
3441            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
3442            } else {
3443              !!!cp ('t92');
3444          }          }
3445    
3446              ## "after head" insertion mode              ## "after head" insertion mode
3447              ## As if <body>              ## As if <body>
3448              !!!insert-element ('body');              !!!insert-element ('body',, $token);
3449              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
3450              ## reprocess              ## reprocess
3451              redo B;              redo B;
3452            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
3453              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
3454                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3455                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});                  !!!cp ('t93');
3456                    !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes}, $token);
3457                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3458                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];
3459                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3460                  !!!next-token;                  !!!next-token;
3461                  redo B;                  redo B;
3462                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3463                    !!!cp ('t94');
3464                  #                  #
3465                } else {                } else {
3466                  !!!parse-error (type => 'in head:head'); # or in head noscript                  !!!cp ('t95');
3467                    !!!parse-error (type => 'in head:head', token => $token); # or in head noscript
3468                  ## Ignore the token                  ## Ignore the token
3469                  !!!next-token;                  !!!next-token;
3470                  redo B;                  redo B;
3471                }                }
3472              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3473                  !!!cp ('t96');
3474                ## As if <head>                ## As if <head>
3475                !!!create-element ($self->{head_element}, 'head');                !!!create-element ($self->{head_element}, 'head',, $token);
3476                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3477                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3478    
3479                $self->{insertion_mode} = IN_HEAD_IM;                $self->{insertion_mode} = IN_HEAD_IM;
3480                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
3481                } else {
3482                  !!!cp ('t97');
3483              }              }
3484    
3485              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
3486                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3487                    !!!cp ('t98');
3488                  ## As if </noscript>                  ## As if </noscript>
3489                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3490                  !!!parse-error (type => 'in noscript:base');                  !!!parse-error (type => 'in noscript:base', token => $token);
3491                                
3492                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3493                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3494                  } else {
3495                    !!!cp ('t99');
3496                }                }
3497    
3498                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3499                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3500                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t100');
3501                    !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3502                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3503                  } else {
3504                    !!!cp ('t101');
3505                }                }
3506                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3507                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3508                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
3509                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3510                !!!next-token;                !!!next-token;
3511                redo B;                redo B;
3512              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
3513                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3514                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3515                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t102');
3516                    !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3517                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3518                  } else {
3519                    !!!cp ('t103');
3520                }                }
3521                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3522                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3523                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
3524                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3525                !!!next-token;                !!!next-token;
3526                redo B;                redo B;
3527              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
3528                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3529                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3530                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t104');
3531                    !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3532                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3533                  } else {
3534                    !!!cp ('t105');
3535                }                }
3536                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3537                my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3538    
3539                unless ($self->{confident}) {                unless ($self->{confident}) {
3540                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) { ## TODO: And if supported
3541                      !!!cp ('t106');
3542                    $self->{change_encoding}                    $self->{change_encoding}
3543                        ->($self, $token->{attributes}->{charset}->{value});                        ->($self, $token->{attributes}->{charset}->{value},
3544                             $token);
3545                                        
3546                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
3547                        ->set_user_data (manakai_has_reference =>                        ->set_user_data (manakai_has_reference =>
# Line 3016  sub _tree_construction_main ($) { Line 3554  sub _tree_construction_main ($) {
3554                            [\x09-\x0D\x20]*=                            [\x09-\x0D\x20]*=
3555                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
3556                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
3557                        !!!cp ('t107');
3558                      $self->{change_encoding}                      $self->{change_encoding}
3559                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
3560                               $token);
3561                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
3562                          ->set_user_data (manakai_has_reference =>                          ->set_user_data (manakai_has_reference =>
3563                                               $token->{attributes}->{content}                                               $token->{attributes}->{content}
3564                                                     ->{has_reference});                                                     ->{has_reference});
3565                      } else {
3566                        !!!cp ('t108');
3567                    }                    }
3568                  }                  }
3569                } else {                } else {
3570                  if ($token->{attributes}->{charset}) {                  if ($token->{attributes}->{charset}) {
3571                      !!!cp ('t109');
3572                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
3573                        ->set_user_data (manakai_has_reference =>                        ->set_user_data (manakai_has_reference =>
3574                                             $token->{attributes}->{charset}                                             $token->{attributes}->{charset}
3575                                                 ->{has_reference});                                                 ->{has_reference});
3576                  }                  }
3577                  if ($token->{attributes}->{content}) {                  if ($token->{attributes}->{content}) {
3578                      !!!cp ('t110');
3579                    $meta_el->[0]->get_attribute_node_ns (undef, 'content')                    $meta_el->[0]->get_attribute_node_ns (undef, 'content')
3580                        ->set_user_data (manakai_has_reference =>                        ->set_user_data (manakai_has_reference =>
3581                                             $token->{attributes}->{content}                                             $token->{attributes}->{content}
# Line 3039  sub _tree_construction_main ($) { Line 3583  sub _tree_construction_main ($) {
3583                  }                  }
3584                }                }
3585    
3586                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
3587                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3588                !!!next-token;                !!!next-token;
3589                redo B;                redo B;
3590              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
3591                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3592                    !!!cp ('t111');
3593                  ## As if </noscript>                  ## As if </noscript>
3594                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3595                  !!!parse-error (type => 'in noscript:title');                  !!!parse-error (type => 'in noscript:title', token => $token);
3596                                
3597                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3598                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3599                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3600                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t112');
3601                    !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3602                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3603                  } else {
3604                    !!!cp ('t113');
3605                }                }
3606    
3607                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3608                my $parent = defined $self->{head_element} ? $self->{head_element}                my $parent = defined $self->{head_element} ? $self->{head_element}
3609                    : $self->{open_elements}->[-1]->[0];                    : $self->{open_elements}->[-1]->[0];
3610                $parse_rcdata->(RCDATA_CONTENT_MODEL,                $parse_rcdata->(RCDATA_CONTENT_MODEL);
3611                                sub { $parent->append_child ($_[0]) });                pop @{$self->{open_elements}} # <head>
               pop @{$self->{open_elements}}  
3612                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3613                redo B;                redo B;
3614              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style') {
# Line 3069  sub _tree_construction_main ($) { Line 3616  sub _tree_construction_main ($) {
3616                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
3617                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3618                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3619                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t114');
3620                    !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3621                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3622                  } else {
3623                    !!!cp ('t115');
3624                }                }
3625                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL);
3626                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
3627                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3628                redo B;                redo B;
3629              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
3630                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
3631                    !!!cp ('t116');
3632                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
3633                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3634                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
3635                  !!!next-token;                  !!!next-token;
3636                  redo B;                  redo B;
3637                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3638                  !!!parse-error (type => 'in noscript:noscript');                  !!!cp ('t117');
3639                    !!!parse-error (type => 'in noscript:noscript', token => $token);
3640                  ## Ignore the token                  ## Ignore the token
3641                  !!!next-token;                  !!!next-token;
3642                  redo B;                  redo B;
3643                } else {                } else {
3644                    !!!cp ('t118');
3645                  #                  #
3646                }                }
3647              } elsif ($token->{tag_name} eq 'script') {              } elsif ($token->{tag_name} eq 'script') {
3648                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3649                    !!!cp ('t119');
3650                  ## As if </noscript>                  ## As if </noscript>
3651                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3652                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:script', token => $token);
3653                                
3654                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3655                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3656                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3657                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t120');
3658                    !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3659                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3660                  } else {
3661                    !!!cp ('t121');
3662                }                }
3663    
3664                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3665                $script_start_tag->($insert_to_current);                $script_start_tag->();
3666                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
3667                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3668                redo B;                redo B;
3669              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
3670                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
3671                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3672                    !!!cp ('t122');
3673                  ## As if </noscript>                  ## As if </noscript>
3674                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3675                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});                  !!!parse-error (type => 'in noscript:'.$token->{tag_name}, token => $token);
3676                                    
3677                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3678                  ## As if </head>                  ## As if </head>
# Line 3122  sub _tree_construction_main ($) { Line 3680  sub _tree_construction_main ($) {
3680                                    
3681                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
3682                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3683                    !!!cp ('t124');
3684                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3685                                    
3686                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
3687                  } else {
3688                    !!!cp ('t125');
3689                }                }
3690    
3691                ## "after head" insertion mode                ## "after head" insertion mode
3692                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3693                if ($token->{tag_name} eq 'body') {                if ($token->{tag_name} eq 'body') {
3694                    !!!cp ('t126');
3695                  $self->{insertion_mode} = IN_BODY_IM;                  $self->{insertion_mode} = IN_BODY_IM;
3696                } elsif ($token->{tag_name} eq 'frameset') {                } elsif ($token->{tag_name} eq 'frameset') {
3697                    !!!cp ('t127');
3698                  $self->{insertion_mode} = IN_FRAMESET_IM;                  $self->{insertion_mode} = IN_FRAMESET_IM;
3699                } else {                } else {
3700                  die "$0: tag name: $self->{tag_name}";                  die "$0: tag name: $self->{tag_name}";
# Line 3139  sub _tree_construction_main ($) { Line 3702  sub _tree_construction_main ($) {
3702                !!!next-token;                !!!next-token;
3703                redo B;                redo B;
3704              } else {              } else {
3705                  !!!cp ('t128');
3706                #                #
3707              }              }
3708    
3709              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3710                  !!!cp ('t129');
3711                ## As if </noscript>                ## As if </noscript>
3712                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3713                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
3714                                
3715                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
3716                ## As if </head>                ## As if </head>
# Line 3153  sub _tree_construction_main ($) { Line 3718  sub _tree_construction_main ($) {
3718    
3719                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
3720              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3721                  !!!cp ('t130');
3722                ## As if </head>                ## As if </head>
3723                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3724    
3725                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
3726                } else {
3727                  !!!cp ('t131');
3728              }              }
3729    
3730              ## "after head" insertion mode              ## "after head" insertion mode
3731              ## As if <body>              ## As if <body>
3732              !!!insert-element ('body');              !!!insert-element ('body',, $token);
3733              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
3734              ## reprocess              ## reprocess
3735              redo B;              redo B;
3736            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
3737              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
3738                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3739                    !!!cp ('t132');
3740                  ## As if <head>                  ## As if <head>
3741                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, 'head',, $token);
3742                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3743                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3744    
# Line 3179  sub _tree_construction_main ($) { Line 3748  sub _tree_construction_main ($) {
3748                  !!!next-token;                  !!!next-token;
3749                  redo B;                  redo B;
3750                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3751                    !!!cp ('t133');
3752                  ## As if </noscript>                  ## As if </noscript>
3753                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3754                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:/head', token => $token);
3755                                    
3756                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3757                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 3189  sub _tree_construction_main ($) { Line 3759  sub _tree_construction_main ($) {
3759                  !!!next-token;                  !!!next-token;
3760                  redo B;                  redo B;
3761                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3762                    !!!cp ('t134');
3763                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3764                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
3765                  !!!next-token;                  !!!next-token;
3766                  redo B;                  redo B;
3767                } else {                } else {
3768                    !!!cp ('t135');
3769                  #                  #
3770                }                }
3771              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
3772                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3773                    !!!cp ('t136');
3774                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3775                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3776                  !!!next-token;                  !!!next-token;
3777                  redo B;                  redo B;
3778                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3779                  !!!parse-error (type => 'unmatched end tag:noscript');                  !!!cp ('t137');
3780                    !!!parse-error (type => 'unmatched end tag:noscript', token => $token);
3781                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
3782                  !!!next-token;                  !!!next-token;
3783                  redo B;                  redo B;
3784                } else {                } else {
3785                    !!!cp ('t138');
3786                  #                  #
3787                }                }
3788              } elsif ({              } elsif ({
3789                        body => 1, html => 1,                        body => 1, html => 1,
3790                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3791                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3792                    !!!cp ('t139');
3793                  ## As if <head>                  ## As if <head>
3794                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, 'head',, $token);
3795                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3796                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3797    
3798                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3799                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3800                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3801                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t140');
3802                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
3803                  ## Ignore the token                  ## Ignore the token
3804                  !!!next-token;                  !!!next-token;
3805                  redo B;                  redo B;
3806                  } else {
3807                    !!!cp ('t141');
3808                }                }
3809                                
3810                #                #
# Line 3233  sub _tree_construction_main ($) { Line 3812  sub _tree_construction_main ($) {
3812                        p => 1, br => 1,                        p => 1, br => 1,
3813                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3814                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3815                    !!!cp ('t142');
3816                  ## As if <head>                  ## As if <head>
3817                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, 'head',, $token);
3818                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3819                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3820    
3821                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3822                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3823                  } else {
3824                    !!!cp ('t143');
3825                }                }
3826    
3827                #                #
3828              } else {              } else {
3829                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3830                    !!!cp ('t144');
3831                  #                  #
3832                } else {                } else {
3833                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t145');
3834                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
3835                  ## Ignore the token                  ## Ignore the token
3836                  !!!next-token;                  !!!next-token;
3837                  redo B;                  redo B;
# Line 3255  sub _tree_construction_main ($) { Line 3839  sub _tree_construction_main ($) {
3839              }              }
3840    
3841              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3842                  !!!cp ('t146');
3843                ## As if </noscript>                ## As if </noscript>
3844                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3845                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
3846                                
3847                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
3848                ## As if </head>                ## As if </head>
# Line 3265  sub _tree_construction_main ($) { Line 3850  sub _tree_construction_main ($) {
3850    
3851                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
3852              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3853                  !!!cp ('t147');
3854                ## As if </head>                ## As if </head>
3855                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3856    
3857                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
3858              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3859                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  ## ISSUE: This case cannot be reached?
3860                  !!!cp ('t148');
3861                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
3862                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
3863                !!!next-token;                !!!next-token;
3864                redo B;                redo B;
3865                } else {
3866                  !!!cp ('t149');
3867              }              }
3868    
3869              ## "after head" insertion mode              ## "after head" insertion mode
3870              ## As if <body>              ## As if <body>
3871              !!!insert-element ('body');              !!!insert-element ('body',, $token);
3872              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
3873              ## reprocess              ## reprocess
3874              redo B;              redo B;
3875            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
3876              die "$0: $token->{type}: Unknown token type";          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3877            }            !!!cp ('t149.1');
3878    
3879              ## NOTE: As if <head>
3880              !!!create-element ($self->{head_element}, 'head',, $token);
3881              $self->{open_elements}->[-1]->[0]->append_child
3882                  ($self->{head_element});
3883              #push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3884              #$self->{insertion_mode} = IN_HEAD_IM;
3885              ## NOTE: Reprocess.
3886    
3887              ## NOTE: As if </head>
3888              #pop @{$self->{open_elements}};
3889              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
3890              ## NOTE: Reprocess.
3891              
3892              #
3893            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3894              !!!cp ('t149.2');
3895    
3896              ## NOTE: As if </head>
3897              pop @{$self->{open_elements}};
3898              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
3899              ## NOTE: Reprocess.
3900    
3901              #
3902            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3903              !!!cp ('t149.3');
3904    
3905              !!!parse-error (type => 'in noscript:#eof', token => $token);
3906    
3907              ## As if </noscript>
3908              pop @{$self->{open_elements}};
3909              #$self->{insertion_mode} = IN_HEAD_IM;
3910              ## NOTE: Reprocess.
3911    
3912              ## NOTE: As if </head>
3913              pop @{$self->{open_elements}};
3914              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
3915              ## NOTE: Reprocess.
3916    
3917              #
3918            } else {
3919              !!!cp ('t149.4');
3920              #
3921            }
3922    
3923            ## NOTE: As if <body>
3924            !!!insert-element ('body',, $token);
3925            $self->{insertion_mode} = IN_BODY_IM;
3926            ## NOTE: Reprocess.
3927            redo B;
3928          } else {
3929            die "$0: $token->{type}: Unknown token type";
3930          }
3931    
3932            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
3933      } elsif ($self->{insertion_mode} & BODY_IMS) {      } elsif ($self->{insertion_mode} & BODY_IMS) {
3934            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
3935                !!!cp ('t150');
3936              ## NOTE: There is a code clone of "character in body".              ## NOTE: There is a code clone of "character in body".
3937              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
3938                            
# Line 3303  sub _tree_construction_main ($) { Line 3947  sub _tree_construction_main ($) {
3947                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
3948                if ($self->{insertion_mode} == IN_CELL_IM) {                if ($self->{insertion_mode} == IN_CELL_IM) {
3949                  ## have an element in table scope                  ## have an element in table scope
3950                  my $tn;                  for (reverse 0..$#{$self->{open_elements}}) {
                 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
3951                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
3952                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {
3953                      $tn = $node->[1];                      !!!cp ('t151');
3954                      last INSCOPE;  
3955                        ## Close the cell
3956                        !!!back-token; # <?>
3957                        $token = {type => END_TAG_TOKEN, tag_name => $node->[1],
3958                                  line => $token->{line},
3959                                  column => $token->{column}};
3960                        redo B;
3961                    } elsif ({                    } elsif ({
3962                              table => 1, html => 1,                              table => 1, html => 1,
3963                             }->{$node->[1]}) {                             }->{$node->[1]}) {
3964                      last INSCOPE;                      !!!cp ('t152');
3965                        ## ISSUE: This case can never be reached, maybe.
3966                        last;
3967                    }                    }
3968                  } # INSCOPE                  }
3969                    unless (defined $tn) {  
3970                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t153');
3971                      ## Ignore the token                  !!!parse-error (type => 'start tag not allowed',
3972                      !!!next-token;                      value => $token->{tag_name}, token => $token);
3973                      redo B;                  ## Ignore the token
3974                    }                  !!!next-token;
                   
                 ## Close the cell  
                 !!!back-token; # <?>  
                 $token = {type => END_TAG_TOKEN, tag_name => $tn};  
3975                  redo B;                  redo B;
3976                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3977                  !!!parse-error (type => 'not closed:caption');                  !!!parse-error (type => 'not closed:caption', token => $token);
3978                                    
3979                  ## As if </caption>                  ## NOTE: As if </caption>.
3980                  ## have a table element in table scope                  ## have a table element in table scope
3981                  my $i;                  my $i;
3982                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
3983                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
3984                    if ($node->[1] eq 'caption') {                      my $node = $self->{open_elements}->[$_];
3985                      $i = $_;                      if ($node->[1] eq 'caption') {
3986                      last INSCOPE;                        !!!cp ('t155');
3987                    } elsif ({                        $i = $_;
3988                              table => 1, html => 1,                        last INSCOPE;
3989                             }->{$node->[1]}) {                      } elsif ({
3990                      last INSCOPE;                                table => 1, html => 1,
3991                                 }->{$node->[1]}) {
3992                          !!!cp ('t156');
3993                          last;
3994                        }
3995                    }                    }
3996    
3997                      !!!cp ('t157');
3998                      !!!parse-error (type => 'start tag not allowed',
3999                                      value => $token->{tag_name}, token => $token);
4000                      ## Ignore the token
4001                      !!!next-token;
4002                      redo B;
4003                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!parse-error (type => 'unmatched end tag:caption');  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
4004                                    
4005                  ## generate implied end tags                  ## generate implied end tags
4006                  if ({                  while ({
4007                       dd => 1, dt => 1, li => 1, p => 1,                          dd => 1, dt => 1, li => 1, p => 1,
4008                       td => 1, th => 1, tr => 1,                         }->{$self->{open_elements}->[-1]->[1]}) {
4009                       tbody => 1, tfoot=> 1, thead => 1,                    !!!cp ('t158');
4010                      }->{$self->{open_elements}->[-1]->[1]}) {                    pop @{$self->{open_elements}};
                   !!!back-token; # <?>  
                   $token = {type => END_TAG_TOKEN, tag_name => 'caption'};  
                   !!!back-token;  
                   $token = {type => END_TAG_TOKEN,  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
4011                  }                  }
4012    
4013                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {
4014                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t159');
4015                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);
4016                    } else {
4017                      !!!cp ('t160');
4018                  }                  }
4019                                    
4020                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3377  sub _tree_construction_main ($) { Line 4026  sub _tree_construction_main ($) {
4026                  ## reprocess                  ## reprocess
4027                  redo B;                  redo B;
4028                } else {                } else {
4029                    !!!cp ('t161');
4030                  #                  #
4031                }                }
4032              } else {              } else {
4033                  !!!cp ('t162');
4034                #                #
4035              }              }
4036            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
# Line 3390  sub _tree_construction_main ($) { Line 4041  sub _tree_construction_main ($) {
4041                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4042                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4043                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[1] eq $token->{tag_name}) {
4044                        !!!cp ('t163');
4045                      $i = $_;                      $i = $_;
4046                      last INSCOPE;                      last INSCOPE;
4047                    } elsif ({                    } elsif ({
4048                              table => 1, html => 1,                              table => 1, html => 1,
4049                             }->{$node->[1]}) {                             }->{$node->[1]}) {
4050                        !!!cp ('t164');
4051                      last INSCOPE;                      last INSCOPE;
4052                    }                    }
4053                  } # INSCOPE                  } # INSCOPE
4054                    unless (defined $i) {                    unless (defined $i) {
4055                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!cp ('t165');
4056                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4057                      ## Ignore the token                      ## Ignore the token
4058                      !!!next-token;                      !!!next-token;
4059                      redo B;                      redo B;
4060                    }                    }
4061                                    
4062                  ## generate implied end tags                  ## generate implied end tags
4063                  if ({                  while ({
4064                       dd => 1, dt => 1, li => 1, p => 1,                          dd => 1, dt => 1, li => 1, p => 1,
4065                       td => ($token->{tag_name} eq 'th'),                         }->{$self->{open_elements}->[-1]->[1]}) {
4066                       th => ($token->{tag_name} eq 'td'),                    !!!cp ('t166');
4067                       tr => 1,                    pop @{$self->{open_elements}};
                      tbody => 1, tfoot=> 1, thead => 1,  
                     }->{$self->{open_elements}->[-1]->[1]}) {  
                   !!!back-token;  
                   $token = {type => END_TAG_TOKEN,  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
4068                  }                  }
4069                    
4070                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
4071                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t167');
4072                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);
4073                    } else {
4074                      !!!cp ('t168');
4075                  }                  }
4076                                    
4077                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3432  sub _tree_construction_main ($) { Line 4083  sub _tree_construction_main ($) {
4083                  !!!next-token;                  !!!next-token;
4084                  redo B;                  redo B;
4085                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4086                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t169');
4087                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4088                  ## Ignore the token                  ## Ignore the token
4089                  !!!next-token;                  !!!next-token;
4090                  redo B;                  redo B;
4091                } else {                } else {
4092                    !!!cp ('t170');
4093                  #                  #
4094                }                }
4095              } elsif ($token->{tag_name} eq 'caption') {              } elsif ($token->{tag_name} eq 'caption') {
4096                if ($self->{insertion_mode} == IN_CAPTION_IM) {                if ($self->{insertion_mode} == IN_CAPTION_IM) {
4097                  ## have a table element in table scope                  ## have a table element in table scope
4098                  my $i;                  my $i;
4099                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4100                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4101                    if ($node->[1] eq $token->{tag_name}) {                      my $node = $self->{open_elements}->[$_];
4102                      $i = $_;                      if ($node->[1] eq $token->{tag_name}) {
4103                      last INSCOPE;                        !!!cp ('t171');
4104                    } elsif ({                        $i = $_;
4105                              table => 1, html => 1,                        last INSCOPE;
4106                             }->{$node->[1]}) {                      } elsif ({
4107                      last INSCOPE;                                table => 1, html => 1,
4108                                 }->{$node->[1]}) {
4109                          !!!cp ('t172');
4110                          last;
4111                        }
4112                    }                    }
4113    
4114                      !!!cp ('t173');
4115                      !!!parse-error (type => 'unmatched end tag',
4116                                      value => $token->{tag_name}, token => $token);
4117                      ## Ignore the token
4118                      !!!next-token;
4119                      redo B;
4120                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
4121                                    
4122                  ## generate implied end tags                  ## generate implied end tags
4123                  if ({                  while ({
4124                       dd => 1, dt => 1, li => 1, p => 1,                          dd => 1, dt => 1, li => 1, p => 1,
4125                       td => 1, th => 1, tr => 1,                         }->{$self->{open_elements}->[-1]->[1]}) {
4126                       tbody => 1, tfoot=> 1, thead => 1,                    !!!cp ('t174');
4127                      }->{$self->{open_elements}->[-1]->[1]}) {                    pop @{$self->{open_elements}};
                   !!!back-token;  
                   $token = {type => END_TAG_TOKEN,  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
4128                  }                  }
4129                                    
4130                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {
4131                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t175');
4132                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);
4133                    } else {
4134                      !!!cp ('t176');
4135                  }                  }
4136                                    
4137                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3486  sub _tree_construction_main ($) { Line 4143  sub _tree_construction_main ($) {
4143                  !!!next-token;                  !!!next-token;
4144                  redo B;                  redo B;
4145                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
4146                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t177');
4147                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4148                  ## Ignore the token                  ## Ignore the token
4149                  !!!next-token;                  !!!next-token;
4150                  redo B;                  redo B;
4151                } else {                } else {
4152                    !!!cp ('t178');
4153                  #                  #
4154                }                }
4155              } elsif ({              } elsif ({
# Line 3501  sub _tree_construction_main ($) { Line 4160  sub _tree_construction_main ($) {
4160                ## have an element in table scope                ## have an element in table scope
4161                my $i;                my $i;
4162                my $tn;                my $tn;
4163                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: {
4164                  my $node = $self->{open_elements}->[$_];                  for (reverse 0..$#{$self->{open_elements}}) {
4165                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
4166                    $i = $_;                    if ($node->[1] eq $token->{tag_name}) {
4167                    last INSCOPE;                      !!!cp ('t179');
4168                  } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {                      $i = $_;
4169                    $tn = $node->[1];  
4170                    ## NOTE: There is exactly one |td| or |th| element                      ## Close the cell
4171                    ## in scope in the stack of open elements by definition.                      !!!back-token; # </?>
4172                  } elsif ({                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
4173                            table => 1, html => 1,                                line => $token->{line},
4174                           }->{$node->[1]}) {                                column => $token->{column}};
4175                    last INSCOPE;                      redo B;
4176                      } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {
4177                        !!!cp ('t180');
4178                        $tn = $node->[1];
4179                        ## NOTE: There is exactly one |td| or |th| element
4180                        ## in scope in the stack of open elements by definition.
4181                      } elsif ({
4182                                table => 1, html => 1,
4183                               }->{$node->[1]}) {
4184                        ## ISSUE: Can this be reached?
4185                        !!!cp ('t181');
4186                        last;
4187                      }
4188                  }                  }
4189                } # INSCOPE  
4190                unless (defined $i) {                  !!!cp ('t182');
4191                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
4192                        value => $token->{tag_name}, token => $token);
4193                  ## Ignore the token                  ## Ignore the token
4194                  !!!next-token;                  !!!next-token;
4195                  redo B;                  redo B;
4196                }                } # INSCOPE
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => END_TAG_TOKEN, tag_name => $tn};  
               redo B;  
4197              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
4198                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4199                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption', token => $token);
4200    
4201                ## As if </caption>                ## As if </caption>
4202                ## have a table element in table scope                ## have a table element in table scope
# Line 3537  sub _tree_construction_main ($) { Line 4204  sub _tree_construction_main ($) {
4204                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4205                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4206                  if ($node->[1] eq 'caption') {                  if ($node->[1] eq 'caption') {
4207                      !!!cp ('t184');
4208                    $i = $_;                    $i = $_;
4209                    last INSCOPE;                    last INSCOPE;
4210                  } elsif ({                  } elsif ({
4211                            table => 1, html => 1,                            table => 1, html => 1,
4212                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4213                      !!!cp ('t185');
4214                    last INSCOPE;                    last INSCOPE;
4215                  }                  }
4216                } # INSCOPE                } # INSCOPE
4217                unless (defined $i) {                unless (defined $i) {
4218                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!cp ('t186');
4219                    !!!parse-error (type => 'unmatched end tag:caption', token => $token);
4220                  ## Ignore the token                  ## Ignore the token
4221                  !!!next-token;                  !!!next-token;
4222                  redo B;                  redo B;
4223                }                }
4224                                
4225                ## generate implied end tags                ## generate implied end tags
4226                if ({                while ({
4227                     dd => 1, dt => 1, li => 1, p => 1,                        dd => 1, dt => 1, li => 1, p => 1,
4228                     td => 1, th => 1, tr => 1,                       }->{$self->{open_elements}->[-1]->[1]}) {
4229                     tbody => 1, tfoot=> 1, thead => 1,                  !!!cp ('t187');
4230                    }->{$self->{open_elements}->[-1]->[1]}) {                  pop @{$self->{open_elements}};
                 !!!back-token; # </table>  
                 $token = {type => END_TAG_TOKEN, tag_name => 'caption'};  
                 !!!back-token;  
                 $token = {type => END_TAG_TOKEN,  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
4231                }                }
4232    
4233                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                if ($self->{open_elements}->[-1]->[1] ne 'caption') {
4234                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t188');
4235                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);
4236                  } else {
4237                    !!!cp ('t189');
4238                }                }
4239    
4240                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
# Line 3582  sub _tree_construction_main ($) { Line 4249  sub _tree_construction_main ($) {
4249                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
4250                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4251                if ($self->{insertion_mode} & BODY_TABLE_IMS) {                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
4252                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t190');
4253                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4254                  ## Ignore the token                  ## Ignore the token
4255                  !!!next-token;                  !!!next-token;
4256                  redo B;                  redo B;
4257                } else {                } else {
4258                    !!!cp ('t191');
4259                  #                  #
4260                }                }
4261              } elsif ({              } elsif ({
# Line 3594  sub _tree_construction_main ($) { Line 4263  sub _tree_construction_main ($) {
4263                        thead => 1, tr => 1,                        thead => 1, tr => 1,
4264                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
4265                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4266                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t192');
4267                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4268                ## Ignore the token                ## Ignore the token
4269                !!!next-token;                !!!next-token;
4270                redo B;                redo B;
4271              } else {              } else {
4272                  !!!cp ('t193');
4273                #                #
4274              }              }
4275          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4276            for my $entry (@{$self->{open_elements}}) {
4277              if (not {
4278                dd => 1, dt => 1, li => 1, p => 1, tbody => 1, td => 1, tfoot => 1,
4279                th => 1, thead => 1, tr => 1, body => 1, html => 1,
4280              }->{$entry->[1]}) {
4281                !!!cp ('t75');
4282                !!!parse-error (type => 'in body:#eof', token => $token);
4283                last;
4284              }
4285            }
4286    
4287            ## Stop parsing.
4288            last B;
4289        } else {        } else {
4290          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
4291        }        }
# Line 3609  sub _tree_construction_main ($) { Line 4294  sub _tree_construction_main ($) {
4294        #        #
4295      } elsif ($self->{insertion_mode} & TABLE_IMS) {      } elsif ($self->{insertion_mode} & TABLE_IMS) {
4296        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4297              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if (not $open_tables->[-1]->[1] and # tainted
4298                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);              $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4299              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4300                                
4301                unless (length $token->{data}) {            unless (length $token->{data}) {
4302                  !!!next-token;              !!!cp ('t194');
4303                  redo B;              !!!next-token;
4304                }              redo B;
4305              }            } else {
4306                !!!cp ('t195');
4307              }
4308            }
4309    
4310              !!!parse-error (type => 'in table:#character');              !!!parse-error (type => 'in table:#character', token => $token);
4311    
4312              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
4313              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
# Line 3638  sub _tree_construction_main ($) { Line 4327  sub _tree_construction_main ($) {
4327                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] eq 'table') {
4328                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4329                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
4330                        !!!cp ('t196');
4331                      $foster_parent_element = $parent;                      $foster_parent_element = $parent;
4332                      $next_sibling = $self->{open_elements}->[$_]->[0];                      $next_sibling = $self->{open_elements}->[$_]->[0];
4333                      $prev_sibling = $next_sibling->previous_sibling;                      $prev_sibling = $next_sibling->previous_sibling;
4334                    } else {                    } else {
4335                        !!!cp ('t197');
4336                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
4337                      $prev_sibling = $foster_parent_element->last_child;                      $prev_sibling = $foster_parent_element->last_child;
4338                    }                    }
# Line 3653  sub _tree_construction_main ($) { Line 4344  sub _tree_construction_main ($) {
4344                  unless defined $foster_parent_element;                  unless defined $foster_parent_element;
4345                if (defined $prev_sibling and                if (defined $prev_sibling and
4346                    $prev_sibling->node_type == 3) {                    $prev_sibling->node_type == 3) {
4347                    !!!cp ('t198');
4348                  $prev_sibling->manakai_append_text ($token->{data});                  $prev_sibling->manakai_append_text ($token->{data});
4349                } else {                } else {
4350                    !!!cp ('t199');
4351                  $foster_parent_element->insert_before                  $foster_parent_element->insert_before
4352                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
4353                     $next_sibling);                     $next_sibling);
4354                }                }
4355              } else {            $open_tables->[-1]->[1] = 1; # tainted
4356                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
4357              }            !!!cp ('t200');
4358              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4359            }
4360                            
4361              !!!next-token;          !!!next-token;
4362              redo B;          redo B;
4363        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4364              if ({              if ({
4365                   tr => ($self->{insertion_mode} != IN_ROW_IM),                   tr => ($self->{insertion_mode} != IN_ROW_IM),
# Line 3674  sub _tree_construction_main ($) { Line 4369  sub _tree_construction_main ($) {
4369                  ## Clear back to table context                  ## Clear back to table context
4370                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while ($self->{open_elements}->[-1]->[1] ne 'table' and
4371                         $self->{open_elements}->[-1]->[1] ne 'html') {                         $self->{open_elements}->[-1]->[1] ne 'html') {
4372                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t201');
4373                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4374                  }                  }
4375                                    
4376                  !!!insert-element ('tbody');                  !!!insert-element ('tbody',, $token);
4377                  $self->{insertion_mode} = IN_TABLE_BODY_IM;                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
4378                  ## reprocess in the "in table body" insertion mode...                  ## reprocess in the "in table body" insertion mode...
4379                }                }
4380    
4381                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
4382                  unless ($token->{tag_name} eq 'tr') {                  unless ($token->{tag_name} eq 'tr') {
4383                    !!!parse-error (type => 'missing start tag:tr');                    !!!cp ('t202');
4384                      !!!parse-error (type => 'missing start tag:tr', token => $token);
4385                  }                  }
4386                                    
4387                  ## Clear back to table body context                  ## Clear back to table body context
4388                  while (not {                  while (not {
4389                    tbody => 1, tfoot => 1, thead => 1, html => 1,                    tbody => 1, tfoot => 1, thead => 1, html => 1,
4390                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4391                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t203');
4392                      ## ISSUE: Can this case be reached?
4393                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4394                  }                  }
4395                                    
4396                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
4397                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
4398                    !!!insert-element ($token->{tag_name}, $token->{attributes});                    !!!cp ('t204');
4399                      !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4400                    !!!next-token;                    !!!next-token;
4401                    redo B;                    redo B;
4402                  } else {                  } else {
4403                    !!!insert-element ('tr');                    !!!cp ('t205');
4404                      !!!insert-element ('tr',, $token);
4405                    ## reprocess in the "in row" insertion mode                    ## reprocess in the "in row" insertion mode
4406                  }                  }
4407                  } else {
4408                    !!!cp ('t206');
4409                }                }
4410    
4411                ## Clear back to table row context                ## Clear back to table row context
4412                while (not {                while (not {
4413                  tr => 1, html => 1,                  tr => 1, html => 1,
4414                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4415                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t207');
4416                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4417                }                }
4418                                
4419                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4420                $self->{insertion_mode} = IN_CELL_IM;                $self->{insertion_mode} = IN_CELL_IM;
4421    
4422                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
# Line 3734  sub _tree_construction_main ($) { Line 4435  sub _tree_construction_main ($) {
4435                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4436                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4437                    if ($node->[1] eq 'tr') {                    if ($node->[1] eq 'tr') {
4438                        !!!cp ('t208');
4439                      $i = $_;                      $i = $_;
4440                      last INSCOPE;                      last INSCOPE;
4441                    } elsif ({                    } elsif ({
4442                              table => 1, html => 1,                              html => 1,
4443    
4444                                ## NOTE: This element does not appear here, maybe.
4445                                table => 1,
4446                             }->{$node->[1]}) {                             }->{$node->[1]}) {
4447                        !!!cp ('t209');
4448                      last INSCOPE;                      last INSCOPE;
4449                    }                    }
4450                  } # INSCOPE                  } # INSCOPE
4451                  unless (defined $i) {                  unless (defined $i) {
4452                    !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                   !!!cp ('t210');
4453    ## TODO: This type is wrong.
4454                     !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);
4455                    ## Ignore the token                    ## Ignore the token
4456                    !!!next-token;                    !!!next-token;
4457                    redo B;                    redo B;
# Line 3753  sub _tree_construction_main ($) { Line 4461  sub _tree_construction_main ($) {
4461                  while (not {                  while (not {
4462                    tr => 1, html => 1,                    tr => 1, html => 1,
4463                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4464                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t211');
4465                      ## ISSUE: Can this case be reached?
4466                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4467                  }                  }
4468                                    
4469                  pop @{$self->{open_elements}}; # tr                  pop @{$self->{open_elements}}; # tr
4470                  $self->{insertion_mode} = IN_TABLE_BODY_IM;                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
4471                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
4472                      !!!cp ('t212');
4473                    ## reprocess                    ## reprocess
4474                    redo B;                    redo B;
4475                  } else {                  } else {
4476                      !!!cp ('t213');
4477                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
4478                  }                  }
4479                }                }
# Line 3775  sub _tree_construction_main ($) { Line 4486  sub _tree_construction_main ($) {
4486                    if ({                    if ({
4487                         tbody => 1, thead => 1, tfoot => 1,                         tbody => 1, thead => 1, tfoot => 1,
4488                        }->{$node->[1]}) {                        }->{$node->[1]}) {
4489                        !!!cp ('t214');
4490                      $i = $_;                      $i = $_;
4491                      last INSCOPE;                      last INSCOPE;
4492                    } elsif ({                    } elsif ({
4493                              table => 1, html => 1,                              table => 1, html => 1,
4494                             }->{$node->[1]}) {                             }->{$node->[1]}) {
4495                        !!!cp ('t215');
4496                      last INSCOPE;                      last INSCOPE;
4497                    }                    }
4498                  } # INSCOPE                  } # INSCOPE
4499                  unless (defined $i) {                  unless (defined $i) {
4500                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!cp ('t216');
4501    ## TODO: This erorr type ios wrong.
4502                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4503                    ## Ignore the token                    ## Ignore the token
4504                    !!!next-token;                    !!!next-token;
4505                    redo B;                    redo B;
# Line 3794  sub _tree_construction_main ($) { Line 4509  sub _tree_construction_main ($) {
4509                  while (not {                  while (not {
4510                    tbody => 1, tfoot => 1, thead => 1, html => 1,                    tbody => 1, tfoot => 1, thead => 1, html => 1,
4511                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4512                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t217');
4513                      ## ISSUE: Can this state be reached?
4514                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4515                  }                  }
4516                                    
# Line 3808  sub _tree_construction_main ($) { Line 4524  sub _tree_construction_main ($) {
4524                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4525                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4526                  ## reprocess in "in table" insertion mode...                  ## reprocess in "in table" insertion mode...
4527                  } else {
4528                    !!!cp ('t218');
4529                }                }
4530    
4531                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
4532                  ## Clear back to table context                  ## Clear back to table context
4533                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while ($self->{open_elements}->[-1]->[1] ne 'table' and
4534                         $self->{open_elements}->[-1]->[1] ne 'html') {                         $self->{open_elements}->[-1]->[1] ne 'html') {
4535                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t219');
4536                      ## ISSUE: Can this state be reached?
4537                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4538                  }                  }
4539                                    
4540                  !!!insert-element ('colgroup');                  !!!insert-element ('colgroup',, $token);
4541                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
4542                  ## reprocess                  ## reprocess
4543                  redo B;                  redo B;
# Line 3830  sub _tree_construction_main ($) { Line 4549  sub _tree_construction_main ($) {
4549                  ## Clear back to table context                  ## Clear back to table context
4550                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while ($self->{open_elements}->[-1]->[1] ne 'table' and
4551                         $self->{open_elements}->[-1]->[1] ne 'html') {                         $self->{open_elements}->[-1]->[1] ne 'html') {
4552                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t220');
4553                      ## ISSUE: Can this state be reached?
4554                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4555                  }                  }
4556                                    
4557                  push @$active_formatting_elements, ['#marker', '']                  push @$active_formatting_elements, ['#marker', '']
4558                      if $token->{tag_name} eq 'caption';                      if $token->{tag_name} eq 'caption';
4559                                    
4560                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4561                  $self->{insertion_mode} = {                  $self->{insertion_mode} = {
4562                                             caption => IN_CAPTION_IM,                                             caption => IN_CAPTION_IM,
4563                                             colgroup => IN_COLUMN_GROUP_IM,                                             colgroup => IN_COLUMN_GROUP_IM,
# Line 3851  sub _tree_construction_main ($) { Line 4571  sub _tree_construction_main ($) {
4571                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
4572                }                }
4573              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
4574                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);
4575    
4576                ## As if </table>                ## As if </table>
4577                ## have a table element in table scope                ## have a table element in table scope
# Line 3859  sub _tree_construction_main ($) { Line 4579  sub _tree_construction_main ($) {
4579                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4580                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4581                  if ($node->[1] eq 'table') {                  if ($node->[1] eq 'table') {
4582                      !!!cp ('t221');
4583                    $i = $_;                    $i = $_;
4584                    last INSCOPE;                    last INSCOPE;
4585                  } elsif ({                  } elsif ({
4586                            table => 1, html => 1,                            #table => 1,
4587                              html => 1,
4588                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4589                      !!!cp ('t222');
4590                    last INSCOPE;                    last INSCOPE;
4591                  }                  }
4592                } # INSCOPE                } # INSCOPE
4593                unless (defined $i) {                unless (defined $i) {
4594                  !!!parse-error (type => 'unmatched end tag:table');                  !!!cp ('t223');
4595    ## TODO: The following is wrong, maybe.
4596                    !!!parse-error (type => 'unmatched end tag:table', token => $token);
4597                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
4598                  !!!next-token;                  !!!next-token;
4599                  redo B;                  redo B;
4600                }                }
4601                                
4602    ## TODO: Followings are removed from the latest spec.
4603                ## generate implied end tags                ## generate implied end tags
4604                if ({                while ({
4605                     dd => 1, dt => 1, li => 1, p => 1,                        dd => 1, dt => 1, li => 1, p => 1,
4606                     td => 1, th => 1, tr => 1,                       }->{$self->{open_elements}->[-1]->[1]}) {
4607                     tbody => 1, tfoot=> 1, thead => 1,                  !!!cp ('t224');
4608                    }->{$self->{open_elements}->[-1]->[1]}) {                  pop @{$self->{open_elements}};
                 !!!back-token; # <table>  
                 $token = {type => END_TAG_TOKEN, tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => END_TAG_TOKEN,  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
4609                }                }
4610    
4611                if ($self->{open_elements}->[-1]->[1] ne 'table') {                if ($self->{open_elements}->[-1]->[1] ne 'table') {
4612                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t225');
4613    ## ISSUE: Can this case be reached?
4614                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);
4615                  } else {
4616                    !!!cp ('t226');
4617                }                }
4618    
4619                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
4620                  pop @{$open_tables};
4621    
4622                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
4623    
4624                ## reprocess                ## reprocess
4625                redo B;                redo B;
4626          } else {          } elsif ($token->{tag_name} eq 'style') {
4627            !!!parse-error (type => 'in table:'.$token->{tag_name});            if (not $open_tables->[-1]->[1]) { # tainted
4628                !!!cp ('t227.8');
4629                ## NOTE: This is a "as if in head" code clone.
4630                $parse_rcdata->(CDATA_CONTENT_MODEL);
4631                redo B;
4632              } else {
4633                !!!cp ('t227.7');
4634                #
4635              }
4636            } elsif ($token->{tag_name} eq 'script') {
4637              if (not $open_tables->[-1]->[1]) { # tainted
4638                !!!cp ('t227.6');
4639                ## NOTE: This is a "as if in head" code clone.
4640                $script_start_tag->();
4641                redo B;
4642              } else {
4643                !!!cp ('t227.5');
4644                #
4645              }
4646            } elsif ($token->{tag_name} eq 'input') {
4647              if (not $open_tables->[-1]->[1]) { # tainted
4648                if ($token->{attributes}->{type}) { ## TODO: case
4649                  my $type = lc $token->{attributes}->{type}->{value};
4650                  if ($type eq 'hidden') {
4651                    !!!cp ('t227.3');
4652                    !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
4653    
4654            $insert = $insert_to_foster;                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4655    
4656                    ## TODO: form element pointer
4657    
4658                    pop @{$self->{open_elements}};
4659    
4660                    !!!next-token;
4661                    redo B;
4662                  } else {
4663                    !!!cp ('t227.2');
4664                    #
4665                  }
4666                } else {
4667                  !!!cp ('t227.1');
4668                  #
4669                }
4670              } else {
4671                !!!cp ('t227.4');
4672                #
4673              }
4674            } else {
4675              !!!cp ('t227');
4676            #            #
4677          }          }
4678    
4679            !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
4680    
4681            $insert = $insert_to_foster;
4682            #
4683        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
4684              if ($token->{tag_name} eq 'tr' and              if ($token->{tag_name} eq 'tr' and
4685                  $self->{insertion_mode} == IN_ROW_IM) {                  $self->{insertion_mode} == IN_ROW_IM) {
# Line 3912  sub _tree_construction_main ($) { Line 4688  sub _tree_construction_main ($) {
4688                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4689                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4690                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
4691                      !!!cp ('t228');
4692                    $i = $_;                    $i = $_;
4693                    last INSCOPE;                    last INSCOPE;
4694                  } elsif ({                  } elsif ({
4695                            table => 1, html => 1,                            table => 1, html => 1,
4696                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4697                      !!!cp ('t229');
4698                    last INSCOPE;                    last INSCOPE;
4699                  }                  }
4700                } # INSCOPE                } # INSCOPE
4701                unless (defined $i) {                unless (defined $i) {
4702                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t230');
4703                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4704                  ## Ignore the token                  ## Ignore the token
4705                  !!!next-token;                  !!!next-token;
4706                  redo B;                  redo B;
4707                  } else {
4708                    !!!cp ('t232');
4709                }                }
4710    
4711                ## Clear back to table row context                ## Clear back to table row context
4712                while (not {                while (not {
4713                  tr => 1, html => 1,                  tr => 1, html => 1,
4714                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4715                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t231');
4716    ## ISSUE: Can this state be reached?
4717                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4718                }                }
4719    
# Line 3947  sub _tree_construction_main ($) { Line 4729  sub _tree_construction_main ($) {
4729                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4730                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4731                    if ($node->[1] eq 'tr') {                    if ($node->[1] eq 'tr') {
4732                        !!!cp ('t233');
4733                      $i = $_;                      $i = $_;
4734                      last INSCOPE;                      last INSCOPE;
4735                    } elsif ({                    } elsif ({
4736                              table => 1, html => 1,                              table => 1, html => 1,
4737                             }->{$node->[1]}) {                             }->{$node->[1]}) {
4738                        !!!cp ('t234');
4739                      last INSCOPE;                      last INSCOPE;
4740                    }                    }
4741                  } # INSCOPE                  } # INSCOPE
4742                  unless (defined $i) {                  unless (defined $i) {
4743                    !!!parse-error (type => 'unmatched end tag:'.$token->{type});                    !!!cp ('t235');
4744    ## TODO: The following is wrong.
4745                      !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);
4746                    ## Ignore the token                    ## Ignore the token
4747                    !!!next-token;                    !!!next-token;
4748                    redo B;                    redo B;
# Line 3966  sub _tree_construction_main ($) { Line 4752  sub _tree_construction_main ($) {
4752                  while (not {                  while (not {
4753                    tr => 1, html => 1,                    tr => 1, html => 1,
4754                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4755                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t236');
4756    ## ISSUE: Can this state be reached?
4757                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4758                  }                  }
4759                                    
# Line 3983  sub _tree_construction_main ($) { Line 4770  sub _tree_construction_main ($) {
4770                    if ({                    if ({
4771                         tbody => 1, thead => 1, tfoot => 1,                         tbody => 1, thead => 1, tfoot => 1,
4772                        }->{$node->[1]}) {                        }->{$node->[1]}) {
4773                        !!!cp ('t237');
4774                      $i = $_;                      $i = $_;
4775                      last INSCOPE;                      last INSCOPE;
4776                    } elsif ({                    } elsif ({
4777                              table => 1, html => 1,                              table => 1, html => 1,
4778                             }->{$node->[1]}) {                             }->{$node->[1]}) {
4779                        !!!cp ('t238');
4780                      last INSCOPE;                      last INSCOPE;
4781                    }                    }
4782                  } # INSCOPE                  } # INSCOPE
4783                  unless (defined $i) {                  unless (defined $i) {
4784                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!cp ('t239');
4785                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4786                    ## Ignore the token                    ## Ignore the token
4787                    !!!next-token;                    !!!next-token;
4788                    redo B;                    redo B;
# Line 4002  sub _tree_construction_main ($) { Line 4792  sub _tree_construction_main ($) {
4792                  while (not {                  while (not {
4793                    tbody => 1, tfoot => 1, thead => 1, html => 1,                    tbody => 1, tfoot => 1, thead => 1, html => 1,
4794                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4795                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t240');
4796                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4797                  }                  }
4798                                    
# Line 4018  sub _tree_construction_main ($) { Line 4808  sub _tree_construction_main ($) {
4808                  ## reprocess in the "in table" insertion mode...                  ## reprocess in the "in table" insertion mode...
4809                }                }
4810    
4811                  ## NOTE: </table> in the "in table" insertion mode.
4812                  ## When you edit the code fragment below, please ensure that
4813                  ## the code for <table> in the "in table" insertion mode
4814                  ## is synced with it.
4815    
4816                ## have a table element in table scope                ## have a table element in table scope
4817                my $i;                my $i;
4818                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4819                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4820                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
4821                      !!!cp ('t241');
4822                    $i = $_;                    $i = $_;
4823                    last INSCOPE;                    last INSCOPE;
4824                  } elsif ({                  } elsif ({
4825                            table => 1, html => 1,                            table => 1, html => 1,
4826                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4827                      !!!cp ('t242');
4828                    last INSCOPE;                    last INSCOPE;
4829                  }                  }
4830                } # INSCOPE                } # INSCOPE
4831                unless (defined $i) {                unless (defined $i) {
4832                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t243');
4833                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4834                  ## Ignore the token                  ## Ignore the token
4835                  !!!next-token;                  !!!next-token;
4836                  redo B;                  redo B;
4837                }                }
   
               ## 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_TOKEN,  
                           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]);  
               }  
4838                                    
4839                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
4840                  pop @{$open_tables};
4841                                
4842                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
4843                                
# Line 4070  sub _tree_construction_main ($) { Line 4853  sub _tree_construction_main ($) {
4853                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4854                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4855                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[1] eq $token->{tag_name}) {
4856                        !!!cp ('t247');
4857                      $i = $_;                      $i = $_;
4858                      last INSCOPE;                      last INSCOPE;
4859                    } elsif ({                    } elsif ({
4860                              table => 1, html => 1,                              table => 1, html => 1,
4861                             }->{$node->[1]}) {                             }->{$node->[1]}) {
4862                        !!!cp ('t248');
4863                      last INSCOPE;                      last INSCOPE;
4864                    }                    }
4865                  } # INSCOPE                  } # INSCOPE
4866                    unless (defined $i) {                    unless (defined $i) {
4867                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!cp ('t249');
4868                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4869                      ## Ignore the token                      ## Ignore the token
4870                      !!!next-token;                      !!!next-token;
4871                      redo B;                      redo B;
# Line 4091  sub _tree_construction_main ($) { Line 4877  sub _tree_construction_main ($) {
4877                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4878                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4879                    if ($node->[1] eq 'tr') {                    if ($node->[1] eq 'tr') {
4880                        !!!cp ('t250');
4881                      $i = $_;                      $i = $_;
4882                      last INSCOPE;                      last INSCOPE;
4883                    } elsif ({                    } elsif ({
4884                              table => 1, html => 1,                              table => 1, html => 1,
4885                             }->{$node->[1]}) {                             }->{$node->[1]}) {
4886                        !!!cp ('t251');
4887                      last INSCOPE;                      last INSCOPE;
4888                    }                    }
4889                  } # INSCOPE                  } # INSCOPE
4890                    unless (defined $i) {                    unless (defined $i) {
4891                      !!!parse-error (type => 'unmatched end tag:tr');                      !!!cp ('t252');
4892                        !!!parse-error (type => 'unmatched end tag:tr', token => $token);
4893                      ## Ignore the token                      ## Ignore the token
4894                      !!!next-token;                      !!!next-token;
4895                      redo B;                      redo B;
# Line 4110  sub _tree_construction_main ($) { Line 4899  sub _tree_construction_main ($) {
4899                  while (not {                  while (not {
4900                    tr => 1, html => 1,                    tr => 1, html => 1,
4901                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4902                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t253');
4903    ## ISSUE: Can this case be reached?
4904                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4905                  }                  }
4906                                    
# Line 4124  sub _tree_construction_main ($) { Line 4914  sub _tree_construction_main ($) {
4914                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4915                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4916                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
4917                      !!!cp ('t254');
4918                    $i = $_;                    $i = $_;
4919                    last INSCOPE;                    last INSCOPE;
4920                  } elsif ({                  } elsif ({
4921                            table => 1, html => 1,                            table => 1, html => 1,
4922                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4923                      !!!cp ('t255');
4924                    last INSCOPE;                    last INSCOPE;
4925                  }                  }
4926                } # INSCOPE                } # INSCOPE
4927                unless (defined $i) {                unless (defined $i) {
4928                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t256');
4929                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4930                  ## Ignore the token                  ## Ignore the token
4931                  !!!next-token;                  !!!next-token;
4932                  redo B;                  redo B;
# Line 4143  sub _tree_construction_main ($) { Line 4936  sub _tree_construction_main ($) {
4936                while (not {                while (not {
4937                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  tbody => 1, tfoot => 1, thead => 1, html => 1,
4938                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4939                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t257');
4940    ## ISSUE: Can this case be reached?
4941                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4942                }                }
4943    
# Line 4157  sub _tree_construction_main ($) { Line 4951  sub _tree_construction_main ($) {
4951                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
4952                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
4953                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4954                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t258');
4955                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4956                ## Ignore the token                ## Ignore the token
4957                !!!next-token;                !!!next-token;
4958                redo B;                redo B;
4959          } else {          } else {
4960            !!!parse-error (type => 'in table:/'.$token->{tag_name});            !!!cp ('t259');
4961              !!!parse-error (type => 'in table:/'.$token->{tag_name}, token => $token);
4962    
4963            $insert = $insert_to_foster;            $insert = $insert_to_foster;
4964            #            #
4965          }          }
4966          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4967            unless ($self->{open_elements}->[-1]->[1] eq 'html' and
4968                    @{$self->{open_elements}} == 1) { # redundant, maybe
4969              !!!parse-error (type => 'in body:#eof', token => $token);
4970              !!!cp ('t259.1');
4971              #
4972            } else {
4973              !!!cp ('t259.2');
4974              #
4975            }
4976    
4977            ## Stop parsing
4978            last B;
4979        } else {        } else {
4980          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
4981        }        }
# Line 4175  sub _tree_construction_main ($) { Line 4984  sub _tree_construction_main ($) {
4984              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4985                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4986                unless (length $token->{data}) {                unless (length $token->{data}) {
4987                    !!!cp ('t260');
4988                  !!!next-token;                  !!!next-token;
4989                  redo B;                  redo B;
4990                }                }
4991              }              }
4992                            
4993                !!!cp ('t261');
4994              #              #
4995            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
4996              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
4997                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!cp ('t262');
4998                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4999                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5000                !!!next-token;                !!!next-token;
5001                redo B;                redo B;
5002              } else {              } else {
5003                  !!!cp ('t263');
5004                #                #
5005              }              }
5006            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
5007              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
5008                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] eq 'html') {
5009                  !!!parse-error (type => 'unmatched end tag:colgroup');                  !!!cp ('t264');
5010                    !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5011                  ## Ignore the token                  ## Ignore the token
5012                  !!!next-token;                  !!!next-token;
5013                  redo B;                  redo B;
5014                } else {                } else {
5015                    !!!cp ('t265');
5016                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
5017                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5018                  !!!next-token;                  !!!next-token;
5019                  redo B;                              redo B;            
5020                }                }
5021              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
5022                !!!parse-error (type => 'unmatched end tag:col');                !!!cp ('t266');
5023                  !!!parse-error (type => 'unmatched end tag:col', token => $token);
5024                ## Ignore the token                ## Ignore the token
5025                !!!next-token;                !!!next-token;
5026                redo B;                redo B;
5027              } else {              } else {
5028                  !!!cp ('t267');
5029                #                #
5030              }              }
5031            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5032              #          if ($self->{open_elements}->[-1]->[1] eq 'html' or
5033            }              @{$self->{open_elements}} == 1) { # redundant, maybe
5034              !!!cp ('t270.2');
5035              ## Stop parsing.
5036              last B;
5037            } else {
5038              ## NOTE: As if </colgroup>.
5039              !!!cp ('t270.1');
5040              pop @{$self->{open_elements}}; # colgroup
5041              $self->{insertion_mode} = IN_TABLE_IM;
5042              ## Reprocess.
5043              redo B;
5044            }
5045          } else {
5046            die "$0: $token->{type}: Unknown token type";
5047          }
5048    
5049            ## As if </colgroup>            ## As if </colgroup>
5050            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] eq 'html') {
5051              !!!parse-error (type => 'unmatched end tag:colgroup');              !!!cp ('t269');
5052    ## TODO: Wrong error type?
5053                !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5054              ## Ignore the token              ## Ignore the token
5055              !!!next-token;              !!!next-token;
5056              redo B;              redo B;
5057            } else {            } else {
5058                !!!cp ('t270');
5059              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
5060              $self->{insertion_mode} = IN_TABLE_IM;              $self->{insertion_mode} = IN_TABLE_IM;
5061              ## reprocess              ## reprocess
5062              redo B;              redo B;
5063            }            }
5064      } elsif ($self->{insertion_mode} == IN_SELECT_IM) {      } elsif ($self->{insertion_mode} & SELECT_IMS) {
5065        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5066            !!!cp ('t271');
5067          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5068          !!!next-token;          !!!next-token;
5069          redo B;          redo B;
5070        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5071              if ($token->{tag_name} eq 'option') {              if ($token->{tag_name} eq 'option') {
5072                if ($self->{open_elements}->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
5073                    !!!cp ('t272');
5074                  ## As if </option>                  ## As if </option>
5075                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5076                  } else {
5077                    !!!cp ('t273');
5078                }                }
5079    
5080                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5081                !!!next-token;                !!!next-token;
5082                redo B;                redo B;
5083              } elsif ($token->{tag_name} eq 'optgroup') {              } elsif ($token->{tag_name} eq 'optgroup') {
5084                if ($self->{open_elements}->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
5085                    !!!cp ('t274');
5086                  ## As if </option>                  ## As if </option>
5087                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5088                  } else {
5089                    !!!cp ('t275');
5090                }                }
5091    
5092                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {
5093                    !!!cp ('t276');
5094                  ## As if </optgroup>                  ## As if </optgroup>
5095                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5096                  } else {
5097                    !!!cp ('t277');
5098                }                }
5099    
5100                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5101                !!!next-token;                !!!next-token;
5102                redo B;                redo B;
5103              } elsif ($token->{tag_name} eq 'select') {          } elsif ($token->{tag_name} eq 'select' or
5104                !!!parse-error (type => 'not closed:select');                   $token->{tag_name} eq 'input' or
5105                ## As if </select> instead                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5106                      {
5107                       caption => 1, table => 1,
5108                       tbody => 1, tfoot => 1, thead => 1,
5109                       tr => 1, td => 1, th => 1,
5110                      }->{$token->{tag_name}})) {
5111              ## TODO: The type below is not good - <select> is replaced by </select>
5112              !!!parse-error (type => 'not closed:select', token => $token);
5113              ## NOTE: As if the token were </select> (<select> case) or
5114              ## as if there were </select> (otherwise).
5115                ## have an element in table scope                ## have an element in table scope
5116                my $i;                my $i;
5117                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5118                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5119                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq 'select') {
5120                      !!!cp ('t278');
5121                    $i = $_;                    $i = $_;
5122                    last INSCOPE;                    last INSCOPE;
5123                  } elsif ({                  } elsif ({
5124                            table => 1, html => 1,                            table => 1, html => 1,
5125                           }->{$node->[1]}) {                           }->{$node->[1]}) {
5126                      !!!cp ('t279');
5127                    last INSCOPE;                    last INSCOPE;
5128                  }                  }
5129                } # INSCOPE                } # INSCOPE
5130                unless (defined $i) {                unless (defined $i) {
5131                  !!!parse-error (type => 'unmatched end tag:select');                  !!!cp ('t280');
5132                    !!!parse-error (type => 'unmatched end tag:select', token => $token);
5133                  ## Ignore the token                  ## Ignore the token
5134                  !!!next-token;                  !!!next-token;
5135                  redo B;                  redo B;
5136                }                }
5137                                
5138                  !!!cp ('t281');
5139                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5140    
5141                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5142    
5143                !!!next-token;            if ($token->{tag_name} eq 'select') {
5144                redo B;              !!!cp ('t281.2');
5145                !!!next-token;
5146                redo B;
5147              } else {
5148                !!!cp ('t281.1');
5149                ## Reprocess the token.
5150                redo B;
5151              }
5152          } else {          } else {
5153            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!cp ('t282');
5154              !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);
5155            ## Ignore the token            ## Ignore the token
5156            !!!next-token;            !!!next-token;
5157            redo B;            redo B;
# Line 4295  sub _tree_construction_main ($) { Line 5160  sub _tree_construction_main ($) {
5160              if ($token->{tag_name} eq 'optgroup') {              if ($token->{tag_name} eq 'optgroup') {
5161                if ($self->{open_elements}->[-1]->[1] eq 'option' and                if ($self->{open_elements}->[-1]->[1] eq 'option' and
5162                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {
5163                    !!!cp ('t283');
5164                  ## As if </option>                  ## As if </option>
5165                  splice @{$self->{open_elements}}, -2;                  splice @{$self->{open_elements}}, -2;
5166                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {
5167                    !!!cp ('t284');
5168                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5169                } else {                } else {
5170                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t285');
5171                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5172                  ## Ignore the token                  ## Ignore the token
5173                }                }
5174                !!!next-token;                !!!next-token;
5175                redo B;                redo B;
5176              } elsif ($token->{tag_name} eq 'option') {              } elsif ($token->{tag_name} eq 'option') {
5177                if ($self->{open_elements}->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
5178                    !!!cp ('t286');
5179                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5180                } else {                } else {
5181                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t287');
5182                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5183                  ## Ignore the token                  ## Ignore the token
5184                }                }
5185                !!!next-token;                !!!next-token;
# Line 4320  sub _tree_construction_main ($) { Line 5190  sub _tree_construction_main ($) {
5190                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5191                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5192                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
5193                      !!!cp ('t288');
5194                    $i = $_;                    $i = $_;
5195                    last INSCOPE;                    last INSCOPE;
5196                  } elsif ({                  } elsif ({
5197                            table => 1, html => 1,                            table => 1, html => 1,
5198                           }->{$node->[1]}) {                           }->{$node->[1]}) {
5199                      !!!cp ('t289');
5200                    last INSCOPE;                    last INSCOPE;
5201                  }                  }
5202                } # INSCOPE                } # INSCOPE
5203                unless (defined $i) {                unless (defined $i) {
5204                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t290');
5205                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5206                  ## Ignore the token                  ## Ignore the token
5207                  !!!next-token;                  !!!next-token;
5208                  redo B;                  redo B;
5209                }                }
5210                                
5211                  !!!cp ('t291');
5212                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5213    
5214                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5215    
5216                !!!next-token;                !!!next-token;
5217                redo B;                redo B;
5218              } elsif ({          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5219                        caption => 1, table => 1, tbody => 1,                   {
5220                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                    caption => 1, table => 1, tbody => 1,
5221                       }->{$token->{tag_name}}) {                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
5222                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                   }->{$token->{tag_name}}) {
5223    ## TODO: The following is wrong?
5224                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5225                                
5226                ## have an element in table scope                ## have an element in table scope
5227                my $i;                my $i;
5228                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5229                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5230                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
5231                      !!!cp ('t292');
5232                    $i = $_;                    $i = $_;
5233                    last INSCOPE;                    last INSCOPE;
5234                  } elsif ({                  } elsif ({
5235                            table => 1, html => 1,                            table => 1, html => 1,
5236                           }->{$node->[1]}) {                           }->{$node->[1]}) {
5237                      !!!cp ('t293');
5238                    last INSCOPE;                    last INSCOPE;
5239                  }                  }
5240                } # INSCOPE                } # INSCOPE
5241                unless (defined $i) {                unless (defined $i) {
5242                    !!!cp ('t294');
5243                  ## Ignore the token                  ## Ignore the token
5244                  !!!next-token;                  !!!next-token;
5245                  redo B;                  redo B;
# Line 4372  sub _tree_construction_main ($) { Line 5251  sub _tree_construction_main ($) {
5251                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5252                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5253                  if ($node->[1] eq 'select') {                  if ($node->[1] eq 'select') {
5254                      !!!cp ('t295');
5255                    $i = $_;                    $i = $_;
5256                    last INSCOPE;                    last INSCOPE;
5257                  } elsif ({                  } elsif ({
5258                            table => 1, html => 1,                            table => 1, html => 1,
5259                           }->{$node->[1]}) {                           }->{$node->[1]}) {
5260    ## ISSUE: Can this state be reached?
5261                      !!!cp ('t296');
5262                    last INSCOPE;                    last INSCOPE;
5263                  }                  }
5264                } # INSCOPE                } # INSCOPE
5265                unless (defined $i) {                unless (defined $i) {
5266                  !!!parse-error (type => 'unmatched end tag:select');                  !!!cp ('t297');
5267    ## TODO: The following error type is correct?
5268                    !!!parse-error (type => 'unmatched end tag:select', token => $token);
5269                  ## Ignore the </select> token                  ## Ignore the </select> token
5270                  !!!next-token; ## TODO: ok?                  !!!next-token; ## TODO: ok?
5271                  redo B;                  redo B;
5272                }                }
5273                                
5274                  !!!cp ('t298');
5275                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5276    
5277                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
# Line 4394  sub _tree_construction_main ($) { Line 5279  sub _tree_construction_main ($) {
5279                ## reprocess                ## reprocess
5280                redo B;                redo B;
5281          } else {          } else {
5282            !!!parse-error (type => 'in select:/'.$token->{tag_name});            !!!cp ('t299');
5283              !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);
5284            ## Ignore the token            ## Ignore the token
5285            !!!next-token;            !!!next-token;
5286            redo B;            redo B;
5287          }          }
5288          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5289            unless ($self->{open_elements}->[-1]->[1] eq 'html' and
5290                    @{$self->{open_elements}} == 1) { # redundant, maybe
5291              !!!cp ('t299.1');
5292              !!!parse-error (type => 'in body:#eof', token => $token);
5293            } else {
5294              !!!cp ('t299.2');
5295            }
5296    
5297            ## Stop parsing.
5298            last B;
5299        } else {        } else {
5300          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5301        }        }
# Line 4412  sub _tree_construction_main ($) { Line 5309  sub _tree_construction_main ($) {
5309            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5310                        
5311            unless (length $token->{data}) {            unless (length $token->{data}) {
5312                !!!cp ('t300');
5313              !!!next-token;              !!!next-token;
5314              redo B;              redo B;
5315            }            }
5316          }          }
5317                    
5318          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5319            !!!parse-error (type => 'after html:#character');            !!!cp ('t301');
5320              !!!parse-error (type => 'after html:#character', token => $token);
5321    
5322            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
5323            } else {
5324              !!!cp ('t302');
5325          }          }
5326                    
5327          ## "after body" insertion mode          ## "after body" insertion mode
5328          !!!parse-error (type => 'after body:#character');          !!!parse-error (type => 'after body:#character', token => $token);
5329    
5330          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
5331          ## reprocess          ## reprocess
5332          redo B;          redo B;
5333        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5334          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5335            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!cp ('t303');
5336              !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
5337                        
5338            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
5339            } else {
5340              !!!cp ('t304');
5341          }          }
5342    
5343          ## "after body" insertion mode          ## "after body" insertion mode
5344          !!!parse-error (type => 'after body:'.$token->{tag_name});          !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);
5345    
5346          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
5347          ## reprocess          ## reprocess
5348          redo B;          redo B;
5349        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5350          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5351            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!cp ('t305');
5352              !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
5353                        
5354            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
5355            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
5356            } else {
5357              !!!cp ('t306');
5358          }          }
5359    
5360          ## "after body" insertion mode          ## "after body" insertion mode
5361          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
5362            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
5363              !!!parse-error (type => 'unmatched end tag:html');              !!!cp ('t307');
5364                !!!parse-error (type => 'unmatched end tag:html', token => $token);
5365              ## Ignore the token              ## Ignore the token
5366              !!!next-token;              !!!next-token;
5367              redo B;              redo B;
5368            } else {            } else {
5369                !!!cp ('t308');
5370              $self->{insertion_mode} = AFTER_HTML_BODY_IM;              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
5371              !!!next-token;              !!!next-token;
5372              redo B;              redo B;
5373            }            }
5374          } else {          } else {
5375            !!!parse-error (type => 'after body:/'.$token->{tag_name});            !!!cp ('t309');
5376              !!!parse-error (type => 'after body:/'.$token->{tag_name}, token => $token);
5377    
5378            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
5379            ## reprocess            ## reprocess
5380            redo B;            redo B;
5381          }          }
5382          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5383            !!!cp ('t309.2');
5384            ## Stop parsing
5385            last B;
5386        } else {        } else {
5387          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5388        }        }
# Line 4478  sub _tree_construction_main ($) { Line 5392  sub _tree_construction_main ($) {
5392            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5393                        
5394            unless (length $token->{data}) {            unless (length $token->{data}) {
5395                !!!cp ('t310');
5396              !!!next-token;              !!!next-token;
5397              redo B;              redo B;
5398            }            }
# Line 4485  sub _tree_construction_main ($) { Line 5400  sub _tree_construction_main ($) {
5400                    
5401          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
5402            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5403              !!!parse-error (type => 'in frameset:#character');              !!!cp ('t311');
5404                !!!parse-error (type => 'in frameset:#character', token => $token);
5405            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
5406              !!!parse-error (type => 'after frameset:#character');              !!!cp ('t312');
5407                !!!parse-error (type => 'after frameset:#character', token => $token);
5408            } else { # "after html frameset"            } else { # "after html frameset"
5409              !!!parse-error (type => 'after html:#character');              !!!cp ('t313');
5410                !!!parse-error (type => 'after html:#character', token => $token);
5411    
5412              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
5413              ## Reprocess in the "main" phase, "after frameset"...              ## Reprocess in the "after frameset" insertion mode.
5414              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#character', token => $token);
5415            }            }
5416                        
5417            ## Ignore the token.            ## Ignore the token.
5418            if (length $token->{data}) {            if (length $token->{data}) {
5419                !!!cp ('t314');
5420              ## reprocess the rest of characters              ## reprocess the rest of characters
5421            } else {            } else {
5422                !!!cp ('t315');
5423              !!!next-token;              !!!next-token;
5424            }            }
5425            redo B;            redo B;
# Line 4508  sub _tree_construction_main ($) { Line 5428  sub _tree_construction_main ($) {
5428          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
5429        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5430          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
5431            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!cp ('t316');
5432              !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
5433    
5434            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
5435            ## Process in the "main" phase, "after frameset" insertion mode...            ## Process in the "after frameset" insertion mode.
5436          }          } else {
5437              !!!cp ('t317');
5438            }
5439    
5440          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
5441              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
5442            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!cp ('t318');
5443              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5444            !!!next-token;            !!!next-token;
5445            redo B;            redo B;
5446          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
5447                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
5448            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!cp ('t319');
5449              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5450            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
5451            !!!next-token;            !!!next-token;
5452            redo B;            redo B;
5453          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
5454              !!!cp ('t320');
5455            ## NOTE: As if in body.            ## NOTE: As if in body.
5456            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);            $parse_rcdata->(CDATA_CONTENT_MODEL);
5457            redo B;            redo B;
5458          } else {          } else {
5459            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5460              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!cp ('t321');
5461                !!!parse-error (type => 'in frameset:'.$token->{tag_name}, token => $token);
5462            } else {            } else {
5463              !!!parse-error (type => 'after frameset:'.$token->{tag_name});              !!!cp ('t322');
5464                !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);
5465            }            }
5466            ## Ignore the token            ## Ignore the token
5467            !!!next-token;            !!!next-token;
# Line 4541  sub _tree_construction_main ($) { Line 5469  sub _tree_construction_main ($) {
5469          }          }
5470        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5471          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
5472            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!cp ('t323');
5473              !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
5474    
5475            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
5476            ## Process in the "main" phase, "after frameset" insertion mode...            ## Process in the "after frameset" insertion mode.
5477            } else {
5478              !!!cp ('t324');
5479          }          }
5480    
5481          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
5482              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
5483            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] eq 'html' and
5484                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
5485              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t325');
5486                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5487              ## Ignore the token              ## Ignore the token
5488              !!!next-token;              !!!next-token;
5489            } else {            } else {
5490                !!!cp ('t326');
5491              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5492              !!!next-token;              !!!next-token;
5493            }            }
5494    
5495            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
5496                $self->{open_elements}->[-1]->[1] ne 'frameset') {                $self->{open_elements}->[-1]->[1] ne 'frameset') {
5497                !!!cp ('t327');
5498              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
5499              } else {
5500                !!!cp ('t328');
5501            }            }
5502            redo B;            redo B;
5503          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
5504                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
5505              !!!cp ('t329');
5506            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
5507            !!!next-token;            !!!next-token;
5508            redo B;            redo B;
5509          } else {          } else {
5510            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5511              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});              !!!cp ('t330');
5512                !!!parse-error (type => 'in frameset:/'.$token->{tag_name}, token => $token);
5513            } else {            } else {
5514              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});              !!!cp ('t331');
5515                !!!parse-error (type => 'after frameset:/'.$token->{tag_name}, token => $token);
5516            }            }
5517            ## Ignore the token            ## Ignore the token
5518            !!!next-token;            !!!next-token;
5519            redo B;            redo B;
5520          }          }
5521          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5522            unless ($self->{open_elements}->[-1]->[1] eq 'html' and
5523                    @{$self->{open_elements}} == 1) { # redundant, maybe
5524              !!!cp ('t331.1');
5525              !!!parse-error (type => 'in body:#eof', token => $token);
5526            } else {
5527              !!!cp ('t331.2');
5528            }
5529            
5530            ## Stop parsing
5531            last B;
5532        } else {        } else {
5533          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5534        }        }
# Line 4591  sub _tree_construction_main ($) { Line 5541  sub _tree_construction_main ($) {
5541      ## "in body" insertion mode      ## "in body" insertion mode
5542      if ($token->{type} == START_TAG_TOKEN) {      if ($token->{type} == START_TAG_TOKEN) {
5543        if ($token->{tag_name} eq 'script') {        if ($token->{tag_name} eq 'script') {
5544            !!!cp ('t332');
5545          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
5546          $script_start_tag->($insert);          $script_start_tag->();
5547          redo B;          redo B;
5548        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
5549            !!!cp ('t333');
5550          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
5551          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL);
5552          redo B;          redo B;
5553        } elsif ({        } elsif ({
5554                  base => 1, link => 1,                  base => 1, link => 1,
5555                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5556            !!!cp ('t334');
5557          ## 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
5558          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5559          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
5560          !!!next-token;          !!!next-token;
5561          redo B;          redo B;
5562        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
5563          ## 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
5564          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5565          my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
5566    
5567          unless ($self->{confident}) {          unless ($self->{confident}) {
5568            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) { ## TODO: And if supported
5569                !!!cp ('t335');
5570              $self->{change_encoding}              $self->{change_encoding}
5571                  ->($self, $token->{attributes}->{charset}->{value});                  ->($self, $token->{attributes}->{charset}->{value}, $token);
5572                            
5573              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
5574                  ->set_user_data (manakai_has_reference =>                  ->set_user_data (manakai_has_reference =>
# Line 4627  sub _tree_construction_main ($) { Line 5581  sub _tree_construction_main ($) {
5581                      [\x09-\x0D\x20]*=                      [\x09-\x0D\x20]*=
5582                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
5583                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
5584                  !!!cp ('t336');
5585                $self->{change_encoding}                $self->{change_encoding}
5586                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
5587                $meta_el->[0]->get_attribute_node_ns (undef, 'content')                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
5588                    ->set_user_data (manakai_has_reference =>                    ->set_user_data (manakai_has_reference =>
5589                                         $token->{attributes}->{content}                                         $token->{attributes}->{content}
# Line 4637  sub _tree_construction_main ($) { Line 5592  sub _tree_construction_main ($) {
5592            }            }
5593          } else {          } else {
5594            if ($token->{attributes}->{charset}) {            if ($token->{attributes}->{charset}) {
5595                !!!cp ('t337');
5596              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
5597                  ->set_user_data (manakai_has_reference =>                  ->set_user_data (manakai_has_reference =>
5598                                       $token->{attributes}->{charset}                                       $token->{attributes}->{charset}
5599                                           ->{has_reference});                                           ->{has_reference});
5600            }            }
5601            if ($token->{attributes}->{content}) {            if ($token->{attributes}->{content}) {
5602                !!!cp ('t338');
5603              $meta_el->[0]->get_attribute_node_ns (undef, 'content')              $meta_el->[0]->get_attribute_node_ns (undef, 'content')
5604                  ->set_user_data (manakai_has_reference =>                  ->set_user_data (manakai_has_reference =>
5605                                       $token->{attributes}->{content}                                       $token->{attributes}->{content}
# Line 4653  sub _tree_construction_main ($) { Line 5610  sub _tree_construction_main ($) {
5610          !!!next-token;          !!!next-token;
5611          redo B;          redo B;
5612        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
5613          !!!parse-error (type => 'in body:title');          !!!cp ('t341');
5614          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
5615          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {          $parse_rcdata->(RCDATA_CONTENT_MODEL);
           if (defined $self->{head_element}) {  
             $self->{head_element}->append_child ($_[0]);  
           } else {  
             $insert->($_[0]);  
           }  
         });  
5616          redo B;          redo B;
5617        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
5618          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body:body', token => $token);
5619                                
5620          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
5621              $self->{open_elements}->[1]->[1] ne 'body') {              $self->{open_elements}->[1]->[1] ne 'body') {
5622              !!!cp ('t342');
5623            ## Ignore the token            ## Ignore the token
5624          } else {          } else {
5625            my $body_el = $self->{open_elements}->[1]->[0];            my $body_el = $self->{open_elements}->[1]->[0];
5626            for my $attr_name (keys %{$token->{attributes}}) {            for my $attr_name (keys %{$token->{attributes}}) {
5627              unless ($body_el->has_attribute_ns (undef, $attr_name)) {              unless ($body_el->has_attribute_ns (undef, $attr_name)) {
5628                  !!!cp ('t343');
5629                $body_el->set_attribute_ns                $body_el->set_attribute_ns
5630                  (undef, [undef, $attr_name],                  (undef, [undef, $attr_name],
5631                   $token->{attributes}->{$attr_name}->{value});                   $token->{attributes}->{$attr_name}->{value});
# Line 4683  sub _tree_construction_main ($) { Line 5636  sub _tree_construction_main ($) {
5636          redo B;          redo B;
5637        } elsif ({        } elsif ({
5638                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
5639                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1,
5640                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5641                  menu => 1, ol => 1, p => 1, ul => 1,                  menu => 1, ol => 1, p => 1, ul => 1,
5642                  pre => 1,                  pre => 1, listing => 1,
5643                    form => 1,
5644                    table => 1,
5645                    hr => 1,
5646                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5647            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
5648              !!!cp ('t350');
5649              !!!parse-error (type => 'in form:form', token => $token);
5650              ## Ignore the token
5651              !!!next-token;
5652              redo B;
5653            }
5654    
5655          ## has a p element in scope          ## has a p element in scope
5656          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5657            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
5658                !!!cp ('t344');
5659              !!!back-token;              !!!back-token;
5660              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
5661                          line => $token->{line}, column => $token->{column}};
5662              redo B;              redo B;
5663            } elsif ({            } elsif ({
5664                      table => 1, caption => 1, td => 1, th => 1,                      applet => 1, table => 1, caption => 1, td => 1, th => 1,
5665                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5666                     }->{$_->[1]}) {                     }->{$_->[1]}) {
5667                !!!cp ('t345');
5668              last INSCOPE;              last INSCOPE;
5669            }            }
5670          } # INSCOPE          } # INSCOPE
5671                        
5672          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5673          if ($token->{tag_name} eq 'pre') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
5674            !!!next-token;            !!!next-token;
5675            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
5676              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
5677              unless (length $token->{data}) {              unless (length $token->{data}) {
5678                  !!!cp ('t346');
5679                !!!next-token;                !!!next-token;
5680                } else {
5681                  !!!cp ('t349');
5682              }              }
5683              } else {
5684                !!!cp ('t348');
5685            }            }
5686          } else {          } elsif ($token->{tag_name} eq 'form') {
5687              !!!cp ('t347.1');
5688              $self->{form_element} = $self->{open_elements}->[-1]->[0];
5689    
5690            !!!next-token;            !!!next-token;
5691          }          } elsif ($token->{tag_name} eq 'table') {
5692          redo B;            !!!cp ('t382');
5693        } elsif ($token->{tag_name} eq 'form') {            push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
5694          if (defined $self->{form_element}) {            
5695            !!!parse-error (type => 'in form:form');            $self->{insertion_mode} = IN_TABLE_IM;
5696            ## Ignore the token  
5697              !!!next-token;
5698            } elsif ($token->{tag_name} eq 'hr') {
5699              !!!cp ('t386');
5700              pop @{$self->{open_elements}};
5701            
5702            !!!next-token;            !!!next-token;
           redo B;  
5703          } else {          } else {
5704            ## has a p element in scope            !!!cp ('t347');
           INSCOPE: for (reverse @{$self->{open_elements}}) {  
             if ($_->[1] eq 'p') {  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
               redo B;  
             } elsif ({  
                       table => 1, caption => 1, td => 1, th => 1,  
                       button => 1, marquee => 1, object => 1, html => 1,  
                      }->{$_->[1]}) {  
               last INSCOPE;  
             }  
           } # INSCOPE  
               
           !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           $self->{form_element} = $self->{open_elements}->[-1]->[0];  
5705            !!!next-token;            !!!next-token;
           redo B;  
5706          }          }
       } elsif ($token->{tag_name} eq 'li') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'li') {  
             if ($i != -1) {  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
             }  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             last LI;  
           }  
             
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
5707          redo B;          redo B;
5708        } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {        } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
5709          ## has a p element in scope          ## has a p element in scope
5710          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5711            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
5712                !!!cp ('t353');
5713              !!!back-token;              !!!back-token;
5714              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
5715                          line => $token->{line}, column => $token->{column}};
5716              redo B;              redo B;
5717            } elsif ({            } elsif ({
5718                      table => 1, caption => 1, td => 1, th => 1,                      applet => 1, table => 1, caption => 1, td => 1, th => 1,
5719                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5720                     }->{$_->[1]}) {                     }->{$_->[1]}) {
5721                !!!cp ('t354');
5722              last INSCOPE;              last INSCOPE;
5723            }            }
5724          } # INSCOPE          } # INSCOPE
# Line 4805  sub _tree_construction_main ($) { Line 5726  sub _tree_construction_main ($) {
5726          ## Step 1          ## Step 1
5727          my $i = -1;          my $i = -1;
5728          my $node = $self->{open_elements}->[$i];          my $node = $self->{open_elements}->[$i];
5729            my $li_or_dtdd = {li => {li => 1},
5730                              dt => {dt => 1, dd => 1},
5731                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
5732          LI: {          LI: {
5733            ## Step 2            ## Step 2
5734            if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {            if ($li_or_dtdd->{$node->[1]}) {
5735              if ($i != -1) {              if ($i != -1) {
5736                  !!!cp ('t355');
5737                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'end tag missing:'.
5738                                $self->{open_elements}->[-1]->[1]);                                $self->{open_elements}->[-1]->[1], token => $token);
5739                } else {
5740                  !!!cp ('t356');
5741              }              }
5742              splice @{$self->{open_elements}}, $i;              splice @{$self->{open_elements}}, $i;
5743              last LI;              last LI;
5744              } else {
5745                !!!cp ('t357');
5746            }            }
5747                        
5748            ## Step 3            ## Step 3
# Line 4822  sub _tree_construction_main ($) { Line 5751  sub _tree_construction_main ($) {
5751                ($special_category->{$node->[1]} or                ($special_category->{$node->[1]} or
5752                 $scoping_category->{$node->[1]}) and                 $scoping_category->{$node->[1]}) and
5753                $node->[1] ne 'address' and $node->[1] ne 'div') {                $node->[1] ne 'address' and $node->[1] ne 'div') {
5754                !!!cp ('t358');
5755              last LI;              last LI;
5756            }            }
5757                        
5758              !!!cp ('t359');
5759            ## Step 4            ## Step 4
5760            $i--;            $i--;
5761            $node = $self->{open_elements}->[$i];            $node = $self->{open_elements}->[$i];
5762            redo LI;            redo LI;
5763          } # LI          } # LI
5764                        
5765          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5766          !!!next-token;          !!!next-token;
5767          redo B;          redo B;
5768        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
5769          ## has a p element in scope          ## has a p element in scope
5770          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5771            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
5772                !!!cp ('t367');
5773              !!!back-token;              !!!back-token;
5774              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
5775                          line => $token->{line}, column => $token->{column}};
5776              redo B;              redo B;
5777            } elsif ({            } elsif ({
5778                      table => 1, caption => 1, td => 1, th => 1,                      applet => 1, table => 1, caption => 1, td => 1, th => 1,
5779                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5780                     }->{$_->[1]}) {                     }->{$_->[1]}) {
5781                !!!cp ('t368');
5782              last INSCOPE;              last INSCOPE;
5783            }            }
5784          } # INSCOPE          } # INSCOPE
5785                        
5786          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5787                        
5788          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
5789                        
5790          !!!next-token;          !!!next-token;
5791          redo B;          redo B;
       } elsif ({  
                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## NOTE: See <http://html5.org/tools/web-apps-tracker?from=925&to=926>  
         ## has an element in scope  
         #my $i;  
         #INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
         #  my $node = $self->{open_elements}->[$_];  
         #  if ({  
         #       h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
         #      }->{$node->[1]}) {  
         #    $i = $_;  
         #    last INSCOPE;  
         #  } elsif ({  
         #            table => 1, caption => 1, td => 1, th => 1,  
         #            button => 1, marquee => 1, object => 1, html => 1,  
         #           }->{$node->[1]}) {  
         #    last INSCOPE;  
         #  }  
         #} # INSCOPE  
         #    
         #if (defined $i) {  
         #  !!! parse-error (type => 'in hn:hn');  
         #  splice @{$self->{open_elements}}, $i;  
         #}  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         !!!next-token;  
         redo B;  
5792        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
5793          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
5794            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
5795            if ($node->[1] eq 'a') {            if ($node->[1] eq 'a') {
5796              !!!parse-error (type => 'in a:a');              !!!cp ('t371');
5797                !!!parse-error (type => 'in a:a', token => $token);
5798                            
5799              !!!back-token;              !!!back-token;
5800              $token = {type => END_TAG_TOKEN, tag_name => 'a'};              $token = {type => END_TAG_TOKEN, tag_name => 'a',
5801              $formatting_end_tag->($token->{tag_name});                        line => $token->{line}, column => $token->{column}};
5802                $formatting_end_tag->($token);
5803                            
5804              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
5805                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
5806                    !!!cp ('t372');
5807                  splice @$active_formatting_elements, $_, 1;                  splice @$active_formatting_elements, $_, 1;
5808                  last AFE2;                  last AFE2;
5809                }                }
5810              } # AFE2              } # AFE2
5811              OE: for (reverse 0..$#{$self->{open_elements}}) {              OE: for (reverse 0..$#{$self->{open_elements}}) {
5812                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
5813                    !!!cp ('t373');
5814                  splice @{$self->{open_elements}}, $_, 1;                  splice @{$self->{open_elements}}, $_, 1;
5815                  last OE;                  last OE;
5816                }                }
5817              } # OE              } # OE
5818              last AFE;              last AFE;
5819            } elsif ($node->[0] eq '#marker') {            } elsif ($node->[0] eq '#marker') {
5820                !!!cp ('t374');
5821              last AFE;              last AFE;
5822            }            }
5823          } # AFE          } # AFE
5824                        
5825          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5826    
5827          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5828          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
5829    
5830          !!!next-token;          !!!next-token;
5831          redo B;          redo B;
       } elsif ({  
                 b => 1, big => 1, em => 1, font => 1, i => 1,  
                 s => 1, small => 1, strile => 1,  
                 strong => 1, tt => 1, u => 1,  
                }->{$token->{tag_name}}) {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, $self->{open_elements}->[-1];  
           
         !!!next-token;  
         redo B;  
5832        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
5833          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5834    
# Line 4954  sub _tree_construction_main ($) { Line 5836  sub _tree_construction_main ($) {
5836          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5837            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
5838            if ($node->[1] eq 'nobr') {            if ($node->[1] eq 'nobr') {
5839              !!!parse-error (type => 'in nobr:nobr');              !!!cp ('t376');
5840                !!!parse-error (type => 'in nobr:nobr', token => $token);
5841              !!!back-token;              !!!back-token;
5842              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
5843                          line => $token->{line}, column => $token->{column}};
5844              redo B;              redo B;
5845            } elsif ({            } elsif ({
5846                      table => 1, caption => 1, td => 1, th => 1,                      applet => 1, table => 1, caption => 1, td => 1, th => 1,
5847                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5848                     }->{$node->[1]}) {                     }->{$node->[1]}) {
5849                !!!cp ('t377');
5850              last INSCOPE;              last INSCOPE;
5851            }            }
5852          } # INSCOPE          } # INSCOPE
5853                    
5854          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5855          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
5856                    
5857          !!!next-token;          !!!next-token;
# Line 4976  sub _tree_construction_main ($) { Line 5861  sub _tree_construction_main ($) {
5861          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5862            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
5863            if ($node->[1] eq 'button') {            if ($node->[1] eq 'button') {
5864              !!!parse-error (type => 'in button:button');              !!!cp ('t378');
5865                !!!parse-error (type => 'in button:button', token => $token);
5866              !!!back-token;              !!!back-token;
5867              $token = {type => END_TAG_TOKEN, tag_name => 'button'};              $token = {type => END_TAG_TOKEN, tag_name => 'button',
5868                          line => $token->{line}, column => $token->{column}};
5869              redo B;              redo B;
5870            } elsif ({            } elsif ({
5871                      table => 1, caption => 1, td => 1, th => 1,                      applet => 1, table => 1, caption => 1, td => 1, th => 1,
5872                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5873                     }->{$node->[1]}) {                     }->{$node->[1]}) {
5874                !!!cp ('t379');
5875              last INSCOPE;              last INSCOPE;
5876            }            }
5877          } # INSCOPE          } # INSCOPE
5878                        
5879          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5880                        
5881          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5882          push @$active_formatting_elements, ['#marker', ''];  
5883            ## TODO: associate with $self->{form_element} if defined
5884    
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'marquee' or  
                $token->{tag_name} eq 'object') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
5885          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
5886            
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'xmp') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         redo B;  
       } elsif ($token->{tag_name} eq 'table') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{insertion_mode} = IN_TABLE_IM;  
             
5887          !!!next-token;          !!!next-token;
5888          redo B;          redo B;
5889        } elsif ({        } elsif ({
5890                  area => 1, basefont => 1, bgsound => 1, br => 1,                  xmp => 1,
5891                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                  iframe => 1,
5892                  image => 1,                  noembed => 1,
5893                    noframes => 1,
5894                    noscript => 0, ## TODO: 1 if scripting is enabled
5895                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5896          if ($token->{tag_name} eq 'image') {          if ($token->{tag_name} eq 'xmp') {
5897            !!!parse-error (type => 'image');            !!!cp ('t381');
5898            $token->{tag_name} = 'img';            $reconstruct_active_formatting_elements->($insert_to_current);
5899            } else {
5900              !!!cp ('t399');
5901          }          }
5902            ## NOTE: There is an "as if in body" code clone.
5903          ## NOTE: There is an "as if <br>" code clone.          $parse_rcdata->(CDATA_CONTENT_MODEL);
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'hr') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
             
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'input') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         ## TODO: associate with $self->{form_element} if defined  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
5904          redo B;          redo B;
5905        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
5906          !!!parse-error (type => 'isindex');          !!!parse-error (type => 'isindex', token => $token);
5907                    
5908          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
5909              !!!cp ('t389');
5910            ## Ignore the token            ## Ignore the token
5911            !!!next-token;            !!!next-token;
5912            redo B;            redo B;
# Line 5093  sub _tree_construction_main ($) { Line 5920  sub _tree_construction_main ($) {
5920            delete $at->{prompt};            delete $at->{prompt};
5921            my @tokens = (            my @tokens = (
5922                          {type => START_TAG_TOKEN, tag_name => 'form',                          {type => START_TAG_TOKEN, tag_name => 'form',
5923                           attributes => $form_attrs},                           attributes => $form_attrs,
5924                          {type => START_TAG_TOKEN, tag_name => 'hr'},                           line => $token->{line}, column => $token->{column}},
5925                          {type => START_TAG_TOKEN, tag_name => 'p'},                          {type => START_TAG_TOKEN, tag_name => 'hr',
5926                          {type => START_TAG_TOKEN, tag_name => 'label'},                           line => $token->{line}, column => $token->{column}},
5927                            {type => START_TAG_TOKEN, tag_name => 'p',
5928                             line => $token->{line}, column => $token->{column}},
5929                            {type => START_TAG_TOKEN, tag_name => 'label',
5930                             line => $token->{line}, column => $token->{column}},
5931                         );                         );
5932            if ($prompt_attr) {            if ($prompt_attr) {
5933              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};              !!!cp ('t390');
5934                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
5935                               #line => $token->{line}, column => $token->{column},
5936                              };
5937            } else {            } else {
5938                !!!cp ('t391');
5939              push @tokens, {type => CHARACTER_TOKEN,              push @tokens, {type => CHARACTER_TOKEN,
5940                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD                             data => 'This is a searchable index. Insert your search keywords here: ',
5941                               #line => $token->{line}, column => $token->{column},
5942                              }; # SHOULD
5943              ## TODO: make this configurable              ## TODO: make this configurable
5944            }            }
5945            push @tokens,            push @tokens,
5946                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
5947                             line => $token->{line}, column => $token->{column}},
5948                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
5949                          {type => END_TAG_TOKEN, tag_name => 'label'},                          {type => END_TAG_TOKEN, tag_name => 'label',
5950                          {type => END_TAG_TOKEN, tag_name => 'p'},                           line => $token->{line}, column => $token->{column}},
5951                          {type => START_TAG_TOKEN, tag_name => 'hr'},                          {type => END_TAG_TOKEN, tag_name => 'p',
5952                          {type => END_TAG_TOKEN, tag_name => 'form'};                           line => $token->{line}, column => $token->{column}},
5953                            {type => START_TAG_TOKEN, tag_name => 'hr',
5954                             line => $token->{line}, column => $token->{column}},
5955                            {type => END_TAG_TOKEN, tag_name => 'form',
5956                             line => $token->{line}, column => $token->{column}};
5957            $token = shift @tokens;            $token = shift @tokens;
5958            !!!back-token (@tokens);            !!!back-token (@tokens);
5959            redo B;            redo B;
# Line 5119  sub _tree_construction_main ($) { Line 5961  sub _tree_construction_main ($) {
5961        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
5962          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
5963          my $el;          my $el;
5964          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $token->{tag_name}, $token->{attributes}, $token);
5965                    
5966          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
5967          $self->{content_model} = RCDATA_CONTENT_MODEL;          $self->{content_model} = RCDATA_CONTENT_MODEL;
# Line 5132  sub _tree_construction_main ($) { Line 5974  sub _tree_construction_main ($) {
5974          if ($token->{type} == CHARACTER_TOKEN) {          if ($token->{type} == CHARACTER_TOKEN) {
5975            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
5976            unless (length $token->{data}) {            unless (length $token->{data}) {
5977                !!!cp ('t392');
5978              !!!next-token;              !!!next-token;
5979              } else {
5980                !!!cp ('t393');
5981            }            }
5982            } else {
5983              !!!cp ('t394');
5984          }          }
5985          while ($token->{type} == CHARACTER_TOKEN) {          while ($token->{type} == CHARACTER_TOKEN) {
5986              !!!cp ('t395');
5987            $text .= $token->{data};            $text .= $token->{data};
5988            !!!next-token;            !!!next-token;
5989          }          }
5990          if (length $text) {          if (length $text) {
5991              !!!cp ('t396');
5992            $el->manakai_append_text ($text);            $el->manakai_append_text ($text);
5993          }          }
5994                    
# Line 5147  sub _tree_construction_main ($) { Line 5996  sub _tree_construction_main ($) {
5996                    
5997          if ($token->{type} == END_TAG_TOKEN and          if ($token->{type} == END_TAG_TOKEN and
5998              $token->{tag_name} eq $tag_name) {              $token->{tag_name} eq $tag_name) {
5999              !!!cp ('t397');
6000            ## Ignore the token            ## Ignore the token
6001          } else {          } else {
6002            !!!parse-error (type => 'in RCDATA:#'.$token->{type});            !!!cp ('t398');
6003              !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
6004          }          }
6005          !!!next-token;          !!!next-token;
6006          redo B;          redo B;
6007        } elsif ({        } elsif ({
                 iframe => 1,  
                 noembed => 1,  
                 noframes => 1,  
                 noscript => 0, ## TODO: 1 if scripting is enabled  
                }->{$token->{tag_name}}) {  
         ## NOTE: There is an "as if in body" code clone.  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         redo B;  
       } elsif ($token->{tag_name} eq 'select') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           
         $self->{insertion_mode} = IN_SELECT_IM;  
         !!!next-token;  
         redo B;  
       } elsif ({  
6008                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
6009                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
6010                  tbody => 1, td => 1, tfoot => 1, th => 1,                  tbody => 1, td => 1, tfoot => 1, th => 1,
6011                  thead => 1, tr => 1,                  thead => 1, tr => 1,
6012                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6013          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!cp ('t401');
6014            !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);
6015          ## Ignore the token          ## Ignore the token
6016          !!!next-token;          !!!next-token;
6017          redo B;          redo B;
6018                    
6019          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
6020        } else {        } else {
6021            if ($token->{tag_name} eq 'image') {
6022              !!!cp ('t384');
6023              !!!parse-error (type => 'image', token => $token);
6024              $token->{tag_name} = 'img';
6025            } else {
6026              !!!cp ('t385');
6027            }
6028    
6029            ## NOTE: There is an "as if <br>" code clone.
6030          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6031                    
6032          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6033    
6034            if ({
6035                 applet => 1, marquee => 1, object => 1,
6036                }->{$token->{tag_name}}) {
6037              !!!cp ('t380');
6038              push @$active_formatting_elements, ['#marker', ''];
6039            } elsif ({
6040                      b => 1, big => 1, em => 1, font => 1, i => 1,
6041                      s => 1, small => 1, strile => 1,
6042                      strong => 1, tt => 1, u => 1,
6043                     }->{$token->{tag_name}}) {
6044              !!!cp ('t375');
6045              push @$active_formatting_elements, $self->{open_elements}->[-1];
6046            } elsif ($token->{tag_name} eq 'input') {
6047              !!!cp ('t388');
6048              ## TODO: associate with $self->{form_element} if defined
6049              pop @{$self->{open_elements}};
6050            } elsif ({
6051                      area => 1, basefont => 1, bgsound => 1, br => 1,
6052                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
6053                      #image => 1,
6054                     }->{$token->{tag_name}}) {
6055              !!!cp ('t388.1');
6056              pop @{$self->{open_elements}};
6057            } elsif ($token->{tag_name} eq 'select') {
6058              ## TODO: associate with $self->{form_element} if defined
6059            
6060              if ($self->{insertion_mode} & TABLE_IMS or
6061                  $self->{insertion_mode} & BODY_TABLE_IMS or
6062                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
6063                !!!cp ('t400.1');
6064                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
6065              } else {
6066                !!!cp ('t400.2');
6067                $self->{insertion_mode} = IN_SELECT_IM;
6068              }
6069            } else {
6070              !!!cp ('t402');
6071            }
6072                    
6073          !!!next-token;          !!!next-token;
6074          redo B;          redo B;
6075        }        }
6076      } elsif ($token->{type} == END_TAG_TOKEN) {      } elsif ($token->{type} == END_TAG_TOKEN) {
6077        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
6078          if (@{$self->{open_elements}} > 1 and          ## has a |body| element in scope
6079              $self->{open_elements}->[1]->[1] eq 'body') {          my $i;
6080            for (@{$self->{open_elements}}) {          INSCOPE: {
6081              unless ({            for (reverse @{$self->{open_elements}}) {
6082                         dd => 1, dt => 1, li => 1, p => 1, td => 1,              if ($_->[1] eq 'body') {
6083                         th => 1, tr => 1, body => 1, html => 1,                !!!cp ('t405');
6084                       tbody => 1, tfoot => 1, thead => 1,                $i = $_;
6085                      }->{$_->[1]}) {                last INSCOPE;
6086                !!!parse-error (type => 'not closed:'.$_->[1]);              } elsif ({
6087                          applet => 1, table => 1, caption => 1, td => 1, th => 1,
6088                          button => 1, marquee => 1, object => 1, html => 1,
6089                         }->{$_->[1]}) {
6090                  !!!cp ('t405.1');
6091                  last;
6092              }              }
6093            }            }
6094    
6095            $self->{insertion_mode} = AFTER_BODY_IM;            !!!parse-error (type => 'start tag not allowed',
6096            !!!next-token;                            value => $token->{tag_name}, token => $token);
6097            redo B;            ## NOTE: Ignore the token.
         } else {  
           !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           ## Ignore the token  
6098            !!!next-token;            !!!next-token;
6099            redo B;            redo B;
6100            } # INSCOPE
6101    
6102            for (@{$self->{open_elements}}) {
6103              unless ({
6104                       dd => 1, dt => 1, li => 1, p => 1, td => 1,
6105                       th => 1, tr => 1, body => 1, html => 1,
6106                       tbody => 1, tfoot => 1, thead => 1,
6107                      }->{$_->[1]}) {
6108                !!!cp ('t403');
6109                !!!parse-error (type => 'not closed:'.$_->[1], token => $token);
6110                last;
6111              } else {
6112                !!!cp ('t404');
6113              }
6114          }          }
6115    
6116            $self->{insertion_mode} = AFTER_BODY_IM;
6117            !!!next-token;
6118            redo B;
6119        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
6120          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') {
6121            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
6122            if ($self->{open_elements}->[-1]->[1] ne 'body') {            if ($self->{open_elements}->[-1]->[1] ne 'body') {
6123              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!cp ('t406');
6124                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1], token => $token);
6125              } else {
6126                !!!cp ('t407');
6127            }            }
6128            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6129            ## reprocess            ## reprocess
6130            redo B;            redo B;
6131          } else {          } else {
6132            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t408');
6133              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6134            ## Ignore the token            ## Ignore the token
6135            !!!next-token;            !!!next-token;
6136            redo B;            redo B;
# Line 5232  sub _tree_construction_main ($) { Line 6139  sub _tree_construction_main ($) {
6139                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6140                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1, listing => 1,
6141                  menu => 1, ol => 1, pre => 1, ul => 1,                  menu => 1, ol => 1, pre => 1, ul => 1,
                 p => 1,  
6142                  dd => 1, dt => 1, li => 1,                  dd => 1, dt => 1, li => 1,
6143                  button => 1, marquee => 1, object => 1,                  applet => 1, button => 1, marquee => 1, object => 1,
6144                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6145          ## has an element in scope          ## has an element in scope
6146          my $i;          my $i;
6147          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6148            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6149            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] eq $token->{tag_name}) {
6150              ## generate implied end tags              !!!cp ('t410');
             if ({  
                  dd => ($token->{tag_name} ne 'dd'),  
                  dt => ($token->{tag_name} ne 'dt'),  
                  li => ($token->{tag_name} ne 'li'),  
                  p => ($token->{tag_name} ne 'p'),  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
6151              $i = $_;              $i = $_;
6152              last INSCOPE unless $token->{tag_name} eq 'p';              last INSCOPE;
6153            } elsif ({            } elsif ({
6154                      table => 1, caption => 1, td => 1, th => 1,                      applet => 1, table => 1, caption => 1, td => 1, th => 1,
6155                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
6156                     }->{$node->[1]}) {                     }->{$node->[1]}) {
6157                !!!cp ('t411');
6158              last INSCOPE;              last INSCOPE;
6159            }            }
6160          } # INSCOPE          } # INSCOPE
6161            
6162          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
6163            if (defined $i) {            !!!cp ('t413');
6164              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6165            } else {
6166              ## Step 1. generate implied end tags
6167              while ({
6168                      dd => ($token->{tag_name} ne 'dd'),
6169                      dt => ($token->{tag_name} ne 'dt'),
6170                      li => ($token->{tag_name} ne 'li'),
6171                      p => 1,
6172                     }->{$self->{open_elements}->[-1]->[1]}) {
6173                !!!cp ('t409');
6174                pop @{$self->{open_elements}};
6175              }
6176    
6177              ## Step 2.
6178              if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
6179                !!!cp ('t412');
6180                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);
6181            } else {            } else {
6182              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t414');
6183            }            }
6184          }  
6185                      ## Step 3.
         if (defined $i) {  
6186            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6187          } elsif ($token->{tag_name} eq 'p') {  
6188            ## As if <p>, then reprocess the current token            ## Step 4.
6189            my $el;            $clear_up_to_marker->()
6190            !!!create-element ($el, 'p');                if {
6191            $insert->($el);                  applet => 1, button => 1, marquee => 1, object => 1,
6192                  }->{$token->{tag_name}};
6193          }          }
         $clear_up_to_marker->()  
           if {  
             button => 1, marquee => 1, object => 1,  
           }->{$token->{tag_name}};  
6194          !!!next-token;          !!!next-token;
6195          redo B;          redo B;
6196        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
6197            undef $self->{form_element};
6198    
6199          ## has an element in scope          ## has an element in scope
6200            my $i;
6201          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6202            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6203            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] eq $token->{tag_name}) {
6204              ## generate implied end tags              !!!cp ('t418');
6205              if ({              $i = $_;
                  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_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
6206              last INSCOPE;              last INSCOPE;
6207            } elsif ({            } elsif ({
6208                      table => 1, caption => 1, td => 1, th => 1,                      applet => 1, table => 1, caption => 1, td => 1, th => 1,
6209                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
6210                     }->{$node->[1]}) {                     }->{$node->[1]}) {
6211                !!!cp ('t419');
6212              last INSCOPE;              last INSCOPE;
6213            }            }
6214          } # INSCOPE          } # INSCOPE
6215            
6216          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {          unless (defined $i) { # has an element in scope
6217            pop @{$self->{open_elements}};            !!!cp ('t421');
6218          } else {            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6219            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          } else {
6220              ## Step 1. generate implied end tags
6221              while ({
6222                      dd => 1, dt => 1, li => 1, p => 1,
6223                     }->{$self->{open_elements}->[-1]->[1]}) {
6224                !!!cp ('t417');
6225                pop @{$self->{open_elements}};
6226              }
6227              
6228              ## Step 2.
6229              if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
6230                !!!cp ('t417.1');
6231                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);
6232              } else {
6233                !!!cp ('t420');
6234              }  
6235              
6236              ## Step 3.
6237              splice @{$self->{open_elements}}, $i;
6238          }          }
6239    
         undef $self->{form_element};  
6240          !!!next-token;          !!!next-token;
6241          redo B;          redo B;
6242        } elsif ({        } elsif ({
# Line 5331  sub _tree_construction_main ($) { Line 6249  sub _tree_construction_main ($) {
6249            if ({            if ({
6250                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6251                }->{$node->[1]}) {                }->{$node->[1]}) {
6252              ## generate implied end tags              !!!cp ('t423');
             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_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
6253              $i = $_;              $i = $_;
6254              last INSCOPE;              last INSCOPE;
6255            } elsif ({            } elsif ({
6256                      table => 1, caption => 1, td => 1, th => 1,                      applet => 1, table => 1, caption => 1, td => 1, th => 1,
6257                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
6258                     }->{$node->[1]}) {                     }->{$node->[1]}) {
6259                !!!cp ('t424');
6260              last INSCOPE;              last INSCOPE;
6261            }            }
6262          } # INSCOPE          } # INSCOPE
6263            
6264          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
6265            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t425.1');
6266              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6267            } else {
6268              ## Step 1. generate implied end tags
6269              while ({
6270                      dd => 1, dt => 1, li => 1, p => 1,
6271                     }->{$self->{open_elements}->[-1]->[1]}) {
6272                !!!cp ('t422');
6273                pop @{$self->{open_elements}};
6274              }
6275              
6276              ## Step 2.
6277              if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
6278                !!!cp ('t425');
6279                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6280              } else {
6281                !!!cp ('t426');
6282              }
6283    
6284              ## Step 3.
6285              splice @{$self->{open_elements}}, $i;
6286          }          }
6287                    
6288          splice @{$self->{open_elements}}, $i if defined $i;          !!!next-token;
6289            redo B;
6290          } elsif ($token->{tag_name} eq 'p') {
6291            ## has an element in scope
6292            my $i;
6293            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6294              my $node = $self->{open_elements}->[$_];
6295              if ($node->[1] eq $token->{tag_name}) {
6296                !!!cp ('t410.1');
6297                $i = $_;
6298                last INSCOPE;
6299              } elsif ({
6300                        applet => 1, table => 1, caption => 1, td => 1, th => 1,
6301                        button => 1, marquee => 1, object => 1, html => 1,
6302                       }->{$node->[1]}) {
6303                !!!cp ('t411.1');
6304                last INSCOPE;
6305              }
6306            } # INSCOPE
6307    
6308            if (defined $i) {
6309              if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
6310                !!!cp ('t412.1');
6311                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);
6312              } else {
6313                !!!cp ('t414.1');
6314              }
6315    
6316              splice @{$self->{open_elements}}, $i;
6317            } else {
6318              !!!cp ('t413.1');
6319              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6320    
6321              !!!cp ('t415.1');
6322              ## As if <p>, then reprocess the current token
6323              my $el;
6324              !!!create-element ($el, 'p',, $token);
6325              $insert->($el);
6326              ## NOTE: Not inserted into |$self->{open_elements}|.
6327            }
6328    
6329          !!!next-token;          !!!next-token;
6330          redo B;          redo B;
6331        } elsif ({        } elsif ({
# Line 5365  sub _tree_construction_main ($) { Line 6334  sub _tree_construction_main ($) {
6334                  nobr => 1, s => 1, small => 1, strile => 1,                  nobr => 1, s => 1, small => 1, strile => 1,
6335                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
6336                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6337          $formatting_end_tag->($token->{tag_name});          !!!cp ('t427');
6338            $formatting_end_tag->($token);
6339          redo B;          redo B;
6340        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
6341          !!!parse-error (type => 'unmatched end tag:br');          !!!cp ('t428');
6342            !!!parse-error (type => 'unmatched end tag:br', token => $token);
6343    
6344          ## As if <br>          ## As if <br>
6345          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6346                    
6347          my $el;          my $el;
6348          !!!create-element ($el, 'br');          !!!create-element ($el, 'br',, $token);
6349          $insert->($el);          $insert->($el);
6350                    
6351          ## Ignore the token.          ## Ignore the token.
# Line 5392  sub _tree_construction_main ($) { Line 6363  sub _tree_construction_main ($) {
6363                  table => 1, textarea => 1, wbr => 1,                  table => 1, textarea => 1, wbr => 1,
6364                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
6365                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6366          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t429');
6367            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6368          ## Ignore the token          ## Ignore the token
6369          !!!next-token;          !!!next-token;
6370          redo B;          redo B;
# Line 5409  sub _tree_construction_main ($) { Line 6381  sub _tree_construction_main ($) {
6381            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] eq $token->{tag_name}) {
6382              ## Step 1              ## Step 1
6383              ## generate implied end tags              ## generate implied end tags
6384              if ({              while ({
6385                   dd => 1, dt => 1, li => 1, p => 1,                      dd => 1, dt => 1, li => 1, p => 1,
6386                   td => 1, th => 1, tr => 1,                     }->{$self->{open_elements}->[-1]->[1]}) {
6387                   tbody => 1, tfoot => 1, thead => 1,                !!!cp ('t430');
6388                  }->{$self->{open_elements}->[-1]->[1]}) {                ## ISSUE: Can this case be reached?
6389                !!!back-token;                pop @{$self->{open_elements}};
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
6390              }              }
6391                    
6392              ## Step 2              ## Step 2
6393              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {
6394                  !!!cp ('t431');
6395                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
6396                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);
6397                } else {
6398                  !!!cp ('t432');
6399              }              }
6400                            
6401              ## Step 3              ## Step 3
# Line 5437  sub _tree_construction_main ($) { Line 6409  sub _tree_construction_main ($) {
6409                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
6410                  ($special_category->{$node->[1]} or                  ($special_category->{$node->[1]} or
6411                   $scoping_category->{$node->[1]})) {                   $scoping_category->{$node->[1]})) {
6412                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t433');
6413                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6414                ## Ignore the token                ## Ignore the token
6415                !!!next-token;                !!!next-token;
6416                last S2;                last S2;
6417              }              }
6418    
6419                !!!cp ('t434');
6420            }            }
6421                        
6422            ## Step 4            ## Step 4
# Line 5457  sub _tree_construction_main ($) { Line 6432  sub _tree_construction_main ($) {
6432      redo B;      redo B;
6433    } # B    } # B
6434    
   ## NOTE: The "trailing end" phase in HTML5 is split into  
   ## two insertion modes: "after html body" and "after html frameset".  
   ## NOTE: States in the main stage is preserved while  
   ## the parser stays in the trailing end phase. # MUST  
   
6435    ## Stop parsing # MUST    ## Stop parsing # MUST
6436        
6437    ## TODO: script stuffs    ## TODO: script stuffs
# Line 5503  sub set_inner_html ($$$) { Line 6473  sub set_inner_html ($$$) {
6473      my $p = $class->new;      my $p = $class->new;
6474      $p->{document} = $doc;      $p->{document} = $doc;
6475    
6476      ## Step 9 # MUST      ## Step 8 # MUST
6477      my $i = 0;      my $i = 0;
6478      my $line = 1;      my $line = 1;
6479      my $column = 0;      my $column = 0;
6480      $p->{set_next_input_character} = sub {      $p->{set_next_char} = sub {
6481        my $self = shift;        my $self = shift;
6482    
6483        pop @{$self->{prev_input_character}};        pop @{$self->{prev_char}};
6484        unshift @{$self->{prev_input_character}}, $self->{next_input_character};        unshift @{$self->{prev_char}}, $self->{next_char};
6485    
6486        $self->{next_input_character} = -1 and return if $i >= length $$s;        $self->{next_char} = -1 and return if $i >= length $$s;
6487        $self->{next_input_character} = ord substr $$s, $i++, 1;        $self->{next_char} = ord substr $$s, $i++, 1;
6488        $column++;        $column++;
6489    
6490        if ($self->{next_input_character} == 0x000A) { # LF        if ($self->{next_char} == 0x000A) { # LF
6491          $line++;          $line++;
6492          $column = 0;          $column = 0;
6493        } elsif ($self->{next_input_character} == 0x000D) { # CR          !!!cp ('i1');
6494          } elsif ($self->{next_char} == 0x000D) { # CR
6495          $i++ if substr ($$s, $i, 1) eq "\x0A";          $i++ if substr ($$s, $i, 1) eq "\x0A";
6496          $self->{next_input_character} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
6497          $line++;          $line++;
6498          $column = 0;          $column = 0;
6499        } elsif ($self->{next_input_character} > 0x10FFFF) {          !!!cp ('i2');
6500          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        } elsif ($self->{next_char} > 0x10FFFF) {
6501        } elsif ($self->{next_input_character} == 0x0000) { # NULL          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
6502            !!!cp ('i3');
6503          } elsif ($self->{next_char} == 0x0000) { # NULL
6504            !!!cp ('i4');
6505          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
6506          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
6507        }        }
6508      };      };
6509      $p->{prev_input_character} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];
6510      $p->{next_input_character} = -1;      $p->{next_char} = -1;
6511            
6512      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
6513        my (%opt) = @_;        my (%opt) = @_;
# Line 5566  sub set_inner_html ($$$) { Line 6540  sub set_inner_html ($$$) {
6540    
6541      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $node_ln];
6542    
6543      ## Step 4      ## Step 3
6544      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
6545        ('http://www.w3.org/1999/xhtml', [undef, 'html']);        ('http://www.w3.org/1999/xhtml', [undef, 'html']);
6546    
6547      ## Step 5 # MUST      ## Step 4 # MUST
6548      $doc->append_child ($root);      $doc->append_child ($root);
6549    
6550      ## Step 6 # MUST      ## Step 5 # MUST
6551      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, 'html'];
6552    
6553      undef $p->{head_element};      undef $p->{head_element};
6554    
6555      ## Step 7 # MUST      ## Step 6 # MUST
6556      $p->_reset_insertion_mode;      $p->_reset_insertion_mode;
6557    
6558      ## Step 8 # MUST      ## Step 7 # MUST
6559      my $anode = $node;      my $anode = $node;
6560      AN: while (defined $anode) {      AN: while (defined $anode) {
6561        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
6562          my $nsuri = $anode->namespace_uri;          my $nsuri = $anode->namespace_uri;
6563          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
6564            if ($anode->manakai_local_name eq 'form') {            if ($anode->manakai_local_name eq 'form') {
6565                !!!cp ('i5');
6566              $p->{form_element} = $anode;              $p->{form_element} = $anode;
6567              last AN;              last AN;
6568            }            }
# Line 5596  sub set_inner_html ($$$) { Line 6571  sub set_inner_html ($$$) {
6571        $anode = $anode->parent_node;        $anode = $anode->parent_node;
6572      } # AN      } # AN
6573            
6574      ## Step 3 # MUST      ## Step 9 # MUST
     ## Step 10 # MUST  
6575      {      {
6576        my $self = $p;        my $self = $p;
6577        !!!next-token;        !!!next-token;
6578      }      }
6579      $p->_tree_construction_main;      $p->_tree_construction_main;
6580    
6581      ## Step 11 # MUST      ## Step 10 # MUST
6582      my @cn = @{$node->child_nodes};      my @cn = @{$node->child_nodes};
6583      for (@cn) {      for (@cn) {
6584        $node->remove_child ($_);        $node->remove_child ($_);
6585      }      }
6586      ## ISSUE: mutation events? read-only?      ## ISSUE: mutation events? read-only?
6587    
6588      ## Step 12 # MUST      ## Step 11 # MUST
6589      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
6590      for (@cn) {      for (@cn) {
6591        $this_doc->adopt_node ($_);        $this_doc->adopt_node ($_);

Legend:
Removed from v.1.72  
changed lines
  Added in v.1.118

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24