/[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.64 by wakaba, Sun Nov 11 08:39:42 2007 UTC revision 1.123 by wakaba, Sun Apr 6 10:32:00 2008 UTC
# Line 8  use Error qw(:try); Line 8  use Error qw(:try);
8  ## doc.write ('');  ## doc.write ('');
9  ## alert (doc.compatMode);  ## alert (doc.compatMode);
10    
11  ## ISSUE: HTML5 revision 967 says that the encoding layer MUST NOT  ## TODO: Control charcters and noncharacters are not allowed (HTML5 revision 1263)
12  ## strip BOM and the HTML layer MUST ignore it.  Whether we can do it  ## TODO: 1252 parse error (revision 1264)
13  ## is not yet clear.  ## TODO: 8859-11 = 874 (revision 1271)
14  ## "{U+FEFF}..." in UTF-16BE/UTF-16LE is three or four characters?  
15  ## "{U+FEFF}..." in GB18030?  sub A_EL () { 0b1 }
16    sub ADDRESS_EL () { 0b10 }
17    sub BODY_EL () { 0b100 }
18    sub BUTTON_EL () { 0b1000 }
19    sub CAPTION_EL () { 0b10000 }
20    sub DD_EL () { 0b100000 }
21    sub DIV_EL () { 0b1000000 }
22    sub DT_EL () { 0b10000000 }
23    sub FORM_EL () { 0b100000000 }
24    sub FORMATTING_EL () { 0b1000000000 }
25    sub FRAMESET_EL () { 0b10000000000 }
26    sub HEADING_EL () { 0b100000000000 }
27    sub HTML_EL () { 0b1000000000000 }
28    sub LI_EL () { 0b10000000000000 }
29    sub NOBR_EL () { 0b100000000000000 }
30    sub OPTION_EL () { 0b1000000000000000 }
31    sub OPTGROUP_EL () { 0b10000000000000000 }
32    sub P_EL () { 0b100000000000000000 }
33    sub SELECT_EL () { 0b1000000000000000000 }
34    sub TABLE_EL () { 0b10000000000000000000 }
35    sub TABLE_CELL_EL () { 0b100000000000000000000 }
36    sub TABLE_ROW_EL () { 0b1000000000000000000000 }
37    sub TABLE_ROW_GROUP_EL () { 0b10000000000000000000000 }
38    sub MISC_SCOPING_EL () { 0b100000000000000000000000 }
39    sub MISC_SPECIAL_EL () { 0b1000000000000000000000000 }
40    
41    sub TABLE_ROWS_EL () {
42      TABLE_EL |
43      TABLE_ROW_EL |
44      TABLE_ROW_GROUP_EL
45    }
46    
47    sub END_TAG_OPTIONAL_EL () {
48      DD_EL |
49      DT_EL |
50      LI_EL |
51      P_EL
52    }
53    
54    sub ALL_END_TAG_OPTIONAL_EL () {
55      END_TAG_OPTIONAL_EL |
56      BODY_EL |
57      HTML_EL |
58      TABLE_CELL_EL |
59      TABLE_ROW_EL |
60      TABLE_ROW_GROUP_EL
61    }
62    
63    sub SCOPING_EL () {
64      BUTTON_EL |
65      CAPTION_EL |
66      HTML_EL |
67      TABLE_EL |
68      TABLE_CELL_EL |
69      MISC_SCOPING_EL
70    }
71    
72    sub TABLE_SCOPING_EL () {
73      HTML_EL |
74      TABLE_EL
75    }
76    
77    sub TABLE_ROWS_SCOPING_EL () {
78      HTML_EL |
79      TABLE_ROW_GROUP_EL
80    }
81    
82    sub TABLE_ROW_SCOPING_EL () {
83      HTML_EL |
84      TABLE_ROW_EL
85    }
86    
87    sub SPECIAL_EL () {
88      ADDRESS_EL |
89      BODY_EL |
90      DIV_EL |
91      END_TAG_OPTIONAL_EL |
92      FORM_EL |
93      FRAMESET_EL |
94      HEADING_EL |
95      OPTION_EL |
96      OPTGROUP_EL |
97      SELECT_EL |
98      TABLE_ROW_EL |
99      TABLE_ROW_GROUP_EL |
100      MISC_SPECIAL_EL
101    }
102    
103    my $el_category = {
104      a => A_EL | FORMATTING_EL,
105      address => ADDRESS_EL,
106      applet => MISC_SCOPING_EL,
107      area => MISC_SPECIAL_EL,
108      b => FORMATTING_EL,
109      base => MISC_SPECIAL_EL,
110      basefont => MISC_SPECIAL_EL,
111      bgsound => MISC_SPECIAL_EL,
112      big => FORMATTING_EL,
113      blockquote => MISC_SPECIAL_EL,
114      body => BODY_EL,
115      br => MISC_SPECIAL_EL,
116      button => BUTTON_EL,
117      caption => CAPTION_EL,
118      center => MISC_SPECIAL_EL,
119      col => MISC_SPECIAL_EL,
120      colgroup => MISC_SPECIAL_EL,
121      dd => DD_EL,
122      dir => MISC_SPECIAL_EL,
123      div => DIV_EL,
124      dl => MISC_SPECIAL_EL,
125      dt => DT_EL,
126      em => FORMATTING_EL,
127      embed => MISC_SPECIAL_EL,
128      fieldset => MISC_SPECIAL_EL,
129      font => FORMATTING_EL,
130      form => FORM_EL,
131      frame => MISC_SPECIAL_EL,
132      frameset => FRAMESET_EL,
133      h1 => HEADING_EL,
134      h2 => HEADING_EL,
135      h3 => HEADING_EL,
136      h4 => HEADING_EL,
137      h5 => HEADING_EL,
138      h6 => HEADING_EL,
139      head => MISC_SPECIAL_EL,
140      hr => MISC_SPECIAL_EL,
141      html => HTML_EL,
142      i => FORMATTING_EL,
143      iframe => MISC_SPECIAL_EL,
144      img => MISC_SPECIAL_EL,
145      input => MISC_SPECIAL_EL,
146      isindex => MISC_SPECIAL_EL,
147      li => LI_EL,
148      link => MISC_SPECIAL_EL,
149      listing => MISC_SPECIAL_EL,
150      marquee => MISC_SCOPING_EL,
151      menu => MISC_SPECIAL_EL,
152      meta => MISC_SPECIAL_EL,
153      nobr => NOBR_EL | FORMATTING_EL,
154      noembed => MISC_SPECIAL_EL,
155      noframes => MISC_SPECIAL_EL,
156      noscript => MISC_SPECIAL_EL,
157      object => MISC_SCOPING_EL,
158      ol => MISC_SPECIAL_EL,
159      optgroup => OPTGROUP_EL,
160      option => OPTION_EL,
161      p => P_EL,
162      param => MISC_SPECIAL_EL,
163      plaintext => MISC_SPECIAL_EL,
164      pre => MISC_SPECIAL_EL,
165      s => FORMATTING_EL,
166      script => MISC_SPECIAL_EL,
167      select => SELECT_EL,
168      small => FORMATTING_EL,
169      spacer => MISC_SPECIAL_EL,
170      strike => FORMATTING_EL,
171      strong => FORMATTING_EL,
172      style => MISC_SPECIAL_EL,
173      table => TABLE_EL,
174      tbody => TABLE_ROW_GROUP_EL,
175      td => TABLE_CELL_EL,
176      textarea => MISC_SPECIAL_EL,
177      tfoot => TABLE_ROW_GROUP_EL,
178      th => TABLE_CELL_EL,
179      thead => TABLE_ROW_GROUP_EL,
180      title => MISC_SPECIAL_EL,
181      tr => TABLE_ROW_EL,
182      tt => FORMATTING_EL,
183      u => FORMATTING_EL,
184      ul => MISC_SPECIAL_EL,
185      wbr => MISC_SPECIAL_EL,
186    };
187    
188  my $permitted_slash_tag_name = {  my $permitted_slash_tag_name = {
189    base => 1,    base => 1,
# Line 20  my $permitted_slash_tag_name = { Line 191  my $permitted_slash_tag_name = {
191    meta => 1,    meta => 1,
192    hr => 1,    hr => 1,
193    br => 1,    br => 1,
194    img=> 1,    img => 1,
195    embed => 1,    embed => 1,
196    param => 1,    param => 1,
197    area => 1,    area => 1,
# Line 76  my $special_category = { Line 247  my $special_category = {
247    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,
248  };  };
249  my $scoping_category = {  my $scoping_category = {
250    button => 1, caption => 1, html => 1, marquee => 1, object => 1,    applet => 1, button => 1, caption => 1, html => 1, marquee => 1, object => 1,
251    table => 1, td => 1, th => 1,    table => 1, td => 1, th => 1,
252  };  };
253  my $formatting_category = {  my $formatting_category = {
# Line 97  sub parse_byte_string ($$$$;$) { Line 268  sub parse_byte_string ($$$$;$) {
268      $self->{input_encoding} = lc $charset; ## TODO: normalize name      $self->{input_encoding} = lc $charset; ## TODO: normalize name
269      $self->{confident} = 1;      $self->{confident} = 1;
270    } else {    } else {
271      $charset = 'windows-1252'; ## TODO: for now.      ## TODO: Implement HTML5 detection algorithm
272        require Whatpm::Charset::UniversalCharDet;
273        $charset = Whatpm::Charset::UniversalCharDet->detect_byte_string
274            (substr ($$bytes_s, 0, 1024));
275        $charset ||= 'windows-1252';
276      $s = \ (Encode::decode ($charset, $$bytes_s));      $s = \ (Encode::decode ($charset, $$bytes_s));
277      $self->{input_encoding} = $charset;      $self->{input_encoding} = $charset;
278      $self->{confident} = 0;      $self->{confident} = 0;
# Line 106  sub parse_byte_string ($$$$;$) { Line 281  sub parse_byte_string ($$$$;$) {
281    $self->{change_encoding} = sub {    $self->{change_encoding} = sub {
282      my $self = shift;      my $self = shift;
283      my $charset = lc shift;      my $charset = lc shift;
284        my $token = shift;
285      ## TODO: if $charset is supported      ## TODO: if $charset is supported
286      ## TODO: normalize charset name      ## TODO: normalize charset name
287    
# Line 124  sub parse_byte_string ($$$$;$) { Line 300  sub parse_byte_string ($$$$;$) {
300      }      }
301    
302      !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.      !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.
303          ':'.$charset, level => 'w');          ':'.$charset, level => 'w', token => $token);
304    
305      ## Step 3      ## Step 3
306      # if (can) {      # if (can) {
# Line 151  sub parse_byte_string ($$$$;$) { Line 327  sub parse_byte_string ($$$$;$) {
327    return $return;    return $return;
328  } # parse_byte_string  } # parse_byte_string
329    
330    ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
331    ## and the HTML layer MUST ignore it.  However, we does strip BOM in
332    ## the encoding layer and the HTML layer does not ignore any U+FEFF,
333    ## because the core part of our HTML parser expects a string of character,
334    ## not a string of bytes or code units or anything which might contain a BOM.
335    ## Therefore, any parser interface that accepts a string of bytes,
336    ## such as |parse_byte_string| in this module, must ensure that it does
337    ## strip the BOM and never strip any ZWNBSP.
338    
339  *parse_char_string = \&parse_string;  *parse_char_string = \&parse_string;
340    
341  sub parse_string ($$$;$) {  sub parse_string ($$$;$) {
# Line 166  sub parse_string ($$$;$) { Line 351  sub parse_string ($$$;$) {
351        if defined $self->{input_encoding};        if defined $self->{input_encoding};
352    
353    my $i = 0;    my $i = 0;
354    my $line = 1;    $self->{line_prev} = $self->{line} = 1;
355    my $column = 0;    $self->{column_prev} = $self->{column} = 0;
356    $self->{set_next_input_character} = sub {    $self->{set_next_char} = sub {
357      my $self = shift;      my $self = shift;
358    
359      pop @{$self->{prev_input_character}};      pop @{$self->{prev_char}};
360      unshift @{$self->{prev_input_character}}, $self->{next_input_character};      unshift @{$self->{prev_char}}, $self->{next_char};
361    
362      $self->{next_input_character} = -1 and return if $i >= length $$s;      $self->{next_char} = -1 and return if $i >= length $$s;
363      $self->{next_input_character} = ord substr $$s, $i++, 1;      $self->{next_char} = ord substr $$s, $i++, 1;
364      $column++;  
365        ($self->{line_prev}, $self->{column_prev})
366            = ($self->{line}, $self->{column});
367        $self->{column}++;
368            
369      if ($self->{next_input_character} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
370        $line++;        $self->{line}++;
371        $column = 0;        $self->{column} = 0;
372      } elsif ($self->{next_input_character} == 0x000D) { # CR      } elsif ($self->{next_char} == 0x000D) { # CR
373        $i++ if substr ($$s, $i, 1) eq "\x0A";        $i++ if substr ($$s, $i, 1) eq "\x0A";
374        $self->{next_input_character} = 0x000A; # LF # MUST        $self->{next_char} = 0x000A; # LF # MUST
375        $line++;        $self->{line}++;
376        $column = 0;        $self->{column} = 0;
377      } elsif ($self->{next_input_character} > 0x10FFFF) {      } elsif ($self->{next_char} > 0x10FFFF) {
378        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
379      } elsif ($self->{next_input_character} == 0x0000) { # NULL      } elsif ($self->{next_char} == 0x0000) { # NULL
380        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
381        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
382      }      }
383    };    };
384    $self->{prev_input_character} = [-1, -1, -1];    $self->{prev_char} = [-1, -1, -1];
385    $self->{next_input_character} = -1;    $self->{next_char} = -1;
386    
387    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
388      my (%opt) = @_;      my (%opt) = @_;
389      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
390        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
391        warn "Parse error ($opt{type}) at line $line column $column\n";
392    };    };
393    $self->{parse_error} = sub {    $self->{parse_error} = sub {
394      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
395    };    };
396    
397    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
# Line 209  sub parse_string ($$$;$) { Line 399  sub parse_string ($$$;$) {
399    $self->_construct_tree;    $self->_construct_tree;
400    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
401    
402      delete $self->{parse_error}; # remove loop
403    
404    return $self->{document};    return $self->{document};
405  } # parse_string  } # parse_string
406    
407  sub new ($) {  sub new ($) {
408    my $class = shift;    my $class = shift;
409    my $self = bless {}, $class;    my $self = bless {}, $class;
410    $self->{set_next_input_character} = sub {    $self->{set_next_char} = sub {
411      $self->{next_input_character} = -1;      $self->{next_char} = -1;
412    };    };
413    $self->{parse_error} = sub {    $self->{parse_error} = sub {
414      #      #
# Line 275  sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUO Line 467  sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUO
467  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }
468  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
469  sub BOGUS_DOCTYPE_STATE () { 32 }  sub BOGUS_DOCTYPE_STATE () { 32 }
470    sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
471    
472  sub DOCTYPE_TOKEN () { 1 }  sub DOCTYPE_TOKEN () { 1 }
473  sub COMMENT_TOKEN () { 2 }  sub COMMENT_TOKEN () { 2 }
# Line 291  sub TABLE_IMS ()      { 0b1000000 } Line 484  sub TABLE_IMS ()      { 0b1000000 }
484  sub ROW_IMS ()        { 0b10000000 }  sub ROW_IMS ()        { 0b10000000 }
485  sub BODY_AFTER_IMS () { 0b100000000 }  sub BODY_AFTER_IMS () { 0b100000000 }
486  sub FRAME_IMS ()      { 0b1000000000 }  sub FRAME_IMS ()      { 0b1000000000 }
487    sub SELECT_IMS ()     { 0b10000000000 }
488    
489    ## NOTE: "initial" and "before html" insertion modes have no constants.
490    
491    ## NOTE: "after after body" insertion mode.
492  sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }  sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
493    
494    ## NOTE: "after after frameset" insertion mode.
495  sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }  sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
496    
497  sub IN_HEAD_IM () { HEAD_IMS | 0b00 }  sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
498  sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }  sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
499  sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }  sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
# Line 307  sub IN_TABLE_IM () { TABLE_IMS } Line 507  sub IN_TABLE_IM () { TABLE_IMS }
507  sub AFTER_BODY_IM () { BODY_AFTER_IMS }  sub AFTER_BODY_IM () { BODY_AFTER_IMS }
508  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
509  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
510  sub IN_SELECT_IM () { 0b01 }  sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
511    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
512  sub IN_COLUMN_GROUP_IM () { 0b10 }  sub IN_COLUMN_GROUP_IM () { 0b10 }
513    
514  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
# Line 321  sub _initialize_tokenizer ($) { Line 522  sub _initialize_tokenizer ($) {
522    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
523    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
524    $self->{char} = [];    $self->{char} = [];
525    # $self->{next_input_character}    # $self->{next_char}
526    !!!next-input-character;    !!!next-input-character;
527    $self->{token} = [];    $self->{token} = [];
528    # $self->{escape}    # $self->{escape}
# Line 334  sub _initialize_tokenizer ($) { Line 535  sub _initialize_tokenizer ($) {
535  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
536  ##   ->{public_identifier} (DOCTYPE_TOKEN)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
537  ##   ->{system_identifier} (DOCTYPE_TOKEN)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
538  ##   ->{correct} == 1 or 0 (DOCTYPE_TOKEN)  ##   ->{quirks} == 1 or 0 (DOCTYPE_TOKEN): "force-quirks" flag
539  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
540    ##        ->{name}
541    ##        ->{value}
542    ##        ->{has_reference} == 1 or 0
543  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
544    
545  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
# Line 369  sub _get_next_token ($) { Line 573  sub _get_next_token ($) {
573    
574    A: {    A: {
575      if ($self->{state} == DATA_STATE) {      if ($self->{state} == DATA_STATE) {
576        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_char} == 0x0026) { # &
577          if ($self->{content_model} & CM_ENTITY) { # PCDATA | RCDATA          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
578                not $self->{escape}) {
579              !!!cp (1);
580            $self->{state} = ENTITY_DATA_STATE;            $self->{state} = ENTITY_DATA_STATE;
581            !!!next-input-character;            !!!next-input-character;
582            redo A;            redo A;
583          } else {          } else {
584              !!!cp (2);
585            #            #
586          }          }
587        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
588          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
589            unless ($self->{escape}) {            unless ($self->{escape}) {
590              if ($self->{prev_input_character}->[0] == 0x002D and # -              if ($self->{prev_char}->[0] == 0x002D and # -
591                  $self->{prev_input_character}->[1] == 0x0021 and # !                  $self->{prev_char}->[1] == 0x0021 and # !
592                  $self->{prev_input_character}->[2] == 0x003C) { # <                  $self->{prev_char}->[2] == 0x003C) { # <
593                  !!!cp (3);
594                $self->{escape} = 1;                $self->{escape} = 1;
595                } else {
596                  !!!cp (4);
597              }              }
598              } else {
599                !!!cp (5);
600            }            }
601          }          }
602                    
603          #          #
604        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_char} == 0x003C) { # <
605          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
606              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
607               not $self->{escape})) {               not $self->{escape})) {
608              !!!cp (6);
609            $self->{state} = TAG_OPEN_STATE;            $self->{state} = TAG_OPEN_STATE;
610            !!!next-input-character;            !!!next-input-character;
611            redo A;            redo A;
612          } else {          } else {
613              !!!cp (7);
614            #            #
615          }          }
616        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
617          if ($self->{escape} and          if ($self->{escape} and
618              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
619            if ($self->{prev_input_character}->[0] == 0x002D and # -            if ($self->{prev_char}->[0] == 0x002D and # -
620                $self->{prev_input_character}->[1] == 0x002D) { # -                $self->{prev_char}->[1] == 0x002D) { # -
621                !!!cp (8);
622              delete $self->{escape};              delete $self->{escape};
623              } else {
624                !!!cp (9);
625            }            }
626            } else {
627              !!!cp (10);
628          }          }
629                    
630          #          #
631        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
632          !!!emit ({type => END_OF_FILE_TOKEN});          !!!cp (11);
633            !!!emit ({type => END_OF_FILE_TOKEN,
634                      line => $self->{line}, column => $self->{column}});
635          last A; ## TODO: ok?          last A; ## TODO: ok?
636          } else {
637            !!!cp (12);
638        }        }
639        # Anything else        # Anything else
640        my $token = {type => CHARACTER_TOKEN,        my $token = {type => CHARACTER_TOKEN,
641                     data => chr $self->{next_input_character}};                     data => chr $self->{next_char},
642                       line => $self->{line}, column => $self->{column},
643                      };
644        ## Stay in the data state        ## Stay in the data state
645        !!!next-input-character;        !!!next-input-character;
646    
# Line 424  sub _get_next_token ($) { Line 649  sub _get_next_token ($) {
649        redo A;        redo A;
650      } elsif ($self->{state} == ENTITY_DATA_STATE) {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
651        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
652    
653          my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
654                
655        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);
656    
657        $self->{state} = DATA_STATE;        $self->{state} = DATA_STATE;
658        # next-input-character is already done        # next-input-character is already done
659    
660        unless (defined $token) {        unless (defined $token) {
661          !!!emit ({type => CHARACTER_TOKEN, data => '&'});          !!!cp (13);
662            !!!emit ({type => CHARACTER_TOKEN, data => '&',
663                      line => $l, column => $c,
664                     });
665        } else {        } else {
666            !!!cp (14);
667          !!!emit ($token);          !!!emit ($token);
668        }        }
669    
670        redo A;        redo A;
671      } elsif ($self->{state} == TAG_OPEN_STATE) {      } elsif ($self->{state} == TAG_OPEN_STATE) {
672        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
673          if ($self->{next_input_character} == 0x002F) { # /          if ($self->{next_char} == 0x002F) { # /
674              !!!cp (15);
675            !!!next-input-character;            !!!next-input-character;
676            $self->{state} = CLOSE_TAG_OPEN_STATE;            $self->{state} = CLOSE_TAG_OPEN_STATE;
677            redo A;            redo A;
678          } else {          } else {
679              !!!cp (16);
680            ## reconsume            ## reconsume
681            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
682    
683            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
684                        line => $self->{line_prev},
685                        column => $self->{column_prev},
686                       });
687    
688            redo A;            redo A;
689          }          }
690        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
691          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_char} == 0x0021) { # !
692              !!!cp (17);
693            $self->{state} = MARKUP_DECLARATION_OPEN_STATE;            $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
694            !!!next-input-character;            !!!next-input-character;
695            redo A;            redo A;
696          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{next_char} == 0x002F) { # /
697              !!!cp (18);
698            $self->{state} = CLOSE_TAG_OPEN_STATE;            $self->{state} = CLOSE_TAG_OPEN_STATE;
699            !!!next-input-character;            !!!next-input-character;
700            redo A;            redo A;
701          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
702                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{next_char} <= 0x005A) { # A..Z
703              !!!cp (19);
704            $self->{current_token}            $self->{current_token}
705              = {type => START_TAG_TOKEN,              = {type => START_TAG_TOKEN,
706                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020),
707                   line => $self->{line_prev},
708                   column => $self->{column_prev}};
709            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
710            !!!next-input-character;            !!!next-input-character;
711            redo A;            redo A;
712          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
713                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
714              !!!cp (20);
715            $self->{current_token} = {type => START_TAG_TOKEN,            $self->{current_token} = {type => START_TAG_TOKEN,
716                              tag_name => chr ($self->{next_input_character})};                                      tag_name => chr ($self->{next_char}),
717                                        line => $self->{line_prev},
718                                        column => $self->{column_prev}};
719            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
720            !!!next-input-character;            !!!next-input-character;
721            redo A;            redo A;
722          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
723            !!!parse-error (type => 'empty start tag');            !!!cp (21);
724              !!!parse-error (type => 'empty start tag',
725                              line => $self->{line_prev},
726                              column => $self->{column_prev});
727            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
728            !!!next-input-character;            !!!next-input-character;
729    
730            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
731                        line => $self->{line_prev},
732                        column => $self->{column_prev},
733                       });
734    
735            redo A;            redo A;
736          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
737            !!!parse-error (type => 'pio');            !!!cp (22);
738              !!!parse-error (type => 'pio',
739                              line => $self->{line_prev},
740                              column => $self->{column_prev});
741            $self->{state} = BOGUS_COMMENT_STATE;            $self->{state} = BOGUS_COMMENT_STATE;
742            ## $self->{next_input_character} is intentionally left as is            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
743                                        line => $self->{line_prev},
744                                        column => $self->{column_prev},
745                                       };
746              ## $self->{next_char} is intentionally left as is
747            redo A;            redo A;
748          } else {          } else {
749              !!!cp (23);
750            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago');
751            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
752            ## reconsume            ## reconsume
753    
754            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
755                        line => $self->{line_prev},
756                        column => $self->{column_prev},
757                       });
758    
759            redo A;            redo A;
760          }          }
# Line 501  sub _get_next_token ($) { Line 762  sub _get_next_token ($) {
762          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
763        }        }
764      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
765          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
766        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
767          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
768    
769            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
770            my @next_char;            my @next_char;
771            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++) {
772              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_char};
773              my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);              my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);
774              my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;              my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;
775              if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {              if ($self->{next_char} == $c or $self->{next_char} == $C) {
776                  !!!cp (24);
777                !!!next-input-character;                !!!next-input-character;
778                next TAGNAME;                next TAGNAME;
779              } else {              } else {
780                $self->{next_input_character} = shift @next_char; # reconsume                !!!cp (25);
781                  $self->{next_char} = shift @next_char; # reconsume
782                !!!back-next-input-character (@next_char);                !!!back-next-input-character (@next_char);
783                $self->{state} = DATA_STATE;                $self->{state} = DATA_STATE;
784    
785                !!!emit ({type => CHARACTER_TOKEN, data => '</'});                !!!emit ({type => CHARACTER_TOKEN, data => '</',
786                            line => $l, column => $c,
787                           });
788        
789                redo A;                redo A;
790              }              }
791            }            }
792            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_char};
793                
794            unless ($self->{next_input_character} == 0x0009 or # HT            unless ($self->{next_char} == 0x0009 or # HT
795                    $self->{next_input_character} == 0x000A or # LF                    $self->{next_char} == 0x000A or # LF
796                    $self->{next_input_character} == 0x000B or # VT                    $self->{next_char} == 0x000B or # VT
797                    $self->{next_input_character} == 0x000C or # FF                    $self->{next_char} == 0x000C or # FF
798                    $self->{next_input_character} == 0x0020 or # SP                    $self->{next_char} == 0x0020 or # SP
799                    $self->{next_input_character} == 0x003E or # >                    $self->{next_char} == 0x003E or # >
800                    $self->{next_input_character} == 0x002F or # /                    $self->{next_char} == 0x002F or # /
801                    $self->{next_input_character} == -1) {                    $self->{next_char} == -1) {
802              $self->{next_input_character} = shift @next_char; # reconsume              !!!cp (26);
803                $self->{next_char} = shift @next_char; # reconsume
804              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
805              $self->{state} = DATA_STATE;              $self->{state} = DATA_STATE;
806              !!!emit ({type => CHARACTER_TOKEN, data => '</'});              !!!emit ({type => CHARACTER_TOKEN, data => '</',
807                          line => $l, column => $c,
808                         });
809              redo A;              redo A;
810            } else {            } else {
811              $self->{next_input_character} = shift @next_char;              !!!cp (27);
812                $self->{next_char} = shift @next_char;
813              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
814              # and consume...              # and consume...
815            }            }
816          } else {          } else {
817            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
818              !!!cp (28);
819            # next-input-character is already done            # next-input-character is already done
820            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
821            !!!emit ({type => CHARACTER_TOKEN, data => '</'});            !!!emit ({type => CHARACTER_TOKEN, data => '</',
822                        line => $l, column => $c,
823                       });
824            redo A;            redo A;
825          }          }
826        }        }
827                
828        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_char} and
829            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
830          $self->{current_token} = {type => END_TAG_TOKEN,          !!!cp (29);
831                            tag_name => chr ($self->{next_input_character} + 0x0020)};          $self->{current_token}
832                = {type => END_TAG_TOKEN,
833                   tag_name => chr ($self->{next_char} + 0x0020),
834                   line => $l, column => $c};
835          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
836          !!!next-input-character;          !!!next-input-character;
837          redo A;          redo A;
838        } elsif (0x0061 <= $self->{next_input_character} and        } elsif (0x0061 <= $self->{next_char} and
839                 $self->{next_input_character} <= 0x007A) { # a..z                 $self->{next_char} <= 0x007A) { # a..z
840            !!!cp (30);
841          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token} = {type => END_TAG_TOKEN,
842                            tag_name => chr ($self->{next_input_character})};                                    tag_name => chr ($self->{next_char}),
843                                      line => $l, column => $c};
844          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
845          !!!next-input-character;          !!!next-input-character;
846          redo A;          redo A;
847        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
848          !!!parse-error (type => 'empty end tag');          !!!cp (31);
849            !!!parse-error (type => 'empty end tag',
850                            line => $self->{line_prev}, ## "<" in "</>"
851                            column => $self->{column_prev} - 1);
852          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
853          !!!next-input-character;          !!!next-input-character;
854          redo A;          redo A;
855        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
856            !!!cp (32);
857          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
858          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
859          # reconsume          # reconsume
860    
861          !!!emit ({type => CHARACTER_TOKEN, data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
862                      line => $l, column => $c,
863                     });
864    
865          redo A;          redo A;
866        } else {        } else {
867            !!!cp (33);
868          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
869          $self->{state} = BOGUS_COMMENT_STATE;          $self->{state} = BOGUS_COMMENT_STATE;
870          ## $self->{next_input_character} is intentionally left as is          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
871                                      line => $self->{line_prev}, # "<" of "</"
872                                      column => $self->{column_prev} - 1,
873                                     };
874            ## $self->{next_char} is intentionally left as is
875          redo A;          redo A;
876        }        }
877      } elsif ($self->{state} == TAG_NAME_STATE) {      } elsif ($self->{state} == TAG_NAME_STATE) {
878        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
879            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
880            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
881            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
882            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
883            !!!cp (34);
884          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
885          !!!next-input-character;          !!!next-input-character;
886          redo A;          redo A;
887        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
888          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
889            $self->{current_token}->{first_start_tag}            !!!cp (35);
               = not defined $self->{last_emitted_start_tag_name};  
890            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
891          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
892            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
893            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
894              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This should never be reached.
895            }            #  !!! cp (36);
896              #  !!! parse-error (type => 'end tag attribute');
897              #} else {
898                !!!cp (37);
899              #}
900          } else {          } else {
901            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
902          }          }
# Line 612  sub _get_next_token ($) { Line 906  sub _get_next_token ($) {
906          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
907    
908          redo A;          redo A;
909        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
910                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
911          $self->{current_token}->{tag_name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (38);
912            $self->{current_token}->{tag_name} .= chr ($self->{next_char} + 0x0020);
913            # start tag or end tag            # start tag or end tag
914          ## Stay in this state          ## Stay in this state
915          !!!next-input-character;          !!!next-input-character;
916          redo A;          redo A;
917        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
918          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
919          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
920            $self->{current_token}->{first_start_tag}            !!!cp (39);
               = not defined $self->{last_emitted_start_tag_name};  
921            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
922          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
923            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
924            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
925              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This state should never be reached.
926            }            #  !!! cp (40);
927              #  !!! parse-error (type => 'end tag attribute');
928              #} else {
929                !!!cp (41);
930              #}
931          } else {          } else {
932            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
933          }          }
# Line 639  sub _get_next_token ($) { Line 937  sub _get_next_token ($) {
937          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
938    
939          redo A;          redo A;
940        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
941          !!!next-input-character;          !!!next-input-character;
942          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_char} == 0x003E and # >
943              $self->{current_token}->{type} == START_TAG_TOKEN and              $self->{current_token}->{type} == START_TAG_TOKEN and
944              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
945            # permitted slash            # permitted slash
946              !!!cp (42);
947            #            #
948          } else {          } else {
949              !!!cp (43);
950            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
951          }          }
952          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
953          # next-input-character is already done          # next-input-character is already done
954          redo A;          redo A;
955        } else {        } else {
956          $self->{current_token}->{tag_name} .= chr $self->{next_input_character};          !!!cp (44);
957            $self->{current_token}->{tag_name} .= chr $self->{next_char};
958            # start tag or end tag            # start tag or end tag
959          ## Stay in the state          ## Stay in the state
960          !!!next-input-character;          !!!next-input-character;
961          redo A;          redo A;
962        }        }
963      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
964        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
965            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
966            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
967            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
968            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
969            !!!cp (45);
970          ## Stay in the state          ## Stay in the state
971          !!!next-input-character;          !!!next-input-character;
972          redo A;          redo A;
973        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
974          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
975            $self->{current_token}->{first_start_tag}            !!!cp (46);
               = not defined $self->{last_emitted_start_tag_name};  
