/[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.121 by wakaba, Thu Mar 20 08:04:58 2008 UTC revision 1.165 by wakaba, Sat Sep 13 07:51:33 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  ## TODO: Control charcters and noncharacters are not allowed (HTML5 revision 1263)  require IO::Handle;
12  ## TODO: 1252 parse error (revision 1264)  
13  ## TODO: 8859-11 = 874 (revision 1271)  my $HTML_NS = q<http://www.w3.org/1999/xhtml>;
14    my $MML_NS = q<http://www.w3.org/1998/Math/MathML>;
15  my $permitted_slash_tag_name = {  my $SVG_NS = q<http://www.w3.org/2000/svg>;
16    base => 1,  my $XLINK_NS = q<http://www.w3.org/1999/xlink>;
17    link => 1,  my $XML_NS = q<http://www.w3.org/XML/1998/namespace>;
18    meta => 1,  my $XMLNS_NS = q<http://www.w3.org/2000/xmlns/>;
19    hr => 1,  
20    br => 1,  sub A_EL () { 0b1 }
21    img => 1,  sub ADDRESS_EL () { 0b10 }
22    embed => 1,  sub BODY_EL () { 0b100 }
23    param => 1,  sub BUTTON_EL () { 0b1000 }
24    area => 1,  sub CAPTION_EL () { 0b10000 }
25    col => 1,  sub DD_EL () { 0b100000 }
26    input => 1,  sub DIV_EL () { 0b1000000 }
27    sub DT_EL () { 0b10000000 }
28    sub FORM_EL () { 0b100000000 }
29    sub FORMATTING_EL () { 0b1000000000 }
30    sub FRAMESET_EL () { 0b10000000000 }
31    sub HEADING_EL () { 0b100000000000 }
32    sub HTML_EL () { 0b1000000000000 }
33    sub LI_EL () { 0b10000000000000 }
34    sub NOBR_EL () { 0b100000000000000 }
35    sub OPTION_EL () { 0b1000000000000000 }
36    sub OPTGROUP_EL () { 0b10000000000000000 }
37    sub P_EL () { 0b100000000000000000 }
38    sub SELECT_EL () { 0b1000000000000000000 }
39    sub TABLE_EL () { 0b10000000000000000000 }
40    sub TABLE_CELL_EL () { 0b100000000000000000000 }
41    sub TABLE_ROW_EL () { 0b1000000000000000000000 }
42    sub TABLE_ROW_GROUP_EL () { 0b10000000000000000000000 }
43    sub MISC_SCOPING_EL () { 0b100000000000000000000000 }
44    sub MISC_SPECIAL_EL () { 0b1000000000000000000000000 }
45    sub FOREIGN_EL () { 0b10000000000000000000000000 }
46    sub FOREIGN_FLOW_CONTENT_EL () { 0b100000000000000000000000000 }
47    sub MML_AXML_EL () { 0b1000000000000000000000000000 }
48    sub RUBY_EL () { 0b10000000000000000000000000000 }
49    sub RUBY_COMPONENT_EL () { 0b100000000000000000000000000000 }
50    
51    sub TABLE_ROWS_EL () {
52      TABLE_EL |
53      TABLE_ROW_EL |
54      TABLE_ROW_GROUP_EL
55    }
56    
57    ## NOTE: Used in "generate implied end tags" algorithm.
58    ## NOTE: There is a code where a modified version of END_TAG_OPTIONAL_EL
59    ## is used in "generate implied end tags" implementation (search for the
60    ## function mae).
61    sub END_TAG_OPTIONAL_EL () {
62      DD_EL |
63      DT_EL |
64      LI_EL |
65      P_EL |
66      RUBY_COMPONENT_EL
67    }
68    
69    ## NOTE: Used in </body> and EOF algorithms.
70    sub ALL_END_TAG_OPTIONAL_EL () {
71      DD_EL |
72      DT_EL |
73      LI_EL |
74      P_EL |
75    
76      BODY_EL |
77      HTML_EL |
78      TABLE_CELL_EL |
79      TABLE_ROW_EL |
80      TABLE_ROW_GROUP_EL
81    }
82    
83    sub SCOPING_EL () {
84      BUTTON_EL |
85      CAPTION_EL |
86      HTML_EL |
87      TABLE_EL |
88      TABLE_CELL_EL |
89      MISC_SCOPING_EL
90    }
91    
92    sub TABLE_SCOPING_EL () {
93      HTML_EL |
94      TABLE_EL
95    }
96    
97    sub TABLE_ROWS_SCOPING_EL () {
98      HTML_EL |
99      TABLE_ROW_GROUP_EL
100    }
101    
102    sub TABLE_ROW_SCOPING_EL () {
103      HTML_EL |
104      TABLE_ROW_EL
105    }
106    
107    sub SPECIAL_EL () {
108      ADDRESS_EL |
109      BODY_EL |
110      DIV_EL |
111    
112      DD_EL |
113      DT_EL |
114      LI_EL |
115      P_EL |
116    
117      FORM_EL |
118      FRAMESET_EL |
119      HEADING_EL |
120      OPTION_EL |
121      OPTGROUP_EL |
122      SELECT_EL |
123      TABLE_ROW_EL |
124      TABLE_ROW_GROUP_EL |
125      MISC_SPECIAL_EL
126    }
127    
128    my $el_category = {
129      a => A_EL | FORMATTING_EL,
130      address => ADDRESS_EL,
131      applet => MISC_SCOPING_EL,
132      area => MISC_SPECIAL_EL,
133      b => FORMATTING_EL,
134      base => MISC_SPECIAL_EL,
135      basefont => MISC_SPECIAL_EL,
136      bgsound => MISC_SPECIAL_EL,
137      big => FORMATTING_EL,
138      blockquote => MISC_SPECIAL_EL,
139      body => BODY_EL,
140      br => MISC_SPECIAL_EL,
141      button => BUTTON_EL,
142      caption => CAPTION_EL,
143      center => MISC_SPECIAL_EL,
144      col => MISC_SPECIAL_EL,
145      colgroup => MISC_SPECIAL_EL,
146      dd => DD_EL,
147      dir => MISC_SPECIAL_EL,
148      div => DIV_EL,
149      dl => MISC_SPECIAL_EL,
150      dt => DT_EL,
151      em => FORMATTING_EL,
152      embed => MISC_SPECIAL_EL,
153      fieldset => MISC_SPECIAL_EL,
154      font => FORMATTING_EL,
155      form => FORM_EL,
156      frame => MISC_SPECIAL_EL,
157      frameset => FRAMESET_EL,
158      h1 => HEADING_EL,
159      h2 => HEADING_EL,
160      h3 => HEADING_EL,
161      h4 => HEADING_EL,
162      h5 => HEADING_EL,
163      h6 => HEADING_EL,
164      head => MISC_SPECIAL_EL,
165      hr => MISC_SPECIAL_EL,
166      html => HTML_EL,
167      i => FORMATTING_EL,
168      iframe => MISC_SPECIAL_EL,
169      img => MISC_SPECIAL_EL,
170      input => MISC_SPECIAL_EL,
171      isindex => MISC_SPECIAL_EL,
172      li => LI_EL,
173      link => MISC_SPECIAL_EL,
174      listing => MISC_SPECIAL_EL,
175      marquee => MISC_SCOPING_EL,
176      menu => MISC_SPECIAL_EL,
177      meta => MISC_SPECIAL_EL,
178      nobr => NOBR_EL | FORMATTING_EL,
179      noembed => MISC_SPECIAL_EL,
180      noframes => MISC_SPECIAL_EL,
181      noscript => MISC_SPECIAL_EL,
182      object => MISC_SCOPING_EL,
183      ol => MISC_SPECIAL_EL,
184      optgroup => OPTGROUP_EL,
185      option => OPTION_EL,
186      p => P_EL,
187      param => MISC_SPECIAL_EL,
188      plaintext => MISC_SPECIAL_EL,
189      pre => MISC_SPECIAL_EL,
190      rp => RUBY_COMPONENT_EL,
191      rt => RUBY_COMPONENT_EL,
192      ruby => RUBY_EL,
193      s => FORMATTING_EL,
194      script => MISC_SPECIAL_EL,
195      select => SELECT_EL,
196      small => FORMATTING_EL,
197      spacer => MISC_SPECIAL_EL,
198      strike => FORMATTING_EL,
199      strong => FORMATTING_EL,
200      style => MISC_SPECIAL_EL,
201      table => TABLE_EL,
202      tbody => TABLE_ROW_GROUP_EL,
203      td => TABLE_CELL_EL,
204      textarea => MISC_SPECIAL_EL,
205      tfoot => TABLE_ROW_GROUP_EL,
206      th => TABLE_CELL_EL,
207      thead => TABLE_ROW_GROUP_EL,
208      title => MISC_SPECIAL_EL,
209      tr => TABLE_ROW_EL,
210      tt => FORMATTING_EL,
211      u => FORMATTING_EL,
212      ul => MISC_SPECIAL_EL,
213      wbr => MISC_SPECIAL_EL,
214    };
215    
216    my $el_category_f = {
217      $MML_NS => {
218        'annotation-xml' => MML_AXML_EL,
219        mi => FOREIGN_FLOW_CONTENT_EL,
220        mo => FOREIGN_FLOW_CONTENT_EL,
221        mn => FOREIGN_FLOW_CONTENT_EL,
222        ms => FOREIGN_FLOW_CONTENT_EL,
223        mtext => FOREIGN_FLOW_CONTENT_EL,
224      },
225      $SVG_NS => {
226        foreignObject => FOREIGN_FLOW_CONTENT_EL,
227        desc => FOREIGN_FLOW_CONTENT_EL,
228        title => FOREIGN_FLOW_CONTENT_EL,
229      },
230      ## NOTE: In addition, FOREIGN_EL is set to non-HTML elements.
231    };
232    
233    my $svg_attr_name = {
234      attributename => 'attributeName',
235      attributetype => 'attributeType',
236      basefrequency => 'baseFrequency',
237      baseprofile => 'baseProfile',
238      calcmode => 'calcMode',
239      clippathunits => 'clipPathUnits',
240      contentscripttype => 'contentScriptType',
241      contentstyletype => 'contentStyleType',
242      diffuseconstant => 'diffuseConstant',
243      edgemode => 'edgeMode',
244      externalresourcesrequired => 'externalResourcesRequired',
245      filterres => 'filterRes',
246      filterunits => 'filterUnits',
247      glyphref => 'glyphRef',
248      gradienttransform => 'gradientTransform',
249      gradientunits => 'gradientUnits',
250      kernelmatrix => 'kernelMatrix',
251      kernelunitlength => 'kernelUnitLength',
252      keypoints => 'keyPoints',
253      keysplines => 'keySplines',
254      keytimes => 'keyTimes',
255      lengthadjust => 'lengthAdjust',
256      limitingconeangle => 'limitingConeAngle',
257      markerheight => 'markerHeight',
258      markerunits => 'markerUnits',
259      markerwidth => 'markerWidth',
260      maskcontentunits => 'maskContentUnits',
261      maskunits => 'maskUnits',
262      numoctaves => 'numOctaves',
263      pathlength => 'pathLength',
264      patterncontentunits => 'patternContentUnits',
265      patterntransform => 'patternTransform',
266      patternunits => 'patternUnits',
267      pointsatx => 'pointsAtX',
268      pointsaty => 'pointsAtY',
269      pointsatz => 'pointsAtZ',
270      preservealpha => 'preserveAlpha',
271      preserveaspectratio => 'preserveAspectRatio',
272      primitiveunits => 'primitiveUnits',
273      refx => 'refX',
274      refy => 'refY',
275      repeatcount => 'repeatCount',
276      repeatdur => 'repeatDur',
277      requiredextensions => 'requiredExtensions',
278      requiredfeatures => 'requiredFeatures',
279      specularconstant => 'specularConstant',
280      specularexponent => 'specularExponent',
281      spreadmethod => 'spreadMethod',
282      startoffset => 'startOffset',
283      stddeviation => 'stdDeviation',
284      stitchtiles => 'stitchTiles',
285      surfacescale => 'surfaceScale',
286      systemlanguage => 'systemLanguage',
287      tablevalues => 'tableValues',
288      targetx => 'targetX',
289      targety => 'targetY',
290      textlength => 'textLength',
291      viewbox => 'viewBox',
292      viewtarget => 'viewTarget',
293      xchannelselector => 'xChannelSelector',
294      ychannelselector => 'yChannelSelector',
295      zoomandpan => 'zoomAndPan',
296    };
297    
298    my $foreign_attr_xname = {
299      'xlink:actuate' => [$XLINK_NS, ['xlink', 'actuate']],
300      'xlink:arcrole' => [$XLINK_NS, ['xlink', 'arcrole']],
301      'xlink:href' => [$XLINK_NS, ['xlink', 'href']],
302      'xlink:role' => [$XLINK_NS, ['xlink', 'role']],
303      'xlink:show' => [$XLINK_NS, ['xlink', 'show']],
304      'xlink:title' => [$XLINK_NS, ['xlink', 'title']],
305      'xlink:type' => [$XLINK_NS, ['xlink', 'type']],
306      'xml:base' => [$XML_NS, ['xml', 'base']],
307      'xml:lang' => [$XML_NS, ['xml', 'lang']],
308      'xml:space' => [$XML_NS, ['xml', 'space']],
309      'xmlns' => [$XMLNS_NS, [undef, 'xmlns']],
310      'xmlns:xlink' => [$XMLNS_NS, ['xmlns', 'xlink']],
311  };  };
312    
313    ## ISSUE: xmlns:xlink="non-xlink-ns" is not an error.
314    
315  my $c1_entity_char = {  my $c1_entity_char = {
316    0x80 => 0x20AC,    0x80 => 0x20AC,
317    0x81 => 0xFFFD,    0x81 => 0xFFFD,
# Line 61  my $c1_entity_char = { Line 347  my $c1_entity_char = {
347    0x9F => 0x0178,    0x9F => 0x0178,
348  }; # $c1_entity_char  }; # $c1_entity_char
349    
 my $special_category = {  
   address => 1, area => 1, base => 1, basefont => 1, bgsound => 1,  
   blockquote => 1, body => 1, br => 1, center => 1, col => 1, colgroup => 1,  
   dd => 1, dir => 1, div => 1, dl => 1, dt => 1, embed => 1, fieldset => 1,  
   form => 1, frame => 1, frameset => 1, h1 => 1, h2 => 1, h3 => 1,  
   h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, iframe => 1, image => 1,  
   img => 1, input => 1, isindex => 1, li => 1, link => 1, listing => 1,  
   menu => 1, meta => 1, noembed => 1, noframes => 1, noscript => 1,  
   ol => 1, optgroup => 1, option => 1, p => 1, param => 1, plaintext => 1,  
   pre => 1, script => 1, select => 1, spacer => 1, style => 1, tbody => 1,  
   textarea => 1, tfoot => 1, thead => 1, title => 1, tr => 1, ul => 1, wbr => 1,  
 };  
 my $scoping_category = {  
   applet => 1, button => 1, caption => 1, html => 1, marquee => 1, object => 1,  
   table => 1, td => 1, th => 1,  
 };  
 my $formatting_category = {  
   a => 1, b => 1, big => 1, em => 1, font => 1, i => 1, nobr => 1,  
   s => 1, small => 1, strile => 1, strong => 1, tt => 1, u => 1,  
 };  
 # $phrasing_category: all other elements  
   
350  sub parse_byte_string ($$$$;$) {  sub parse_byte_string ($$$$;$) {
351      my $self = shift;
352      my $charset_name = shift;
353      open my $input, '<', ref $_[0] ? $_[0] : \($_[0]);
354      return $self->parse_byte_stream ($charset_name, $input, @_[1..$#_]);
355    } # parse_byte_string
356    
357    sub parse_byte_stream ($$$$;$$) {
358      # my ($self, $charset_name, $byte_stream, $doc, $onerror, $get_wrapper) = @_;
359    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
360    my $charset = shift;    my $charset_name = shift;
361    my $bytes_s = ref $_[0] ? $_[0] : \($_[0]);    my $byte_stream = $_[0];
362    my $s;  
363        my $onerror = $_[2] || sub {
364    if (defined $charset) {      my (%opt) = @_;
365      require Encode; ## TODO: decode(utf8) don't delete BOM      warn "Parse error ($opt{type})\n";
366      $s = \ (Encode::decode ($charset, $$bytes_s));    };
367      $self->{input_encoding} = lc $charset; ## TODO: normalize name    $self->{parse_error} = $onerror; # updated later by parse_char_string
368      $self->{confident} = 1;  
369    } else {    my $get_wrapper = $_[3] || sub ($) {
370      ## TODO: Implement HTML5 detection algorithm      return $_[0]; # $_[0] = byte stream handle, returned = arg to char handle
371      };
372    
373      ## HTML5 encoding sniffing algorithm
374      require Message::Charset::Info;
375      my $charset;
376      my $buffer;
377      my ($char_stream, $e_status);
378    
379      SNIFFING: {
380        ## NOTE: By setting |allow_fallback| option true when the
381        ## |get_decode_handle| method is invoked, we ignore what the HTML5
382        ## spec requires, i.e. unsupported encoding should be ignored.
383          ## TODO: We should not do this unless the parser is invoked
384          ## in the conformance checking mode, in which this behavior
385          ## would be useful.
386    
387        ## Step 1
388        if (defined $charset_name) {
389          $charset = Message::Charset::Info->get_by_html_name ($charset_name);
390              ## TODO: Is this ok?  Transfer protocol's parameter should be
391              ## interpreted in its semantics?
392    
393          ## ISSUE: Unsupported encoding is not ignored according to the spec.
394          ($char_stream, $e_status) = $charset->get_decode_handle
395              ($byte_stream, allow_error_reporting => 1,
396               allow_fallback => 1);
397          if ($char_stream) {
398            $self->{confident} = 1;
399            last SNIFFING;
400          } else {
401            ## TODO: unsupported error
402          }
403        }
404    
405        ## Step 2
406        my $byte_buffer = '';
407        for (1..1024) {
408          my $char = $byte_stream->getc;
409          last unless defined $char;
410          $byte_buffer .= $char;
411        } ## TODO: timeout
412    
413        ## Step 3
414        if ($byte_buffer =~ /^\xFE\xFF/) {
415          $charset = Message::Charset::Info->get_by_html_name ('utf-16be');
416          ($char_stream, $e_status) = $charset->get_decode_handle
417              ($byte_stream, allow_error_reporting => 1,
418               allow_fallback => 1, byte_buffer => \$byte_buffer);
419          $self->{confident} = 1;
420          last SNIFFING;
421        } elsif ($byte_buffer =~ /^\xFF\xFE/) {
422          $charset = Message::Charset::Info->get_by_html_name ('utf-16le');
423          ($char_stream, $e_status) = $charset->get_decode_handle
424              ($byte_stream, allow_error_reporting => 1,
425               allow_fallback => 1, byte_buffer => \$byte_buffer);
426          $self->{confident} = 1;
427          last SNIFFING;
428        } elsif ($byte_buffer =~ /^\xEF\xBB\xBF/) {
429          $charset = Message::Charset::Info->get_by_html_name ('utf-8');
430          ($char_stream, $e_status) = $charset->get_decode_handle
431              ($byte_stream, allow_error_reporting => 1,
432               allow_fallback => 1, byte_buffer => \$byte_buffer);
433          $self->{confident} = 1;
434          last SNIFFING;
435        }
436    
437        ## Step 4
438        ## TODO: <meta charset>
439    
440        ## Step 5
441        ## TODO: from history
442    
443        ## Step 6
444      require Whatpm::Charset::UniversalCharDet;      require Whatpm::Charset::UniversalCharDet;
445      $charset = Whatpm::Charset::UniversalCharDet->detect_byte_string      $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
446          (substr ($$bytes_s, 0, 1024));          ($byte_buffer);
447      $charset ||= 'windows-1252';      if (defined $charset_name) {
448      $s = \ (Encode::decode ($charset, $$bytes_s));        $charset = Message::Charset::Info->get_by_html_name ($charset_name);
449      $self->{input_encoding} = $charset;  
450          ## ISSUE: Unsupported encoding is not ignored according to the spec.
451          require Whatpm::Charset::DecodeHandle;
452          $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
453              ($byte_stream);
454          ($char_stream, $e_status) = $charset->get_decode_handle
455              ($buffer, allow_error_reporting => 1,
456               allow_fallback => 1, byte_buffer => \$byte_buffer);
457          if ($char_stream) {
458            $buffer->{buffer} = $byte_buffer;
459            !!!parse-error (type => 'sniffing:chardet',
460                            text => $charset_name,
461                            level => $self->{level}->{info},
462                            layer => 'encode',
463                            line => 1, column => 1);
464            $self->{confident} = 0;
465            last SNIFFING;
466          }
467        }
468    
469        ## Step 7: default
470        ## TODO: Make this configurable.
471        $charset = Message::Charset::Info->get_by_html_name ('windows-1252');
472            ## NOTE: We choose |windows-1252| here, since |utf-8| should be
473            ## detectable in the step 6.
474        require Whatpm::Charset::DecodeHandle;
475        $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
476            ($byte_stream);
477        ($char_stream, $e_status)
478            = $charset->get_decode_handle ($buffer,
479                                           allow_error_reporting => 1,
480                                           allow_fallback => 1,
481                                           byte_buffer => \$byte_buffer);
482        $buffer->{buffer} = $byte_buffer;
483        !!!parse-error (type => 'sniffing:default',
484                        text => 'windows-1252',
485                        level => $self->{level}->{info},
486                        line => 1, column => 1,
487                        layer => 'encode');
488      $self->{confident} = 0;      $self->{confident} = 0;
489      } # SNIFFING
490    
491      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
492        $self->{input_encoding} = $charset->get_iana_name; ## TODO: Should we set actual charset decoder's encoding name?
493        !!!parse-error (type => 'chardecode:fallback',
494                        #text => $self->{input_encoding},
495                        level => $self->{level}->{uncertain},
496                        line => 1, column => 1,
497                        layer => 'encode');
498      } elsif (not ($e_status &
499                    Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
500        $self->{input_encoding} = $charset->get_iana_name;
501        !!!parse-error (type => 'chardecode:no error',
502                        text => $self->{input_encoding},
503                        level => $self->{level}->{uncertain},
504                        line => 1, column => 1,
505                        layer => 'encode');
506      } else {
507        $self->{input_encoding} = $charset->get_iana_name;
508    }    }
509    
510    $self->{change_encoding} = sub {    $self->{change_encoding} = sub {
511      my $self = shift;      my $self = shift;
512      my $charset = lc shift;      $charset_name = shift;
513      my $token = shift;      my $token = shift;
     ## TODO: if $charset is supported  
     ## TODO: normalize charset name  
514    
515      ## "Change the encoding" algorithm:      $charset = Message::Charset::Info->get_by_html_name ($charset_name);
516        ($char_stream, $e_status) = $charset->get_decode_handle
517            ($byte_stream, allow_error_reporting => 1, allow_fallback => 1,
518             byte_buffer => \ $buffer->{buffer});
519        
520        if ($char_stream) { # if supported
521          ## "Change the encoding" algorithm:
522    
523      ## Step 1            ## Step 1    
524      if ($charset eq 'utf-16') { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?        if ($charset->{category} &
525        $charset = 'utf-8';            Message::Charset::Info::CHARSET_CATEGORY_UTF16 ()) {
526      }          $charset = Message::Charset::Info->get_by_html_name ('utf-8');
527            ($char_stream, $e_status) = $charset->get_decode_handle
528                ($byte_stream,
529                 byte_buffer => \ $buffer->{buffer});
530          }
531          $charset_name = $charset->get_iana_name;
532          
533          ## Step 2
534          if (defined $self->{input_encoding} and
535              $self->{input_encoding} eq $charset_name) {
536            !!!parse-error (type => 'charset label:matching',
537                            text => $charset_name,
538                            level => $self->{level}->{info});
539            $self->{confident} = 1;
540            return;
541          }
542    
543      ## Step 2        !!!parse-error (type => 'charset label detected',
544      if (defined $self->{input_encoding} and                        text => $self->{input_encoding},
545          $self->{input_encoding} eq $charset) {                        value => $charset_name,
546        $self->{confident} = 1;                        level => $self->{level}->{warn},
547        return;                        token => $token);
548          
549          ## Step 3
550          # if (can) {
551            ## change the encoding on the fly.
552            #$self->{confident} = 1;
553            #return;
554          # }
555          
556          ## Step 4
557          throw Whatpm::HTML::RestartParser ();
558      }      }
559      }; # $self->{change_encoding}
560    
561      !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.    my $char_onerror = sub {
562          ':'.$charset, level => 'w', token => $token);      my (undef, $type, %opt) = @_;
563        !!!parse-error (layer => 'encode',
564      ## Step 3                      %opt, type => $type,
565      # if (can) {                      line => $self->{line}, column => $self->{column} + 1);
566        ## change the encoding on the fly.      if ($opt{octets}) {
567        #$self->{confident} = 1;        ${$opt{octets}} = "\x{FFFD}"; # relacement character
568        #return;      }
569      # }    };
570    
571      ## Step 4    my $wrapped_char_stream = $get_wrapper->($char_stream);
572      throw Whatpm::HTML::RestartParser (charset => $charset);    $wrapped_char_stream->onerror ($char_onerror);
   }; # $self->{change_encoding}  
573    
574    my @args = @_; shift @args; # $s    my @args = @_; shift @args; # $s
575    my $return;    my $return;
576    try {    try {
577      $return = $self->parse_char_string ($s, @args);        $return = $self->parse_char_stream ($wrapped_char_stream, @args);  
578    } catch Whatpm::HTML::RestartParser with {    } catch Whatpm::HTML::RestartParser with {
579      my $charset = shift->{charset};      ## NOTE: Invoked after {change_encoding}.
580      $s = \ (Encode::decode ($charset, $$bytes_s));      
581      $self->{input_encoding} = $charset; ## TODO: normalize      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
582          $self->{input_encoding} = $charset->get_iana_name; ## TODO: Should we set actual charset decoder's encoding name?
583          !!!parse-error (type => 'chardecode:fallback',
584                          level => $self->{level}->{uncertain},
585                          #text => $self->{input_encoding},
586                          line => 1, column => 1,
587                          layer => 'encode');
588        } elsif (not ($e_status &
589                      Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
590          $self->{input_encoding} = $charset->get_iana_name;
591          !!!parse-error (type => 'chardecode:no error',
592                          text => $self->{input_encoding},
593                          level => $self->{level}->{uncertain},
594                          line => 1, column => 1,
595                          layer => 'encode');
596        } else {
597          $self->{input_encoding} = $charset->get_iana_name;
598        }
599      $self->{confident} = 1;      $self->{confident} = 1;
600      $return = $self->parse_char_string ($s, @args);  
601        $wrapped_char_stream = $get_wrapper->($char_stream);
602        $wrapped_char_stream->onerror ($char_onerror);
603    
604        $return = $self->parse_char_stream ($wrapped_char_stream, @args);
605    };    };
606    return $return;    return $return;
607  } # parse_byte_string  } # parse_byte_stream
608    
609  ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM  ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
610  ## and the HTML layer MUST ignore it.  However, we does strip BOM in  ## and the HTML layer MUST ignore it.  However, we does strip BOM in
# Line 163  sub parse_byte_string ($$$$;$) { Line 615  sub parse_byte_string ($$$$;$) {
615  ## such as |parse_byte_string| in this module, must ensure that it does  ## such as |parse_byte_string| in this module, must ensure that it does
616  ## strip the BOM and never strip any ZWNBSP.  ## strip the BOM and never strip any ZWNBSP.
617    
618  *parse_char_string = \&parse_string;  sub parse_char_string ($$$;$$) {
619      #my ($self, $s, $doc, $onerror, $get_wrapper) = @_;
620      my $self = shift;
621      require utf8;
622      my $s = ref $_[0] ? $_[0] : \($_[0]);
623      open my $input, '<' . (utf8::is_utf8 ($$s) ? ':utf8' : ''), $s;
624      if ($_[3]) {
625        $input = $_[3]->($input);
626      }
627      return $self->parse_char_stream ($input, @_[1..$#_]);
628    } # parse_char_string
629    *parse_string = \&parse_char_string; ## NOTE: Alias for backward compatibility.
630    
631  sub parse_string ($$$;$) {  sub parse_char_stream ($$$;$) {
632    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
633    my $s = ref $_[0] ? $_[0] : \($_[0]);    my $input = $_[0];
634    $self->{document} = $_[1];    $self->{document} = $_[1];
635    @{$self->{document}->child_nodes} = ();    @{$self->{document}->child_nodes} = ();
636    
# Line 186  sub parse_string ($$$;$) { Line 649  sub parse_string ($$$;$) {
649      pop @{$self->{prev_char}};      pop @{$self->{prev_char}};
650      unshift @{$self->{prev_char}}, $self->{next_char};      unshift @{$self->{prev_char}}, $self->{next_char};
651    
652      $self->{next_char} = -1 and return if $i >= length $$s;      my $char;
653      $self->{next_char} = ord substr $$s, $i++, 1;      if (defined $self->{next_next_char}) {
654          $char = $self->{next_next_char};
655          delete $self->{next_next_char};
656        } else {
657          $char = $input->getc;
658        }
659        $self->{next_char} = -1 and return unless defined $char;
660        $self->{next_char} = ord $char;
661    
662      ($self->{line_prev}, $self->{column_prev})      ($self->{line_prev}, $self->{column_prev})
663          = ($self->{line}, $self->{column});          = ($self->{line}, $self->{column});
664      $self->{column}++;      $self->{column}++;
665            
666      if ($self->{next_char} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
667          !!!cp ('j1');
668        $self->{line}++;        $self->{line}++;
669        $self->{column} = 0;        $self->{column} = 0;
670      } elsif ($self->{next_char} == 0x000D) { # CR      } elsif ($self->{next_char} == 0x000D) { # CR
671        $i++ if substr ($$s, $i, 1) eq "\x0A";        !!!cp ('j2');
672          my $next = $input->getc;
673          if (defined $next and $next ne "\x0A") {
674            $self->{next_next_char} = $next;
675          }
676        $self->{next_char} = 0x000A; # LF # MUST        $self->{next_char} = 0x000A; # LF # MUST
677        $self->{line}++;        $self->{line}++;
678        $self->{column} = 0;        $self->{column} = 0;
679      } elsif ($self->{next_char} > 0x10FFFF) {      } elsif ($self->{next_char} > 0x10FFFF) {
680          !!!cp ('j3');
681        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
682      } elsif ($self->{next_char} == 0x0000) { # NULL      } elsif ($self->{next_char} == 0x0000) { # NULL
683          !!!cp ('j4');
684        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
685        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
686        } elsif ($self->{next_char} <= 0x0008 or
687                 (0x000E <= $self->{next_char} and $self->{next_char} <= 0x001F) or
688                 (0x007F <= $self->{next_char} and $self->{next_char} <= 0x009F) or
689                 (0xD800 <= $self->{next_char} and $self->{next_char} <= 0xDFFF) or
690                 (0xFDD0 <= $self->{next_char} and $self->{next_char} <= 0xFDDF) or
691                 {
692                  0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
693                  0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
694                  0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
695                  0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
696                  0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
697                  0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
698                  0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
699                  0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
700                  0x10FFFE => 1, 0x10FFFF => 1,
701                 }->{$self->{next_char}}) {
702          !!!cp ('j5');
703          if ($self->{next_char} < 0x10000) {
704            !!!parse-error (type => 'control char',
705                            text => (sprintf 'U+%04X', $self->{next_char}));
706          } else {
707            !!!parse-error (type => 'control char',
708                            text => (sprintf 'U-%08X', $self->{next_char}));
709          }
710      }      }
711    };    };
712    $self->{prev_char} = [-1, -1, -1];    $self->{prev_char} = [-1, -1, -1];
# Line 229  sub parse_string ($$$;$) { Line 730  sub parse_string ($$$;$) {
730    delete $self->{parse_error}; # remove loop    delete $self->{parse_error}; # remove loop
731    
732    return $self->{document};    return $self->{document};
733  } # parse_string  } # parse_char_stream
734    
735  sub new ($) {  sub new ($) {
736    my $class = shift;    my $class = shift;
737    my $self = bless {}, $class;    my $self = bless {
738        level => {must => 'm',
739                  should => 's',
740                  warn => 'w',
741                  info => 'i',
742                  uncertain => 'u'},
743      }, $class;
744    $self->{set_next_char} = sub {    $self->{set_next_char} = sub {
745      $self->{next_char} = -1;      $self->{next_char} = -1;
746    };    };
# Line 295  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO Line 802  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO
802  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
803  sub BOGUS_DOCTYPE_STATE () { 32 }  sub BOGUS_DOCTYPE_STATE () { 32 }
804  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
805    sub SELF_CLOSING_START_TAG_STATE () { 34 }
806    sub CDATA_SECTION_STATE () { 35 }
807    sub MD_HYPHEN_STATE () { 36 } # "markup declaration open state" in the spec
808    sub MD_DOCTYPE_STATE () { 37 } # "markup declaration open state" in the spec
809    sub MD_CDATA_STATE () { 38 } # "markup declaration open state" in the spec
810    sub CDATA_PCDATA_CLOSE_TAG_STATE () { 39 } # "close tag open state" in the spec
811    sub CDATA_SECTION_MSE1_STATE () { 40 } # "CDATA section state" in the spec
812    sub CDATA_SECTION_MSE2_STATE () { 41 } # "CDATA section state" in the spec
813    
814  sub DOCTYPE_TOKEN () { 1 }  sub DOCTYPE_TOKEN () { 1 }
815  sub COMMENT_TOKEN () { 2 }  sub COMMENT_TOKEN () { 2 }
# Line 312  sub ROW_IMS ()        { 0b10000000 } Line 827  sub ROW_IMS ()        { 0b10000000 }
827  sub BODY_AFTER_IMS () { 0b100000000 }  sub BODY_AFTER_IMS () { 0b100000000 }
828  sub FRAME_IMS ()      { 0b1000000000 }  sub FRAME_IMS ()      { 0b1000000000 }
829  sub SELECT_IMS ()     { 0b10000000000 }  sub SELECT_IMS ()     { 0b10000000000 }
830    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
831        ## NOTE: "in foreign content" insertion mode is special; it is combined
832        ## with the secondary insertion mode.  In this parser, they are stored
833        ## together in the bit-or'ed form.
834    
835  ## NOTE: "initial" and "before html" insertion modes have no constants.  ## NOTE: "initial" and "before html" insertion modes have no constants.
836    
# Line 343  sub IN_COLUMN_GROUP_IM () { 0b10 } Line 862  sub IN_COLUMN_GROUP_IM () { 0b10 }
862  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
863    my $self = shift;    my $self = shift;
864    $self->{state} = DATA_STATE; # MUST    $self->{state} = DATA_STATE; # MUST
865      #$self->{state_keyword}; # initialized when used
866    $self->{content_model} = PCDATA_CONTENT_MODEL; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
867    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token};
868    undef $self->{current_attribute};    undef $self->{current_attribute};
869    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
870    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
871      delete $self->{self_closing};
872    $self->{char} = [];    $self->{char} = [];
873    # $self->{next_char}    # $self->{next_char}
874    !!!next-input-character;    !!!next-input-character;
# Line 368  sub _initialize_tokenizer ($) { Line 889  sub _initialize_tokenizer ($) {
889  ##        ->{value}  ##        ->{value}
890  ##        ->{has_reference} == 1 or 0  ##        ->{has_reference} == 1 or 0
891  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
892    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
893    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
894    ##     while the token is pushed back to the stack.
895    
896  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
897    
# Line 394  sub _initialize_tokenizer ($) { Line 918  sub _initialize_tokenizer ($) {
918    
919  sub _get_next_token ($) {  sub _get_next_token ($) {
920    my $self = shift;    my $self = shift;
921    
922      if ($self->{self_closing}) {
923        !!!parse-error (type => 'nestc', token => $self->{current_token});
924        ## NOTE: The |self_closing| flag is only set by start tag token.
925        ## In addition, when a start tag token is emitted, it is always set to
926        ## |current_token|.
927        delete $self->{self_closing};
928      }
929    
930    if (@{$self->{token}}) {    if (@{$self->{token}}) {
931        $self->{self_closing} = $self->{token}->[0]->{self_closing};
932      return shift @{$self->{token}};      return shift @{$self->{token}};
933    }    }
934    
# Line 574  sub _get_next_token ($) { Line 1108  sub _get_next_token ($) {
1108            redo A;            redo A;
1109          } else {          } else {
1110            !!!cp (23);            !!!cp (23);
1111            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago',
1112                              line => $self->{line_prev},
1113                              column => $self->{column_prev});
1114            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1115            ## reconsume            ## reconsume
1116    
# Line 589  sub _get_next_token ($) { Line 1125  sub _get_next_token ($) {
1125          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
1126        }        }
1127      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
1128          ## NOTE: The "close tag open state" in the spec is implemented as
1129          ## |CLOSE_TAG_OPEN_STATE| and |CDATA_PCDATA_CLOSE_TAG_STATE|.
1130    
1131        my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"        my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
1132        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1133          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
1134              $self->{state} = CDATA_PCDATA_CLOSE_TAG_STATE;
1135            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            $self->{state_keyword} = '';
1136            my @next_char;            ## Reconsume.
1137            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {            redo A;
             push @next_char, $self->{next_char};  
             my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);  
             my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;  
             if ($self->{next_char} == $c or $self->{next_char} == $C) {  
               !!!cp (24);  
               !!!next-input-character;  
               next TAGNAME;  
             } else {  
               !!!cp (25);  
               $self->{next_char} = shift @next_char; # reconsume  
               !!!back-next-input-character (@next_char);  
               $self->{state} = DATA_STATE;  
   
               !!!emit ({type => CHARACTER_TOKEN, data => '</',  
                         line => $l, column => $c,  
                        });  
     
               redo A;  
             }  
           }  
           push @next_char, $self->{next_char};  
         
           unless ($self->{next_char} == 0x0009 or # HT  
                   $self->{next_char} == 0x000A or # LF  
                   $self->{next_char} == 0x000B or # VT  
                   $self->{next_char} == 0x000C or # FF  
                   $self->{next_char} == 0x0020 or # SP  
                   $self->{next_char} == 0x003E or # >  
                   $self->{next_char} == 0x002F or # /  
                   $self->{next_char} == -1) {  
             !!!cp (26);  
             $self->{next_char} = shift @next_char; # reconsume  
             !!!back-next-input-character (@next_char);  
             $self->{state} = DATA_STATE;  
             !!!emit ({type => CHARACTER_TOKEN, data => '</',  
                       line => $l, column => $c,  
                      });  
             redo A;  
           } else {  
             !!!cp (27);  
             $self->{next_char} = shift @next_char;  
             !!!back-next-input-character (@next_char);  
             # and consume...  
           }  
1138          } else {          } else {
1139            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
1140              ## NOTE: See <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>.
1141            !!!cp (28);            !!!cp (28);
           # next-input-character is already done  
1142            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1143              ## Reconsume.
1144            !!!emit ({type => CHARACTER_TOKEN, data => '</',            !!!emit ({type => CHARACTER_TOKEN, data => '</',
1145                      line => $l, column => $c,                      line => $l, column => $c,
1146                     });                     });
1147            redo A;            redo A;
1148          }          }
1149        }        }
1150          
1151        if (0x0041 <= $self->{next_char} and        if (0x0041 <= $self->{next_char} and
1152            $self->{next_char} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
1153          !!!cp (29);          !!!cp (29);
# Line 698  sub _get_next_token ($) { Line 1194  sub _get_next_token ($) {
1194                                    line => $self->{line_prev}, # "<" of "</"                                    line => $self->{line_prev}, # "<" of "</"
1195                                    column => $self->{column_prev} - 1,                                    column => $self->{column_prev} - 1,
1196                                   };                                   };
1197          ## $self->{next_char} is intentionally left as is          ## NOTE: $self->{next_char} is intentionally left as is.
1198          redo A;          ## Although the "anything else" case of the spec not explicitly
1199            ## states that the next input character is to be reconsumed,
1200            ## it will be included to the |data| of the comment token
1201            ## generated from the bogus end tag, as defined in the
1202            ## "bogus comment state" entry.
1203            redo A;
1204          }
1205        } elsif ($self->{state} == CDATA_PCDATA_CLOSE_TAG_STATE) {
1206          my $ch = substr $self->{last_emitted_start_tag_name}, length $self->{state_keyword}, 1;
1207          if (length $ch) {
1208            my $CH = $ch;
1209            $ch =~ tr/a-z/A-Z/;
1210            my $nch = chr $self->{next_char};
1211            if ($nch eq $ch or $nch eq $CH) {
1212              !!!cp (24);
1213              ## Stay in the state.
1214              $self->{state_keyword} .= $nch;
1215              !!!next-input-character;
1216              redo A;
1217            } else {
1218              !!!cp (25);
1219              $self->{state} = DATA_STATE;
1220              ## Reconsume.
1221              !!!emit ({type => CHARACTER_TOKEN,
1222                        data => '</' . $self->{state_keyword},
1223                        line => $self->{line_prev},
1224                        column => $self->{column_prev} - 1 - length $self->{state_keyword},
1225                       });
1226              redo A;
1227            }
1228          } else { # after "<{tag-name}"
1229            unless ({
1230                     0x0009 => 1, # HT
1231                     0x000A => 1, # LF
1232                     0x000B => 1, # VT
1233                     0x000C => 1, # FF
1234                     0x0020 => 1, # SP
1235                     0x003E => 1, # >
1236                     0x002F => 1, # /
1237                     -1 => 1, # EOF
1238                    }->{$self->{next_char}}) {
1239              !!!cp (26);
1240              ## Reconsume.
1241              $self->{state} = DATA_STATE;
1242              !!!emit ({type => CHARACTER_TOKEN,
1243                        data => '</' . $self->{state_keyword},
1244                        line => $self->{line_prev},
1245                        column => $self->{column_prev} - 1 - length $self->{state_keyword},
1246                       });
1247              redo A;
1248            } else {
1249              !!!cp (27);
1250              $self->{current_token}
1251                  = {type => END_TAG_TOKEN,
1252                     tag_name => $self->{last_emitted_start_tag_name},
1253                     line => $self->{line_prev},
1254                     column => $self->{column_prev} - 1 - length $self->{state_keyword}};
1255              $self->{state} = TAG_NAME_STATE;
1256              ## Reconsume.
1257              redo A;
1258            }
1259        }        }
1260      } elsif ($self->{state} == TAG_NAME_STATE) {      } elsif ($self->{state} == TAG_NAME_STATE) {
1261        if ($self->{next_char} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
# Line 765  sub _get_next_token ($) { Line 1321  sub _get_next_token ($) {
1321    
1322          redo A;          redo A;
1323        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1324            !!!cp (42);
1325            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1326          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_char} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           !!!cp (42);  
           #  
         } else {  
           !!!cp (43);  
           !!!parse-error (type => 'nestc');  
         }  
         $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;  
         # next-input-character is already done  
