/[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.151 by wakaba, Sun Jun 8 05:08:42 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 = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
359    my $charset = shift;    my $charset_name = shift;
360    my $bytes_s = ref $_[0] ? $_[0] : \($_[0]);    my $byte_stream = $_[0];
   my $s;  
     
   if (defined $charset) {  
     require Encode; ## TODO: decode(utf8) don't delete BOM  
     $s = \ (Encode::decode ($charset, $$bytes_s));  
     $self->{input_encoding} = lc $charset; ## TODO: normalize name  
     $self->{confident} = 1;  
   } else {  
     ## TODO: Implement HTML5 detection algorithm  
     require Whatpm::Charset::UniversalCharDet;  
     $charset = Whatpm::Charset::UniversalCharDet->detect_byte_string  
         (substr ($$bytes_s, 0, 1024));  
     $charset ||= 'windows-1252';  
     $s = \ (Encode::decode ($charset, $$bytes_s));  
     $self->{input_encoding} = $charset;  
     $self->{confident} = 0;  
   }  
361    
362    $self->{change_encoding} = sub {    my $onerror = $_[2] || sub {
363      my $self = shift;      my (%opt) = @_;
364      my $charset = lc shift;      warn "Parse error ($opt{type})\n";
365      my $token = shift;    };
366      ## TODO: if $charset is supported    $self->{parse_error} = $onerror; # updated later by parse_char_string
     ## TODO: normalize charset name  
367    
368      ## "Change the encoding" algorithm:    ## HTML5 encoding sniffing algorithm
369      require Message::Charset::Info;
370      my $charset;
371      my $buffer;
372      my ($char_stream, $e_status);
373    
374      ## Step 1        SNIFFING: {
375      if ($charset eq 'utf-16') { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?  
376        $charset = 'utf-8';      ## Step 1
377        if (defined $charset_name) {
378          $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
379    
380          ## ISSUE: Unsupported encoding is not ignored according to the spec.
381          ($char_stream, $e_status) = $charset->get_decode_handle
382              ($byte_stream, allow_error_reporting => 1,
383               allow_fallback => 1);
384          if ($char_stream) {
385            $self->{confident} = 1;
386            last SNIFFING;
387          } else {
388            ## TODO: unsupported error
389          }
390      }      }
391    
392      ## Step 2      ## Step 2
393      if (defined $self->{input_encoding} and      my $byte_buffer = '';
394          $self->{input_encoding} eq $charset) {      for (1..1024) {
395          my $char = $byte_stream->getc;
396          last unless defined $char;
397          $byte_buffer .= $char;
398        } ## TODO: timeout
399    
400        ## Step 3
401        if ($byte_buffer =~ /^\xFE\xFF/) {
402          $charset = Message::Charset::Info->get_by_iana_name ('utf-16be');
403          ($char_stream, $e_status) = $charset->get_decode_handle
404              ($byte_stream, allow_error_reporting => 1,
405               allow_fallback => 1, byte_buffer => \$byte_buffer);
406        $self->{confident} = 1;        $self->{confident} = 1;
407        return;        last SNIFFING;
408        } elsif ($byte_buffer =~ /^\xFF\xFE/) {
409          $charset = Message::Charset::Info->get_by_iana_name ('utf-16le');
410          ($char_stream, $e_status) = $charset->get_decode_handle
411              ($byte_stream, allow_error_reporting => 1,
412               allow_fallback => 1, byte_buffer => \$byte_buffer);
413          $self->{confident} = 1;
414          last SNIFFING;
415        } elsif ($byte_buffer =~ /^\xEF\xBB\xBF/) {
416          $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
417          ($char_stream, $e_status) = $charset->get_decode_handle
418              ($byte_stream, allow_error_reporting => 1,
419               allow_fallback => 1, byte_buffer => \$byte_buffer);
420          $self->{confident} = 1;
421          last SNIFFING;
422      }      }
423    
424      !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.      ## Step 4
425          ':'.$charset, level => 'w', token => $token);      ## TODO: <meta charset>
426    
427      ## Step 3      ## Step 5
428      # if (can) {      ## TODO: from history
       ## change the encoding on the fly.  
       #$self->{confident} = 1;  
       #return;  
     # }  
429    
430      ## Step 4      ## Step 6
431      throw Whatpm::HTML::RestartParser (charset => $charset);      require Whatpm::Charset::UniversalCharDet;
432        $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
433            ($byte_buffer);
434        if (defined $charset_name) {
435          $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
436    
437          ## ISSUE: Unsupported encoding is not ignored according to the spec.
438          require Whatpm::Charset::DecodeHandle;
439          $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
440              ($byte_stream);
441          ($char_stream, $e_status) = $charset->get_decode_handle
442              ($buffer, allow_error_reporting => 1,
443               allow_fallback => 1, byte_buffer => \$byte_buffer);
444          if ($char_stream) {
445            $buffer->{buffer} = $byte_buffer;
446            !!!parse-error (type => 'sniffing:chardet', ## TODO: type name
447                            value => $charset_name,
448                            level => $self->{info_level},
449                            line => 1, column => 1);
450            $self->{confident} = 0;
451            last SNIFFING;
452          }
453        }
454    
455        ## Step 7: default
456        ## TODO: Make this configurable.
457        $charset = Message::Charset::Info->get_by_iana_name ('windows-1252');
458            ## NOTE: We choose |windows-1252| here, since |utf-8| should be
459            ## detectable in the step 6.
460        require Whatpm::Charset::DecodeHandle;
461        $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
462            ($byte_stream);
463        ($char_stream, $e_status)
464            = $charset->get_decode_handle ($buffer,
465                                           allow_error_reporting => 1,
466                                           allow_fallback => 1,
467                                           byte_buffer => \$byte_buffer);
468        $buffer->{buffer} = $byte_buffer;
469        !!!parse-error (type => 'sniffing:default', ## TODO: type name
470                        value => 'windows-1252',
471                        level => $self->{info_level},
472                        line => 1, column => 1);
473        $self->{confident} = 0;
474      } # SNIFFING
475    
476      $self->{input_encoding} = $charset->get_iana_name;
477      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
478        !!!parse-error (type => 'chardecode:fallback', ## TODO: type name
479                        value => $self->{input_encoding},
480                        level => $self->{unsupported_level},
481                        line => 1, column => 1);
482      } elsif (not ($e_status &
483                    Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
484        !!!parse-error (type => 'chardecode:no error', ## TODO: type name
485                        value => $self->{input_encoding},
486                        level => $self->{unsupported_level},
487                        line => 1, column => 1);
488      }
489    
490      $self->{change_encoding} = sub {
491        my $self = shift;
492        $charset_name = shift;
493        my $token = shift;
494    
495        $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
496        ($char_stream, $e_status) = $charset->get_decode_handle
497            ($byte_stream, allow_error_reporting => 1, allow_fallback => 1,
498             byte_buffer => \ $buffer->{buffer});
499        
500        if ($char_stream) { # if supported
501          ## "Change the encoding" algorithm:
502    
503          ## Step 1    
504          if ($charset->{category} &
505              Message::Charset::Info::CHARSET_CATEGORY_UTF16 ()) {
506            $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
507            ($char_stream, $e_status) = $charset->get_decode_handle
508                ($byte_stream,
509                 byte_buffer => \ $buffer->{buffer});
510          }
511          $charset_name = $charset->get_iana_name;
512          
513          ## Step 2
514          if (defined $self->{input_encoding} and
515              $self->{input_encoding} eq $charset_name) {
516            !!!parse-error (type => 'charset label:matching', ## TODO: type
517                            value => $charset_name,
518                            level => $self->{info_level});
519            $self->{confident} = 1;
520            return;
521          }
522    
523          !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.
524              ':'.$charset_name, level => 'w', token => $token);
525          
526          ## Step 3
527          # if (can) {
528            ## change the encoding on the fly.
529            #$self->{confident} = 1;
530            #return;
531          # }
532          
533          ## Step 4
534          throw Whatpm::HTML::RestartParser ();
535        }
536    }; # $self->{change_encoding}    }; # $self->{change_encoding}
537    
538      my $char_onerror = sub {
539        my (undef, $type, %opt) = @_;
540        !!!parse-error (%opt, type => $type,
541                        line => $self->{line}, column => $self->{column} + 1);
542        if ($opt{octets}) {
543          ${$opt{octets}} = "\x{FFFD}"; # relacement character
544        }
545      };
546      $char_stream->onerror ($char_onerror);
547    
548    my @args = @_; shift @args; # $s    my @args = @_; shift @args; # $s
549    my $return;    my $return;
550    try {    try {
551      $return = $self->parse_char_string ($s, @args);        $return = $self->parse_char_stream ($char_stream, @args);  
552    } catch Whatpm::HTML::RestartParser with {    } catch Whatpm::HTML::RestartParser with {
553      my $charset = shift->{charset};      ## NOTE: Invoked after {change_encoding}.
554      $s = \ (Encode::decode ($charset, $$bytes_s));      
555      $self->{input_encoding} = $charset; ## TODO: normalize      $self->{input_encoding} = $charset->get_iana_name;
556        if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
557          !!!parse-error (type => 'chardecode:fallback', ## TODO: type name
558                          value => $self->{input_encoding},
559                          level => $self->{unsupported_level},
560                          line => 1, column => 1);
561        } elsif (not ($e_status &
562                      Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
563          !!!parse-error (type => 'chardecode:no error', ## TODO: type name
564                          value => $self->{input_encoding},
565                          level => $self->{unsupported_level},
566                          line => 1, column => 1);
567        }
568      $self->{confident} = 1;      $self->{confident} = 1;
569      $return = $self->parse_char_string ($s, @args);      $char_stream->onerror ($char_onerror);
570        $return = $self->parse_char_stream ($char_stream, @args);
571    };    };
572    return $return;    return $return;
573  } # parse_byte_string  } # parse_byte_stream
574    
575  ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM  ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
576  ## 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 581  sub parse_byte_string ($$$$;$) {
581  ## 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
582  ## strip the BOM and never strip any ZWNBSP.  ## strip the BOM and never strip any ZWNBSP.
583    
584  *parse_char_string = \&parse_string;  sub parse_char_string ($$$;$) {
585      my $self = shift;
586      require utf8;
587      my $s = ref $_[0] ? $_[0] : \($_[0]);
588      open my $input, '<' . (utf8::is_utf8 ($$s) ? ':utf8' : ''), $s;
589      return $self->parse_char_stream ($input, @_[1..$#_]);
590    } # parse_char_string
591    *parse_string = \&parse_char_string;
592    
593  sub parse_string ($$$;$) {  sub parse_char_stream ($$$;$) {
594    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
595    my $s = ref $_[0] ? $_[0] : \($_[0]);    my $input = $_[0];
596    $self->{document} = $_[1];    $self->{document} = $_[1];
597    @{$self->{document}->child_nodes} = ();    @{$self->{document}->child_nodes} = ();
598    
# Line 186  sub parse_string ($$$;$) { Line 611  sub parse_string ($$$;$) {
611      pop @{$self->{prev_char}};      pop @{$self->{prev_char}};
612      unshift @{$self->{prev_char}}, $self->{next_char};      unshift @{$self->{prev_char}}, $self->{next_char};
613    
614      $self->{next_char} = -1 and return if $i >= length $$s;      my $char;
615      $self->{next_char} = ord substr $$s, $i++, 1;      if (defined $self->{next_next_char}) {
616          $char = $self->{next_next_char};
617          delete $self->{next_next_char};
618        } else {
619          $char = $input->getc;
620        }
621        $self->{next_char} = -1 and return unless defined $char;
622        $self->{next_char} = ord $char;
623    
624      ($self->{line_prev}, $self->{column_prev})      ($self->{line_prev}, $self->{column_prev})
625          = ($self->{line}, $self->{column});          = ($self->{line}, $self->{column});
626      $self->{column}++;      $self->{column}++;
627            
628      if ($self->{next_char} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
629          !!!cp ('j1');
630        $self->{line}++;        $self->{line}++;
631        $self->{column} = 0;        $self->{column} = 0;
632      } elsif ($self->{next_char} == 0x000D) { # CR      } elsif ($self->{next_char} == 0x000D) { # CR
633        $i++ if substr ($$s, $i, 1) eq "\x0A";        !!!cp ('j2');
634          my $next = $input->getc;
635          if (defined $next and $next ne "\x0A") {
636            $self->{next_next_char} = $next;
637          }
638        $self->{next_char} = 0x000A; # LF # MUST        $self->{next_char} = 0x000A; # LF # MUST
639        $self->{line}++;        $self->{line}++;
640        $self->{column} = 0;        $self->{column} = 0;
641      } elsif ($self->{next_char} > 0x10FFFF) {      } elsif ($self->{next_char} > 0x10FFFF) {
642          !!!cp ('j3');
643        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
644      } elsif ($self->{next_char} == 0x0000) { # NULL      } elsif ($self->{next_char} == 0x0000) { # NULL
645          !!!cp ('j4');
646        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
647        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
648        } elsif ($self->{next_char} <= 0x0008 or
649                 (0x000E <= $self->{next_char} and $self->{next_char} <= 0x001F) or
650                 (0x007F <= $self->{next_char} and $self->{next_char} <= 0x009F) or
651                 (0xD800 <= $self->{next_char} and $self->{next_char} <= 0xDFFF) or
652                 (0xFDD0 <= $self->{next_char} and $self->{next_char} <= 0xFDDF) or
653                 {
654                  0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
655                  0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
656                  0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
657                  0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
658                  0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
659                  0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
660                  0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
661                  0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
662                  0x10FFFE => 1, 0x10FFFF => 1,
663                 }->{$self->{next_char}}) {
664          !!!cp ('j5');
665          !!!parse-error (type => 'control char', level => $self->{must_level});
666    ## TODO: error type documentation
667      }      }
668    };    };
669    $self->{prev_char} = [-1, -1, -1];    $self->{prev_char} = [-1, -1, -1];
# Line 229  sub parse_string ($$$;$) { Line 687  sub parse_string ($$$;$) {
687    delete $self->{parse_error}; # remove loop    delete $self->{parse_error}; # remove loop
688    
689    return $self->{document};    return $self->{document};
690  } # parse_string  } # parse_char_stream
691    
692  sub new ($) {  sub new ($) {
693    my $class = shift;    my $class = shift;
694    my $self = bless {}, $class;    my $self = bless {
695        must_level => 'm',
696        should_level => 's',
697        good_level => 'w',
698        warn_level => 'w',
699        info_level => 'i',
700        unsupported_level => 'u',
701      }, $class;
702    $self->{set_next_char} = sub {    $self->{set_next_char} = sub {
703      $self->{next_char} = -1;      $self->{next_char} = -1;
704    };    };
# Line 295  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO Line 760  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO
760  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
761  sub BOGUS_DOCTYPE_STATE () { 32 }  sub BOGUS_DOCTYPE_STATE () { 32 }
762  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
763    sub SELF_CLOSING_START_TAG_STATE () { 34 }
764    sub CDATA_BLOCK_STATE () { 35 }
765    
766  sub DOCTYPE_TOKEN () { 1 }  sub DOCTYPE_TOKEN () { 1 }
767  sub COMMENT_TOKEN () { 2 }  sub COMMENT_TOKEN () { 2 }
# Line 312  sub ROW_IMS ()        { 0b10000000 } Line 779  sub ROW_IMS ()        { 0b10000000 }
779  sub BODY_AFTER_IMS () { 0b100000000 }  sub BODY_AFTER_IMS () { 0b100000000 }
780  sub FRAME_IMS ()      { 0b1000000000 }  sub FRAME_IMS ()      { 0b1000000000 }
781  sub SELECT_IMS ()     { 0b10000000000 }  sub SELECT_IMS ()     { 0b10000000000 }
782    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
783        ## NOTE: "in foreign content" insertion mode is special; it is combined
784        ## with the secondary insertion mode.  In this parser, they are stored
785        ## together in the bit-or'ed form.
786    
787  ## NOTE: "initial" and "before html" insertion modes have no constants.  ## NOTE: "initial" and "before html" insertion modes have no constants.
788    
# Line 348  sub _initialize_tokenizer ($) { Line 819  sub _initialize_tokenizer ($) {
819    undef $self->{current_attribute};    undef $self->{current_attribute};
820    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
821    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
822      delete $self->{self_closing};
823    $self->{char} = [];    $self->{char} = [];
824    # $self->{next_char}    # $self->{next_char}
825    !!!next-input-character;    !!!next-input-character;
# Line 368  sub _initialize_tokenizer ($) { Line 840  sub _initialize_tokenizer ($) {
840  ##        ->{value}  ##        ->{value}
841  ##        ->{has_reference} == 1 or 0  ##        ->{has_reference} == 1 or 0
842  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
843    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
844    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
845    ##     while the token is pushed back to the stack.
846    
847  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
848    
# Line 394  sub _initialize_tokenizer ($) { Line 869  sub _initialize_tokenizer ($) {
869    
870  sub _get_next_token ($) {  sub _get_next_token ($) {
871    my $self = shift;    my $self = shift;
872    
873      if ($self->{self_closing}) {
874        !!!parse-error (type => 'nestc', token => $self->{current_token});
875        ## NOTE: The |self_closing| flag is only set by start tag token.
876        ## In addition, when a start tag token is emitted, it is always set to
877        ## |current_token|.
878        delete $self->{self_closing};
879      }
880    
881    if (@{$self->{token}}) {    if (@{$self->{token}}) {
882        $self->{self_closing} = $self->{token}->[0]->{self_closing};
883      return shift @{$self->{token}};      return shift @{$self->{token}};
884    }    }
885    
# Line 574  sub _get_next_token ($) { Line 1059  sub _get_next_token ($) {
1059            redo A;            redo A;
1060          } else {          } else {
1061            !!!cp (23);            !!!cp (23);
1062            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago',
1063                              line => $self->{line_prev},
1064                              column => $self->{column_prev});
1065            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1066            ## reconsume            ## reconsume
1067    
# Line 765  sub _get_next_token ($) { Line 1252  sub _get_next_token ($) {
1252    
1253          redo A;          redo A;
1254        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1255            !!!cp (42);
1256            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1257          !!!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  
1258          redo A;          redo A;
1259        } else {        } else {
1260          !!!cp (44);          !!!cp (44);
# Line 829  sub _get_next_token ($) { Line 1306  sub _get_next_token ($) {
1306          !!!next-input-character;          !!!next-input-character;
1307          redo A;          redo A;
1308        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1309            !!!cp (50);
1310            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1311          !!!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  
1312          redo A;          redo A;
1313        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1314          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
# Line 942  sub _get_next_token ($) { Line 1409  sub _get_next_token ($) {
1409          !!!next-input-character;          !!!next-input-character;
1410          redo A;          redo A;
1411        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1412            !!!cp (64);
1413          $before_leave->();          $before_leave->();
1414            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1415          !!!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  
1416          redo A;          redo A;
1417        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1418          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
# Line 1042  sub _get_next_token ($) { Line 1499  sub _get_next_token ($) {
1499          !!!next-input-character;          !!!next-input-character;
1500          redo A;          redo A;
1501        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1502            !!!cp (77);
1503            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1504          !!!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  
1505          redo A;          redo A;
1506        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1507          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
# Line 1388  sub _get_next_token ($) { Line 1834  sub _get_next_token ($) {
1834    
1835          redo A;          redo A;
1836        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1837            !!!cp (122);
1838            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1839          !!!next-input-character;          !!!next-input-character;
1840          if ($self->{next_char} == 0x003E and # >          redo A;
1841              $self->{current_token}->{type} == START_TAG_TOKEN and        } elsif ($self->{next_char} == -1) {
1842              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {          !!!parse-error (type => 'unclosed tag');
1843            # permitted slash          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1844            !!!cp (122);            !!!cp (122.3);
1845            #            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1846            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1847              if ($self->{current_token}->{attributes}) {
1848                !!!cp (122.1);
1849                !!!parse-error (type => 'end tag attribute');
1850              } else {
1851                ## NOTE: This state should never be reached.
1852                !!!cp (122.2);
1853              }
1854          } else {          } else {
1855            !!!cp (123);            die "$0: $self->{current_token}->{type}: Unknown token type";
           !!!parse-error (type => 'nestc');  
1856          }          }
1857          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = DATA_STATE;
1858          # next-input-character is already done          ## Reconsume.
1859            !!!emit ($self->{current_token}); # start tag or end tag
1860          redo A;          redo A;
1861        } else {        } else {
1862          !!!cp (124);          !!!cp ('124.1');
1863          !!!parse-error (type => 'no space between attributes');          !!!parse-error (type => 'no space between attributes');
1864          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1865          ## reconsume          ## reconsume
1866          redo A;          redo A;
1867        }        }
1868        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1869          if ($self->{next_char} == 0x003E) { # >
1870            if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1871              !!!cp ('124.2');
1872              !!!parse-error (type => 'nestc', token => $self->{current_token});
1873              ## TODO: Different type than slash in start tag
1874              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1875              if ($self->{current_token}->{attributes}) {
1876                !!!cp ('124.4');
1877                !!!parse-error (type => 'end tag attribute');
1878              } else {
1879                !!!cp ('124.5');
1880              }
1881              ## TODO: Test |<title></title/>|
1882            } else {
1883              !!!cp ('124.3');
1884              $self->{self_closing} = 1;
1885            }
1886    
1887            $self->{state} = DATA_STATE;
1888            !!!next-input-character;
1889    
1890            !!!emit ($self->{current_token}); # start tag or end tag
1891    
1892            redo A;
1893          } elsif ($self->{next_char} == -1) {
1894            !!!parse-error (type => 'unclosed tag');
1895            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1896              !!!cp (124.7);
1897              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1898            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1899              if ($self->{current_token}->{attributes}) {
1900                !!!cp (124.5);
1901                !!!parse-error (type => 'end tag attribute');
1902              } else {
1903                ## NOTE: This state should never be reached.
1904                !!!cp (124.6);
1905              }
1906            } else {
1907              die "$0: $self->{current_token}->{type}: Unknown token type";
1908            }
1909            $self->{state} = DATA_STATE;
1910            ## Reconsume.
1911            !!!emit ($self->{current_token}); # start tag or end tag
1912            redo A;
1913          } else {
1914            !!!cp ('124.4');
1915            !!!parse-error (type => 'nestc');
1916            ## TODO: This error type is wrong.
1917            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1918            ## Reconsume.
1919            redo A;
1920          }
1921      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1922        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1923                
# Line 1516  sub _get_next_token ($) { Line 2025  sub _get_next_token ($) {
2025          } else {          } else {
2026            !!!cp (135);            !!!cp (135);
2027          }          }
2028          } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
2029                   $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
2030                   $self->{next_char} == 0x005B) { # [
2031            !!!next-input-character;
2032            push @next_char, $self->{next_char};
2033            if ($self->{next_char} == 0x0043) { # C
2034              !!!next-input-character;
2035              push @next_char, $self->{next_char};
2036              if ($self->{next_char} == 0x0044) { # D
2037                !!!next-input-character;
2038                push @next_char, $self->{next_char};
2039                if ($self->{next_char} == 0x0041) { # A
2040                  !!!next-input-character;
2041                  push @next_char, $self->{next_char};
2042                  if ($self->{next_char} == 0x0054) { # T
2043                    !!!next-input-character;
2044                    push @next_char, $self->{next_char};
2045                    if ($self->{next_char} == 0x0041) { # A
2046                      !!!next-input-character;
2047                      push @next_char, $self->{next_char};
2048                      if ($self->{next_char} == 0x005B) { # [
2049                        !!!cp (135.1);
2050                        $self->{state} = CDATA_BLOCK_STATE;
2051                        !!!next-input-character;
2052                        redo A;
2053                      } else {
2054                        !!!cp (135.2);
2055                      }
2056                    } else {
2057                      !!!cp (135.3);
2058                    }
2059                  } else {
2060                    !!!cp (135.4);                
2061                  }
2062                } else {
2063                  !!!cp (135.5);
2064                }
2065              } else {
2066                !!!cp (135.6);
2067              }
2068            } else {
2069              !!!cp (135.7);
2070            }
2071        } else {        } else {
2072          !!!cp (136);          !!!cp (136);
2073        }        }
# Line 2199  sub _get_next_token ($) { Line 2751  sub _get_next_token ($) {
2751        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
2752          !!!cp (217);          !!!cp (217);
2753          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
   