976            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
977          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
978            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
979            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
980                !!!cp (47);
981              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
982              } else {
983                !!!cp (48);
984            }            }
985          } else {          } else {
986            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 687  sub _get_next_token ($) { Line 991  sub _get_next_token ($) {
991          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
992    
993          redo A;          redo A;
994        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
995                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
996          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (49);
997                                value => ''};          $self->{current_attribute}
998                = {name => chr ($self->{next_char} + 0x0020),
999                   value => '',
1000                   line => $self->{line}, column => $self->{column}};
1001          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1002          !!!next-input-character;          !!!next-input-character;
1003          redo A;          redo A;
1004        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1005          !!!next-input-character;          !!!next-input-character;
1006          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_char} == 0x003E and # >
1007              $self->{current_token}->{type} == START_TAG_TOKEN and              $self->{current_token}->{type} == START_TAG_TOKEN and
1008              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
1009            # permitted slash            # permitted slash
1010              !!!cp (50);
1011            #            #
1012          } else {          } else {
1013              !!!cp (51);
1014            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
1015          }          }
1016          ## Stay in the state          ## Stay in the state
1017          # next-input-character is already done          # next-input-character is already done
1018          redo A;          redo A;
1019        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1020          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1021          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1022            $self->{current_token}->{first_start_tag}            !!!cp (52);
               = not defined $self->{last_emitted_start_tag_name};  
1023            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1024          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1025            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1026            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1027                !!!cp (53);
1028              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1029              } else {
1030                !!!cp (54);
1031            }            }
1032          } else {          } else {
1033            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 728  sub _get_next_token ($) { Line 1039  sub _get_next_token ($) {
1039    
1040          redo A;          redo A;
1041        } else {        } else {
1042          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ({
1043                                value => ''};               0x0022 => 1, # "
1044                 0x0027 => 1, # '
1045                 0x003D => 1, # =
1046                }->{$self->{next_char}}) {
1047              !!!cp (55);
1048              !!!parse-error (type => 'bad attribute name');
1049            } else {
1050              !!!cp (56);
1051            }
1052            $self->{current_attribute}
1053                = {name => chr ($self->{next_char}),
1054                   value => '',
1055                   line => $self->{line}, column => $self->{column}};
1056          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1057          !!!next-input-character;          !!!next-input-character;
1058          redo A;          redo A;
# Line 738  sub _get_next_token ($) { Line 1061  sub _get_next_token ($) {
1061        my $before_leave = sub {        my $before_leave = sub {
1062          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1063              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1064            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});            !!!cp (57);
1065              !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name}, line => $self->{current_attribute}->{line}, column => $self->{current_attribute}->{column});
1066            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1067          } else {          } else {
1068              !!!cp (58);
1069            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
1070              = $self->{current_attribute};              = $self->{current_attribute};
1071          }          }
1072        }; # $before_leave        }; # $before_leave
1073    
1074        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1075            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1076            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1077            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1078            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1079            !!!cp (59);
1080          $before_leave->();          $before_leave->();
1081          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
1082          !!!next-input-character;          !!!next-input-character;
1083          redo A;          redo A;
1084        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1085            !!!cp (60);
1086          $before_leave->();          $before_leave->();
1087          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1088          !!!next-input-character;          !!!next-input-character;
1089          redo A;          redo A;
1090        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1091          $before_leave->();          $before_leave->();
1092          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1093            $self->{current_token}->{first_start_tag}            !!!cp (61);
               = not defined $self->{last_emitted_start_tag_name};  
1094            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1095          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1096              !!!cp (62);
1097            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1098            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1099              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 780  sub _get_next_token ($) { Line 1107  sub _get_next_token ($) {
1107          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1108    
1109          redo A;          redo A;
1110        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1111                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1112          $self->{current_attribute}->{name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (63);
1113            $self->{current_attribute}->{name} .= chr ($self->{next_char} + 0x0020);
1114          ## Stay in the state          ## Stay in the state
1115          !!!next-input-character;          !!!next-input-character;
1116          redo A;          redo A;
1117        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1118          $before_leave->();          $before_leave->();
1119          !!!next-input-character;          !!!next-input-character;
1120          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_char} == 0x003E and # >
1121              $self->{current_token}->{type} == START_TAG_TOKEN and              $self->{current_token}->{type} == START_TAG_TOKEN and
1122              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
1123            # permitted slash            # permitted slash
1124              !!!cp (64);
1125            #            #
1126          } else {          } else {
1127              !!!cp (65);
1128            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
1129          }          }
1130          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1131          # next-input-character is already done          # next-input-character is already done
1132          redo A;          redo A;
1133        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1134          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1135          $before_leave->();          $before_leave->();
1136          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1137            $self->{current_token}->{first_start_tag}            !!!cp (66);
               = not defined $self->{last_emitted_start_tag_name};  
1138            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1139          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1140            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1141            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1142                !!!cp (67);
1143              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1144              } else {
1145                ## NOTE: This state should never be reached.
1146                !!!cp (68);
1147            }            }
1148          } else {          } else {
1149            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 822  sub _get_next_token ($) { Line 1155  sub _get_next_token ($) {
1155    
1156          redo A;          redo A;
1157        } else {        } else {
1158          $self->{current_attribute}->{name} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x0022 or # "
1159                $self->{next_char} == 0x0027) { # '
1160              !!!cp (69);
1161              !!!parse-error (type => 'bad attribute name');
1162            } else {
1163              !!!cp (70);
1164            }
1165            $self->{current_attribute}->{name} .= chr ($self->{next_char});
1166          ## Stay in the state          ## Stay in the state
1167          !!!next-input-character;          !!!next-input-character;
1168          redo A;          redo A;
1169        }        }
1170      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
1171        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1172            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1173            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1174            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1175            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1176            !!!cp (71);
1177          ## Stay in the state          ## Stay in the state
1178          !!!next-input-character;          !!!next-input-character;
1179          redo A;          redo A;
1180        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1181            !!!cp (72);
1182          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1183          !!!next-input-character;          !!!next-input-character;
1184          redo A;          redo A;
1185        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1186          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1187            $self->{current_token}->{first_start_tag}            !!!cp (73);
               = not defined $self->{last_emitted_start_tag_name};  
1188            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1189          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1190            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1191            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1192                !!!cp (74);
1193              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1194              } else {
1195                ## NOTE: This state should never be reached.
1196                !!!cp (75);
1197            }            }
1198          } else {          } else {
1199            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 859  sub _get_next_token ($) { Line 1204  sub _get_next_token ($) {
1204          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1205    
1206          redo A;          redo A;
1207        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1208                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1209          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (76);
1210                                value => ''};          $self->{current_attribute}
1211                = {name => chr ($self->{next_char} + 0x0020),
1212                   value => '',
1213                   line => $self->{line}, column => $self->{column}};
1214          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1215          !!!next-input-character;          !!!next-input-character;
1216          redo A;          redo A;
1217        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1218          !!!next-input-character;          !!!next-input-character;
1219          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_char} == 0x003E and # >
1220              $self->{current_token}->{type} == START_TAG_TOKEN and              $self->{current_token}->{type} == START_TAG_TOKEN and
1221              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
1222            # permitted slash            # permitted slash
1223              !!!cp (77);
1224            #            #
1225          } else {          } else {
1226              !!!cp (78);
1227            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
1228            ## TODO: Different error type for <aa / bb> than <aa/>            ## TODO: Different error type for <aa / bb> than <aa/>
1229          }          }
1230          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1231          # next-input-character is already done          # next-input-character is already done
1232          redo A;          redo A;
1233        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1234          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1235          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1236            $self->{current_token}->{first_start_tag}            !!!cp (79);
               = not defined $self->{last_emitted_start_tag_name};  
1237            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1238          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1239            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1240            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1241                !!!cp (80);
1242              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1243              } else {
1244                ## NOTE: This state should never be reached.
1245                !!!cp (81);
1246            }            }
1247          } else {          } else {
1248            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 901  sub _get_next_token ($) { Line 1254  sub _get_next_token ($) {
1254    
1255          redo A;          redo A;
1256        } else {        } else {
1257          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          !!!cp (82);
1258                                value => ''};          $self->{current_attribute}
1259                = {name => chr ($self->{next_char}),
1260                   value => '',
1261                   line => $self->{line}, column => $self->{column}};
1262          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1263          !!!next-input-character;          !!!next-input-character;
1264          redo A;                  redo A;        
1265        }        }
1266      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
1267        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1268            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1269            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1270            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1271            $self->{next_input_character} == 0x0020) { # SP                  $self->{next_char} == 0x0020) { # SP      
1272            !!!cp (83);
1273          ## Stay in the state          ## Stay in the state
1274          !!!next-input-character;          !!!next-input-character;
1275          redo A;          redo A;
1276        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
1277            !!!cp (84);
1278          $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
1279          !!!next-input-character;          !!!next-input-character;
1280          redo A;          redo A;
1281        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1282            !!!cp (85);
1283          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1284          ## reconsume          ## reconsume
1285          redo A;          redo A;
1286        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
1287            !!!cp (86);
1288          $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
1289          !!!next-input-character;          !!!next-input-character;
1290          redo A;          redo A;
1291        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1292          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1293            $self->{current_token}->{first_start_tag}            !!!cp (87);
               = not defined $self->{last_emitted_start_tag_name};  
1294            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1295          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1296            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1297            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1298                !!!cp (88);
1299              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1300              } else {
1301                ## NOTE: This state should never be reached.
1302                !!!cp (89);
1303            }            }
1304          } else {          } else {
1305            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 947  sub _get_next_token ($) { Line 1310  sub _get_next_token ($) {
1310          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1311    
1312          redo A;          redo A;
1313        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1314          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1315          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1316            $self->{current_token}->{first_start_tag}            !!!cp (90);
               = not defined $self->{last_emitted_start_tag_name};  
1317            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1318          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1319            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1320            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1321                !!!cp (91);
1322              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1323              } else {
1324                ## NOTE: This state should never be reached.
1325                !!!cp (92);
1326            }            }
1327          } else {          } else {
1328            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 968  sub _get_next_token ($) { Line 1334  sub _get_next_token ($) {
1334    
1335          redo A;          redo A;
1336        } else {        } else {
1337          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x003D) { # =
1338              !!!cp (93);
1339              !!!parse-error (type => 'bad attribute value');
1340            } else {
1341              !!!cp (94);
1342            }
1343            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1344          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1345          !!!next-input-character;          !!!next-input-character;
1346          redo A;          redo A;
1347        }        }
1348      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
1349        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
1350          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          !!!cp (95);
1351            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1352          !!!next-input-character;          !!!next-input-character;
1353          redo A;          redo A;
1354        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1355            !!!cp (96);
1356          $self->{last_attribute_value_state} = $self->{state};          $self->{last_attribute_value_state} = $self->{state};
1357          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1358          !!!next-input-character;          !!!next-input-character;
1359          redo A;          redo A;
1360        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1361          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1362          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1363            $self->{current_token}->{first_start_tag}            !!!cp (97);
               = not defined $self->{last_emitted_start_tag_name};  