1327          redo A;          redo A;
1328        } else {        } else {
1329          !!!cp (44);          !!!cp (44);
# Line 829  sub _get_next_token ($) { Line 1375  sub _get_next_token ($) {
1375          !!!next-input-character;          !!!next-input-character;
1376          redo A;          redo A;
1377        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1378            !!!cp (50);
1379            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1380          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_char} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           !!!cp (50);  
           #  
         } else {  
           !!!cp (51);  
           !!!parse-error (type => 'nestc');  
         }  
         ## Stay in the state  
         # next-input-character is already done  
1381          redo A;          redo A;
1382        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1383          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
# Line 889  sub _get_next_token ($) { Line 1425  sub _get_next_token ($) {
1425          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1426              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1427            !!!cp (57);            !!!cp (57);
1428            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name}, line => $self->{current_attribute}->{line}, column => $self->{current_attribute}->{column});            !!!parse-error (type => 'duplicate attribute', text => $self->{current_attribute}->{name}, line => $self->{current_attribute}->{line}, column => $self->{current_attribute}->{column});
1429            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1430          } else {          } else {
1431            !!!cp (58);            !!!cp (58);
# Line 942  sub _get_next_token ($) { Line 1478  sub _get_next_token ($) {
1478          !!!next-input-character;          !!!next-input-character;
1479          redo A;          redo A;
1480        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1481            !!!cp (64);
1482          $before_leave->();          $before_leave->();
1483            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1484          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_char} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           !!!cp (64);  
           #  
         } else {  
           !!!cp (65);  
           !!!parse-error (type => 'nestc');  
         }  
         $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;  
         # next-input-character is already done  
1485          redo A;          redo A;
1486        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1487          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
# Line 1042  sub _get_next_token ($) { Line 1568  sub _get_next_token ($) {
1568          !!!next-input-character;          !!!next-input-character;
1569          redo A;          redo A;
1570        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1571            !!!cp (77);
1572            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1573          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_char} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           !!!cp (77);  
           #  
         } else {  
           !!!cp (78);  
           !!!parse-error (type => 'nestc');  
           ## TODO: Different error type for <aa / bb> than <aa/>  
         }  
         $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;  
         # next-input-character is already done  
1574          redo A;          redo A;
1575        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1576          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
# Line 1081  sub _get_next_token ($) { Line 1596  sub _get_next_token ($) {
1596    
1597          redo A;          redo A;
1598        } else {        } else {
1599          !!!cp (82);          if ($self->{next_char} == 0x0022 or # "
1600                $self->{next_char} == 0x0027) { # '
1601              !!!cp (78);
1602              !!!parse-error (type => 'bad attribute name');
1603            } else {
1604              !!!cp (82);
1605            }
1606          $self->{current_attribute}          $self->{current_attribute}
1607              = {name => chr ($self->{next_char}),              = {name => chr ($self->{next_char}),
1608                 value => '',                 value => '',
# Line 1116  sub _get_next_token ($) { Line 1637  sub _get_next_token ($) {
1637          !!!next-input-character;          !!!next-input-character;
1638          redo A;          redo A;
1639        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1640            !!!parse-error (type => 'empty unquoted attribute value');
1641          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1642            !!!cp (87);            !!!cp (87);
1643            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
# Line 1388  sub _get_next_token ($) { Line 1910  sub _get_next_token ($) {
1910    
1911          redo A;          redo A;
1912        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1913            !!!cp (122);
1914            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1915          !!!next-input-character;          !!!next-input-character;
1916          if ($self->{next_char} == 0x003E and # >          redo A;
1917              $self->{current_token}->{type} == START_TAG_TOKEN and        } elsif ($self->{next_char} == -1) {
1918              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {          !!!parse-error (type => 'unclosed tag');
1919            # permitted slash          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1920            !!!cp (122);            !!!cp (122.3);
1921            #            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1922            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1923              if ($self->{current_token}->{attributes}) {
1924                !!!cp (122.1);
1925                !!!parse-error (type => 'end tag attribute');
1926              } else {
1927                ## NOTE: This state should never be reached.
1928                !!!cp (122.2);
1929              }
1930          } else {          } else {
1931            !!!cp (123);            die "$0: $self->{current_token}->{type}: Unknown token type";
           !!!parse-error (type => 'nestc');  
1932          }          }
1933          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = DATA_STATE;
1934          # next-input-character is already done          ## Reconsume.
1935            !!!emit ($self->{current_token}); # start tag or end tag
1936          redo A;          redo A;
1937        } else {        } else {
1938          !!!cp (124);          !!!cp ('124.1');
1939          !!!parse-error (type => 'no space between attributes');          !!!parse-error (type => 'no space between attributes');
1940          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1941          ## reconsume          ## reconsume
1942          redo A;          redo A;
1943        }        }
1944        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1945          if ($self->{next_char} == 0x003E) { # >
1946            if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1947              !!!cp ('124.2');
1948              !!!parse-error (type => 'nestc', token => $self->{current_token});
1949              ## TODO: Different type than slash in start tag
1950              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1951              if ($self->{current_token}->{attributes}) {
1952                !!!cp ('124.4');
1953                !!!parse-error (type => 'end tag attribute');
1954              } else {
1955                !!!cp ('124.5');
1956              }
1957              ## TODO: Test |<title></title/>|
1958            } else {
1959              !!!cp ('124.3');
1960              $self->{self_closing} = 1;
1961            }
1962    
1963            $self->{state} = DATA_STATE;
1964            !!!next-input-character;
1965    
1966            !!!emit ($self->{current_token}); # start tag or end tag
1967    
1968            redo A;
1969          } elsif ($self->{next_char} == -1) {
1970            !!!parse-error (type => 'unclosed tag');
1971            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1972              !!!cp (124.7);
1973              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1974            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1975              if ($self->{current_token}->{attributes}) {
1976                !!!cp (124.5);
1977                !!!parse-error (type => 'end tag attribute');
1978              } else {
1979                ## NOTE: This state should never be reached.
1980                !!!cp (124.6);
1981              }
1982            } else {
1983              die "$0: $self->{current_token}->{type}: Unknown token type";
1984            }
1985            $self->{state} = DATA_STATE;
1986            ## Reconsume.
1987            !!!emit ($self->{current_token}); # start tag or end tag
1988            redo A;
1989          } else {
1990            !!!cp ('124.4');
1991            !!!parse-error (type => 'nestc');
1992            ## TODO: This error type is wrong.
1993            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1994            ## Reconsume.
1995            redo A;
1996          }
1997      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1998        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1999                
# Line 1443  sub _get_next_token ($) { Line 2028  sub _get_next_token ($) {
2028        die "$0: _get_next_token: unexpected case [BC]";        die "$0: _get_next_token: unexpected case [BC]";
2029      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
2030        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
   
       my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1);  
   
       my @next_char;  
       push @next_char, $self->{next_char};  