2754          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2755          ## reconsume          ## reconsume
2756    
# Line 2240  sub _get_next_token ($) { Line 2791  sub _get_next_token ($) {
2791          !!!next-input-character;          !!!next-input-character;
2792          redo A;          redo A;
2793        }        }
2794        } elsif ($self->{state} == CDATA_BLOCK_STATE) {
2795          my $s = '';
2796          
2797          my ($l, $c) = ($self->{line}, $self->{column});
2798    
2799          CS: while ($self->{next_char} != -1) {
2800            if ($self->{next_char} == 0x005D) { # ]
2801              !!!next-input-character;
2802              if ($self->{next_char} == 0x005D) { # ]
2803                !!!next-input-character;
2804                MDC: {
2805                  if ($self->{next_char} == 0x003E) { # >
2806                    !!!cp (221.1);
2807                    !!!next-input-character;
2808                    last CS;
2809                  } elsif ($self->{next_char} == 0x005D) { # ]
2810                    !!!cp (221.2);
2811                    $s .= ']';
2812                    !!!next-input-character;
2813                    redo MDC;
2814                  } else {
2815                    !!!cp (221.3);
2816                    $s .= ']]';
2817                    #
2818                  }
2819                } # MDC
2820              } else {
2821                !!!cp (221.4);
2822                $s .= ']';
2823                #
2824              }
2825            } else {
2826              !!!cp (221.5);
2827              #
2828            }
2829            $s .= chr $self->{next_char};
2830            !!!next-input-character;
2831          } # CS
2832    
2833          $self->{state} = DATA_STATE;
2834          ## next-input-character done or EOF, which is reconsumed.
2835    
2836          if (length $s) {
2837            !!!cp (221.6);
2838            !!!emit ({type => CHARACTER_TOKEN, data => $s,
2839                      line => $l, column => $c});
2840          } else {
2841            !!!cp (221.7);
2842          }
2843    
2844          redo A;
2845    
2846          ## ISSUE: "text tokens" in spec.
2847          ## TODO: Streaming support
2848      } else {      } else {
2849        die "$0: $self->{state}: Unknown state";        die "$0: $self->{state}: Unknown state";
2850      }      }
# Line 2390  sub _tokenize_attempt_to_consume_an_enti Line 2995  sub _tokenize_attempt_to_consume_an_enti
2995      require Whatpm::_NamedEntityList;      require Whatpm::_NamedEntityList;
2996      our $EntityChar;      our $EntityChar;
2997    
2998      while (length $entity_name < 10 and      while (length $entity_name < 30 and
2999             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
3000             ((0x0041 <= $self->{next_char} and # a             ((0x0041 <= $self->{next_char} and # a
3001               $self->{next_char} <= 0x005A) or # x               $self->{next_char} <= 0x005A) or # x
# Line 2532  sub _tree_construction_initial ($) { Line 3137  sub _tree_construction_initial ($) {
3137                
3138        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
3139          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3140          ## NOTE: Default value for both |public_id| and |system_id| attributes
3141          ## are empty strings, so that we don't set any value in missing cases.
3142        $doctype->public_id ($token->{public_identifier})        $doctype->public_id ($token->{public_identifier})
3143            if defined $token->{public_identifier};            if defined $token->{public_identifier};
3144        $doctype->system_id ($token->{system_identifier})        $doctype->system_id ($token->{system_identifier})
# Line 2546  sub _tree_construction_initial ($) { Line 3153  sub _tree_construction_initial ($) {
3153        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{public_identifier}) {
3154          my $pubid = $token->{public_identifier};          my $pubid = $token->{public_identifier};
3155          $pubid =~ tr/a-z/A-z/;          $pubid =~ tr/a-z/A-z/;
3156          if ({          my $prefix = [
3157            "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,            "+//SILMARIL//DTD HTML PRO V0R11 19970101//",
3158            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3159            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3160            "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 1//",
3161            "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 2//",
3162            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//",
3163            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//",
3164            "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT//",
3165            "-//IETF//DTD HTML 2.0//EN" => 1,            "-//IETF//DTD HTML 2.0//",
3166            "-//IETF//DTD HTML 2.1E//EN" => 1,            "-//IETF//DTD HTML 2.1E//",
3167            "-//IETF//DTD HTML 3.0//EN" => 1,            "-//IETF//DTD HTML 3.0//",
3168            "-//IETF//DTD HTML 3.0//EN//" => 1,            "-//IETF//DTD HTML 3.2 FINAL//",
3169            "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,            "-//IETF//DTD HTML 3.2//",
3170            "-//IETF//DTD HTML 3.2//EN" => 1,            "-//IETF//DTD HTML 3//",
3171            "-//IETF//DTD HTML 3//EN" => 1,            "-//IETF//DTD HTML LEVEL 0//",
3172            "-//IETF//DTD HTML LEVEL 0//EN" => 1,            "-//IETF//DTD HTML LEVEL 1//",
3173            "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,            "-//IETF//DTD HTML LEVEL 2//",
3174            "-//IETF//DTD HTML LEVEL 1//EN" => 1,            "-//IETF//DTD HTML LEVEL 3//",
3175            "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 0//",
3176            "-//IETF//DTD HTML LEVEL 2//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 1//",
3177            "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 2//",
3178            "-//IETF//DTD HTML LEVEL 3//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 3//",
3179            "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,            "-//IETF//DTD HTML STRICT//",
3180            "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,            "-//IETF//DTD HTML//",
3181            "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,            "-//METRIUS//DTD METRIUS PRESENTATIONAL//",
3182            "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//",
3183            "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//",
3184            "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//",
3185            "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//",
3186            "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//",
3187            "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//",
3188            "-//IETF//DTD HTML STRICT//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD HTML//",
3189            "-//IETF//DTD HTML STRICT//EN//2.0" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//",
3190            "-//IETF//DTD HTML STRICT//EN//3.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//",
3191            "-//IETF//DTD HTML//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//",
3192            "-//IETF//DTD HTML//EN//2.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//",
3193            "-//IETF//DTD HTML//EN//3.0" => 1,            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//",
3194            "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//",
3195            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//",
3196            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//",
3197            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//",
3198            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//",
3199            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,            "-//W3C//DTD HTML 3 1995-03-24//",
3200            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,            "-//W3C//DTD HTML 3.2 DRAFT//",
3201            "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,            "-//W3C//DTD HTML 3.2 FINAL//",
3202            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//W3C//DTD HTML 3.2//",
3203            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//W3C//DTD HTML 3.2S DRAFT//",
3204            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 FRAMESET//",
3205            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 TRANSITIONAL//",
3206            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMETNAL 19960712//",
3207            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMENTAL 970421//",
3208            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//W3C//DTD W3 HTML//",
3209            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//W3O//DTD W3 HTML 3.0//",
3210            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML 2.0//",
3211            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML//",
3212            "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,          ]; # $prefix
3213            "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,          my $match;
3214            "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,          for (@$prefix) {
3215            "-//W3C//DTD HTML 3.2//EN" => 1,            if (substr ($prefix, 0, length $_) eq $_) {
3216            "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,              $match = 1;
3217            "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,              last;
3218            "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,            }
3219            "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,          }
3220            "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,          if ($match or
3221            "-//W3C//DTD W3 HTML//EN" => 1,              $pubid eq "-//W3O//DTD W3 HTML STRICT 3.0//EN//" or
3222            "-//W3O//DTD W3 HTML 3.0//EN" => 1,              $pubid eq "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" or
3223            "-//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}) {  
3224            !!!cp ('t5');            !!!cp ('t5');
3225            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3226          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD HTML 4.01 FRAMESET//] or
3227                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD HTML 4.01 TRANSITIONAL//]) {
3228            if (defined $token->{system_identifier}) {            if (defined $token->{system_identifier}) {
3229              !!!cp ('t6');              !!!cp ('t6');
3230              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
# Line 2631  sub _tree_construction_initial ($) { Line 3232  sub _tree_construction_initial ($) {
3232              !!!cp ('t7');              !!!cp ('t7');
3233              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
3234            }            }
3235          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD XHTML 1.0 FRAMESET//] or
3236                   $pubid eq "-//W3C//DTD XHTML 1.0 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD XHTML 1.0 TRANSITIONAL//]) {
3237            !!!cp ('t8');            !!!cp ('t8');
3238            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
3239          } else {          } else {
# Line 2645  sub _tree_construction_initial ($) { Line 3246  sub _tree_construction_initial ($) {
3246          my $sysid = $token->{system_identifier};          my $sysid = $token->{system_identifier};
3247          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
3248          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") {
3249            ## TODO: Check the spec: PUBLIC "(limited quirks)" "(quirks)"            ## NOTE: Ensure that |PUBLIC "(limited quirks)" "(quirks)"| is
3250              ## marked as quirks.
3251            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3252            !!!cp ('t11');            !!!cp ('t11');
3253          } else {          } else {
# Line 2668  sub _tree_construction_initial ($) { Line 3270  sub _tree_construction_initial ($) {
3270        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3271        ## Go to the "before html" insertion mode.        ## Go to the "before html" insertion mode.
3272        ## reprocess        ## reprocess
3273          !!!ack-later;
3274        return;        return;
3275      } elsif ($token->{type} == CHARACTER_TOKEN) {      } elsif ($token->{type} == CHARACTER_TOKEN) {
3276        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 3351  sub _tree_construction_root_element ($)
3351        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
3352          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
3353            my $root_element;            my $root_element;
3354            !!!create-element ($root_element, $token->{tag_name}, $token->{attributes}, $token);            !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3355            $self->{document}->append_child ($root_element);            $self->{document}->append_child ($root_element);
3356            push @{$self->{open_elements}}, [$root_element, 'html'];            push @{$self->{open_elements}},
3357                  [$root_element, $el_category->{html}];
3358    
3359            if ($token->{attributes}->{manifest}) {            if ($token->{attributes}->{manifest}) {
3360              !!!cp ('t24');              !!!cp ('t24');
# Line 2765  sub _tree_construction_root_element ($) Line 3369  sub _tree_construction_root_element ($)
3369              $self->{application_cache_selection}->(undef);              $self->{application_cache_selection}->(undef);
3370            }            }
3371    
3372              !!!nack ('t25c');
3373    
3374            !!!next-token;            !!!next-token;
3375            return; ## Go to the "before head" insertion mode.            return; ## Go to the "before head" insertion mode.
3376          } else {          } else {
# Line 2781  sub _tree_construction_root_element ($) Line 3387  sub _tree_construction_root_element ($)
3387          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
3388        }        }
3389    
3390      my $root_element; !!!create-element ($root_element, 'html',, $token);      my $root_element;
3391        !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3392      $self->{document}->append_child ($root_element);      $self->{document}->append_child ($root_element);
3393      push @{$self->{open_elements}}, [$root_element, 'html'];      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3394    
3395      $self->{application_cache_selection}->(undef);      $self->{application_cache_selection}->(undef);
3396    
3397      ## NOTE: Reprocess the token.      ## NOTE: Reprocess the token.
3398        !!!ack-later;
3399      return; ## Go to the "before head" insertion mode.      return; ## Go to the "before head" insertion mode.
3400    
3401      ## ISSUE: There is an issue in the spec      ## ISSUE: There is an issue in the spec
# Line 2811  sub _reset_insertion_mode ($) { Line 3419  sub _reset_insertion_mode ($) {
3419        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3420          $last = 1;          $last = 1;
3421          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
3422            if ($self->{inner_html_node}->[1] eq 'td' or            !!!cp ('t28');
3423                $self->{inner_html_node}->[1] eq 'th') {            $node = $self->{inner_html_node};
3424              !!!cp ('t27');          } else {
3425              #            die "_reset_insertion_mode: t27";
           } else {  
             !!!cp ('t28');  
             $node = $self->{inner_html_node};  
           }  
3426          }          }
3427        }        }
3428              
3429        ## Step 4..13        ## Step 4..14
3430        my $new_mode = {        my $new_mode;
3431          if ($node->[1] & FOREIGN_EL) {
3432            !!!cp ('t28.1');
3433            ## NOTE: Strictly spaking, the line below only applies to MathML and
3434            ## SVG elements.  Currently the HTML syntax supports only MathML and
3435            ## SVG elements as foreigners.
3436            $new_mode = IN_BODY_IM | IN_FOREIGN_CONTENT_IM;
3437          } elsif ($node->[1] & TABLE_CELL_EL) {
3438            if ($last) {
3439              !!!cp ('t28.2');
3440              #
3441            } else {
3442              !!!cp ('t28.3');
3443              $new_mode = IN_CELL_IM;
3444            }
3445          } else {
3446            !!!cp ('t28.4');
3447            $new_mode = {
3448                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
3449                        ## NOTE: |option| and |optgroup| do not set                        ## NOTE: |option| and |optgroup| do not set
3450                        ## insertion mode to "in select" by themselves.                        ## insertion mode to "in select" by themselves.
                       td => IN_CELL_IM,  
                       th => IN_CELL_IM,  
3451                        tr => IN_ROW_IM,                        tr => IN_ROW_IM,
3452                        tbody => IN_TABLE_BODY_IM,                        tbody => IN_TABLE_BODY_IM,
3453                        thead => IN_TABLE_BODY_IM,                        thead => IN_TABLE_BODY_IM,
# Line 2839  sub _reset_insertion_mode ($) { Line 3458  sub _reset_insertion_mode ($) {
3458                        head => IN_BODY_IM, # not in head!                        head => IN_BODY_IM, # not in head!
3459                        body => IN_BODY_IM,                        body => IN_BODY_IM,
3460                        frameset => IN_FRAMESET_IM,                        frameset => IN_FRAMESET_IM,
3461                       }->{$node->[1]};                       }->{$node->[0]->manakai_local_name};
3462          }
3463        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3464                
3465        ## Step 14        ## Step 15
3466        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3467          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3468            !!!cp ('t29');            !!!cp ('t29');
3469            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
# Line 2857  sub _reset_insertion_mode ($) { Line 3477  sub _reset_insertion_mode ($) {
3477          !!!cp ('t31');          !!!cp ('t31');
3478        }        }
3479                
3480        ## Step 15        ## Step 16
3481        $self->{insertion_mode} = IN_BODY_IM and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
3482                
3483        ## Step 16        ## Step 17
3484        $i--;        $i--;
3485        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3486                
3487        ## Step 17        ## Step 18
3488        redo S3;        redo S3;
3489      } # S3      } # S3
3490    
# Line 2976  sub _tree_construction_main ($) { Line 3596  sub _tree_construction_main ($) {
3596      ## Step 1      ## Step 1
3597      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3598      my $el;      my $el;
3599      !!!create-element ($el, $start_tag_name, $token->{attributes}, $token);      !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3600    
3601      ## Step 2      ## Step 2
3602      $insert->($el);      $insert->($el);
# Line 2987  sub _tree_construction_main ($) { Line 3607  sub _tree_construction_main ($) {
3607    
3608      ## Step 4      ## Step 4
3609      my $text = '';      my $text = '';
3610        !!!nack ('t40.1');
3611      !!!next-token;      !!!next-token;
3612      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3613        !!!cp ('t40');        !!!cp ('t40');
# Line 3026  sub _tree_construction_main ($) { Line 3647  sub _tree_construction_main ($) {
3647    
3648    my $script_start_tag = sub () {    my $script_start_tag = sub () {
3649      my $script_el;      my $script_el;
3650      !!!create-element ($script_el, 'script', $token->{attributes}, $token);      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
3651      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3652    
3653      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
3654      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
3655            
3656      my $text = '';      my $text = '';
3657        !!!nack ('t45.1');
3658      !!!next-token;      !!!next-token;
3659      while ($token->{type} == CHARACTER_TOKEN) {      while ($token->{type} == CHARACTER_TOKEN) {
3660        !!!cp ('t45');        !!!cp ('t45');
# Line 3090  sub _tree_construction_main ($) { Line 3712  sub _tree_construction_main ($) {
3712        my $formatting_element;        my $formatting_element;
3713        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3714        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3715          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3716              !!!cp ('t52');
3717              last AFE;
3718            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
3719                         eq $tag_name) {
3720            !!!cp ('t51');            !!!cp ('t51');
3721            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3722            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3723            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           !!!cp ('t52');  
           last AFE;  
3724          }          }
3725        } # AFE        } # AFE
3726        unless (defined $formatting_element) {        unless (defined $formatting_element) {
# Line 3125  sub _tree_construction_main ($) { Line 3748  sub _tree_construction_main ($) {
3748              !!!next-token;              !!!next-token;
3749              return;              return;
3750            }            }
3751          } 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]}) {  
3752            !!!cp ('t56');            !!!cp ('t56');
3753            $in_scope = 0;            $in_scope = 0;
3754          }          }
# Line 3143  sub _tree_construction_main ($) { Line 3763  sub _tree_construction_main ($) {
3763        }        }
3764        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3765          !!!cp ('t58');          !!!cp ('t58');
3766          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1],          !!!parse-error (type => 'not closed',
3767                            value => $self->{open_elements}->[-1]->[0]
3768                                ->manakai_local_name,
3769                          token => $end_tag_token);                          token => $end_tag_token);
3770        }        }
3771                
# Line 3152  sub _tree_construction_main ($) { Line 3774  sub _tree_construction_main ($) {
3774        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
3775        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3776          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3777          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
3778              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3779              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
3780               $scoping_category->{$node->[1]})) { ## Scoping is redundant, maybe               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
3781            !!!cp ('t59');            !!!cp ('t59');
3782            $furthest_block = $node;            $furthest_block = $node;
3783            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
# Line 3241  sub _tree_construction_main ($) { Line 3863  sub _tree_construction_main ($) {
3863        } # S7          } # S7  
3864                
3865        ## Step 8        ## Step 8
3866        if ({        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
            table => 1, tbody => 1, tfoot => 1, thead => 1, tr => 1,  
           }->{$common_ancestor_node->[1]}) {  
3867          my $foster_parent_element;          my $foster_parent_element;
3868          my $next_sibling;          my $next_sibling;
3869                           OE: for (reverse 0..$#{$self->{open_elements}}) {          OE: for (reverse 0..$#{$self->{open_elements}}) {
3870                             if ($self->{open_elements}->[$_]->[1] eq 'table') {            if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3871                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3872                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3873                                 !!!cp ('t65.1');                                 !!!cp ('t65.1');
# Line 3320  sub _tree_construction_main ($) { Line 3940  sub _tree_construction_main ($) {
3940    
3941    my $insert_to_foster = sub {    my $insert_to_foster = sub {
3942      my $child = shift;      my $child = shift;
3943      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]}) {  
3944        # MUST        # MUST
3945        my $foster_parent_element;        my $foster_parent_element;
3946        my $next_sibling;        my $next_sibling;
3947                           OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3948                             if ($self->{open_elements}->[$_]->[1] eq 'table') {          if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3949                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3950                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3951                                 !!!cp ('t70');                                 !!!cp ('t70');
# Line 3352  sub _tree_construction_main ($) { Line 3970  sub _tree_construction_main ($) {
3970      }      }
3971    }; # $insert_to_foster    }; # $insert_to_foster
3972    
3973    B: {    B: while (1) {
3974      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
3975        !!!cp ('t73');        !!!cp ('t73');
3976        !!!parse-error (type => 'DOCTYPE in the middle', token => $token);        !!!parse-error (type => 'DOCTYPE in the middle', token => $token);
3977        ## Ignore the token        ## Ignore the token
3978        ## Stay in the phase        ## Stay in the phase
3979        !!!next-token;        !!!next-token;
3980        redo B;        next B;
3981      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
3982               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
3983        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
# Line 3385  sub _tree_construction_main ($) { Line 4003  sub _tree_construction_main ($) {
4003               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
4004          }          }
4005        }        }
4006          !!!nack ('t84.1');
4007        !!!next-token;        !!!next-token;
4008        redo B;        next B;
4009      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
4010        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
4011        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
# Line 3400  sub _tree_construction_main ($) { Line 4019  sub _tree_construction_main ($) {
4019          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
4020        }        }
4021        !!!next-token;        !!!next-token;
4022        redo B;        next B;
4023      } elsif ($self->{insertion_mode} & HEAD_IMS) {      } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
4024          if ($token->{type} == CHARACTER_TOKEN) {
4025            !!!cp ('t87.1');
4026            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4027            !!!next-token;
4028            next B;
4029          } elsif ($token->{type} == START_TAG_TOKEN) {
4030            if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
4031                 $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
4032                not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
4033                ($token->{tag_name} eq 'svg' and
4034                 $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
4035              ## NOTE: "using the rules for secondary insertion mode"then"continue"
4036              !!!cp ('t87.2');
4037              #
4038            } elsif ({
4039                      b => 1, big => 1, blockquote => 1, body => 1, br => 1,
4040                      center => 1, code => 1, dd => 1, div => 1, dl => 1, dt => 1,
4041                      em => 1, embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1,
4042                      h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, i => 1,
4043                      img => 1, li => 1, listing => 1, menu => 1, meta => 1,
4044                      nobr => 1, ol => 1, p => 1, pre => 1, ruby => 1, s => 1,
4045                      small => 1, span => 1, strong => 1, strike => 1, sub => 1,
4046                      sup => 1, table => 1, tt => 1, u => 1, ul => 1, var => 1,
4047                     }->{$token->{tag_name}}) {
4048              !!!cp ('t87.2');
4049              !!!parse-error (type => 'not closed',
4050                              value => $self->{open_elements}->[-1]->[0]
4051                                  ->manakai_local_name,
4052                              token => $token);
4053    
4054              pop @{$self->{open_elements}}
4055                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4056    
4057              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4058              ## Reprocess.
4059              next B;
4060            } else {
4061              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
4062              my $tag_name = $token->{tag_name};
4063              if ($nsuri eq $SVG_NS) {
4064                $tag_name = {
4065                   altglyph => 'altGlyph',
4066                   altglyphdef => 'altGlyphDef',
4067                   altglyphitem => 'altGlyphItem',
4068                   animatecolor => 'animateColor',
4069                   animatemotion => 'animateMotion',
4070                   animatetransform => 'animateTransform',
4071                   clippath => 'clipPath',
4072                   feblend => 'feBlend',
4073                   fecolormatrix => 'feColorMatrix',
4074                   fecomponenttransfer => 'feComponentTransfer',
4075                   fecomposite => 'feComposite',
4076                   feconvolvematrix => 'feConvolveMatrix',
4077                   fediffuselighting => 'feDiffuseLighting',
4078                   fedisplacementmap => 'feDisplacementMap',
4079                   fedistantlight => 'feDistantLight',
4080                   feflood => 'feFlood',
4081                   fefunca => 'feFuncA',
4082                   fefuncb => 'feFuncB',
4083                   fefuncg => 'feFuncG',
4084                   fefuncr => 'feFuncR',
4085                   fegaussianblur => 'feGaussianBlur',
4086                   feimage => 'feImage',
4087                   femerge => 'feMerge',
4088                   femergenode => 'feMergeNode',
4089                   femorphology => 'feMorphology',
4090                   feoffset => 'feOffset',
4091                   fepointlight => 'fePointLight',
4092                   fespecularlighting => 'feSpecularLighting',
4093                   fespotlight => 'feSpotLight',
4094                   fetile => 'feTile',
4095                   feturbulence => 'feTurbulence',
4096                   foreignobject => 'foreignObject',
4097                   glyphref => 'glyphRef',
4098                   lineargradient => 'linearGradient',
4099                   radialgradient => 'radialGradient',
4100                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4101                   textpath => 'textPath',  
4102                }->{$tag_name} || $tag_name;
4103              }
4104    
4105              ## "adjust SVG attributes" (SVG only) - done in insert-element-f
4106    
4107              ## "adjust foreign attributes" - done in insert-element-f
4108    
4109              !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
4110    
4111              if ($self->{self_closing}) {
4112                pop @{$self->{open_elements}};
4113                !!!ack ('t87.3');
4114              } else {
4115                !!!cp ('t87.4');
4116              }
4117    
4118              !!!next-token;
4119              next B;
4120            }
4121          } elsif ($token->{type} == END_TAG_TOKEN) {
4122            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4123            !!!cp ('t87.5');
4124            #
4125          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4126            !!!cp ('t87.6');
4127            !!!parse-error (type => 'not closed',
4128                            value => $self->{open_elements}->[-1]->[0]
4129                                ->manakai_local_name,
4130                            token => $token);
4131    
4132            pop @{$self->{open_elements}}
4133                while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4134    
4135            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4136            ## Reprocess.
4137            next B;
4138          } else {
4139            die "$0: $token->{type}: Unknown token type";        
4140          }
4141        }
4142    
4143        if ($self->{insertion_mode} & HEAD_IMS) {
4144        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4145          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4146            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
# Line 3411  sub _tree_construction_main ($) { Line 4150  sub _tree_construction_main ($) {
4150              !!!cp ('t88.1');              !!!cp ('t88.1');
4151              ## Ignore the token.              ## Ignore the token.
4152              !!!next-token;              !!!next-token;
4153              redo B;              next B;
4154            }            }
4155            unless (length $token->{data}) {            unless (length $token->{data}) {
4156              !!!cp ('t88');              !!!cp ('t88');
4157              !!!next-token;              !!!next-token;
4158              redo B;              next B;
4159            }            }
4160          }          }
4161    
4162          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4163            !!!cp ('t89');            !!!cp ('t89');
4164            ## As if <head>            ## As if <head>
4165            !!!create-element ($self->{head_element}, 'head',, $token);            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4166            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4167            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            push @{$self->{open_elements}},
4168                  [$self->{head_element}, $el_category->{head}];
4169    
4170            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4171            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
# Line 3451  sub _tree_construction_main ($) { Line 4191  sub _tree_construction_main ($) {
4191            !!!cp ('t92');            !!!cp ('t92');
4192          }          }
4193    
4194              ## "after head" insertion mode          ## "after head" insertion mode
4195              ## As if <body>          ## As if <body>
4196              !!!insert-element ('body',, $token);          !!!insert-element ('body',, $token);
4197              $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4198              ## reprocess          ## reprocess
4199              redo B;          next B;
4200            } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4201              if ($token->{tag_name} eq 'head') {          if ($token->{tag_name} eq 'head') {
4202                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4203                  !!!cp ('t93');              !!!cp ('t93');
4204                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes}, $token);              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4205                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              $self->{open_elements}->[-1]->[0]->append_child
4206                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];                  ($self->{head_element});
4207                  $self->{insertion_mode} = IN_HEAD_IM;              push @{$self->{open_elements}},
4208                  !!!next-token;                  [$self->{head_element}, $el_category->{head}];
4209                  redo B;              $self->{insertion_mode} = IN_HEAD_IM;
4210                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {              !!!nack ('t93.1');
4211                  !!!cp ('t94');              !!!next-token;
4212                  #              next B;
4213                } else {            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4214                  !!!cp ('t95');              !!!cp ('t93.2');
4215                  !!!parse-error (type => 'in head:head', token => $token); # or in head noscript              !!!parse-error (type => 'after head:head', token => $token); ## TODO: error type
4216                  ## Ignore the token              ## Ignore the token
4217                  !!!next-token;              !!!nack ('t93.3');
4218                  redo B;              !!!next-token;
4219                }              next B;
4220              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {            } else {
4221                !!!cp ('t96');              !!!cp ('t95');
4222                ## As if <head>              !!!parse-error (type => 'in head:head', token => $token); # or in head noscript
4223                !!!create-element ($self->{head_element}, 'head',, $token);              ## Ignore the token
4224                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              !!!nack ('t95.1');
4225                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];              !!!next-token;
4226                next B;
4227              }
4228            } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4229              !!!cp ('t96');
4230              ## As if <head>
4231              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4232              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4233              push @{$self->{open_elements}},
4234                  [$self->{head_element}, $el_category->{head}];
4235    
4236                $self->{insertion_mode} = IN_HEAD_IM;            $self->{insertion_mode} = IN_HEAD_IM;
4237                ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4238              } else {          } else {
4239                !!!cp ('t97');            !!!cp ('t97');
4240              }          }
4241    
4242              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
4243                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
# Line 3507  sub _tree_construction_main ($) { Line 4256  sub _tree_construction_main ($) {
4256                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4257                  !!!cp ('t100');                  !!!cp ('t100');
4258                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4259                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4260                        [$self->{head_element}, $el_category->{head}];
4261                } else {                } else {
4262                  !!!cp ('t101');                  !!!cp ('t101');
4263                }                }
# Line 3515  sub _tree_construction_main ($) { Line 4265  sub _tree_construction_main ($) {
4265                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4266                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4267                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4268                  !!!nack ('t101.1');
4269                !!!next-token;                !!!next-token;
4270                redo B;                next B;
4271              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
4272                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4273                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4274                  !!!cp ('t102');                  !!!cp ('t102');
4275                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4276                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4277                        [$self->{head_element}, $el_category->{head}];
4278                } else {                } else {
4279                  !!!cp ('t103');                  !!!cp ('t103');
4280                }                }
# Line 3530  sub _tree_construction_main ($) { Line 4282  sub _tree_construction_main ($) {
4282                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4283                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4284                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4285                  !!!ack ('t103.1');
4286                !!!next-token;                !!!next-token;
4287                redo B;                next B;
4288              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
4289                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4290                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4291                  !!!cp ('t104');                  !!!cp ('t104');
4292                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4293                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4294                        [$self->{head_element}, $el_category->{head}];
4295                } else {                } else {
4296                  !!!cp ('t105');                  !!!cp ('t105');
4297                }                }
# Line 3545  sub _tree_construction_main ($) { Line 4299  sub _tree_construction_main ($) {
4299                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.
4300    
4301                unless ($self->{confident}) {                unless ($self->{confident}) {
4302                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) {
4303                    !!!cp ('t106');                    !!!cp ('t106');
4304                      ## NOTE: Whether the encoding is supported or not is handled
4305                      ## in the {change_encoding} callback.
4306                    $self->{change_encoding}                    $self->{change_encoding}
4307                        ->($self, $token->{attributes}->{charset}->{value},                        ->($self, $token->{attributes}->{charset}->{value},
4308                           $token);                           $token);
# Line 3556  sub _tree_construction_main ($) { Line 4312  sub _tree_construction_main ($) {
4312                                             $token->{attributes}->{charset}                                             $token->{attributes}->{charset}
4313                                                 ->{has_reference});                                                 ->{has_reference});
4314                  } elsif ($token->{attributes}->{content}) {                  } elsif ($token->{attributes}->{content}) {
                   ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
4315                    if ($token->{attributes}->{content}->{value}                    if ($token->{attributes}->{content}->{value}
4316                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                        =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4317                            [\x09-\x0D\x20]*=                            [\x09-\x0D\x20]*=
4318                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4319                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
4320                      !!!cp ('t107');                      !!!cp ('t107');
4321                        ## NOTE: Whether the encoding is supported or not is handled
4322                        ## in the {change_encoding} callback.
4323                      $self->{change_encoding}                      $self->{change_encoding}
4324                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4325                             $token);                             $token);
# Line 3593  sub _tree_construction_main ($) { Line 4350  sub _tree_construction_main ($) {
4350    
4351                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4352                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4353                  !!!ack ('t110.1');
4354                !!!next-token;                !!!next-token;
4355                redo B;                next B;
4356              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
4357                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4358                  !!!cp ('t111');                  !!!cp ('t111');
# Line 3607  sub _tree_construction_main ($) { Line 4365  sub _tree_construction_main ($) {
4365                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4366                  !!!cp ('t112');                  !!!cp ('t112');
4367                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4368                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4369                        [$self->{head_element}, $el_category->{head}];
4370                } else {                } else {
4371                  !!!cp ('t113');                  !!!cp ('t113');
4372                }                }
# Line 3618  sub _tree_construction_main ($) { Line 4377  sub _tree_construction_main ($) {
4377                $parse_rcdata->(RCDATA_CONTENT_MODEL);                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4378                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4379                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4380                redo B;                next B;
4381              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style' or
4382                         $token->{tag_name} eq 'noframes') {
4383                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4384                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
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 ('t114');                  !!!cp ('t114');
4388                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4389                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4390                        [$self->{head_element}, $el_category->{head}];
4391                } else {                } else {
4392                  !!!cp ('t115');                  !!!cp ('t115');
4393                }                }
4394                $parse_rcdata->(CDATA_CONTENT_MODEL);                $parse_rcdata->(CDATA_CONTENT_MODEL);
4395                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4396                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4397                redo B;                next B;
4398              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4399                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
4400                  !!!cp ('t116');                  !!!cp ('t116');
4401                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
4402                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4403                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4404                    !!!nack ('t116.1');
4405                  !!!next-token;                  !!!next-token;
4406                  redo B;                  next B;
4407                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4408                  !!!cp ('t117');                  !!!cp ('t117');
4409                  !!!parse-error (type => 'in noscript:noscript', token => $token);                  !!!parse-error (type => 'in noscript:noscript', token => $token);
4410                  ## Ignore the token                  ## Ignore the token
4411                    !!!nack ('t117.1');
4412                  !!!next-token;                  !!!next-token;
4413                  redo B;                  next B;
4414                } else {                } else {
4415                  !!!cp ('t118');                  !!!cp ('t118');
4416                  #                  #
# Line 3664  sub _tree_construction_main ($) { Line 4427  sub _tree_construction_main ($) {
4427                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4428                  !!!cp ('t120');                  !!!cp ('t120');
4429                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4430                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4431                        [$self->{head_element}, $el_category->{head}];
4432                } else {                } else {
4433                  !!!cp ('t121');                  !!!cp ('t121');
4434                }                }
# Line 3673  sub _tree_construction_main ($) { Line 4437  sub _tree_construction_main ($) {
4437                $script_start_tag->();                $script_start_tag->();
4438                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4439                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4440                redo B;                next B;
4441              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
4442                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
4443                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
# Line 3707  sub _tree_construction_main ($) { Line 4471  sub _tree_construction_main ($) {
4471                } else {                } else {
4472                  die "$0: tag name: $self->{tag_name}";                  die "$0: tag name: $self->{tag_name}";
4473                }                }
4474                  !!!nack ('t127.1');
4475                !!!next-token;                !!!next-token;
4476                redo B;                next B;
4477              } else {              } else {
4478                !!!cp ('t128');                !!!cp ('t128');
4479                #                #
# Line 3740  sub _tree_construction_main ($) { Line 4505  sub _tree_construction_main ($) {
4505              !!!insert-element ('body',, $token);              !!!insert-element ('body',, $token);
4506              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4507              ## reprocess              ## reprocess
4508              redo B;              !!!ack-later;
4509                next B;
4510            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
4511              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
4512                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4513                  !!!cp ('t132');                  !!!cp ('t132');
4514                  ## As if <head>                  ## As if <head>
4515                  !!!create-element ($self->{head_element}, 'head',, $token);                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4516                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4517                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4518                        [$self->{head_element}, $el_category->{head}];
4519    
4520                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4521                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4522                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4523                  !!!next-token;                  !!!next-token;
4524                  redo B;                  next B;
4525                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4526                  !!!cp ('t133');                  !!!cp ('t133');
4527                  ## As if </noscript>                  ## As if </noscript>
# Line 3765  sub _tree_construction_main ($) { Line 4532  sub _tree_construction_main ($) {
4532                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4533                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4534                  !!!next-token;                  !!!next-token;
4535                  redo B;                  next B;
4536                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4537                  !!!cp ('t134');                  !!!cp ('t134');
4538                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4539                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4540                  !!!next-token;                  !!!next-token;
4541                  redo B;                  next B;
4542                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4543                    !!!cp ('t134.1');
4544                    !!!parse-error (type => 'unmatched end tag:head', token => $token);
4545                    ## Ignore the token
4546                    !!!next-token;
4547                    next B;
4548                } else {                } else {
4549                  !!!cp ('t135');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
                 #  
4550                }                }
4551              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4552                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
# Line 3782  sub _tree_construction_main ($) { Line 4554  sub _tree_construction_main ($) {
4554                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4555                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4556                  !!!next-token;                  !!!next-token;
4557                  redo B;                  next B;
4558                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
4559                           $self->{insertion_mode} == AFTER_HEAD_IM) {
4560                  !!!cp ('t137');                  !!!cp ('t137');
4561                  !!!parse-error (type => 'unmatched end tag:noscript', token => $token);                  !!!parse-error (type => 'unmatched end tag:noscript', token => $token);
4562                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
4563                  !!!next-token;                  !!!next-token;
4564                  redo B;                  next B;
4565                } else {                } else {
4566                  !!!cp ('t138');                  !!!cp ('t138');
4567                  #                  #
# Line 3796  sub _tree_construction_main ($) { Line 4569  sub _tree_construction_main ($) {
4569              } elsif ({              } elsif ({
4570                        body => 1, html => 1,                        body => 1, html => 1,
4571                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4572                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM or
4573                  !!!cp ('t139');                    $self->{insertion_mode} == IN_HEAD_IM or
4574                  ## 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) {  
4575                  !!!cp ('t140');                  !!!cp ('t140');
4576                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4577                  ## Ignore the token                  ## Ignore the token
4578                  !!!next-token;                  !!!next-token;
4579                  redo B;                  next B;
4580                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4581                    !!!cp ('t140.1');
4582                    !!!parse-error (type => 'unmatched end tag:' . $token->{tag_name}, token => $token);
4583                    ## Ignore the token
4584                    !!!next-token;
4585                    next B;
4586                } else {                } else {
4587                  !!!cp ('t141');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
4588                }                }
4589                              } elsif ($token->{tag_name} eq 'p') {
4590                #                !!!cp ('t142');
4591              } elsif ({                !!!parse-error (type => 'unmatched end tag:p', token => $token);
4592                        p => 1, br => 1,                ## Ignore the token
4593                       }->{$token->{tag_name}}) {                !!!next-token;
4594                  next B;
4595                } elsif ($token->{tag_name} eq 'br') {
4596                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4597                  !!!cp ('t142');                  !!!cp ('t142.2');
4598                  ## As if <head>                  ## (before head) as if <head>, (in head) as if </head>
4599                  !!!create-element ($self->{head_element}, 'head',, $token);                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4600                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4601                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  $self->{insertion_mode} = AFTER_HEAD_IM;
4602      
4603                    ## Reprocess in the "after head" insertion mode...
4604                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4605                    !!!cp ('t143.2');
4606                    ## As if </head>
4607                    pop @{$self->{open_elements}};
4608                    $self->{insertion_mode} = AFTER_HEAD_IM;
4609      
4610                    ## Reprocess in the "after head" insertion mode...
4611                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4612                    !!!cp ('t143.3');
4613                    ## ISSUE: Two parse errors for <head><noscript></br>
4614                    !!!parse-error (type => 'unmatched end tag:br', token => $token);
4615                    ## As if </noscript>
4616                    pop @{$self->{open_elements}};
4617                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4618    
4619                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4620                } else {                  ## As if </head>
4621                  !!!cp ('t143');                  pop @{$self->{open_elements}};
4622                }                  $self->{insertion_mode} = AFTER_HEAD_IM;
4623    
4624                #                  ## Reprocess in the "after head" insertion mode...
4625              } else {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4626                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                  !!!cp ('t143.4');
                 !!!cp ('t144');  
4627                  #                  #
4628                } else {                } else {
4629                  !!!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;  
4630                }                }
4631    
4632                  ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
4633                  !!!parse-error (type => 'unmatched end tag:br', token => $token);
4634                  ## Ignore the token
4635                  !!!next-token;
4636                  next B;
4637                } else {
4638                  !!!cp ('t145');
4639                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4640                  ## Ignore the token
4641                  !!!next-token;
4642                  next B;
4643              }              }
4644    
4645              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
# Line 3869  sub _tree_construction_main ($) { Line 4665  sub _tree_construction_main ($) {
4665                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4666                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
4667                !!!next-token;                !!!next-token;
4668                redo B;                next B;
4669              } else {              } else {
4670                !!!cp ('t149');                !!!cp ('t149');
4671              }              }
# Line 3879  sub _tree_construction_main ($) { Line 4675  sub _tree_construction_main ($) {
4675              !!!insert-element ('body',, $token);              !!!insert-element ('body',, $token);
4676              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4677              ## reprocess              ## reprocess
4678              redo B;              next B;
4679        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4680          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4681            !!!cp ('t149.1');            !!!cp ('t149.1');
4682    
4683            ## NOTE: As if <head>            ## NOTE: As if <head>
4684            !!!create-element ($self->{head_element}, 'head',, $token);            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4685            $self->{open_elements}->[-1]->[0]->append_child            $self->{open_elements}->[-1]->[0]->append_child
4686                ($self->{head_element});                ($self->{head_element});
4687            #push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            #push @{$self->{open_elements}},
4688              #    [$self->{head_element}, $el_category->{head}];
4689            #$self->{insertion_mode} = IN_HEAD_IM;            #$self->{insertion_mode} = IN_HEAD_IM;
4690            ## NOTE: Reprocess.            ## NOTE: Reprocess.
4691    
# Line 3932  sub _tree_construction_main ($) { Line 4729  sub _tree_construction_main ($) {
4729          !!!insert-element ('body',, $token);          !!!insert-element ('body',, $token);
4730          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4731          ## NOTE: Reprocess.          ## NOTE: Reprocess.
4732          redo B;          next B;
4733        } else {        } else {
4734          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
4735        }        }
# Line 3947  sub _tree_construction_main ($) { Line 4744  sub _tree_construction_main ($) {
4744              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4745    
4746              !!!next-token;              !!!next-token;
4747              redo B;              next B;
4748            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
4749              if ({              if ({
4750                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
# Line 3957  sub _tree_construction_main ($) { Line 4754  sub _tree_construction_main ($) {
4754                  ## have an element in table scope                  ## have an element in table scope
4755                  for (reverse 0..$#{$self->{open_elements}}) {                  for (reverse 0..$#{$self->{open_elements}}) {
4756                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4757                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] & TABLE_CELL_EL) {
4758                      !!!cp ('t151');                      !!!cp ('t151');
4759    
4760                      ## Close the cell                      ## Close the cell
4761                      !!!back-token; # <?>                      !!!back-token; # <x>
4762                      $token = {type => END_TAG_TOKEN, tag_name => $node->[1],                      $token = {type => END_TAG_TOKEN,
4763                                  tag_name => $node->[0]->manakai_local_name,
4764                                line => $token->{line},                                line => $token->{line},
4765                                column => $token->{column}};                                column => $token->{column}};
4766                      redo B;                      next B;
4767                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
4768                      !!!cp ('t152');                      !!!cp ('t152');
4769                      ## ISSUE: This case can never be reached, maybe.                      ## ISSUE: This case can never be reached, maybe.
4770                      last;                      last;
# Line 3979  sub _tree_construction_main ($) { Line 4775  sub _tree_construction_main ($) {
4775                  !!!parse-error (type => 'start tag not allowed',                  !!!parse-error (type => 'start tag not allowed',
4776                      value => $token->{tag_name}, token => $token);                      value => $token->{tag_name}, token => $token);
4777                  ## Ignore the token                  ## Ignore the token
4778                    !!!nack ('t153.1');
4779                  !!!next-token;                  !!!next-token;
4780                  redo B;                  next B;
4781                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4782                  !!!parse-error (type => 'not closed:caption', token => $token);                  !!!parse-error (type => 'not closed:caption', token => $token);
4783                                    
# Line 3990  sub _tree_construction_main ($) { Line 4787  sub _tree_construction_main ($) {
4787                  INSCOPE: {                  INSCOPE: {
4788                    for (reverse 0..$#{$self->{open_elements}}) {                    for (reverse 0..$#{$self->{open_elements}}) {
4789                      my $node = $self->{open_elements}->[$_];                      my $node = $self->{open_elements}->[$_];
4790                      if ($node->[1] eq 'caption') {                      if ($node->[1] & CAPTION_EL) {
4791                        !!!cp ('t155');                        !!!cp ('t155');
4792                        $i = $_;                        $i = $_;
4793                        last INSCOPE;                        last INSCOPE;
4794                      } elsif ({                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
                               table => 1, html => 1,  
                              }->{$node->[1]}) {  
4795                        !!!cp ('t156');                        !!!cp ('t156');
4796                        last;                        last;
4797                      }                      }
# Line 4006  sub _tree_construction_main ($) { Line 4801  sub _tree_construction_main ($) {
4801                    !!!parse-error (type => 'start tag not allowed',                    !!!parse-error (type => 'start tag not allowed',
4802                                    value => $token->{tag_name}, token => $token);                                    value => $token->{tag_name}, token => $token);
4803                    ## Ignore the token                    ## Ignore the token
4804                      !!!nack ('t157.1');
4805                    !!!next-token;                    !!!next-token;
4806                    redo B;                    next B;
4807                  } # INSCOPE                  } # INSCOPE
4808                                    
4809                  ## generate implied end tags                  ## generate implied end tags
4810                  while ({                  while ($self->{open_elements}->[-1]->[1]
4811                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4812                    !!!cp ('t158');                    !!!cp ('t158');
4813                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4814                  }                  }
4815    
4816                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4817                    !!!cp ('t159');                    !!!cp ('t159');
4818                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                    !!!parse-error (type => 'not closed',
4819                                      value => $self->{open_elements}->[-1]->[0]
4820                                          ->manakai_local_name,
4821                                      token => $token);
4822                  } else {                  } else {
4823                    !!!cp ('t160');                    !!!cp ('t160');
4824                  }                  }
# Line 4032  sub _tree_construction_main ($) { Line 4830  sub _tree_construction_main ($) {
4830                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4831                                    
4832                  ## reprocess                  ## reprocess
4833                  redo B;                  !!!ack-later;
4834                    next B;
4835                } else {                } else {
4836                  !!!cp ('t161');                  !!!cp ('t161');
4837                  #                  #
# Line 4048  sub _tree_construction_main ($) { Line 4847  sub _tree_construction_main ($) {
4847                  my $i;                  my $i;
4848                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4849                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4850                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4851                      !!!cp ('t163');                      !!!cp ('t163');
4852                      $i = $_;                      $i = $_;
4853                      last INSCOPE;                      last INSCOPE;
4854                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
4855                      !!!cp ('t164');                      !!!cp ('t164');
4856                      last INSCOPE;                      last INSCOPE;
4857                    }                    }
# Line 4064  sub _tree_construction_main ($) { Line 4861  sub _tree_construction_main ($) {
4861                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4862                      ## Ignore the token                      ## Ignore the token
4863                      !!!next-token;                      !!!next-token;
4864                      redo B;                      next B;
4865                    }                    }
4866                                    
4867                  ## generate implied end tags                  ## generate implied end tags
4868                  while ({                  while ($self->{open_elements}->[-1]->[1]
4869                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4870                    !!!cp ('t166');                    !!!cp ('t166');
4871                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4872                  }                  }
4873    
4874                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
4875                            ne $token->{tag_name}) {
4876                    !!!cp ('t167');                    !!!cp ('t167');
4877                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                    !!!parse-error (type => 'not closed',
4878                                      value => $self->{open_elements}->[-1]->[0]
4879                                          ->manakai_local_name,
4880                                      token => $token);
4881                  } else {                  } else {
4882                    !!!cp ('t168');                    !!!cp ('t168');
4883                  }                  }
# Line 4089  sub _tree_construction_main ($) { Line 4889  sub _tree_construction_main ($) {
4889                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
4890                                    
4891                  !!!next-token;                  !!!next-token;
4892                  redo B;                  next B;
4893                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4894                  !!!cp ('t169');                  !!!cp ('t169');
4895                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4896                  ## Ignore the token                  ## Ignore the token
4897                  !!!next-token;                  !!!next-token;
4898                  redo B;                  next B;
4899                } else {                } else {
4900                  !!!cp ('t170');                  !!!cp ('t170');
4901                  #                  #
# Line 4107  sub _tree_construction_main ($) { Line 4907  sub _tree_construction_main ($) {
4907                  INSCOPE: {                  INSCOPE: {
4908                    for (reverse 0..$#{$self->{open_elements}}) {                    for (reverse 0..$#{$self->{open_elements}}) {
4909                      my $node = $self->{open_elements}->[$_];                      my $node = $self->{open_elements}->[$_];
4910                      if ($node->[1] eq $token->{tag_name}) {                      if ($node->[1] & CAPTION_EL) {
4911                        !!!cp ('t171');                        !!!cp ('t171');
4912                        $i = $_;                        $i = $_;
4913                        last INSCOPE;                        last INSCOPE;
4914                      } elsif ({                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
                               table => 1, html => 1,  
                              }->{$node->[1]}) {  
4915                        !!!cp ('t172');                        !!!cp ('t172');
4916                        last;                        last;
4917                      }                      }
# Line 4124  sub _tree_construction_main ($) { Line 4922  sub _tree_construction_main ($) {
4922                                    value => $token->{tag_name}, token => $token);                                    value => $token->{tag_name}, token => $token);
4923                    ## Ignore the token                    ## Ignore the token
4924                    !!!next-token;                    !!!next-token;
4925                    redo B;                    next B;
4926                  } # INSCOPE                  } # INSCOPE
4927                                    
4928                  ## generate implied end tags                  ## generate implied end tags
4929                  while ({                  while ($self->{open_elements}->[-1]->[1]
4930                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4931                    !!!cp ('t174');                    !!!cp ('t174');
4932                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4933                  }                  }
4934                                    
4935                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4936                    !!!cp ('t175');                    !!!cp ('t175');
4937                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                    !!!parse-error (type => 'not closed',
4938                                      value => $self->{open_elements}->[-1]->[0]
4939                                          ->manakai_local_name,
4940                                      token => $token);
4941                  } else {                  } else {
4942                    !!!cp ('t176');                    !!!cp ('t176');
4943                  }                  }
# Line 4149  sub _tree_construction_main ($) { Line 4949  sub _tree_construction_main ($) {
4949                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4950                                    
4951                  !!!next-token;                  !!!next-token;
4952                  redo B;                  next B;
4953                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
4954                  !!!cp ('t177');                  !!!cp ('t177');
4955                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4956                  ## Ignore the token                  ## Ignore the token
4957                  !!!next-token;                  !!!next-token;
4958                  redo B;                  next B;
4959                } else {                } else {
4960                  !!!cp ('t178');                  !!!cp ('t178');
4961                  #                  #
# Line 4171  sub _tree_construction_main ($) { Line 4971  sub _tree_construction_main ($) {
4971                INSCOPE: {                INSCOPE: {
4972                  for (reverse 0..$#{$self->{open_elements}}) {                  for (reverse 0..$#{$self->{open_elements}}) {
4973                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4974                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4975                      !!!cp ('t179');                      !!!cp ('t179');
4976                      $i = $_;                      $i = $_;
4977    
4978                      ## Close the cell                      ## Close the cell
4979                      !!!back-token; # </?>                      !!!back-token; # </x>
4980                      $token = {type => END_TAG_TOKEN, tag_name => $tn,                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
4981                                line => $token->{line},                                line => $token->{line},
4982                                column => $token->{column}};                                column => $token->{column}};
4983                      redo B;                      next B;
4984                    } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {                    } elsif ($node->[1] & TABLE_CELL_EL) {
4985                      !!!cp ('t180');                      !!!cp ('t180');
4986                      $tn = $node->[1];                      $tn = $node->[0]->manakai_local_name;
4987                      ## NOTE: There is exactly one |td| or |th| element                      ## NOTE: There is exactly one |td| or |th| element
4988                      ## in scope in the stack of open elements by definition.                      ## in scope in the stack of open elements by definition.
4989                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
4990                      ## ISSUE: Can this be reached?                      ## ISSUE: Can this be reached?
4991                      !!!cp ('t181');                      !!!cp ('t181');
4992                      last;                      last;
# Line 4200  sub _tree_construction_main ($) { Line 4998  sub _tree_construction_main ($) {
4998                      value => $token->{tag_name}, token => $token);                      value => $token->{tag_name}, token => $token);
4999                  ## Ignore the token                  ## Ignore the token
5000                  !!!next-token;                  !!!next-token;
5001                  redo B;                  next B;
5002                } # INSCOPE                } # INSCOPE
5003              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
5004                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
# Line 4211  sub _tree_construction_main ($) { Line 5009  sub _tree_construction_main ($) {
5009                my $i;                my $i;
5010                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5011                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5012                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
5013                    !!!cp ('t184');                    !!!cp ('t184');
5014                    $i = $_;                    $i = $_;
5015                    last INSCOPE;                    last INSCOPE;
5016                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5017                    !!!cp ('t185');                    !!!cp ('t185');
5018                    last INSCOPE;                    last INSCOPE;
5019                  }                  }
# Line 4227  sub _tree_construction_main ($) { Line 5023  sub _tree_construction_main ($) {
5023                  !!!parse-error (type => 'unmatched end tag:caption', token => $token);                  !!!parse-error (type => 'unmatched end tag:caption', token => $token);
5024                  ## Ignore the token                  ## Ignore the token
5025                  !!!next-token;                  !!!next-token;
5026                  redo B;                  next B;
5027                }                }
5028                                
5029                ## generate implied end tags                ## generate implied end tags
5030                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
5031                  !!!cp ('t187');                  !!!cp ('t187');
5032                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5033                }                }
5034    
5035                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5036                  !!!cp ('t188');                  !!!cp ('t188');
5037                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                  !!!parse-error (type => 'not closed',
5038                                    value => $self->{open_elements}->[-1]->[0]
5039                                        ->manakai_local_name,
5040                                    token => $token);
5041                } else {                } else {
5042                  !!!cp ('t189');                  !!!cp ('t189');
5043                }                }
# Line 4252  sub _tree_construction_main ($) { Line 5049  sub _tree_construction_main ($) {
5049                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5050    
5051                ## reprocess                ## reprocess
5052                redo B;                next B;
5053              } elsif ({              } elsif ({
5054                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
5055                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
# Line 4261  sub _tree_construction_main ($) { Line 5058  sub _tree_construction_main ($) {
5058                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5059                  ## Ignore the token                  ## Ignore the token
5060                  !!!next-token;                  !!!next-token;
5061                  redo B;                  next B;
5062                } else {                } else {
5063                  !!!cp ('t191');                  !!!cp ('t191');
5064                  #                  #
# Line 4275  sub _tree_construction_main ($) { Line 5072  sub _tree_construction_main ($) {
5072                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5073                ## Ignore the token                ## Ignore the token
5074                !!!next-token;                !!!next-token;
5075                redo B;                next B;
5076              } else {              } else {
5077                !!!cp ('t193');                !!!cp ('t193');
5078                #                #
5079              }              }
5080        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5081          for my $entry (@{$self->{open_elements}}) {          for my $entry (@{$self->{open_elements}}) {
5082            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]}) {  
5083              !!!cp ('t75');              !!!cp ('t75');
5084              !!!parse-error (type => 'in body:#eof', token => $token);              !!!parse-error (type => 'in body:#eof', token => $token);
5085              last;              last;
# Line 4309  sub _tree_construction_main ($) { Line 5103  sub _tree_construction_main ($) {
5103            unless (length $token->{data}) {            unless (length $token->{data}) {
5104              !!!cp ('t194');              !!!cp ('t194');
5105              !!!next-token;              !!!next-token;
5106              redo B;              next B;
5107            } else {            } else {
5108              !!!cp ('t195');              !!!cp ('t195');
5109            }            }
# Line 4323  sub _tree_construction_main ($) { Line 5117  sub _tree_construction_main ($) {
5117              ## result in a new Text node.              ## result in a new Text node.
5118              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
5119                            
5120              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]}) {  
5121                # MUST                # MUST
5122                my $foster_parent_element;                my $foster_parent_element;
5123                my $next_sibling;                my $next_sibling;
5124                my $prev_sibling;                my $prev_sibling;
5125                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
5126                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
5127                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
5128                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
5129                      !!!cp ('t196');                      !!!cp ('t196');
# Line 4367  sub _tree_construction_main ($) { Line 5158  sub _tree_construction_main ($) {
5158          }          }
5159                            
5160          !!!next-token;          !!!next-token;
5161          redo B;          next B;
5162        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5163              if ({              if ({
5164                   tr => ($self->{insertion_mode} != IN_ROW_IM),                   tr => ($self->{insertion_mode} != IN_ROW_IM),
# Line 4375  sub _tree_construction_main ($) { Line 5166  sub _tree_construction_main ($) {
5166                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
5167                if ($self->{insertion_mode} == IN_TABLE_IM) {                if ($self->{insertion_mode} == IN_TABLE_IM) {
5168                  ## Clear back to table context                  ## Clear back to table context
5169                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5170                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5171                    !!!cp ('t201');                    !!!cp ('t201');
5172                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5173                  }                  }
# Line 4393  sub _tree_construction_main ($) { Line 5184  sub _tree_construction_main ($) {
5184                  }                  }
5185                                    
5186                  ## Clear back to table body context                  ## Clear back to table body context
5187                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5188                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5189                    !!!cp ('t203');                    !!!cp ('t203');
5190                    ## ISSUE: Can this case be reached?                    ## ISSUE: Can this case be reached?
5191                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4405  sub _tree_construction_main ($) { Line 5195  sub _tree_construction_main ($) {
5195                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5196                    !!!cp ('t204');                    !!!cp ('t204');
5197                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5198                      !!!nack ('t204');
5199                    !!!next-token;                    !!!next-token;
5200                    redo B;                    next B;
5201                  } else {                  } else {
5202                    !!!cp ('t205');                    !!!cp ('t205');
5203                    !!!insert-element ('tr',, $token);                    !!!insert-element ('tr',, $token);
# Line 4417  sub _tree_construction_main ($) { Line 5208  sub _tree_construction_main ($) {
5208                }                }
5209    
5210                ## Clear back to table row context                ## Clear back to table row context
5211                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5212                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5213                  !!!cp ('t207');                  !!!cp ('t207');
5214                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5215                }                }
# Line 4429  sub _tree_construction_main ($) { Line 5219  sub _tree_construction_main ($) {
5219    
5220                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
5221                                
5222                  !!!nack ('t207.1');
5223                !!!next-token;                !!!next-token;
5224                redo B;                next B;
5225              } elsif ({              } elsif ({
5226                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5227                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
# Line 4442  sub _tree_construction_main ($) { Line 5233  sub _tree_construction_main ($) {
5233                  my $i;                  my $i;
5234                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5235                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5236                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5237                      !!!cp ('t208');                      !!!cp ('t208');
5238                      $i = $_;                      $i = $_;
5239                      last INSCOPE;                      last INSCOPE;
5240                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             html => 1,  
   
                             ## NOTE: This element does not appear here, maybe.  
                             table => 1,  
                            }->{$node->[1]}) {  
5241                      !!!cp ('t209');                      !!!cp ('t209');
5242                      last INSCOPE;                      last INSCOPE;
5243                    }                    }
5244                  } # INSCOPE                  } # INSCOPE
5245                  unless (defined $i) {                  unless (defined $i) {
5246                   !!!cp ('t210');                    !!!cp ('t210');
5247  ## TODO: This type is wrong.  ## TODO: This type is wrong.
5248                   !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);                    !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);
5249                    ## Ignore the token                    ## Ignore the token
5250                      !!!nack ('t210.1');
5251                    !!!next-token;                    !!!next-token;
5252                    redo B;                    next B;
5253                  }                  }
5254                                    
5255                  ## Clear back to table row context                  ## Clear back to table row context
5256                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5257                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5258                    !!!cp ('t211');                    !!!cp ('t211');
5259                    ## ISSUE: Can this case be reached?                    ## ISSUE: Can this case be reached?
5260                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4479  sub _tree_construction_main ($) { Line 5265  sub _tree_construction_main ($) {
5265                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5266                    !!!cp ('t212');                    !!!cp ('t212');
5267                    ## reprocess                    ## reprocess
5268                    redo B;                    !!!ack-later;
5269                      next B;
5270                  } else {                  } else {
5271                    !!!cp ('t213');                    !!!cp ('t213');
5272                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
# Line 4491  sub _tree_construction_main ($) { Line 5278  sub _tree_construction_main ($) {
5278                  my $i;                  my $i;
5279                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5280                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5281                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5282                      !!!cp ('t214');                      !!!cp ('t214');
5283                      $i = $_;                      $i = $_;
5284                      last INSCOPE;                      last INSCOPE;
5285                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5286                      !!!cp ('t215');                      !!!cp ('t215');
5287                      last INSCOPE;                      last INSCOPE;
5288                    }                    }
# Line 4509  sub _tree_construction_main ($) { Line 5292  sub _tree_construction_main ($) {
5292  ## TODO: This erorr type ios wrong.  ## TODO: This erorr type ios wrong.
5293                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5294                    ## Ignore the token                    ## Ignore the token
5295                      !!!nack ('t216.1');
5296                    !!!next-token;                    !!!next-token;
5297                    redo B;                    next B;
5298                  }                  }
5299    
5300                  ## Clear back to table body context                  ## Clear back to table body context
5301                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5302                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5303                    !!!cp ('t217');                    !!!cp ('t217');
5304                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5305                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4538  sub _tree_construction_main ($) { Line 5321  sub _tree_construction_main ($) {
5321    
5322                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
5323                  ## Clear back to table context                  ## Clear back to table context
5324                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5325                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5326                    !!!cp ('t219');                    !!!cp ('t219');
5327                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5328                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4548  sub _tree_construction_main ($) { Line 5331  sub _tree_construction_main ($) {
5331                  !!!insert-element ('colgroup',, $token);                  !!!insert-element ('colgroup',, $token);
5332                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5333                  ## reprocess                  ## reprocess
5334                  redo B;                  !!!ack-later;
5335                    next B;
5336                } elsif ({                } elsif ({
5337                          caption => 1,                          caption => 1,
5338                          colgroup => 1,                          colgroup => 1,
5339                          tbody => 1, tfoot => 1, thead => 1,                          tbody => 1, tfoot => 1, thead => 1,
5340                         }->{$token->{tag_name}}) {                         }->{$token->{tag_name}}) {
5341                  ## Clear back to table context                  ## Clear back to table context
5342                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5343                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5344                    !!!cp ('t220');                    !!!cp ('t220');
5345                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5346                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4574  sub _tree_construction_main ($) { Line 5358  sub _tree_construction_main ($) {
5358                                             thead => IN_TABLE_BODY_IM,                                             thead => IN_TABLE_BODY_IM,
5359                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
5360                  !!!next-token;                  !!!next-token;
5361                  redo B;                  !!!nack ('t220.1');
5362                    next B;
5363                } else {                } else {
5364                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
5365                }                }
5366              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5367                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                !!!parse-error (type => 'not closed',
5368                                  value => $self->{open_elements}->[-1]->[0]
5369                                      ->manakai_local_name,
5370                                  token => $token);
5371    
5372                ## As if </table>                ## As if </table>
5373                ## have a table element in table scope                ## have a table element in table scope
5374                my $i;                my $i;
5375                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5376                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5377                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5378                    !!!cp ('t221');                    !!!cp ('t221');
5379                    $i = $_;                    $i = $_;
5380                    last INSCOPE;                    last INSCOPE;
5381                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           #table => 1,  
                           html => 1,  
                          }->{$node->[1]}) {  
5382                    !!!cp ('t222');                    !!!cp ('t222');
5383                    last INSCOPE;                    last INSCOPE;
5384                  }                  }
# Line 4603  sub _tree_construction_main ($) { Line 5388  sub _tree_construction_main ($) {
5388  ## TODO: The following is wrong, maybe.  ## TODO: The following is wrong, maybe.
5389                  !!!parse-error (type => 'unmatched end tag:table', token => $token);                  !!!parse-error (type => 'unmatched end tag:table', token => $token);
5390                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5391                    !!!nack ('t223.1');
5392                  !!!next-token;                  !!!next-token;
5393                  redo B;                  next B;
5394                }                }
5395                                
5396  ## TODO: Followings are removed from the latest spec.  ## TODO: Followings are removed from the latest spec.
5397                ## generate implied end tags                ## generate implied end tags
5398                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
5399                  !!!cp ('t224');                  !!!cp ('t224');
5400                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5401                }                }
5402    
5403                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5404                  !!!cp ('t225');                  !!!cp ('t225');
5405  ## ISSUE: Can this case be reached?                  ## NOTE: |<table><tr><table>|
5406                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                  !!!parse-error (type => 'not closed',
5407                                    value => $self->{open_elements}->[-1]->[0]
5408                                        ->manakai_local_name,
5409                                    token => $token);
5410                } else {                } else {
5411                  !!!cp ('t226');                  !!!cp ('t226');
5412                }                }
# Line 4629  sub _tree_construction_main ($) { Line 5416  sub _tree_construction_main ($) {
5416    
5417                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5418    
5419                ## reprocess            ## reprocess
5420                redo B;            !!!ack-later;
5421              next B;
5422          } elsif ($token->{tag_name} eq 'style') {          } elsif ($token->{tag_name} eq 'style') {
5423            if (not $open_tables->[-1]->[1]) { # tainted            if (not $open_tables->[-1]->[1]) { # tainted
5424              !!!cp ('t227.8');              !!!cp ('t227.8');
5425              ## NOTE: This is a "as if in head" code clone.              ## NOTE: This is a "as if in head" code clone.
5426              $parse_rcdata->(CDATA_CONTENT_MODEL);              $parse_rcdata->(CDATA_CONTENT_MODEL);
5427              redo B;              next B;
5428            } else {            } else {
5429              !!!cp ('t227.7');              !!!cp ('t227.7');
5430              #              #
# Line 4646  sub _tree_construction_main ($) { Line 5434  sub _tree_construction_main ($) {
5434              !!!cp ('t227.6');              !!!cp ('t227.6');
5435              ## NOTE: This is a "as if in head" code clone.              ## NOTE: This is a "as if in head" code clone.
5436              $script_start_tag->();              $script_start_tag->();
5437              redo B;              next B;
5438            } else {            } else {
5439              !!!cp ('t227.5');              !!!cp ('t227.5');
5440              #              #
# Line 4666  sub _tree_construction_main ($) { Line 5454  sub _tree_construction_main ($) {
5454                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5455    
5456                  !!!next-token;                  !!!next-token;
5457                  redo B;                  !!!ack ('t227.2.1');
5458                    next B;
5459                } else {                } else {
5460                  !!!cp ('t227.2');                  !!!cp ('t227.2');
5461                  #                  #
# Line 4695  sub _tree_construction_main ($) { Line 5484  sub _tree_construction_main ($) {
5484                my $i;                my $i;
5485                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5486                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5487                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_ROW_EL) {
5488                    !!!cp ('t228');                    !!!cp ('t228');
5489                    $i = $_;                    $i = $_;
5490                    last INSCOPE;                    last INSCOPE;
5491                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5492                    !!!cp ('t229');                    !!!cp ('t229');
5493                    last INSCOPE;                    last INSCOPE;
5494                  }                  }
# Line 4710  sub _tree_construction_main ($) { Line 5497  sub _tree_construction_main ($) {
5497                  !!!cp ('t230');                  !!!cp ('t230');
5498                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5499                  ## Ignore the token                  ## Ignore the token
5500                    !!!nack ('t230.1');
5501                  !!!next-token;                  !!!next-token;
5502                  redo B;                  next B;
5503                } else {                } else {
5504                  !!!cp ('t232');                  !!!cp ('t232');
5505                }                }
5506    
5507                ## Clear back to table row context                ## Clear back to table row context
5508                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5509                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5510                  !!!cp ('t231');                  !!!cp ('t231');
5511  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
5512                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 4728  sub _tree_construction_main ($) { Line 5515  sub _tree_construction_main ($) {
5515                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5516                $self->{insertion_mode} = IN_TABLE_BODY_IM;                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5517                !!!next-token;                !!!next-token;
5518                redo B;                !!!nack ('t231.1');
5519                  next B;
5520              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5521                if ($self->{insertion_mode} == IN_ROW_IM) {                if ($self->{insertion_mode} == IN_ROW_IM) {
5522                  ## As if </tr>                  ## As if </tr>
# Line 4736  sub _tree_construction_main ($) { Line 5524  sub _tree_construction_main ($) {
5524                  my $i;                  my $i;
5525                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5526                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5527                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5528                      !!!cp ('t233');                      !!!cp ('t233');
5529                      $i = $_;                      $i = $_;
5530                      last INSCOPE;                      last INSCOPE;
5531                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5532                      !!!cp ('t234');                      !!!cp ('t234');
5533                      last INSCOPE;                      last INSCOPE;
5534                    }                    }
# Line 4752  sub _tree_construction_main ($) { Line 5538  sub _tree_construction_main ($) {
5538  ## TODO: The following is wrong.  ## TODO: The following is wrong.
5539                    !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);                    !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);
5540                    ## Ignore the token                    ## Ignore the token
5541                      !!!nack ('t236.1');
5542                    !!!next-token;                    !!!next-token;
5543                    redo B;                    next B;
5544                  }                  }
5545                                    
5546                  ## Clear back to table row context                  ## Clear back to table row context
5547                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5548                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5549                    !!!cp ('t236');                    !!!cp ('t236');
5550  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
5551                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4775  sub _tree_construction_main ($) { Line 5561  sub _tree_construction_main ($) {
5561                  my $i;                  my $i;
5562                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5563                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5564                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5565                      !!!cp ('t237');                      !!!cp ('t237');
5566                      $i = $_;                      $i = $_;
5567                      last INSCOPE;                      last INSCOPE;
5568                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5569                      !!!cp ('t238');                      !!!cp ('t238');
5570                      last INSCOPE;                      last INSCOPE;
5571                    }                    }
# Line 4792  sub _tree_construction_main ($) { Line 5574  sub _tree_construction_main ($) {
5574                    !!!cp ('t239');                    !!!cp ('t239');
5575                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5576                    ## Ignore the token                    ## Ignore the token
5577                      !!!nack ('t239.1');
5578                    !!!next-token;                    !!!next-token;
5579                    redo B;                    next B;
5580                  }                  }
5581                                    
5582                  ## Clear back to table body context                  ## Clear back to table body context
5583                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5584                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5585                    !!!cp ('t240');                    !!!cp ('t240');
5586                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5587                  }                  }
# Line 4825  sub _tree_construction_main ($) { Line 5607  sub _tree_construction_main ($) {
5607                my $i;                my $i;
5608                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5609                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5610                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_EL) {
5611                    !!!cp ('t241');                    !!!cp ('t241');
5612                    $i = $_;                    $i = $_;
5613                    last INSCOPE;                    last INSCOPE;
5614                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5615                    !!!cp ('t242');                    !!!cp ('t242');
5616                    last INSCOPE;                    last INSCOPE;
5617                  }                  }
# Line 4840  sub _tree_construction_main ($) { Line 5620  sub _tree_construction_main ($) {
5620                  !!!cp ('t243');                  !!!cp ('t243');
5621                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5622                  ## Ignore the token                  ## Ignore the token
5623                    !!!nack ('t243.1');
5624                  !!!next-token;                  !!!next-token;
5625                  redo B;                  next B;
5626                }                }
5627                                    
5628                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
# Line 4850  sub _tree_construction_main ($) { Line 5631  sub _tree_construction_main ($) {
5631                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5632                                
5633                !!!next-token;                !!!next-token;
5634                redo B;                next B;
5635              } elsif ({              } elsif ({
5636                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5637                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
# Line 4860  sub _tree_construction_main ($) { Line 5641  sub _tree_construction_main ($) {
5641                  my $i;                  my $i;
5642                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5643                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5644                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5645                      !!!cp ('t247');                      !!!cp ('t247');
5646                      $i = $_;                      $i = $_;
5647                      last INSCOPE;                      last INSCOPE;
5648                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5649                      !!!cp ('t248');                      !!!cp ('t248');
5650                      last INSCOPE;                      last INSCOPE;
5651                    }                    }
# Line 4875  sub _tree_construction_main ($) { Line 5654  sub _tree_construction_main ($) {
5654                      !!!cp ('t249');                      !!!cp ('t249');
5655                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5656                      ## Ignore the token                      ## Ignore the token
5657                        !!!nack ('t249.1');
5658                      !!!next-token;                      !!!next-token;
5659                      redo B;                      next B;
5660                    }                    }
5661                                    
5662                  ## As if </tr>                  ## As if </tr>
# Line 4884  sub _tree_construction_main ($) { Line 5664  sub _tree_construction_main ($) {
5664                  my $i;                  my $i;
5665                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5666                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5667                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5668                      !!!cp ('t250');                      !!!cp ('t250');
5669                      $i = $_;                      $i = $_;
5670                      last INSCOPE;                      last INSCOPE;
5671                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5672                      !!!cp ('t251');                      !!!cp ('t251');
5673                      last INSCOPE;                      last INSCOPE;
5674                    }                    }
# Line 4899  sub _tree_construction_main ($) { Line 5677  sub _tree_construction_main ($) {
5677                      !!!cp ('t252');                      !!!cp ('t252');
5678                      !!!parse-error (type => 'unmatched end tag:tr', token => $token);                      !!!parse-error (type => 'unmatched end tag:tr', token => $token);
5679                      ## Ignore the token                      ## Ignore the token
5680                        !!!nack ('t252.1');
5681                      !!!next-token;                      !!!next-token;
5682                      redo B;                      next B;
5683                    }                    }
5684                                    
5685                  ## Clear back to table row context                  ## Clear back to table row context
5686                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5687                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5688                    !!!cp ('t253');                    !!!cp ('t253');
5689  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
5690                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4921  sub _tree_construction_main ($) { Line 5699  sub _tree_construction_main ($) {
5699                my $i;                my $i;
5700                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5701                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5702                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5703                    !!!cp ('t254');                    !!!cp ('t254');
5704                    $i = $_;                    $i = $_;
5705                    last INSCOPE;                    last INSCOPE;
5706                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5707                    !!!cp ('t255');                    !!!cp ('t255');
5708                    last INSCOPE;                    last INSCOPE;
5709                  }                  }
# Line 4936  sub _tree_construction_main ($) { Line 5712  sub _tree_construction_main ($) {
5712                  !!!cp ('t256');                  !!!cp ('t256');
5713                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5714                  ## Ignore the token                  ## Ignore the token
5715                    !!!nack ('t256.1');
5716                  !!!next-token;                  !!!next-token;
5717                  redo B;                  next B;
5718                }                }
5719    
5720                ## Clear back to table body context                ## Clear back to table body context
5721                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5722                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5723                  !!!cp ('t257');                  !!!cp ('t257');
5724  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
5725                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 4951  sub _tree_construction_main ($) { Line 5727  sub _tree_construction_main ($) {
5727    
5728                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5729                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5730                  !!!nack ('t257.1');
5731                !!!next-token;                !!!next-token;
5732                redo B;                next B;
5733              } elsif ({              } elsif ({
5734                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
5735                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
5736                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5737                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
5738                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5739                !!!cp ('t258');            !!!cp ('t258');
5740                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5741                ## Ignore the token            ## Ignore the token
5742                !!!next-token;            !!!nack ('t258.1');
5743                redo B;             !!!next-token;
5744              next B;
5745          } else {          } else {
5746            !!!cp ('t259');            !!!cp ('t259');
5747            !!!parse-error (type => 'in table:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'in table:/'.$token->{tag_name}, token => $token);
# Line 4972  sub _tree_construction_main ($) { Line 5750  sub _tree_construction_main ($) {
5750            #            #
5751          }          }
5752        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5753          unless ($self->{open_elements}->[-1]->[1] eq 'html' and          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5754                  @{$self->{open_elements}} == 1) { # redundant, maybe                  @{$self->{open_elements}} == 1) { # redundant, maybe
5755            !!!parse-error (type => 'in body:#eof', token => $token);            !!!parse-error (type => 'in body:#eof', token => $token);
5756            !!!cp ('t259.1');            !!!cp ('t259.1');
# Line 4994  sub _tree_construction_main ($) { Line 5772  sub _tree_construction_main ($) {
5772                unless (length $token->{data}) {                unless (length $token->{data}) {
5773                  !!!cp ('t260');                  !!!cp ('t260');
5774                  !!!next-token;                  !!!next-token;
5775                  redo B;                  next B;
5776                }                }
5777              }              }
5778                            
# Line 5005  sub _tree_construction_main ($) { Line 5783  sub _tree_construction_main ($) {
5783                !!!cp ('t262');                !!!cp ('t262');
5784                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5785                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5786                  !!!ack ('t262.1');
5787                !!!next-token;                !!!next-token;
5788                redo B;                next B;
5789              } else {              } else {
5790                !!!cp ('t263');                !!!cp ('t263');
5791                #                #
5792              }              }
5793            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
5794              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
5795                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5796                  !!!cp ('t264');                  !!!cp ('t264');
5797                  !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);                  !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5798                  ## Ignore the token                  ## Ignore the token
5799                  !!!next-token;                  !!!next-token;
5800                  redo B;                  next B;
5801                } else {                } else {
5802                  !!!cp ('t265');                  !!!cp ('t265');
5803                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
5804                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5805                  !!!next-token;                  !!!next-token;
5806                  redo B;                              next B;            
5807                }                }
5808              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
5809                !!!cp ('t266');                !!!cp ('t266');
5810                !!!parse-error (type => 'unmatched end tag:col', token => $token);                !!!parse-error (type => 'unmatched end tag:col', token => $token);
5811                ## Ignore the token                ## Ignore the token
5812                !!!next-token;                !!!next-token;
5813                redo B;                next B;
5814              } else {              } else {
5815                !!!cp ('t267');                !!!cp ('t267');
5816                #                #
5817              }              }
5818        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5819          if ($self->{open_elements}->[-1]->[1] eq 'html' or          if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5820              @{$self->{open_elements}} == 1) { # redundant, maybe              @{$self->{open_elements}} == 1) { # redundant, maybe
5821            !!!cp ('t270.2');            !!!cp ('t270.2');
5822            ## Stop parsing.            ## Stop parsing.
# Line 5048  sub _tree_construction_main ($) { Line 5827  sub _tree_construction_main ($) {
5827            pop @{$self->{open_elements}}; # colgroup            pop @{$self->{open_elements}}; # colgroup
5828            $self->{insertion_mode} = IN_TABLE_IM;            $self->{insertion_mode} = IN_TABLE_IM;
5829            ## Reprocess.            ## Reprocess.
5830            redo B;            next B;
5831          }          }
5832        } else {        } else {
5833          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5834        }        }
5835    
5836            ## As if </colgroup>            ## As if </colgroup>
5837            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5838              !!!cp ('t269');              !!!cp ('t269');
5839  ## TODO: Wrong error type?  ## TODO: Wrong error type?
5840              !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);              !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5841              ## Ignore the token              ## Ignore the token
5842                !!!nack ('t269.1');
5843              !!!next-token;              !!!next-token;
5844              redo B;              next B;
5845            } else {            } else {
5846              !!!cp ('t270');              !!!cp ('t270');
5847              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
5848              $self->{insertion_mode} = IN_TABLE_IM;              $self->{insertion_mode} = IN_TABLE_IM;
5849                !!!ack-later;
5850              ## reprocess              ## reprocess
5851              redo B;              next B;
5852            }            }
5853      } elsif ($self->{insertion_mode} & SELECT_IMS) {      } elsif ($self->{insertion_mode} & SELECT_IMS) {
5854        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5855          !!!cp ('t271');          !!!cp ('t271');
5856          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5857          !!!next-token;          !!!next-token;
5858          redo B;          next B;
5859        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5860              if ($token->{tag_name} eq 'option') {          if ($token->{tag_name} eq 'option') {
5861                if ($self->{open_elements}->[-1]->[1] eq 'option') {            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5862                  !!!cp ('t272');              !!!cp ('t272');
5863                  ## As if </option>              ## As if </option>
5864                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5865                } else {            } else {
5866                  !!!cp ('t273');              !!!cp ('t273');
5867                }            }
5868    
5869                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5870                !!!next-token;            !!!nack ('t273.1');
5871                redo B;            !!!next-token;
5872              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
5873                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
5874                  !!!cp ('t274');            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5875                  ## As if </option>              !!!cp ('t274');
5876                  pop @{$self->{open_elements}};              ## As if </option>
5877                } else {              pop @{$self->{open_elements}};
5878                  !!!cp ('t275');            } else {
5879                }              !!!cp ('t275');
5880              }
5881    
5882                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5883                  !!!cp ('t276');              !!!cp ('t276');
5884                  ## As if </optgroup>              ## As if </optgroup>
5885                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5886                } else {            } else {
5887                  !!!cp ('t277');              !!!cp ('t277');
5888                }            }
5889    
5890                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5891                !!!next-token;            !!!nack ('t277.1');
5892                redo B;            !!!next-token;
5893          } elsif ($token->{tag_name} eq 'select' or            next B;
5894                   $token->{tag_name} eq 'input' or          } elsif ({
5895                       select => 1, input => 1, textarea => 1,
5896                     }->{$token->{tag_name}} or
5897                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5898                    {                    {
5899                     caption => 1, table => 1,                     caption => 1, table => 1,
# Line 5120  sub _tree_construction_main ($) { Line 5904  sub _tree_construction_main ($) {
5904            !!!parse-error (type => 'not closed:select', token => $token);            !!!parse-error (type => 'not closed:select', token => $token);
5905            ## NOTE: As if the token were </select> (<select> case) or            ## NOTE: As if the token were </select> (<select> case) or
5906            ## as if there were </select> (otherwise).            ## as if there were </select> (otherwise).
5907                ## have an element in table scope            ## have an element in table scope
5908                my $i;            my $i;
5909                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5910                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
5911                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
5912                    !!!cp ('t278');                !!!cp ('t278');
5913                    $i = $_;                $i = $_;
5914                    last INSCOPE;                last INSCOPE;
5915                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5916                            table => 1, html => 1,                !!!cp ('t279');
5917                           }->{$node->[1]}) {                last INSCOPE;
5918                    !!!cp ('t279');              }
5919                    last INSCOPE;            } # INSCOPE
5920                  }            unless (defined $i) {
5921                } # INSCOPE              !!!cp ('t280');
5922                unless (defined $i) {              !!!parse-error (type => 'unmatched end tag:select', token => $token);
5923                  !!!cp ('t280');              ## Ignore the token
5924                  !!!parse-error (type => 'unmatched end tag:select', token => $token);              !!!nack ('t280.1');
5925                  ## Ignore the token              !!!next-token;
5926                  !!!next-token;              next B;
5927                  redo B;            }
               }  