1364            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1365          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1366            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1367            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1368                !!!cp (98);
1369              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1370              } else {
1371                ## NOTE: This state should never be reached.
1372                !!!cp (99);
1373            }            }
1374          } else {          } else {
1375            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 1004  sub _get_next_token ($) { Line 1381  sub _get_next_token ($) {
1381    
1382          redo A;          redo A;
1383        } else {        } else {
1384          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (100);
1385            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1386          ## Stay in the state          ## Stay in the state
1387          !!!next-input-character;          !!!next-input-character;
1388          redo A;          redo A;
1389        }        }
1390      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1391        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
1392          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          !!!cp (101);
1393            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1394          !!!next-input-character;          !!!next-input-character;
1395          redo A;          redo A;
1396        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1397            !!!cp (102);
1398          $self->{last_attribute_value_state} = $self->{state};          $self->{last_attribute_value_state} = $self->{state};
1399          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1400          !!!next-input-character;          !!!next-input-character;
1401          redo A;          redo A;
1402        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1403          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1404          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1405            $self->{current_token}->{first_start_tag}            !!!cp (103);
               = not defined $self->{last_emitted_start_tag_name};  
1406            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1407          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1408            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1409            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1410                !!!cp (104);
1411              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1412              } else {
1413                ## NOTE: This state should never be reached.
1414                !!!cp (105);
1415            }            }
1416          } else {          } else {
1417            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 1040  sub _get_next_token ($) { Line 1423  sub _get_next_token ($) {
1423    
1424          redo A;          redo A;
1425        } else {        } else {
1426          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (106);
1427            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1428          ## Stay in the state          ## Stay in the state
1429          !!!next-input-character;          !!!next-input-character;
1430          redo A;          redo A;
1431        }        }
1432      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1433        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1434            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1435            $self->{next_input_character} == 0x000B or # HT            $self->{next_char} == 0x000B or # HT
1436            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1437            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1438            !!!cp (107);
1439          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1440          !!!next-input-character;          !!!next-input-character;
1441          redo A;          redo A;
1442        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1443            !!!cp (108);
1444          $self->{last_attribute_value_state} = $self->{state};          $self->{last_attribute_value_state} = $self->{state};
1445          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1446          !!!next-input-character;          !!!next-input-character;
1447          redo A;          redo A;
1448        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1449          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1450            $self->{current_token}->{first_start_tag}            !!!cp (109);
               = not defined $self->{last_emitted_start_tag_name};  
1451            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1452          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1453            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1454            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1455                !!!cp (110);
1456              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1457              } else {
1458                ## NOTE: This state should never be reached.
1459                !!!cp (111);
1460            }            }
1461          } else {          } else {
1462            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 1078  sub _get_next_token ($) { Line 1467  sub _get_next_token ($) {
1467          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1468    
1469          redo A;          redo A;
1470        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1471          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1472          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1473            $self->{current_token}->{first_start_tag}            !!!cp (112);
               = not defined $self->{last_emitted_start_tag_name};  
1474            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1475          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1476            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1477            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1478                !!!cp (113);
1479              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1480              } else {
1481                ## NOTE: This state should never be reached.
1482                !!!cp (114);
1483            }            }
1484          } else {          } else {
1485            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 1099  sub _get_next_token ($) { Line 1491  sub _get_next_token ($) {
1491    
1492          redo A;          redo A;
1493        } else {        } else {
1494          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ({
1495                 0x0022 => 1, # "
1496                 0x0027 => 1, # '
1497                 0x003D => 1, # =
1498                }->{$self->{next_char}}) {
1499              !!!cp (115);
1500              !!!parse-error (type => 'bad attribute value');
1501            } else {
1502              !!!cp (116);
1503            }
1504            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1505          ## Stay in the state          ## Stay in the state
1506          !!!next-input-character;          !!!next-input-character;
1507          redo A;          redo A;
1508        }        }
1509      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {
1510        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);        my $token = $self->_tokenize_attempt_to_consume_an_entity
1511              (1,
1512               $self->{last_attribute_value_state}
1513                 == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE ? 0x0022 : # "
1514               $self->{last_attribute_value_state}
1515                 == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE ? 0x0027 : # '
1516               -1);
1517    
1518        unless (defined $token) {        unless (defined $token) {
1519            !!!cp (117);
1520          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
1521        } else {        } else {
1522            !!!cp (118);
1523          $self->{current_attribute}->{value} .= $token->{data};          $self->{current_attribute}->{value} .= $token->{data};
1524            $self->{current_attribute}->{has_reference} = $token->{has_reference};
1525          ## 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"
1526        }        }
1527    
1528        $self->{state} = $self->{last_attribute_value_state};        $self->{state} = $self->{last_attribute_value_state};
1529        # next-input-character is already done        # next-input-character is already done
1530        redo A;        redo A;
1531        } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1532          if ($self->{next_char} == 0x0009 or # HT
1533              $self->{next_char} == 0x000A or # LF
1534              $self->{next_char} == 0x000B or # VT
1535              $self->{next_char} == 0x000C or # FF
1536              $self->{next_char} == 0x0020) { # SP
1537            !!!cp (118);
1538            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1539            !!!next-input-character;
1540            redo A;
1541          } elsif ($self->{next_char} == 0x003E) { # >
1542            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1543              !!!cp (119);
1544              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1545            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1546              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1547              if ($self->{current_token}->{attributes}) {
1548                !!!cp (120);
1549                !!!parse-error (type => 'end tag attribute');
1550              } else {
1551                ## NOTE: This state should never be reached.
1552                !!!cp (121);
1553              }
1554            } else {
1555              die "$0: $self->{current_token}->{type}: Unknown token type";
1556            }
1557            $self->{state} = DATA_STATE;
1558            !!!next-input-character;
1559    
1560            !!!emit ($self->{current_token}); # start tag or end tag
1561    
1562            redo A;
1563          } elsif ($self->{next_char} == 0x002F) { # /
1564            !!!next-input-character;
1565            if ($self->{next_char} == 0x003E and # >
1566                $self->{current_token}->{type} == START_TAG_TOKEN and
1567                $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
1568              # permitted slash
1569              !!!cp (122);
1570              #
1571            } else {
1572              !!!cp (123);
1573              !!!parse-error (type => 'nestc');
1574            }
1575            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1576            # next-input-character is already done
1577            redo A;
1578          } else {
1579            !!!cp (124);
1580            !!!parse-error (type => 'no space between attributes');
1581            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1582            ## reconsume
1583            redo A;
1584          }
1585      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1586        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1587                
1588        my $token = {type => COMMENT_TOKEN, data => ''};        ## NOTE: Set by the previous state
1589          #my $token = {type => COMMENT_TOKEN, data => ''};
1590    
1591        BC: {        BC: {
1592          if ($self->{next_input_character} == 0x003E) { # >          if ($self->{next_char} == 0x003E) { # >
1593              !!!cp (124);
1594            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1595            !!!next-input-character;            !!!next-input-character;
1596    
1597            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1598    
1599            redo A;            redo A;
1600          } elsif ($self->{next_input_character} == -1) {          } elsif ($self->{next_char} == -1) {
1601              !!!cp (125);
1602            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1603            ## reconsume            ## reconsume
1604    
1605            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1606    
1607            redo A;            redo A;
1608          } else {          } else {
1609            $token->{data} .= chr ($self->{next_input_character});            !!!cp (126);
1610              $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1611            !!!next-input-character;            !!!next-input-character;
1612            redo BC;            redo BC;
1613          }          }
1614        } # BC        } # BC
1615    
1616          die "$0: _get_next_token: unexpected case [BC]";
1617      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1618        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1619    
1620          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1);
1621    
1622        my @next_char;        my @next_char;
1623        push @next_char, $self->{next_input_character};        push @next_char, $self->{next_char};
1624                
1625        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1626          !!!next-input-character;          !!!next-input-character;
1627          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
1628          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_char} == 0x002D) { # -
1629            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};            !!!cp (127);
1630              $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1631                                        line => $l, column => $c,
1632                                       };
1633            $self->{state} = COMMENT_START_STATE;            $self->{state} = COMMENT_START_STATE;
1634            !!!next-input-character;            !!!next-input-character;
1635            redo A;            redo A;
1636            } else {
1637              !!!cp (128);
1638          }          }
1639        } elsif ($self->{next_input_character} == 0x0044 or # D        } elsif ($self->{next_char} == 0x0044 or # D
1640                 $self->{next_input_character} == 0x0064) { # d                 $self->{next_char} == 0x0064) { # d
1641          !!!next-input-character;          !!!next-input-character;
1642          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
1643          if ($self->{next_input_character} == 0x004F or # O          if ($self->{next_char} == 0x004F or # O
1644              $self->{next_input_character} == 0x006F) { # o              $self->{next_char} == 0x006F) { # o
1645            !!!next-input-character;            !!!next-input-character;
1646            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_char};
1647            if ($self->{next_input_character} == 0x0043 or # C            if ($self->{next_char} == 0x0043 or # C
1648                $self->{next_input_character} == 0x0063) { # c                $self->{next_char} == 0x0063) { # c
1649              !!!next-input-character;              !!!next-input-character;
1650              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_char};
1651              if ($self->{next_input_character} == 0x0054 or # T              if ($self->{next_char} == 0x0054 or # T
1652                  $self->{next_input_character} == 0x0074) { # t                  $self->{next_char} == 0x0074) { # t
1653                !!!next-input-character;                !!!next-input-character;
1654                push @next_char, $self->{next_input_character};                push @next_char, $self->{next_char};
1655                if ($self->{next_input_character} == 0x0059 or # Y                if ($self->{next_char} == 0x0059 or # Y
1656                    $self->{next_input_character} == 0x0079) { # y                    $self->{next_char} == 0x0079) { # y
1657                  !!!next-input-character;                  !!!next-input-character;
1658                  push @next_char, $self->{next_input_character};                  push @next_char, $self->{next_char};
1659                  if ($self->{next_input_character} == 0x0050 or # P                  if ($self->{next_char} == 0x0050 or # P
1660                      $self->{next_input_character} == 0x0070) { # p                      $self->{next_char} == 0x0070) { # p
1661                    !!!next-input-character;                    !!!next-input-character;
1662                    push @next_char, $self->{next_input_character};                    push @next_char, $self->{next_char};
1663                    if ($self->{next_input_character} == 0x0045 or # E                    if ($self->{next_char} == 0x0045 or # E
1664                        $self->{next_input_character} == 0x0065) { # e                        $self->{next_char} == 0x0065) { # e
1665                      ## ISSUE: What a stupid code this is!                      !!!cp (129);
1666                        ## TODO: What a stupid code this is!
1667                      $self->{state} = DOCTYPE_STATE;                      $self->{state} = DOCTYPE_STATE;
1668                        $self->{current_token} = {type => DOCTYPE_TOKEN,
1669                                                  quirks => 1,
1670                                                  line => $l, column => $c,
1671                                                 };
1672                      !!!next-input-character;                      !!!next-input-character;
1673                      redo A;                      redo A;
1674                      } else {
1675                        !!!cp (130);
1676                    }                    }
1677                    } else {
1678                      !!!cp (131);
1679                  }                  }
1680                  } else {
1681                    !!!cp (132);
1682                }                }
1683                } else {
1684                  !!!cp (133);
1685              }              }
1686              } else {
1687                !!!cp (134);
1688            }            }
1689            } else {
1690              !!!cp (135);
1691          }          }
1692          } else {
1693            !!!cp (136);
1694        }        }
1695    
1696        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'bogus comment');
1697        $self->{next_input_character} = shift @next_char;        $self->{next_char} = shift @next_char;
1698        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
1699        $self->{state} = BOGUS_COMMENT_STATE;        $self->{state} = BOGUS_COMMENT_STATE;
1700          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1701                                    line => $l, column => $c,
1702                                   };
1703        redo A;        redo A;
1704                
1705        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
1706        ## 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?
1707      } elsif ($self->{state} == COMMENT_START_STATE) {      } elsif ($self->{state} == COMMENT_START_STATE) {
1708        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1709            !!!cp (137);
1710          $self->{state} = COMMENT_START_DASH_STATE;          $self->{state} = COMMENT_START_DASH_STATE;
1711          !!!next-input-character;          !!!next-input-character;
1712          redo A;          redo A;
1713        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1714            !!!cp (138);
1715          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
1716          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1717          !!!next-input-character;          !!!next-input-character;
# Line 1217  sub _get_next_token ($) { Line 1719  sub _get_next_token ($) {
1719          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1720    
1721          redo A;          redo A;
1722        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1723            !!!cp (139);
1724          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1725          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1726          ## reconsume          ## reconsume
# Line 1226  sub _get_next_token ($) { Line 1729  sub _get_next_token ($) {
1729    
1730          redo A;          redo A;
1731        } else {        } else {
1732            !!!cp (140);
1733          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
1734              .= chr ($self->{next_input_character});              .= chr ($self->{next_char});
1735          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
1736          !!!next-input-character;          !!!next-input-character;
1737          redo A;          redo A;
1738        }        }
1739      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
1740        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1741            !!!cp (141);
1742          $self->{state} = COMMENT_END_STATE;          $self->{state} = COMMENT_END_STATE;
1743          !!!next-input-character;          !!!next-input-character;
1744          redo A;          redo A;
1745        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1746            !!!cp (142);
1747          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
1748          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1749          !!!next-input-character;          !!!next-input-character;
# Line 1245  sub _get_next_token ($) { Line 1751  sub _get_next_token ($) {
1751          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1752    
1753          redo A;          redo A;
1754        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1755            !!!cp (143);
1756          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1757          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1758          ## reconsume          ## reconsume
# Line 1254  sub _get_next_token ($) { Line 1761  sub _get_next_token ($) {
1761    
1762          redo A;          redo A;
1763        } else {        } else {
1764            !!!cp (144);
1765          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
1766              .= '-' . chr ($self->{next_input_character});              .= '-' . chr ($self->{next_char});
1767          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
1768          !!!next-input-character;          !!!next-input-character;
1769          redo A;          redo A;
1770        }        }
1771      } elsif ($self->{state} == COMMENT_STATE) {      } elsif ($self->{state} == COMMENT_STATE) {
1772        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1773            !!!cp (145);
1774          $self->{state} = COMMENT_END_DASH_STATE;          $self->{state} = COMMENT_END_DASH_STATE;
1775          !!!next-input-character;          !!!next-input-character;
1776          redo A;          redo A;
1777        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1778            !!!cp (146);
1779          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1780          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1781          ## reconsume          ## reconsume
# Line 1274  sub _get_next_token ($) { Line 1784  sub _get_next_token ($) {
1784    
1785          redo A;          redo A;
1786        } else {        } else {
1787          $self->{current_token}->{data} .= chr ($self->{next_input_character}); # comment          !!!cp (147);
1788            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1789          ## Stay in the state          ## Stay in the state
1790          !!!next-input-character;          !!!next-input-character;
1791          redo A;          redo A;
1792        }        }
1793      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
1794        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1795            !!!cp (148);
1796          $self->{state} = COMMENT_END_STATE;          $self->{state} = COMMENT_END_STATE;
1797          !!!next-input-character;          !!!next-input-character;
1798          redo A;          redo A;
1799        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1800            !!!cp (149);
1801          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1802          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1803          ## reconsume          ## reconsume
# Line 1293  sub _get_next_token ($) { Line 1806  sub _get_next_token ($) {
1806    
1807          redo A;          redo A;
1808        } else {        } else {
1809          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          !!!cp (150);
1810            $self->{current_token}->{data} .= '-' . chr ($self->{next_char}); # comment
1811          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
1812          !!!next-input-character;          !!!next-input-character;
1813          redo A;          redo A;
1814        }        }
1815      } elsif ($self->{state} == COMMENT_END_STATE) {      } elsif ($self->{state} == COMMENT_END_STATE) {
1816        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
1817            !!!cp (151);
1818          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1819          !!!next-input-character;          !!!next-input-character;
1820    
1821          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1822    
1823          redo A;          redo A;
1824        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
1825          !!!parse-error (type => 'dash in comment');          !!!cp (152);
1826            !!!parse-error (type => 'dash in comment',
1827                            line => $self->{line_prev},
1828                            column => $self->{column_prev});
1829          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
1830          ## Stay in the state          ## Stay in the state
1831          !!!next-input-character;          !!!next-input-character;
1832          redo A;          redo A;
1833        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1834            !!!cp (153);
1835          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1836          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1837          ## reconsume          ## reconsume
# Line 1321  sub _get_next_token ($) { Line 1840  sub _get_next_token ($) {
1840    
1841          redo A;          redo A;
1842        } else {        } else {
1843          !!!parse-error (type => 'dash in comment');          !!!cp (154);
1844          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          !!!parse-error (type => 'dash in comment',
1845                            line => $self->{line_prev},
1846                            column => $self->{column_prev});
1847            $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
1848          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
1849          !!!next-input-character;          !!!next-input-character;
1850          redo A;          redo A;
1851        }        }
1852      } elsif ($self->{state} == DOCTYPE_STATE) {      } elsif ($self->{state} == DOCTYPE_STATE) {
1853        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1854            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1855            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1856            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1857            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1858            !!!cp (155);
1859          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1860          !!!next-input-character;          !!!next-input-character;
1861          redo A;          redo A;
1862        } else {        } else {
1863            !!!cp (156);
1864          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
1865          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1866          ## reconsume          ## reconsume
1867          redo A;          redo A;
1868        }        }
1869      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
1870        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1871            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1872            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1873            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1874            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1875            !!!cp (157);
1876          ## Stay in the state          ## Stay in the state
1877          !!!next-input-character;          !!!next-input-character;
1878          redo A;          redo A;
1879        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1880            !!!cp (158);
1881          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1882          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1883          !!!next-input-character;          !!!next-input-character;
1884    
1885          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
1886    
1887          redo A;          redo A;
1888        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1889            !!!cp (159);
1890          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1891          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1892          ## reconsume          ## reconsume
1893    
1894          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
1895    
1896          redo A;          redo A;
1897        } else {        } else {
1898          $self->{current_token}          !!!cp (160);
1899              = {type => DOCTYPE_TOKEN,          $self->{current_token}->{name} = chr $self->{next_char};
1900                 name => chr ($self->{next_input_character}),          delete $self->{current_token}->{quirks};
                correct => 1};  
1901  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
1902          $self->{state} = DOCTYPE_NAME_STATE;          $self->{state} = DOCTYPE_NAME_STATE;
1903          !!!next-input-character;          !!!next-input-character;
# Line 1379  sub _get_next_token ($) { Line 1905  sub _get_next_token ($) {
1905        }        }
1906      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
1907  ## ISSUE: Redundant "First," in the spec.  ## ISSUE: Redundant "First," in the spec.
1908        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1909            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1910            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1911            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1912            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1913            !!!cp (161);
1914          $self->{state} = AFTER_DOCTYPE_NAME_STATE;          $self->{state} = AFTER_DOCTYPE_NAME_STATE;
1915          !!!next-input-character;          !!!next-input-character;
1916          redo A;          redo A;
1917        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1918            !!!cp (162);
1919          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1920          !!!next-input-character;          !!!next-input-character;
1921    
1922          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1923    
1924          redo A;          redo A;
1925        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1926            !!!cp (163);
1927          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1928          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1929          ## reconsume          ## reconsume
1930    
1931          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1932          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1933    
1934          redo A;          redo A;
1935        } else {        } else {
1936            !!!cp (164);
1937          $self->{current_token}->{name}          $self->{current_token}->{name}
1938            .= chr ($self->{next_input_character}); # DOCTYPE            .= chr ($self->{next_char}); # DOCTYPE
1939          ## Stay in the state          ## Stay in the state
1940          !!!next-input-character;          !!!next-input-character;
1941          redo A;          redo A;
1942        }        }
1943      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
1944        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1945            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1946            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1947            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1948            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1949            !!!cp (165);
1950          ## Stay in the state          ## Stay in the state
1951          !!!next-input-character;          !!!next-input-character;
1952          redo A;          redo A;
1953        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1954            !!!cp (166);
1955          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1956          !!!next-input-character;          !!!next-input-character;
1957    
1958          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1959    
1960          redo A;          redo A;
1961        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1962            !!!cp (167);
1963          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1964          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1965          ## reconsume          ## reconsume
1966    
1967          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1968          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1969    
1970          redo A;          redo A;
1971        } elsif ($self->{next_input_character} == 0x0050 or # P        } elsif ($self->{next_char} == 0x0050 or # P
1972                 $self->{next_input_character} == 0x0070) { # p                 $self->{next_char} == 0x0070) { # p
1973          !!!next-input-character;          !!!next-input-character;
1974          if ($self->{next_input_character} == 0x0055 or # U          if ($self->{next_char} == 0x0055 or # U
1975              $self->{next_input_character} == 0x0075) { # u              $self->{next_char} == 0x0075) { # u
1976            !!!next-input-character;            !!!next-input-character;
1977            if ($self->{next_input_character} == 0x0042 or # B            if ($self->{next_char} == 0x0042 or # B
1978                $self->{next_input_character} == 0x0062) { # b                $self->{next_char} == 0x0062) { # b
1979              !!!next-input-character;              !!!next-input-character;
1980              if ($self->{next_input_character} == 0x004C or # L              if ($self->{next_char} == 0x004C or # L
1981                  $self->{next_input_character} == 0x006C) { # l                  $self->{next_char} == 0x006C) { # l
1982                !!!next-input-character;                !!!next-input-character;
1983                if ($self->{next_input_character} == 0x0049 or # I                if ($self->{next_char} == 0x0049 or # I
1984                    $self->{next_input_character} == 0x0069) { # i                    $self->{next_char} == 0x0069) { # i
1985                  !!!next-input-character;                  !!!next-input-character;
1986                  if ($self->{next_input_character} == 0x0043 or # C                  if ($self->{next_char} == 0x0043 or # C
1987                      $self->{next_input_character} == 0x0063) { # c                      $self->{next_char} == 0x0063) { # c
1988                      !!!cp (168);
1989                    $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;                    $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1990                    !!!next-input-character;                    !!!next-input-character;
1991                    redo A;                    redo A;
1992                    } else {
1993                      !!!cp (169);
1994                  }                  }
1995                  } else {
1996                    !!!cp (170);
1997                }                }
1998                } else {
1999                  !!!cp (171);
2000              }              }
2001              } else {
2002                !!!cp (172);
2003            }            }
2004            } else {
2005              !!!cp (173);
2006          }          }
2007    
2008          #          #
2009        } elsif ($self->{next_input_character} == 0x0053 or # S        } elsif ($self->{next_char} == 0x0053 or # S
2010                 $self->{next_input_character} == 0x0073) { # s                 $self->{next_char} == 0x0073) { # s
2011          !!!next-input-character;          !!!next-input-character;
2012          if ($self->{next_input_character} == 0x0059 or # Y          if ($self->{next_char} == 0x0059 or # Y
2013              $self->{next_input_character} == 0x0079) { # y              $self->{next_char} == 0x0079) { # y
2014            !!!next-input-character;            !!!next-input-character;
2015            if ($self->{next_input_character} == 0x0053 or # S            if ($self->{next_char} == 0x0053 or # S
2016                $self->{next_input_character} == 0x0073) { # s                $self->{next_char} == 0x0073) { # s
2017              !!!next-input-character;              !!!next-input-character;
2018              if ($self->{next_input_character} == 0x0054 or # T              if ($self->{next_char} == 0x0054 or # T
2019                  $self->{next_input_character} == 0x0074) { # t                  $self->{next_char} == 0x0074) { # t
2020                !!!next-input-character;                !!!next-input-character;
2021                if ($self->{next_input_character} == 0x0045 or # E                if ($self->{next_char} == 0x0045 or # E
2022                    $self->{next_input_character} == 0x0065) { # e                    $self->{next_char} == 0x0065) { # e
2023                  !!!next-input-character;                  !!!next-input-character;
2024                  if ($self->{next_input_character} == 0x004D or # M                  if ($self->{next_char} == 0x004D or # M
2025                      $self->{next_input_character} == 0x006D) { # m                      $self->{next_char} == 0x006D) { # m
2026                      !!!cp (174);
2027                    $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;                    $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2028                    !!!next-input-character;                    !!!next-input-character;
2029                    redo A;                    redo A;
2030                    } else {
2031                      !!!cp (175);
2032                  }                  }
2033                  } else {
2034                    !!!cp (176);
2035                }                }
2036                } else {
2037                  !!!cp (177);
2038              }              }
2039              } else {
2040                !!!cp (178);
2041            }            }
2042            } else {
2043              !!!cp (179);
2044          }          }
2045    
2046          #          #
2047        } else {        } else {
2048            !!!cp (180);
2049          !!!next-input-character;          !!!next-input-character;
2050          #          #
2051        }        }
2052    
2053        !!!parse-error (type => 'string after DOCTYPE name');        !!!parse-error (type => 'string after DOCTYPE name');
2054          $self->{current_token}->{quirks} = 1;
2055    
2056        $self->{state} = BOGUS_DOCTYPE_STATE;        $self->{state} = BOGUS_DOCTYPE_STATE;
2057        # next-input-character is already done        # next-input-character is already done
2058        redo A;        redo A;
# Line 1502  sub _get_next_token ($) { Line 2060  sub _get_next_token ($) {
2060        if ({        if ({
2061              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2062              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2063            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2064            !!!cp (181);
2065          ## Stay in the state          ## Stay in the state
2066          !!!next-input-character;          !!!next-input-character;
2067          redo A;          redo A;
2068        } elsif ($self->{next_input_character} eq 0x0022) { # "        } elsif ($self->{next_char} eq 0x0022) { # "
2069            !!!cp (182);
2070          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2071          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
2072          !!!next-input-character;          !!!next-input-character;
2073          redo A;          redo A;
2074        } elsif ($self->{next_input_character} eq 0x0027) { # '        } elsif ($self->{next_char} eq 0x0027) { # '
2075            !!!cp (183);
2076          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2077          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
2078          !!!next-input-character;          !!!next-input-character;
2079          redo A;          redo A;
2080        } elsif ($self->{next_input_character} eq 0x003E) { # >        } elsif ($self->{next_char} eq 0x003E) { # >
2081            !!!cp (184);
2082          !!!parse-error (type => 'no PUBLIC literal');          !!!parse-error (type => 'no PUBLIC literal');
2083    
2084          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2085          !!!next-input-character;          !!!next-input-character;
2086    
2087          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2088          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2089    
2090          redo A;          redo A;
2091        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2092            !!!cp (185);
2093          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2094    
2095          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2096          ## reconsume          ## reconsume
2097    
2098          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2099          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2100    
2101          redo A;          redo A;
2102        } else {        } else {
2103            !!!cp (186);
2104          !!!parse-error (type => 'string after PUBLIC');          !!!parse-error (type => 'string after PUBLIC');
2105            $self->{current_token}->{quirks} = 1;
2106    
2107          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2108          !!!next-input-character;          !!!next-input-character;
2109          redo A;          redo A;
2110        }        }
2111      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2112        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
2113            !!!cp (187);
2114          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2115          !!!next-input-character;          !!!next-input-character;
2116          redo A;          redo A;
2117        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2118            !!!cp (188);
2119            !!!parse-error (type => 'unclosed PUBLIC literal');
2120    
2121            $self->{state} = DATA_STATE;
2122            !!!next-input-character;
2123    
2124            $self->{current_token}->{quirks} = 1;
2125            !!!emit ($self->{current_token}); # DOCTYPE
2126    
2127            redo A;
2128          } elsif ($self->{next_char} == -1) {
2129            !!!cp (189);
2130          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2131    
2132          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2133          ## reconsume          ## reconsume
2134    
2135          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2136          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2137    
2138          redo A;          redo A;
2139        } else {        } else {
2140            !!!cp (190);
2141          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
2142              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2143          ## Stay in the state          ## Stay in the state
2144          !!!next-input-character;          !!!next-input-character;
2145          redo A;          redo A;
2146        }        }
2147      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
2148        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
2149            !!!cp (191);
2150          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2151          !!!next-input-character;          !!!next-input-character;
2152          redo A;          redo A;
2153        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2154            !!!cp (192);
2155            !!!parse-error (type => 'unclosed PUBLIC literal');
2156    
2157            $self->{state} = DATA_STATE;
2158            !!!next-input-character;
2159    
2160            $self->{current_token}->{quirks} = 1;
2161            !!!emit ($self->{current_token}); # DOCTYPE
2162    
2163            redo A;
2164          } elsif ($self->{next_char} == -1) {
2165            !!!cp (193);
2166          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2167    
2168          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2169          ## reconsume          ## reconsume
2170    
2171          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2172          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2173    
2174          redo A;          redo A;
2175        } else {        } else {
2176            !!!cp (194);
2177          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
2178              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2179          ## Stay in the state          ## Stay in the state
2180          !!!next-input-character;          !!!next-input-character;
2181          redo A;          redo A;
# Line 1590  sub _get_next_token ($) { Line 2184  sub _get_next_token ($) {
2184        if ({        if ({
2185              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2186              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2187            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2188            !!!cp (195);
2189          ## Stay in the state          ## Stay in the state
2190          !!!next-input-character;          !!!next-input-character;
2191          redo A;          redo A;
2192        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
2193            !!!cp (196);
2194          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2195          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2196          !!!next-input-character;          !!!next-input-character;
2197          redo A;          redo A;
2198        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
2199            !!!cp (197);
2200          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2201          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2202          !!!next-input-character;          !!!next-input-character;
2203          redo A;          redo A;
2204        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2205            !!!cp (198);
2206          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2207          !!!next-input-character;          !!!next-input-character;
2208    
2209          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2210    
2211          redo A;          redo A;
2212        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2213            !!!cp (199);
2214          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2215    
2216          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2217          ## reconsume          ## reconsume
2218    
2219          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2220          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2221    
2222          redo A;          redo A;
2223        } else {        } else {
2224            !!!cp (200);
2225          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after PUBLIC literal');
2226            $self->{current_token}->{quirks} = 1;
2227    
2228          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2229          !!!next-input-character;          !!!next-input-character;
2230          redo A;          redo A;
# Line 1631  sub _get_next_token ($) { Line 2233  sub _get_next_token ($) {
2233        if ({        if ({
2234              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2235              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2236            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2237            !!!cp (201);
2238          ## Stay in the state          ## Stay in the state
2239          !!!next-input-character;          !!!next-input-character;
2240          redo A;          redo A;
2241        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
2242            !!!cp (202);
2243          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2244          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2245          !!!next-input-character;          !!!next-input-character;
2246          redo A;          redo A;
2247        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
2248            !!!cp (203);
2249          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2250          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2251          !!!next-input-character;          !!!next-input-character;
2252          redo A;          redo A;
2253        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2254            !!!cp (204);
2255          !!!parse-error (type => 'no SYSTEM literal');          !!!parse-error (type => 'no SYSTEM literal');
2256          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2257          !!!next-input-character;          !!!next-input-character;
2258    
2259          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2260          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2261    
2262          redo A;          redo A;
2263        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2264            !!!cp (205);
2265          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2266    
2267          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2268          ## reconsume          ## reconsume
2269    
2270          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2271          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2272    
2273          redo A;          redo A;
2274        } else {        } else {
2275            !!!cp (206);
2276          !!!parse-error (type => 'string after SYSTEM');          !!!parse-error (type => 'string after SYSTEM');
2277            $self->{current_token}->{quirks} = 1;
2278    
2279          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2280          !!!next-input-character;          !!!next-input-character;
2281          redo A;          redo A;
2282        }        }
2283      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2284        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
2285            !!!cp (207);
2286          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2287          !!!next-input-character;          !!!next-input-character;
2288          redo A;          redo A;
2289        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2290            !!!cp (208);
2291            !!!parse-error (type => 'unclosed PUBLIC literal');
2292    
2293            $self->{state} = DATA_STATE;
2294            !!!next-input-character;
2295    
2296            $self->{current_token}->{quirks} = 1;
2297            !!!emit ($self->{current_token}); # DOCTYPE
2298    
2299            redo A;
2300          } elsif ($self->{next_char} == -1) {
2301            !!!cp (209);
2302          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2303    
2304          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2305          ## reconsume          ## reconsume
2306    
2307          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2308          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2309    
2310          redo A;          redo A;
2311        } else {        } else {
2312            !!!cp (210);
2313          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2314              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2315          ## Stay in the state          ## Stay in the state
2316          !!!next-input-character;          !!!next-input-character;
2317          redo A;          redo A;
2318        }        }
2319      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
2320        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
2321            !!!cp (211);
2322          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2323          !!!next-input-character;          !!!next-input-character;
2324          redo A;          redo A;
2325        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2326            !!!cp (212);
2327            !!!parse-error (type => 'unclosed PUBLIC literal');
2328    
2329            $self->{state} = DATA_STATE;
2330            !!!next-input-character;
2331    
2332            $self->{current_token}->{quirks} = 1;
2333            !!!emit ($self->{current_token}); # DOCTYPE
2334    
2335            redo A;
2336          } elsif ($self->{next_char} == -1) {
2337            !!!cp (213);
2338          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2339    
2340          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2341          ## reconsume          ## reconsume
2342    
2343          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2344          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2345    
2346          redo A;          redo A;
2347        } else {        } else {
2348            !!!cp (214);
2349          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2350              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2351          ## Stay in the state          ## Stay in the state
2352          !!!next-input-character;          !!!next-input-character;
2353          redo A;          redo A;
# Line 1718  sub _get_next_token ($) { Line 2356  sub _get_next_token ($) {
2356        if ({        if ({
2357              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2358              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2359            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2360            !!!cp (215);
2361          ## Stay in the state          ## Stay in the state
2362          !!!next-input-character;          !!!next-input-character;
2363          redo A;          redo A;
2364        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2365            !!!cp (216);
2366          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2367          !!!next-input-character;          !!!next-input-character;
2368    
2369          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2370    
2371          redo A;          redo A;
2372        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2373            !!!cp (217);
2374          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2375    
2376          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2377          ## reconsume          ## reconsume
2378    
2379          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2380          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2381    
2382          redo A;          redo A;
2383        } else {        } else {
2384            !!!cp (218);
2385          !!!parse-error (type => 'string after SYSTEM literal');          !!!parse-error (type => 'string after SYSTEM literal');
2386            #$self->{current_token}->{quirks} = 1;
2387    
2388          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2389          !!!next-input-character;          !!!next-input-character;
2390          redo A;          redo A;
2391        }        }
2392      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
2393        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2394            !!!cp (219);
2395          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2396          !!!next-input-character;          !!!next-input-character;
2397    
         delete $self->{current_token}->{correct};  
2398          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2399    
2400          redo A;          redo A;
2401        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2402            !!!cp (220);
2403          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2404          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2405          ## reconsume          ## reconsume
2406    
         delete $self->{current_token}->{correct};  
2407          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2408    
2409          redo A;          redo A;
2410        } else {        } else {
2411            !!!cp (221);
2412          ## Stay in the state          ## Stay in the state
2413          !!!next-input-character;          !!!next-input-character;
2414          redo A;          redo A;
# Line 1776  sub _get_next_token ($) { Line 2421  sub _get_next_token ($) {
2421    die "$0: _get_next_token: unexpected case";    die "$0: _get_next_token: unexpected case";
2422  } # _get_next_token  } # _get_next_token
2423    
2424  sub _tokenize_attempt_to_consume_an_entity ($$) {  sub _tokenize_attempt_to_consume_an_entity ($$$) {
2425    my ($self, $in_attr) = @_;    my ($self, $in_attr, $additional) = @_;
2426    
2427      my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
2428    
2429    if ({    if ({
2430         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
2431         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
2432        }->{$self->{next_input_character}}) {         $additional => 1,
2433          }->{$self->{next_char}}) {
2434        !!!cp (1001);
2435      ## Don't consume      ## Don't consume
2436      ## No error      ## No error
2437      return undef;      return undef;
2438    } elsif ($self->{next_input_character} == 0x0023) { # #    } elsif ($self->{next_char} == 0x0023) { # #
2439      !!!next-input-character;      !!!next-input-character;
2440      if ($self->{next_input_character} == 0x0078 or # x      if ($self->{next_char} == 0x0078 or # x
2441          $self->{next_input_character} == 0x0058) { # X          $self->{next_char} == 0x0058) { # X
2442        my $code;        my $code;
2443        X: {        X: {
2444          my $x_char = $self->{next_input_character};          my $x_char = $self->{next_char};
2445          !!!next-input-character;          !!!next-input-character;
2446          if (0x0030 <= $self->{next_input_character} and          if (0x0030 <= $self->{next_char} and
2447              $self->{next_input_character} <= 0x0039) { # 0..9              $self->{next_char} <= 0x0039) { # 0..9
2448              !!!cp (1002);
2449            $code ||= 0;            $code ||= 0;
2450            $code *= 0x10;            $code *= 0x10;
2451            $code += $self->{next_input_character} - 0x0030;            $code += $self->{next_char} - 0x0030;
2452            redo X;            redo X;
2453          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
2454                   $self->{next_input_character} <= 0x0066) { # a..f                   $self->{next_char} <= 0x0066) { # a..f
2455              !!!cp (1003);
2456            $code ||= 0;            $code ||= 0;
2457            $code *= 0x10;            $code *= 0x10;
2458            $code += $self->{next_input_character} - 0x0060 + 9;            $code += $self->{next_char} - 0x0060 + 9;
2459            redo X;            redo X;
2460          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
2461                   $self->{next_input_character} <= 0x0046) { # A..F                   $self->{next_char} <= 0x0046) { # A..F
2462              !!!cp (1004);
2463            $code ||= 0;            $code ||= 0;
2464            $code *= 0x10;            $code *= 0x10;
2465            $code += $self->{next_input_character} - 0x0040 + 9;            $code += $self->{next_char} - 0x0040 + 9;
2466            redo X;            redo X;
2467          } elsif (not defined $code) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
2468            !!!parse-error (type => 'bare hcro');            !!!cp (1005);
2469            !!!back-next-input-character ($x_char, $self->{next_input_character});            !!!parse-error (type => 'bare hcro', line => $l, column => $c);
2470            $self->{next_input_character} = 0x0023; # #            !!!back-next-input-character ($x_char, $self->{next_char});
2471              $self->{next_char} = 0x0023; # #
2472            return undef;            return undef;
2473          } elsif ($self->{next_input_character} == 0x003B) { # ;          } elsif ($self->{next_char} == 0x003B) { # ;
2474              !!!cp (1006);
2475            !!!next-input-character;            !!!next-input-character;
2476          } else {          } else {
2477            !!!parse-error (type => 'no refc');            !!!cp (1007);
2478              !!!parse-error (type => 'no refc', line => $l, column => $c);
2479          }          }
2480    
2481          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2482            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);            !!!cp (1008);
2483              !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2484            $code = 0xFFFD;            $code = 0xFFFD;
2485          } elsif ($code > 0x10FFFF) {          } elsif ($code > 0x10FFFF) {
2486            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);            !!!cp (1009);
2487              !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2488            $code = 0xFFFD;            $code = 0xFFFD;
2489          } elsif ($code == 0x000D) {          } elsif ($code == 0x000D) {
2490            !!!parse-error (type => 'CR character reference');            !!!cp (1010);
2491              !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2492            $code = 0x000A;            $code = 0x000A;
2493          } elsif (0x80 <= $code and $code <= 0x9F) {          } elsif (0x80 <= $code and $code <= 0x9F) {
2494            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);            !!!cp (1011);
2495              !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2496            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
2497          }          }
2498    
2499          return {type => CHARACTER_TOKEN, data => chr $code};          return {type => CHARACTER_TOKEN, data => chr $code,
2500                    has_reference => 1,
2501                    line => $l, column => $c,
2502                   };
2503        } # X        } # X
2504      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_char} and
2505               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_char} <= 0x0039) { # 0..9
2506        my $code = $self->{next_input_character} - 0x0030;        my $code = $self->{next_char} - 0x0030;
2507        !!!next-input-character;        !!!next-input-character;
2508                
2509        while (0x0030 <= $self->{next_input_character} and        while (0x0030 <= $self->{next_char} and
2510                  $self->{next_input_character} <= 0x0039) { # 0..9                  $self->{next_char} <= 0x0039) { # 0..9
2511            !!!cp (1012);
2512          $code *= 10;          $code *= 10;
2513          $code += $self->{next_input_character} - 0x0030;          $code += $self->{next_char} - 0x0030;
2514                    
2515          !!!next-input-character;          !!!next-input-character;
2516        }        }
2517    
2518        if ($self->{next_input_character} == 0x003B) { # ;        if ($self->{next_char} == 0x003B) { # ;
2519            !!!cp (1013);
2520          !!!next-input-character;          !!!next-input-character;
2521        } else {        } else {
2522          !!!parse-error (type => 'no refc');          !!!cp (1014);
2523            !!!parse-error (type => 'no refc', line => $l, column => $c);
2524        }        }
2525    
2526        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2527          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);          !!!cp (1015);
2528            !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2529          $code = 0xFFFD;          $code = 0xFFFD;
2530        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
2531          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);          !!!cp (1016);
2532            !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2533          $code = 0xFFFD;          $code = 0xFFFD;
2534        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
2535          !!!parse-error (type => 'CR character reference');          !!!cp (1017);
2536            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2537          $code = 0x000A;          $code = 0x000A;
2538        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
2539          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!cp (1018);
2540            !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2541          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
2542        }        }
2543                
2544        return {type => CHARACTER_TOKEN, data => chr $code};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,
2545                  line => $l, column => $c,
2546                 };
2547      } else {      } else {
2548        !!!parse-error (type => 'bare nero');        !!!cp (1019);
2549        !!!back-next-input-character ($self->{next_input_character});        !!!parse-error (type => 'bare nero', line => $l, column => $c);
2550        $self->{next_input_character} = 0x0023; # #        !!!back-next-input-character ($self->{next_char});
2551          $self->{next_char} = 0x0023; # #
2552        return undef;        return undef;
2553      }      }
2554    } elsif ((0x0041 <= $self->{next_input_character} and    } elsif ((0x0041 <= $self->{next_char} and
2555              $self->{next_input_character} <= 0x005A) or              $self->{next_char} <= 0x005A) or
2556             (0x0061 <= $self->{next_input_character} and             (0x0061 <= $self->{next_char} and
2557              $self->{next_input_character} <= 0x007A)) {              $self->{next_char} <= 0x007A)) {
2558      my $entity_name = chr $self->{next_input_character};      my $entity_name = chr $self->{next_char};
2559      !!!next-input-character;      !!!next-input-character;
2560    
2561      my $value = $entity_name;      my $value = $entity_name;
# Line 1893  sub _tokenize_attempt_to_consume_an_enti Line 2565  sub _tokenize_attempt_to_consume_an_enti
2565    
2566      while (length $entity_name < 10 and      while (length $entity_name < 10 and
2567             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
2568             ((0x0041 <= $self->{next_input_character} and # a             ((0x0041 <= $self->{next_char} and # a
2569               $self->{next_input_character} <= 0x005A) or # x               $self->{next_char} <= 0x005A) or # x
2570              (0x0061 <= $self->{next_input_character} and # a              (0x0061 <= $self->{next_char} and # a
2571               $self->{next_input_character} <= 0x007A) or # z               $self->{next_char} <= 0x007A) or # z
2572              (0x0030 <= $self->{next_input_character} and # 0              (0x0030 <= $self->{next_char} and # 0
2573               $self->{next_input_character} <= 0x0039) or # 9               $self->{next_char} <= 0x0039) or # 9
2574              $self->{next_input_character} == 0x003B)) { # ;              $self->{next_char} == 0x003B)) { # ;
2575        $entity_name .= chr $self->{next_input_character};        $entity_name .= chr $self->{next_char};
2576        if (defined $EntityChar->{$entity_name}) {        if (defined $EntityChar->{$entity_name}) {
2577          if ($self->{next_input_character} == 0x003B) { # ;          if ($self->{next_char} == 0x003B) { # ;
2578              !!!cp (1020);
2579            $value = $EntityChar->{$entity_name};            $value = $EntityChar->{$entity_name};
2580            $match = 1;            $match = 1;
2581            !!!next-input-character;            !!!next-input-character;
2582            last;            last;
2583          } else {          } else {
2584              !!!cp (1021);
2585            $value = $EntityChar->{$entity_name};            $value = $EntityChar->{$entity_name};
2586            $match = -1;            $match = -1;
2587            !!!next-input-character;            !!!next-input-character;
2588          }          }
2589        } else {        } else {
2590          $value .= chr $self->{next_input_character};          !!!cp (1022);
2591            $value .= chr $self->{next_char};
2592          $match *= 2;          $match *= 2;
2593          !!!next-input-character;          !!!next-input-character;
2594        }        }
2595      }      }
2596            
2597      if ($match > 0) {      if ($match > 0) {
2598        return {type => CHARACTER_TOKEN, data => $value};        !!!cp (1023);
2599          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
2600                  line => $l, column => $c,
2601                 };
2602      } elsif ($match < 0) {      } elsif ($match < 0) {
2603        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc', line => $l, column => $c);
2604        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
2605          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};          !!!cp (1024);
2606        } else {          return {type => CHARACTER_TOKEN, data => '&'.$entity_name,
2607          return {type => CHARACTER_TOKEN, data => $value};                  line => $l, column => $c,
2608                   };
2609          } else {
2610            !!!cp (1025);
2611            return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
2612                    line => $l, column => $c,
2613                   };
2614        }        }
2615      } else {      } else {
2616        !!!parse-error (type => 'bare ero');        !!!cp (1026);
2617        ## NOTE: No characters are consumed in the spec.        !!!parse-error (type => 'bare ero', line => $l, column => $c);
2618        return {type => CHARACTER_TOKEN, data => '&'.$value};        ## NOTE: "No characters are consumed" in the spec.
2619          return {type => CHARACTER_TOKEN, data => '&'.$value,
2620                  line => $l, column => $c,
2621                 };
2622      }      }
2623    } else {    } else {
2624        !!!cp (1027);
2625      ## no characters are consumed      ## no characters are consumed
2626      !!!parse-error (type => 'bare ero');      !!!parse-error (type => 'bare ero', line => $l, column => $c);
2627      return undef;      return undef;
2628    }    }
2629  } # _tokenize_attempt_to_consume_an_entity  } # _tokenize_attempt_to_consume_an_entity
# Line 1973  sub _construct_tree ($) { Line 2661  sub _construct_tree ($) {
2661        
2662    !!!next-token;    !!!next-token;
2663    
   $self->{insertion_mode} = BEFORE_HEAD_IM;  