2031                
2032        if ($self->{next_char} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2033            !!!cp (133);
2034            $self->{state} = MD_HYPHEN_STATE;
2035          !!!next-input-character;          !!!next-input-character;
2036          push @next_char, $self->{next_char};          redo A;
         if ($self->{next_char} == 0x002D) { # -  
           !!!cp (127);  
           $self->{current_token} = {type => COMMENT_TOKEN, data => '',  
                                     line => $l, column => $c,  
                                    };  
           $self->{state} = COMMENT_START_STATE;  
           !!!next-input-character;  
           redo A;  
         } else {  
           !!!cp (128);  
         }  
2037        } elsif ($self->{next_char} == 0x0044 or # D        } elsif ($self->{next_char} == 0x0044 or # D
2038                 $self->{next_char} == 0x0064) { # d                 $self->{next_char} == 0x0064) { # d
2039            ## ASCII case-insensitive.
2040            !!!cp (130);
2041            $self->{state} = MD_DOCTYPE_STATE;
2042            $self->{state_keyword} = chr $self->{next_char};
2043          !!!next-input-character;          !!!next-input-character;
2044          push @next_char, $self->{next_char};          redo A;
2045          if ($self->{next_char} == 0x004F or # O        } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
2046              $self->{next_char} == 0x006F) { # o                 $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
2047            !!!next-input-character;                 $self->{next_char} == 0x005B) { # [
2048            push @next_char, $self->{next_char};          !!!cp (135.4);                
2049            if ($self->{next_char} == 0x0043 or # C          $self->{state} = MD_CDATA_STATE;
2050                $self->{next_char} == 0x0063) { # c          $self->{state_keyword} = '[';
2051              !!!next-input-character;          !!!next-input-character;
2052              push @next_char, $self->{next_char};          redo A;
             if ($self->{next_char} == 0x0054 or # T  
                 $self->{next_char} == 0x0074) { # t  
               !!!next-input-character;  
               push @next_char, $self->{next_char};  
               if ($self->{next_char} == 0x0059 or # Y  
                   $self->{next_char} == 0x0079) { # y  
                 !!!next-input-character;  
                 push @next_char, $self->{next_char};  
                 if ($self->{next_char} == 0x0050 or # P  
                     $self->{next_char} == 0x0070) { # p  
                   !!!next-input-character;  
                   push @next_char, $self->{next_char};  
                   if ($self->{next_char} == 0x0045 or # E  
                       $self->{next_char} == 0x0065) { # e  
                     !!!cp (129);  
                     ## TODO: What a stupid code this is!  
                     $self->{state} = DOCTYPE_STATE;  
                     $self->{current_token} = {type => DOCTYPE_TOKEN,  
                                               quirks => 1,  
                                               line => $l, column => $c,  
                                              };  
                     !!!next-input-character;  
                     redo A;  
                   } else {  
                     !!!cp (130);  
                   }  
                 } else {  
                   !!!cp (131);  
                 }  
               } else {  
                 !!!cp (132);  
               }  
             } else {  
               !!!cp (133);  
             }  
           } else {  
             !!!cp (134);  
           }  
         } else {  
           !!!cp (135);  
         }  
2053        } else {        } else {
2054          !!!cp (136);          !!!cp (136);
2055        }        }
2056    
2057        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'bogus comment',
2058        $self->{next_char} = shift @next_char;                        line => $self->{line_prev},
2059        !!!back-next-input-character (@next_char);                        column => $self->{column_prev} - 1);
2060          ## Reconsume.
2061        $self->{state} = BOGUS_COMMENT_STATE;        $self->{state} = BOGUS_COMMENT_STATE;
2062        $self->{current_token} = {type => COMMENT_TOKEN, data => '',        $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2063                                  line => $l, column => $c,                                  line => $self->{line_prev},
2064                                    column => $self->{column_prev} - 1,
2065                                 };                                 };
2066        redo A;        redo A;
2067              } elsif ($self->{state} == MD_HYPHEN_STATE) {
2068        ## ISSUE: typos in spec: chacacters, is is a parse error        if ($self->{next_char} == 0x002D) { # -
2069        ## 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?          !!!cp (127);
2070            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2071                                      line => $self->{line_prev},
2072                                      column => $self->{column_prev} - 2,
2073                                     };
2074            $self->{state} = COMMENT_START_STATE;
2075            !!!next-input-character;
2076            redo A;
2077          } else {
2078            !!!cp (128);
2079            !!!parse-error (type => 'bogus comment',
2080                            line => $self->{line_prev},
2081                            column => $self->{column_prev} - 2);
2082            $self->{state} = BOGUS_COMMENT_STATE;
2083            ## Reconsume.
2084            $self->{current_token} = {type => COMMENT_TOKEN,
2085                                      data => '-',
2086                                      line => $self->{line_prev},
2087                                      column => $self->{column_prev} - 2,
2088                                     };
2089            redo A;
2090          }
2091        } elsif ($self->{state} == MD_DOCTYPE_STATE) {
2092          ## ASCII case-insensitive.
2093          if ($self->{next_char} == [
2094                undef,
2095                0x004F, # O
2096                0x0043, # C
2097                0x0054, # T
2098                0x0059, # Y
2099                0x0050, # P
2100              ]->[length $self->{state_keyword}] or
2101              $self->{next_char} == [
2102                undef,
2103                0x006F, # o
2104                0x0063, # c
2105                0x0074, # t
2106                0x0079, # y
2107                0x0070, # p
2108              ]->[length $self->{state_keyword}]) {
2109            !!!cp (131);
2110            ## Stay in the state.
2111            $self->{state_keyword} .= chr $self->{next_char};
2112            !!!next-input-character;
2113            redo A;
2114          } elsif ((length $self->{state_keyword}) == 6 and
2115                   ($self->{next_char} == 0x0045 or # E
2116                    $self->{next_char} == 0x0065)) { # e
2117            !!!cp (129);
2118            $self->{state} = DOCTYPE_STATE;
2119            $self->{current_token} = {type => DOCTYPE_TOKEN,
2120                                      quirks => 1,
2121                                      line => $self->{line_prev},
2122                                      column => $self->{column_prev} - 7,
2123                                     };
2124            !!!next-input-character;
2125            redo A;
2126          } else {
2127            !!!cp (132);        
2128            !!!parse-error (type => 'bogus comment',
2129                            line => $self->{line_prev},
2130                            column => $self->{column_prev} - 1 - length $self->{state_keyword});
2131            $self->{state} = BOGUS_COMMENT_STATE;
2132            ## Reconsume.
2133            $self->{current_token} = {type => COMMENT_TOKEN,
2134                                      data => $self->{state_keyword},
2135                                      line => $self->{line_prev},
2136                                      column => $self->{column_prev} - 1 - length $self->{state_keyword},
2137                                     };
2138            redo A;
2139          }
2140        } elsif ($self->{state} == MD_CDATA_STATE) {
2141          if ($self->{next_char} == {
2142                '[' => 0x0043, # C
2143                '[C' => 0x0044, # D
2144                '[CD' => 0x0041, # A
2145                '[CDA' => 0x0054, # T
2146                '[CDAT' => 0x0041, # A
2147              }->{$self->{state_keyword}}) {
2148            !!!cp (135.1);
2149            ## Stay in the state.
2150            $self->{state_keyword} .= chr $self->{next_char};
2151            !!!next-input-character;
2152            redo A;
2153          } elsif ($self->{state_keyword} eq '[CDATA' and
2154                   $self->{next_char} == 0x005B) { # [
2155            !!!cp (135.2);
2156            $self->{current_token} = {type => CHARACTER_TOKEN,
2157                                      data => '',
2158                                      line => $self->{line_prev},
2159                                      column => $self->{column_prev} - 7};
2160            $self->{state} = CDATA_SECTION_STATE;
2161            !!!next-input-character;
2162            redo A;
2163          } else {
2164            !!!cp (135.3);
2165            !!!parse-error (type => 'bogus comment',
2166                            line => $self->{line_prev},
2167                            column => $self->{column_prev} - 1 - length $self->{state_keyword});
2168            $self->{state} = BOGUS_COMMENT_STATE;
2169            ## Reconsume.
2170            $self->{current_token} = {type => COMMENT_TOKEN,
2171                                      data => $self->{state_keyword},
2172                                      line => $self->{line_prev},
2173                                      column => $self->{column_prev} - 1 - length $self->{state_keyword},
2174                                     };
2175            redo A;
2176          }
2177      } elsif ($self->{state} == COMMENT_START_STATE) {      } elsif ($self->{state} == COMMENT_START_STATE) {
2178        if ($self->{next_char} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2179          !!!cp (137);          !!!cp (137);
# Line 2115  sub _get_next_token ($) { Line 2758  sub _get_next_token ($) {
2758          redo A;          redo A;
2759        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2760          !!!cp (208);          !!!cp (208);
2761          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2762    
2763          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2764          !!!next-input-character;          !!!next-input-character;
# Line 2151  sub _get_next_token ($) { Line 2794  sub _get_next_token ($) {
2794          redo A;          redo A;
2795        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2796          !!!cp (212);          !!!cp (212);
2797          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2798    
2799          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2800          !!!next-input-character;          !!!next-input-character;
# Line 2199  sub _get_next_token ($) { Line 2842  sub _get_next_token ($) {
2842        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
2843          !!!cp (217);          !!!cp (217);
2844          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
   
2845          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2846          ## reconsume          ## reconsume
2847    
# Line 2240  sub _get_next_token ($) { Line 2882  sub _get_next_token ($) {
2882          !!!next-input-character;          !!!next-input-character;
2883          redo A;          redo A;
2884        }        }
2885        } elsif ($self->{state} == CDATA_SECTION_STATE) {
2886          ## NOTE: "CDATA section state" in the state is jointly implemented
2887          ## by three states, |CDATA_SECTION_STATE|, |CDATA_SECTION_MSE1_STATE|,
2888          ## and |CDATA_SECTION_MSE2_STATE|.
2889          
2890          if ($self->{next_char} == 0x005D) { # ]
2891            !!!cp (221.1);
2892            $self->{state} = CDATA_SECTION_MSE1_STATE;
2893            !!!next-input-character;
2894            redo A;
2895          } elsif ($self->{next_char} == -1) {
2896            $self->{state} = DATA_STATE;
2897            !!!next-input-character;
2898            if (length $self->{current_token}->{data}) { # character
2899              !!!cp (221.2);
2900              !!!emit ($self->{current_token}); # character
2901            } else {
2902              !!!cp (221.3);
2903              ## No token to emit. $self->{current_token} is discarded.
2904            }        
2905            redo A;
2906          } else {
2907            !!!cp (221.4);
2908            $self->{current_token}->{data} .= chr $self->{next_char};
2909            ## Stay in the state.
2910            !!!next-input-character;
2911            redo A;
2912          }
2913    
2914          ## ISSUE: "text tokens" in spec.
2915        } elsif ($self->{state} == CDATA_SECTION_MSE1_STATE) {
2916          if ($self->{next_char} == 0x005D) { # ]
2917            !!!cp (221.5);
2918            $self->{state} = CDATA_SECTION_MSE2_STATE;
2919            !!!next-input-character;
2920            redo A;
2921          } else {
2922            !!!cp (221.6);
2923            $self->{current_token}->{data} .= ']';
2924            $self->{state} = CDATA_SECTION_STATE;
2925            ## Reconsume.
2926            redo A;
2927          }
2928        } elsif ($self->{state} == CDATA_SECTION_MSE2_STATE) {
2929          if ($self->{next_char} == 0x003E) { # >
2930            $self->{state} = DATA_STATE;
2931            !!!next-input-character;
2932            if (length $self->{current_token}->{data}) { # character
2933              !!!cp (221.7);
2934              !!!emit ($self->{current_token}); # character
2935            } else {
2936              !!!cp (221.8);
2937              ## No token to emit. $self->{current_token} is discarded.
2938            }
2939            redo A;
2940          } elsif ($self->{next_char} == 0x005D) { # ]
2941            !!!cp (221.9); # character
2942            $self->{current_token}->{data} .= ']'; ## Add first "]" of "]]]".
2943            ## Stay in the state.
2944            !!!next-input-character;
2945            redo A;
2946          } else {
2947            !!!cp (221.11);
2948            $self->{current_token}->{data} .= ']]'; # character
2949            $self->{state} = CDATA_SECTION_STATE;
2950            ## Reconsume.
2951            redo A;
2952          }
2953      } else {      } else {
2954        die "$0: $self->{state}: Unknown state";        die "$0: $self->{state}: Unknown state";
2955      }      }
# Line 2307  sub _tokenize_attempt_to_consume_an_enti Line 3017  sub _tokenize_attempt_to_consume_an_enti
3017    
3018          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3019            !!!cp (1008);            !!!cp (1008);
3020            !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);            !!!parse-error (type => 'invalid character reference',
3021                              text => (sprintf 'U+%04X', $code),
3022                              line => $l, column => $c);
3023            $code = 0xFFFD;            $code = 0xFFFD;
3024          } elsif ($code > 0x10FFFF) {          } elsif ($code > 0x10FFFF) {
3025            !!!cp (1009);            !!!cp (1009);
3026            !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);            !!!parse-error (type => 'invalid character reference',
3027                              text => (sprintf 'U-%08X', $code),
3028                              line => $l, column => $c);
3029            $code = 0xFFFD;            $code = 0xFFFD;
3030          } elsif ($code == 0x000D) {          } elsif ($code == 0x000D) {
3031            !!!cp (1010);            !!!cp (1010);
# Line 2319  sub _tokenize_attempt_to_consume_an_enti Line 3033  sub _tokenize_attempt_to_consume_an_enti
3033            $code = 0x000A;            $code = 0x000A;
3034          } elsif (0x80 <= $code and $code <= 0x9F) {          } elsif (0x80 <= $code and $code <= 0x9F) {
3035            !!!cp (1011);            !!!cp (1011);
3036            !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);            !!!parse-error (type => 'C1 character reference', text => (sprintf 'U+%04X', $code), line => $l, column => $c);
3037            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
3038          }          }
3039    
# Line 2352  sub _tokenize_attempt_to_consume_an_enti Line 3066  sub _tokenize_attempt_to_consume_an_enti
3066    
3067        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3068          !!!cp (1015);          !!!cp (1015);
3069          !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);          !!!parse-error (type => 'invalid character reference',
3070                            text => (sprintf 'U+%04X', $code),
3071                            line => $l, column => $c);
3072          $code = 0xFFFD;          $code = 0xFFFD;
3073        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
3074          !!!cp (1016);          !!!cp (1016);
3075          !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);          !!!parse-error (type => 'invalid character reference',
3076                            text => (sprintf 'U-%08X', $code),
3077                            line => $l, column => $c);
3078          $code = 0xFFFD;          $code = 0xFFFD;
3079        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
3080          !!!cp (1017);          !!!cp (1017);
3081          !!!parse-error (type => 'CR character reference', line => $l, column => $c);          !!!parse-error (type => 'CR character reference',
3082                            line => $l, column => $c);
3083          $code = 0x000A;          $code = 0x000A;
3084        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
3085          !!!cp (1018);          !!!cp (1018);
3086          !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);          !!!parse-error (type => 'C1 character reference',
3087                            text => (sprintf 'U+%04X', $code),
3088                            line => $l, column => $c);
3089          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
3090        }        }
3091                
# Line 2390  sub _tokenize_attempt_to_consume_an_enti Line 3111  sub _tokenize_attempt_to_consume_an_enti
3111      require Whatpm::_NamedEntityList;      require Whatpm::_NamedEntityList;
3112      our $EntityChar;      our $EntityChar;
3113    
3114      while (length $entity_name < 10 and      while (length $entity_name < 30 and
3115             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
3116             ((0x0041 <= $self->{next_char} and # a             ((0x0041 <= $self->{next_char} and # a
3117               $self->{next_char} <= 0x005A) or # x               $self->{next_char} <= 0x005A) or # x
# Line 2462  sub _initialize_tree_constructor ($) { Line 3183  sub _initialize_tree_constructor ($) {
3183    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
3184    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
3185    $self->{document}->manakai_is_html (1); # MUST    $self->{document}->manakai_is_html (1); # MUST
3186      $self->{document}->set_user_data (manakai_source_line => 1);
3187      $self->{document}->set_user_data (manakai_source_column => 1);
3188  } # _initialize_tree_constructor  } # _initialize_tree_constructor
3189    
3190  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 2516  sub _tree_construction_initial ($) { Line 3239  sub _tree_construction_initial ($) {
3239        ## language.        ## language.
3240        my $doctype_name = $token->{name};        my $doctype_name = $token->{name};
3241        $doctype_name = '' unless defined $doctype_name;        $doctype_name = '' unless defined $doctype_name;
3242        $doctype_name =~ tr/a-z/A-Z/;        $doctype_name =~ tr/a-z/A-Z/; # ASCII case-insensitive
3243        if (not defined $token->{name} or # <!DOCTYPE>        if (not defined $token->{name} or # <!DOCTYPE>
           defined $token->{public_identifier} or  
3244            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
3245          !!!cp ('t1');          !!!cp ('t1');
3246          !!!parse-error (type => 'not HTML5', token => $token);          !!!parse-error (type => 'not HTML5', token => $token);
3247        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
3248          !!!cp ('t2');          !!!cp ('t2');
         ## ISSUE: ASCII case-insensitive? (in fact it does not matter)  
3249          !!!parse-error (type => 'not HTML5', token => $token);          !!!parse-error (type => 'not HTML5', token => $token);
3250          } elsif (defined $token->{public_identifier}) {
3251            if ($token->{public_identifier} eq 'XSLT-compat') {
3252              !!!cp ('t1.2');
3253              !!!parse-error (type => 'XSLT-compat', token => $token,
3254                              level => $self->{level}->{should});
3255            } else {
3256              !!!parse-error (type => 'not HTML5', token => $token);
3257            }
3258        } else {        } else {
3259          !!!cp ('t3');          !!!cp ('t3');
3260            #
3261        }        }
3262                
3263        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
3264          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3265          ## NOTE: Default value for both |public_id| and |system_id| attributes
3266          ## are empty strings, so that we don't set any value in missing cases.
3267        $doctype->public_id ($token->{public_identifier})        $doctype->public_id ($token->{public_identifier})
3268            if defined $token->{public_identifier};            if defined $token->{public_identifier};
3269        $doctype->system_id ($token->{system_identifier})        $doctype->system_id ($token->{system_identifier})
# Line 2546  sub _tree_construction_initial ($) { Line 3278  sub _tree_construction_initial ($) {
3278        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{public_identifier}) {
3279          my $pubid = $token->{public_identifier};          my $pubid = $token->{public_identifier};
3280          $pubid =~ tr/a-z/A-z/;          $pubid =~ tr/a-z/A-z/;
3281          if ({          my $prefix = [
3282            "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,            "+//SILMARIL//DTD HTML PRO V0R11 19970101//",
3283            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3284            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3285            "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 1//",
3286            "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 2//",
3287            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//",
3288            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//",
3289            "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT//",
3290            "-//IETF//DTD HTML 2.0//EN" => 1,            "-//IETF//DTD HTML 2.0//",
3291            "-//IETF//DTD HTML 2.1E//EN" => 1,            "-//IETF//DTD HTML 2.1E//",
3292            "-//IETF//DTD HTML 3.0//EN" => 1,            "-//IETF//DTD HTML 3.0//",
3293            "-//IETF//DTD HTML 3.0//EN//" => 1,            "-//IETF//DTD HTML 3.2 FINAL//",
3294            "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,            "-//IETF//DTD HTML 3.2//",
3295            "-//IETF//DTD HTML 3.2//EN" => 1,            "-//IETF//DTD HTML 3//",
3296            "-//IETF//DTD HTML 3//EN" => 1,            "-//IETF//DTD HTML LEVEL 0//",
3297            "-//IETF//DTD HTML LEVEL 0//EN" => 1,            "-//IETF//DTD HTML LEVEL 1//",
3298            "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,            "-//IETF//DTD HTML LEVEL 2//",
3299            "-//IETF//DTD HTML LEVEL 1//EN" => 1,            "-//IETF//DTD HTML LEVEL 3//",
3300            "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 0//",
3301            "-//IETF//DTD HTML LEVEL 2//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 1//",
3302            "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 2//",
3303            "-//IETF//DTD HTML LEVEL 3//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 3//",
3304            "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,            "-//IETF//DTD HTML STRICT//",
3305            "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,            "-//IETF//DTD HTML//",
3306            "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,            "-//METRIUS//DTD METRIUS PRESENTATIONAL//",
3307            "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//",
3308            "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//",
3309            "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//",
3310            "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//",
3311            "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//",
3312            "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//",
3313            "-//IETF//DTD HTML STRICT//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD HTML//",
3314            "-//IETF//DTD HTML STRICT//EN//2.0" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//",
3315            "-//IETF//DTD HTML STRICT//EN//3.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//",
3316            "-//IETF//DTD HTML//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//",
3317            "-//IETF//DTD HTML//EN//2.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//",
3318            "-//IETF//DTD HTML//EN//3.0" => 1,            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//",
3319            "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//",
3320            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//",
3321            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//",
3322            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//",
3323            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//",
3324            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,            "-//W3C//DTD HTML 3 1995-03-24//",
3325            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,            "-//W3C//DTD HTML 3.2 DRAFT//",
3326            "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,            "-//W3C//DTD HTML 3.2 FINAL//",
3327            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//W3C//DTD HTML 3.2//",
3328            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//W3C//DTD HTML 3.2S DRAFT//",
3329            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 FRAMESET//",
3330            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 TRANSITIONAL//",
3331            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMETNAL 19960712//",
3332            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMENTAL 970421//",
3333            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//W3C//DTD W3 HTML//",
3334            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//W3O//DTD W3 HTML 3.0//",
3335            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML 2.0//",
3336            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML//",
3337            "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,          ]; # $prefix
3338            "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,          my $match;
3339            "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,          for (@$prefix) {
3340            "-//W3C//DTD HTML 3.2//EN" => 1,            if (substr ($prefix, 0, length $_) eq $_) {
3341            "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,              $match = 1;
3342            "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,              last;
3343            "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,            }
3344            "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,          }
3345            "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,          if ($match or
3346            "-//W3C//DTD W3 HTML//EN" => 1,              $pubid eq "-//W3O//DTD W3 HTML STRICT 3.0//EN//" or
3347            "-//W3O//DTD W3 HTML 3.0//EN" => 1,              $pubid eq "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" or
3348            "-//W3O//DTD W3 HTML 3.0//EN//" => 1,              $pubid eq "HTML") {
           "-//W3O//DTD W3 HTML STRICT 3.0//EN//" => 1,  
           "-//WEBTECHS//DTD MOZILLA HTML 2.0//EN" => 1,  
           "-//WEBTECHS//DTD MOZILLA HTML//EN" => 1,  
           "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,  
           "HTML" => 1,  
         }->{$pubid}) {  
3349            !!!cp ('t5');            !!!cp ('t5');
3350            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3351          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD HTML 4.01 FRAMESET//] or
3352                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD HTML 4.01 TRANSITIONAL//]) {
3353            if (defined $token->{system_identifier}) {            if (defined $token->{system_identifier}) {
3354              !!!cp ('t6');              !!!cp ('t6');
3355              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
# Line 2631  sub _tree_construction_initial ($) { Line 3357  sub _tree_construction_initial ($) {
3357              !!!cp ('t7');              !!!cp ('t7');
3358              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
3359            }            }
3360          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD XHTML 1.0 FRAMESET//] or
3361                   $pubid eq "-//W3C//DTD XHTML 1.0 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD XHTML 1.0 TRANSITIONAL//]) {
3362            !!!cp ('t8');            !!!cp ('t8');
3363            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
3364          } else {          } else {
# Line 2645  sub _tree_construction_initial ($) { Line 3371  sub _tree_construction_initial ($) {
3371          my $sysid = $token->{system_identifier};          my $sysid = $token->{system_identifier};
3372          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
3373          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") {
3374            ## TODO: Check the spec: PUBLIC "(limited quirks)" "(quirks)"            ## NOTE: Ensure that |PUBLIC "(limited quirks)" "(quirks)"| is
3375              ## marked as quirks.
3376            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3377            !!!cp ('t11');            !!!cp ('t11');
3378          } else {          } else {
# Line 2668  sub _tree_construction_initial ($) { Line 3395  sub _tree_construction_initial ($) {
3395        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3396        ## Go to the "before html" insertion mode.        ## Go to the "before html" insertion mode.
3397        ## reprocess        ## reprocess
3398          !!!ack-later;
3399        return;        return;
3400      } elsif ($token->{type} == CHARACTER_TOKEN) {      } elsif ($token->{type} == CHARACTER_TOKEN) {
3401        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
# Line 2748  sub _tree_construction_root_element ($) Line 3476  sub _tree_construction_root_element ($)
3476        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
3477          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
3478            my $root_element;            my $root_element;
3479            !!!create-element ($root_element, $token->{tag_name}, $token->{attributes}, $token);            !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3480            $self->{document}->append_child ($root_element);            $self->{document}->append_child ($root_element);
3481            push @{$self->{open_elements}}, [$root_element, 'html'];            push @{$self->{open_elements}},
3482                  [$root_element, $el_category->{html}];
3483    
3484            if ($token->{attributes}->{manifest}) {            if ($token->{attributes}->{manifest}) {
3485              !!!cp ('t24');              !!!cp ('t24');
# Line 2765  sub _tree_construction_root_element ($) Line 3494  sub _tree_construction_root_element ($)
3494              $self->{application_cache_selection}->(undef);              $self->{application_cache_selection}->(undef);
3495            }            }
3496    
3497              !!!nack ('t25c');
3498    
3499            !!!next-token;            !!!next-token;
3500            return; ## Go to the "before head" insertion mode.            return; ## Go to the "before head" insertion mode.
3501          } else {          } else {
# Line 2781  sub _tree_construction_root_element ($) Line 3512  sub _tree_construction_root_element ($)
3512          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
3513        }        }
3514    
3515      my $root_element; !!!create-element ($root_element, 'html',, $token);      my $root_element;
3516        !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3517      $self->{document}->append_child ($root_element);      $self->{document}->append_child ($root_element);
3518      push @{$self->{open_elements}}, [$root_element, 'html'];      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3519    
3520      $self->{application_cache_selection}->(undef);      $self->{application_cache_selection}->(undef);
3521    
3522      ## NOTE: Reprocess the token.      ## NOTE: Reprocess the token.
3523        !!!ack-later;
3524      return; ## Go to the "before head" insertion mode.      return; ## Go to the "before head" insertion mode.
3525    
3526      ## ISSUE: There is an issue in the spec      ## ISSUE: There is an issue in the spec
# Line 2811  sub _reset_insertion_mode ($) { Line 3544  sub _reset_insertion_mode ($) {
3544        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3545          $last = 1;          $last = 1;
3546          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
3547            if ($self->{inner_html_node}->[1] eq 'td' or            !!!cp ('t28');
3548                $self->{inner_html_node}->[1] eq 'th') {            $node = $self->{inner_html_node};
3549              !!!cp ('t27');          } else {
3550              #            die "_reset_insertion_mode: t27";
           } else {  
             !!!cp ('t28');  
             $node = $self->{inner_html_node};  
           }  
3551          }          }
3552        }        }
3553              
3554        ## Step 4..13        ## Step 4..14
3555        my $new_mode = {        my $new_mode;
3556          if ($node->[1] & FOREIGN_EL) {
3557            !!!cp ('t28.1');
3558            ## NOTE: Strictly spaking, the line below only applies to MathML and
3559            ## SVG elements.  Currently the HTML syntax supports only MathML and
3560            ## SVG elements as foreigners.
3561            $new_mode = IN_BODY_IM | IN_FOREIGN_CONTENT_IM;
3562          } elsif ($node->[1] & TABLE_CELL_EL) {
3563            if ($last) {
3564              !!!cp ('t28.2');
3565              #
3566            } else {
3567              !!!cp ('t28.3');
3568              $new_mode = IN_CELL_IM;
3569            }
3570          } else {
3571            !!!cp ('t28.4');
3572            $new_mode = {
3573                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
3574                        ## NOTE: |option| and |optgroup| do not set                        ## NOTE: |option| and |optgroup| do not set
3575                        ## insertion mode to "in select" by themselves.                        ## insertion mode to "in select" by themselves.
                       td => IN_CELL_IM,  
                       th => IN_CELL_IM,  
3576                        tr => IN_ROW_IM,                        tr => IN_ROW_IM,
3577                        tbody => IN_TABLE_BODY_IM,                        tbody => IN_TABLE_BODY_IM,
3578                        thead => IN_TABLE_BODY_IM,                        thead => IN_TABLE_BODY_IM,
# Line 2839  sub _reset_insertion_mode ($) { Line 3583  sub _reset_insertion_mode ($) {
3583                        head => IN_BODY_IM, # not in head!                        head => IN_BODY_IM, # not in head!
3584                        body => IN_BODY_IM,                        body => IN_BODY_IM,
3585                        frameset => IN_FRAMESET_IM,                        frameset => IN_FRAMESET_IM,
3586                       }->{$node->[1]};                       }->{$node->[0]->manakai_local_name};
3587          }
3588        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3589                
3590        ## Step 14        ## Step 15
3591        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3592          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3593            !!!cp ('t29');            !!!cp ('t29');
3594            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
# Line 2857  sub _reset_insertion_mode ($) { Line 3602  sub _reset_insertion_mode ($) {
3602          !!!cp ('t31');          !!!cp ('t31');
3603        }        }
3604                
3605        ## Step 15        ## Step 16
3606        $self->{insertion_mode} = IN_BODY_IM and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
3607                
3608        ## Step 16        ## Step 17
3609        $i--;        $i--;
3610        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3611                
3612        ## Step 17        ## Step 18
3613        redo S3;        redo S3;
3614      } # S3      } # S3
3615    
# Line 2976  sub _tree_construction_main ($) { Line 3721  sub _tree_construction_main ($) {
3721      ## Step 1      ## Step 1
3722      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3723      my $el;      my $el;
3724      !!!create-element ($el, $start_tag_name, $token->{attributes}, $token);      !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3725    
3726      ## Step 2      ## Step 2
3727      $insert->($el);      $insert->($el);
# Line 2987  sub _tree_construction_main ($) { Line 3732  sub _tree_construction_main ($) {
3732    
3733      ## Step 4      ## Step 4
3734      my $text = '';      my $text = '';
3735        !!!nack ('t40.1');
3736      !!!next-token;      !!!next-token;
3737      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3738        !!!cp ('t40');        !!!cp ('t40');
# Line 3013  sub _tree_construction_main ($) { Line 3759  sub _tree_construction_main ($) {
3759        ## NOTE: An end-of-file token.        ## NOTE: An end-of-file token.
3760        if ($content_model_flag == CDATA_CONTENT_MODEL) {        if ($content_model_flag == CDATA_CONTENT_MODEL) {
3761          !!!cp ('t43');          !!!cp ('t43');
3762          !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);          !!!parse-error (type => 'in CDATA:#eof', token => $token);
3763        } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {        } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3764          !!!cp ('t44');          !!!cp ('t44');
3765          !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);          !!!parse-error (type => 'in RCDATA:#eof', token => $token);
3766        } else {        } else {
3767          die "$0: $content_model_flag in parse_rcdata";          die "$0: $content_model_flag in parse_rcdata";
3768        }        }
# Line 3026  sub _tree_construction_main ($) { Line 3772  sub _tree_construction_main ($) {
3772    
3773    my $script_start_tag = sub () {    my $script_start_tag = sub () {
3774      my $script_el;      my $script_el;
3775      !!!create-element ($script_el, 'script', $token->{attributes}, $token);      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
3776      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3777    
3778      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
3779      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
3780            
3781      my $text = '';      my $text = '';
3782        !!!nack ('t45.1');
3783      !!!next-token;      !!!next-token;
3784      while ($token->{type} == CHARACTER_TOKEN) {      while ($token->{type} == CHARACTER_TOKEN) {
3785        !!!cp ('t45');        !!!cp ('t45');
# Line 3052  sub _tree_construction_main ($) { Line 3799  sub _tree_construction_main ($) {
3799        ## Ignore the token        ## Ignore the token
3800      } else {      } else {
3801        !!!cp ('t48');        !!!cp ('t48');
3802        !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);        !!!parse-error (type => 'in CDATA:#eof', token => $token);
3803        ## ISSUE: And ignore?        ## ISSUE: And ignore?
3804        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3805      }      }
# Line 3090  sub _tree_construction_main ($) { Line 3837  sub _tree_construction_main ($) {
3837        my $formatting_element;        my $formatting_element;
3838        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3839        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3840          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3841              !!!cp ('t52');
3842              last AFE;
3843            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
3844                         eq $tag_name) {
3845            !!!cp ('t51');            !!!cp ('t51');
3846            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3847            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3848            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           !!!cp ('t52');  
           last AFE;  
3849          }          }
3850        } # AFE        } # AFE
3851        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3852          !!!cp ('t53');          !!!cp ('t53');
3853          !!!parse-error (type => 'unmatched end tag:'.$tag_name, token => $end_tag_token);          !!!parse-error (type => 'unmatched end tag', text => $tag_name, token => $end_tag_token);
3854          ## Ignore the token          ## Ignore the token
3855          !!!next-token;          !!!next-token;
3856          return;          return;
# Line 3119  sub _tree_construction_main ($) { Line 3867  sub _tree_construction_main ($) {
3867              last INSCOPE;              last INSCOPE;
3868            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3869              !!!cp ('t55');              !!!cp ('t55');
3870              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},              !!!parse-error (type => 'unmatched end tag',
3871                                text => $token->{tag_name},
3872                              token => $end_tag_token);                              token => $end_tag_token);
3873              ## Ignore the token              ## Ignore the token
3874              !!!next-token;              !!!next-token;
3875              return;              return;
3876            }            }
3877          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
                   applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