5928                                
5929                !!!cp ('t281');            !!!cp ('t281');
5930                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
5931    
5932                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5933    
5934            if ($token->{tag_name} eq 'select') {            if ($token->{tag_name} eq 'select') {
5935              !!!cp ('t281.2');              !!!nack ('t281.2');
5936              !!!next-token;              !!!next-token;
5937              redo B;              next B;
5938            } else {            } else {
5939              !!!cp ('t281.1');              !!!cp ('t281.1');
5940                !!!ack-later;
5941              ## Reprocess the token.              ## Reprocess the token.
5942              redo B;              next B;
5943            }            }
5944          } else {          } else {
5945            !!!cp ('t282');            !!!cp ('t282');
5946            !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);
5947            ## Ignore the token            ## Ignore the token
5948              !!!nack ('t282.1');
5949            !!!next-token;            !!!next-token;
5950            redo B;            next B;
5951          }          }
5952        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5953              if ($token->{tag_name} eq 'optgroup') {          if ($token->{tag_name} eq 'optgroup') {
5954                if ($self->{open_elements}->[-1]->[1] eq 'option' and            if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
5955                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
5956                  !!!cp ('t283');              !!!cp ('t283');
5957                  ## As if </option>              ## As if </option>
5958                  splice @{$self->{open_elements}}, -2;              splice @{$self->{open_elements}}, -2;
5959                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5960                  !!!cp ('t284');              !!!cp ('t284');
5961                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5962                } else {            } else {
5963                  !!!cp ('t285');              !!!cp ('t285');
5964                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5965                  ## Ignore the token              ## Ignore the token
5966                }            }
5967                !!!next-token;            !!!nack ('t285.1');
5968                redo B;            !!!next-token;
5969              } elsif ($token->{tag_name} eq 'option') {            next B;
5970                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'option') {
5971                  !!!cp ('t286');            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5972                  pop @{$self->{open_elements}};              !!!cp ('t286');
5973                } else {              pop @{$self->{open_elements}};
5974                  !!!cp ('t287');            } else {
5975                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!cp ('t287');
5976                  ## Ignore the token              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5977                }              ## Ignore the token
5978                !!!next-token;            }
5979                redo B;            !!!nack ('t287.1');
5980              } elsif ($token->{tag_name} eq 'select') {            !!!next-token;
5981                ## have an element in table scope            next B;
5982                my $i;          } elsif ($token->{tag_name} eq 'select') {
5983                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            ## have an element in table scope
5984                  my $node = $self->{open_elements}->[$_];            my $i;
5985                  if ($node->[1] eq $token->{tag_name}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5986                    !!!cp ('t288');              my $node = $self->{open_elements}->[$_];
5987                    $i = $_;              if ($node->[1] & SELECT_EL) {
5988                    last INSCOPE;                !!!cp ('t288');
5989                  } elsif ({                $i = $_;
5990                            table => 1, html => 1,                last INSCOPE;
5991                           }->{$node->[1]}) {              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5992                    !!!cp ('t289');                !!!cp ('t289');
5993                    last INSCOPE;                last INSCOPE;
5994                  }              }
5995                } # INSCOPE            } # INSCOPE
5996                unless (defined $i) {            unless (defined $i) {
5997                  !!!cp ('t290');              !!!cp ('t290');
5998                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5999                  ## Ignore the token              ## Ignore the token
6000                  !!!next-token;              !!!nack ('t290.1');
6001                  redo B;              !!!next-token;
6002                }              next B;
6003              }
6004                                
6005                !!!cp ('t291');            !!!cp ('t291');
6006                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6007    
6008                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6009    
6010                !!!next-token;            !!!nack ('t291.1');
6011                redo B;            !!!next-token;
6012              next B;
6013          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6014                   {                   {
6015                    caption => 1, table => 1, tbody => 1,                    caption => 1, table => 1, tbody => 1,
6016                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
6017                   }->{$token->{tag_name}}) {                   }->{$token->{tag_name}}) {
6018  ## TODO: The following is wrong?  ## TODO: The following is wrong?
6019                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6020                                
6021                ## have an element in table scope            ## have an element in table scope
6022                my $i;            my $i;
6023                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6024                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6025                  if ($node->[1] eq $token->{tag_name}) {              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6026                    !!!cp ('t292');                !!!cp ('t292');
6027                    $i = $_;                $i = $_;
6028                    last INSCOPE;                last INSCOPE;
6029                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6030                            table => 1, html => 1,                !!!cp ('t293');
6031                           }->{$node->[1]}) {                last INSCOPE;
6032                    !!!cp ('t293');              }
6033                    last INSCOPE;            } # INSCOPE
6034                  }            unless (defined $i) {
6035                } # INSCOPE              !!!cp ('t294');
6036                unless (defined $i) {              ## Ignore the token
6037                  !!!cp ('t294');              !!!nack ('t294.1');
6038                  ## Ignore the token              !!!next-token;
6039                  !!!next-token;              next B;
6040                  redo B;            }
               }  