2664    undef $self->{form_element};    undef $self->{form_element};
2665    undef $self->{head_element};    undef $self->{head_element};
2666    $self->{open_elements} = [];    $self->{open_elements} = [];
2667    undef $self->{inner_html_node};    undef $self->{inner_html_node};
2668    
2669      ## NOTE: The "initial" insertion mode.
2670    $self->_tree_construction_initial; # MUST    $self->_tree_construction_initial; # MUST
2671    
2672      ## NOTE: The "before html" insertion mode.
2673    $self->_tree_construction_root_element;    $self->_tree_construction_root_element;
2674      $self->{insertion_mode} = BEFORE_HEAD_IM;
2675    
2676      ## NOTE: The "before head" insertion mode and so on.
2677    $self->_tree_construction_main;    $self->_tree_construction_main;
2678  } # _construct_tree  } # _construct_tree
2679    
2680  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
2681    my $self = shift;    my $self = shift;
2682    
2683      ## NOTE: "initial" insertion mode
2684    
2685    INITIAL: {    INITIAL: {
2686      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
2687        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
# Line 1997  sub _tree_construction_initial ($) { Line 2693  sub _tree_construction_initial ($) {
2693        if (not defined $token->{name} or # <!DOCTYPE>        if (not defined $token->{name} or # <!DOCTYPE>
2694            defined $token->{public_identifier} or            defined $token->{public_identifier} or
2695            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
2696          !!!parse-error (type => 'not HTML5');          !!!cp ('t1');
2697            !!!parse-error (type => 'not HTML5', token => $token);
2698        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
2699            !!!cp ('t2');
2700          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)
2701          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
2702          } else {
2703            !!!cp ('t3');
2704        }        }
2705                
2706        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
2707          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
2708          ## NOTE: Default value for both |public_id| and |system_id| attributes
2709          ## are empty strings, so that we don't set any value in missing cases.
2710        $doctype->public_id ($token->{public_identifier})        $doctype->public_id ($token->{public_identifier})
2711            if defined $token->{public_identifier};            if defined $token->{public_identifier};
2712        $doctype->system_id ($token->{system_identifier})        $doctype->system_id ($token->{system_identifier})
# Line 2013  sub _tree_construction_initial ($) { Line 2715  sub _tree_construction_initial ($) {
2715        ## ISSUE: internalSubset = null??        ## ISSUE: internalSubset = null??
2716        $self->{document}->append_child ($doctype);        $self->{document}->append_child ($doctype);
2717                
2718        if (not $token->{correct} or $doctype_name ne 'HTML') {        if ($token->{quirks} or $doctype_name ne 'HTML') {
2719            !!!cp ('t4');
2720          $self->{document}->manakai_compat_mode ('quirks');          $self->{document}->manakai_compat_mode ('quirks');
2721        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{public_identifier}) {
2722          my $pubid = $token->{public_identifier};          my $pubid = $token->{public_identifier};
# Line 2067  sub _tree_construction_initial ($) { Line 2770  sub _tree_construction_initial ($) {
2770            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,
2771            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,
2772            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,
2773              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//EN" => 1,
2774              "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//EN" => 1,
2775              "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//EN" => 1,
2776            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,
2777            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,
2778            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,
# Line 2089  sub _tree_construction_initial ($) { Line 2795  sub _tree_construction_initial ($) {
2795            "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,            "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,
2796            "HTML" => 1,            "HTML" => 1,
2797          }->{$pubid}) {          }->{$pubid}) {
2798              !!!cp ('t5');
2799            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
2800          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or
2801                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {
2802            if (defined $token->{system_identifier}) {            if (defined $token->{system_identifier}) {
2803                !!!cp ('t6');
2804              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
2805            } else {            } else {
2806                !!!cp ('t7');
2807              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
2808            }            }
2809          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 Frameset//EN" or          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 FRAMESET//EN" or
2810                   $pubid eq "-//W3C//DTD XHTML 1.0 Transitional//EN") {                   $pubid eq "-//W3C//DTD XHTML 1.0 TRANSITIONAL//EN") {
2811              !!!cp ('t8');
2812            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
2813            } else {
2814              !!!cp ('t9');
2815          }          }
2816          } else {
2817            !!!cp ('t10');
2818        }        }
2819        if (defined $token->{system_identifier}) {        if (defined $token->{system_identifier}) {
2820          my $sysid = $token->{system_identifier};          my $sysid = $token->{system_identifier};
2821          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
2822          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") {
2823              ## TODO: Check the spec: PUBLIC "(limited quirks)" "(quirks)"
2824            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
2825              !!!cp ('t11');
2826            } else {
2827              !!!cp ('t12');
2828          }          }
2829          } else {
2830            !!!cp ('t13');
2831        }        }
2832                
2833        ## Go to the root element phase.        ## Go to the "before html" insertion mode.
2834        !!!next-token;        !!!next-token;
2835        return;        return;
2836      } elsif ({      } elsif ({
# Line 2118  sub _tree_construction_initial ($) { Line 2838  sub _tree_construction_initial ($) {
2838                END_TAG_TOKEN, 1,                END_TAG_TOKEN, 1,
2839                END_OF_FILE_TOKEN, 1,                END_OF_FILE_TOKEN, 1,
2840               }->{$token->{type}}) {               }->{$token->{type}}) {
2841        !!!parse-error (type => 'no DOCTYPE');        !!!cp ('t14');
2842          !!!parse-error (type => 'no DOCTYPE', token => $token);
2843        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
2844        ## Go to the root element phase        ## Go to the "before html" insertion mode.
2845        ## reprocess        ## reprocess
2846        return;        return;
2847      } elsif ($token->{type} == CHARACTER_TOKEN) {      } elsif ($token->{type} == CHARACTER_TOKEN) {
# Line 2128  sub _tree_construction_initial ($) { Line 2849  sub _tree_construction_initial ($) {
2849          ## Ignore the token          ## Ignore the token
2850    
2851          unless (length $token->{data}) {          unless (length $token->{data}) {
2852            ## Stay in the phase            !!!cp ('t15');
2853              ## Stay in the insertion mode.
2854            !!!next-token;            !!!next-token;
2855            redo INITIAL;            redo INITIAL;
2856            } else {
2857              !!!cp ('t16');
2858          }          }
2859          } else {
2860            !!!cp ('t17');
2861        }        }
2862    
2863        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
2864        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
2865        ## Go to the root element phase        ## Go to the "before html" insertion mode.
2866        ## reprocess        ## reprocess
2867        return;        return;
2868      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
2869          !!!cp ('t18');
2870        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
2871        $self->{document}->append_child ($comment);        $self->{document}->append_child ($comment);
2872                
2873        ## Stay in the phase.        ## Stay in the insertion mode.
2874        !!!next-token;        !!!next-token;
2875        redo INITIAL;        redo INITIAL;
2876      } else {      } else {
2877        die "$0: $token->{type}: Unknown token type";        die "$0: $token->{type}: Unknown token type";
2878      }      }
2879    } # INITIAL    } # INITIAL
2880    
2881      die "$0: _tree_construction_initial: This should be never reached";
2882  } # _tree_construction_initial  } # _tree_construction_initial
2883    
2884  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
2885    my $self = shift;    my $self = shift;
2886    
2887      ## NOTE: "before html" insertion mode.
2888        
2889    B: {    B: {
2890        if ($token->{type} == DOCTYPE_TOKEN) {        if ($token->{type} == DOCTYPE_TOKEN) {
2891          !!!parse-error (type => 'in html:#DOCTYPE');          !!!cp ('t19');
2892            !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
2893          ## Ignore the token          ## Ignore the token
2894          ## Stay in the phase          ## Stay in the insertion mode.
2895          !!!next-token;          !!!next-token;
2896          redo B;          redo B;
2897        } elsif ($token->{type} == COMMENT_TOKEN) {        } elsif ($token->{type} == COMMENT_TOKEN) {
2898            !!!cp ('t20');
2899          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
2900          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
2901          ## Stay in the phase          ## Stay in the insertion mode.
2902          !!!next-token;          !!!next-token;
2903          redo B;          redo B;
2904        } elsif ($token->{type} == CHARACTER_TOKEN) {        } elsif ($token->{type} == CHARACTER_TOKEN) {
# Line 2173  sub _tree_construction_root_element ($) Line 2906  sub _tree_construction_root_element ($)
2906            ## Ignore the token.            ## Ignore the token.
2907    
2908            unless (length $token->{data}) {            unless (length $token->{data}) {
2909              ## Stay in the phase              !!!cp ('t21');
2910                ## Stay in the insertion mode.
2911              !!!next-token;              !!!next-token;
2912              redo B;              redo B;
2913              } else {
2914                !!!cp ('t22');
2915            }            }
2916            } else {
2917              !!!cp ('t23');
2918          }          }
2919    
2920          $self->{application_cache_selection}->(undef);          $self->{application_cache_selection}->(undef);
2921    
2922          #          #
2923        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
2924          if ($token->{tag_name} eq 'html' and          if ($token->{tag_name} eq 'html') {
2925              $token->{attributes}->{manifest}) { ## ISSUE: Spec spells as "application"            my $root_element;
2926            $self->{application_cache_selection}            !!!create-element ($root_element, $token->{tag_name}, $token->{attributes}, $token);
2927                 ->($token->{attributes}->{manifest}->{value});            $self->{document}->append_child ($root_element);
2928            ## ISSUE: No relative reference resolution?            push @{$self->{open_elements}},
2929                  [$root_element, $el_category->{html}];
2930    
2931              if ($token->{attributes}->{manifest}) {
2932                !!!cp ('t24');
2933                $self->{application_cache_selection}
2934                    ->($token->{attributes}->{manifest}->{value});
2935                ## ISSUE: Spec is unclear on relative references.
2936                ## According to Hixie (#whatwg 2008-03-19), it should be
2937                ## resolved against the base URI of the document in HTML
2938                ## or xml:base of the element in XHTML.
2939              } else {
2940                !!!cp ('t25');
2941                $self->{application_cache_selection}->(undef);
2942              }
2943    
2944              !!!next-token;
2945              return; ## Go to the "before head" insertion mode.
2946          } else {          } else {
2947            $self->{application_cache_selection}->(undef);            !!!cp ('t25.1');
2948              #
2949          }          }
   
         ## ISSUE: There is an issue in the spec  
         #  
2950        } elsif ({        } elsif ({
2951                  END_TAG_TOKEN, 1,                  END_TAG_TOKEN, 1,
2952                  END_OF_FILE_TOKEN, 1,                  END_OF_FILE_TOKEN, 1,
2953                 }->{$token->{type}}) {                 }->{$token->{type}}) {
2954          $self->{application_cache_selection}->(undef);          !!!cp ('t26');
   
         ## ISSUE: There is an issue in the spec  
2955          #          #
2956        } else {        } else {
2957          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
2958        }        }
2959    
2960        my $root_element; !!!create-element ($root_element, 'html');      my $root_element; !!!create-element ($root_element, 'html',, $token);
2961        $self->{document}->append_child ($root_element);      $self->{document}->append_child ($root_element);
2962        push @{$self->{open_elements}}, [$root_element, 'html'];      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
2963        ## reprocess  
2964        #redo B;      $self->{application_cache_selection}->(undef);
2965        return; ## Go to the main phase.  
2966        ## NOTE: Reprocess the token.
2967        return; ## Go to the "before head" insertion mode.
2968    
2969        ## ISSUE: There is an issue in the spec
2970    } # B    } # B
2971    
2972      die "$0: _tree_construction_root_element: This should never be reached";
2973  } # _tree_construction_root_element  } # _tree_construction_root_element
2974    
2975  sub _reset_insertion_mode ($) {  sub _reset_insertion_mode ($) {
# Line 2227  sub _reset_insertion_mode ($) { Line 2984  sub _reset_insertion_mode ($) {
2984            
2985      ## Step 3      ## Step 3
2986      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"!?  
2987        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
2988          $last = 1;          $last = 1;
2989          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
2990            if ($self->{inner_html_node}->[1] eq 'td' or            if ($self->{inner_html_node}->[1] & TABLE_CELL_EL) {
2991                $self->{inner_html_node}->[1] eq 'th') {              !!!cp ('t27');
2992              #              #
2993            } else {            } else {
2994                !!!cp ('t28');
2995              $node = $self->{inner_html_node};              $node = $self->{inner_html_node};
2996            }            }
2997          }          }
# Line 2247  sub _reset_insertion_mode ($) { Line 3000  sub _reset_insertion_mode ($) {
3000        ## Step 4..13        ## Step 4..13
3001        my $new_mode = {        my $new_mode = {
3002                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
3003                          ## NOTE: |option| and |optgroup| do not set
3004                          ## insertion mode to "in select" by themselves.
3005                        td => IN_CELL_IM,                        td => IN_CELL_IM,
3006                        th => IN_CELL_IM,                        th => IN_CELL_IM,
3007                        tr => IN_ROW_IM,                        tr => IN_ROW_IM,
# Line 2259  sub _reset_insertion_mode ($) { Line 3014  sub _reset_insertion_mode ($) {
3014                        head => IN_BODY_IM, # not in head!                        head => IN_BODY_IM, # not in head!
3015                        body => IN_BODY_IM,                        body => IN_BODY_IM,
3016                        frameset => IN_FRAMESET_IM,                        frameset => IN_FRAMESET_IM,
3017                       }->{$node->[1]};                       }->{$node->[0]->manakai_local_name};
3018                         ## TODO: Foreign namespace case OK?
3019        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3020                
3021        ## Step 14        ## Step 14
3022        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3023          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3024              !!!cp ('t29');
3025            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
3026          } else {          } else {
3027              ## ISSUE: Can this state be reached?
3028              !!!cp ('t30');
3029            $self->{insertion_mode} = AFTER_HEAD_IM;            $self->{insertion_mode} = AFTER_HEAD_IM;
3030          }          }
3031          return;          return;
3032          } else {
3033            !!!cp ('t31');
3034        }        }
3035                
3036        ## Step 15        ## Step 15
# Line 2282  sub _reset_insertion_mode ($) { Line 3043  sub _reset_insertion_mode ($) {
3043        ## Step 17        ## Step 17
3044        redo S3;        redo S3;
3045      } # S3      } # S3
3046    
3047      die "$0: _reset_insertion_mode: This line should never be reached";
3048  } # _reset_insertion_mode  } # _reset_insertion_mode
3049    
3050  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
# Line 2303  sub _tree_construction_main ($) { Line 3066  sub _tree_construction_main ($) {
3066      return if $entry->[0] eq '#marker';      return if $entry->[0] eq '#marker';
3067      for (@{$self->{open_elements}}) {      for (@{$self->{open_elements}}) {
3068        if ($entry->[0] eq $_->[0]) {        if ($entry->[0] eq $_->[0]) {
3069            !!!cp ('t32');
3070          return;          return;
3071        }        }
3072      }      }
# Line 2317  sub _tree_construction_main ($) { Line 3081  sub _tree_construction_main ($) {
3081    
3082        ## Step 6        ## Step 6
3083        if ($entry->[0] eq '#marker') {        if ($entry->[0] eq '#marker') {
3084            !!!cp ('t33_1');
3085          #          #
3086        } else {        } else {
3087          my $in_open_elements;          my $in_open_elements;
3088          OE: for (@{$self->{open_elements}}) {          OE: for (@{$self->{open_elements}}) {
3089            if ($entry->[0] eq $_->[0]) {            if ($entry->[0] eq $_->[0]) {
3090                !!!cp ('t33');
3091              $in_open_elements = 1;              $in_open_elements = 1;
3092              last OE;              last OE;
3093            }            }
3094          }          }
3095          if ($in_open_elements) {          if ($in_open_elements) {
3096              !!!cp ('t34');
3097            #            #
3098          } else {          } else {
3099              ## NOTE: <!DOCTYPE HTML><p><b><i><u></p> <p>X
3100              !!!cp ('t35');
3101            redo S4;            redo S4;
3102          }          }
3103        }        }
# Line 2351  sub _tree_construction_main ($) { Line 3120  sub _tree_construction_main ($) {
3120    
3121        ## Step 11        ## Step 11
3122        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {
3123            !!!cp ('t36');
3124          ## Step 7'          ## Step 7'
3125          $i++;          $i++;
3126          $entry = $active_formatting_elements->[$i];          $entry = $active_formatting_elements->[$i];
3127                    
3128          redo S7;          redo S7;
3129        }        }
3130    
3131          !!!cp ('t37');
3132      } # S7      } # S7
3133    }; # $reconstruct_active_formatting_elements    }; # $reconstruct_active_formatting_elements
3134    
3135    my $clear_up_to_marker = sub {    my $clear_up_to_marker = sub {
3136      for (reverse 0..$#$active_formatting_elements) {      for (reverse 0..$#$active_formatting_elements) {
3137        if ($active_formatting_elements->[$_]->[0] eq '#marker') {        if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3138            !!!cp ('t38');
3139          splice @$active_formatting_elements, $_;          splice @$active_formatting_elements, $_;
3140          return;          return;
3141        }        }
3142      }      }
3143    
3144        !!!cp ('t39');
3145    }; # $clear_up_to_marker    }; # $clear_up_to_marker
3146    
3147    my $parse_rcdata = sub ($$) {    my $insert;
3148      my ($content_model_flag, $insert) = @_;  
3149      my $parse_rcdata = sub ($) {
3150        my ($content_model_flag) = @_;
3151    
3152      ## Step 1      ## Step 1
3153      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3154      my $el;      my $el;
3155      !!!create-element ($el, $start_tag_name, $token->{attributes});      !!!create-element ($el, $start_tag_name, $token->{attributes}, $token);
3156    
3157      ## Step 2      ## Step 2
3158      $insert->($el); # /context node/->append_child ($el)      $insert->($el);
3159    
3160      ## Step 3      ## Step 3
3161      $self->{content_model} = $content_model_flag; # CDATA or RCDATA      $self->{content_model} = $content_model_flag; # CDATA or RCDATA
# Line 2388  sub _tree_construction_main ($) { Line 3165  sub _tree_construction_main ($) {
3165      my $text = '';      my $text = '';
3166      !!!next-token;      !!!next-token;
3167      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3168          !!!cp ('t40');
3169        $text .= $token->{data};        $text .= $token->{data};
3170        !!!next-token;        !!!next-token;
3171      }      }
3172    
3173      ## Step 5      ## Step 5
3174      if (length $text) {      if (length $text) {
3175          !!!cp ('t41');
3176        my $text = $self->{document}->create_text_node ($text);        my $text = $self->{document}->create_text_node ($text);
3177        $el->append_child ($text);        $el->append_child ($text);
3178      }      }
# Line 2402  sub _tree_construction_main ($) { Line 3181  sub _tree_construction_main ($) {
3181      $self->{content_model} = PCDATA_CONTENT_MODEL;      $self->{content_model} = PCDATA_CONTENT_MODEL;
3182    
3183      ## Step 7      ## Step 7
3184      if ($token->{type} == END_TAG_TOKEN and $token->{tag_name} eq $start_tag_name) {      if ($token->{type} == END_TAG_TOKEN and
3185            $token->{tag_name} eq $start_tag_name) {
3186          !!!cp ('t42');
3187        ## 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});  
3188      } else {      } else {
3189        die "$0: $content_model_flag in parse_rcdata";        ## NOTE: An end-of-file token.
3190          if ($content_model_flag == CDATA_CONTENT_MODEL) {
3191            !!!cp ('t43');
3192            !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3193          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3194            !!!cp ('t44');
3195            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
3196          } else {
3197            die "$0: $content_model_flag in parse_rcdata";
3198          }
3199      }      }
3200      !!!next-token;      !!!next-token;
3201    }; # $parse_rcdata    }; # $parse_rcdata
3202    
3203    my $script_start_tag = sub ($) {    my $script_start_tag = sub () {
     my $insert = $_[0];  
3204      my $script_el;      my $script_el;
3205      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, 'script', $token->{attributes}, $token);
3206      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3207    
3208      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
# Line 2426  sub _tree_construction_main ($) { Line 3211  sub _tree_construction_main ($) {
3211      my $text = '';      my $text = '';
3212      !!!next-token;      !!!next-token;
3213      while ($token->{type} == CHARACTER_TOKEN) {      while ($token->{type} == CHARACTER_TOKEN) {
3214          !!!cp ('t45');
3215        $text .= $token->{data};        $text .= $token->{data};
3216        !!!next-token;        !!!next-token;
3217      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
3218      if (length $text) {      if (length $text) {
3219          !!!cp ('t46');
3220        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
3221      }      }
3222                                
# Line 2437  sub _tree_construction_main ($) { Line 3224  sub _tree_construction_main ($) {
3224    
3225      if ($token->{type} == END_TAG_TOKEN and      if ($token->{type} == END_TAG_TOKEN and
3226          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
3227          !!!cp ('t47');
3228        ## Ignore the token        ## Ignore the token
3229      } else {      } else {
3230        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!cp ('t48');
3231          !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3232        ## ISSUE: And ignore?        ## ISSUE: And ignore?
3233        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3234      }      }
3235            
3236      if (defined $self->{inner_html_node}) {      if (defined $self->{inner_html_node}) {
3237          !!!cp ('t49');
3238        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3239      } else {      } else {
3240          !!!cp ('t50');
3241        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
3242        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
3243    
# Line 2460  sub _tree_construction_main ($) { Line 3251  sub _tree_construction_main ($) {
3251      !!!next-token;      !!!next-token;
3252    }; # $script_start_tag    }; # $script_start_tag
3253    
3254      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
3255      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
3256      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
3257    
3258    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
3259      my $tag_name = shift;      my $end_tag_token = shift;
3260        my $tag_name = $end_tag_token->{tag_name};
3261    
3262        ## NOTE: The adoption agency algorithm (AAA).
3263    
3264      FET: {      FET: {
3265        ## Step 1        ## Step 1
3266        my $formatting_element;        my $formatting_element;
3267        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3268        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3269          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3270              !!!cp ('t52');
3271              last AFE;
3272            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
3273                         eq $tag_name) {
3274              !!!cp ('t51');
3275            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3276            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3277            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           last AFE;  
3278          }          }
3279        } # AFE        } # AFE
3280        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3281          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!cp ('t53');
3282            !!!parse-error (type => 'unmatched end tag:'.$tag_name, token => $end_tag_token);
3283          ## Ignore the token          ## Ignore the token
3284          !!!next-token;          !!!next-token;
3285          return;          return;
# Line 2489  sub _tree_construction_main ($) { Line 3291  sub _tree_construction_main ($) {
3291          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3292          if ($node->[0] eq $formatting_element->[0]) {          if ($node->[0] eq $formatting_element->[0]) {
3293            if ($in_scope) {            if ($in_scope) {
3294                !!!cp ('t54');
3295              $formatting_element_i_in_open = $_;              $formatting_element_i_in_open = $_;
3296              last INSCOPE;              last INSCOPE;
3297            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3298              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t55');
3299                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3300                                token => $end_tag_token);
3301              ## Ignore the token              ## Ignore the token
3302              !!!next-token;              !!!next-token;
3303              return;              return;
3304            }            }
3305          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
3306                    table => 1, caption => 1, td => 1, th => 1,            !!!cp ('t56');
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
3307            $in_scope = 0;            $in_scope = 0;
3308          }          }
3309        } # INSCOPE        } # INSCOPE
3310        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3311          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t57');
3312            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3313                            token => $end_tag_token);
3314          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3315          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
3316          return;          return;
3317        }        }
3318        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3319          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!cp ('t58');
3320            !!!parse-error (type => 'not closed',
3321                            value => $self->{open_elements}->[-1]->[0]
3322                                ->manakai_local_name,
3323                            token => $end_tag_token);
3324        }        }
3325                
3326        ## Step 2        ## Step 2
# Line 2519  sub _tree_construction_main ($) { Line 3328  sub _tree_construction_main ($) {
3328        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
3329        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3330          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3331          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
3332              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3333              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
3334               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
3335              !!!cp ('t59');
3336            $furthest_block = $node;            $furthest_block = $node;
3337            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
3338          } elsif ($node->[0] eq $formatting_element->[0]) {          } elsif ($node->[0] eq $formatting_element->[0]) {
3339              !!!cp ('t60');
3340            last OE;            last OE;
3341          }          }
3342        } # OE        } # OE
3343                
3344        ## Step 3        ## Step 3
3345        unless (defined $furthest_block) { # MUST        unless (defined $furthest_block) { # MUST
3346            !!!cp ('t61');
3347          splice @{$self->{open_elements}}, $formatting_element_i_in_open;          splice @{$self->{open_elements}}, $formatting_element_i_in_open;
3348          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;
3349          !!!next-token;          !!!next-token;
# Line 2544  sub _tree_construction_main ($) { Line 3356  sub _tree_construction_main ($) {
3356        ## Step 5        ## Step 5
3357        my $furthest_block_parent = $furthest_block->[0]->parent_node;        my $furthest_block_parent = $furthest_block->[0]->parent_node;
3358        if (defined $furthest_block_parent) {        if (defined $furthest_block_parent) {
3359            !!!cp ('t62');
3360          $furthest_block_parent->remove_child ($furthest_block->[0]);          $furthest_block_parent->remove_child ($furthest_block->[0]);
3361        }        }
3362                
# Line 2566  sub _tree_construction_main ($) { Line 3379  sub _tree_construction_main ($) {
3379          S7S2: {          S7S2: {
3380            for (reverse 0..$#$active_formatting_elements) {            for (reverse 0..$#$active_formatting_elements) {
3381              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
3382                  !!!cp ('t63');
3383                $node_i_in_active = $_;                $node_i_in_active = $_;
3384                last S7S2;                last S7S2;
3385              }              }
# Line 2579  sub _tree_construction_main ($) { Line 3393  sub _tree_construction_main ($) {
3393                    
3394          ## Step 4          ## Step 4
3395          if ($last_node->[0] eq $furthest_block->[0]) {          if ($last_node->[0] eq $furthest_block->[0]) {
3396              !!!cp ('t64');
3397            $bookmark_prev_el = $node->[0];            $bookmark_prev_el = $node->[0];
3398          }          }
3399                    
3400          ## Step 5          ## Step 5
3401          if ($node->[0]->has_child_nodes ()) {          if ($node->[0]->has_child_nodes ()) {
3402              !!!cp ('t65');
3403            my $clone = [$node->[0]->clone_node (0), $node->[1]];            my $clone = [$node->[0]->clone_node (0), $node->[1]];
3404            $active_formatting_elements->[$node_i_in_active] = $clone;            $active_formatting_elements->[$node_i_in_active] = $clone;
3405            $self->{open_elements}->[$node_i_in_open] = $clone;            $self->{open_elements}->[$node_i_in_open] = $clone;
# Line 2601  sub _tree_construction_main ($) { Line 3417  sub _tree_construction_main ($) {
3417        } # S7          } # S7  
3418                
3419        ## Step 8        ## Step 8
3420        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
3421            my $foster_parent_element;
3422            my $next_sibling;
3423            OE: for (reverse 0..$#{$self->{open_elements}}) {
3424              if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3425                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3426                                 if (defined $parent and $parent->node_type == 1) {
3427                                   !!!cp ('t65.1');
3428                                   $foster_parent_element = $parent;
3429                                   $next_sibling = $self->{open_elements}->[$_]->[0];
3430                                 } else {
3431                                   !!!cp ('t65.2');
3432                                   $foster_parent_element
3433                                     = $self->{open_elements}->[$_ - 1]->[0];
3434                                 }
3435                                 last OE;
3436                               }
3437                             } # OE
3438                             $foster_parent_element = $self->{open_elements}->[0]->[0]
3439                               unless defined $foster_parent_element;
3440            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
3441            $open_tables->[-1]->[1] = 1; # tainted
3442          } else {
3443            !!!cp ('t65.3');
3444            $common_ancestor_node->[0]->append_child ($last_node->[0]);
3445          }
3446                
3447        ## Step 9        ## Step 9
3448        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 2618  sub _tree_construction_main ($) { Line 3459  sub _tree_construction_main ($) {
3459        my $i;        my $i;
3460        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3461          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {
3462              !!!cp ('t66');
3463            splice @$active_formatting_elements, $_, 1;            splice @$active_formatting_elements, $_, 1;
3464            $i-- and last AFE if defined $i;            $i-- and last AFE if defined $i;
3465          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {
3466              !!!cp ('t67');
3467            $i = $_;            $i = $_;
3468          }          }
3469        } # AFE        } # AFE
# Line 2630  sub _tree_construction_main ($) { Line 3473  sub _tree_construction_main ($) {
3473        undef $i;        undef $i;
3474        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3475          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {
3476              !!!cp ('t68');
3477            splice @{$self->{open_elements}}, $_, 1;            splice @{$self->{open_elements}}, $_, 1;
3478            $i-- and last OE if defined $i;            $i-- and last OE if defined $i;
3479          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {
3480              !!!cp ('t69');
3481            $i = $_;            $i = $_;
3482          }          }
3483        } # OE        } # OE
# Line 2643  sub _tree_construction_main ($) { Line 3488  sub _tree_construction_main ($) {
3488      } # FET      } # FET
3489    }; # $formatting_end_tag    }; # $formatting_end_tag
3490    
3491    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
3492      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
3493    }; # $insert_to_current    }; # $insert_to_current
3494    
3495    my $insert_to_foster = sub {    my $insert_to_foster = sub {
3496                         my $child = shift;      my $child = shift;
3497                         if ({      if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
3498                              table => 1, tbody => 1, tfoot => 1,        # MUST
3499                              thead => 1, tr => 1,        my $foster_parent_element;
3500                             }->{$self->{open_elements}->[-1]->[1]}) {        my $next_sibling;
3501                           # MUST        OE: for (reverse 0..$#{$self->{open_elements}}) {
3502                           my $foster_parent_element;          if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
                          my $next_sibling;  
                          OE: for (reverse 0..$#{$self->{open_elements}}) {  
                            if ($self->{open_elements}->[$_]->[1] eq 'table') {  
3503                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3504                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3505                                   !!!cp ('t70');
3506                                 $foster_parent_element = $parent;                                 $foster_parent_element = $parent;
3507                                 $next_sibling = $self->{open_elements}->[$_]->[0];                                 $next_sibling = $self->{open_elements}->[$_]->[0];
3508                               } else {                               } else {
3509                                   !!!cp ('t71');
3510                                 $foster_parent_element                                 $foster_parent_element
3511                                   = $self->{open_elements}->[$_ - 1]->[0];                                   = $self->{open_elements}->[$_ - 1]->[0];
3512                               }                               }
# Line 2673  sub _tree_construction_main ($) { Line 3517  sub _tree_construction_main ($) {
3517                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
3518                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
3519                             ($child, $next_sibling);                             ($child, $next_sibling);
3520                         } else {        $open_tables->[-1]->[1] = 1; # tainted
3521                           $self->{open_elements}->[-1]->[0]->append_child ($child);      } else {
3522                         }        !!!cp ('t72');
3523          $self->{open_elements}->[-1]->[0]->append_child ($child);
3524        }
3525    }; # $insert_to_foster    }; # $insert_to_foster
3526    
   my $insert;  
   