3878            !!!cp ('t56');            !!!cp ('t56');
3879            $in_scope = 0;            $in_scope = 0;
3880          }          }
3881        } # INSCOPE        } # INSCOPE
3882        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3883          !!!cp ('t57');          !!!cp ('t57');
3884          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},          !!!parse-error (type => 'unmatched end tag',
3885                            text => $token->{tag_name},
3886                          token => $end_tag_token);                          token => $end_tag_token);
3887          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3888          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
# Line 3143  sub _tree_construction_main ($) { Line 3890  sub _tree_construction_main ($) {
3890        }        }
3891        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3892          !!!cp ('t58');          !!!cp ('t58');
3893          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1],          !!!parse-error (type => 'not closed',
3894                            text => $self->{open_elements}->[-1]->[0]
3895                                ->manakai_local_name,
3896                          token => $end_tag_token);                          token => $end_tag_token);
3897        }        }
3898                
# Line 3152  sub _tree_construction_main ($) { Line 3901  sub _tree_construction_main ($) {
3901        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
3902        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3903          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3904          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
3905              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3906              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
3907               $scoping_category->{$node->[1]})) { ## Scoping is redundant, maybe               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
3908            !!!cp ('t59');            !!!cp ('t59');
3909            $furthest_block = $node;            $furthest_block = $node;
3910            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
# Line 3241  sub _tree_construction_main ($) { Line 3990  sub _tree_construction_main ($) {
3990        } # S7          } # S7  
3991                
3992        ## Step 8        ## Step 8
3993        if ({        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
            table => 1, tbody => 1, tfoot => 1, thead => 1, tr => 1,  
           }->{$common_ancestor_node->[1]}) {  
3994          my $foster_parent_element;          my $foster_parent_element;
3995          my $next_sibling;          my $next_sibling;
3996                           OE: for (reverse 0..$#{$self->{open_elements}}) {          OE: for (reverse 0..$#{$self->{open_elements}}) {
3997                             if ($self->{open_elements}->[$_]->[1] eq 'table') {            if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3998                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3999                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
4000                                 !!!cp ('t65.1');                                 !!!cp ('t65.1');
# Line 3320  sub _tree_construction_main ($) { Line 4067  sub _tree_construction_main ($) {
4067    
4068    my $insert_to_foster = sub {    my $insert_to_foster = sub {
4069      my $child = shift;      my $child = shift;
4070      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]}) {  
4071        # MUST        # MUST
4072        my $foster_parent_element;        my $foster_parent_element;
4073        my $next_sibling;        my $next_sibling;
4074                           OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
4075                             if ($self->{open_elements}->[$_]->[1] eq 'table') {          if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
4076                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4077                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
4078                                 !!!cp ('t70');                                 !!!cp ('t70');
# Line 3352  sub _tree_construction_main ($) { Line 4097  sub _tree_construction_main ($) {
4097      }      }
4098    }; # $insert_to_foster    }; # $insert_to_foster
4099    
4100    B: {    B: while (1) {
4101      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
4102        !!!cp ('t73');        !!!cp ('t73');
4103        !!!parse-error (type => 'DOCTYPE in the middle', token => $token);        !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
4104        ## Ignore the token        ## Ignore the token
4105        ## Stay in the phase        ## Stay in the phase
4106        !!!next-token;        !!!next-token;
4107        redo B;        next B;
4108      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
4109               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
4110        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4111          !!!cp ('t79');          !!!cp ('t79');
4112          !!!parse-error (type => 'after html:html', token => $token);          !!!parse-error (type => 'after html', text => 'html', token => $token);
4113          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
4114        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4115          !!!cp ('t80');          !!!cp ('t80');
4116          !!!parse-error (type => 'after html:html', token => $token);          !!!parse-error (type => 'after html', text => 'html', token => $token);
4117          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
4118        } else {        } else {
4119          !!!cp ('t81');          !!!cp ('t81');
# Line 3385  sub _tree_construction_main ($) { Line 4130  sub _tree_construction_main ($) {
4130               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
4131          }          }
4132        }        }
4133          !!!nack ('t84.1');
4134        !!!next-token;        !!!next-token;
4135        redo B;        next B;
4136      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
4137        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
4138        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
# Line 3400  sub _tree_construction_main ($) { Line 4146  sub _tree_construction_main ($) {
4146          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
4147        }        }
4148        !!!next-token;        !!!next-token;
4149        redo B;        next B;
4150      } elsif ($self->{insertion_mode} & HEAD_IMS) {      } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
4151          if ($token->{type} == CHARACTER_TOKEN) {
4152            !!!cp ('t87.1');
4153            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4154            !!!next-token;
4155            next B;
4156          } elsif ($token->{type} == START_TAG_TOKEN) {
4157            if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
4158                 $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
4159                not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
4160                ($token->{tag_name} eq 'svg' and
4161                 $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
4162              ## NOTE: "using the rules for secondary insertion mode"then"continue"
4163              !!!cp ('t87.2');
4164              #
4165            } elsif ({
4166                      b => 1, big => 1, blockquote => 1, body => 1, br => 1,
4167                      center => 1, code => 1, dd => 1, div => 1, dl => 1, dt => 1,
4168                      em => 1, embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1,
4169                      h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, i => 1,
4170                      img => 1, li => 1, listing => 1, menu => 1, meta => 1,
4171                      nobr => 1, ol => 1, p => 1, pre => 1, ruby => 1, s => 1,
4172                      small => 1, span => 1, strong => 1, strike => 1, sub => 1,
4173                      sup => 1, table => 1, tt => 1, u => 1, ul => 1, var => 1,
4174                     }->{$token->{tag_name}}) {
4175              !!!cp ('t87.2');
4176              !!!parse-error (type => 'not closed',
4177                              text => $self->{open_elements}->[-1]->[0]
4178                                  ->manakai_local_name,
4179                              token => $token);
4180    
4181              pop @{$self->{open_elements}}
4182                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4183    
4184              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4185              ## Reprocess.
4186              next B;
4187            } else {
4188              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
4189              my $tag_name = $token->{tag_name};
4190              if ($nsuri eq $SVG_NS) {
4191                $tag_name = {
4192                   altglyph => 'altGlyph',
4193                   altglyphdef => 'altGlyphDef',
4194                   altglyphitem => 'altGlyphItem',
4195                   animatecolor => 'animateColor',
4196                   animatemotion => 'animateMotion',
4197                   animatetransform => 'animateTransform',
4198                   clippath => 'clipPath',
4199                   feblend => 'feBlend',
4200                   fecolormatrix => 'feColorMatrix',
4201                   fecomponenttransfer => 'feComponentTransfer',
4202                   fecomposite => 'feComposite',
4203                   feconvolvematrix => 'feConvolveMatrix',
4204                   fediffuselighting => 'feDiffuseLighting',
4205                   fedisplacementmap => 'feDisplacementMap',
4206                   fedistantlight => 'feDistantLight',
4207                   feflood => 'feFlood',
4208                   fefunca => 'feFuncA',
4209                   fefuncb => 'feFuncB',
4210                   fefuncg => 'feFuncG',
4211                   fefuncr => 'feFuncR',
4212                   fegaussianblur => 'feGaussianBlur',
4213                   feimage => 'feImage',
4214                   femerge => 'feMerge',
4215                   femergenode => 'feMergeNode',
4216                   femorphology => 'feMorphology',
4217                   feoffset => 'feOffset',
4218                   fepointlight => 'fePointLight',
4219                   fespecularlighting => 'feSpecularLighting',
4220                   fespotlight => 'feSpotLight',
4221                   fetile => 'feTile',
4222                   feturbulence => 'feTurbulence',
4223                   foreignobject => 'foreignObject',
4224                   glyphref => 'glyphRef',
4225                   lineargradient => 'linearGradient',
4226                   radialgradient => 'radialGradient',
4227                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4228                   textpath => 'textPath',  
4229                }->{$tag_name} || $tag_name;
4230              }
4231    
4232              ## "adjust SVG attributes" (SVG only) - done in insert-element-f
4233    
4234              ## "adjust foreign attributes" - done in insert-element-f
4235    
4236              !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
4237    
4238              if ($self->{self_closing}) {
4239                pop @{$self->{open_elements}};
4240                !!!ack ('t87.3');
4241              } else {
4242                !!!cp ('t87.4');
4243              }
4244    
4245              !!!next-token;
4246              next B;
4247            }
4248          } elsif ($token->{type} == END_TAG_TOKEN) {
4249            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4250            !!!cp ('t87.5');
4251            #
4252          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4253            !!!cp ('t87.6');
4254            !!!parse-error (type => 'not closed',
4255                            text => $self->{open_elements}->[-1]->[0]
4256                                ->manakai_local_name,
4257                            token => $token);
4258    
4259            pop @{$self->{open_elements}}
4260                while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4261    
4262            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4263            ## Reprocess.
4264            next B;
4265          } else {
4266            die "$0: $token->{type}: Unknown token type";        
4267          }
4268        }
4269    
4270        if ($self->{insertion_mode} & HEAD_IMS) {
4271        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4272          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4273            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
# Line 3411  sub _tree_construction_main ($) { Line 4277  sub _tree_construction_main ($) {
4277              !!!cp ('t88.1');              !!!cp ('t88.1');
4278              ## Ignore the token.              ## Ignore the token.
4279              !!!next-token;              !!!next-token;
4280              redo B;              next B;
4281            }            }
4282            unless (length $token->{data}) {            unless (length $token->{data}) {
4283              !!!cp ('t88');              !!!cp ('t88');
4284              !!!next-token;              !!!next-token;
4285              redo B;              next B;
4286            }            }
4287          }          }
4288    
4289          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4290            !!!cp ('t89');            !!!cp ('t89');
4291            ## As if <head>            ## As if <head>
4292            !!!create-element ($self->{head_element}, 'head',, $token);            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4293            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4294            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            push @{$self->{open_elements}},
4295                  [$self->{head_element}, $el_category->{head}];
4296    
4297            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4298            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
# Line 3435  sub _tree_construction_main ($) { Line 4302  sub _tree_construction_main ($) {
4302            !!!cp ('t90');            !!!cp ('t90');
4303            ## As if </noscript>            ## As if </noscript>
4304            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4305            !!!parse-error (type => 'in noscript:#character', token => $token);            !!!parse-error (type => 'in noscript:#text', token => $token);
4306                        
4307            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4308            ## As if </head>            ## As if </head>
# Line 3451  sub _tree_construction_main ($) { Line 4318  sub _tree_construction_main ($) {
4318            !!!cp ('t92');            !!!cp ('t92');
4319          }          }
4320    
4321              ## "after head" insertion mode          ## "after head" insertion mode
4322              ## As if <body>          ## As if <body>
4323              !!!insert-element ('body',, $token);          !!!insert-element ('body',, $token);
4324              $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4325              ## reprocess          ## reprocess
4326              redo B;          next B;
4327            } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4328              if ($token->{tag_name} eq 'head') {          if ($token->{tag_name} eq 'head') {
4329                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4330                  !!!cp ('t93');              !!!cp ('t93');
4331                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes}, $token);              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4332                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              $self->{open_elements}->[-1]->[0]->append_child
4333                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];                  ($self->{head_element});
4334                  $self->{insertion_mode} = IN_HEAD_IM;              push @{$self->{open_elements}},
4335                  !!!next-token;                  [$self->{head_element}, $el_category->{head}];
4336                  redo B;              $self->{insertion_mode} = IN_HEAD_IM;
4337                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {              !!!nack ('t93.1');
4338                  !!!cp ('t94');              !!!next-token;
4339                  #              next B;
4340                } else {            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4341                  !!!cp ('t95');              !!!cp ('t93.2');
4342                  !!!parse-error (type => 'in head:head', token => $token); # or in head noscript              !!!parse-error (type => 'after head', text => 'head',
4343                  ## Ignore the token                              token => $token);
4344                  !!!next-token;              ## Ignore the token
4345                  redo B;              !!!nack ('t93.3');
4346                }              !!!next-token;
4347              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              next B;
4348                !!!cp ('t96');            } else {
4349                ## As if <head>              !!!cp ('t95');
4350                !!!create-element ($self->{head_element}, 'head',, $token);              !!!parse-error (type => 'in head:head',
4351                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                              token => $token); # or in head noscript
4352                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];              ## Ignore the token
4353                !!!nack ('t95.1');
4354                !!!next-token;
4355                next B;
4356              }
4357            } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4358              !!!cp ('t96');
4359              ## As if <head>
4360              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4361              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4362              push @{$self->{open_elements}},
4363                  [$self->{head_element}, $el_category->{head}];
4364    
4365                $self->{insertion_mode} = IN_HEAD_IM;            $self->{insertion_mode} = IN_HEAD_IM;
4366                ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4367              } else {          } else {
4368                !!!cp ('t97');            !!!cp ('t97');
4369              }          }
4370    
4371              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
4372                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4373                  !!!cp ('t98');                  !!!cp ('t98');
4374                  ## As if </noscript>                  ## As if </noscript>
4375                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4376                  !!!parse-error (type => 'in noscript:base', token => $token);                  !!!parse-error (type => 'in noscript', text => 'base',
4377                                    token => $token);
4378                                
4379                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4380                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
# Line 3506  sub _tree_construction_main ($) { Line 4385  sub _tree_construction_main ($) {
4385                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4386                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4387                  !!!cp ('t100');                  !!!cp ('t100');
4388                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4389                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4390                    push @{$self->{open_elements}},
4391                        [$self->{head_element}, $el_category->{head}];
4392                } else {                } else {
4393                  !!!cp ('t101');                  !!!cp ('t101');
4394                }                }
# Line 3515  sub _tree_construction_main ($) { Line 4396  sub _tree_construction_main ($) {
4396                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4397                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4398                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4399                  !!!nack ('t101.1');
4400                !!!next-token;                !!!next-token;
4401                redo B;                next B;
4402              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
4403                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4404                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4405                  !!!cp ('t102');                  !!!cp ('t102');
4406                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4407                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4408                    push @{$self->{open_elements}},
4409                        [$self->{head_element}, $el_category->{head}];
4410                } else {                } else {
4411                  !!!cp ('t103');                  !!!cp ('t103');
4412                }                }
# Line 3530  sub _tree_construction_main ($) { Line 4414  sub _tree_construction_main ($) {
4414                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4415                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4416                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4417                  !!!ack ('t103.1');
4418                !!!next-token;                !!!next-token;
4419                redo B;                next B;
4420              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
4421                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4422                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4423                  !!!cp ('t104');                  !!!cp ('t104');
4424                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4425                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4426                    push @{$self->{open_elements}},
4427                        [$self->{head_element}, $el_category->{head}];
4428                } else {                } else {
4429                  !!!cp ('t105');                  !!!cp ('t105');
4430                }                }
# Line 3545  sub _tree_construction_main ($) { Line 4432  sub _tree_construction_main ($) {
4432                my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4433    
4434                unless ($self->{confident}) {                unless ($self->{confident}) {
4435                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) {
4436                    !!!cp ('t106');                    !!!cp ('t106');
4437                      ## NOTE: Whether the encoding is supported or not is handled
4438                      ## in the {change_encoding} callback.
4439                    $self->{change_encoding}                    $self->{change_encoding}
4440                        ->($self, $token->{attributes}->{charset}->{value},                        ->($self, $token->{attributes}->{charset}->{value},
4441                           $token);                           $token);
# Line 3556  sub _tree_construction_main ($) { Line 4445  sub _tree_construction_main ($) {
4445                                             $token->{attributes}->{charset}                                             $token->{attributes}->{charset}
4446                                                 ->{has_reference});                                                 ->{has_reference});
4447                  } elsif ($token->{attributes}->{content}) {                  } elsif ($token->{attributes}->{content}) {
                   ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
4448                    if ($token->{attributes}->{content}->{value}                    if ($token->{attributes}->{content}->{value}
4449                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                        =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4450                            [\x09-\x0D\x20]*=                            [\x09-\x0D\x20]*=
4451                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4452                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
4453                      !!!cp ('t107');                      !!!cp ('t107');
4454                        ## NOTE: Whether the encoding is supported or not is handled
4455                        ## in the {change_encoding} callback.
4456                      $self->{change_encoding}                      $self->{change_encoding}
4457                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4458                             $token);                             $token);
# Line 3593  sub _tree_construction_main ($) { Line 4483  sub _tree_construction_main ($) {
4483    
4484                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4485                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4486                  !!!ack ('t110.1');
4487                !!!next-token;                !!!next-token;
4488                redo B;                next B;
4489              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
4490                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4491                  !!!cp ('t111');                  !!!cp ('t111');
4492                  ## As if </noscript>                  ## As if </noscript>
4493                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4494                  !!!parse-error (type => 'in noscript:title', token => $token);                  !!!parse-error (type => 'in noscript', text => 'title',
4495                                    token => $token);
4496                                
4497                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4498                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4499                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4500                  !!!cp ('t112');                  !!!cp ('t112');
4501                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4502                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4503                    push @{$self->{open_elements}},
4504                        [$self->{head_element}, $el_category->{head}];
4505                } else {                } else {
4506                  !!!cp ('t113');                  !!!cp ('t113');
4507                }                }
# Line 3618  sub _tree_construction_main ($) { Line 4512  sub _tree_construction_main ($) {
4512                $parse_rcdata->(RCDATA_CONTENT_MODEL);                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4513                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4514                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4515                redo B;                next B;
4516              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style' or
4517                         $token->{tag_name} eq 'noframes') {
4518                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4519                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
4520                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4521                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4522                  !!!cp ('t114');                  !!!cp ('t114');
4523                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4524                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4525                    push @{$self->{open_elements}},
4526                        [$self->{head_element}, $el_category->{head}];
4527                } else {                } else {
4528                  !!!cp ('t115');                  !!!cp ('t115');
4529                }                }
4530                $parse_rcdata->(CDATA_CONTENT_MODEL);                $parse_rcdata->(CDATA_CONTENT_MODEL);
4531                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4532                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4533                redo B;                next B;
4534              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4535                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
4536                  !!!cp ('t116');                  !!!cp ('t116');
4537                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
4538                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4539                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4540                    !!!nack ('t116.1');
4541                  !!!next-token;                  !!!next-token;
4542                  redo B;                  next B;
4543                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4544                  !!!cp ('t117');                  !!!cp ('t117');
4545                  !!!parse-error (type => 'in noscript:noscript', token => $token);                  !!!parse-error (type => 'in noscript', text => 'noscript',
4546                                    token => $token);
4547                  ## Ignore the token                  ## Ignore the token
4548                    !!!nack ('t117.1');
4549                  !!!next-token;                  !!!next-token;
4550                  redo B;                  next B;
4551                } else {                } else {
4552                  !!!cp ('t118');                  !!!cp ('t118');
4553                  #                  #
# Line 3657  sub _tree_construction_main ($) { Line 4557  sub _tree_construction_main ($) {
4557                  !!!cp ('t119');                  !!!cp ('t119');
4558                  ## As if </noscript>                  ## As if </noscript>
4559                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4560                  !!!parse-error (type => 'in noscript:script', token => $token);                  !!!parse-error (type => 'in noscript', text => 'script',
4561                                    token => $token);
4562                                
4563                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4564                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4565                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4566                  !!!cp ('t120');                  !!!cp ('t120');
4567                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4568                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4569                    push @{$self->{open_elements}},
4570                        [$self->{head_element}, $el_category->{head}];
4571                } else {                } else {
4572                  !!!cp ('t121');                  !!!cp ('t121');
4573                }                }
# Line 3673  sub _tree_construction_main ($) { Line 4576  sub _tree_construction_main ($) {
4576                $script_start_tag->();                $script_start_tag->();
4577                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4578                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4579                redo B;                next B;
4580              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
4581                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
4582                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4583                  !!!cp ('t122');                  !!!cp ('t122');
4584                  ## As if </noscript>                  ## As if </noscript>
4585                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4586                  !!!parse-error (type => 'in noscript:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'in noscript',
4587                                    text => $token->{tag_name}, token => $token);
4588                                    
4589                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4590                  ## As if </head>                  ## As if </head>
# Line 3707  sub _tree_construction_main ($) { Line 4611  sub _tree_construction_main ($) {
4611                } else {                } else {
4612                  die "$0: tag name: $self->{tag_name}";                  die "$0: tag name: $self->{tag_name}";
4613                }                }
4614                  !!!nack ('t127.1');
4615                !!!next-token;                !!!next-token;
4616                redo B;                next B;
4617              } else {              } else {
4618                !!!cp ('t128');                !!!cp ('t128');
4619                #                #
# Line 3718  sub _tree_construction_main ($) { Line 4623  sub _tree_construction_main ($) {
4623                !!!cp ('t129');                !!!cp ('t129');
4624                ## As if </noscript>                ## As if </noscript>
4625                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4626                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'in noscript:/',
4627                                  text => $token->{tag_name}, token => $token);
4628                                
4629                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4630                ## As if </head>                ## As if </head>
# Line 3740  sub _tree_construction_main ($) { Line 4646  sub _tree_construction_main ($) {
4646              !!!insert-element ('body',, $token);              !!!insert-element ('body',, $token);
4647              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4648              ## reprocess              ## reprocess
4649              redo B;              !!!ack-later;
4650                next B;
4651            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
4652              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
4653                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4654                  !!!cp ('t132');                  !!!cp ('t132');
4655                  ## As if <head>                  ## As if <head>
4656                  !!!create-element ($self->{head_element}, 'head',, $token);                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4657                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4658                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4659                        [$self->{head_element}, $el_category->{head}];
4660    
4661                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4662                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4663                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4664                  !!!next-token;                  !!!next-token;
4665                  redo B;                  next B;
4666                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4667                  !!!cp ('t133');                  !!!cp ('t133');
4668                  ## As if </noscript>                  ## As if </noscript>
4669                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4670                  !!!parse-error (type => 'in noscript:/head', token => $token);                  !!!parse-error (type => 'in noscript:/',
4671                                    text => 'head', token => $token);
4672                                    
4673                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4674                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4675                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4676                  !!!next-token;                  !!!next-token;
4677                  redo B;                  next B;
4678                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4679                  !!!cp ('t134');                  !!!cp ('t134');
4680                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4681                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4682                  !!!next-token;                  !!!next-token;
4683                  redo B;                  next B;
4684                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4685                    !!!cp ('t134.1');
4686                    !!!parse-error (type => 'unmatched end tag', text => 'head',
4687                                    token => $token);
4688                    ## Ignore the token
4689                    !!!next-token;
4690                    next B;
4691                } else {                } else {
4692                  !!!cp ('t135');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
                 #  
4693                }                }
4694              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4695                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
# Line 3782  sub _tree_construction_main ($) { Line 4697  sub _tree_construction_main ($) {
4697                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4698                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4699                  !!!next-token;                  !!!next-token;
4700                  redo B;                  next B;
4701                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
4702                           $self->{insertion_mode} == AFTER_HEAD_IM) {
4703                  !!!cp ('t137');                  !!!cp ('t137');
4704                  !!!parse-error (type => 'unmatched end tag:noscript', token => $token);                  !!!parse-error (type => 'unmatched end tag',
4705                                    text => 'noscript', token => $token);
4706                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
4707                  !!!next-token;                  !!!next-token;
4708                  redo B;                  next B;
4709                } else {                } else {
4710                  !!!cp ('t138');                  !!!cp ('t138');
4711                  #                  #
# Line 3796  sub _tree_construction_main ($) { Line 4713  sub _tree_construction_main ($) {
4713              } elsif ({              } elsif ({
4714                        body => 1, html => 1,                        body => 1, html => 1,
4715                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4716                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM or
4717                  !!!cp ('t139');                    $self->{insertion_mode} == IN_HEAD_IM or
4718                  ## As if <head>                    $self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
                 !!!create-element ($self->{head_element}, 'head',, $token);  
                 $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});  
                 push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
   
                 $self->{insertion_mode} = IN_HEAD_IM;  
                 ## Reprocess in the "in head" insertion mode...  
               } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {  
4719                  !!!cp ('t140');                  !!!cp ('t140');
4720                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
4721                                    text => $token->{tag_name}, token => $token);
4722                    ## Ignore the token
4723                    !!!next-token;
4724                    next B;
4725                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4726                    !!!cp ('t140.1');
4727                    !!!parse-error (type => 'unmatched end tag',
4728                                    text => $token->{tag_name}, token => $token);
4729                  ## Ignore the token                  ## Ignore the token
4730                  !!!next-token;                  !!!next-token;
4731                  redo B;                  next B;
4732                } else {                } else {
4733                  !!!cp ('t141');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
4734                }                }
4735                              } elsif ($token->{tag_name} eq 'p') {
4736                #                !!!cp ('t142');
4737              } elsif ({                !!!parse-error (type => 'unmatched end tag',
4738                        p => 1, br => 1,                                text => $token->{tag_name}, token => $token);
4739                       }->{$token->{tag_name}}) {                ## Ignore the token
4740                  !!!next-token;
4741                  next B;
4742                } elsif ($token->{tag_name} eq 'br') {
4743                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4744                  !!!cp ('t142');                  !!!cp ('t142.2');
4745                  ## As if <head>                  ## (before head) as if <head>, (in head) as if </head>
4746                  !!!create-element ($self->{head_element}, 'head',, $token);                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4747                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4748                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  $self->{insertion_mode} = AFTER_HEAD_IM;
4749      
4750                    ## Reprocess in the "after head" insertion mode...
4751                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4752                    !!!cp ('t143.2');
4753                    ## As if </head>
4754                    pop @{$self->{open_elements}};
4755                    $self->{insertion_mode} = AFTER_HEAD_IM;
4756      
4757                    ## Reprocess in the "after head" insertion mode...
4758                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4759                    !!!cp ('t143.3');
4760                    ## ISSUE: Two parse errors for <head><noscript></br>
4761                    !!!parse-error (type => 'unmatched end tag',
4762                                    text => 'br', token => $token);
4763                    ## As if </noscript>
4764                    pop @{$self->{open_elements}};
4765                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4766    
4767                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4768                } else {                  ## As if </head>
4769                  !!!cp ('t143');                  pop @{$self->{open_elements}};
4770                }                  $self->{insertion_mode} = AFTER_HEAD_IM;
4771    
4772                #                  ## Reprocess in the "after head" insertion mode...
4773              } else {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4774                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                  !!!cp ('t143.4');
                 !!!cp ('t144');  
4775                  #                  #
4776                } else {                } else {
4777                  !!!cp ('t145');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
4778                }                }
4779    
4780                  ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
4781                  !!!parse-error (type => 'unmatched end tag',
4782                                  text => 'br', token => $token);
4783                  ## Ignore the token
4784                  !!!next-token;
4785                  next B;
4786                } else {
4787                  !!!cp ('t145');
4788                  !!!parse-error (type => 'unmatched end tag',
4789                                  text => $token->{tag_name}, token => $token);
4790                  ## Ignore the token
4791                  !!!next-token;
4792                  next B;
4793              }              }
4794    
4795              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4796                !!!cp ('t146');                !!!cp ('t146');
4797                ## As if </noscript>                ## As if </noscript>
4798                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4799                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'in noscript:/',
4800                                  text => $token->{tag_name}, token => $token);
4801                                
4802                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4803                ## As if </head>                ## As if </head>
# Line 3866  sub _tree_construction_main ($) { Line 4813  sub _tree_construction_main ($) {
4813              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4814  ## ISSUE: This case cannot be reached?  ## ISSUE: This case cannot be reached?
4815                !!!cp ('t148');                !!!cp ('t148');
4816                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag',
4817                                  text => $token->{tag_name}, token => $token);
4818                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
4819                !!!next-token;                !!!next-token;
4820                redo B;                next B;
4821              } else {              } else {
4822                !!!cp ('t149');                !!!cp ('t149');
4823              }              }
# Line 3879  sub _tree_construction_main ($) { Line 4827  sub _tree_construction_main ($) {
4827              !!!insert-element ('body',, $token);              !!!insert-element ('body',, $token);
4828              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4829              ## reprocess              ## reprocess
4830              redo B;              next B;
4831        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4832          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4833            !!!cp ('t149.1');            !!!cp ('t149.1');
4834    
4835            ## NOTE: As if <head>            ## NOTE: As if <head>
4836            !!!create-element ($self->{head_element}, 'head',, $token);            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4837            $self->{open_elements}->[-1]->[0]->append_child            $self->{open_elements}->[-1]->[0]->append_child
4838                ($self->{head_element});                ($self->{head_element});
4839            #push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            #push @{$self->{open_elements}},
4840              #    [$self->{head_element}, $el_category->{head}];
4841            #$self->{insertion_mode} = IN_HEAD_IM;            #$self->{insertion_mode} = IN_HEAD_IM;
4842            ## NOTE: Reprocess.            ## NOTE: Reprocess.
4843    
# Line 3932  sub _tree_construction_main ($) { Line 4881  sub _tree_construction_main ($) {
4881          !!!insert-element ('body',, $token);          !!!insert-element ('body',, $token);
4882          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4883          ## NOTE: Reprocess.          ## NOTE: Reprocess.
4884          redo B;          next B;
4885        } else {        } else {
4886          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
4887        }        }
# Line 3947  sub _tree_construction_main ($) { Line 4896  sub _tree_construction_main ($) {
4896              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4897    
4898              !!!next-token;              !!!next-token;
4899              redo B;              next B;
4900            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
4901              if ({              if ({
4902                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
# Line 3957  sub _tree_construction_main ($) { Line 4906  sub _tree_construction_main ($) {
4906                  ## have an element in table scope                  ## have an element in table scope
4907                  for (reverse 0..$#{$self->{open_elements}}) {                  for (reverse 0..$#{$self->{open_elements}}) {
4908                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4909                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] & TABLE_CELL_EL) {
4910                      !!!cp ('t151');                      !!!cp ('t151');
4911    
4912                      ## Close the cell                      ## Close the cell
4913                      !!!back-token; # <?>                      !!!back-token; # <x>
4914                      $token = {type => END_TAG_TOKEN, tag_name => $node->[1],                      $token = {type => END_TAG_TOKEN,
4915                                  tag_name => $node->[0]->manakai_local_name,
4916                                line => $token->{line},                                line => $token->{line},
4917                                column => $token->{column}};                                column => $token->{column}};
4918                      redo B;                      next B;
4919                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
4920                      !!!cp ('t152');                      !!!cp ('t152');
4921                      ## ISSUE: This case can never be reached, maybe.                      ## ISSUE: This case can never be reached, maybe.
4922                      last;                      last;
# Line 3977  sub _tree_construction_main ($) { Line 4925  sub _tree_construction_main ($) {
4925    
4926                  !!!cp ('t153');                  !!!cp ('t153');
4927                  !!!parse-error (type => 'start tag not allowed',                  !!!parse-error (type => 'start tag not allowed',
4928                      value => $token->{tag_name}, token => $token);                      text => $token->{tag_name}, token => $token);
4929                  ## Ignore the token                  ## Ignore the token
4930                    !!!nack ('t153.1');
4931                  !!!next-token;                  !!!next-token;
4932                  redo B;                  next B;
4933                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4934                  !!!parse-error (type => 'not closed:caption', token => $token);                  !!!parse-error (type => 'not closed', text => 'caption',
4935                                    token => $token);
4936                                    
4937                  ## NOTE: As if </caption>.                  ## NOTE: As if </caption>.
4938                  ## have a table element in table scope                  ## have a table element in table scope
# Line 3990  sub _tree_construction_main ($) { Line 4940  sub _tree_construction_main ($) {
4940                  INSCOPE: {                  INSCOPE: {
4941                    for (reverse 0..$#{$self->{open_elements}}) {                    for (reverse 0..$#{$self->{open_elements}}) {
4942                      my $node = $self->{open_elements}->[$_];                      my $node = $self->{open_elements}->[$_];
4943                      if ($node->[1] eq 'caption') {                      if ($node->[1] & CAPTION_EL) {
4944                        !!!cp ('t155');                        !!!cp ('t155');
4945                        $i = $_;                        $i = $_;
4946                        last INSCOPE;                        last INSCOPE;
4947                      } elsif ({                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
                               table => 1, html => 1,  
                              }->{$node->[1]}) {  
4948                        !!!cp ('t156');                        !!!cp ('t156');
4949                        last;                        last;
4950                      }                      }
# Line 4004  sub _tree_construction_main ($) { Line 4952  sub _tree_construction_main ($) {
4952    
4953                    !!!cp ('t157');                    !!!cp ('t157');
4954                    !!!parse-error (type => 'start tag not allowed',                    !!!parse-error (type => 'start tag not allowed',
4955                                    value => $token->{tag_name}, token => $token);                                    text => $token->{tag_name}, token => $token);
4956                    ## Ignore the token                    ## Ignore the token
4957                      !!!nack ('t157.1');
4958                    !!!next-token;                    !!!next-token;
4959                    redo B;                    next B;
4960                  } # INSCOPE                  } # INSCOPE
4961                                    
4962                  ## generate implied end tags                  ## generate implied end tags
4963                  while ({                  while ($self->{open_elements}->[-1]->[1]
4964                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4965                    !!!cp ('t158');                    !!!cp ('t158');
4966                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4967                  }                  }
4968    
4969                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4970                    !!!cp ('t159');                    !!!cp ('t159');
4971                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                    !!!parse-error (type => 'not closed',
4972                                      text => $self->{open_elements}->[-1]->[0]
4973                                          ->manakai_local_name,
4974                                      token => $token);
4975                  } else {                  } else {
4976                    !!!cp ('t160');                    !!!cp ('t160');
4977                  }                  }
# Line 4032  sub _tree_construction_main ($) { Line 4983  sub _tree_construction_main ($) {
4983                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4984                                    
4985                  ## reprocess                  ## reprocess
4986                  redo B;                  !!!ack-later;
4987                    next B;
4988                } else {                } else {
4989                  !!!cp ('t161');                  !!!cp ('t161');
4990                  #                  #
# Line 4048  sub _tree_construction_main ($) { Line 5000  sub _tree_construction_main ($) {
5000                  my $i;                  my $i;
5001                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5002                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5003                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5004                      !!!cp ('t163');                      !!!cp ('t163');
5005                      $i = $_;                      $i = $_;
5006                      last INSCOPE;                      last INSCOPE;
5007                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5008                      !!!cp ('t164');                      !!!cp ('t164');
5009                      last INSCOPE;                      last INSCOPE;
5010                    }                    }
5011                  } # INSCOPE                  } # INSCOPE
5012                    unless (defined $i) {                    unless (defined $i) {
5013                      !!!cp ('t165');                      !!!cp ('t165');
5014                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                      !!!parse-error (type => 'unmatched end tag',
5015                                        text => $token->{tag_name},
5016                                        token => $token);
5017                      ## Ignore the token                      ## Ignore the token
5018                      !!!next-token;                      !!!next-token;
5019                      redo B;                      next B;
5020                    }                    }
5021                                    
5022                  ## generate implied end tags                  ## generate implied end tags
5023                  while ({                  while ($self->{open_elements}->[-1]->[1]
5024                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
5025                    !!!cp ('t166');                    !!!cp ('t166');
5026                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5027                  }                  }
5028    
5029                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
5030                            ne $token->{tag_name}) {
5031                    !!!cp ('t167');                    !!!cp ('t167');
5032                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                    !!!parse-error (type => 'not closed',
5033                                      text => $self->{open_elements}->[-1]->[0]
5034                                          ->manakai_local_name,
5035                                      token => $token);
5036                  } else {                  } else {
5037                    !!!cp ('t168');                    !!!cp ('t168');
5038                  }                  }
# Line 4089  sub _tree_construction_main ($) { Line 5044  sub _tree_construction_main ($) {
5044                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
5045                                    
5046                  !!!next-token;                  !!!next-token;
5047                  redo B;                  next B;
5048                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
5049                  !!!cp ('t169');                  !!!cp ('t169');
5050                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
5051                                    text => $token->{tag_name}, token => $token);
5052                  ## Ignore the token                  ## Ignore the token
5053                  !!!next-token;                  !!!next-token;
5054                  redo B;                  next B;
5055                } else {                } else {
5056                  !!!cp ('t170');                  !!!cp ('t170');
5057                  #                  #
# Line 4107  sub _tree_construction_main ($) { Line 5063  sub _tree_construction_main ($) {
5063                  INSCOPE: {                  INSCOPE: {
5064                    for (reverse 0..$#{$self->{open_elements}}) {                    for (reverse 0..$#{$self->{open_elements}}) {
5065                      my $node = $self->{open_elements}->[$_];                      my $node = $self->{open_elements}->[$_];
5066                      if ($node->[1] eq $token->{tag_name}) {                      if ($node->[1] & CAPTION_EL) {
5067                        !!!cp ('t171');                        !!!cp ('t171');
5068                        $i = $_;                        $i = $_;
5069                        last INSCOPE;                        last INSCOPE;
5070                      } elsif ({                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
                               table => 1, html => 1,  
                              }->{$node->[1]}) {  
5071                        !!!cp ('t172');                        !!!cp ('t172');
5072                        last;                        last;
5073                      }                      }
# Line 4121  sub _tree_construction_main ($) { Line 5075  sub _tree_construction_main ($) {
5075    
5076                    !!!cp ('t173');                    !!!cp ('t173');
5077                    !!!parse-error (type => 'unmatched end tag',                    !!!parse-error (type => 'unmatched end tag',
5078                                    value => $token->{tag_name}, token => $token);                                    text => $token->{tag_name}, token => $token);
5079                    ## Ignore the token                    ## Ignore the token
5080                    !!!next-token;                    !!!next-token;
5081                    redo B;                    next B;
5082                  } # INSCOPE                  } # INSCOPE
5083                                    
5084                  ## generate implied end tags                  ## generate implied end tags
5085                  while ({                  while ($self->{open_elements}->[-1]->[1]
5086                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
5087                    !!!cp ('t174');                    !!!cp ('t174');
5088                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5089                  }                  }
5090                                    
5091                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5092                    !!!cp ('t175');                    !!!cp ('t175');
5093                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                    !!!parse-error (type => 'not closed',
5094                                      text => $self->{open_elements}->[-1]->[0]
5095                                          ->manakai_local_name,
5096                                      token => $token);
5097                  } else {                  } else {
5098                    !!!cp ('t176');                    !!!cp ('t176');
5099                  }                  }
# Line 4149  sub _tree_construction_main ($) { Line 5105  sub _tree_construction_main ($) {
5105                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5106                                    
5107                  !!!next-token;                  !!!next-token;
5108                  redo B;                  next B;
5109                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
5110                  !!!cp ('t177');                  !!!cp ('t177');
5111                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
5112                                    text => $token->{tag_name}, token => $token);
5113                  ## Ignore the token                  ## Ignore the token
5114                  !!!next-token;                  !!!next-token;
5115                  redo B;                  next B;
5116                } else {                } else {
5117                  !!!cp ('t178');                  !!!cp ('t178');
5118                  #                  #
# Line 4171  sub _tree_construction_main ($) { Line 5128  sub _tree_construction_main ($) {
5128                INSCOPE: {                INSCOPE: {
5129                  for (reverse 0..$#{$self->{open_elements}}) {                  for (reverse 0..$#{$self->{open_elements}}) {
5130                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5131                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5132                      !!!cp ('t179');                      !!!cp ('t179');
5133                      $i = $_;                      $i = $_;
5134    
5135                      ## Close the cell                      ## Close the cell
5136                      !!!back-token; # </?>                      !!!back-token; # </x>
5137                      $token = {type => END_TAG_TOKEN, tag_name => $tn,                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
5138                                line => $token->{line},                                line => $token->{line},
5139                                column => $token->{column}};                                column => $token->{column}};
5140                      redo B;                      next B;
5141                    } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {                    } elsif ($node->[1] & TABLE_CELL_EL) {
5142                      !!!cp ('t180');                      !!!cp ('t180');
5143                      $tn = $node->[1];                      $tn = $node->[0]->manakai_local_name;
5144                      ## NOTE: There is exactly one |td| or |th| element                      ## NOTE: There is exactly one |td| or |th| element
5145                      ## in scope in the stack of open elements by definition.                      ## in scope in the stack of open elements by definition.
5146                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5147                      ## ISSUE: Can this be reached?                      ## ISSUE: Can this be reached?
5148                      !!!cp ('t181');                      !!!cp ('t181');
5149                      last;                      last;
# Line 4197  sub _tree_construction_main ($) { Line 5152  sub _tree_construction_main ($) {
5152    
5153                  !!!cp ('t182');                  !!!cp ('t182');
5154                  !!!parse-error (type => 'unmatched end tag',                  !!!parse-error (type => 'unmatched end tag',
5155                      value => $token->{tag_name}, token => $token);                      text => $token->{tag_name}, token => $token);
5156                  ## Ignore the token                  ## Ignore the token
5157                  !!!next-token;                  !!!next-token;
5158                  redo B;                  next B;
5159                } # INSCOPE                } # INSCOPE
5160              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
5161                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5162                !!!parse-error (type => 'not closed:caption', token => $token);                !!!parse-error (type => 'not closed', text => 'caption',
5163                                  token => $token);
5164    
5165                ## As if </caption>                ## As if </caption>
5166                ## have a table element in table scope                ## have a table element in table scope
5167                my $i;                my $i;
5168                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5169                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5170                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
5171                    !!!cp ('t184');                    !!!cp ('t184');
5172                    $i = $_;                    $i = $_;
5173                    last INSCOPE;                    last INSCOPE;
5174                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5175                    !!!cp ('t185');                    !!!cp ('t185');
5176                    last INSCOPE;                    last INSCOPE;
5177                  }                  }
5178                } # INSCOPE                } # INSCOPE
5179                unless (defined $i) {                unless (defined $i) {
5180                  !!!cp ('t186');                  !!!cp ('t186');
5181                  !!!parse-error (type => 'unmatched end tag:caption', token => $token);                  !!!parse-error (type => 'unmatched end tag',
5182                                    text => 'caption', token => $token);
5183                  ## Ignore the token                  ## Ignore the token
5184                  !!!next-token;                  !!!next-token;
5185                  redo B;                  next B;
5186                }                }
5187                                
5188                ## generate implied end tags                ## generate implied end tags
5189                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
5190                  !!!cp ('t187');                  !!!cp ('t187');
5191                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5192                }                }
5193    
5194                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5195                  !!!cp ('t188');                  !!!cp ('t188');
5196                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                  !!!parse-error (type => 'not closed',
5197                                    text => $self->{open_elements}->[-1]->[0]
5198                                        ->manakai_local_name,
5199                                    token => $token);
5200                } else {                } else {
5201                  !!!cp ('t189');                  !!!cp ('t189');
5202                }                }
# Line 4252  sub _tree_construction_main ($) { Line 5208  sub _tree_construction_main ($) {
5208                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5209    
5210                ## reprocess                ## reprocess
5211                redo B;                next B;
5212              } elsif ({              } elsif ({
5213                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
5214                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5215                if ($self->{insertion_mode} & BODY_TABLE_IMS) {                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
5216                  !!!cp ('t190');                  !!!cp ('t190');
5217                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
5218                                    text => $token->{tag_name}, token => $token);
5219                  ## Ignore the token                  ## Ignore the token
5220                  !!!next-token;                  !!!next-token;
5221                  redo B;                  next B;
5222                } else {                } else {
5223                  !!!cp ('t191');                  !!!cp ('t191');
5224                  #                  #
# Line 4272  sub _tree_construction_main ($) { Line 5229  sub _tree_construction_main ($) {
5229                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
5230                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5231                !!!cp ('t192');                !!!cp ('t192');
5232                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag',
5233                                  text => $token->{tag_name}, token => $token);
5234                ## Ignore the token                ## Ignore the token
5235                !!!next-token;                !!!next-token;
5236                redo B;                next B;
5237              } else {              } else {
5238                !!!cp ('t193');                !!!cp ('t193');
5239                #                #
5240              }              }
5241        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5242          for my $entry (@{$self->{open_elements}}) {          for my $entry (@{$self->{open_elements}}) {
5243            if (not {            unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
             dd => 1, dt => 1, li => 1, p => 1, tbody => 1, td => 1, tfoot => 1,  
             th => 1, thead => 1, tr => 1, body => 1, html => 1,  
           }->{$entry->[1]}) {  
5244              !!!cp ('t75');              !!!cp ('t75');
5245              !!!parse-error (type => 'in body:#eof', token => $token);              !!!parse-error (type => 'in body:#eof', token => $token);
5246              last;              last;
# Line 4309  sub _tree_construction_main ($) { Line 5264  sub _tree_construction_main ($) {
5264            unless (length $token->{data}) {            unless (length $token->{data}) {
5265              !!!cp ('t194');              !!!cp ('t194');
5266              !!!next-token;              !!!next-token;
5267              redo B;              next B;
5268            } else {            } else {
5269              !!!cp ('t195');              !!!cp ('t195');
5270            }            }
5271          }          }
5272    
5273              !!!parse-error (type => 'in table:#character', token => $token);          !!!parse-error (type => 'in table:#text', token => $token);
5274    
5275              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
5276              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
# Line 4323  sub _tree_construction_main ($) { Line 5278  sub _tree_construction_main ($) {
5278              ## result in a new Text node.              ## result in a new Text node.
5279              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
5280                            
5281              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]}) {  
5282                # MUST                # MUST
5283                my $foster_parent_element;                my $foster_parent_element;
5284                my $next_sibling;                my $next_sibling;
5285                my $prev_sibling;                my $prev_sibling;
5286                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
5287                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
5288                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
5289                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
5290                      !!!cp ('t196');                      !!!cp ('t196');
# Line 4367  sub _tree_construction_main ($) { Line 5319  sub _tree_construction_main ($) {
5319          }          }
5320                            
5321          !!!next-token;          !!!next-token;
5322          redo B;          next B;
5323        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5324              if ({          if ({
5325                   tr => ($self->{insertion_mode} != IN_ROW_IM),               tr => ($self->{insertion_mode} != IN_ROW_IM),
5326                   th => 1, td => 1,               th => 1, td => 1,
5327                  }->{$token->{tag_name}}) {              }->{$token->{tag_name}}) {
5328                if ($self->{insertion_mode} == IN_TABLE_IM) {            if ($self->{insertion_mode} == IN_TABLE_IM) {
5329                  ## Clear back to table context              ## Clear back to table context
5330                  while ($self->{open_elements}->[-1]->[1] ne 'table' and              while (not ($self->{open_elements}->[-1]->[1]
5331                         $self->{open_elements}->[-1]->[1] ne 'html') {                              & TABLE_SCOPING_EL)) {
5332                    !!!cp ('t201');                !!!cp ('t201');
5333                    pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5334                  }              }
5335                                
5336                  !!!insert-element ('tbody',, $token);              !!!insert-element ('tbody',, $token);
5337                  $self->{insertion_mode} = IN_TABLE_BODY_IM;              $self->{insertion_mode} = IN_TABLE_BODY_IM;
5338                  ## reprocess in the "in table body" insertion mode...              ## reprocess in the "in table body" insertion mode...
5339                }            }
5340              
5341                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {            if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5342                  unless ($token->{tag_name} eq 'tr') {              unless ($token->{tag_name} eq 'tr') {
5343                    !!!cp ('t202');                !!!cp ('t202');
5344                    !!!parse-error (type => 'missing start tag:tr', token => $token);                !!!parse-error (type => 'missing start tag:tr', token => $token);
5345                  }              }
5346                                    
5347                  ## Clear back to table body context              ## Clear back to table body context
5348                  while (not {              while (not ($self->{open_elements}->[-1]->[1]
5349                    tbody => 1, tfoot => 1, thead => 1, html => 1,                              & TABLE_ROWS_SCOPING_EL)) {
5350                  }->{$self->{open_elements}->[-1]->[1]}) {                !!!cp ('t203');
5351                    !!!cp ('t203');                ## ISSUE: Can this case be reached?
5352                    ## ISSUE: Can this case be reached?                pop @{$self->{open_elements}};
5353                    pop @{$self->{open_elements}};              }
                 }  
5354                                    
5355                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
5356                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5357                    !!!cp ('t204');                    !!!cp ('t204');
5358                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5359                      !!!nack ('t204');
5360                    !!!next-token;                    !!!next-token;
5361                    redo B;                    next B;
5362                  } else {                  } else {
5363                    !!!cp ('t205');                    !!!cp ('t205');
5364                    !!!insert-element ('tr',, $token);                    !!!insert-element ('tr',, $token);
# Line 4417  sub _tree_construction_main ($) { Line 5369  sub _tree_construction_main ($) {
5369                }                }
5370    
5371                ## Clear back to table row context                ## Clear back to table row context
5372                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5373                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5374                  !!!cp ('t207');                  !!!cp ('t207');
5375                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5376                }                }
# Line 4429  sub _tree_construction_main ($) { Line 5380  sub _tree_construction_main ($) {
5380    
5381                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
5382                                
5383                  !!!nack ('t207.1');
5384                !!!next-token;                !!!next-token;
5385                redo B;                next B;
5386              } elsif ({              } elsif ({
5387                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5388                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
# Line 4442  sub _tree_construction_main ($) { Line 5394  sub _tree_construction_main ($) {
5394                  my $i;                  my $i;
5395                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5396                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5397                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5398                      !!!cp ('t208');                      !!!cp ('t208');
5399                      $i = $_;                      $i = $_;
5400                      last INSCOPE;                      last INSCOPE;
5401                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             html => 1,  
   
                             ## NOTE: This element does not appear here, maybe.  
                             table => 1,  
                            }->{$node->[1]}) {  
5402                      !!!cp ('t209');                      !!!cp ('t209');
5403                      last INSCOPE;                      last INSCOPE;
5404                    }                    }
5405                  } # INSCOPE                  } # INSCOPE
5406                  unless (defined $i) {                  unless (defined $i) {
5407                   !!!cp ('t210');                    !!!cp ('t210');
5408  ## TODO: This type is wrong.  ## TODO: This type is wrong.
5409                   !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);                    !!!parse-error (type => 'unmacthed end tag',
5410                                      text => $token->{tag_name}, token => $token);
5411                    ## Ignore the token                    ## Ignore the token
5412                      !!!nack ('t210.1');
5413                    !!!next-token;                    !!!next-token;
5414                    redo B;                    next B;
5415                  }                  }
5416                                    
5417                  ## Clear back to table row context                  ## Clear back to table row context
5418                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5419                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5420                    !!!cp ('t211');                    !!!cp ('t211');
5421                    ## ISSUE: Can this case be reached?                    ## ISSUE: Can this case be reached?
5422                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4479  sub _tree_construction_main ($) { Line 5427  sub _tree_construction_main ($) {
5427                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5428                    !!!cp ('t212');                    !!!cp ('t212');
5429                    ## reprocess                    ## reprocess
5430                    redo B;                    !!!ack-later;
5431                      next B;
5432                  } else {                  } else {
5433                    !!!cp ('t213');                    !!!cp ('t213');
5434                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
# Line 4491  sub _tree_construction_main ($) { Line 5440  sub _tree_construction_main ($) {
5440                  my $i;                  my $i;
5441                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5442                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5443                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5444                      !!!cp ('t214');                      !!!cp ('t214');
5445                      $i = $_;                      $i = $_;
5446                      last INSCOPE;                      last INSCOPE;
5447                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5448                      !!!cp ('t215');                      !!!cp ('t215');
5449                      last INSCOPE;                      last INSCOPE;
5450                    }                    }
5451                  } # INSCOPE                  } # INSCOPE
5452                  unless (defined $i) {                  unless (defined $i) {
5453                    !!!cp ('t216');                    !!!cp ('t216');
5454  ## TODO: This erorr type ios wrong.  ## TODO: This erorr type is wrong.
5455                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                    !!!parse-error (type => 'unmatched end tag',
5456                                      text => $token->{tag_name}, token => $token);
5457                    ## Ignore the token                    ## Ignore the token
5458                      !!!nack ('t216.1');
5459                    !!!next-token;                    !!!next-token;
5460                    redo B;                    next B;
5461                  }                  }
5462    
5463                  ## Clear back to table body context                  ## Clear back to table body context
5464                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5465                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5466                    !!!cp ('t217');                    !!!cp ('t217');
5467                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5468                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4538  sub _tree_construction_main ($) { Line 5484  sub _tree_construction_main ($) {
5484    
5485                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
5486                  ## Clear back to table context                  ## Clear back to table context
5487                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5488                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5489                    !!!cp ('t219');                    !!!cp ('t219');
5490                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5491                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4548  sub _tree_construction_main ($) { Line 5494  sub _tree_construction_main ($) {
5494                  !!!insert-element ('colgroup',, $token);                  !!!insert-element ('colgroup',, $token);
5495                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5496                  ## reprocess                  ## reprocess
5497                  redo B;                  !!!ack-later;
5498                    next B;
5499                } elsif ({                } elsif ({
5500                          caption => 1,                          caption => 1,
5501                          colgroup => 1,                          colgroup => 1,
5502                          tbody => 1, tfoot => 1, thead => 1,                          tbody => 1, tfoot => 1, thead => 1,
5503                         }->{$token->{tag_name}}) {                         }->{$token->{tag_name}}) {
5504                  ## Clear back to table context                  ## Clear back to table context
5505                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5506                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5507                    !!!cp ('t220');                    !!!cp ('t220');
5508                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5509                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4574  sub _tree_construction_main ($) { Line 5521  sub _tree_construction_main ($) {
5521                                             thead => IN_TABLE_BODY_IM,                                             thead => IN_TABLE_BODY_IM,
5522                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
5523                  !!!next-token;                  !!!next-token;
5524                  redo B;                  !!!nack ('t220.1');
5525                    next B;
5526                } else {                } else {
5527                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
5528                }                }
5529              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5530                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                !!!parse-error (type => 'not closed',
5531                                  text => $self->{open_elements}->[-1]->[0]
5532                                      ->manakai_local_name,
5533                                  token => $token);
5534    
5535                ## As if </table>                ## As if </table>
5536                ## have a table element in table scope                ## have a table element in table scope
5537                my $i;                my $i;
5538                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5539                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5540                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5541                    !!!cp ('t221');                    !!!cp ('t221');
5542                    $i = $_;                    $i = $_;
5543                    last INSCOPE;                    last INSCOPE;
5544                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           #table => 1,  
                           html => 1,  
                          }->{$node->[1]}) {  
5545                    !!!cp ('t222');                    !!!cp ('t222');
5546                    last INSCOPE;                    last INSCOPE;
5547                  }                  }
# Line 4601  sub _tree_construction_main ($) { Line 5549  sub _tree_construction_main ($) {
5549                unless (defined $i) {                unless (defined $i) {
5550                  !!!cp ('t223');                  !!!cp ('t223');
5551  ## TODO: The following is wrong, maybe.  ## TODO: The following is wrong, maybe.
5552                  !!!parse-error (type => 'unmatched end tag:table', token => $token);                  !!!parse-error (type => 'unmatched end tag', text => 'table',
5553                                    token => $token);
5554                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5555                    !!!nack ('t223.1');
5556                  !!!next-token;                  !!!next-token;
5557                  redo B;                  next B;
5558                }                }
5559                                
5560  ## TODO: Followings are removed from the latest spec.  ## TODO: Followings are removed from the latest spec.
5561                ## generate implied end tags                ## generate implied end tags
5562                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
5563                  !!!cp ('t224');                  !!!cp ('t224');
5564                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5565                }                }
5566    
5567                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5568                  !!!cp ('t225');                  !!!cp ('t225');
5569  ## ISSUE: Can this case be reached?                  ## NOTE: |<table><tr><table>|
5570                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                  !!!parse-error (type => 'not closed',
5571                                    text => $self->{open_elements}->[-1]->[0]
5572                                        ->manakai_local_name,
5573                                    token => $token);
5574                } else {                } else {
5575                  !!!cp ('t226');                  !!!cp ('t226');
5576                }                }
# Line 4629  sub _tree_construction_main ($) { Line 5580  sub _tree_construction_main ($) {
5580    
5581                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5582    
5583                ## reprocess            ## reprocess
5584                redo B;            !!!ack-later;
5585              next B;
5586          } elsif ($token->{tag_name} eq 'style') {          } elsif ($token->{tag_name} eq 'style') {
5587            if (not $open_tables->[-1]->[1]) { # tainted            if (not $open_tables->[-1]->[1]) { # tainted
5588              !!!cp ('t227.8');              !!!cp ('t227.8');
5589              ## NOTE: This is a "as if in head" code clone.              ## NOTE: This is a "as if in head" code clone.
5590              $parse_rcdata->(CDATA_CONTENT_MODEL);              $parse_rcdata->(CDATA_CONTENT_MODEL);
5591              redo B;              next B;
5592            } else {            } else {
5593              !!!cp ('t227.7');              !!!cp ('t227.7');
5594              #              #
# Line 4646  sub _tree_construction_main ($) { Line 5598  sub _tree_construction_main ($) {
5598              !!!cp ('t227.6');              !!!cp ('t227.6');
5599              ## NOTE: This is a "as if in head" code clone.              ## NOTE: This is a "as if in head" code clone.
5600              $script_start_tag->();              $script_start_tag->();
5601              redo B;              next B;
5602            } else {            } else {
5603              !!!cp ('t227.5');              !!!cp ('t227.5');
5604              #              #
# Line 4657  sub _tree_construction_main ($) { Line 5609  sub _tree_construction_main ($) {
5609                my $type = lc $token->{attributes}->{type}->{value};                my $type = lc $token->{attributes}->{type}->{value};
5610                if ($type eq 'hidden') {                if ($type eq 'hidden') {
5611                  !!!cp ('t227.3');                  !!!cp ('t227.3');
5612                  !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'in table',
5613                                    text => $token->{tag_name}, token => $token);
5614    
5615                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5616    
# Line 4666  sub _tree_construction_main ($) { Line 5619  sub _tree_construction_main ($) {
5619                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5620    
5621                  !!!next-token;                  !!!next-token;
5622                  redo B;                  !!!ack ('t227.2.1');
5623                    next B;
5624                } else {                } else {
5625                  !!!cp ('t227.2');                  !!!cp ('t227.2');
5626                  #                  #
# Line 4684  sub _tree_construction_main ($) { Line 5638  sub _tree_construction_main ($) {
5638            #            #
5639          }          }
5640    
5641          !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'in table', text => $token->{tag_name},
5642                            token => $token);
5643    
5644          $insert = $insert_to_foster;          $insert = $insert_to_foster;
5645          #          #
# Line 4695  sub _tree_construction_main ($) { Line 5650  sub _tree_construction_main ($) {
5650                my $i;                my $i;
5651                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5652                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5653                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_ROW_EL) {
5654                    !!!cp ('t228');                    !!!cp ('t228');
5655                    $i = $_;                    $i = $_;
5656                    last INSCOPE;                    last INSCOPE;
5657                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5658                    !!!cp ('t229');                    !!!cp ('t229');
5659                    last INSCOPE;                    last INSCOPE;
5660                  }                  }
5661                } # INSCOPE                } # INSCOPE
5662                unless (defined $i) {                unless (defined $i) {
5663                  !!!cp ('t230');                  !!!cp ('t230');
5664                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
5665                                    text => $token->{tag_name}, token => $token);
5666                  ## Ignore the token                  ## Ignore the token
5667                    !!!nack ('t230.1');
5668                  !!!next-token;                  !!!next-token;
5669                  redo B;                  next B;
5670                } else {                } else {
5671                  !!!cp ('t232');                  !!!cp ('t232');
5672                }                }
5673    
5674                ## Clear back to table row context                ## Clear back to table row context
5675                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5676                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5677                  !!!cp ('t231');                  !!!cp ('t231');
5678  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
5679                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 4728  sub _tree_construction_main ($) { Line 5682  sub _tree_construction_main ($) {
5682                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5683                $self->{insertion_mode} = IN_TABLE_BODY_IM;                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5684                !!!next-token;                !!!next-token;
5685                redo B;                !!!nack ('t231.1');
5686                  next B;
5687              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5688                if ($self->{insertion_mode} == IN_ROW_IM) {                if ($self->{insertion_mode} == IN_ROW_IM) {
5689                  ## As if </tr>                  ## As if </tr>
# Line 4736  sub _tree_construction_main ($) { Line 5691  sub _tree_construction_main ($) {
5691                  my $i;                  my $i;
5692                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5693                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5694                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5695                      !!!cp ('t233');                      !!!cp ('t233');
5696                      $i = $_;                      $i = $_;
5697                      last INSCOPE;                      last INSCOPE;
5698                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5699                      !!!cp ('t234');                      !!!cp ('t234');
5700                      last INSCOPE;                      last INSCOPE;
5701                    }                    }
# Line 4750  sub _tree_construction_main ($) { Line 5703  sub _tree_construction_main ($) {
5703                  unless (defined $i) {                  unless (defined $i) {
5704                    !!!cp ('t235');                    !!!cp ('t235');
5705  ## TODO: The following is wrong.  ## TODO: The following is wrong.
5706                    !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);                    !!!parse-error (type => 'unmatched end tag',
5707                                      text => $token->{type}, token => $token);
5708                    ## Ignore the token                    ## Ignore the token
5709                      !!!nack ('t236.1');
5710                    !!!next-token;                    !!!next-token;
5711                    redo B;                    next B;
5712                  }                  }
5713                                    
5714                  ## Clear back to table row context                  ## Clear back to table row context
5715                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5716                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5717                    !!!cp ('t236');                    !!!cp ('t236');
5718  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
5719                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4775  sub _tree_construction_main ($) { Line 5729  sub _tree_construction_main ($) {
5729                  my $i;                  my $i;
5730                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5731                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5732                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5733                      !!!cp ('t237');                      !!!cp ('t237');
5734                      $i = $_;                      $i = $_;
5735                      last INSCOPE;                      last INSCOPE;
5736                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5737                      !!!cp ('t238');                      !!!cp ('t238');
5738                      last INSCOPE;                      last INSCOPE;
5739                    }                    }
5740                  } # INSCOPE                  } # INSCOPE
5741                  unless (defined $i) {                  unless (defined $i) {
5742                    !!!cp ('t239');                    !!!cp ('t239');
5743                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                    !!!parse-error (type => 'unmatched end tag',
5744                                      text => $token->{tag_name}, token => $token);
5745                    ## Ignore the token                    ## Ignore the token
5746                      !!!nack ('t239.1');
5747                    !!!next-token;                    !!!next-token;
5748                    redo B;                    next B;
5749                  }                  }
5750                                    
5751                  ## Clear back to table body context                  ## Clear back to table body context
5752                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5753                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5754                    !!!cp ('t240');                    !!!cp ('t240');
5755                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5756                  }                  }
# Line 4825  sub _tree_construction_main ($) { Line 5776  sub _tree_construction_main ($) {
5776                my $i;                my $i;
5777                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5778                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5779                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_EL) {
5780                    !!!cp ('t241');                    !!!cp ('t241');
5781                    $i = $_;                    $i = $_;
5782                    last INSCOPE;                    last INSCOPE;
5783                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5784                    !!!cp ('t242');                    !!!cp ('t242');
5785                    last INSCOPE;                    last INSCOPE;
5786                  }                  }
5787                } # INSCOPE                } # INSCOPE
5788                unless (defined $i) {                unless (defined $i) {
5789                  !!!cp ('t243');                  !!!cp ('t243');
5790                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
5791                                    text => $token->{tag_name}, token => $token);
5792                  ## Ignore the token                  ## Ignore the token
5793                    !!!nack ('t243.1');
5794                  !!!next-token;                  !!!next-token;
5795                  redo B;                  next B;
5796                }                }
5797                                    
5798                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
# Line 4850  sub _tree_construction_main ($) { Line 5801  sub _tree_construction_main ($) {
5801                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5802                                
5803                !!!next-token;                !!!next-token;
5804                redo B;                next B;
5805              } elsif ({              } elsif ({
5806                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5807                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
# Line 4860  sub _tree_construction_main ($) { Line 5811  sub _tree_construction_main ($) {
5811                  my $i;                  my $i;
5812                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5813                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5814                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5815                      !!!cp ('t247');                      !!!cp ('t247');
5816                      $i = $_;                      $i = $_;
5817                      last INSCOPE;                      last INSCOPE;
5818                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5819                      !!!cp ('t248');                      !!!cp ('t248');
5820                      last INSCOPE;                      last INSCOPE;
5821                    }                    }
5822                  } # INSCOPE                  } # INSCOPE
5823                    unless (defined $i) {                    unless (defined $i) {
5824                      !!!cp ('t249');                      !!!cp ('t249');
5825                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                      !!!parse-error (type => 'unmatched end tag',
5826                                        text => $token->{tag_name}, token => $token);
5827                      ## Ignore the token                      ## Ignore the token
5828                        !!!nack ('t249.1');
5829                      !!!next-token;                      !!!next-token;
5830                      redo B;                      next B;
5831                    }                    }
5832                                    
5833                  ## As if </tr>                  ## As if </tr>
# Line 4884  sub _tree_construction_main ($) { Line 5835  sub _tree_construction_main ($) {
5835                  my $i;                  my $i;
5836                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5837                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5838                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5839                      !!!cp ('t250');                      !!!cp ('t250');
5840                      $i = $_;                      $i = $_;
5841                      last INSCOPE;                      last INSCOPE;
5842                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5843                      !!!cp ('t251');                      !!!cp ('t251');
5844                      last INSCOPE;                      last INSCOPE;
5845                    }                    }
5846                  } # INSCOPE                  } # INSCOPE
5847                    unless (defined $i) {                    unless (defined $i) {
5848                      !!!cp ('t252');                      !!!cp ('t252');
5849                      !!!parse-error (type => 'unmatched end tag:tr', token => $token);                      !!!parse-error (type => 'unmatched end tag',
5850                                        text => 'tr', token => $token);
5851                      ## Ignore the token                      ## Ignore the token
5852                        !!!nack ('t252.1');
5853                      !!!next-token;                      !!!next-token;
5854                      redo B;                      next B;
5855                    }                    }
5856                                    
5857                  ## Clear back to table row context                  ## Clear back to table row context
5858                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5859                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5860                    !!!cp ('t253');                    !!!cp ('t253');
5861  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
5862                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4921  sub _tree_construction_main ($) { Line 5871  sub _tree_construction_main ($) {
5871                my $i;                my $i;
5872                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5873                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5874                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5875                    !!!cp ('t254');                    !!!cp ('t254');
5876                    $i = $_;                    $i = $_;
5877                    last INSCOPE;                    last INSCOPE;
5878                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5879                    !!!cp ('t255');                    !!!cp ('t255');
5880                    last INSCOPE;                    last INSCOPE;
5881                  }                  }
5882                } # INSCOPE                } # INSCOPE
5883                unless (defined $i) {                unless (defined $i) {
5884                  !!!cp ('t256');                  !!!cp ('t256');
5885                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
5886                                    text => $token->{tag_name}, token => $token);
5887                  ## Ignore the token                  ## Ignore the token
5888                    !!!nack ('t256.1');
5889                  !!!next-token;                  !!!next-token;
5890                  redo B;                  next B;
5891                }                }
5892    
5893                ## Clear back to table body context                ## Clear back to table body context
5894                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5895                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5896                  !!!cp ('t257');                  !!!cp ('t257');
5897  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
5898                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 4951  sub _tree_construction_main ($) { Line 5900  sub _tree_construction_main ($) {
5900    
5901                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5902                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5903                  !!!nack ('t257.1');
5904                !!!next-token;                !!!next-token;
5905                redo B;                next B;
5906              } elsif ({              } elsif ({
5907                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
5908                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
5909                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5910                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
5911                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5912                !!!cp ('t258');            !!!cp ('t258');
5913                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
5914                ## Ignore the token                            text => $token->{tag_name}, token => $token);
5915                !!!next-token;            ## Ignore the token
5916                redo B;            !!!nack ('t258.1');
5917               !!!next-token;
5918              next B;
5919          } else {          } else {
5920            !!!cp ('t259');            !!!cp ('t259');
5921            !!!parse-error (type => 'in table:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'in table:/',
5922                              text => $token->{tag_name}, token => $token);
5923    
5924            $insert = $insert_to_foster;            $insert = $insert_to_foster;
5925            #            #
5926          }          }
5927        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5928          unless ($self->{open_elements}->[-1]->[1] eq 'html' and          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5929                  @{$self->{open_elements}} == 1) { # redundant, maybe                  @{$self->{open_elements}} == 1) { # redundant, maybe
5930            !!!parse-error (type => 'in body:#eof', token => $token);            !!!parse-error (type => 'in body:#eof', token => $token);
5931            !!!cp ('t259.1');            !!!cp ('t259.1');
# Line 4994  sub _tree_construction_main ($) { Line 5947  sub _tree_construction_main ($) {
5947                unless (length $token->{data}) {                unless (length $token->{data}) {
5948                  !!!cp ('t260');                  !!!cp ('t260');
5949                  !!!next-token;                  !!!next-token;
5950                  redo B;                  next B;
5951                }                }
5952              }              }
5953                            
# Line 5005  sub _tree_construction_main ($) { Line 5958  sub _tree_construction_main ($) {
5958                !!!cp ('t262');                !!!cp ('t262');
5959                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5960                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5961                  !!!ack ('t262.1');
5962                !!!next-token;                !!!next-token;
5963                redo B;                next B;
5964              } else {              } else {
5965                !!!cp ('t263');                !!!cp ('t263');
5966                #                #
5967              }              }
5968            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
5969              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
5970                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5971                  !!!cp ('t264');                  !!!cp ('t264');
5972                  !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);                  !!!parse-error (type => 'unmatched end tag',
5973                                    text => 'colgroup', token => $token);
5974                  ## Ignore the token                  ## Ignore the token
5975                  !!!next-token;                  !!!next-token;
5976                  redo B;                  next B;
5977                } else {                } else {
5978                  !!!cp ('t265');                  !!!cp ('t265');
5979                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
5980                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5981                  !!!next-token;                  !!!next-token;
5982                  redo B;                              next B;            
5983                }                }
5984              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
5985                !!!cp ('t266');                !!!cp ('t266');
5986                !!!parse-error (type => 'unmatched end tag:col', token => $token);                !!!parse-error (type => 'unmatched end tag',
5987                                  text => 'col', token => $token);
5988                ## Ignore the token                ## Ignore the token
5989                !!!next-token;                !!!next-token;
5990                redo B;                next B;
5991              } else {              } else {
5992                !!!cp ('t267');                !!!cp ('t267');
5993                #                #
5994              }              }
5995        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5996          if ($self->{open_elements}->[-1]->[1] eq 'html' or          if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5997              @{$self->{open_elements}} == 1) { # redundant, maybe              @{$self->{open_elements}} == 1) { # redundant, maybe
5998            !!!cp ('t270.2');            !!!cp ('t270.2');
5999            ## Stop parsing.            ## Stop parsing.
# Line 5048  sub _tree_construction_main ($) { Line 6004  sub _tree_construction_main ($) {
6004            pop @{$self->{open_elements}}; # colgroup            pop @{$self->{open_elements}}; # colgroup
6005            $self->{insertion_mode} = IN_TABLE_IM;            $self->{insertion_mode} = IN_TABLE_IM;
6006            ## Reprocess.            ## Reprocess.
6007            redo B;            next B;
6008          }          }
6009        } else {        } else {
6010          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6011        }        }
6012    
6013            ## As if </colgroup>            ## As if </colgroup>
6014            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
6015              !!!cp ('t269');              !!!cp ('t269');
6016  ## TODO: Wrong error type?  ## TODO: Wrong error type?
6017              !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);              !!!parse-error (type => 'unmatched end tag',
6018                                text => 'colgroup', token => $token);
6019              ## Ignore the token              ## Ignore the token
6020                !!!nack ('t269.1');
6021              !!!next-token;              !!!next-token;
6022              redo B;              next B;
6023            } else {            } else {
6024              !!!cp ('t270');              !!!cp ('t270');
6025              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
6026              $self->{insertion_mode} = IN_TABLE_IM;              $self->{insertion_mode} = IN_TABLE_IM;
6027                !!!ack-later;
6028              ## reprocess              ## reprocess
6029              redo B;              next B;
6030            }            }
6031      } elsif ($self->{insertion_mode} & SELECT_IMS) {      } elsif ($self->{insertion_mode} & SELECT_IMS) {
6032        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
6033          !!!cp ('t271');          !!!cp ('t271');
6034          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
6035          !!!next-token;          !!!next-token;
6036          redo B;          next B;
6037        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6038              if ($token->{tag_name} eq 'option') {          if ($token->{tag_name} eq 'option') {
6039                if ($self->{open_elements}->[-1]->[1] eq 'option') {            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6040                  !!!cp ('t272');              !!!cp ('t272');
6041                  ## As if </option>              ## As if </option>
6042                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6043                } else {            } else {
6044                  !!!cp ('t273');              !!!cp ('t273');
6045                }            }
6046    
6047                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6048                !!!next-token;            !!!nack ('t273.1');
6049                redo B;            !!!next-token;
6050              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
6051                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
6052                  !!!cp ('t274');            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6053                  ## As if </option>              !!!cp ('t274');
6054                  pop @{$self->{open_elements}};              ## As if </option>
6055                } else {              pop @{$self->{open_elements}};
6056                  !!!cp ('t275');            } else {
6057                }              !!!cp ('t275');
6058              }
6059    
6060                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6061                  !!!cp ('t276');              !!!cp ('t276');
6062                  ## As if </optgroup>              ## As if </optgroup>
6063                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6064                } else {            } else {
6065                  !!!cp ('t277');              !!!cp ('t277');
6066                }            }
6067    
6068                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6069                !!!next-token;            !!!nack ('t277.1');
6070                redo B;            !!!next-token;
6071          } elsif ($token->{tag_name} eq 'select' or            next B;
6072                   $token->{tag_name} eq 'input' or          } elsif ({
6073                       select => 1, input => 1, textarea => 1,
6074                     }->{$token->{tag_name}} or
6075                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6076                    {                    {
6077                     caption => 1, table => 1,                     caption => 1, table => 1,
# Line 5117  sub _tree_construction_main ($) { Line 6079  sub _tree_construction_main ($) {
6079                     tr => 1, td => 1, th => 1,                     tr => 1, td => 1, th => 1,
6080                    }->{$token->{tag_name}})) {                    }->{$token->{tag_name}})) {
6081            ## TODO: The type below is not good - <select> is replaced by </select>            ## TODO: The type below is not good - <select> is replaced by </select>
6082            !!!parse-error (type => 'not closed:select', token => $token);            !!!parse-error (type => 'not closed', text => 'select',
6083                              token => $token);
6084            ## NOTE: As if the token were </select> (<select> case) or            ## NOTE: As if the token were </select> (<select> case) or
6085            ## as if there were </select> (otherwise).            ## as if there were </select> (otherwise).
6086                ## have an element in table scope            ## have an element in table scope
6087                my $i;            my $i;
6088                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6089                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6090                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
6091                    !!!cp ('t278');                !!!cp ('t278');
6092                    $i = $_;                $i = $_;
6093                    last INSCOPE;                last INSCOPE;
6094                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6095                            table => 1, html => 1,                !!!cp ('t279');
6096                           }->{$node->[1]}) {                last INSCOPE;
6097                    !!!cp ('t279');              }
6098                    last INSCOPE;            } # INSCOPE
6099                  }            unless (defined $i) {
6100                } # INSCOPE              !!!cp ('t280');
6101                unless (defined $i) {              !!!parse-error (type => 'unmatched end tag',
6102                  !!!cp ('t280');                              text => 'select', token => $token);
6103                  !!!parse-error (type => 'unmatched end tag:select', token => $token);              ## Ignore the token
6104                  ## Ignore the token              !!!nack ('t280.1');
6105                  !!!next-token;              !!!next-token;
6106                  redo B;              next B;
6107                }            }
6108                                
6109                !!!cp ('t281');            !!!cp ('t281');
6110                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6111    
6112                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6113    
6114            if ($token->{tag_name} eq 'select') {            if ($token->{tag_name} eq 'select') {
6115              !!!cp ('t281.2');              !!!nack ('t281.2');
6116              !!!next-token;              !!!next-token;
6117              redo B;              next B;
6118            } else {            } else {
6119              !!!cp ('t281.1');              !!!cp ('t281.1');
6120                !!!ack-later;
6121              ## Reprocess the token.              ## Reprocess the token.
6122              redo B;              next B;
6123            }            }
6124          } else {          } else {
6125            !!!cp ('t282');            !!!cp ('t282');
6126            !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'in select',
6127                              text => $token->{tag_name}, token => $token);
6128            ## Ignore the token            ## Ignore the token
6129              !!!nack ('t282.1');
6130            !!!next-token;            !!!next-token;
6131            redo B;            next B;
6132          }          }
6133        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6134              if ($token->{tag_name} eq 'optgroup') {          if ($token->{tag_name} eq 'optgroup') {
6135                if ($self->{open_elements}->[-1]->[1] eq 'option' and            if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
6136                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
6137                  !!!cp ('t283');              !!!cp ('t283');
6138                  ## As if </option>              ## As if </option>
6139                  splice @{$self->{open_elements}}, -2;              splice @{$self->{open_elements}}, -2;
6140                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6141                  !!!cp ('t284');              !!!cp ('t284');
6142                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6143                } else {            } else {
6144                  !!!cp ('t285');              !!!cp ('t285');
6145                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag',
6146                  ## Ignore the token                              text => $token->{tag_name}, token => $token);
6147                }              ## Ignore the token
6148                !!!next-token;            }
6149                redo B;            !!!nack ('t285.1');
6150              } elsif ($token->{tag_name} eq 'option') {            !!!next-token;
6151                if ($self->{open_elements}->[-1]->[1] eq 'option') {            next B;
6152                  !!!cp ('t286');          } elsif ($token->{tag_name} eq 'option') {
6153                  pop @{$self->{open_elements}};            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6154                } else {              !!!cp ('t286');
6155                  !!!cp ('t287');              pop @{$self->{open_elements}};
6156                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            } else {
6157                  ## Ignore the token              !!!cp ('t287');
6158                }              !!!parse-error (type => 'unmatched end tag',
6159                !!!next-token;                              text => $token->{tag_name}, token => $token);
6160                redo B;              ## Ignore the token
6161              } elsif ($token->{tag_name} eq 'select') {            }
6162                ## have an element in table scope            !!!nack ('t287.1');
6163                my $i;            !!!next-token;
6164                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            next B;
6165                  my $node = $self->{open_elements}->[$_];          } elsif ($token->{tag_name} eq 'select') {
6166                  if ($node->[1] eq $token->{tag_name}) {            ## have an element in table scope
6167                    !!!cp ('t288');            my $i;
6168                    $i = $_;            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6169                    last INSCOPE;              my $node = $self->{open_elements}->[$_];
6170                  } elsif ({              if ($node->[1] & SELECT_EL) {
6171                            table => 1, html => 1,                !!!cp ('t288');
6172                           }->{$node->[1]}) {                $i = $_;
6173                    !!!cp ('t289');                last INSCOPE;
6174                    last INSCOPE;              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6175                  }                !!!cp ('t289');
6176                } # INSCOPE                last INSCOPE;
6177                unless (defined $i) {              }
6178                  !!!cp ('t290');            } # INSCOPE
6179                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            unless (defined $i) {
6180                  ## Ignore the token              !!!cp ('t290');
6181                  !!!next-token;              !!!parse-error (type => 'unmatched end tag',
6182                  redo B;                              text => $token->{tag_name}, token => $token);
6183                }              ## Ignore the token
6184                !!!nack ('t290.1');
6185                !!!next-token;
6186                next B;
6187              }
6188                                
6189                !!!cp ('t291');            !!!cp ('t291');
6190                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6191    
6192                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6193    
6194                !!!next-token;            !!!nack ('t291.1');
6195                redo B;            !!!next-token;
6196              next B;
6197          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6198                   {                   {
6199                    caption => 1, table => 1, tbody => 1,                    caption => 1, table => 1, tbody => 1,
6200                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
6201                   }->{$token->{tag_name}}) {                   }->{$token->{tag_name}}) {
6202  ## TODO: The following is wrong?  ## TODO: The following is wrong?
6203                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
6204                              text => $token->{tag_name}, token => $token);
6205                                
6206                ## have an element in table scope            ## have an element in table scope
6207                my $i;            my $i;
6208                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6209                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6210                  if ($node->[1] eq $token->{tag_name}) {              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6211                    !!!cp ('t292');                !!!cp ('t292');
6212                    $i = $_;                $i = $_;
6213                    last INSCOPE;                last INSCOPE;
6214                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6215                            table => 1, html => 1,                !!!cp ('t293');
6216                           }->{$node->[1]}) {                last INSCOPE;
6217                    !!!cp ('t293');              }
6218                    last INSCOPE;            } # INSCOPE
6219                  }            unless (defined $i) {
6220                } # INSCOPE              !!!cp ('t294');
6221                unless (defined $i) {              ## Ignore the token
6222                  !!!cp ('t294');              !!!nack ('t294.1');
6223                  ## Ignore the token              !!!next-token;
6224                  !!!next-token;              next B;
6225                  redo B;            }
               }  