6041                                
6042                ## As if </select>            ## As if </select>
6043                ## have an element in table scope            ## have an element in table scope
6044                undef $i;            undef $i;
6045                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6046                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6047                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
6048                    !!!cp ('t295');                !!!cp ('t295');
6049                    $i = $_;                $i = $_;
6050                    last INSCOPE;                last INSCOPE;
6051                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
6052  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
6053                    !!!cp ('t296');                !!!cp ('t296');
6054                    last INSCOPE;                last INSCOPE;
6055                  }              }
6056                } # INSCOPE            } # INSCOPE
6057                unless (defined $i) {            unless (defined $i) {
6058                  !!!cp ('t297');              !!!cp ('t297');
6059  ## TODO: The following error type is correct?  ## TODO: The following error type is correct?
6060                  !!!parse-error (type => 'unmatched end tag:select', token => $token);              !!!parse-error (type => 'unmatched end tag:select', token => $token);
6061                  ## Ignore the </select> token              ## Ignore the </select> token
6062                  !!!next-token; ## TODO: ok?              !!!nack ('t297.1');
6063                  redo B;              !!!next-token; ## TODO: ok?
6064                }              next B;
6065              }
6066                                
6067                !!!cp ('t298');            !!!cp ('t298');
6068                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6069    
6070                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6071    
6072                ## reprocess            !!!ack-later;
6073                redo B;            ## reprocess
6074              next B;
6075          } else {          } else {
6076            !!!cp ('t299');            !!!cp ('t299');
6077            !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);
6078            ## Ignore the token            ## Ignore the token
6079              !!!nack ('t299.3');
6080            !!!next-token;            !!!next-token;
6081            redo B;            next B;
6082          }          }
6083        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6084          unless ($self->{open_elements}->[-1]->[1] eq 'html' and          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6085                  @{$self->{open_elements}} == 1) { # redundant, maybe                  @{$self->{open_elements}} == 1) { # redundant, maybe
6086            !!!cp ('t299.1');            !!!cp ('t299.1');
6087            !!!parse-error (type => 'in body:#eof', token => $token);            !!!parse-error (type => 'in body:#eof', token => $token);
# Line 5319  sub _tree_construction_main ($) { Line 6106  sub _tree_construction_main ($) {
6106            unless (length $token->{data}) {            unless (length $token->{data}) {
6107              !!!cp ('t300');              !!!cp ('t300');
6108              !!!next-token;              !!!next-token;
6109              redo B;              next B;
6110            }            }
6111          }          }
6112                    
# Line 5337  sub _tree_construction_main ($) { Line 6124  sub _tree_construction_main ($) {
6124    
6125          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6126          ## reprocess          ## reprocess
6127          redo B;          next B;
6128        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6129          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6130            !!!cp ('t303');            !!!cp ('t303');
# Line 5352  sub _tree_construction_main ($) { Line 6139  sub _tree_construction_main ($) {
6139          !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);
6140    
6141          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6142            !!!ack-later;
6143          ## reprocess          ## reprocess
6144          redo B;          next B;
6145        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6146          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6147            !!!cp ('t305');            !!!cp ('t305');
# Line 5372  sub _tree_construction_main ($) { Line 6160  sub _tree_construction_main ($) {
6160              !!!parse-error (type => 'unmatched end tag:html', token => $token);              !!!parse-error (type => 'unmatched end tag:html', token => $token);
6161              ## Ignore the token              ## Ignore the token
6162              !!!next-token;              !!!next-token;
6163              redo B;              next B;
6164            } else {            } else {
6165              !!!cp ('t308');              !!!cp ('t308');
6166              $self->{insertion_mode} = AFTER_HTML_BODY_IM;              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6167              !!!next-token;              !!!next-token;
6168              redo B;              next B;
6169            }            }
6170          } else {          } else {
6171            !!!cp ('t309');            !!!cp ('t309');
# Line 5385  sub _tree_construction_main ($) { Line 6173  sub _tree_construction_main ($) {
6173    
6174            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
6175            ## reprocess            ## reprocess
6176            redo B;            next B;
6177          }          }
6178        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6179          !!!cp ('t309.2');          !!!cp ('t309.2');
# Line 5402  sub _tree_construction_main ($) { Line 6190  sub _tree_construction_main ($) {
6190            unless (length $token->{data}) {            unless (length $token->{data}) {
6191              !!!cp ('t310');              !!!cp ('t310');
6192              !!!next-token;              !!!next-token;
6193              redo B;              next B;
6194            }            }
6195          }          }
6196                    
# Line 5430  sub _tree_construction_main ($) { Line 6218  sub _tree_construction_main ($) {
6218              !!!cp ('t315');              !!!cp ('t315');
6219              !!!next-token;              !!!next-token;
6220            }            }
6221            redo B;            next B;
6222          }          }
6223                    
6224          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
# Line 5449  sub _tree_construction_main ($) { Line 6237  sub _tree_construction_main ($) {
6237              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6238            !!!cp ('t318');            !!!cp ('t318');
6239            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6240              !!!nack ('t318.1');
6241            !!!next-token;            !!!next-token;
6242            redo B;            next B;
6243          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
6244                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
6245            !!!cp ('t319');            !!!cp ('t319');
6246            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6247            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6248              !!!ack ('t319.1');
6249            !!!next-token;            !!!next-token;
6250            redo B;            next B;
6251          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
6252            !!!cp ('t320');            !!!cp ('t320');
6253            ## NOTE: As if in body.            ## NOTE: As if in head.
6254            $parse_rcdata->(CDATA_CONTENT_MODEL);            $parse_rcdata->(CDATA_CONTENT_MODEL);
6255            redo B;            next B;
6256          } else {          } else {
6257            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6258              !!!cp ('t321');              !!!cp ('t321');
# Line 5472  sub _tree_construction_main ($) { Line 6262  sub _tree_construction_main ($) {
6262              !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);
6263            }            }
6264            ## Ignore the token            ## Ignore the token
6265              !!!nack ('t322.1');
6266            !!!next-token;            !!!next-token;
6267            redo B;            next B;
6268          }          }
6269        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6270          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
# Line 5488  sub _tree_construction_main ($) { Line 6279  sub _tree_construction_main ($) {
6279    
6280          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6281              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6282            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6283                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
6284              !!!cp ('t325');              !!!cp ('t325');
6285              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
# Line 5501  sub _tree_construction_main ($) { Line 6292  sub _tree_construction_main ($) {
6292            }            }
6293    
6294            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
6295                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6296              !!!cp ('t327');              !!!cp ('t327');
6297              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6298            } else {            } else {
6299              !!!cp ('t328');              !!!cp ('t328');
6300            }            }
6301            redo B;            next B;
6302          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
6303                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6304            !!!cp ('t329');            !!!cp ('t329');
6305            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6306            !!!next-token;            !!!next-token;
6307            redo B;            next B;
6308          } else {          } else {
6309            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6310              !!!cp ('t330');              !!!cp ('t330');
# Line 5524  sub _tree_construction_main ($) { Line 6315  sub _tree_construction_main ($) {
6315            }            }
6316            ## Ignore the token            ## Ignore the token
6317            !!!next-token;            !!!next-token;
6318            redo B;            next B;
6319          }          }
6320        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6321          unless ($self->{open_elements}->[-1]->[1] eq 'html' and          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6322                  @{$self->{open_elements}} == 1) { # redundant, maybe                  @{$self->{open_elements}} == 1) { # redundant, maybe
6323            !!!cp ('t331.1');            !!!cp ('t331.1');
6324            !!!parse-error (type => 'in body:#eof', token => $token);            !!!parse-error (type => 'in body:#eof', token => $token);
# Line 5552  sub _tree_construction_main ($) { Line 6343  sub _tree_construction_main ($) {
6343          !!!cp ('t332');          !!!cp ('t332');
6344          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6345          $script_start_tag->();          $script_start_tag->();
6346          redo B;          next B;
6347        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
6348          !!!cp ('t333');          !!!cp ('t333');
6349          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6350          $parse_rcdata->(CDATA_CONTENT_MODEL);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6351          redo B;          next B;
6352        } elsif ({        } elsif ({
6353                  base => 1, link => 1,                  base => 1, link => 1,
6354                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 5565  sub _tree_construction_main ($) { Line 6356  sub _tree_construction_main ($) {
6356          ## 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
6357          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6358          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6359            !!!ack ('t334.1');
6360          !!!next-token;          !!!next-token;
6361          redo B;          next B;
6362        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
6363          ## 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
6364          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6365          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.
6366    
6367          unless ($self->{confident}) {          unless ($self->{confident}) {
6368            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) {
6369              !!!cp ('t335');              !!!cp ('t335');
6370                ## NOTE: Whether the encoding is supported or not is handled
6371                ## in the {change_encoding} callback.
6372              $self->{change_encoding}              $self->{change_encoding}
6373                  ->($self, $token->{attributes}->{charset}->{value}, $token);                  ->($self, $token->{attributes}->{charset}->{value}, $token);
6374                            
# Line 5583  sub _tree_construction_main ($) { Line 6377  sub _tree_construction_main ($) {
6377                                       $token->{attributes}->{charset}                                       $token->{attributes}->{charset}
6378                                           ->{has_reference});                                           ->{has_reference});
6379            } elsif ($token->{attributes}->{content}) {            } elsif ($token->{attributes}->{content}) {
             ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
6380              if ($token->{attributes}->{content}->{value}              if ($token->{attributes}->{content}->{value}
6381                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                  =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6382                      [\x09-\x0D\x20]*=                      [\x09-\x0D\x20]*=
6383                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6384                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
6385                !!!cp ('t336');                !!!cp ('t336');
6386                  ## NOTE: Whether the encoding is supported or not is handled
6387                  ## in the {change_encoding} callback.
6388                $self->{change_encoding}                $self->{change_encoding}
6389                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6390                $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 6410  sub _tree_construction_main ($) {
6410            }            }
6411          }          }
6412    
6413            !!!ack ('t338.1');
6414          !!!next-token;          !!!next-token;
6415          redo B;          next B;
6416        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
6417          !!!cp ('t341');          !!!cp ('t341');
6418          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6419          $parse_rcdata->(RCDATA_CONTENT_MODEL);          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6420          redo B;          next B;
6421        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
6422          !!!parse-error (type => 'in body:body', token => $token);          !!!parse-error (type => 'in body:body', token => $token);
6423                                
6424          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
6425              $self->{open_elements}->[1]->[1] ne 'body') {              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6426            !!!cp ('t342');            !!!cp ('t342');
6427            ## Ignore the token            ## Ignore the token
6428          } else {          } else {
# Line 5640  sub _tree_construction_main ($) { Line 6436  sub _tree_construction_main ($) {
6436              }              }
6437            }            }
6438          }          }
6439            !!!nack ('t343.1');
6440          !!!next-token;          !!!next-token;
6441          redo B;          next B;
6442        } elsif ({        } elsif ({
6443                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6444                  div => 1, dl => 1, fieldset => 1,                  div => 1, dl => 1, fieldset => 1,
# Line 5656  sub _tree_construction_main ($) { Line 6453  sub _tree_construction_main ($) {
6453            !!!cp ('t350');            !!!cp ('t350');
6454            !!!parse-error (type => 'in form:form', token => $token);            !!!parse-error (type => 'in form:form', token => $token);
6455            ## Ignore the token            ## Ignore the token
6456              !!!nack ('t350.1');
6457            !!!next-token;            !!!next-token;
6458            redo B;            next B;
6459          }          }
6460    
6461          ## has a p element in scope          ## has a p element in scope
6462          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6463            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6464              !!!cp ('t344');              !!!cp ('t344');
6465              !!!back-token;              !!!back-token; # <form>
6466              $token = {type => END_TAG_TOKEN, tag_name => 'p',              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6467                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6468              redo B;              next B;
6469            } elsif ({            } elsif ($_->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6470              !!!cp ('t345');              !!!cp ('t345');
6471              last INSCOPE;              last INSCOPE;
6472            }            }
# Line 5679  sub _tree_construction_main ($) { Line 6474  sub _tree_construction_main ($) {
6474                        
6475          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6476          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6477              !!!nack ('t346.1');
6478            !!!next-token;            !!!next-token;
6479            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
6480              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
# Line 5695  sub _tree_construction_main ($) { Line 6491  sub _tree_construction_main ($) {
6491            !!!cp ('t347.1');            !!!cp ('t347.1');
6492            $self->{form_element} = $self->{open_elements}->[-1]->[0];            $self->{form_element} = $self->{open_elements}->[-1]->[0];
6493    
6494              !!!nack ('t347.2');
6495            !!!next-token;            !!!next-token;
6496          } elsif ($token->{tag_name} eq 'table') {          } elsif ($token->{tag_name} eq 'table') {
6497            !!!cp ('t382');            !!!cp ('t382');
# Line 5702  sub _tree_construction_main ($) { Line 6499  sub _tree_construction_main ($) {
6499                        
6500            $self->{insertion_mode} = IN_TABLE_IM;            $self->{insertion_mode} = IN_TABLE_IM;
6501    
6502              !!!nack ('t382.1');
6503            !!!next-token;            !!!next-token;
6504          } elsif ($token->{tag_name} eq 'hr') {          } elsif ($token->{tag_name} eq 'hr') {
6505            !!!cp ('t386');            !!!cp ('t386');
6506            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6507                    
6508              !!!nack ('t386.1');
6509            !!!next-token;            !!!next-token;
6510          } else {          } else {
6511            !!!cp ('t347');            !!!nack ('t347.1');
6512            !!!next-token;            !!!next-token;
6513          }          }
6514          redo B;          next B;
6515        } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {        } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
6516          ## has a p element in scope          ## has a p element in scope
6517          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6518            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6519              !!!cp ('t353');              !!!cp ('t353');
6520              !!!back-token;              !!!back-token; # <x>
6521              $token = {type => END_TAG_TOKEN, tag_name => 'p',              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6522                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6523              redo B;              next B;
6524            } elsif ({            } elsif ($_->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6525              !!!cp ('t354');              !!!cp ('t354');
6526              last INSCOPE;              last INSCOPE;
6527            }            }
# Line 5739  sub _tree_construction_main ($) { Line 6535  sub _tree_construction_main ($) {
6535                            dd => {dt => 1, dd => 1}}->{$token->{tag_name}};                            dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6536          LI: {          LI: {
6537            ## Step 2            ## Step 2
6538            if ($li_or_dtdd->{$node->[1]}) {            if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6539              if ($i != -1) {              if ($i != -1) {
6540                !!!cp ('t355');                !!!cp ('t355');
6541                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'not closed',
6542                                $self->{open_elements}->[-1]->[1], token => $token);                                value => $self->{open_elements}->[-1]->[0]
6543                                      ->manakai_local_name,
6544                                  token => $token);
6545              } else {              } else {
6546                !!!cp ('t356');                !!!cp ('t356');
6547              }              }
# Line 5754  sub _tree_construction_main ($) { Line 6552  sub _tree_construction_main ($) {
6552            }            }
6553                        
6554            ## Step 3            ## Step 3
6555            if (not $formatting_category->{$node->[1]} and            if (not ($node->[1] & FORMATTING_EL) and
6556                #not $phrasing_category->{$node->[1]} and                #not $phrasing_category->{$node->[1]} and
6557                ($special_category->{$node->[1]} or                ($node->[1] & SPECIAL_EL or
6558                 $scoping_category->{$node->[1]}) and                 $node->[1] & SCOPING_EL) and
6559                $node->[1] ne 'address' and $node->[1] ne 'div') {                not ($node->[1] & ADDRESS_EL) and
6560                  not ($node->[1] & DIV_EL)) {
6561              !!!cp ('t358');              !!!cp ('t358');
6562              last LI;              last LI;
6563            }            }
# Line 5771  sub _tree_construction_main ($) { Line 6570  sub _tree_construction_main ($) {
6570          } # LI          } # LI
6571                        
6572          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6573            !!!nack ('t359.1');
6574          !!!next-token;          !!!next-token;
6575          redo B;          next B;
6576        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
6577          ## has a p element in scope          ## has a p element in scope
6578          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6579            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6580              !!!cp ('t367');              !!!cp ('t367');
6581              !!!back-token;              !!!back-token; # <plaintext>
6582              $token = {type => END_TAG_TOKEN, tag_name => 'p',              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6583                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6584              redo B;              next B;
6585            } elsif ({            } elsif ($_->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6586              !!!cp ('t368');              !!!cp ('t368');
6587              last INSCOPE;              last INSCOPE;
6588            }            }
# Line 5795  sub _tree_construction_main ($) { Line 6592  sub _tree_construction_main ($) {
6592                        
6593          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
6594                        
6595            !!!nack ('t368.1');
6596          !!!next-token;          !!!next-token;
6597          redo B;          next B;
6598        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
6599          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
6600            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
6601            if ($node->[1] eq 'a') {            if ($node->[1] & A_EL) {
6602              !!!cp ('t371');              !!!cp ('t371');
6603              !!!parse-error (type => 'in a:a', token => $token);              !!!parse-error (type => 'in a:a', token => $token);
6604                            
6605              !!!back-token;              !!!back-token; # <a>
6606              $token = {type => END_TAG_TOKEN, tag_name => 'a',              $token = {type => END_TAG_TOKEN, tag_name => 'a',
6607                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6608              $formatting_end_tag->($token);              $formatting_end_tag->($token);
# Line 5835  sub _tree_construction_main ($) { Line 6633  sub _tree_construction_main ($) {
6633          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6634          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6635    
6636            !!!nack ('t374.1');
6637          !!!next-token;          !!!next-token;
6638          redo B;          next B;
6639        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
6640          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6641    
6642          ## has a |nobr| element in scope          ## has a |nobr| element in scope
6643          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6644            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6645            if ($node->[1] eq 'nobr') {            if ($node->[1] & NOBR_EL) {
6646              !!!cp ('t376');              !!!cp ('t376');
6647              !!!parse-error (type => 'in nobr:nobr', token => $token);              !!!parse-error (type => 'in nobr:nobr', token => $token);
6648              !!!back-token;              !!!back-token; # <nobr>
6649              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
6650                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6651              redo B;              next B;
6652            } 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]}) {  
6653              !!!cp ('t377');              !!!cp ('t377');
6654              last INSCOPE;              last INSCOPE;
6655            }            }
# Line 5862  sub _tree_construction_main ($) { Line 6658  sub _tree_construction_main ($) {
6658          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6659          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6660                    
6661            !!!nack ('t377.1');
6662          !!!next-token;          !!!next-token;
6663          redo B;          next B;
6664        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
6665          ## has a button element in scope          ## has a button element in scope
6666          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6667            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6668            if ($node->[1] eq 'button') {            if ($node->[1] & BUTTON_EL) {
6669              !!!cp ('t378');              !!!cp ('t378');
6670              !!!parse-error (type => 'in button:button', token => $token);              !!!parse-error (type => 'in button:button', token => $token);
6671              !!!back-token;              !!!back-token; # <button>
6672              $token = {type => END_TAG_TOKEN, tag_name => 'button',              $token = {type => END_TAG_TOKEN, tag_name => 'button',
6673                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6674              redo B;              next B;
6675            } 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]}) {  
6676              !!!cp ('t379');              !!!cp ('t379');
6677              last INSCOPE;              last INSCOPE;
6678            }            }
# Line 5892  sub _tree_construction_main ($) { Line 6686  sub _tree_construction_main ($) {
6686    
6687          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
6688    
6689            !!!nack ('t379.1');
6690          !!!next-token;          !!!next-token;
6691          redo B;          next B;
6692        } elsif ({        } elsif ({
6693                  xmp => 1,                  xmp => 1,
6694                  iframe => 1,                  iframe => 1,
6695                  noembed => 1,                  noembed => 1,
6696                  noframes => 1,                  noframes => 1, ## NOTE: This is an "as if in head" code clone.
6697                  noscript => 0, ## TODO: 1 if scripting is enabled                  noscript => 0, ## TODO: 1 if scripting is enabled
6698                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6699          if ($token->{tag_name} eq 'xmp') {          if ($token->{tag_name} eq 'xmp') {
# Line 5909  sub _tree_construction_main ($) { Line 6704  sub _tree_construction_main ($) {
6704          }          }
6705          ## NOTE: There is an "as if in body" code clone.          ## NOTE: There is an "as if in body" code clone.
6706          $parse_rcdata->(CDATA_CONTENT_MODEL);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6707          redo B;          next B;
6708        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
6709          !!!parse-error (type => 'isindex', token => $token);          !!!parse-error (type => 'isindex', token => $token);
6710                    
6711          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
6712            !!!cp ('t389');            !!!cp ('t389');
6713            ## Ignore the token            ## Ignore the token
6714              !!!nack ('t389'); ## NOTE: Not acknowledged.
6715            !!!next-token;            !!!next-token;
6716            redo B;            next B;
6717          } else {          } else {
6718              !!!ack ('t391.1');
6719    
6720            my $at = $token->{attributes};            my $at = $token->{attributes};
6721            my $form_attrs;            my $form_attrs;
6722            $form_attrs->{action} = $at->{action} if $at->{action};            $form_attrs->{action} = $at->{action} if $at->{action};
# Line 5962  sub _tree_construction_main ($) { Line 6760  sub _tree_construction_main ($) {
6760                           line => $token->{line}, column => $token->{column}},                           line => $token->{line}, column => $token->{column}},
6761                          {type => END_TAG_TOKEN, tag_name => 'form',                          {type => END_TAG_TOKEN, tag_name => 'form',
6762                           line => $token->{line}, column => $token->{column}};                           line => $token->{line}, column => $token->{column}};
           $token = shift @tokens;  
6763            !!!back-token (@tokens);            !!!back-token (@tokens);
6764            redo B;            !!!next-token;
6765              next B;
6766          }          }
6767        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
6768          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
6769          my $el;          my $el;
6770          !!!create-element ($el, $token->{tag_name}, $token->{attributes}, $token);          !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
6771                    
6772          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
6773          $self->{content_model} = RCDATA_CONTENT_MODEL;          $self->{content_model} = RCDATA_CONTENT_MODEL;
# Line 5978  sub _tree_construction_main ($) { Line 6776  sub _tree_construction_main ($) {
6776          $insert->($el);          $insert->($el);
6777                    
6778          my $text = '';          my $text = '';
6779            !!!nack ('t392.1');
6780          !!!next-token;          !!!next-token;
6781          if ($token->{type} == CHARACTER_TOKEN) {          if ($token->{type} == CHARACTER_TOKEN) {
6782            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
# Line 6011  sub _tree_construction_main ($) { Line 6810  sub _tree_construction_main ($) {
6810            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
6811          }          }
6812          !!!next-token;          !!!next-token;
6813            next B;
6814          } elsif ($token->{tag_name} eq 'rt' or
6815                   $token->{tag_name} eq 'rp') {
6816            ## has a |ruby| element in scope
6817            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6818              my $node = $self->{open_elements}->[$_];
6819              if ($node->[1] & RUBY_EL) {
6820                !!!cp ('t398.1');
6821                ## generate implied end tags
6822                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
6823                  !!!cp ('t398.2');
6824                  pop @{$self->{open_elements}};
6825                }
6826                unless ($self->{open_elements}->[-1]->[1] & RUBY_EL) {
6827                  !!!cp ('t398.3');
6828                  !!!parse-error (type => 'not closed',
6829                                  value => $self->{open_elements}->[-1]->[0]
6830                                      ->manakai_local_name,
6831                                  token => $token);
6832                  pop @{$self->{open_elements}}
6833                      while not $self->{open_elements}->[-1]->[1] & RUBY_EL;
6834                }
6835                last INSCOPE;
6836              } elsif ($node->[1] & SCOPING_EL) {
6837                !!!cp ('t398.4');
6838                last INSCOPE;
6839              }
6840            } # INSCOPE
6841    
6842            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6843    
6844            !!!nack ('t398.5');
6845            !!!next-token;
6846          redo B;          redo B;
6847          } elsif ($token->{tag_name} eq 'math' or
6848                   $token->{tag_name} eq 'svg') {
6849            $reconstruct_active_formatting_elements->($insert_to_current);
6850    
6851            ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
6852    
6853            ## "adjust foreign attributes" - done in insert-element-f
6854            
6855            !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
6856            
6857            if ($self->{self_closing}) {
6858              pop @{$self->{open_elements}};
6859              !!!ack ('t398.1');
6860            } else {
6861              !!!cp ('t398.2');
6862              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
6863              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
6864              ## mode, "in body" (not "in foreign content") secondary insertion
6865              ## mode, maybe.
6866            }
6867    
6868            !!!next-token;
6869            next B;
6870        } elsif ({        } elsif ({
6871                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
6872                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 6021  sub _tree_construction_main ($) { Line 6876  sub _tree_construction_main ($) {
6876          !!!cp ('t401');          !!!cp ('t401');
6877          !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);
6878          ## Ignore the token          ## Ignore the token
6879            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
6880          !!!next-token;          !!!next-token;
6881          redo B;          next B;
6882                    
6883          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
6884        } else {        } else {
# Line 6044  sub _tree_construction_main ($) { Line 6900  sub _tree_construction_main ($) {
6900              }->{$token->{tag_name}}) {              }->{$token->{tag_name}}) {
6901            !!!cp ('t380');            !!!cp ('t380');
6902            push @$active_formatting_elements, ['#marker', ''];            push @$active_formatting_elements, ['#marker', ''];
6903              !!!nack ('t380.1');
6904          } elsif ({          } elsif ({
6905                    b => 1, big => 1, em => 1, font => 1, i => 1,                    b => 1, big => 1, em => 1, font => 1, i => 1,
6906                    s => 1, small => 1, strile => 1,                    s => 1, small => 1, strile => 1,
# Line 6051  sub _tree_construction_main ($) { Line 6908  sub _tree_construction_main ($) {
6908                   }->{$token->{tag_name}}) {                   }->{$token->{tag_name}}) {
6909            !!!cp ('t375');            !!!cp ('t375');
6910            push @$active_formatting_elements, $self->{open_elements}->[-1];            push @$active_formatting_elements, $self->{open_elements}->[-1];
6911              !!!nack ('t375.1');
6912          } elsif ($token->{tag_name} eq 'input') {          } elsif ($token->{tag_name} eq 'input') {
6913            !!!cp ('t388');            !!!cp ('t388');
6914            ## TODO: associate with $self->{form_element} if defined            ## TODO: associate with $self->{form_element} if defined
6915            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6916              !!!ack ('t388.2');
6917          } elsif ({          } elsif ({
6918                    area => 1, basefont => 1, bgsound => 1, br => 1,                    area => 1, basefont => 1, bgsound => 1, br => 1,
6919                    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 6921  sub _tree_construction_main ($) {
6921                   }->{$token->{tag_name}}) {                   }->{$token->{tag_name}}) {
6922            !!!cp ('t388.1');            !!!cp ('t388.1');
6923            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6924              !!!ack ('t388.3');
6925          } elsif ($token->{tag_name} eq 'select') {          } elsif ($token->{tag_name} eq 'select') {
6926            ## TODO: associate with $self->{form_element} if defined            ## TODO: associate with $self->{form_element} if defined
6927                    
# Line 6074  sub _tree_construction_main ($) { Line 6934  sub _tree_construction_main ($) {
6934              !!!cp ('t400.2');              !!!cp ('t400.2');
6935              $self->{insertion_mode} = IN_SELECT_IM;              $self->{insertion_mode} = IN_SELECT_IM;
6936            }            }
6937              !!!nack ('t400.3');
6938          } else {          } else {
6939            !!!cp ('t402');            !!!nack ('t402');
6940          }          }
6941                    
6942          !!!next-token;          !!!next-token;
6943          redo B;          next B;
6944        }        }
6945      } elsif ($token->{type} == END_TAG_TOKEN) {      } elsif ($token->{type} == END_TAG_TOKEN) {
6946        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
# Line 6087  sub _tree_construction_main ($) { Line 6948  sub _tree_construction_main ($) {
6948          my $i;          my $i;
6949          INSCOPE: {          INSCOPE: {
6950            for (reverse @{$self->{open_elements}}) {            for (reverse @{$self->{open_elements}}) {
6951              if ($_->[1] eq 'body') {              if ($_->[1] & BODY_EL) {
6952                !!!cp ('t405');                !!!cp ('t405');
6953                $i = $_;                $i = $_;
6954                last INSCOPE;                last INSCOPE;
6955              } elsif ({              } elsif ($_->[1] & SCOPING_EL) {
                       applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                       button => 1, marquee => 1, object => 1, html => 1,  
                      }->{$_->[1]}) {  
6956                !!!cp ('t405.1');                !!!cp ('t405.1');
6957                last;                last;
6958              }              }
# Line 6104  sub _tree_construction_main ($) { Line 6962  sub _tree_construction_main ($) {
6962                            value => $token->{tag_name}, token => $token);                            value => $token->{tag_name}, token => $token);
6963            ## NOTE: Ignore the token.            ## NOTE: Ignore the token.
6964            !!!next-token;            !!!next-token;
6965            redo B;            next B;
6966          } # INSCOPE          } # INSCOPE
6967    
6968          for (@{$self->{open_elements}}) {          for (@{$self->{open_elements}}) {
6969            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]}) {  
6970              !!!cp ('t403');              !!!cp ('t403');
6971              !!!parse-error (type => 'not closed:'.$_->[1], token => $token);              !!!parse-error (type => 'not closed',
6972                                value => $_->[0]->manakai_local_name,
6973                                token => $token);
6974              last;              last;
6975            } else {            } else {
6976              !!!cp ('t404');              !!!cp ('t404');
# Line 6123  sub _tree_construction_main ($) { Line 6979  sub _tree_construction_main ($) {
6979    
6980          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
6981          !!!next-token;          !!!next-token;
6982          redo B;          next B;
6983        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
6984          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
6985            ## up-to-date, though it has same effect as speced.
6986            if (@{$self->{open_elements}} > 1 and
6987                $self->{open_elements}->[1]->[1] & BODY_EL) {
6988            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
6989            if ($self->{open_elements}->[-1]->[1] ne 'body') {            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
6990              !!!cp ('t406');              !!!cp ('t406');
6991              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1], token => $token);              !!!parse-error (type => 'not closed',
6992                                value => $self->{open_elements}->[1]->[0]
6993                                    ->manakai_local_name,
6994                                token => $token);
6995            } else {            } else {
6996              !!!cp ('t407');              !!!cp ('t407');
6997            }            }
6998            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6999            ## reprocess            ## reprocess
7000            redo B;            next B;
7001          } else {          } else {
7002            !!!cp ('t408');            !!!cp ('t408');
7003            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7004            ## Ignore the token            ## Ignore the token
7005            !!!next-token;            !!!next-token;
7006            redo B;            next B;
7007          }          }
7008        } elsif ({        } elsif ({
7009                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
# Line 6154  sub _tree_construction_main ($) { Line 7016  sub _tree_construction_main ($) {
7016          my $i;          my $i;
7017          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7018            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7019            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7020              !!!cp ('t410');              !!!cp ('t410');
7021              $i = $_;              $i = $_;
7022              last INSCOPE;              last INSCOPE;
7023            } 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]}) {  
7024              !!!cp ('t411');              !!!cp ('t411');
7025              last INSCOPE;              last INSCOPE;
7026            }            }
# Line 6173  sub _tree_construction_main ($) { Line 7032  sub _tree_construction_main ($) {
7032          } else {          } else {
7033            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7034            while ({            while ({
7035                      ## END_TAG_OPTIONAL_EL
7036                    dd => ($token->{tag_name} ne 'dd'),                    dd => ($token->{tag_name} ne 'dd'),
7037                    dt => ($token->{tag_name} ne 'dt'),                    dt => ($token->{tag_name} ne 'dt'),
7038                    li => ($token->{tag_name} ne 'li'),                    li => ($token->{tag_name} ne 'li'),
7039                    p => 1,                    p => 1,
7040                   }->{$self->{open_elements}->[-1]->[1]}) {                    rt => 1,
7041                      rp => 1,
7042                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
7043              !!!cp ('t409');              !!!cp ('t409');
7044              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
7045            }            }
7046    
7047            ## Step 2.            ## Step 2.
7048            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7049                      ne $token->{tag_name}) {
7050              !!!cp ('t412');              !!!cp ('t412');
7051              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);              !!!parse-error (type => 'not closed',
7052                                value => $self->{open_elements}->[-1]->[0]
7053                                    ->manakai_local_name,
7054                                token => $token);
7055            } else {            } else {
7056              !!!cp ('t414');              !!!cp ('t414');
7057            }            }
# Line 6200  sub _tree_construction_main ($) { Line 7066  sub _tree_construction_main ($) {
7066                }->{$token->{tag_name}};                }->{$token->{tag_name}};
7067          }          }
7068          !!!next-token;          !!!next-token;
7069          redo B;          next B;
7070        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
7071          undef $self->{form_element};          undef $self->{form_element};
7072    
# Line 6208  sub _tree_construction_main ($) { Line 7074  sub _tree_construction_main ($) {
7074          my $i;          my $i;
7075          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7076            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7077            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & FORM_EL) {
7078              !!!cp ('t418');              !!!cp ('t418');
7079              $i = $_;              $i = $_;
7080              last INSCOPE;              last INSCOPE;
7081            } 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]}) {  
7082              !!!cp ('t419');              !!!cp ('t419');
7083              last INSCOPE;              last INSCOPE;
7084            }            }
# Line 6226  sub _tree_construction_main ($) { Line 7089  sub _tree_construction_main ($) {
7089            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7090          } else {          } else {
7091            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7092            while ({            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                   dd => 1, dt => 1, li => 1, p => 1,  
                  }->{$self->{open_elements}->[-1]->[1]}) {  
7093              !!!cp ('t417');              !!!cp ('t417');
7094              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
7095            }            }
7096                        
7097            ## Step 2.            ## Step 2.
7098            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7099                      ne $token->{tag_name}) {
7100              !!!cp ('t417.1');              !!!cp ('t417.1');
7101              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);              !!!parse-error (type => 'not closed',
7102                                value => $self->{open_elements}->[-1]->[0]
7103                                    ->manakai_local_name,
7104                                token => $token);
7105            } else {            } else {
7106              !!!cp ('t420');              !!!cp ('t420');
7107            }              }  
# Line 6246  sub _tree_construction_main ($) { Line 7111  sub _tree_construction_main ($) {
7111          }          }
7112    
7113          !!!next-token;          !!!next-token;
7114          redo B;          next B;
7115        } elsif ({        } elsif ({
7116                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
7117                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 6254  sub _tree_construction_main ($) { Line 7119  sub _tree_construction_main ($) {
7119          my $i;          my $i;
7120          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7121            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7122            if ({            if ($node->[1] & HEADING_EL) {
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
7123              !!!cp ('t423');              !!!cp ('t423');
7124              $i = $_;              $i = $_;
7125              last INSCOPE;              last INSCOPE;
7126            } 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]}) {  
7127              !!!cp ('t424');              !!!cp ('t424');
7128              last INSCOPE;              last INSCOPE;
7129            }            }
# Line 6274  sub _tree_construction_main ($) { Line 7134  sub _tree_construction_main ($) {
7134            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7135          } else {          } else {
7136            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7137            while ({            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                   dd => 1, dt => 1, li => 1, p => 1,  
                  }->{$self->{open_elements}->[-1]->[1]}) {  
7138              !!!cp ('t422');              !!!cp ('t422');
7139              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
7140            }            }
7141                        
7142            ## Step 2.            ## Step 2.
7143            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7144                      ne $token->{tag_name}) {
7145              !!!cp ('t425');              !!!cp ('t425');
7146              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7147            } else {            } else {
# Line 6294  sub _tree_construction_main ($) { Line 7153  sub _tree_construction_main ($) {
7153          }          }
7154                    
7155          !!!next-token;          !!!next-token;
7156          redo B;          next B;
7157        } elsif ($token->{tag_name} eq 'p') {        } elsif ($token->{tag_name} eq 'p') {
7158          ## has an element in scope          ## has an element in scope
7159          my $i;          my $i;
7160          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7161            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7162            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & P_EL) {
7163              !!!cp ('t410.1');              !!!cp ('t410.1');
7164              $i = $_;              $i = $_;
7165              last INSCOPE;              last INSCOPE;
7166            } 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]}) {  
7167              !!!cp ('t411.1');              !!!cp ('t411.1');
7168              last INSCOPE;              last INSCOPE;
7169            }            }
7170          } # INSCOPE          } # INSCOPE
7171    
7172          if (defined $i) {          if (defined $i) {
7173            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7174                      ne $token->{tag_name}) {
7175              !!!cp ('t412.1');              !!!cp ('t412.1');
7176              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);              !!!parse-error (type => 'not closed',
7177                                value => $self->{open_elements}->[-1]->[0]
7178                                    ->manakai_local_name,
7179                                token => $token);
7180            } else {            } else {
7181              !!!cp ('t414.1');              !!!cp ('t414.1');
7182            }            }
# Line 6329  sub _tree_construction_main ($) { Line 7189  sub _tree_construction_main ($) {
7189            !!!cp ('t415.1');            !!!cp ('t415.1');
7190            ## As if <p>, then reprocess the current token            ## As if <p>, then reprocess the current token
7191            my $el;            my $el;
7192            !!!create-element ($el, 'p',, $token);            !!!create-element ($el, $HTML_NS, 'p',, $token);
7193            $insert->($el);            $insert->($el);
7194            ## NOTE: Not inserted into |$self->{open_elements}|.            ## NOTE: Not inserted into |$self->{open_elements}|.
7195          }          }
7196    
7197          !!!next-token;          !!!next-token;
7198          redo B;          next B;
7199        } elsif ({        } elsif ({
7200                  a => 1,                  a => 1,
7201                  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 7204  sub _tree_construction_main ($) {
7204                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7205          !!!cp ('t427');          !!!cp ('t427');
7206          $formatting_end_tag->($token);          $formatting_end_tag->($token);
7207          redo B;          next B;
7208        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
7209          !!!cp ('t428');          !!!cp ('t428');
7210          !!!parse-error (type => 'unmatched end tag:br', token => $token);          !!!parse-error (type => 'unmatched end tag:br', token => $token);
# Line 6353  sub _tree_construction_main ($) { Line 7213  sub _tree_construction_main ($) {
7213          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7214                    
7215          my $el;          my $el;
7216          !!!create-element ($el, 'br',, $token);          !!!create-element ($el, $HTML_NS, 'br',, $token);
7217          $insert->($el);          $insert->($el);
7218                    
7219          ## Ignore the token.          ## Ignore the token.
7220          !!!next-token;          !!!next-token;
7221          redo B;          next B;
7222        } elsif ({        } elsif ({
7223                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7224                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 6375  sub _tree_construction_main ($) { Line 7235  sub _tree_construction_main ($) {
7235          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7236          ## Ignore the token          ## Ignore the token
7237          !!!next-token;          !!!next-token;
7238          redo B;          next B;
7239                    
7240          ## ISSUE: Issue on HTML5 new elements in spec          ## ISSUE: Issue on HTML5 new elements in spec
7241                    
# Line 6386  sub _tree_construction_main ($) { Line 7246  sub _tree_construction_main ($) {
7246    
7247          ## Step 2          ## Step 2
7248          S2: {          S2: {
7249            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7250              ## Step 1              ## Step 1
7251              ## generate implied end tags              ## generate implied end tags
7252              while ({              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                     dd => 1, dt => 1, li => 1, p => 1,  
                    }->{$self->{open_elements}->[-1]->[1]}) {  
7253                !!!cp ('t430');                !!!cp ('t430');
7254                ## ISSUE: Can this case be reached?                ## NOTE: |<ruby><rt></ruby>|.
7255                  ## ISSUE: <ruby><rt></rt> will also take this code path,
7256                  ## which seems wrong.
7257                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
7258                  $node_i++;
7259              }              }
7260                    
7261              ## Step 2              ## Step 2
7262              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7263                        ne $token->{tag_name}) {
7264                !!!cp ('t431');                !!!cp ('t431');
7265                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
7266                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                !!!parse-error (type => 'not closed',
7267                                  value => $self->{open_elements}->[-1]->[0]
7268                                      ->manakai_local_name,
7269                                  token => $token);
7270              } else {              } else {
7271                !!!cp ('t432');                !!!cp ('t432');
7272              }              }
7273                            
7274              ## Step 3              ## Step 3
7275              splice @{$self->{open_elements}}, $node_i;              splice @{$self->{open_elements}}, $node_i if $node_i < 0;
7276    
7277              !!!next-token;              !!!next-token;
7278              last S2;              last S2;
7279            } else {            } else {
7280              ## Step 3              ## Step 3
7281              if (not $formatting_category->{$node->[1]} and              if (not ($node->[1] & FORMATTING_EL) and
7282                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
7283                  ($special_category->{$node->[1]} or                  ($node->[1] & SPECIAL_EL or
7284                   $scoping_category->{$node->[1]})) {                   $node->[1] & SCOPING_EL)) {
7285                !!!cp ('t433');                !!!cp ('t433');
7286                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7287                ## Ignore the token                ## Ignore the token
# Line 6434  sub _tree_construction_main ($) { Line 7299  sub _tree_construction_main ($) {
7299            ## Step 5;            ## Step 5;
7300            redo S2;            redo S2;
7301          } # S2          } # S2
7302          redo B;          next B;
7303        }        }
7304      }      }
7305      redo B;      next B;
7306      } continue { # B
7307        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7308          ## NOTE: The code below is executed in cases where it does not have
7309          ## to be, but it it is harmless even in those cases.
7310          ## has an element in scope
7311          INSCOPE: {
7312            for (reverse 0..$#{$self->{open_elements}}) {
7313              my $node = $self->{open_elements}->[$_];
7314              if ($node->[1] & FOREIGN_EL) {
7315                last INSCOPE;
7316              } elsif ($node->[1] & SCOPING_EL) {
7317                last;
7318              }
7319            }
7320            
7321            ## NOTE: No foreign element in scope.
7322            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7323          } # INSCOPE
7324        }
7325    } # B    } # B
7326    
7327    ## Stop parsing # MUST    ## Stop parsing # MUST
# Line 6514  sub set_inner_html ($$$) { Line 7398  sub set_inner_html ($$$) {
7398          !!!cp ('i4');          !!!cp ('i4');
7399          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
7400          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7401          } elsif ($self->{next_char} <= 0x0008 or
7402                   (0x000E <= $self->{next_char} and
7403                    $self->{next_char} <= 0x001F) or
7404                   (0x007F <= $self->{next_char} and
7405                    $self->{next_char} <= 0x009F) or
7406                   (0xD800 <= $self->{next_char} and
7407                    $self->{next_char} <= 0xDFFF) or
7408                   (0xFDD0 <= $self->{next_char} and
7409                    $self->{next_char} <= 0xFDDF) or
7410                   {
7411                    0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
7412                    0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
7413                    0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
7414                    0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
7415                    0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
7416                    0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
7417                    0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
7418                    0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
7419                    0x10FFFE => 1, 0x10FFFF => 1,
7420                   }->{$self->{next_char}}) {
7421            !!!cp ('i4.1');
7422            !!!parse-error (type => 'control char', level => $self->{must_level});
7423    ## TODO: error type documentation
7424        }        }
7425      };      };
7426      $p->{prev_char} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];
# Line 6554  sub set_inner_html ($$$) { Line 7461  sub set_inner_html ($$$) {
7461          unless defined $p->{content_model};          unless defined $p->{content_model};
7462          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
7463    
7464      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7465          ## TODO: Foreign element OK?
7466    
7467      ## Step 3      ## Step 3
7468      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
# Line 6564  sub set_inner_html ($$$) { Line 7472  sub set_inner_html ($$$) {
7472      $doc->append_child ($root);      $doc->append_child ($root);
7473    
7474      ## Step 5 # MUST      ## Step 5 # MUST
7475      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7476    
7477      undef $p->{head_element};      undef $p->{head_element};
7478    

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24