3527    B: {    B: {
3528      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
3529        !!!parse-error (type => 'DOCTYPE in the middle');        !!!cp ('t73');
3530          !!!parse-error (type => 'DOCTYPE in the middle', token => $token);
3531        ## Ignore the token        ## Ignore the token
3532        ## Stay in the phase        ## Stay in the phase
3533        !!!next-token;        !!!next-token;
3534        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;  
3535      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
3536               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
3537        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
3538          ## Turn into the main phase          !!!cp ('t79');
3539          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html', token => $token);
3540          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
3541        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
3542          ## Turn into the main phase          !!!cp ('t80');
3543          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html', token => $token);
3544          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
3545          } else {
3546            !!!cp ('t81');
3547        }        }
3548    
3549  ## ISSUE: "aa<html>" is not a parse error.        !!!cp ('t82');
3550  ## 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');  
       }  
3551        my $top_el = $self->{open_elements}->[0]->[0];        my $top_el = $self->{open_elements}->[0]->[0];
3552        for my $attr_name (keys %{$token->{attributes}}) {        for my $attr_name (keys %{$token->{attributes}}) {
3553          unless ($top_el->has_attribute_ns (undef, $attr_name)) {          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
3554              !!!cp ('t84');
3555            $top_el->set_attribute_ns            $top_el->set_attribute_ns
3556              (undef, [undef, $attr_name],              (undef, [undef, $attr_name],
3557               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
# Line 2745  sub _tree_construction_main ($) { Line 3562  sub _tree_construction_main ($) {
3562      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
3563        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
3564        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
3565            !!!cp ('t85');
3566          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
3567        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
3568            !!!cp ('t86');
3569          $self->{open_elements}->[0]->[0]->append_child ($comment);          $self->{open_elements}->[0]->[0]->append_child ($comment);
3570        } else {        } else {
3571            !!!cp ('t87');
3572          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
3573        }        }
3574        !!!next-token;        !!!next-token;
# Line 2756  sub _tree_construction_main ($) { Line 3576  sub _tree_construction_main ($) {
3576      } elsif ($self->{insertion_mode} & HEAD_IMS) {      } elsif ($self->{insertion_mode} & HEAD_IMS) {
3577        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
3578          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3579            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3580                !!!cp ('t88.2');
3581                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3582              } else {
3583                !!!cp ('t88.1');
3584                ## Ignore the token.
3585                !!!next-token;
3586                redo B;
3587              }
3588            unless (length $token->{data}) {            unless (length $token->{data}) {
3589                !!!cp ('t88');
3590              !!!next-token;              !!!next-token;
3591              redo B;              redo B;
3592            }            }
3593          }          }
3594    
3595          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3596              !!!cp ('t89');
3597            ## As if <head>            ## As if <head>
3598            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, 'head',, $token);
3599            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3600            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            push @{$self->{open_elements}},
3601                  [$self->{head_element}, $el_category->{head}];
3602    
3603            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
3604            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
3605    
3606            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
3607          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3608              !!!cp ('t90');
3609            ## As if </noscript>            ## As if </noscript>
3610            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
3611            !!!parse-error (type => 'in noscript:#character');            !!!parse-error (type => 'in noscript:#character', token => $token);
3612                        
3613            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
3614            ## As if </head>            ## As if </head>
# Line 2784  sub _tree_construction_main ($) { Line 3616  sub _tree_construction_main ($) {
3616    
3617            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
3618          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3619              !!!cp ('t91');
3620            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
3621    
3622            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
3623            } else {
3624              !!!cp ('t92');
3625          }          }
3626    
3627              ## "after head" insertion mode          ## "after head" insertion mode
3628              ## As if <body>          ## As if <body>
3629              !!!insert-element ('body');          !!!insert-element ('body',, $token);
3630              $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
3631              ## reprocess          ## reprocess
3632            redo B;
3633          } elsif ($token->{type} == START_TAG_TOKEN) {
3634            if ($token->{tag_name} eq 'head') {
3635              if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3636                !!!cp ('t93');
3637                !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes}, $token);
3638                $self->{open_elements}->[-1]->[0]->append_child
3639                    ($self->{head_element});
3640                push @{$self->{open_elements}},
3641                    [$self->{head_element}, $el_category->{head}];
3642                $self->{insertion_mode} = IN_HEAD_IM;
3643                !!!next-token;
3644              redo B;              redo B;
           } elsif ($token->{type} == START_TAG_TOKEN) {  
             if ($token->{tag_name} eq 'head') {  
               if ($self->{insertion_mode} == BEFORE_HEAD_IM) {  
                 !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});  
                 $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});  
                 push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];  
                 $self->{insertion_mode} = IN_HEAD_IM;  
                 !!!next-token;  
                 redo B;  
3645                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3646                    !!!cp ('t94');
3647                  #                  #
3648                } else {                } else {
3649                  !!!parse-error (type => 'in head:head'); # or in head noscript                  !!!cp ('t95');
3650                    !!!parse-error (type => 'in head:head', token => $token); # or in head noscript
3651                  ## Ignore the token                  ## Ignore the token
3652                  !!!next-token;                  !!!next-token;
3653                  redo B;                  redo B;
3654                }                }
3655              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3656                  !!!cp ('t96');
3657                ## As if <head>                ## As if <head>
3658                !!!create-element ($self->{head_element}, 'head');                !!!create-element ($self->{head_element}, 'head',, $token);
3659                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3660                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                push @{$self->{open_elements}},
3661                      [$self->{head_element}, $el_category->{head}];
3662    
3663                $self->{insertion_mode} = IN_HEAD_IM;                $self->{insertion_mode} = IN_HEAD_IM;
3664                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
3665                } else {
3666                  !!!cp ('t97');
3667              }              }
3668    
3669              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
3670                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3671                    !!!cp ('t98');
3672                  ## As if </noscript>                  ## As if </noscript>
3673                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3674                  !!!parse-error (type => 'in noscript:base');                  !!!parse-error (type => 'in noscript:base', token => $token);
3675                                
3676                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3677                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3678                  } else {
3679                    !!!cp ('t99');
3680                }                }
3681    
3682                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3683                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3684                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t100');
3685                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3686                    push @{$self->{open_elements}},
3687                        [$self->{head_element}, $el_category->{head}];
3688                  } else {
3689                    !!!cp ('t101');
3690                }                }
3691                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3692                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3693                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
3694                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3695                !!!next-token;                !!!next-token;
3696                redo B;                redo B;
3697              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
3698                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3699                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3700                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t102');
3701                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3702                    push @{$self->{open_elements}},
3703                        [$self->{head_element}, $el_category->{head}];
3704                  } else {
3705                    !!!cp ('t103');
3706                }                }
3707                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3708                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3709                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
3710                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3711                !!!next-token;                !!!next-token;
3712                redo B;                redo B;
3713              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
3714                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3715                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3716                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t104');
3717                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3718                    push @{$self->{open_elements}},
3719                        [$self->{head_element}, $el_category->{head}];
3720                  } else {
3721                    !!!cp ('t105');
3722                }                }
3723                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3724                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.
3725    
3726                unless ($self->{confident}) {                unless ($self->{confident}) {
3727                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) { ## TODO: And if supported
3728                      !!!cp ('t106');
3729                    $self->{change_encoding}                    $self->{change_encoding}
3730                        ->($self, $token->{attributes}->{charset}->{value});                        ->($self, $token->{attributes}->{charset}->{value},
3731                             $token);
3732                      
3733                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
3734                          ->set_user_data (manakai_has_reference =>
3735                                               $token->{attributes}->{charset}
3736                                                   ->{has_reference});
3737                  } elsif ($token->{attributes}->{content}) {                  } elsif ($token->{attributes}->{content}) {
3738                    ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.                    ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
3739                    if ($token->{attributes}->{content}->{value}                    if ($token->{attributes}->{content}->{value}
3740                        =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
3741                              [\x09-\x0D\x20]*=
3742                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
3743                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
3744                        !!!cp ('t107');
3745                      $self->{change_encoding}                      $self->{change_encoding}
3746                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
3747                               $token);
3748                        $meta_el->[0]->get_attribute_node_ns (undef, 'content')
3749                            ->set_user_data (manakai_has_reference =>
3750                                                 $token->{attributes}->{content}
3751                                                       ->{has_reference});
3752                      } else {
3753                        !!!cp ('t108');
3754                    }                    }
3755                  }                  }
3756                  } else {
3757                    if ($token->{attributes}->{charset}) {
3758                      !!!cp ('t109');
3759                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
3760                          ->set_user_data (manakai_has_reference =>
3761                                               $token->{attributes}->{charset}
3762                                                   ->{has_reference});
3763                    }
3764                    if ($token->{attributes}->{content}) {
3765                      !!!cp ('t110');
3766                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
3767                          ->set_user_data (manakai_has_reference =>
3768                                               $token->{attributes}->{content}
3769                                                   ->{has_reference});
3770                    }
3771                }                }
3772    
3773                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
3774                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3775                !!!next-token;                !!!next-token;
3776                redo B;                redo B;
3777              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
3778                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3779                    !!!cp ('t111');
3780                  ## As if </noscript>                  ## As if </noscript>
3781                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3782                  !!!parse-error (type => 'in noscript:title');                  !!!parse-error (type => 'in noscript:title', token => $token);
3783                                
3784                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3785                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3786                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3787                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t112');
3788                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3789                    push @{$self->{open_elements}},
3790                        [$self->{head_element}, $el_category->{head}];
3791                  } else {
3792                    !!!cp ('t113');
3793                }                }
3794    
3795                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3796                my $parent = defined $self->{head_element} ? $self->{head_element}                my $parent = defined $self->{head_element} ? $self->{head_element}
3797                    : $self->{open_elements}->[-1]->[0];                    : $self->{open_elements}->[-1]->[0];
3798                $parse_rcdata->(RCDATA_CONTENT_MODEL,                $parse_rcdata->(RCDATA_CONTENT_MODEL);
3799                                sub { $parent->append_child ($_[0]) });                pop @{$self->{open_elements}} # <head>
               pop @{$self->{open_elements}}  
3800                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3801                redo B;                redo B;
3802              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style') {
# Line 2910  sub _tree_construction_main ($) { Line 3804  sub _tree_construction_main ($) {
3804                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
3805                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3806                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3807                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t114');
3808                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3809                    push @{$self->{open_elements}},
3810                        [$self->{head_element}, $el_category->{head}];
3811                  } else {
3812                    !!!cp ('t115');
3813                }                }
3814                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL);
3815                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
3816                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3817                redo B;                redo B;
3818              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
3819                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
3820                    !!!cp ('t116');
3821                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
3822                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3823                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
3824                  !!!next-token;                  !!!next-token;
3825                  redo B;                  redo B;
3826                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3827                  !!!parse-error (type => 'in noscript:noscript');                  !!!cp ('t117');
3828                    !!!parse-error (type => 'in noscript:noscript', token => $token);
3829                  ## Ignore the token                  ## Ignore the token
3830                  !!!next-token;                  !!!next-token;
3831                  redo B;                  redo B;
3832                } else {                } else {
3833                    !!!cp ('t118');
3834                  #                  #
3835                }                }
3836              } elsif ($token->{tag_name} eq 'script') {              } elsif ($token->{tag_name} eq 'script') {
3837                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3838                    !!!cp ('t119');
3839                  ## As if </noscript>                  ## As if </noscript>
3840                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3841                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:script', token => $token);
3842                                
3843                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3844                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3845                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3846                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t120');
3847                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3848                    push @{$self->{open_elements}},
3849                        [$self->{head_element}, $el_category->{head}];
3850                  } else {
3851                    !!!cp ('t121');
3852                }                }
3853    
3854                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3855                $script_start_tag->($insert_to_current);                $script_start_tag->();
3856                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
3857                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3858                redo B;                redo B;
3859              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
3860                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
3861                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3862                    !!!cp ('t122');
3863                  ## As if </noscript>                  ## As if </noscript>
3864                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3865                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});                  !!!parse-error (type => 'in noscript:'.$token->{tag_name}, token => $token);
3866                                    
3867                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3868                  ## As if </head>                  ## As if </head>
# Line 2963  sub _tree_construction_main ($) { Line 3870  sub _tree_construction_main ($) {
3870                                    
3871                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
3872                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3873                    !!!cp ('t124');
3874                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3875                                    
3876                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
3877                  } else {
3878                    !!!cp ('t125');
3879                }                }
3880    
3881                ## "after head" insertion mode                ## "after head" insertion mode
3882                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3883                if ($token->{tag_name} eq 'body') {                if ($token->{tag_name} eq 'body') {
3884                    !!!cp ('t126');
3885                  $self->{insertion_mode} = IN_BODY_IM;                  $self->{insertion_mode} = IN_BODY_IM;
3886                } elsif ($token->{tag_name} eq 'frameset') {                } elsif ($token->{tag_name} eq 'frameset') {
3887                    !!!cp ('t127');
3888                  $self->{insertion_mode} = IN_FRAMESET_IM;                  $self->{insertion_mode} = IN_FRAMESET_IM;
3889                } else {                } else {
3890                  die "$0: tag name: $self->{tag_name}";                  die "$0: tag name: $self->{tag_name}";
# Line 2980  sub _tree_construction_main ($) { Line 3892  sub _tree_construction_main ($) {
3892                !!!next-token;                !!!next-token;
3893                redo B;                redo B;
3894              } else {              } else {
3895                  !!!cp ('t128');
3896                #                #
3897              }              }
3898    
3899              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3900                  !!!cp ('t129');
3901                ## As if </noscript>                ## As if </noscript>
3902                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3903                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
3904                                
3905                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
3906                ## As if </head>                ## As if </head>
# Line 2994  sub _tree_construction_main ($) { Line 3908  sub _tree_construction_main ($) {
3908    
3909                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
3910              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3911                  !!!cp ('t130');
3912                ## As if </head>                ## As if </head>
3913                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3914    
3915                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
3916                } else {
3917                  !!!cp ('t131');
3918              }              }
3919    
3920              ## "after head" insertion mode              ## "after head" insertion mode
3921              ## As if <body>              ## As if <body>
3922              !!!insert-element ('body');              !!!insert-element ('body',, $token);
3923              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
3924              ## reprocess              ## reprocess
3925              redo B;              redo B;
3926            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
3927              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
3928                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3929                    !!!cp ('t132');
3930                  ## As if <head>                  ## As if <head>
3931                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, 'head',, $token);
3932                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3933                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
3934                        [$self->{head_element}, $el_category->{head}];
3935    
3936                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3937                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 3020  sub _tree_construction_main ($) { Line 3939  sub _tree_construction_main ($) {
3939                  !!!next-token;                  !!!next-token;
3940                  redo B;                  redo B;
3941                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3942                    !!!cp ('t133');
3943                  ## As if </noscript>                  ## As if </noscript>
3944                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3945                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:/head', token => $token);
3946                                    
3947                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3948                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 3030  sub _tree_construction_main ($) { Line 3950  sub _tree_construction_main ($) {
3950                  !!!next-token;                  !!!next-token;
3951                  redo B;                  redo B;
3952                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3953                    !!!cp ('t134');
3954                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3955                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
3956                  !!!next-token;                  !!!next-token;
3957                  redo B;                  redo B;
3958                } else {                } else {
3959                    !!!cp ('t135');
3960                  #                  #
3961                }                }
3962              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
3963                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3964                    !!!cp ('t136');
3965                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3966                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3967                  !!!next-token;                  !!!next-token;
3968                  redo B;                  redo B;
3969                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3970                  !!!parse-error (type => 'unmatched end tag:noscript');                  !!!cp ('t137');
3971                    !!!parse-error (type => 'unmatched end tag:noscript', token => $token);
3972                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
3973                  !!!next-token;                  !!!next-token;
3974                  redo B;                  redo B;
3975                } else {                } else {
3976                    !!!cp ('t138');
3977                  #                  #
3978                }                }
3979              } elsif ({              } elsif ({
3980                        body => 1, html => 1,                        body => 1, html => 1,
3981                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3982                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3983                    !!!cp ('t139');
3984                  ## As if <head>                  ## As if <head>
3985                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, 'head',, $token);
3986                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3987                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
3988                        [$self->{head_element}, $el_category->{head}];
3989    
3990                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3991                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3992                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3993                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t140');
3994                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
3995                  ## Ignore the token                  ## Ignore the token
3996                  !!!next-token;                  !!!next-token;
3997                  redo B;                  redo B;
3998                  } else {
3999                    !!!cp ('t141');
4000                }                }
4001                                
4002                #                #
# Line 3074  sub _tree_construction_main ($) { Line 4004  sub _tree_construction_main ($) {
4004                        p => 1, br => 1,                        p => 1, br => 1,
4005                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4006                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4007                    !!!cp ('t142');
4008                  ## As if <head>                  ## As if <head>
4009                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, 'head',, $token);
4010                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4011                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4012                        [$self->{head_element}, $el_category->{head}];
4013    
4014                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4015                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4016                  } else {
4017                    !!!cp ('t143');
4018                }                }
4019    
4020                #                #
4021              } else {              } else {
4022                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4023                    !!!cp ('t144');
4024                  #                  #
4025                } else {                } else {
4026                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t145');
4027                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4028                  ## Ignore the token                  ## Ignore the token
4029                  !!!next-token;                  !!!next-token;
4030                  redo B;                  redo B;
# Line 3096  sub _tree_construction_main ($) { Line 4032  sub _tree_construction_main ($) {
4032              }              }
4033    
4034              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4035                  !!!cp ('t146');
4036                ## As if </noscript>                ## As if </noscript>
4037                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4038                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
4039                                
4040                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4041                ## As if </head>                ## As if </head>
# Line 3106  sub _tree_construction_main ($) { Line 4043  sub _tree_construction_main ($) {
4043    
4044                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
4045              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4046                  !!!cp ('t147');
4047                ## As if </head>                ## As if </head>
4048                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4049    
4050                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
4051              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4052                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  ## ISSUE: This case cannot be reached?
4053                  !!!cp ('t148');
4054                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4055                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
4056                !!!next-token;                !!!next-token;
4057                redo B;                redo B;
4058                } else {
4059                  !!!cp ('t149');
4060              }              }
4061    
4062              ## "after head" insertion mode              ## "after head" insertion mode
4063              ## As if <body>              ## As if <body>
4064              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4065              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4066              ## reprocess              ## reprocess
4067              redo B;              redo B;
4068            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4069              die "$0: $token->{type}: Unknown token type";          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4070            }            !!!cp ('t149.1');
4071    
4072              ## NOTE: As if <head>
4073              !!!create-element ($self->{head_element}, 'head',, $token);
4074              $self->{open_elements}->[-1]->[0]->append_child
4075                  ($self->{head_element});
4076              #push @{$self->{open_elements}},
4077              #    [$self->{head_element}, $el_category->{head}];
4078              #$self->{insertion_mode} = IN_HEAD_IM;
4079              ## NOTE: Reprocess.
4080    
4081              ## NOTE: As if </head>
4082              #pop @{$self->{open_elements}};
4083              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4084              ## NOTE: Reprocess.
4085              
4086              #
4087            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4088              !!!cp ('t149.2');
4089    
4090              ## NOTE: As if </head>
4091              pop @{$self->{open_elements}};
4092              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4093              ## NOTE: Reprocess.
4094    
4095              #
4096            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4097              !!!cp ('t149.3');
4098    
4099              !!!parse-error (type => 'in noscript:#eof', token => $token);
4100    
4101              ## As if </noscript>
4102              pop @{$self->{open_elements}};
4103              #$self->{insertion_mode} = IN_HEAD_IM;
4104              ## NOTE: Reprocess.
4105    
4106              ## NOTE: As if </head>
4107              pop @{$self->{open_elements}};
4108              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4109              ## NOTE: Reprocess.
4110    
4111              #
4112            } else {
4113              !!!cp ('t149.4');
4114              #
4115            }
4116    
4117            ## NOTE: As if <body>
4118            !!!insert-element ('body',, $token);
4119            $self->{insertion_mode} = IN_BODY_IM;
4120            ## NOTE: Reprocess.
4121            redo B;
4122          } else {
4123            die "$0: $token->{type}: Unknown token type";
4124          }
4125    
4126            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
4127      } elsif ($self->{insertion_mode} & BODY_IMS) {      } elsif ($self->{insertion_mode} & BODY_IMS) {
4128            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
4129                !!!cp ('t150');
4130              ## NOTE: There is a code clone of "character in body".              ## NOTE: There is a code clone of "character in body".
4131              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
4132                            
# Line 3144  sub _tree_construction_main ($) { Line 4141  sub _tree_construction_main ($) {
4141                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4142                if ($self->{insertion_mode} == IN_CELL_IM) {                if ($self->{insertion_mode} == IN_CELL_IM) {
4143                  ## have an element in table scope                  ## have an element in table scope
4144                  my $tn;                  for (reverse 0..$#{$self->{open_elements}}) {
                 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
4145                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4146                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] & TABLE_CELL_EL) {
4147                      $tn = $node->[1];                      !!!cp ('t151');
4148                      last INSCOPE;  
4149                    } elsif ({                      ## Close the cell
4150                              table => 1, html => 1,                      !!!back-token; # <?>
4151                             }->{$node->[1]}) {                      $token = {type => END_TAG_TOKEN,
4152                      last INSCOPE;                                tag_name => $node->[0]->manakai_local_name,
4153                    }                                line => $token->{line},
4154                  } # INSCOPE                                column => $token->{column}};
                   unless (defined $tn) {  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
4155                      redo B;                      redo B;
4156                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4157                        !!!cp ('t152');
4158                        ## ISSUE: This case can never be reached, maybe.
4159                        last;
4160                    }                    }
4161                                    }
4162                  ## Close the cell  
4163                  !!!back-token; # <?>                  !!!cp ('t153');
4164                  $token = {type => END_TAG_TOKEN, tag_name => $tn};                  !!!parse-error (type => 'start tag not allowed',
4165                        value => $token->{tag_name}, token => $token);
4166                    ## Ignore the token
4167                    !!!next-token;
4168                  redo B;                  redo B;
4169                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4170                  !!!parse-error (type => 'not closed:caption');                  !!!parse-error (type => 'not closed:caption', token => $token);
4171                                    
4172                  ## As if </caption>                  ## NOTE: As if </caption>.
4173                  ## have a table element in table scope                  ## have a table element in table scope
4174                  my $i;                  my $i;
4175                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4176                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4177                    if ($node->[1] eq 'caption') {                      my $node = $self->{open_elements}->[$_];
4178                      $i = $_;                      if ($node->[1] & CAPTION_EL) {
4179                      last INSCOPE;                        !!!cp ('t155');
4180                    } elsif ({                        $i = $_;
4181                              table => 1, html => 1,                        last INSCOPE;
4182                             }->{$node->[1]}) {                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4183                      last INSCOPE;                        !!!cp ('t156');
4184                          last;
4185                        }
4186                    }                    }
4187    
4188                      !!!cp ('t157');
4189                      !!!parse-error (type => 'start tag not allowed',
4190                                      value => $token->{tag_name}, token => $token);
4191                      ## Ignore the token
4192                      !!!next-token;
4193                      redo B;
4194                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!parse-error (type => 'unmatched end tag:caption');  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
4195                                    
4196                  ## generate implied end tags                  ## generate implied end tags
4197                  if ({                  while ($self->{open_elements}->[-1]->[1]
4198                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
4199                       td => 1, th => 1, tr => 1,                    !!!cp ('t158');
4200                       tbody => 1, tfoot=> 1, thead => 1,                    pop @{$self->{open_elements}};
                     }->{$self->{open_elements}->[-1]->[1]}) {  
                   !!!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;  
4201                  }                  }
4202    
4203                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4204                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t159');
4205                      !!!parse-error (type => 'not closed',
4206                                      value => $self->{open_elements}->[-1]->[0]
4207                                          ->manakai_local_name,
4208                                      token => $token);
4209                    } else {
4210                      !!!cp ('t160');
4211                  }                  }
4212                                    
4213                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3218  sub _tree_construction_main ($) { Line 4219  sub _tree_construction_main ($) {
4219                  ## reprocess                  ## reprocess
4220                  redo B;                  redo B;
4221                } else {                } else {
4222                    !!!cp ('t161');
4223                  #                  #
4224                }                }
4225              } else {              } else {
4226                  !!!cp ('t162');
4227                #                #
4228              }              }
4229            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
# Line 3230  sub _tree_construction_main ($) { Line 4233  sub _tree_construction_main ($) {
4233                  my $i;                  my $i;
4234                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4235                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4236                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4237                        !!!cp ('t163');
4238                      $i = $_;                      $i = $_;
4239                      last INSCOPE;                      last INSCOPE;
4240                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
4241                              table => 1, html => 1,                      !!!cp ('t164');
                            }->{$node->[1]}) {  
4242                      last INSCOPE;                      last INSCOPE;
4243                    }                    }
4244                  } # INSCOPE                  } # INSCOPE
4245                    unless (defined $i) {                    unless (defined $i) {
4246                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!cp ('t165');
4247                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4248                      ## Ignore the token                      ## Ignore the token
4249                      !!!next-token;                      !!!next-token;
4250                      redo B;                      redo B;
4251                    }                    }
4252                                    
4253                  ## generate implied end tags                  ## generate implied end tags
4254                  if ({                  while ($self->{open_elements}->[-1]->[1]
4255                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
4256                       td => ($token->{tag_name} eq 'th'),                    !!!cp ('t166');
4257                       th => ($token->{tag_name} eq 'td'),                    pop @{$self->{open_elements}};
                      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;  
4258                  }                  }
4259                    
4260                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
4261                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                          ne $token->{tag_name}) {
4262                      !!!cp ('t167');
4263                      !!!parse-error (type => 'not closed',
4264                                      value => $self->{open_elements}->[-1]->[0]
4265                                          ->manakai_local_name,
4266                                      token => $token);
4267                    } else {
4268                      !!!cp ('t168');
4269                  }                  }
4270                                    
4271                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3273  sub _tree_construction_main ($) { Line 4277  sub _tree_construction_main ($) {
4277                  !!!next-token;                  !!!next-token;
4278                  redo B;                  redo B;
4279                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4280                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t169');
4281                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4282                  ## Ignore the token                  ## Ignore the token
4283                  !!!next-token;                  !!!next-token;
4284                  redo B;                  redo B;
4285                } else {                } else {
4286                    !!!cp ('t170');
4287                  #                  #
4288                }                }
4289              } elsif ($token->{tag_name} eq 'caption') {              } elsif ($token->{tag_name} eq 'caption') {
4290                if ($self->{insertion_mode} == IN_CAPTION_IM) {                if ($self->{insertion_mode} == IN_CAPTION_IM) {
4291                  ## have a table element in table scope                  ## have a table element in table scope
4292                  my $i;                  my $i;
4293                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4294                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4295                    if ($node->[1] eq $token->{tag_name}) {                      my $node = $self->{open_elements}->[$_];
4296                      $i = $_;                      if ($node->[1] & CAPTION_EL) {
4297                      last INSCOPE;                        !!!cp ('t171');
4298                    } elsif ({                        $i = $_;
4299                              table => 1, html => 1,                        last INSCOPE;
4300                             }->{$node->[1]}) {                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4301                      last INSCOPE;                        !!!cp ('t172');
4302                          last;
4303                        }
4304                    }                    }
4305    
4306                      !!!cp ('t173');
4307                      !!!parse-error (type => 'unmatched end tag',
4308                                      value => $token->{tag_name}, token => $token);
4309                      ## Ignore the token
4310                      !!!next-token;
4311                      redo B;
4312                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
4313                                    
4314                  ## generate implied end tags                  ## generate implied end tags
4315                  if ({                  while ($self->{open_elements}->[-1]->[1]
4316                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
4317                       td => 1, th => 1, tr => 1,                    !!!cp ('t174');
4318                       tbody => 1, tfoot=> 1, thead => 1,                    pop @{$self->{open_elements}};
                     }->{$self->{open_elements}->[-1]->[1]}) {  
                   !!!back-token;  
                   $token = {type => END_TAG_TOKEN,  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
4319                  }                  }
4320                                    
4321                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4322                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t175');
4323                      !!!parse-error (type => 'not closed',
4324                                      value => $self->{open_elements}->[-1]->[0]
4325                                          ->manakai_local_name,
4326                                      token => $token);
4327                    } else {
4328                      !!!cp ('t176');
4329                  }                  }
4330                                    
4331                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3327  sub _tree_construction_main ($) { Line 4337  sub _tree_construction_main ($) {
4337                  !!!next-token;                  !!!next-token;
4338                  redo B;                  redo B;
4339                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
4340                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t177');
4341                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4342                  ## Ignore the token                  ## Ignore the token
4343                  !!!next-token;                  !!!next-token;
4344                  redo B;                  redo B;
4345                } else {                } else {
4346                    !!!cp ('t178');
4347                  #                  #
4348                }                }
4349              } elsif ({              } elsif ({
# Line 3342  sub _tree_construction_main ($) { Line 4354  sub _tree_construction_main ($) {
4354                ## have an element in table scope                ## have an element in table scope
4355                my $i;                my $i;
4356                my $tn;                my $tn;
4357                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: {
4358                  my $node = $self->{open_elements}->[$_];                  for (reverse 0..$#{$self->{open_elements}}) {
4359                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
4360                    $i = $_;                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4361                    last INSCOPE;                      !!!cp ('t179');
4362                  } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {                      $i = $_;
4363                    $tn = $node->[1];  
4364                    ## NOTE: There is exactly one |td| or |th| element                      ## Close the cell
4365                    ## in scope in the stack of open elements by definition.                      !!!back-token; # </?>
4366                  } elsif ({                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
4367                            table => 1, html => 1,                                line => $token->{line},
4368                           }->{$node->[1]}) {                                column => $token->{column}};
4369                    last INSCOPE;                      redo B;
4370                      } elsif ($node->[1] & TABLE_CELL_EL) {
4371                        !!!cp ('t180');
4372                        $tn = $node->[0]->manakai_local_name;
4373                        ## NOTE: There is exactly one |td| or |th| element
4374                        ## in scope in the stack of open elements by definition.
4375                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4376                        ## ISSUE: Can this be reached?
4377                        !!!cp ('t181');
4378                        last;
4379                      }
4380                  }                  }
4381                } # INSCOPE  
4382                unless (defined $i) {                  !!!cp ('t182');
4383                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
4384                        value => $token->{tag_name}, token => $token);
4385                  ## Ignore the token                  ## Ignore the token
4386                  !!!next-token;                  !!!next-token;
4387                  redo B;                  redo B;
4388                }                } # INSCOPE
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => END_TAG_TOKEN, tag_name => $tn};  
               redo B;  