6226                                
6227                ## As if </select>            ## As if </select>
6228                ## have an element in table scope            ## have an element in table scope
6229                undef $i;            undef $i;
6230                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6231                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6232                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
6233                    !!!cp ('t295');                !!!cp ('t295');
6234                    $i = $_;                $i = $_;
6235                    last INSCOPE;                last INSCOPE;
6236                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
6237  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
6238                    !!!cp ('t296');                !!!cp ('t296');
6239                    last INSCOPE;                last INSCOPE;
6240                  }              }
6241                } # INSCOPE            } # INSCOPE
6242                unless (defined $i) {            unless (defined $i) {
6243                  !!!cp ('t297');              !!!cp ('t297');
6244  ## TODO: The following error type is correct?  ## TODO: The following error type is correct?
6245                  !!!parse-error (type => 'unmatched end tag:select', token => $token);              !!!parse-error (type => 'unmatched end tag',
6246                  ## Ignore the </select> token                              text => 'select', token => $token);
6247                  !!!next-token; ## TODO: ok?              ## Ignore the </select> token
6248                  redo B;              !!!nack ('t297.1');
6249                }              !!!next-token; ## TODO: ok?
6250                next B;
6251              }
6252                                
6253                !!!cp ('t298');            !!!cp ('t298');
6254                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6255    
6256                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6257    
6258                ## reprocess            !!!ack-later;
6259                redo B;            ## reprocess
6260              next B;
6261          } else {          } else {
6262            !!!cp ('t299');            !!!cp ('t299');
6263            !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'in select:/',
6264                              text => $token->{tag_name}, token => $token);
6265            ## Ignore the token            ## Ignore the token
6266              !!!nack ('t299.3');
6267            !!!next-token;            !!!next-token;
6268            redo B;            next B;
6269          }          }
6270        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6271          unless ($self->{open_elements}->[-1]->[1] eq 'html' and          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6272                  @{$self->{open_elements}} == 1) { # redundant, maybe                  @{$self->{open_elements}} == 1) { # redundant, maybe
6273            !!!cp ('t299.1');            !!!cp ('t299.1');
6274            !!!parse-error (type => 'in body:#eof', token => $token);            !!!parse-error (type => 'in body:#eof', token => $token);
# Line 5319  sub _tree_construction_main ($) { Line 6293  sub _tree_construction_main ($) {
6293            unless (length $token->{data}) {            unless (length $token->{data}) {
6294              !!!cp ('t300');              !!!cp ('t300');
6295              !!!next-token;              !!!next-token;
6296              redo B;              next B;
6297            }            }
6298          }          }
6299                    
6300          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6301            !!!cp ('t301');            !!!cp ('t301');
6302            !!!parse-error (type => 'after html:#character', token => $token);            !!!parse-error (type => 'after html:#text', token => $token);
6303    
6304            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
6305          } else {          } else {
# Line 5333  sub _tree_construction_main ($) { Line 6307  sub _tree_construction_main ($) {
6307          }          }
6308                    
6309          ## "after body" insertion mode          ## "after body" insertion mode
6310          !!!parse-error (type => 'after body:#character', token => $token);          !!!parse-error (type => 'after body:#text', token => $token);
6311    
6312          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6313          ## reprocess          ## reprocess
6314          redo B;          next B;
6315        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6316          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6317            !!!cp ('t303');            !!!cp ('t303');
6318            !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'after html',
6319                              text => $token->{tag_name}, token => $token);
6320                        
6321            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
6322          } else {          } else {
# Line 5349  sub _tree_construction_main ($) { Line 6324  sub _tree_construction_main ($) {
6324          }          }
6325    
6326          ## "after body" insertion mode          ## "after body" insertion mode
6327          !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'after body',
6328                            text => $token->{tag_name}, token => $token);
6329    
6330          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6331            !!!ack-later;
6332          ## reprocess          ## reprocess
6333          redo B;          next B;
6334        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6335          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6336            !!!cp ('t305');            !!!cp ('t305');
6337            !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'after html:/',
6338                              text => $token->{tag_name}, token => $token);
6339                        
6340            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6341            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
# Line 5369  sub _tree_construction_main ($) { Line 6347  sub _tree_construction_main ($) {
6347          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
6348            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
6349              !!!cp ('t307');              !!!cp ('t307');
6350              !!!parse-error (type => 'unmatched end tag:html', token => $token);              !!!parse-error (type => 'unmatched end tag',
6351                                text => 'html', token => $token);
6352              ## Ignore the token              ## Ignore the token
6353              !!!next-token;              !!!next-token;
6354              redo B;              next B;
6355            } else {            } else {
6356              !!!cp ('t308');              !!!cp ('t308');
6357              $self->{insertion_mode} = AFTER_HTML_BODY_IM;              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6358              !!!next-token;              !!!next-token;
6359              redo B;              next B;
6360            }            }
6361          } else {          } else {
6362            !!!cp ('t309');            !!!cp ('t309');
6363            !!!parse-error (type => 'after body:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'after body:/',
6364                              text => $token->{tag_name}, token => $token);
6365    
6366            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
6367            ## reprocess            ## reprocess
6368            redo B;            next B;
6369          }          }
6370        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6371          !!!cp ('t309.2');          !!!cp ('t309.2');
# Line 5402  sub _tree_construction_main ($) { Line 6382  sub _tree_construction_main ($) {
6382            unless (length $token->{data}) {            unless (length $token->{data}) {
6383              !!!cp ('t310');              !!!cp ('t310');
6384              !!!next-token;              !!!next-token;
6385              redo B;              next B;
6386            }            }
6387          }          }
6388                    
6389          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
6390            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6391              !!!cp ('t311');              !!!cp ('t311');
6392              !!!parse-error (type => 'in frameset:#character', token => $token);              !!!parse-error (type => 'in frameset:#text', token => $token);
6393            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6394              !!!cp ('t312');              !!!cp ('t312');
6395              !!!parse-error (type => 'after frameset:#character', token => $token);              !!!parse-error (type => 'after frameset:#text', token => $token);
6396            } else { # "after html frameset"            } else { # "after after frameset"
6397              !!!cp ('t313');              !!!cp ('t313');
6398              !!!parse-error (type => 'after html:#character', token => $token);              !!!parse-error (type => 'after html:#text', token => $token);
   
             $self->{insertion_mode} = AFTER_FRAMESET_IM;  
             ## Reprocess in the "after frameset" insertion mode.  
             !!!parse-error (type => 'after frameset:#character', token => $token);  
6399            }            }
6400                        
6401            ## Ignore the token.            ## Ignore the token.
# Line 5430  sub _tree_construction_main ($) { Line 6406  sub _tree_construction_main ($) {
6406              !!!cp ('t315');              !!!cp ('t315');
6407              !!!next-token;              !!!next-token;
6408            }            }
6409            redo B;            next B;
6410          }          }
6411                    
6412          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
6413        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
         if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {  
           !!!cp ('t316');  
           !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);  
   
           $self->{insertion_mode} = AFTER_FRAMESET_IM;  
           ## Process in the "after frameset" insertion mode.  
         } else {  
           !!!cp ('t317');  
         }  
   
6414          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6415              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6416            !!!cp ('t318');            !!!cp ('t318');
6417            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6418              !!!nack ('t318.1');
6419            !!!next-token;            !!!next-token;
6420            redo B;            next B;
6421          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
6422                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
6423            !!!cp ('t319');            !!!cp ('t319');
6424            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6425            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6426              !!!ack ('t319.1');
6427            !!!next-token;            !!!next-token;
6428            redo B;            next B;
6429          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
6430            !!!cp ('t320');            !!!cp ('t320');
6431            ## NOTE: As if in body.            ## NOTE: As if in head.
6432            $parse_rcdata->(CDATA_CONTENT_MODEL);            $parse_rcdata->(CDATA_CONTENT_MODEL);
6433            redo B;            next B;
6434    
6435              ## NOTE: |<!DOCTYPE HTML><frameset></frameset></html><noframes></noframes>|
6436              ## has no parse error.
6437          } else {          } else {
6438            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6439              !!!cp ('t321');              !!!cp ('t321');
6440              !!!parse-error (type => 'in frameset:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'in frameset',
6441            } else {                              text => $token->{tag_name}, token => $token);
6442              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6443              !!!cp ('t322');              !!!cp ('t322');
6444              !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'after frameset',
6445                                text => $token->{tag_name}, token => $token);
6446              } else { # "after after frameset"
6447                !!!cp ('t322.2');
6448                !!!parse-error (type => 'after after frameset',
6449                                text => $token->{tag_name}, token => $token);
6450            }            }
6451            ## Ignore the token            ## Ignore the token
6452              !!!nack ('t322.1');
6453            !!!next-token;            !!!next-token;
6454            redo B;            next B;
6455          }          }
6456        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
         if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {  
           !!!cp ('t323');  
           !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);  
   
           $self->{insertion_mode} = AFTER_FRAMESET_IM;  
           ## Process in the "after frameset" insertion mode.  
         } else {  
           !!!cp ('t324');  
         }  
   