4389              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
4390                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4391                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption', token => $token);
4392    
4393                ## As if </caption>                ## As if </caption>
4394                ## have a table element in table scope                ## have a table element in table scope
4395                my $i;                my $i;
4396                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4397                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4398                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
4399                      !!!cp ('t184');
4400                    $i = $_;                    $i = $_;
4401                    last INSCOPE;                    last INSCOPE;
4402                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
4403                            table => 1, html => 1,                    !!!cp ('t185');
                          }->{$node->[1]}) {  
4404                    last INSCOPE;                    last INSCOPE;
4405                  }                  }
4406                } # INSCOPE                } # INSCOPE
4407                unless (defined $i) {                unless (defined $i) {
4408                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!cp ('t186');
4409                    !!!parse-error (type => 'unmatched end tag:caption', token => $token);
4410                  ## Ignore the token                  ## Ignore the token
4411                  !!!next-token;                  !!!next-token;
4412                  redo B;                  redo B;
4413                }                }
4414                                
4415                ## generate implied end tags                ## generate implied end tags
4416                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
4417                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t187');
4418                     td => 1, th => 1, tr => 1,                  pop @{$self->{open_elements}};
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!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;  
4419                }                }
4420    
4421                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4422                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t188');
4423                    !!!parse-error (type => 'not closed',
4424                                    value => $self->{open_elements}->[-1]->[0]
4425                                        ->manakai_local_name,
4426                                    token => $token);
4427                  } else {
4428                    !!!cp ('t189');
4429                }                }
4430    
4431                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
# Line 3423  sub _tree_construction_main ($) { Line 4440  sub _tree_construction_main ($) {
4440                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
4441                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4442                if ($self->{insertion_mode} & BODY_TABLE_IMS) {                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
4443                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t190');
4444                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4445                  ## Ignore the token                  ## Ignore the token
4446                  !!!next-token;                  !!!next-token;
4447                  redo B;                  redo B;
4448                } else {                } else {
4449                    !!!cp ('t191');
4450                  #                  #
4451                }                }
4452              } elsif ({              } elsif ({
# Line 3435  sub _tree_construction_main ($) { Line 4454  sub _tree_construction_main ($) {
4454                        thead => 1, tr => 1,                        thead => 1, tr => 1,
4455                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
4456                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4457                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t192');
4458                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4459                ## Ignore the token                ## Ignore the token
4460                !!!next-token;                !!!next-token;
4461                redo B;                redo B;
4462              } else {              } else {
4463                  !!!cp ('t193');
4464                #                #
4465              }              }
4466          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4467            for my $entry (@{$self->{open_elements}}) {
4468              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
4469                !!!cp ('t75');
4470                !!!parse-error (type => 'in body:#eof', token => $token);
4471                last;
4472              }
4473            }
4474    
4475            ## Stop parsing.
4476            last B;
4477        } else {        } else {
4478          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
4479        }        }
# Line 3450  sub _tree_construction_main ($) { Line 4482  sub _tree_construction_main ($) {
4482        #        #
4483      } elsif ($self->{insertion_mode} & TABLE_IMS) {      } elsif ($self->{insertion_mode} & TABLE_IMS) {
4484        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4485              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if (not $open_tables->[-1]->[1] and # tainted
4486                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);              $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4487              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4488                                
4489                unless (length $token->{data}) {            unless (length $token->{data}) {
4490                  !!!next-token;              !!!cp ('t194');
4491                  redo B;              !!!next-token;
4492                }              redo B;
4493              }            } else {
4494                !!!cp ('t195');
4495              }
4496            }
4497    
4498              !!!parse-error (type => 'in table:#character');              !!!parse-error (type => 'in table:#character', token => $token);
4499    
4500              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
4501              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
# Line 3467  sub _tree_construction_main ($) { Line 4503  sub _tree_construction_main ($) {
4503              ## result in a new Text node.              ## result in a new Text node.
4504              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
4505                            
4506              if ({              if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
                  table => 1, tbody => 1, tfoot => 1,  
                  thead => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
4507                # MUST                # MUST
4508                my $foster_parent_element;                my $foster_parent_element;
4509                my $next_sibling;                my $next_sibling;
4510                my $prev_sibling;                my $prev_sibling;
4511                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
4512                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
4513                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4514                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
4515                        !!!cp ('t196');
4516                      $foster_parent_element = $parent;                      $foster_parent_element = $parent;
4517                      $next_sibling = $self->{open_elements}->[$_]->[0];                      $next_sibling = $self->{open_elements}->[$_]->[0];
4518                      $prev_sibling = $next_sibling->previous_sibling;                      $prev_sibling = $next_sibling->previous_sibling;
4519                    } else {                    } else {
4520                        !!!cp ('t197');
4521                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
4522                      $prev_sibling = $foster_parent_element->last_child;                      $prev_sibling = $foster_parent_element->last_child;
4523                    }                    }
# Line 3494  sub _tree_construction_main ($) { Line 4529  sub _tree_construction_main ($) {
4529                  unless defined $foster_parent_element;                  unless defined $foster_parent_element;
4530                if (defined $prev_sibling and                if (defined $prev_sibling and
4531                    $prev_sibling->node_type == 3) {                    $prev_sibling->node_type == 3) {
4532                    !!!cp ('t198');
4533                  $prev_sibling->manakai_append_text ($token->{data});                  $prev_sibling->manakai_append_text ($token->{data});
4534                } else {                } else {
4535                    !!!cp ('t199');
4536                  $foster_parent_element->insert_before                  $foster_parent_element->insert_before
4537                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
4538                     $next_sibling);                     $next_sibling);
4539                }                }
4540              } else {            $open_tables->[-1]->[1] = 1; # tainted
4541                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
4542              }            !!!cp ('t200');
4543              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4544            }
4545                            
4546              !!!next-token;          !!!next-token;
4547              redo B;          redo B;
4548        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4549              if ({              if ({
4550                   tr => ($self->{insertion_mode} != IN_ROW_IM),                   tr => ($self->{insertion_mode} != IN_ROW_IM),
# Line 3513  sub _tree_construction_main ($) { Line 4552  sub _tree_construction_main ($) {
4552                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4553                if ($self->{insertion_mode} == IN_TABLE_IM) {                if ($self->{insertion_mode} == IN_TABLE_IM) {
4554                  ## Clear back to table context                  ## Clear back to table context
4555                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
4556                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
4557                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t201');
4558                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4559                  }                  }
4560                                    
4561                  !!!insert-element ('tbody');                  !!!insert-element ('tbody',, $token);
4562                  $self->{insertion_mode} = IN_TABLE_BODY_IM;                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
4563                  ## reprocess in the "in table body" insertion mode...                  ## reprocess in the "in table body" insertion mode...
4564                }                }
4565    
4566                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
4567                  unless ($token->{tag_name} eq 'tr') {                  unless ($token->{tag_name} eq 'tr') {
4568                    !!!parse-error (type => 'missing start tag:tr');                    !!!cp ('t202');
4569                      !!!parse-error (type => 'missing start tag:tr', token => $token);
4570                  }                  }
4571                                    
4572                  ## Clear back to table body context                  ## Clear back to table body context
4573                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
4574                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
4575                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t203');
4576                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this case be reached?
4577                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4578                  }                  }
4579                                    
4580                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
4581                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
4582                    !!!insert-element ($token->{tag_name}, $token->{attributes});                    !!!cp ('t204');
4583                      !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4584                    !!!next-token;                    !!!next-token;
4585                    redo B;                    redo B;
4586                  } else {                  } else {
4587                    !!!insert-element ('tr');                    !!!cp ('t205');
4588                      !!!insert-element ('tr',, $token);
4589                    ## reprocess in the "in row" insertion mode                    ## reprocess in the "in row" insertion mode
4590                  }                  }
4591                  } else {
4592                    !!!cp ('t206');
4593                }                }
4594    
4595                ## Clear back to table row context                ## Clear back to table row context
4596                while (not {                while (not ($self->{open_elements}->[-1]->[1]
4597                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
4598                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t207');
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4599                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4600                }                }
4601                                
4602                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4603                $self->{insertion_mode} = IN_CELL_IM;                $self->{insertion_mode} = IN_CELL_IM;
4604    
4605                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
# Line 3574  sub _tree_construction_main ($) { Line 4617  sub _tree_construction_main ($) {
4617                  my $i;                  my $i;
4618                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4619                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4620                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
4621                        !!!cp ('t208');
4622                      $i = $_;                      $i = $_;
4623                      last INSCOPE;                      last INSCOPE;
4624                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
4625                              table => 1, html => 1,                      !!!cp ('t209');
                            }->{$node->[1]}) {  
4626                      last INSCOPE;                      last INSCOPE;
4627                    }                    }
4628                  } # INSCOPE                  } # INSCOPE
4629                  unless (defined $i) {                  unless (defined $i) {
4630                    !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                   !!!cp ('t210');
4631    ## TODO: This type is wrong.
4632                     !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);
4633                    ## Ignore the token                    ## Ignore the token
4634                    !!!next-token;                    !!!next-token;
4635                    redo B;                    redo B;
4636                  }                  }
4637                                    
4638                  ## Clear back to table row context                  ## Clear back to table row context
4639                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
4640                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
4641                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t211');
4642                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this case be reached?
4643                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4644                  }                  }
4645                                    
4646                  pop @{$self->{open_elements}}; # tr                  pop @{$self->{open_elements}}; # tr
4647                  $self->{insertion_mode} = IN_TABLE_BODY_IM;                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
4648                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
4649                      !!!cp ('t212');
4650                    ## reprocess                    ## reprocess
4651                    redo B;                    redo B;
4652                  } else {                  } else {
4653                      !!!cp ('t213');
4654                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
4655                  }                  }
4656                }                }
# Line 3613  sub _tree_construction_main ($) { Line 4660  sub _tree_construction_main ($) {
4660                  my $i;                  my $i;
4661                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4662                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4663                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
4664                         tbody => 1, thead => 1, tfoot => 1,                      !!!cp ('t214');
                       }->{$node->[1]}) {  
4665                      $i = $_;                      $i = $_;
4666                      last INSCOPE;                      last INSCOPE;
4667                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
4668                              table => 1, html => 1,                      !!!cp ('t215');
                            }->{$node->[1]}) {  
4669                      last INSCOPE;                      last INSCOPE;
4670                    }                    }
4671                  } # INSCOPE                  } # INSCOPE
4672                  unless (defined $i) {                  unless (defined $i) {
4673                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!cp ('t216');
4674    ## TODO: This erorr type ios wrong.
4675                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4676                    ## Ignore the token                    ## Ignore the token
4677                    !!!next-token;                    !!!next-token;
4678                    redo B;                    redo B;
4679                  }                  }
4680    
4681                  ## Clear back to table body context                  ## Clear back to table body context
4682                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
4683                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
4684                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t217');
4685                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this state be reached?
4686                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4687                  }                  }
4688                                    
# Line 3649  sub _tree_construction_main ($) { Line 4696  sub _tree_construction_main ($) {
4696                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4697                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4698                  ## reprocess in "in table" insertion mode...                  ## reprocess in "in table" insertion mode...
4699                  } else {
4700                    !!!cp ('t218');
4701                }                }
4702    
4703                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
4704                  ## Clear back to table context                  ## Clear back to table context
4705                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
4706                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
4707                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t219');
4708                      ## ISSUE: Can this state be reached?
4709                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4710                  }                  }
4711                                    
4712                  !!!insert-element ('colgroup');                  !!!insert-element ('colgroup',, $token);
4713                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
4714                  ## reprocess                  ## reprocess
4715                  redo B;                  redo B;
# Line 3669  sub _tree_construction_main ($) { Line 4719  sub _tree_construction_main ($) {
4719                          tbody => 1, tfoot => 1, thead => 1,                          tbody => 1, tfoot => 1, thead => 1,
4720                         }->{$token->{tag_name}}) {                         }->{$token->{tag_name}}) {
4721                  ## Clear back to table context                  ## Clear back to table context
4722                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
4723                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
4724                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t220');
4725                      ## ISSUE: Can this state be reached?
4726                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4727                  }                  }
4728                                    
4729                  push @$active_formatting_elements, ['#marker', '']                  push @$active_formatting_elements, ['#marker', '']
4730                      if $token->{tag_name} eq 'caption';                      if $token->{tag_name} eq 'caption';
4731                                    
4732                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4733                  $self->{insertion_mode} = {                  $self->{insertion_mode} = {
4734                                             caption => IN_CAPTION_IM,                                             caption => IN_CAPTION_IM,
4735                                             colgroup => IN_COLUMN_GROUP_IM,                                             colgroup => IN_COLUMN_GROUP_IM,
# Line 3692  sub _tree_construction_main ($) { Line 4743  sub _tree_construction_main ($) {
4743                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
4744                }                }
4745              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
4746                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
4747                                  value => $self->{open_elements}->[-1]->[0]
4748                                      ->manakai_local_name,
4749                                  token => $token);
4750    
4751                ## As if </table>                ## As if </table>
4752                ## have a table element in table scope                ## have a table element in table scope
4753                my $i;                my $i;
4754                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4755                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4756                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
4757                      !!!cp ('t221');
4758                    $i = $_;                    $i = $_;
4759                    last INSCOPE;                    last INSCOPE;
4760                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
4761                            table => 1, html => 1,                    !!!cp ('t222');
                          }->{$node->[1]}) {  
4762                    last INSCOPE;                    last INSCOPE;
4763                  }                  }
4764                } # INSCOPE                } # INSCOPE
4765                unless (defined $i) {                unless (defined $i) {
4766                  !!!parse-error (type => 'unmatched end tag:table');                  !!!cp ('t223');
4767    ## TODO: The following is wrong, maybe.
4768                    !!!parse-error (type => 'unmatched end tag:table', token => $token);
4769                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
4770                  !!!next-token;                  !!!next-token;
4771                  redo B;                  redo B;
4772                }                }
4773                                
4774    ## TODO: Followings are removed from the latest spec.
4775                ## generate implied end tags                ## generate implied end tags
4776                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
4777                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t224');
4778                     td => 1, th => 1, tr => 1,                  pop @{$self->{open_elements}};
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!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;  
4779                }                }
4780    
4781                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
4782                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t225');
4783                    ## NOTE: |<table><tr><table>|
4784                    !!!parse-error (type => 'not closed',
4785                                    value => $self->{open_elements}->[-1]->[0]
4786                                        ->manakai_local_name,
4787                                    token => $token);
4788                  } else {
4789                    !!!cp ('t226');
4790                }                }
4791    
4792                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
4793                  pop @{$open_tables};
4794    
4795                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
4796    
4797                ## reprocess                ## reprocess
4798                redo B;                redo B;
4799          } else {          } elsif ($token->{tag_name} eq 'style') {
4800            !!!parse-error (type => 'in table:'.$token->{tag_name});            if (not $open_tables->[-1]->[1]) { # tainted
4801                !!!cp ('t227.8');
4802                ## NOTE: This is a "as if in head" code clone.
4803                $parse_rcdata->(CDATA_CONTENT_MODEL);
4804                redo B;
4805              } else {
4806                !!!cp ('t227.7');
4807                #
4808              }
4809            } elsif ($token->{tag_name} eq 'script') {
4810              if (not $open_tables->[-1]->[1]) { # tainted
4811                !!!cp ('t227.6');
4812                ## NOTE: This is a "as if in head" code clone.
4813                $script_start_tag->();
4814                redo B;
4815              } else {
4816                !!!cp ('t227.5');
4817                #
4818              }
4819            } elsif ($token->{tag_name} eq 'input') {
4820              if (not $open_tables->[-1]->[1]) { # tainted
4821                if ($token->{attributes}->{type}) { ## TODO: case
4822                  my $type = lc $token->{attributes}->{type}->{value};
4823                  if ($type eq 'hidden') {
4824                    !!!cp ('t227.3');
4825                    !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
4826    
4827            $insert = $insert_to_foster;                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4828    
4829                    ## TODO: form element pointer
4830    
4831                    pop @{$self->{open_elements}};
4832    
4833                    !!!next-token;
4834                    redo B;
4835                  } else {
4836                    !!!cp ('t227.2');
4837                    #
4838                  }
4839                } else {
4840                  !!!cp ('t227.1');
4841                  #
4842                }
4843              } else {
4844                !!!cp ('t227.4');
4845                #
4846              }
4847            } else {
4848              !!!cp ('t227');
4849            #            #
4850          }          }
4851    
4852            !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
4853    
4854            $insert = $insert_to_foster;
4855            #
4856        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
4857              if ($token->{tag_name} eq 'tr' and              if ($token->{tag_name} eq 'tr' and
4858                  $self->{insertion_mode} == IN_ROW_IM) {                  $self->{insertion_mode} == IN_ROW_IM) {
# Line 3752  sub _tree_construction_main ($) { Line 4860  sub _tree_construction_main ($) {
4860                my $i;                my $i;
4861                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4862                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4863                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_ROW_EL) {
4864                      !!!cp ('t228');
4865                    $i = $_;                    $i = $_;
4866                    last INSCOPE;                    last INSCOPE;
4867                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
4868                            table => 1, html => 1,                    !!!cp ('t229');
                          }->{$node->[1]}) {  
4869                    last INSCOPE;                    last INSCOPE;
4870                  }                  }
4871                } # INSCOPE                } # INSCOPE
4872                unless (defined $i) {                unless (defined $i) {
4873                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t230');
4874                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4875                  ## Ignore the token                  ## Ignore the token
4876                  !!!next-token;                  !!!next-token;
4877                  redo B;                  redo B;
4878                  } else {
4879                    !!!cp ('t232');
4880                }                }
4881    
4882                ## Clear back to table row context                ## Clear back to table row context
4883                while (not {                while (not ($self->{open_elements}->[-1]->[1]
4884                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
4885                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t231');
4886                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
4887                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4888                }                }
4889    
# Line 3787  sub _tree_construction_main ($) { Line 4898  sub _tree_construction_main ($) {
4898                  my $i;                  my $i;
4899                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4900                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4901                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
4902                        !!!cp ('t233');
4903                      $i = $_;                      $i = $_;
4904                      last INSCOPE;                      last INSCOPE;
4905                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
4906                              table => 1, html => 1,                      !!!cp ('t234');
                            }->{$node->[1]}) {  
4907                      last INSCOPE;                      last INSCOPE;
4908                    }                    }
4909                  } # INSCOPE                  } # INSCOPE
4910                  unless (defined $i) {                  unless (defined $i) {
4911                    !!!parse-error (type => 'unmatched end tag:'.$token->{type});                    !!!cp ('t235');
4912    ## TODO: The following is wrong.
4913                      !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);
4914                    ## Ignore the token                    ## Ignore the token
4915                    !!!next-token;                    !!!next-token;
4916                    redo B;                    redo B;
4917                  }                  }
4918                                    
4919                  ## Clear back to table row context                  ## Clear back to table row context
4920                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
4921                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
4922                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t236');
4923                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
4924                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4925                  }                  }
4926                                    
# Line 3821  sub _tree_construction_main ($) { Line 4934  sub _tree_construction_main ($) {
4934                  my $i;                  my $i;
4935                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4936                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4937                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
4938                         tbody => 1, thead => 1, tfoot => 1,                      !!!cp ('t237');
                       }->{$node->[1]}) {  
4939                      $i = $_;                      $i = $_;
4940                      last INSCOPE;                      last INSCOPE;
4941                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
4942                              table => 1, html => 1,                      !!!cp ('t238');
                            }->{$node->[1]}) {  
4943                      last INSCOPE;                      last INSCOPE;
4944                    }                    }
4945                  } # INSCOPE                  } # INSCOPE
4946                  unless (defined $i) {                  unless (defined $i) {
4947                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!cp ('t239');
4948                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4949                    ## Ignore the token                    ## Ignore the token
4950                    !!!next-token;                    !!!next-token;
4951                    redo B;                    redo B;
4952                  }                  }
4953                                    
4954                  ## Clear back to table body context                  ## Clear back to table body context
4955                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
4956                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
4957                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t240');
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4958                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4959                  }                  }
4960                                    
# Line 3859  sub _tree_construction_main ($) { Line 4970  sub _tree_construction_main ($) {
4970                  ## reprocess in the "in table" insertion mode...                  ## reprocess in the "in table" insertion mode...
4971                }                }
4972    
4973                  ## NOTE: </table> in the "in table" insertion mode.
4974                  ## When you edit the code fragment below, please ensure that
4975                  ## the code for <table> in the "in table" insertion mode
4976                  ## is synced with it.
4977    
4978                ## have a table element in table scope                ## have a table element in table scope
4979                my $i;                my $i;
4980                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4981                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4982                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_EL) {
4983                      !!!cp ('t241');
4984                    $i = $_;                    $i = $_;
4985                    last INSCOPE;                    last INSCOPE;
4986                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
4987                            table => 1, html => 1,                    !!!cp ('t242');
                          }->{$node->[1]}) {  
4988                    last INSCOPE;                    last INSCOPE;
4989                  }                  }
4990                } # INSCOPE                } # INSCOPE
4991                unless (defined $i) {                unless (defined $i) {
4992                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t243');
4993                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4994                  ## Ignore the token                  ## Ignore the token
4995                  !!!next-token;                  !!!next-token;
4996                  redo B;                  redo B;
4997                }                }
   
               ## 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]);  
               }  
4998                                    
4999                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5000                  pop @{$open_tables};
5001                                
5002                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5003                                
# Line 3910  sub _tree_construction_main ($) { Line 5012  sub _tree_construction_main ($) {
5012                  my $i;                  my $i;
5013                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5014                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5015                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5016                        !!!cp ('t247');
5017                      $i = $_;                      $i = $_;
5018                      last INSCOPE;                      last INSCOPE;
5019                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5020                              table => 1, html => 1,                      !!!cp ('t248');
                            }->{$node->[1]}) {  
5021                      last INSCOPE;                      last INSCOPE;
5022                    }                    }
5023                  } # INSCOPE                  } # INSCOPE
5024                    unless (defined $i) {                    unless (defined $i) {
5025                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!cp ('t249');
5026                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5027                      ## Ignore the token                      ## Ignore the token
5028                      !!!next-token;                      !!!next-token;
5029                      redo B;                      redo B;
# Line 3931  sub _tree_construction_main ($) { Line 5034  sub _tree_construction_main ($) {
5034                  my $i;                  my $i;
5035                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5036                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5037                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5038                        !!!cp ('t250');
5039                      $i = $_;                      $i = $_;
5040                      last INSCOPE;                      last INSCOPE;
5041                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5042                              table => 1, html => 1,                      !!!cp ('t251');
                            }->{$node->[1]}) {  
5043                      last INSCOPE;                      last INSCOPE;
5044                    }                    }
5045                  } # INSCOPE                  } # INSCOPE
5046                    unless (defined $i) {                    unless (defined $i) {
5047                      !!!parse-error (type => 'unmatched end tag:tr');                      !!!cp ('t252');
5048                        !!!parse-error (type => 'unmatched end tag:tr', token => $token);
5049                      ## Ignore the token                      ## Ignore the token
5050                      !!!next-token;                      !!!next-token;
5051                      redo B;                      redo B;
5052                    }                    }
5053                                    
5054                  ## Clear back to table row context                  ## Clear back to table row context
5055                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5056                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
5057                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t253');
5058                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
5059                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5060                  }                  }
5061                                    
# Line 3964  sub _tree_construction_main ($) { Line 5068  sub _tree_construction_main ($) {
5068                my $i;                my $i;
5069                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5070                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5071                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5072                      !!!cp ('t254');
5073                    $i = $_;                    $i = $_;
5074                    last INSCOPE;                    last INSCOPE;
5075                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5076                            table => 1, html => 1,                    !!!cp ('t255');
                          }->{$node->[1]}) {  
5077                    last INSCOPE;                    last INSCOPE;
5078                  }                  }
5079                } # INSCOPE                } # INSCOPE
5080                unless (defined $i) {                unless (defined $i) {
5081                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t256');
5082                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5083                  ## Ignore the token                  ## Ignore the token
5084                  !!!next-token;                  !!!next-token;
5085                  redo B;                  redo B;
5086                }                }
5087    
5088                ## Clear back to table body context                ## Clear back to table body context
5089                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5090                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
5091                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t257');
5092                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
5093                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5094                }                }
5095    
# Line 3998  sub _tree_construction_main ($) { Line 5103  sub _tree_construction_main ($) {
5103                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5104                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
5105                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5106                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t258');
5107                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5108                ## Ignore the token                ## Ignore the token
5109                !!!next-token;                !!!next-token;
5110                redo B;                redo B;
5111          } else {          } else {
5112            !!!parse-error (type => 'in table:/'.$token->{tag_name});            !!!cp ('t259');
5113              !!!parse-error (type => 'in table:/'.$token->{tag_name}, token => $token);
5114    
5115            $insert = $insert_to_foster;            $insert = $insert_to_foster;
5116            #            #
5117          }          }
5118          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5119            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5120                    @{$self->{open_elements}} == 1) { # redundant, maybe
5121              !!!parse-error (type => 'in body:#eof', token => $token);
5122              !!!cp ('t259.1');
5123              #
5124            } else {
5125              !!!cp ('t259.2');
5126              #
5127            }
5128    
5129            ## Stop parsing
5130            last B;
5131        } else {        } else {
5132          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5133        }        }
# Line 4016  sub _tree_construction_main ($) { Line 5136  sub _tree_construction_main ($) {
5136              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5137                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5138                unless (length $token->{data}) {                unless (length $token->{data}) {
5139                    !!!cp ('t260');
5140                  !!!next-token;                  !!!next-token;
5141                  redo B;                  redo B;
5142                }                }
5143              }              }
5144                            
5145                !!!cp ('t261');
5146              #              #
5147            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
5148              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
5149                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!cp ('t262');
5150                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5151                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5152                !!!next-token;                !!!next-token;
5153                redo B;                redo B;
5154              } else {              } else {
5155                  !!!cp ('t263');
5156                #                #
5157              }              }
5158            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
5159              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
5160                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5161                  !!!parse-error (type => 'unmatched end tag:colgroup');                  !!!cp ('t264');
5162                    !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5163                  ## Ignore the token                  ## Ignore the token
5164                  !!!next-token;                  !!!next-token;
5165                  redo B;                  redo B;
5166                } else {                } else {
5167                    !!!cp ('t265');
5168                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
5169                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5170                  !!!next-token;                  !!!next-token;
5171                  redo B;                              redo B;            
5172                }                }
5173              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
5174                !!!parse-error (type => 'unmatched end tag:col');                !!!cp ('t266');
5175                  !!!parse-error (type => 'unmatched end tag:col', token => $token);
5176                ## Ignore the token                ## Ignore the token
5177                !!!next-token;                !!!next-token;
5178                redo B;                redo B;
5179              } else {              } else {
5180                  !!!cp ('t267');
5181                #                #
5182              }              }
5183            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5184              #          if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5185            }              @{$self->{open_elements}} == 1) { # redundant, maybe
5186              !!!cp ('t270.2');
5187              ## Stop parsing.
5188              last B;
5189            } else {
5190              ## NOTE: As if </colgroup>.
5191              !!!cp ('t270.1');
5192              pop @{$self->{open_elements}}; # colgroup
5193              $self->{insertion_mode} = IN_TABLE_IM;
5194              ## Reprocess.
5195              redo B;
5196            }
5197          } else {
5198            die "$0: $token->{type}: Unknown token type";
5199          }
5200    
5201            ## As if </colgroup>            ## As if </colgroup>
5202            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5203              !!!parse-error (type => 'unmatched end tag:colgroup');              !!!cp ('t269');
5204    ## TODO: Wrong error type?
5205                !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5206              ## Ignore the token              ## Ignore the token
5207              !!!next-token;              !!!next-token;
5208              redo B;              redo B;
5209            } else {            } else {
5210                !!!cp ('t270');
5211              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
5212              $self->{insertion_mode} = IN_TABLE_IM;              $self->{insertion_mode} = IN_TABLE_IM;
5213              ## reprocess              ## reprocess
5214              redo B;              redo B;
5215            }            }
5216      } elsif ($self->{insertion_mode} == IN_SELECT_IM) {      } elsif ($self->{insertion_mode} & SELECT_IMS) {
5217        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5218            !!!cp ('t271');
5219          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5220          !!!next-token;          !!!next-token;
5221          redo B;          redo B;
5222        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5223              if ($token->{tag_name} eq 'option') {          if ($token->{tag_name} eq 'option') {
5224                if ($self->{open_elements}->[-1]->[1] eq 'option') {            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5225                  ## As if </option>              !!!cp ('t272');
5226                  pop @{$self->{open_elements}};              ## As if </option>
5227                }              pop @{$self->{open_elements}};
5228              } else {
5229                !!!cp ('t273');
5230              }
5231    
5232                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5233                !!!next-token;            !!!next-token;
5234                redo B;            redo B;
5235              } elsif ($token->{tag_name} eq 'optgroup') {          } elsif ($token->{tag_name} eq 'optgroup') {
5236                if ($self->{open_elements}->[-1]->[1] eq 'option') {            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5237                  ## As if </option>              !!!cp ('t274');
5238                  pop @{$self->{open_elements}};              ## As if </option>
5239                }              pop @{$self->{open_elements}};
5240              } else {
5241                !!!cp ('t275');
5242              }
5243    
5244                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5245                  ## As if </optgroup>              !!!cp ('t276');
5246                  pop @{$self->{open_elements}};              ## As if </optgroup>
5247                }              pop @{$self->{open_elements}};
5248              } else {
5249                !!!cp ('t277');
5250              }
5251    
5252                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5253                !!!next-token;            !!!next-token;
5254                redo B;            redo B;
5255              } elsif ($token->{tag_name} eq 'select') {          } elsif ($token->{tag_name} eq 'select' or
5256                !!!parse-error (type => 'not closed:select');                   $token->{tag_name} eq 'input' or
5257                ## As if </select> instead                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5258                ## have an element in table scope                    {
5259                my $i;                     caption => 1, table => 1,
5260                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                     tbody => 1, tfoot => 1, thead => 1,
5261                  my $node = $self->{open_elements}->[$_];                     tr => 1, td => 1, th => 1,
5262                  if ($node->[1] eq $token->{tag_name}) {                    }->{$token->{tag_name}})) {
5263                    $i = $_;            ## TODO: The type below is not good - <select> is replaced by </select>
5264                    last INSCOPE;            !!!parse-error (type => 'not closed:select', token => $token);
5265                  } elsif ({            ## NOTE: As if the token were </select> (<select> case) or
5266                            table => 1, html => 1,            ## as if there were </select> (otherwise).
5267                           }->{$node->[1]}) {            ## have an element in table scope
5268                    last INSCOPE;            my $i;
5269                  }            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5270                } # INSCOPE              my $node = $self->{open_elements}->[$_];
5271                unless (defined $i) {              if ($node->[1] & SELECT_EL) {
5272                  !!!parse-error (type => 'unmatched end tag:select');                !!!cp ('t278');
5273                  ## Ignore the token                $i = $_;
5274                  !!!next-token;                last INSCOPE;
5275                  redo B;              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5276                }                !!!cp ('t279');
5277                  last INSCOPE;
5278                }
5279              } # INSCOPE
5280              unless (defined $i) {
5281                !!!cp ('t280');
5282                !!!parse-error (type => 'unmatched end tag:select', token => $token);
5283                ## Ignore the token
5284                !!!next-token;
5285                redo B;
5286              }
5287                                
5288                splice @{$self->{open_elements}}, $i;            !!!cp ('t281');
5289              splice @{$self->{open_elements}}, $i;
5290    
5291                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5292    
5293                !!!next-token;            if ($token->{tag_name} eq 'select') {
5294                redo B;              !!!cp ('t281.2');
5295                !!!next-token;
5296                redo B;
5297              } else {
5298                !!!cp ('t281.1');
5299                ## Reprocess the token.
5300                redo B;
5301              }
5302          } else {          } else {
5303            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!cp ('t282');
5304              !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);
5305            ## Ignore the token            ## Ignore the token
5306            !!!next-token;            !!!next-token;
5307            redo B;            redo B;
5308          }          }
5309        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5310              if ($token->{tag_name} eq 'optgroup') {          if ($token->{tag_name} eq 'optgroup') {
5311                if ($self->{open_elements}->[-1]->[1] eq 'option' and            if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
5312                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
5313                  ## As if </option>              !!!cp ('t283');
5314                  splice @{$self->{open_elements}}, -2;              ## As if </option>
5315                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {              splice @{$self->{open_elements}}, -2;
5316                  pop @{$self->{open_elements}};            } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5317                } else {              !!!cp ('t284');
5318                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              pop @{$self->{open_elements}};
5319                  ## Ignore the token            } else {
5320                }              !!!cp ('t285');
5321                !!!next-token;              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5322                redo B;              ## Ignore the token
5323              } elsif ($token->{tag_name} eq 'option') {            }
5324                if ($self->{open_elements}->[-1]->[1] eq 'option') {            !!!next-token;
5325                  pop @{$self->{open_elements}};            redo B;
5326                } else {          } elsif ($token->{tag_name} eq 'option') {
5327                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5328                  ## Ignore the token              !!!cp ('t286');
5329                }              pop @{$self->{open_elements}};
5330                !!!next-token;            } else {
5331                redo B;              !!!cp ('t287');
5332              } elsif ($token->{tag_name} eq 'select') {              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5333                ## have an element in table scope              ## Ignore the token
5334                my $i;            }
5335                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            !!!next-token;
5336                  my $node = $self->{open_elements}->[$_];            redo B;
5337                  if ($node->[1] eq $token->{tag_name}) {          } elsif ($token->{tag_name} eq 'select') {
5338                    $i = $_;            ## have an element in table scope
5339                    last INSCOPE;            my $i;
5340                  } elsif ({            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5341                            table => 1, html => 1,              my $node = $self->{open_elements}->[$_];
5342                           }->{$node->[1]}) {              if ($node->[1] & SELECT_EL) {
5343                    last INSCOPE;                !!!cp ('t288');
5344                  }                $i = $_;
5345                } # INSCOPE                last INSCOPE;
5346                unless (defined $i) {              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5347                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t289');
5348                  ## Ignore the token                last INSCOPE;
5349                  !!!next-token;              }
5350                  redo B;            } # INSCOPE
5351                }            unless (defined $i) {
5352                !!!cp ('t290');
5353                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5354                ## Ignore the token
5355                !!!next-token;
5356                redo B;
5357              }
5358                                
5359                splice @{$self->{open_elements}}, $i;            !!!cp ('t291');
5360              splice @{$self->{open_elements}}, $i;
5361    
5362                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5363    
5364                !!!next-token;            !!!next-token;
5365                redo B;            redo B;
5366              } elsif ({          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5367                        caption => 1, table => 1, tbody => 1,                   {
5368                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                    caption => 1, table => 1, tbody => 1,
5369                       }->{$token->{tag_name}}) {                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
5370                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                   }->{$token->{tag_name}}) {
5371    ## TODO: The following is wrong?
5372              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5373                                
5374                ## have an element in table scope            ## have an element in table scope
5375                my $i;            my $i;
5376                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5377                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
5378                  if ($node->[1] eq $token->{tag_name}) {              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5379                    $i = $_;                !!!cp ('t292');
5380                    last INSCOPE;                $i = $_;
5381                  } elsif ({                last INSCOPE;
5382                            table => 1, html => 1,              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5383                           }->{$node->[1]}) {                !!!cp ('t293');
5384                    last INSCOPE;                last INSCOPE;
5385                  }              }
5386                } # INSCOPE            } # INSCOPE
5387                unless (defined $i) {            unless (defined $i) {
5388                  ## Ignore the token              !!!cp ('t294');
5389                  !!!next-token;              ## Ignore the token
5390                  redo B;              !!!next-token;
5391                }              redo B;
5392              }
5393                                
5394                ## As if </select>            ## As if </select>
5395                ## have an element in table scope            ## have an element in table scope
5396                undef $i;            undef $i;
5397                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5398                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
5399                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
5400                    $i = $_;                !!!cp ('t295');
5401                    last INSCOPE;                $i = $_;
5402                  } elsif ({                last INSCOPE;
5403                            table => 1, html => 1,              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5404                           }->{$node->[1]}) {  ## ISSUE: Can this state be reached?
5405                    last INSCOPE;                !!!cp ('t296');
5406                  }                last INSCOPE;
5407                } # INSCOPE              }
5408                unless (defined $i) {            } # INSCOPE
5409                  !!!parse-error (type => 'unmatched end tag:select');            unless (defined $i) {
5410                  ## Ignore the </select> token              !!!cp ('t297');
5411                  !!!next-token; ## TODO: ok?  ## TODO: The following error type is correct?
5412                  redo B;              !!!parse-error (type => 'unmatched end tag:select', token => $token);
5413                }              ## Ignore the </select> token
5414                !!!next-token; ## TODO: ok?
5415                redo B;
5416              }
5417                                
5418                splice @{$self->{open_elements}}, $i;            !!!cp ('t298');
5419              splice @{$self->{open_elements}}, $i;
5420    
5421                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5422    
5423                ## reprocess            ## reprocess
5424                redo B;            redo B;
5425          } else {          } else {
5426            !!!parse-error (type => 'in select:/'.$token->{tag_name});            !!!cp ('t299');
5427              !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);
5428            ## Ignore the token            ## Ignore the token
5429            !!!next-token;            !!!next-token;
5430            redo B;            redo B;
5431          }          }
5432          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5433            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5434                    @{$self->{open_elements}} == 1) { # redundant, maybe
5435              !!!cp ('t299.1');
5436              !!!parse-error (type => 'in body:#eof', token => $token);
5437            } else {
5438              !!!cp ('t299.2');
5439            }
5440    
5441            ## Stop parsing.
5442            last B;
5443        } else {        } else {
5444          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5445        }        }
# Line 4253  sub _tree_construction_main ($) { Line 5453  sub _tree_construction_main ($) {
5453            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5454                        
5455            unless (length $token->{data}) {            unless (length $token->{data}) {
5456                !!!cp ('t300');
5457              !!!next-token;              !!!next-token;
5458              redo B;              redo B;
5459            }            }
5460          }          }
5461                    
5462          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5463            !!!parse-error (type => 'after html:#character');            !!!cp ('t301');
5464              !!!parse-error (type => 'after html:#character', token => $token);
5465    
5466            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
5467            } else {
5468              !!!cp ('t302');
5469          }          }
5470                    
5471          ## "after body" insertion mode          ## "after body" insertion mode
5472          !!!parse-error (type => 'after body:#character');          !!!parse-error (type => 'after body:#character', token => $token);
5473    
5474          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
5475          ## reprocess          ## reprocess
5476          redo B;          redo B;
5477        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5478          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5479            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!cp ('t303');
5480              !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
5481                        
5482            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
5483            } else {
5484              !!!cp ('t304');
5485          }          }
5486    
5487          ## "after body" insertion mode          ## "after body" insertion mode
5488          !!!parse-error (type => 'after body:'.$token->{tag_name});          !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);
5489    
5490          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
5491          ## reprocess          ## reprocess
5492          redo B;          redo B;
5493        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5494          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5495            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!cp ('t305');
5496              !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
5497                        
5498            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
5499            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
5500            } else {
5501              !!!cp ('t306');
5502          }          }
5503    
5504          ## "after body" insertion mode          ## "after body" insertion mode
5505          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
5506            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
5507              !!!parse-error (type => 'unmatched end tag:html');              !!!cp ('t307');
5508                !!!parse-error (type => 'unmatched end tag:html', token => $token);
5509              ## Ignore the token              ## Ignore the token
5510              !!!next-token;              !!!next-token;
5511              redo B;              redo B;
5512            } else {            } else {
5513                !!!cp ('t308');
5514              $self->{insertion_mode} = AFTER_HTML_BODY_IM;              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
5515              !!!next-token;              !!!next-token;
5516              redo B;              redo B;
5517            }            }
5518          } else {          } else {
5519            !!!parse-error (type => 'after body:/'.$token->{tag_name});            !!!cp ('t309');
5520              !!!parse-error (type => 'after body:/'.$token->{tag_name}, token => $token);
5521    
5522            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
5523            ## reprocess            ## reprocess
5524            redo B;            redo B;
5525          }          }
5526          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5527            !!!cp ('t309.2');
5528            ## Stop parsing
5529            last B;
5530        } else {        } else {
5531          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5532        }        }
# Line 4319  sub _tree_construction_main ($) { Line 5536  sub _tree_construction_main ($) {
5536            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5537                        
5538            unless (length $token->{data}) {            unless (length $token->{data}) {
5539                !!!cp ('t310');
5540              !!!next-token;              !!!next-token;
5541              redo B;              redo B;
5542            }            }
# Line 4326  sub _tree_construction_main ($) { Line 5544  sub _tree_construction_main ($) {
5544                    
5545          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
5546            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5547              !!!parse-error (type => 'in frameset:#character');              !!!cp ('t311');
5548                !!!parse-error (type => 'in frameset:#character', token => $token);
5549            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
5550              !!!parse-error (type => 'after frameset:#character');              !!!cp ('t312');
5551                !!!parse-error (type => 'after frameset:#character', token => $token);
5552            } else { # "after html frameset"            } else { # "after html frameset"
5553              !!!parse-error (type => 'after html:#character');              !!!cp ('t313');
5554                !!!parse-error (type => 'after html:#character', token => $token);
5555    
5556              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
5557              ## Reprocess in the "main" phase, "after frameset"...              ## Reprocess in the "after frameset" insertion mode.
5558              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#character', token => $token);
5559            }            }
5560                        
5561            ## Ignore the token.            ## Ignore the token.
5562            if (length $token->{data}) {            if (length $token->{data}) {
5563                !!!cp ('t314');
5564              ## reprocess the rest of characters              ## reprocess the rest of characters
5565            } else {            } else {
5566                !!!cp ('t315');
5567              !!!next-token;              !!!next-token;
5568            }            }
5569            redo B;            redo B;
# Line 4349  sub _tree_construction_main ($) { Line 5572  sub _tree_construction_main ($) {
5572          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
5573        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5574          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
5575            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!cp ('t316');
5576              !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
5577    
5578            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
5579            ## Process in the "main" phase, "after frameset" insertion mode...            ## Process in the "after frameset" insertion mode.
5580          }          } else {
5581              !!!cp ('t317');
5582            }
5583    
5584          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
5585              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
5586            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!cp ('t318');
5587              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5588            !!!next-token;            !!!next-token;
5589            redo B;            redo B;
5590          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
5591                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
5592            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!cp ('t319');
5593              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5594            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
5595            !!!next-token;            !!!next-token;
5596            redo B;            redo B;
5597          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
5598              !!!cp ('t320');
5599            ## NOTE: As if in body.            ## NOTE: As if in body.
5600            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);            $parse_rcdata->(CDATA_CONTENT_MODEL);
5601            redo B;            redo B;
5602          } else {          } else {
5603            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5604              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!cp ('t321');
5605                !!!parse-error (type => 'in frameset:'.$token->{tag_name}, token => $token);
5606            } else {            } else {
5607              !!!parse-error (type => 'after frameset:'.$token->{tag_name});              !!!cp ('t322');
5608                !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);
5609            }            }
5610            ## Ignore the token            ## Ignore the token
5611            !!!next-token;            !!!next-token;
# Line 4382  sub _tree_construction_main ($) { Line 5613  sub _tree_construction_main ($) {
5613          }          }
5614        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5615          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
5616            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!cp ('t323');
5617              !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
5618    
5619            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
5620            ## Process in the "main" phase, "after frameset" insertion mode...            ## Process in the "after frameset" insertion mode.
5621            } else {
5622              !!!cp ('t324');
5623          }          }
5624    
5625          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
5626              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
5627            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5628                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
5629              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t325');
5630                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5631              ## Ignore the token              ## Ignore the token
5632              !!!next-token;              !!!next-token;
5633            } else {            } else {
5634                !!!cp ('t326');
5635              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5636              !!!next-token;              !!!next-token;
5637            }            }
5638    
5639            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
5640                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
5641                !!!cp ('t327');
5642              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
5643              } else {
5644                !!!cp ('t328');
5645            }            }
5646            redo B;            redo B;
5647          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
5648                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
5649              !!!cp ('t329');
5650            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
5651            !!!next-token;            !!!next-token;
5652            redo B;            redo B;
5653          } else {          } else {
5654            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5655              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});              !!!cp ('t330');
5656                !!!parse-error (type => 'in frameset:/'.$token->{tag_name}, token => $token);
5657            } else {            } else {
5658              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});              !!!cp ('t331');
5659                !!!parse-error (type => 'after frameset:/'.$token->{tag_name}, token => $token);
5660            }            }
5661            ## Ignore the token            ## Ignore the token
5662            !!!next-token;            !!!next-token;
5663            redo B;            redo B;
5664          }          }
5665          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5666            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5667                    @{$self->{open_elements}} == 1) { # redundant, maybe
5668              !!!cp ('t331.1');
5669              !!!parse-error (type => 'in body:#eof', token => $token);
5670            } else {
5671              !!!cp ('t331.2');
5672            }
5673            
5674            ## Stop parsing
5675            last B;
5676        } else {        } else {
5677          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5678        }        }
# Line 4432  sub _tree_construction_main ($) { Line 5685  sub _tree_construction_main ($) {
5685      ## "in body" insertion mode      ## "in body" insertion mode
5686      if ($token->{type} == START_TAG_TOKEN) {      if ($token->{type} == START_TAG_TOKEN) {
5687        if ($token->{tag_name} eq 'script') {        if ($token->{tag_name} eq 'script') {
5688            !!!cp ('t332');
5689          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
5690          $script_start_tag->($insert);          $script_start_tag->();
5691          redo B;          redo B;
5692        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
5693            !!!cp ('t333');
5694          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
5695          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL);
5696          redo B;          redo B;
5697        } elsif ({        } elsif ({
5698                  base => 1, link => 1,                  base => 1, link => 1,
5699                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5700            !!!cp ('t334');
5701          ## 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
5702          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5703          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
5704          !!!next-token;          !!!next-token;
5705          redo B;          redo B;
5706        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
5707          ## 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
5708          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5709          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.
5710    
5711          unless ($self->{confident}) {          unless ($self->{confident}) {
5712            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) { ## TODO: And if supported
5713                !!!cp ('t335');
5714              $self->{change_encoding}              $self->{change_encoding}
5715                  ->($self, $token->{attributes}->{charset}->{value});                  ->($self, $token->{attributes}->{charset}->{value}, $token);
5716                
5717                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
5718                    ->set_user_data (manakai_has_reference =>
5719                                         $token->{attributes}->{charset}
5720                                             ->{has_reference});
5721            } elsif ($token->{attributes}->{content}) {            } elsif ($token->{attributes}->{content}) {
5722              ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.              ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
5723              if ($token->{attributes}->{content}->{value}              if ($token->{attributes}->{content}->{value}
5724                  =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
5725                        [\x09-\x0D\x20]*=
5726                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
5727                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
5728                  !!!cp ('t336');
5729                $self->{change_encoding}                $self->{change_encoding}
5730                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
5731                  $meta_el->[0]->get_attribute_node_ns (undef, 'content')
5732                      ->set_user_data (manakai_has_reference =>
5733                                           $token->{attributes}->{content}
5734                                                 ->{has_reference});
5735              }              }
5736            }            }
5737            } else {
5738              if ($token->{attributes}->{charset}) {
5739                !!!cp ('t337');
5740                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
5741                    ->set_user_data (manakai_has_reference =>
5742                                         $token->{attributes}->{charset}
5743                                             ->{has_reference});
5744              }
5745              if ($token->{attributes}->{content}) {
5746                !!!cp ('t338');
5747                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
5748                    ->set_user_data (manakai_has_reference =>
5749                                         $token->{attributes}->{content}
5750                                             ->{has_reference});
5751              }
5752          }          }
5753    
5754          !!!next-token;          !!!next-token;
5755          redo B;          redo B;
5756        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
5757          !!!parse-error (type => 'in body:title');          !!!cp ('t341');
5758          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
5759          $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]);  
           }  
         });  
5760          redo B;          redo B;
5761        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
5762          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body:body', token => $token);
5763                                
5764          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
5765              $self->{open_elements}->[1]->[1] ne 'body') {              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
5766              !!!cp ('t342');
5767            ## Ignore the token            ## Ignore the token
5768          } else {          } else {
5769            my $body_el = $self->{open_elements}->[1]->[0];            my $body_el = $self->{open_elements}->[1]->[0];
5770            for my $attr_name (keys %{$token->{attributes}}) {            for my $attr_name (keys %{$token->{attributes}}) {
5771              unless ($body_el->has_attribute_ns (undef, $attr_name)) {              unless ($body_el->has_attribute_ns (undef, $attr_name)) {
5772                  !!!cp ('t343');
5773                $body_el->set_attribute_ns                $body_el->set_attribute_ns
5774                  (undef, [undef, $attr_name],                  (undef, [undef, $attr_name],
5775                   $token->{attributes}->{$attr_name}->{value});                   $token->{attributes}->{$attr_name}->{value});
# Line 4501  sub _tree_construction_main ($) { Line 5780  sub _tree_construction_main ($) {
5780          redo B;          redo B;
5781        } elsif ({        } elsif ({
5782                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
5783                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1,
5784                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5785                  menu => 1, ol => 1, p => 1, ul => 1,                  menu => 1, ol => 1, p => 1, ul => 1,
5786                  pre => 1,                  pre => 1, listing => 1,
5787                    form => 1,
5788                    table => 1,
5789                    hr => 1,
5790                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5791            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
5792              !!!cp ('t350');
5793              !!!parse-error (type => 'in form:form', token => $token);
5794              ## Ignore the token
5795              !!!next-token;
5796              redo B;
5797            }
5798    
5799          ## has a p element in scope          ## has a p element in scope
5800          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5801            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
5802                !!!cp ('t344');
5803              !!!back-token;              !!!back-token;
5804              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
5805                          line => $token->{line}, column => $token->{column}};
5806              redo B;              redo B;
5807            } elsif ({            } elsif ($_->[1] & SCOPING_EL) {
5808                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t345');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
5809              last INSCOPE;              last INSCOPE;
5810            }            }
5811          } # INSCOPE          } # INSCOPE
5812                        
5813          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5814          if ($token->{tag_name} eq 'pre') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
5815            !!!next-token;            !!!next-token;
5816            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
5817              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
5818              unless (length $token->{data}) {              unless (length $token->{data}) {
5819                  !!!cp ('t346');
5820                !!!next-token;                !!!next-token;
5821                } else {
5822                  !!!cp ('t349');
5823              }              }
5824              } else {
5825                !!!cp ('t348');
5826            }            }
5827          } else {          } elsif ($token->{tag_name} eq 'form') {
5828              !!!cp ('t347.1');
5829              $self->{form_element} = $self->{open_elements}->[-1]->[0];
5830    
5831            !!!next-token;            !!!next-token;
5832          }          } elsif ($token->{tag_name} eq 'table') {
5833          redo B;            !!!cp ('t382');
5834        } elsif ($token->{tag_name} eq 'form') {            push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
5835          if (defined $self->{form_element}) {            
5836            !!!parse-error (type => 'in form:form');            $self->{insertion_mode} = IN_TABLE_IM;
5837            ## Ignore the token  
5838              !!!next-token;
5839            } elsif ($token->{tag_name} eq 'hr') {
5840              !!!cp ('t386');
5841              pop @{$self->{open_elements}};
5842            
5843            !!!next-token;            !!!next-token;
           redo B;  
5844          } else {          } else {
5845            ## 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];  
5846            !!!next-token;            !!!next-token;
           redo B;  
5847          }          }
       } 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;  
5848          redo B;          redo B;
5849        } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {        } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
5850          ## has a p element in scope          ## has a p element in scope
5851          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5852            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
5853                !!!cp ('t353');
5854              !!!back-token;              !!!back-token;
5855              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
5856                          line => $token->{line}, column => $token->{column}};
5857              redo B;              redo B;
5858            } elsif ({            } elsif ($_->[1] & SCOPING_EL) {
5859                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t354');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
5860              last INSCOPE;              last INSCOPE;
5861            }            }
5862          } # INSCOPE          } # INSCOPE
# Line 4623  sub _tree_construction_main ($) { Line 5864  sub _tree_construction_main ($) {
5864          ## Step 1          ## Step 1
5865          my $i = -1;          my $i = -1;
5866          my $node = $self->{open_elements}->[$i];          my $node = $self->{open_elements}->[$i];
5867            my $li_or_dtdd = {li => {li => 1},
5868                              dt => {dt => 1, dd => 1},
5869                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
5870          LI: {          LI: {
5871            ## Step 2            ## Step 2
5872            if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {            if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
5873              if ($i != -1) {              if ($i != -1) {
5874                !!!parse-error (type => 'end tag missing:'.                !!!cp ('t355');
5875                                $self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
5876                                  value => $self->{open_elements}->[-1]->[0]
5877                                      ->manakai_local_name,
5878                                  token => $token);
5879                } else {
5880                  !!!cp ('t356');
5881              }              }
5882              splice @{$self->{open_elements}}, $i;              splice @{$self->{open_elements}}, $i;
5883              last LI;              last LI;
5884              } else {
5885                !!!cp ('t357');
5886            }            }
5887                        
5888            ## Step 3            ## Step 3
5889            if (not $formatting_category->{$node->[1]} and            if (not ($node->[1] & FORMATTING_EL) and
5890                #not $phrasing_category->{$node->[1]} and                #not $phrasing_category->{$node->[1]} and
5891                ($special_category->{$node->[1]} or                ($node->[1] & SPECIAL_EL or
5892                 $scoping_category->{$node->[1]}) and                 $node->[1] & SCOPING_EL) and
5893                $node->[1] ne 'address' and $node->[1] ne 'div') {                not ($node->[1] & ADDRESS_EL) and
5894                  not ($node->[1] & DIV_EL)) {
5895                !!!cp ('t358');
5896              last LI;              last LI;
5897            }            }
5898                        
5899              !!!cp ('t359');
5900            ## Step 4            ## Step 4
5901            $i--;            $i--;
5902            $node = $self->{open_elements}->[$i];            $node = $self->{open_elements}->[$i];
5903            redo LI;            redo LI;
5904          } # LI          } # LI
5905                        
5906          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5907          !!!next-token;          !!!next-token;
5908          redo B;          redo B;
5909        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
5910          ## has a p element in scope          ## has a p element in scope
5911          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5912            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
5913                !!!cp ('t367');
5914              !!!back-token;              !!!back-token;
5915              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
5916                          line => $token->{line}, column => $token->{column}};
5917              redo B;              redo B;
5918            } elsif ({            } elsif ($_->[1] & SCOPING_EL) {
5919                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t368');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
5920              last INSCOPE;              last INSCOPE;
5921            }            }
5922          } # INSCOPE          } # INSCOPE
5923                        
5924          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5925                        
5926          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
5927                        
5928          !!!next-token;          !!!next-token;
5929          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;  
5930        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
5931          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
5932            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
5933            if ($node->[1] eq 'a') {            if ($node->[1] & A_EL) {
5934              !!!parse-error (type => 'in a:a');              !!!cp ('t371');
5935                !!!parse-error (type => 'in a:a', token => $token);
5936                            
5937              !!!back-token;              !!!back-token;
5938              $token = {type => END_TAG_TOKEN, tag_name => 'a'};              $token = {type => END_TAG_TOKEN, tag_name => 'a',
5939              $formatting_end_tag->($token->{tag_name});                        line => $token->{line}, column => $token->{column}};
5940                $formatting_end_tag->($token);
5941                            
5942              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
5943                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
5944                    !!!cp ('t372');
5945                  splice @$active_formatting_elements, $_, 1;                  splice @$active_formatting_elements, $_, 1;
5946                  last AFE2;                  last AFE2;
5947                }                }
5948              } # AFE2              } # AFE2
5949              OE: for (reverse 0..$#{$self->{open_elements}}) {              OE: for (reverse 0..$#{$self->{open_elements}}) {
5950                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
5951                    !!!cp ('t373');
5952                  splice @{$self->{open_elements}}, $_, 1;                  splice @{$self->{open_elements}}, $_, 1;
5953                  last OE;                  last OE;
5954                }                }
5955              } # OE              } # OE
5956              last AFE;              last AFE;
5957            } elsif ($node->[0] eq '#marker') {            } elsif ($node->[0] eq '#marker') {
5958                !!!cp ('t374');
5959              last AFE;              last AFE;
5960            }            }
5961          } # AFE          } # AFE
5962                        
5963          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5964    
5965          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5966          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
5967    
5968          !!!next-token;          !!!next-token;
5969          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;  
5970        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
5971          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5972    
5973          ## has a |nobr| element in scope          ## has a |nobr| element in scope
5974          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5975            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
5976            if ($node->[1] eq 'nobr') {            if ($node->[1] & NOBR_EL) {
5977              !!!parse-error (type => 'in nobr:nobr');              !!!cp ('t376');
5978                !!!parse-error (type => 'in nobr:nobr', token => $token);
5979              !!!back-token;              !!!back-token;
5980              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
5981                          line => $token->{line}, column => $token->{column}};
5982              redo B;              redo B;
5983            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
5984                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t377');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
5985              last INSCOPE;              last INSCOPE;
5986            }            }
5987          } # INSCOPE          } # INSCOPE
5988                    
5989          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5990          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
5991                    
5992          !!!next-token;          !!!next-token;
# Line 4793  sub _tree_construction_main ($) { Line 5995  sub _tree_construction_main ($) {
5995          ## has a button element in scope          ## has a button element in scope
5996          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5997            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
5998            if ($node->[1] eq 'button') {            if ($node->[1] & BUTTON_EL) {
5999              !!!parse-error (type => 'in button:button');              !!!cp ('t378');
6000                !!!parse-error (type => 'in button:button', token => $token);
6001              !!!back-token;              !!!back-token;
6002              $token = {type => END_TAG_TOKEN, tag_name => 'button'};              $token = {type => END_TAG_TOKEN, tag_name => 'button',
6003                          line => $token->{line}, column => $token->{column}};
6004              redo B;              redo B;
6005            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
6006                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t379');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6007              last INSCOPE;              last INSCOPE;
6008            }            }
6009          } # INSCOPE          } # INSCOPE
6010                        
6011          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6012                        
6013          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6014          push @$active_formatting_elements, ['#marker', ''];  
6015            ## TODO: associate with $self->{form_element} if defined
6016    
         !!!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});  