6457          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6458              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6459            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6460                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
6461              !!!cp ('t325');              !!!cp ('t325');
6462              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag',
6463                                text => $token->{tag_name}, token => $token);
6464              ## Ignore the token              ## Ignore the token
6465              !!!next-token;              !!!next-token;
6466            } else {            } else {
# Line 5501  sub _tree_construction_main ($) { Line 6470  sub _tree_construction_main ($) {
6470            }            }
6471    
6472            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
6473                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6474              !!!cp ('t327');              !!!cp ('t327');
6475              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6476            } else {            } else {
6477              !!!cp ('t328');              !!!cp ('t328');
6478            }            }
6479            redo B;            next B;
6480          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
6481                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6482            !!!cp ('t329');            !!!cp ('t329');
6483            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6484            !!!next-token;            !!!next-token;
6485            redo B;            next B;
6486          } else {          } else {
6487            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6488              !!!cp ('t330');              !!!cp ('t330');
6489              !!!parse-error (type => 'in frameset:/'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'in frameset:/',
6490            } else {                              text => $token->{tag_name}, token => $token);
6491              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6492                !!!cp ('t330.1');
6493                !!!parse-error (type => 'after frameset:/',
6494                                text => $token->{tag_name}, token => $token);
6495              } else { # "after after html"
6496              !!!cp ('t331');              !!!cp ('t331');
6497              !!!parse-error (type => 'after frameset:/'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'after after frameset:/',
6498                                text => $token->{tag_name}, token => $token);
6499            }            }
6500            ## Ignore the token            ## Ignore the token
6501            !!!next-token;            !!!next-token;
6502            redo B;            next B;
6503          }          }
6504        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6505          unless ($self->{open_elements}->[-1]->[1] eq 'html' and          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6506                  @{$self->{open_elements}} == 1) { # redundant, maybe                  @{$self->{open_elements}} == 1) { # redundant, maybe
6507            !!!cp ('t331.1');            !!!cp ('t331.1');
6508            !!!parse-error (type => 'in body:#eof', token => $token);            !!!parse-error (type => 'in body:#eof', token => $token);
# Line 5552  sub _tree_construction_main ($) { Line 6527  sub _tree_construction_main ($) {
6527          !!!cp ('t332');          !!!cp ('t332');
6528          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6529          $script_start_tag->();          $script_start_tag->();
6530          redo B;          next B;
6531        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
6532          !!!cp ('t333');          !!!cp ('t333');
6533          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6534          $parse_rcdata->(CDATA_CONTENT_MODEL);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6535          redo B;          next B;
6536        } elsif ({        } elsif ({
6537                  base => 1, link => 1,                  base => 1, link => 1,
6538                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 5565  sub _tree_construction_main ($) { Line 6540  sub _tree_construction_main ($) {
6540          ## 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
6541          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6542          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6543            !!!ack ('t334.1');
6544          !!!next-token;          !!!next-token;
6545          redo B;          next B;
6546        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
6547          ## 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
6548          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6549          my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6550    
6551          unless ($self->{confident}) {          unless ($self->{confident}) {
6552            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) {
6553              !!!cp ('t335');              !!!cp ('t335');
6554                ## NOTE: Whether the encoding is supported or not is handled
6555                ## in the {change_encoding} callback.
6556              $self->{change_encoding}              $self->{change_encoding}
6557                  ->($self, $token->{attributes}->{charset}->{value}, $token);                  ->($self, $token->{attributes}->{charset}->{value}, $token);
6558                            
# Line 5583  sub _tree_construction_main ($) { Line 6561  sub _tree_construction_main ($) {
6561                                       $token->{attributes}->{charset}                                       $token->{attributes}->{charset}
6562                                           ->{has_reference});                                           ->{has_reference});
6563            } elsif ($token->{attributes}->{content}) {            } elsif ($token->{attributes}->{content}) {
             ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
6564              if ($token->{attributes}->{content}->{value}              if ($token->{attributes}->{content}->{value}
6565                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                  =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6566                      [\x09-\x0D\x20]*=                      [\x09-\x0D\x20]*=
6567                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6568                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
6569                !!!cp ('t336');                !!!cp ('t336');
6570                  ## NOTE: Whether the encoding is supported or not is handled
6571                  ## in the {change_encoding} callback.
6572                $self->{change_encoding}                $self->{change_encoding}
6573                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6574                $meta_el->[0]->get_attribute_node_ns (undef, 'content')                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
# Line 5615  sub _tree_construction_main ($) { Line 6594  sub _tree_construction_main ($) {
6594            }            }
6595          }          }
6596    
6597            !!!ack ('t338.1');
6598          !!!next-token;          !!!next-token;
6599          redo B;          next B;
6600        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
6601          !!!cp ('t341');          !!!cp ('t341');
6602          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6603          $parse_rcdata->(RCDATA_CONTENT_MODEL);          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6604          redo B;          next B;
6605        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
6606          !!!parse-error (type => 'in body:body', token => $token);          !!!parse-error (type => 'in body', text => 'body', token => $token);
6607                                
6608          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
6609              $self->{open_elements}->[1]->[1] ne 'body') {              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6610            !!!cp ('t342');            !!!cp ('t342');
6611            ## Ignore the token            ## Ignore the token
6612          } else {          } else {
# Line 5640  sub _tree_construction_main ($) { Line 6620  sub _tree_construction_main ($) {
6620              }              }
6621            }            }
6622          }          }
6623            !!!nack ('t343.1');
6624          !!!next-token;          !!!next-token;
6625          redo B;          next B;
6626        } elsif ({        } elsif ({
6627                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6628                  div => 1, dl => 1, fieldset => 1,                  div => 1, dl => 1, fieldset => 1,
# Line 5656  sub _tree_construction_main ($) { Line 6637  sub _tree_construction_main ($) {
6637            !!!cp ('t350');            !!!cp ('t350');
6638            !!!parse-error (type => 'in form:form', token => $token);            !!!parse-error (type => 'in form:form', token => $token);
6639            ## Ignore the token            ## Ignore the token
6640              !!!nack ('t350.1');
6641            !!!next-token;            !!!next-token;
6642            redo B;            next B;
6643          }          }
6644    
6645          ## has a p element in scope          ## has a p element in scope
6646          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6647            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6648              !!!cp ('t344');              !!!cp ('t344');
6649              !!!back-token;              !!!back-token; # <form>
6650              $token = {type => END_TAG_TOKEN, tag_name => 'p',              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6651                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6652              redo B;              next B;
6653            } elsif ({            } elsif ($_->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6654              !!!cp ('t345');              !!!cp ('t345');
6655              last INSCOPE;              last INSCOPE;
6656            }            }
# Line 5679  sub _tree_construction_main ($) { Line 6658  sub _tree_construction_main ($) {
6658                        
6659          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6660          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6661              !!!nack ('t346.1');
6662            !!!next-token;            !!!next-token;
6663            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
6664              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
# Line 5695  sub _tree_construction_main ($) { Line 6675  sub _tree_construction_main ($) {
6675            !!!cp ('t347.1');            !!!cp ('t347.1');
6676            $self->{form_element} = $self->{open_elements}->[-1]->[0];            $self->{form_element} = $self->{open_elements}->[-1]->[0];
6677    
6678              !!!nack ('t347.2');
6679            !!!next-token;            !!!next-token;
6680          } elsif ($token->{tag_name} eq 'table') {          } elsif ($token->{tag_name} eq 'table') {
6681            !!!cp ('t382');            !!!cp ('t382');
# Line 5702  sub _tree_construction_main ($) { Line 6683  sub _tree_construction_main ($) {
6683                        
6684            $self->{insertion_mode} = IN_TABLE_IM;            $self->{insertion_mode} = IN_TABLE_IM;
6685    
6686              !!!nack ('t382.1');
6687            !!!next-token;            !!!next-token;
6688          } elsif ($token->{tag_name} eq 'hr') {          } elsif ($token->{tag_name} eq 'hr') {
6689            !!!cp ('t386');            !!!cp ('t386');
6690            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6691                    
6692              !!!nack ('t386.1');
6693            !!!next-token;            !!!next-token;
6694          } else {          } else {
6695            !!!cp ('t347');            !!!nack ('t347.1');
6696            !!!next-token;            !!!next-token;
6697          }          }
6698          redo B;          next B;
6699        } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {        } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
6700          ## has a p element in scope          ## has a p element in scope
6701          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6702            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6703              !!!cp ('t353');              !!!cp ('t353');
6704              !!!back-token;              !!!back-token; # <x>
6705              $token = {type => END_TAG_TOKEN, tag_name => 'p',              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6706                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6707              redo B;              next B;
6708            } elsif ({            } elsif ($_->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6709              !!!cp ('t354');              !!!cp ('t354');
6710              last INSCOPE;              last INSCOPE;
6711            }            }
# Line 5739  sub _tree_construction_main ($) { Line 6719  sub _tree_construction_main ($) {
6719                            dd => {dt => 1, dd => 1}}->{$token->{tag_name}};                            dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6720          LI: {          LI: {
6721            ## Step 2            ## Step 2
6722            if ($li_or_dtdd->{$node->[1]}) {            if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6723              if ($i != -1) {              if ($i != -1) {
6724                !!!cp ('t355');                !!!cp ('t355');
6725                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'not closed',
6726                                $self->{open_elements}->[-1]->[1], token => $token);                                text => $self->{open_elements}->[-1]->[0]
6727                                      ->manakai_local_name,
6728                                  token => $token);
6729              } else {              } else {
6730                !!!cp ('t356');                !!!cp ('t356');
6731              }              }
# Line 5754  sub _tree_construction_main ($) { Line 6736  sub _tree_construction_main ($) {
6736            }            }
6737                        
6738            ## Step 3            ## Step 3
6739            if (not $formatting_category->{$node->[1]} and            if (not ($node->[1] & FORMATTING_EL) and
6740                #not $phrasing_category->{$node->[1]} and                #not $phrasing_category->{$node->[1]} and
6741                ($special_category->{$node->[1]} or                ($node->[1] & SPECIAL_EL or
6742                 $scoping_category->{$node->[1]}) and                 $node->[1] & SCOPING_EL) and
6743                $node->[1] ne 'address' and $node->[1] ne 'div') {                not ($node->[1] & ADDRESS_EL) and
6744                  not ($node->[1] & DIV_EL)) {
6745              !!!cp ('t358');              !!!cp ('t358');
6746              last LI;              last LI;
6747            }            }
# Line 5771  sub _tree_construction_main ($) { Line 6754  sub _tree_construction_main ($) {
6754          } # LI          } # LI
6755                        
6756          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6757            !!!nack ('t359.1');
6758          !!!next-token;          !!!next-token;
6759          redo B;          next B;
6760        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
6761          ## has a p element in scope          ## has a p element in scope
6762          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6763            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6764              !!!cp ('t367');              !!!cp ('t367');
6765              !!!back-token;              !!!back-token; # <plaintext>
6766              $token = {type => END_TAG_TOKEN, tag_name => 'p',              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6767                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6768              redo B;              next B;
6769            } elsif ({            } elsif ($_->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6770              !!!cp ('t368');              !!!cp ('t368');
6771              last INSCOPE;              last INSCOPE;
6772            }            }
# Line 5795  sub _tree_construction_main ($) { Line 6776  sub _tree_construction_main ($) {
6776                        
6777          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
6778                        
6779            !!!nack ('t368.1');
6780          !!!next-token;          !!!next-token;
6781          redo B;          next B;
6782        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
6783          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
6784            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
6785            if ($node->[1] eq 'a') {            if ($node->[1] & A_EL) {
6786              !!!cp ('t371');              !!!cp ('t371');
6787              !!!parse-error (type => 'in a:a', token => $token);              !!!parse-error (type => 'in a:a', token => $token);
6788                            
6789              !!!back-token;              !!!back-token; # <a>
6790              $token = {type => END_TAG_TOKEN, tag_name => 'a',              $token = {type => END_TAG_TOKEN, tag_name => 'a',
6791                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6792              $formatting_end_tag->($token);              $formatting_end_tag->($token);
# Line 5835  sub _tree_construction_main ($) { Line 6817  sub _tree_construction_main ($) {
6817          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6818          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6819    
6820            !!!nack ('t374.1');
6821          !!!next-token;          !!!next-token;
6822          redo B;          next B;
6823        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
6824          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6825    
6826          ## has a |nobr| element in scope          ## has a |nobr| element in scope
6827          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6828            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6829            if ($node->[1] eq 'nobr') {            if ($node->[1] & NOBR_EL) {
6830              !!!cp ('t376');              !!!cp ('t376');
6831              !!!parse-error (type => 'in nobr:nobr', token => $token);              !!!parse-error (type => 'in nobr:nobr', token => $token);
6832              !!!back-token;              !!!back-token; # <nobr>
6833              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
6834                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6835              redo B;              next B;
6836            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6837              !!!cp ('t377');              !!!cp ('t377');
6838              last INSCOPE;              last INSCOPE;
6839            }            }
# Line 5862  sub _tree_construction_main ($) { Line 6842  sub _tree_construction_main ($) {
6842          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6843          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6844                    
6845            !!!nack ('t377.1');
6846          !!!next-token;          !!!next-token;
6847          redo B;          next B;
6848        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
6849          ## has a button element in scope          ## has a button element in scope
6850          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6851            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6852            if ($node->[1] eq 'button') {            if ($node->[1] & BUTTON_EL) {
6853              !!!cp ('t378');              !!!cp ('t378');
6854              !!!parse-error (type => 'in button:button', token => $token);              !!!parse-error (type => 'in button:button', token => $token);
6855              !!!back-token;              !!!back-token; # <button>
6856              $token = {type => END_TAG_TOKEN, tag_name => 'button',              $token = {type => END_TAG_TOKEN, tag_name => 'button',
6857                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6858              redo B;              next B;
6859            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6860              !!!cp ('t379');              !!!cp ('t379');
6861              last INSCOPE;              last INSCOPE;
6862            }            }
# Line 5892  sub _tree_construction_main ($) { Line 6870  sub _tree_construction_main ($) {
6870    
6871          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
6872    
6873            !!!nack ('t379.1');
6874          !!!next-token;          !!!next-token;
6875          redo B;          next B;
6876        } elsif ({        } elsif ({
6877                  xmp => 1,                  xmp => 1,
6878                  iframe => 1,                  iframe => 1,
6879                  noembed => 1,                  noembed => 1,
6880                  noframes => 1,                  noframes => 1, ## NOTE: This is an "as if in head" code clone.
6881                  noscript => 0, ## TODO: 1 if scripting is enabled                  noscript => 0, ## TODO: 1 if scripting is enabled
6882                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6883          if ($token->{tag_name} eq 'xmp') {          if ($token->{tag_name} eq 'xmp') {
# Line 5909  sub _tree_construction_main ($) { Line 6888  sub _tree_construction_main ($) {
6888          }          }
6889          ## NOTE: There is an "as if in body" code clone.          ## NOTE: There is an "as if in body" code clone.
6890          $parse_rcdata->(CDATA_CONTENT_MODEL);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6891          redo B;          next B;
6892        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
6893          !!!parse-error (type => 'isindex', token => $token);          !!!parse-error (type => 'isindex', token => $token);
6894                    
6895          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
6896            !!!cp ('t389');            !!!cp ('t389');
6897            ## Ignore the token            ## Ignore the token
6898              !!!nack ('t389'); ## NOTE: Not acknowledged.
6899            !!!next-token;            !!!next-token;
6900            redo B;            next B;
6901          } else {          } else {
6902              !!!ack ('t391.1');
6903    
6904            my $at = $token->{attributes};            my $at = $token->{attributes};
6905            my $form_attrs;            my $form_attrs;
6906            $form_attrs->{action} = $at->{action} if $at->{action};            $form_attrs->{action} = $at->{action} if $at->{action};
# Line 5962  sub _tree_construction_main ($) { Line 6944  sub _tree_construction_main ($) {
6944                           line => $token->{line}, column => $token->{column}},                           line => $token->{line}, column => $token->{column}},
6945                          {type => END_TAG_TOKEN, tag_name => 'form',                          {type => END_TAG_TOKEN, tag_name => 'form',
6946                           line => $token->{line}, column => $token->{column}};                           line => $token->{line}, column => $token->{column}};
           $token = shift @tokens;  
6947            !!!back-token (@tokens);            !!!back-token (@tokens);
6948            redo B;            !!!next-token;
6949              next B;
6950          }          }
6951        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
6952          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
6953          my $el;          my $el;
6954          !!!create-element ($el, $token->{tag_name}, $token->{attributes}, $token);          !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
6955                    
6956          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
6957          $self->{content_model} = RCDATA_CONTENT_MODEL;          $self->{content_model} = RCDATA_CONTENT_MODEL;
# Line 5978  sub _tree_construction_main ($) { Line 6960  sub _tree_construction_main ($) {
6960          $insert->($el);          $insert->($el);
6961                    
6962          my $text = '';          my $text = '';
6963            !!!nack ('t392.1');
6964          !!!next-token;          !!!next-token;
6965          if ($token->{type} == CHARACTER_TOKEN) {          if ($token->{type} == CHARACTER_TOKEN) {
6966            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
# Line 6008  sub _tree_construction_main ($) { Line 6991  sub _tree_construction_main ($) {
6991            ## Ignore the token            ## Ignore the token
6992          } else {          } else {
6993            !!!cp ('t398');            !!!cp ('t398');
6994            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);            !!!parse-error (type => 'in RCDATA:#eof', token => $token);
6995          }          }
6996          !!!next-token;          !!!next-token;
6997            next B;
6998          } elsif ($token->{tag_name} eq 'rt' or
6999                   $token->{tag_name} eq 'rp') {
7000            ## has a |ruby| element in scope
7001            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7002              my $node = $self->{open_elements}->[$_];
7003              if ($node->[1] & RUBY_EL) {
7004                !!!cp ('t398.1');
7005                ## generate implied end tags
7006                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7007                  !!!cp ('t398.2');
7008                  pop @{$self->{open_elements}};
7009                }
7010                unless ($self->{open_elements}->[-1]->[1] & RUBY_EL) {
7011                  !!!cp ('t398.3');
7012                  !!!parse-error (type => 'not closed',
7013                                  text => $self->{open_elements}->[-1]->[0]
7014                                      ->manakai_local_name,
7015                                  token => $token);
7016                  pop @{$self->{open_elements}}
7017                      while not $self->{open_elements}->[-1]->[1] & RUBY_EL;
7018                }
7019                last INSCOPE;
7020              } elsif ($node->[1] & SCOPING_EL) {
7021                !!!cp ('t398.4');
7022                last INSCOPE;
7023              }
7024            } # INSCOPE
7025    
7026            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7027    
7028            !!!nack ('t398.5');
7029            !!!next-token;
7030          redo B;          redo B;
7031          } elsif ($token->{tag_name} eq 'math' or
7032                   $token->{tag_name} eq 'svg') {
7033            $reconstruct_active_formatting_elements->($insert_to_current);
7034    
7035            ## "Adjust MathML attributes" ('math' only) - done in insert-element-f
7036    
7037            ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
7038    
7039            ## "adjust foreign attributes" - done in insert-element-f
7040            
7041            !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
7042            
7043            if ($self->{self_closing}) {
7044              pop @{$self->{open_elements}};
7045              !!!ack ('t398.1');
7046            } else {
7047              !!!cp ('t398.2');
7048              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
7049              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
7050              ## mode, "in body" (not "in foreign content") secondary insertion
7051              ## mode, maybe.
7052            }
7053    
7054            !!!next-token;
7055            next B;
7056        } elsif ({        } elsif ({
7057                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7058                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 6019  sub _tree_construction_main ($) { Line 7060  sub _tree_construction_main ($) {
7060                  thead => 1, tr => 1,                  thead => 1, tr => 1,
7061                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7062          !!!cp ('t401');          !!!cp ('t401');
7063          !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'in body',
7064                            text => $token->{tag_name}, token => $token);
7065          ## Ignore the token          ## Ignore the token
7066            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
7067          !!!next-token;          !!!next-token;
7068          redo B;          next B;
7069                    
7070          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
7071        } else {        } else {
# Line 6044  sub _tree_construction_main ($) { Line 7087  sub _tree_construction_main ($) {
7087              }->{$token->{tag_name}}) {              }->{$token->{tag_name}}) {
7088            !!!cp ('t380');            !!!cp ('t380');
7089            push @$active_formatting_elements, ['#marker', ''];            push @$active_formatting_elements, ['#marker', ''];
7090              !!!nack ('t380.1');
7091          } elsif ({          } elsif ({
7092                    b => 1, big => 1, em => 1, font => 1, i => 1,                    b => 1, big => 1, em => 1, font => 1, i => 1,
7093                    s => 1, small => 1, strile => 1,                    s => 1, small => 1, strile => 1,
# Line 6051  sub _tree_construction_main ($) { Line 7095  sub _tree_construction_main ($) {
7095                   }->{$token->{tag_name}}) {                   }->{$token->{tag_name}}) {
7096            !!!cp ('t375');            !!!cp ('t375');
7097            push @$active_formatting_elements, $self->{open_elements}->[-1];            push @$active_formatting_elements, $self->{open_elements}->[-1];
7098              !!!nack ('t375.1');
7099          } elsif ($token->{tag_name} eq 'input') {          } elsif ($token->{tag_name} eq 'input') {
7100            !!!cp ('t388');            !!!cp ('t388');
7101            ## TODO: associate with $self->{form_element} if defined            ## TODO: associate with $self->{form_element} if defined
7102            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
7103              !!!ack ('t388.2');
7104          } elsif ({          } elsif ({
7105                    area => 1, basefont => 1, bgsound => 1, br => 1,                    area => 1, basefont => 1, bgsound => 1, br => 1,
7106                    embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                    embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
# Line 6062  sub _tree_construction_main ($) { Line 7108  sub _tree_construction_main ($) {
7108                   }->{$token->{tag_name}}) {                   }->{$token->{tag_name}}) {
7109            !!!cp ('t388.1');            !!!cp ('t388.1');
7110            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
7111              !!!ack ('t388.3');
7112          } elsif ($token->{tag_name} eq 'select') {          } elsif ($token->{tag_name} eq 'select') {
7113            ## TODO: associate with $self->{form_element} if defined            ## TODO: associate with $self->{form_element} if defined
7114                    
# Line 6074  sub _tree_construction_main ($) { Line 7121  sub _tree_construction_main ($) {
7121              !!!cp ('t400.2');              !!!cp ('t400.2');
7122              $self->{insertion_mode} = IN_SELECT_IM;              $self->{insertion_mode} = IN_SELECT_IM;
7123            }            }
7124              !!!nack ('t400.3');
7125          } else {          } else {
7126            !!!cp ('t402');            !!!nack ('t402');
7127          }          }
7128                    
7129          !!!next-token;          !!!next-token;
7130          redo B;          next B;
7131        }        }
7132      } elsif ($token->{type} == END_TAG_TOKEN) {      } elsif ($token->{type} == END_TAG_TOKEN) {
7133        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
# Line 6087  sub _tree_construction_main ($) { Line 7135  sub _tree_construction_main ($) {
7135          my $i;          my $i;
7136          INSCOPE: {          INSCOPE: {
7137            for (reverse @{$self->{open_elements}}) {            for (reverse @{$self->{open_elements}}) {
7138              if ($_->[1] eq 'body') {              if ($_->[1] & BODY_EL) {
7139                !!!cp ('t405');                !!!cp ('t405');
7140                $i = $_;                $i = $_;
7141                last INSCOPE;                last INSCOPE;
7142              } elsif ({              } elsif ($_->[1] & SCOPING_EL) {
                       applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                       button => 1, marquee => 1, object => 1, html => 1,  
                      }->{$_->[1]}) {  
7143                !!!cp ('t405.1');                !!!cp ('t405.1');
7144                last;                last;
7145              }              }
7146            }            }
7147    
7148            !!!parse-error (type => 'start tag not allowed',            !!!parse-error (type => 'start tag not allowed',
7149                            value => $token->{tag_name}, token => $token);                            text => $token->{tag_name}, token => $token);
7150            ## NOTE: Ignore the token.            ## NOTE: Ignore the token.
7151            !!!next-token;            !!!next-token;
7152            redo B;            next B;
7153          } # INSCOPE          } # INSCOPE
7154    
7155          for (@{$self->{open_elements}}) {          for (@{$self->{open_elements}}) {
7156            unless ({            unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
                    dd => 1, dt => 1, li => 1, p => 1, td => 1,  
                    th => 1, tr => 1, body => 1, html => 1,  
                    tbody => 1, tfoot => 1, thead => 1,  
                   }->{$_->[1]}) {  
7157              !!!cp ('t403');              !!!cp ('t403');
7158              !!!parse-error (type => 'not closed:'.$_->[1], token => $token);              !!!parse-error (type => 'not closed',
7159                                text => $_->[0]->manakai_local_name,
7160                                token => $token);
7161              last;              last;
7162            } else {            } else {
7163              !!!cp ('t404');              !!!cp ('t404');
# Line 6123  sub _tree_construction_main ($) { Line 7166  sub _tree_construction_main ($) {
7166    
7167          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
7168          !!!next-token;          !!!next-token;
7169          redo B;          next B;
7170        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
7171          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
7172            ## up-to-date, though it has same effect as speced.
7173            if (@{$self->{open_elements}} > 1 and
7174                $self->{open_elements}->[1]->[1] & BODY_EL) {
7175            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
7176            if ($self->{open_elements}->[-1]->[1] ne 'body') {            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
7177              !!!cp ('t406');              !!!cp ('t406');
7178              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1], token => $token);              !!!parse-error (type => 'not closed',
7179                                text => $self->{open_elements}->[1]->[0]
7180                                    ->manakai_local_name,
7181                                token => $token);
7182            } else {            } else {
7183              !!!cp ('t407');              !!!cp ('t407');
7184            }            }
7185            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
7186            ## reprocess            ## reprocess
7187            redo B;            next B;
7188          } else {          } else {
7189            !!!cp ('t408');            !!!cp ('t408');
7190            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
7191                              text => $token->{tag_name}, token => $token);
7192            ## Ignore the token            ## Ignore the token
7193            !!!next-token;            !!!next-token;
7194            redo B;            next B;
7195          }          }
7196        } elsif ({        } elsif ({
7197                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
# Line 6154  sub _tree_construction_main ($) { Line 7204  sub _tree_construction_main ($) {
7204          my $i;          my $i;
7205          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7206            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7207            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7208              !!!cp ('t410');              !!!cp ('t410');
7209              $i = $_;              $i = $_;
7210              last INSCOPE;              last INSCOPE;
7211            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7212              !!!cp ('t411');              !!!cp ('t411');
7213              last INSCOPE;              last INSCOPE;
7214            }            }
# Line 6169  sub _tree_construction_main ($) { Line 7216  sub _tree_construction_main ($) {
7216    
7217          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
7218            !!!cp ('t413');            !!!cp ('t413');
7219            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
7220                              text => $token->{tag_name}, token => $token);
7221              ## NOTE: Ignore the token.
7222          } else {          } else {
7223            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7224            while ({            while ({
7225                      ## END_TAG_OPTIONAL_EL
7226                    dd => ($token->{tag_name} ne 'dd'),                    dd => ($token->{tag_name} ne 'dd'),
7227                    dt => ($token->{tag_name} ne 'dt'),                    dt => ($token->{tag_name} ne 'dt'),
7228                    li => ($token->{tag_name} ne 'li'),                    li => ($token->{tag_name} ne 'li'),
7229                    p => 1,                    p => 1,
7230                   }->{$self->{open_elements}->[-1]->[1]}) {                    rt => 1,
7231                      rp => 1,
7232                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
7233              !!!cp ('t409');              !!!cp ('t409');
7234              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
7235            }            }
7236    
7237            ## Step 2.            ## Step 2.
7238            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7239                      ne $token->{tag_name}) {
7240              !!!cp ('t412');              !!!cp ('t412');
7241              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);              !!!parse-error (type => 'not closed',
7242                                text => $self->{open_elements}->[-1]->[0]
7243                                    ->manakai_local_name,
7244                                token => $token);
7245            } else {            } else {
7246              !!!cp ('t414');              !!!cp ('t414');
7247            }            }
# Line 6200  sub _tree_construction_main ($) { Line 7256  sub _tree_construction_main ($) {
7256                }->{$token->{tag_name}};                }->{$token->{tag_name}};
7257          }          }
7258          !!!next-token;          !!!next-token;
7259          redo B;          next B;
7260        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
7261          undef $self->{form_element};          undef $self->{form_element};
7262    
# Line 6208  sub _tree_construction_main ($) { Line 7264  sub _tree_construction_main ($) {
7264          my $i;          my $i;
7265          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7266            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7267            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & FORM_EL) {
7268              !!!cp ('t418');              !!!cp ('t418');
7269              $i = $_;              $i = $_;
7270              last INSCOPE;              last INSCOPE;
7271            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7272              !!!cp ('t419');              !!!cp ('t419');
7273              last INSCOPE;              last INSCOPE;
7274            }            }
# Line 6223  sub _tree_construction_main ($) { Line 7276  sub _tree_construction_main ($) {
7276    
7277          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
7278            !!!cp ('t421');            !!!cp ('t421');
7279            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
7280                              text => $token->{tag_name}, token => $token);
7281              ## NOTE: Ignore the token.
7282          } else {          } else {
7283            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7284            while ({            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                   dd => 1, dt => 1, li => 1, p => 1,  
                  }->{$self->{open_elements}->[-1]->[1]}) {  
7285              !!!cp ('t417');              !!!cp ('t417');
7286              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
7287            }            }
7288                        
7289            ## Step 2.            ## Step 2.
7290            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7291                      ne $token->{tag_name}) {
7292              !!!cp ('t417.1');              !!!cp ('t417.1');
7293              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);              !!!parse-error (type => 'not closed',
7294                                text => $self->{open_elements}->[-1]->[0]
7295                                    ->manakai_local_name,
7296                                token => $token);
7297            } else {            } else {
7298              !!!cp ('t420');              !!!cp ('t420');
7299            }              }  
# Line 6246  sub _tree_construction_main ($) { Line 7303  sub _tree_construction_main ($) {
7303          }          }
7304    
7305          !!!next-token;          !!!next-token;
7306          redo B;          next B;
7307        } elsif ({        } elsif ({
7308                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
7309                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 6254  sub _tree_construction_main ($) { Line 7311  sub _tree_construction_main ($) {
7311          my $i;          my $i;
7312          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7313            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7314            if ({            if ($node->[1] & HEADING_EL) {
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
7315              !!!cp ('t423');              !!!cp ('t423');
7316              $i = $_;              $i = $_;
7317              last INSCOPE;              last INSCOPE;
7318            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7319              !!!cp ('t424');              !!!cp ('t424');
7320              last INSCOPE;              last INSCOPE;
7321            }            }
# Line 6271  sub _tree_construction_main ($) { Line 7323  sub _tree_construction_main ($) {
7323    
7324          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
7325            !!!cp ('t425.1');            !!!cp ('t425.1');
7326            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
7327                              text => $token->{tag_name}, token => $token);
7328              ## NOTE: Ignore the token.
7329          } else {          } else {
7330            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7331            while ({            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                   dd => 1, dt => 1, li => 1, p => 1,  
                  }->{$self->{open_elements}->[-1]->[1]}) {  
7332              !!!cp ('t422');              !!!cp ('t422');
7333              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
7334            }            }
7335                        
7336            ## Step 2.            ## Step 2.
7337            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7338                      ne $token->{tag_name}) {
7339              !!!cp ('t425');              !!!cp ('t425');
7340              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag',
7341                                text => $token->{tag_name}, token => $token);
7342            } else {            } else {
7343              !!!cp ('t426');              !!!cp ('t426');
7344            }            }
# Line 6294  sub _tree_construction_main ($) { Line 7348  sub _tree_construction_main ($) {
7348          }          }
7349                    
7350          !!!next-token;          !!!next-token;
7351          redo B;          next B;
7352        } elsif ($token->{tag_name} eq 'p') {        } elsif ($token->{tag_name} eq 'p') {
7353          ## has an element in scope          ## has an element in scope
7354          my $i;          my $i;
7355          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7356            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7357            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & P_EL) {
7358              !!!cp ('t410.1');              !!!cp ('t410.1');
7359              $i = $_;              $i = $_;
7360              last INSCOPE;              last INSCOPE;
7361            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7362              !!!cp ('t411.1');              !!!cp ('t411.1');
7363              last INSCOPE;              last INSCOPE;
7364            }            }
7365          } # INSCOPE          } # INSCOPE
7366    
7367          if (defined $i) {          if (defined $i) {
7368            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7369                      ne $token->{tag_name}) {
7370              !!!cp ('t412.1');              !!!cp ('t412.1');
7371              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);              !!!parse-error (type => 'not closed',
7372                                text => $self->{open_elements}->[-1]->[0]
7373                                    ->manakai_local_name,
7374                                token => $token);
7375            } else {            } else {
7376              !!!cp ('t414.1');              !!!cp ('t414.1');
7377            }            }
# Line 6324  sub _tree_construction_main ($) { Line 7379  sub _tree_construction_main ($) {
7379            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
7380          } else {          } else {
7381            !!!cp ('t413.1');            !!!cp ('t413.1');
7382            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
7383                              text => $token->{tag_name}, token => $token);
7384    
7385            !!!cp ('t415.1');            !!!cp ('t415.1');
7386            ## As if <p>, then reprocess the current token            ## As if <p>, then reprocess the current token
7387            my $el;            my $el;
7388            !!!create-element ($el, 'p',, $token);            !!!create-element ($el, $HTML_NS, 'p',, $token);
7389            $insert->($el);            $insert->($el);
7390            ## NOTE: Not inserted into |$self->{open_elements}|.            ## NOTE: Not inserted into |$self->{open_elements}|.
7391          }          }
7392    
7393          !!!next-token;          !!!next-token;
7394          redo B;          next B;
7395        } elsif ({        } elsif ({
7396                  a => 1,                  a => 1,
7397                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
# Line 6344  sub _tree_construction_main ($) { Line 7400  sub _tree_construction_main ($) {
7400                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7401          !!!cp ('t427');          !!!cp ('t427');
7402          $formatting_end_tag->($token);          $formatting_end_tag->($token);
7403          redo B;          next B;
7404        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
7405          !!!cp ('t428');          !!!cp ('t428');
7406          !!!parse-error (type => 'unmatched end tag:br', token => $token);          !!!parse-error (type => 'unmatched end tag',
7407                            text => 'br', token => $token);
7408    
7409          ## As if <br>          ## As if <br>
7410          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7411                    
7412          my $el;          my $el;
7413          !!!create-element ($el, 'br',, $token);          !!!create-element ($el, $HTML_NS, 'br',, $token);
7414          $insert->($el);          $insert->($el);
7415                    
7416          ## Ignore the token.          ## Ignore the token.
7417          !!!next-token;          !!!next-token;
7418          redo B;          next B;
7419        } elsif ({        } elsif ({
7420                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7421                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 6372  sub _tree_construction_main ($) { Line 7429  sub _tree_construction_main ($) {
7429                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
7430                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7431          !!!cp ('t429');          !!!cp ('t429');
7432          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'unmatched end tag',
7433                            text => $token->{tag_name}, token => $token);
7434          ## Ignore the token          ## Ignore the token
7435          !!!next-token;          !!!next-token;
7436          redo B;          next B;
7437                    
7438          ## ISSUE: Issue on HTML5 new elements in spec          ## ISSUE: Issue on HTML5 new elements in spec
7439                    
# Line 6386  sub _tree_construction_main ($) { Line 7444  sub _tree_construction_main ($) {
7444    
7445          ## Step 2          ## Step 2
7446          S2: {          S2: {
7447            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7448              ## Step 1              ## Step 1
7449              ## generate implied end tags              ## generate implied end tags
7450              while ({              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                     dd => 1, dt => 1, li => 1, p => 1,  
                    }->{$self->{open_elements}->[-1]->[1]}) {  
7451                !!!cp ('t430');                !!!cp ('t430');
7452                ## ISSUE: Can this case be reached?                ## NOTE: |<ruby><rt></ruby>|.
7453                  ## ISSUE: <ruby><rt></rt> will also take this code path,
7454                  ## which seems wrong.
7455                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
7456                  $node_i++;
7457              }              }
7458                    
7459              ## Step 2              ## Step 2
7460              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7461                        ne $token->{tag_name}) {
7462                !!!cp ('t431');                !!!cp ('t431');
7463                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
7464                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                !!!parse-error (type => 'not closed',
7465                                  text => $self->{open_elements}->[-1]->[0]
7466                                      ->manakai_local_name,
7467                                  token => $token);
7468              } else {              } else {
7469                !!!cp ('t432');                !!!cp ('t432');
7470              }              }
7471                            
7472              ## Step 3              ## Step 3
7473              splice @{$self->{open_elements}}, $node_i;              splice @{$self->{open_elements}}, $node_i if $node_i < 0;
7474    
7475              !!!next-token;              !!!next-token;
7476              last S2;              last S2;
7477            } else {            } else {
7478              ## Step 3              ## Step 3
7479              if (not $formatting_category->{$node->[1]} and              if (not ($node->[1] & FORMATTING_EL) and
7480                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
7481                  ($special_category->{$node->[1]} or                  ($node->[1] & SPECIAL_EL or
7482                   $scoping_category->{$node->[1]})) {                   $node->[1] & SCOPING_EL)) {
7483                !!!cp ('t433');                !!!cp ('t433');
7484                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag',
7485                                  text => $token->{tag_name}, token => $token);
7486                ## Ignore the token                ## Ignore the token
7487                !!!next-token;                !!!next-token;
7488                last S2;                last S2;
# Line 6434  sub _tree_construction_main ($) { Line 7498  sub _tree_construction_main ($) {
7498            ## Step 5;            ## Step 5;
7499            redo S2;            redo S2;
7500          } # S2          } # S2
7501          redo B;          next B;
7502        }        }
7503      }      }
7504      redo B;      next B;
7505      } continue { # B
7506        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7507          ## NOTE: The code below is executed in cases where it does not have
7508          ## to be, but it it is harmless even in those cases.
7509          ## has an element in scope
7510          INSCOPE: {
7511            for (reverse 0..$#{$self->{open_elements}}) {
7512              my $node = $self->{open_elements}->[$_];
7513              if ($node->[1] & FOREIGN_EL) {
7514                last INSCOPE;
7515              } elsif ($node->[1] & SCOPING_EL) {
7516                last;
7517              }
7518            }
7519            
7520            ## NOTE: No foreign element in scope.
7521            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7522          } # INSCOPE
7523        }
7524    } # B    } # B
7525    
7526    ## Stop parsing # MUST    ## Stop parsing # MUST
# Line 6445  sub _tree_construction_main ($) { Line 7528  sub _tree_construction_main ($) {
7528    ## TODO: script stuffs    ## TODO: script stuffs
7529  } # _tree_construct_main  } # _tree_construct_main
7530    
7531  sub set_inner_html ($$$) {  sub set_inner_html ($$$;$) {
7532    my $class = shift;    my $class = shift;
7533    my $node = shift;    my $node = shift;
7534    my $s = \$_[0];    my $s = \$_[0];
7535    my $onerror = $_[1];    my $onerror = $_[1];
7536      my $get_wrapper = $_[2] || sub ($) { return $_[0] };
7537    
7538    ## ISSUE: Should {confident} be true?    ## ISSUE: Should {confident} be true?
7539    
# Line 6468  sub set_inner_html ($$$) { Line 7552  sub set_inner_html ($$$) {
7552      }      }
7553    
7554      ## Step 3, 4, 5 # MUST      ## Step 3, 4, 5 # MUST
7555      $class->parse_string ($$s => $node, $onerror);      $class->parse_char_string ($$s => $node, $onerror, $get_wrapper);
7556    } elsif ($nt == 1) {    } elsif ($nt == 1) {
7557      ## TODO: If non-html element      ## TODO: If non-html element
7558    
7559      ## NOTE: Most of this code is copied from |parse_string|      ## NOTE: Most of this code is copied from |parse_string|
7560    
7561    ## TODO: Support for $get_wrapper
7562    
7563      ## Step 1 # MUST      ## Step 1 # MUST
7564      my $this_doc = $node->owner_document;      my $this_doc = $node->owner_document;
7565      my $doc = $this_doc->implementation->create_document;      my $doc = $this_doc->implementation->create_document;
# Line 6514  sub set_inner_html ($$$) { Line 7600  sub set_inner_html ($$$) {
7600          !!!cp ('i4');          !!!cp ('i4');
7601          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
7602          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7603          } elsif ($self->{next_char} <= 0x0008 or
7604                   (0x000E <= $self->{next_char} and
7605                    $self->{next_char} <= 0x001F) or
7606                   (0x007F <= $self->{next_char} and
7607                    $self->{next_char} <= 0x009F) or
7608                   (0xD800 <= $self->{next_char} and
7609                    $self->{next_char} <= 0xDFFF) or
7610                   (0xFDD0 <= $self->{next_char} and
7611                    $self->{next_char} <= 0xFDDF) or
7612                   {
7613                    0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
7614                    0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
7615                    0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
7616                    0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
7617                    0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
7618                    0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
7619                    0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
7620                    0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
7621                    0x10FFFE => 1, 0x10FFFF => 1,
7622                   }->{$self->{next_char}}) {
7623            !!!cp ('i4.1');
7624            if ($self->{next_char} < 0x10000) {
7625              !!!parse-error (type => 'control char',
7626                              text => (sprintf 'U+%04X', $self->{next_char}));
7627            } else {
7628              !!!parse-error (type => 'control char',
7629                              text => (sprintf 'U-%08X', $self->{next_char}));
7630            }
7631        }        }
7632      };      };
7633      $p->{prev_char} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];
# Line 6554  sub set_inner_html ($$$) { Line 7668  sub set_inner_html ($$$) {
7668          unless defined $p->{content_model};          unless defined $p->{content_model};
7669          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
7670    
7671      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7672          ## TODO: Foreign element OK?
7673    
7674      ## Step 3      ## Step 3
7675      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
# Line 6564  sub set_inner_html ($$$) { Line 7679  sub set_inner_html ($$$) {
7679      $doc->append_child ($root);      $doc->append_child ($root);
7680    
7681      ## Step 5 # MUST      ## Step 5 # MUST
7682      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7683    
7684      undef $p->{head_element};      undef $p->{head_element};
7685    

Legend:
Removed from v.1.121  
changed lines
  Added in v.1.165

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24