6017          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
6018            
         !!!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;  
             
6019          !!!next-token;          !!!next-token;
6020          redo B;          redo B;
6021        } elsif ({        } elsif ({
6022                  area => 1, basefont => 1, bgsound => 1, br => 1,                  xmp => 1,
6023                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                  iframe => 1,
6024                  image => 1,                  noembed => 1,
6025                    noframes => 1,
6026                    noscript => 0, ## TODO: 1 if scripting is enabled
6027                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6028          if ($token->{tag_name} eq 'image') {          if ($token->{tag_name} eq 'xmp') {
6029            !!!parse-error (type => 'image');            !!!cp ('t381');
6030            $token->{tag_name} = 'img';            $reconstruct_active_formatting_elements->($insert_to_current);
6031            } else {
6032              !!!cp ('t399');
6033          }          }
6034            ## NOTE: There is an "as if in body" code clone.
6035          ## 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;  
6036          redo B;          redo B;
6037        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
6038          !!!parse-error (type => 'isindex');          !!!parse-error (type => 'isindex', token => $token);
6039                    
6040          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
6041              !!!cp ('t389');
6042            ## Ignore the token            ## Ignore the token
6043            !!!next-token;            !!!next-token;
6044            redo B;            redo B;
# Line 4911  sub _tree_construction_main ($) { Line 6052  sub _tree_construction_main ($) {
6052            delete $at->{prompt};            delete $at->{prompt};
6053            my @tokens = (            my @tokens = (
6054                          {type => START_TAG_TOKEN, tag_name => 'form',                          {type => START_TAG_TOKEN, tag_name => 'form',
6055                           attributes => $form_attrs},                           attributes => $form_attrs,
6056                          {type => START_TAG_TOKEN, tag_name => 'hr'},                           line => $token->{line}, column => $token->{column}},
6057                          {type => START_TAG_TOKEN, tag_name => 'p'},                          {type => START_TAG_TOKEN, tag_name => 'hr',
6058                          {type => START_TAG_TOKEN, tag_name => 'label'},                           line => $token->{line}, column => $token->{column}},
6059                            {type => START_TAG_TOKEN, tag_name => 'p',
6060                             line => $token->{line}, column => $token->{column}},
6061                            {type => START_TAG_TOKEN, tag_name => 'label',
6062                             line => $token->{line}, column => $token->{column}},
6063                         );                         );
6064            if ($prompt_attr) {            if ($prompt_attr) {
6065              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};              !!!cp ('t390');
6066                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
6067                               #line => $token->{line}, column => $token->{column},
6068                              };
6069            } else {            } else {
6070                !!!cp ('t391');
6071              push @tokens, {type => CHARACTER_TOKEN,              push @tokens, {type => CHARACTER_TOKEN,
6072                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD                             data => 'This is a searchable index. Insert your search keywords here: ',
6073                               #line => $token->{line}, column => $token->{column},
6074                              }; # SHOULD
6075              ## TODO: make this configurable              ## TODO: make this configurable
6076            }            }
6077            push @tokens,            push @tokens,
6078                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
6079                             line => $token->{line}, column => $token->{column}},
6080                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
6081                          {type => END_TAG_TOKEN, tag_name => 'label'},                          {type => END_TAG_TOKEN, tag_name => 'label',
6082                          {type => END_TAG_TOKEN, tag_name => 'p'},                           line => $token->{line}, column => $token->{column}},
6083                          {type => START_TAG_TOKEN, tag_name => 'hr'},                          {type => END_TAG_TOKEN, tag_name => 'p',
6084                          {type => END_TAG_TOKEN, tag_name => 'form'};                           line => $token->{line}, column => $token->{column}},
6085                            {type => START_TAG_TOKEN, tag_name => 'hr',
6086                             line => $token->{line}, column => $token->{column}},
6087                            {type => END_TAG_TOKEN, tag_name => 'form',
6088                             line => $token->{line}, column => $token->{column}};
6089            $token = shift @tokens;            $token = shift @tokens;
6090            !!!back-token (@tokens);            !!!back-token (@tokens);
6091            redo B;            redo B;
# Line 4937  sub _tree_construction_main ($) { Line 6093  sub _tree_construction_main ($) {
6093        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
6094          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
6095          my $el;          my $el;
6096          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $token->{tag_name}, $token->{attributes}, $token);
6097                    
6098          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
6099          $self->{content_model} = RCDATA_CONTENT_MODEL;          $self->{content_model} = RCDATA_CONTENT_MODEL;
# Line 4950  sub _tree_construction_main ($) { Line 6106  sub _tree_construction_main ($) {
6106          if ($token->{type} == CHARACTER_TOKEN) {          if ($token->{type} == CHARACTER_TOKEN) {
6107            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
6108            unless (length $token->{data}) {            unless (length $token->{data}) {
6109                !!!cp ('t392');
6110              !!!next-token;              !!!next-token;
6111              } else {
6112                !!!cp ('t393');
6113            }            }
6114            } else {
6115              !!!cp ('t394');
6116          }          }
6117          while ($token->{type} == CHARACTER_TOKEN) {          while ($token->{type} == CHARACTER_TOKEN) {
6118              !!!cp ('t395');
6119            $text .= $token->{data};            $text .= $token->{data};
6120            !!!next-token;            !!!next-token;
6121          }          }
6122          if (length $text) {          if (length $text) {
6123              !!!cp ('t396');
6124            $el->manakai_append_text ($text);            $el->manakai_append_text ($text);
6125          }          }
6126                    
# Line 4965  sub _tree_construction_main ($) { Line 6128  sub _tree_construction_main ($) {
6128                    
6129          if ($token->{type} == END_TAG_TOKEN and          if ($token->{type} == END_TAG_TOKEN and
6130              $token->{tag_name} eq $tag_name) {              $token->{tag_name} eq $tag_name) {
6131              !!!cp ('t397');
6132            ## Ignore the token            ## Ignore the token
6133          } else {          } else {
6134            !!!parse-error (type => 'in RCDATA:#'.$token->{type});            !!!cp ('t398');
6135              !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
6136          }          }
6137          !!!next-token;          !!!next-token;
6138          redo B;          redo B;
6139        } 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 ({  
6140                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
6141                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
6142                  tbody => 1, td => 1, tfoot => 1, th => 1,                  tbody => 1, td => 1, tfoot => 1, th => 1,
6143                  thead => 1, tr => 1,                  thead => 1, tr => 1,
6144                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6145          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!cp ('t401');
6146            !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);
6147          ## Ignore the token          ## Ignore the token
6148          !!!next-token;          !!!next-token;
6149          redo B;          redo B;
6150                    
6151          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
6152        } else {        } else {
6153            if ($token->{tag_name} eq 'image') {
6154              !!!cp ('t384');
6155              !!!parse-error (type => 'image', token => $token);
6156              $token->{tag_name} = 'img';
6157            } else {
6158              !!!cp ('t385');
6159            }
6160    
6161            ## NOTE: There is an "as if <br>" code clone.
6162          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6163                    
6164          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6165    
6166            if ({
6167                 applet => 1, marquee => 1, object => 1,
6168                }->{$token->{tag_name}}) {
6169              !!!cp ('t380');
6170              push @$active_formatting_elements, ['#marker', ''];
6171            } elsif ({
6172                      b => 1, big => 1, em => 1, font => 1, i => 1,
6173                      s => 1, small => 1, strile => 1,
6174                      strong => 1, tt => 1, u => 1,
6175                     }->{$token->{tag_name}}) {
6176              !!!cp ('t375');
6177              push @$active_formatting_elements, $self->{open_elements}->[-1];
6178            } elsif ($token->{tag_name} eq 'input') {
6179              !!!cp ('t388');
6180              ## TODO: associate with $self->{form_element} if defined
6181              pop @{$self->{open_elements}};
6182            } elsif ({
6183                      area => 1, basefont => 1, bgsound => 1, br => 1,
6184                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
6185                      #image => 1,
6186                     }->{$token->{tag_name}}) {
6187              !!!cp ('t388.1');
6188              pop @{$self->{open_elements}};
6189            } elsif ($token->{tag_name} eq 'select') {
6190              ## TODO: associate with $self->{form_element} if defined
6191            
6192              if ($self->{insertion_mode} & TABLE_IMS or
6193                  $self->{insertion_mode} & BODY_TABLE_IMS or
6194                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
6195                !!!cp ('t400.1');
6196                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
6197              } else {
6198                !!!cp ('t400.2');
6199                $self->{insertion_mode} = IN_SELECT_IM;
6200              }
6201            } else {
6202              !!!cp ('t402');
6203            }
6204                    
6205          !!!next-token;          !!!next-token;
6206          redo B;          redo B;
6207        }        }
6208      } elsif ($token->{type} == END_TAG_TOKEN) {      } elsif ($token->{type} == END_TAG_TOKEN) {
6209        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
6210          if (@{$self->{open_elements}} > 1 and          ## has a |body| element in scope
6211              $self->{open_elements}->[1]->[1] eq 'body') {          my $i;
6212            for (@{$self->{open_elements}}) {          INSCOPE: {
6213              unless ({            for (reverse @{$self->{open_elements}}) {
6214                         dd => 1, dt => 1, li => 1, p => 1, td => 1,              if ($_->[1] & BODY_EL) {
6215                         th => 1, tr => 1, body => 1, html => 1,                !!!cp ('t405');
6216                       tbody => 1, tfoot => 1, thead => 1,                $i = $_;
6217                      }->{$_->[1]}) {                last INSCOPE;
6218                !!!parse-error (type => 'not closed:'.$_->[1]);              } elsif ($_->[1] & SCOPING_EL) {
6219                  !!!cp ('t405.1');
6220                  last;
6221              }              }
6222            }            }
6223    
6224            $self->{insertion_mode} = AFTER_BODY_IM;            !!!parse-error (type => 'start tag not allowed',
6225            !!!next-token;                            value => $token->{tag_name}, token => $token);
6226            redo B;            ## NOTE: Ignore the token.
         } else {  
           !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           ## Ignore the token  
6227            !!!next-token;            !!!next-token;
6228            redo B;            redo B;
6229            } # INSCOPE
6230    
6231            for (@{$self->{open_elements}}) {
6232              unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
6233                !!!cp ('t403');
6234                !!!parse-error (type => 'not closed',
6235                                value => $_->[0]->manakai_local_name,
6236                                token => $token);
6237                last;
6238              } else {
6239                !!!cp ('t404');
6240              }
6241          }          }
6242    
6243            $self->{insertion_mode} = AFTER_BODY_IM;
6244            !!!next-token;
6245            redo B;
6246        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
6247          if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {          ## TODO: Update this code.  It seems that the code below is not
6248            ## up-to-date, though it has same effect as speced.
6249            if (@{$self->{open_elements}} > 1 and
6250                $self->{open_elements}->[1]->[1] & BODY_EL) {
6251            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
6252            if ($self->{open_elements}->[-1]->[1] ne 'body') {            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
6253              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!cp ('t406');
6254                !!!parse-error (type => 'not closed',
6255                                value => $self->{open_elements}->[1]->[0]
6256                                    ->manakai_local_name,
6257                                token => $token);
6258              } else {
6259                !!!cp ('t407');
6260            }            }
6261            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6262            ## reprocess            ## reprocess
6263            redo B;            redo B;
6264          } else {          } else {
6265            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t408');
6266              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6267            ## Ignore the token            ## Ignore the token
6268            !!!next-token;            !!!next-token;
6269            redo B;            redo B;
# Line 5050  sub _tree_construction_main ($) { Line 6272  sub _tree_construction_main ($) {
6272                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6273                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1, listing => 1,
6274                  menu => 1, ol => 1, pre => 1, ul => 1,                  menu => 1, ol => 1, pre => 1, ul => 1,
                 p => 1,  
6275                  dd => 1, dt => 1, li => 1,                  dd => 1, dt => 1, li => 1,
6276                  button => 1, marquee => 1, object => 1,                  applet => 1, button => 1, marquee => 1, object => 1,
6277                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6278          ## has an element in scope          ## has an element in scope
6279          my $i;          my $i;
6280          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6281            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6282            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6283              ## 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;  
             }  
6284              $i = $_;              $i = $_;
6285              last INSCOPE unless $token->{tag_name} eq 'p';              last INSCOPE;
6286            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
6287                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t411');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6288              last INSCOPE;              last INSCOPE;
6289            }            }
6290          } # INSCOPE          } # INSCOPE
6291            
6292          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
6293            if (defined $i) {            !!!cp ('t413');
6294              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6295            } else {
6296              ## Step 1. generate implied end tags
6297              while ({
6298                      dd => ($token->{tag_name} ne 'dd'),
6299                      dt => ($token->{tag_name} ne 'dt'),
6300                      li => ($token->{tag_name} ne 'li'),
6301                      p => 1,
6302                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
6303                !!!cp ('t409');
6304                pop @{$self->{open_elements}};
6305              }
6306    
6307              ## Step 2.
6308              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6309                      ne $token->{tag_name}) {
6310                !!!cp ('t412');
6311                !!!parse-error (type => 'not closed',
6312                                value => $self->{open_elements}->[-1]->[0]
6313                                    ->manakai_local_name,
6314                                token => $token);
6315            } else {            } else {
6316              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t414');
6317            }            }
6318          }  
6319                      ## Step 3.
         if (defined $i) {  
6320            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6321          } elsif ($token->{tag_name} eq 'p') {  
6322            ## As if <p>, then reprocess the current token            ## Step 4.
6323            my $el;            $clear_up_to_marker->()
6324            !!!create-element ($el, 'p');                if {
6325            $insert->($el);                  applet => 1, button => 1, marquee => 1, object => 1,
6326                  }->{$token->{tag_name}};
6327          }          }
         $clear_up_to_marker->()  
           if {  
             button => 1, marquee => 1, object => 1,  
           }->{$token->{tag_name}};  
6328          !!!next-token;          !!!next-token;
6329          redo B;          redo B;
6330        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
6331            undef $self->{form_element};
6332    
6333          ## has an element in scope          ## has an element in scope
6334            my $i;
6335          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6336            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6337            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & FORM_EL) {
6338              ## generate implied end tags              !!!cp ('t418');
6339              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;  
             }  
6340              last INSCOPE;              last INSCOPE;
6341            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
6342                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t419');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6343              last INSCOPE;              last INSCOPE;
6344            }            }
6345          } # INSCOPE          } # INSCOPE
6346            
6347          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {          unless (defined $i) { # has an element in scope
6348            pop @{$self->{open_elements}};            !!!cp ('t421');
6349          } else {            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6350            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          } else {
6351              ## Step 1. generate implied end tags
6352              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
6353                !!!cp ('t417');
6354                pop @{$self->{open_elements}};
6355              }
6356              
6357              ## Step 2.
6358              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6359                      ne $token->{tag_name}) {
6360                !!!cp ('t417.1');
6361                !!!parse-error (type => 'not closed',
6362                                value => $self->{open_elements}->[-1]->[0]
6363                                    ->manakai_local_name,
6364                                token => $token);
6365              } else {
6366                !!!cp ('t420');
6367              }  
6368              
6369              ## Step 3.
6370              splice @{$self->{open_elements}}, $i;
6371          }          }
6372    
         undef $self->{form_element};  
6373          !!!next-token;          !!!next-token;
6374          redo B;          redo B;
6375        } elsif ({        } elsif ({
# Line 5146  sub _tree_construction_main ($) { Line 6379  sub _tree_construction_main ($) {
6379          my $i;          my $i;
6380          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6381            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6382            if ({            if ($node->[1] & HEADING_EL) {
6383                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,              !!!cp ('t423');
               }->{$node->[1]}) {  
             ## 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;  
             }  
6384              $i = $_;              $i = $_;
6385              last INSCOPE;              last INSCOPE;
6386            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
6387                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t424');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6388              last INSCOPE;              last INSCOPE;
6389            }            }
6390          } # INSCOPE          } # INSCOPE
6391            
6392          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
6393            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t425.1');
6394              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6395            } else {
6396              ## Step 1. generate implied end tags
6397              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
6398                !!!cp ('t422');
6399                pop @{$self->{open_elements}};
6400              }
6401              
6402              ## Step 2.
6403              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6404                      ne $token->{tag_name}) {
6405                !!!cp ('t425');
6406                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6407              } else {
6408                !!!cp ('t426');
6409              }
6410    
6411              ## Step 3.
6412              splice @{$self->{open_elements}}, $i;
6413          }          }
6414                    
6415          splice @{$self->{open_elements}}, $i if defined $i;          !!!next-token;
6416            redo B;
6417          } elsif ($token->{tag_name} eq 'p') {
6418            ## has an element in scope
6419            my $i;
6420            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6421              my $node = $self->{open_elements}->[$_];
6422              if ($node->[1] & P_EL) {
6423                !!!cp ('t410.1');
6424                $i = $_;
6425                last INSCOPE;
6426              } elsif ($node->[1] & SCOPING_EL) {
6427                !!!cp ('t411.1');
6428                last INSCOPE;
6429              }
6430            } # INSCOPE
6431    
6432            if (defined $i) {
6433              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6434                      ne $token->{tag_name}) {
6435                !!!cp ('t412.1');
6436                !!!parse-error (type => 'not closed',
6437                                value => $self->{open_elements}->[-1]->[0]
6438                                    ->manakai_local_name,
6439                                token => $token);
6440              } else {
6441                !!!cp ('t414.1');
6442              }
6443    
6444              splice @{$self->{open_elements}}, $i;
6445            } else {
6446              !!!cp ('t413.1');
6447              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6448    
6449              !!!cp ('t415.1');
6450              ## As if <p>, then reprocess the current token
6451              my $el;
6452              !!!create-element ($el, 'p',, $token);
6453              $insert->($el);
6454              ## NOTE: Not inserted into |$self->{open_elements}|.
6455            }
6456    
6457          !!!next-token;          !!!next-token;
6458          redo B;          redo B;
6459        } elsif ({        } elsif ({
# Line 5183  sub _tree_construction_main ($) { Line 6462  sub _tree_construction_main ($) {
6462                  nobr => 1, s => 1, small => 1, strile => 1,                  nobr => 1, s => 1, small => 1, strile => 1,
6463                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
6464                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6465          $formatting_end_tag->($token->{tag_name});          !!!cp ('t427');
6466            $formatting_end_tag->($token);
6467          redo B;          redo B;
6468        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
6469          !!!parse-error (type => 'unmatched end tag:br');          !!!cp ('t428');
6470            !!!parse-error (type => 'unmatched end tag:br', token => $token);
6471    
6472          ## As if <br>          ## As if <br>
6473          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6474                    
6475          my $el;          my $el;
6476          !!!create-element ($el, 'br');          !!!create-element ($el, 'br',, $token);
6477          $insert->($el);          $insert->($el);
6478                    
6479          ## Ignore the token.          ## Ignore the token.
# Line 5210  sub _tree_construction_main ($) { Line 6491  sub _tree_construction_main ($) {
6491                  table => 1, textarea => 1, wbr => 1,                  table => 1, textarea => 1, wbr => 1,
6492                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
6493                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6494          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t429');
6495            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6496          ## Ignore the token          ## Ignore the token
6497          !!!next-token;          !!!next-token;
6498          redo B;          redo B;
# Line 5224  sub _tree_construction_main ($) { Line 6506  sub _tree_construction_main ($) {
6506    
6507          ## Step 2          ## Step 2
6508          S2: {          S2: {
6509            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6510              ## Step 1              ## Step 1
6511              ## generate implied end tags              ## generate implied end tags
6512              if ({              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
6513                   dd => 1, dt => 1, li => 1, p => 1,                !!!cp ('t430');
6514                   td => 1, th => 1, tr => 1,                ## ISSUE: Can this case be reached?
6515                   tbody => 1, tfoot => 1, thead => 1,                pop @{$self->{open_elements}};
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
6516              }              }
6517                    
6518              ## Step 2              ## Step 2
6519              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6520                        ne $token->{tag_name}) {
6521                  !!!cp ('t431');
6522                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
6523                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
6524                                  value => $self->{open_elements}->[-1]->[0]
6525                                      ->manakai_local_name,
6526                                  token => $token);
6527                } else {
6528                  !!!cp ('t432');
6529              }              }
6530                            
6531              ## Step 3              ## Step 3
# Line 5251  sub _tree_construction_main ($) { Line 6535  sub _tree_construction_main ($) {
6535              last S2;              last S2;
6536            } else {            } else {
6537              ## Step 3              ## Step 3
6538              if (not $formatting_category->{$node->[1]} and              if (not ($node->[1] & FORMATTING_EL) and
6539                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
6540                  ($special_category->{$node->[1]} or                  ($node->[1] & SPECIAL_EL or
6541                   $scoping_category->{$node->[1]})) {                   $node->[1] & SCOPING_EL)) {
6542                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t433');
6543                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6544                ## Ignore the token                ## Ignore the token
6545                !!!next-token;                !!!next-token;
6546                last S2;                last S2;
6547              }              }
6548    
6549                !!!cp ('t434');
6550            }            }
6551                        
6552            ## Step 4            ## Step 4
# Line 5275  sub _tree_construction_main ($) { Line 6562  sub _tree_construction_main ($) {
6562      redo B;      redo B;
6563    } # B    } # B
6564    
   ## 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  
   
6565    ## Stop parsing # MUST    ## Stop parsing # MUST
6566        
6567    ## TODO: script stuffs    ## TODO: script stuffs
# Line 5321  sub set_inner_html ($$$) { Line 6603  sub set_inner_html ($$$) {
6603      my $p = $class->new;      my $p = $class->new;
6604      $p->{document} = $doc;      $p->{document} = $doc;
6605    
6606      ## Step 9 # MUST      ## Step 8 # MUST
6607      my $i = 0;      my $i = 0;
6608      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
6609      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
6610      $p->{set_next_input_character} = sub {      $p->{set_next_char} = sub {
6611        my $self = shift;        my $self = shift;
6612    
6613        pop @{$self->{prev_input_character}};        pop @{$self->{prev_char}};
6614        unshift @{$self->{prev_input_character}}, $self->{next_input_character};        unshift @{$self->{prev_char}}, $self->{next_char};
6615    
6616          $self->{next_char} = -1 and return if $i >= length $$s;
6617          $self->{next_char} = ord substr $$s, $i++, 1;
6618    
6619        $self->{next_input_character} = -1 and return if $i >= length $$s;        ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
6620        $self->{next_input_character} = ord substr $$s, $i++, 1;        $p->{column}++;
6621        $column++;  
6622          if ($self->{next_char} == 0x000A) { # LF
6623        if ($self->{next_input_character} == 0x000A) { # LF          $p->{line}++;
6624          $line++;          $p->{column} = 0;
6625          $column = 0;          !!!cp ('i1');
6626        } elsif ($self->{next_input_character} == 0x000D) { # CR        } elsif ($self->{next_char} == 0x000D) { # CR
6627          $i++ if substr ($$s, $i, 1) eq "\x0A";          $i++ if substr ($$s, $i, 1) eq "\x0A";
6628          $self->{next_input_character} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
6629          $line++;          $p->{line}++;
6630          $column = 0;          $p->{column} = 0;
6631        } elsif ($self->{next_input_character} > 0x10FFFF) {          !!!cp ('i2');
6632          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        } elsif ($self->{next_char} > 0x10FFFF) {
6633        } elsif ($self->{next_input_character} == 0x0000) { # NULL          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
6634            !!!cp ('i3');
6635          } elsif ($self->{next_char} == 0x0000) { # NULL
6636            !!!cp ('i4');
6637          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
6638          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
6639        }        }
6640      };      };
6641      $p->{prev_input_character} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];
6642      $p->{next_input_character} = -1;      $p->{next_char} = -1;
6643            
6644      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
6645        my (%opt) = @_;        my (%opt) = @_;
6646        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
6647          my $column = $opt{column};
6648          if (defined $opt{token} and defined $opt{token}->{line}) {
6649            $line = $opt{token}->{line};
6650            $column = $opt{token}->{column};
6651          }
6652          warn "Parse error ($opt{type}) at line $line column $column\n";
6653      };      };
6654      $p->{parse_error} = sub {      $p->{parse_error} = sub {
6655        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
6656      };      };
6657            
6658      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
6659      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
6660    
6661      ## Step 2      ## Step 2
6662      my $node_ln = $node->local_name;      my $node_ln = $node->manakai_local_name;
6663      $p->{content_model} = {      $p->{content_model} = {
6664        title => RCDATA_CONTENT_MODEL,        title => RCDATA_CONTENT_MODEL,
6665        textarea => RCDATA_CONTENT_MODEL,        textarea => RCDATA_CONTENT_MODEL,
# Line 5382  sub set_inner_html ($$$) { Line 6676  sub set_inner_html ($$$) {
6676          unless defined $p->{content_model};          unless defined $p->{content_model};
6677          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
6678    
6679      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
6680          ## TODO: Foreign element OK?
6681    
6682      ## Step 4      ## Step 3
6683      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
6684        ('http://www.w3.org/1999/xhtml', [undef, 'html']);        ('http://www.w3.org/1999/xhtml', [undef, 'html']);
6685    
6686      ## Step 5 # MUST      ## Step 4 # MUST
6687      $doc->append_child ($root);      $doc->append_child ($root);
6688    
6689      ## Step 6 # MUST      ## Step 5 # MUST
6690      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
6691    
6692      undef $p->{head_element};      undef $p->{head_element};
6693    
6694      ## Step 7 # MUST      ## Step 6 # MUST
6695      $p->_reset_insertion_mode;      $p->_reset_insertion_mode;
6696    
6697      ## Step 8 # MUST      ## Step 7 # MUST
6698      my $anode = $node;      my $anode = $node;
6699      AN: while (defined $anode) {      AN: while (defined $anode) {
6700        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
6701          my $nsuri = $anode->namespace_uri;          my $nsuri = $anode->namespace_uri;
6702          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
6703            if ($anode->local_name eq 'form') { ## TODO: case?            if ($anode->manakai_local_name eq 'form') {
6704                !!!cp ('i5');
6705              $p->{form_element} = $anode;              $p->{form_element} = $anode;
6706              last AN;              last AN;
6707            }            }
# Line 5414  sub set_inner_html ($$$) { Line 6710  sub set_inner_html ($$$) {
6710        $anode = $anode->parent_node;        $anode = $anode->parent_node;
6711      } # AN      } # AN
6712            
6713      ## Step 3 # MUST      ## Step 9 # MUST
     ## Step 10 # MUST  
6714      {      {
6715        my $self = $p;        my $self = $p;
6716        !!!next-token;        !!!next-token;
6717      }      }
6718      $p->_tree_construction_main;      $p->_tree_construction_main;
6719    
6720      ## Step 11 # MUST      ## Step 10 # MUST
6721      my @cn = @{$node->child_nodes};      my @cn = @{$node->child_nodes};
6722      for (@cn) {      for (@cn) {
6723        $node->remove_child ($_);        $node->remove_child ($_);
6724      }      }
6725      ## ISSUE: mutation events? read-only?      ## ISSUE: mutation events? read-only?
6726    
6727      ## Step 12 # MUST      ## Step 11 # MUST
6728      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
6729      for (@cn) {      for (@cn) {
6730        $this_doc->adopt_node ($_);        $this_doc->adopt_node ($_);
# Line 5438  sub set_inner_html ($$$) { Line 6733  sub set_inner_html ($$$) {
6733      ## ISSUE: mutation events?      ## ISSUE: mutation events?
6734    
6735      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
6736    
6737        delete $p->{parse_error}; # delete loop
6738    } else {    } else {
6739      die "$0: |set_inner_html| is not defined for node of type $nt";      die "$0: |set_inner_html| is not defined for node of type $nt";
6740    }    }

Legend:
Removed from v.1.64  
changed lines
  Added in v.1.123

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24