/[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.116 by wakaba, Mon Mar 17 13:23:39 2008 UTC revision 1.153 by wakaba, Fri Aug 15 08:32:41 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
367      ## TODO: normalize charset name  
368      ## HTML5 encoding sniffing algorithm
369      require Message::Charset::Info;
370      my $charset;
371      my $buffer;
372      my ($char_stream, $e_status);
373    
374      ## "Change the encoding" algorithm:    SNIFFING: {
375    
376      ## Step 1          ## Step 1
377      if ($charset eq 'utf-16') { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?      if (defined $charset_name) {
378        $charset = 'utf-8';        $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',
447                            text => $charset_name,
448                            level => $self->{level}->{info},
449                            layer => 'encode',
450                            line => 1, column => 1);
451            $self->{confident} = 0;
452            last SNIFFING;
453          }
454        }
455    
456        ## Step 7: default
457        ## TODO: Make this configurable.
458        $charset = Message::Charset::Info->get_by_iana_name ('windows-1252');
459            ## NOTE: We choose |windows-1252| here, since |utf-8| should be
460            ## detectable in the step 6.
461        require Whatpm::Charset::DecodeHandle;
462        $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
463            ($byte_stream);
464        ($char_stream, $e_status)
465            = $charset->get_decode_handle ($buffer,
466                                           allow_error_reporting => 1,
467                                           allow_fallback => 1,
468                                           byte_buffer => \$byte_buffer);
469        $buffer->{buffer} = $byte_buffer;
470        !!!parse-error (type => 'sniffing:default',
471                        text => 'windows-1252',
472                        level => $self->{level}->{info},
473                        line => 1, column => 1,
474                        layer => 'encode');
475        $self->{confident} = 0;
476      } # SNIFFING
477    
478      $self->{input_encoding} = $charset->get_iana_name;
479      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
480        !!!parse-error (type => 'chardecode:fallback',
481                        text => $self->{input_encoding},
482                        level => $self->{level}->{uncertain},
483                        line => 1, column => 1,
484                        layer => 'encode');
485      } elsif (not ($e_status &
486                    Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
487        !!!parse-error (type => 'chardecode:no error',
488                        text => $self->{input_encoding},
489                        level => $self->{level}->{uncertain},
490                        line => 1, column => 1,
491                        layer => 'encode');
492      }
493    
494      $self->{change_encoding} = sub {
495        my $self = shift;
496        $charset_name = shift;
497        my $token = shift;
498    
499        $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
500        ($char_stream, $e_status) = $charset->get_decode_handle
501            ($byte_stream, allow_error_reporting => 1, allow_fallback => 1,
502             byte_buffer => \ $buffer->{buffer});
503        
504        if ($char_stream) { # if supported
505          ## "Change the encoding" algorithm:
506    
507          ## Step 1    
508          if ($charset->{category} &
509              Message::Charset::Info::CHARSET_CATEGORY_UTF16 ()) {
510            $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
511            ($char_stream, $e_status) = $charset->get_decode_handle
512                ($byte_stream,
513                 byte_buffer => \ $buffer->{buffer});
514          }
515          $charset_name = $charset->get_iana_name;
516          
517          ## Step 2
518          if (defined $self->{input_encoding} and
519              $self->{input_encoding} eq $charset_name) {
520            !!!parse-error (type => 'charset label:matching',
521                            text => $charset_name,
522                            level => $self->{level}->{info});
523            $self->{confident} = 1;
524            return;
525          }
526    
527          !!!parse-error (type => 'charset label detected',
528                          text => $self->{input_encoding},
529                          value => $charset_name,
530                          level => $self->{level}->{warn},
531                          token => $token);
532          
533          ## Step 3
534          # if (can) {
535            ## change the encoding on the fly.
536            #$self->{confident} = 1;
537            #return;
538          # }
539          
540          ## Step 4
541          throw Whatpm::HTML::RestartParser ();
542        }
543    }; # $self->{change_encoding}    }; # $self->{change_encoding}
544    
545      my $char_onerror = sub {
546        my (undef, $type, %opt) = @_;
547        !!!parse-error (layer => 'encode',
548                        %opt, type => $type,
549                        line => $self->{line}, column => $self->{column} + 1);
550        if ($opt{octets}) {
551          ${$opt{octets}} = "\x{FFFD}"; # relacement character
552        }
553      };
554      $char_stream->onerror ($char_onerror);
555    
556    my @args = @_; shift @args; # $s    my @args = @_; shift @args; # $s
557    my $return;    my $return;
558    try {    try {
559      $return = $self->parse_char_string ($s, @args);        $return = $self->parse_char_stream ($char_stream, @args);  
560    } catch Whatpm::HTML::RestartParser with {    } catch Whatpm::HTML::RestartParser with {
561      my $charset = shift->{charset};      ## NOTE: Invoked after {change_encoding}.
562      $s = \ (Encode::decode ($charset, $$bytes_s));      
563      $self->{input_encoding} = $charset; ## TODO: normalize      $self->{input_encoding} = $charset->get_iana_name;
564        if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
565          !!!parse-error (type => 'chardecode:fallback',
566                          text => $self->{input_encoding},
567                          level => $self->{level}->{uncertain},
568                          line => 1, column => 1,
569                          layer => 'encode');
570        } elsif (not ($e_status &
571                      Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
572          !!!parse-error (type => 'chardecode:no error',
573                          text => $self->{input_encoding},
574                          level => $self->{level}->{uncertain},
575                          line => 1, column => 1,
576                          layer => 'encode');
577        }
578      $self->{confident} = 1;      $self->{confident} = 1;
579      $return = $self->parse_char_string ($s, @args);      $char_stream->onerror ($char_onerror);
580        $return = $self->parse_char_stream ($char_stream, @args);
581    };    };
582    return $return;    return $return;
583  } # parse_byte_string  } # parse_byte_stream
584    
585  ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM  ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
586  ## 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 591  sub parse_byte_string ($$$$;$) {
591  ## 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
592  ## strip the BOM and never strip any ZWNBSP.  ## strip the BOM and never strip any ZWNBSP.
593    
594  *parse_char_string = \&parse_string;  sub parse_char_string ($$$;$) {
595      my $self = shift;
596      require utf8;
597      my $s = ref $_[0] ? $_[0] : \($_[0]);
598      open my $input, '<' . (utf8::is_utf8 ($$s) ? ':utf8' : ''), $s;
599      return $self->parse_char_stream ($input, @_[1..$#_]);
600    } # parse_char_string
601    *parse_string = \&parse_char_string;
602    
603  sub parse_string ($$$;$) {  sub parse_char_stream ($$$;$) {
604    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
605    my $s = ref $_[0] ? $_[0] : \($_[0]);    my $input = $_[0];
606    $self->{document} = $_[1];    $self->{document} = $_[1];
607    @{$self->{document}->child_nodes} = ();    @{$self->{document}->child_nodes} = ();
608    
# Line 186  sub parse_string ($$$;$) { Line 621  sub parse_string ($$$;$) {
621      pop @{$self->{prev_char}};      pop @{$self->{prev_char}};
622      unshift @{$self->{prev_char}}, $self->{next_char};      unshift @{$self->{prev_char}}, $self->{next_char};
623    
624      $self->{next_char} = -1 and return if $i >= length $$s;      my $char;
625      $self->{next_char} = ord substr $$s, $i++, 1;      if (defined $self->{next_next_char}) {
626          $char = $self->{next_next_char};
627          delete $self->{next_next_char};
628        } else {
629          $char = $input->getc;
630        }
631        $self->{next_char} = -1 and return unless defined $char;
632        $self->{next_char} = ord $char;
633    
634      ($self->{line_prev}, $self->{column_prev})      ($self->{line_prev}, $self->{column_prev})
635          = ($self->{line}, $self->{column});          = ($self->{line}, $self->{column});
636      $self->{column}++;      $self->{column}++;
637            
638      if ($self->{next_char} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
639          !!!cp ('j1');
640        $self->{line}++;        $self->{line}++;
641        $self->{column} = 0;        $self->{column} = 0;
642      } elsif ($self->{next_char} == 0x000D) { # CR      } elsif ($self->{next_char} == 0x000D) { # CR
643        $i++ if substr ($$s, $i, 1) eq "\x0A";        !!!cp ('j2');
644          my $next = $input->getc;
645          if (defined $next and $next ne "\x0A") {
646            $self->{next_next_char} = $next;
647          }
648        $self->{next_char} = 0x000A; # LF # MUST        $self->{next_char} = 0x000A; # LF # MUST
649        $self->{line}++;        $self->{line}++;
650        $self->{column} = 0;        $self->{column} = 0;
651      } elsif ($self->{next_char} > 0x10FFFF) {      } elsif ($self->{next_char} > 0x10FFFF) {
652          !!!cp ('j3');
653        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
654      } elsif ($self->{next_char} == 0x0000) { # NULL      } elsif ($self->{next_char} == 0x0000) { # NULL
655          !!!cp ('j4');
656        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
657        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
658        } elsif ($self->{next_char} <= 0x0008 or
659                 (0x000E <= $self->{next_char} and $self->{next_char} <= 0x001F) or
660                 (0x007F <= $self->{next_char} and $self->{next_char} <= 0x009F) or
661                 (0xD800 <= $self->{next_char} and $self->{next_char} <= 0xDFFF) or
662                 (0xFDD0 <= $self->{next_char} and $self->{next_char} <= 0xFDDF) or
663                 {
664                  0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
665                  0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
666                  0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
667                  0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
668                  0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
669                  0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
670                  0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
671                  0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
672                  0x10FFFE => 1, 0x10FFFF => 1,
673                 }->{$self->{next_char}}) {
674          !!!cp ('j5');
675          if ($self->{next_char} < 0x10000) {
676            !!!parse-error (type => 'control char',
677                            text => (sprintf 'U+%04X', $self->{next_char}));
678          } else {
679            !!!parse-error (type => 'control char',
680                            text => (sprintf 'U-%08X', $self->{next_char}));
681          }
682      }      }
683    };    };
684    $self->{prev_char} = [-1, -1, -1];    $self->{prev_char} = [-1, -1, -1];
# Line 229  sub parse_string ($$$;$) { Line 702  sub parse_string ($$$;$) {
702    delete $self->{parse_error}; # remove loop    delete $self->{parse_error}; # remove loop
703    
704    return $self->{document};    return $self->{document};
705  } # parse_string  } # parse_char_stream
706    
707  sub new ($) {  sub new ($) {
708    my $class = shift;    my $class = shift;
709    my $self = bless {}, $class;    my $self = bless {
710        level => {must => 'm',
711                  warn => 'w',
712                  info => 'i',
713                  uncertain => 'u'},
714      }, $class;
715    $self->{set_next_char} = sub {    $self->{set_next_char} = sub {
716      $self->{next_char} = -1;      $self->{next_char} = -1;
717    };    };
# Line 295  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO Line 773  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO
773  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
774  sub BOGUS_DOCTYPE_STATE () { 32 }  sub BOGUS_DOCTYPE_STATE () { 32 }
775  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
776    sub SELF_CLOSING_START_TAG_STATE () { 34 }
777    sub CDATA_BLOCK_STATE () { 35 }
778    
779  sub DOCTYPE_TOKEN () { 1 }  sub DOCTYPE_TOKEN () { 1 }
780  sub COMMENT_TOKEN () { 2 }  sub COMMENT_TOKEN () { 2 }
# Line 312  sub ROW_IMS ()        { 0b10000000 } Line 792  sub ROW_IMS ()        { 0b10000000 }
792  sub BODY_AFTER_IMS () { 0b100000000 }  sub BODY_AFTER_IMS () { 0b100000000 }
793  sub FRAME_IMS ()      { 0b1000000000 }  sub FRAME_IMS ()      { 0b1000000000 }
794  sub SELECT_IMS ()     { 0b10000000000 }  sub SELECT_IMS ()     { 0b10000000000 }
795    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
796        ## NOTE: "in foreign content" insertion mode is special; it is combined
797        ## with the secondary insertion mode.  In this parser, they are stored
798        ## together in the bit-or'ed form.
799    
800  ## NOTE: "initial" and "before html" insertion modes have no constants.  ## NOTE: "initial" and "before html" insertion modes have no constants.
801    
# Line 348  sub _initialize_tokenizer ($) { Line 832  sub _initialize_tokenizer ($) {
832    undef $self->{current_attribute};    undef $self->{current_attribute};
833    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
834    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
835      delete $self->{self_closing};
836    $self->{char} = [];    $self->{char} = [];
837    # $self->{next_char}    # $self->{next_char}
838    !!!next-input-character;    !!!next-input-character;
# Line 368  sub _initialize_tokenizer ($) { Line 853  sub _initialize_tokenizer ($) {
853  ##        ->{value}  ##        ->{value}
854  ##        ->{has_reference} == 1 or 0  ##        ->{has_reference} == 1 or 0
855  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
856    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
857    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
858    ##     while the token is pushed back to the stack.
859    
860  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
861    
# Line 394  sub _initialize_tokenizer ($) { Line 882  sub _initialize_tokenizer ($) {
882    
883  sub _get_next_token ($) {  sub _get_next_token ($) {
884    my $self = shift;    my $self = shift;
885    
886      if ($self->{self_closing}) {
887        !!!parse-error (type => 'nestc', token => $self->{current_token});
888        ## NOTE: The |self_closing| flag is only set by start tag token.
889        ## In addition, when a start tag token is emitted, it is always set to
890        ## |current_token|.
891        delete $self->{self_closing};
892      }
893    
894    if (@{$self->{token}}) {    if (@{$self->{token}}) {
895        $self->{self_closing} = $self->{token}->[0]->{self_closing};
896      return shift @{$self->{token}};      return shift @{$self->{token}};
897    }    }
898    
# Line 466  sub _get_next_token ($) { Line 964  sub _get_next_token ($) {
964        # Anything else        # Anything else
965        my $token = {type => CHARACTER_TOKEN,        my $token = {type => CHARACTER_TOKEN,
966                     data => chr $self->{next_char},                     data => chr $self->{next_char},
967                     line => $self->{line}, column => $self->{column}};                     line => $self->{line}, column => $self->{column},
968                      };
969        ## Stay in the data state        ## Stay in the data state
970        !!!next-input-character;        !!!next-input-character;
971    
# Line 486  sub _get_next_token ($) { Line 985  sub _get_next_token ($) {
985        unless (defined $token) {        unless (defined $token) {
986          !!!cp (13);          !!!cp (13);
987          !!!emit ({type => CHARACTER_TOKEN, data => '&',          !!!emit ({type => CHARACTER_TOKEN, data => '&',
988                    line => $l, column => $c});                    line => $l, column => $c,
989                     });
990        } else {        } else {
991          !!!cp (14);          !!!cp (14);
992          !!!emit ($token);          !!!emit ($token);
# Line 507  sub _get_next_token ($) { Line 1007  sub _get_next_token ($) {
1007    
1008            !!!emit ({type => CHARACTER_TOKEN, data => '<',            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1009                      line => $self->{line_prev},                      line => $self->{line_prev},
1010                      column => $self->{column_prev}});                      column => $self->{column_prev},
1011                       });
1012    
1013            redo A;            redo A;
1014          }          }
# Line 553  sub _get_next_token ($) { Line 1054  sub _get_next_token ($) {
1054    
1055            !!!emit ({type => CHARACTER_TOKEN, data => '<>',            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
1056                      line => $self->{line_prev},                      line => $self->{line_prev},
1057                      column => $self->{column_prev}});                      column => $self->{column_prev},
1058                       });
1059    
1060            redo A;            redo A;
1061          } elsif ($self->{next_char} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
# Line 564  sub _get_next_token ($) { Line 1066  sub _get_next_token ($) {
1066            $self->{state} = BOGUS_COMMENT_STATE;            $self->{state} = BOGUS_COMMENT_STATE;
1067            $self->{current_token} = {type => COMMENT_TOKEN, data => '',            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1068                                      line => $self->{line_prev},                                      line => $self->{line_prev},
1069                                      column => $self->{column_prev}};                                      column => $self->{column_prev},
1070                                       };
1071            ## $self->{next_char} is intentionally left as is            ## $self->{next_char} is intentionally left as is
1072            redo A;            redo A;
1073          } else {          } else {
1074            !!!cp (23);            !!!cp (23);
1075            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago',
1076                              line => $self->{line_prev},
1077                              column => $self->{column_prev});
1078            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1079            ## reconsume            ## reconsume
1080    
1081            !!!emit ({type => CHARACTER_TOKEN, data => '<',            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1082                      line => $self->{line_prev},                      line => $self->{line_prev},
1083                      column => $self->{column_prev}});                      column => $self->{column_prev},
1084                       });
1085    
1086            redo A;            redo A;
1087          }          }
# Line 604  sub _get_next_token ($) { Line 1110  sub _get_next_token ($) {
1110                $self->{state} = DATA_STATE;                $self->{state} = DATA_STATE;
1111    
1112                !!!emit ({type => CHARACTER_TOKEN, data => '</',                !!!emit ({type => CHARACTER_TOKEN, data => '</',
1113                          line => $l, column => $c});                          line => $l, column => $c,
1114                           });
1115        
1116                redo A;                redo A;
1117              }              }
# Line 624  sub _get_next_token ($) { Line 1131  sub _get_next_token ($) {
1131              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
1132              $self->{state} = DATA_STATE;              $self->{state} = DATA_STATE;
1133              !!!emit ({type => CHARACTER_TOKEN, data => '</',              !!!emit ({type => CHARACTER_TOKEN, data => '</',
1134                        line => $l, column => $c});                        line => $l, column => $c,
1135                         });
1136              redo A;              redo A;
1137            } else {            } else {
1138              !!!cp (27);              !!!cp (27);
# Line 638  sub _get_next_token ($) { Line 1146  sub _get_next_token ($) {
1146            # next-input-character is already done            # next-input-character is already done
1147            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1148            !!!emit ({type => CHARACTER_TOKEN, data => '</',            !!!emit ({type => CHARACTER_TOKEN, data => '</',
1149                      line => $l, column => $c});                      line => $l, column => $c,
1150                       });
1151            redo A;            redo A;
1152          }          }
1153        }        }
# Line 677  sub _get_next_token ($) { Line 1186  sub _get_next_token ($) {
1186          # reconsume          # reconsume
1187    
1188          !!!emit ({type => CHARACTER_TOKEN, data => '</',          !!!emit ({type => CHARACTER_TOKEN, data => '</',
1189                    line => $l, column => $c});                    line => $l, column => $c,
1190                     });
1191    
1192          redo A;          redo A;
1193        } else {        } else {
# Line 686  sub _get_next_token ($) { Line 1196  sub _get_next_token ($) {
1196          $self->{state} = BOGUS_COMMENT_STATE;          $self->{state} = BOGUS_COMMENT_STATE;
1197          $self->{current_token} = {type => COMMENT_TOKEN, data => '',          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1198                                    line => $self->{line_prev}, # "<" of "</"                                    line => $self->{line_prev}, # "<" of "</"
1199                                    column => $self->{column_prev} - 1};                                    column => $self->{column_prev} - 1,
1200                                     };
1201          ## $self->{next_char} is intentionally left as is          ## $self->{next_char} is intentionally left as is
1202          redo A;          redo A;
1203        }        }
# Line 703  sub _get_next_token ($) { Line 1214  sub _get_next_token ($) {
1214        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1215          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1216            !!!cp (35);            !!!cp (35);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1217            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1218          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1219            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 736  sub _get_next_token ($) { Line 1245  sub _get_next_token ($) {
1245          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1246          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1247            !!!cp (39);            !!!cp (39);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1248            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1249          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1250            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 758  sub _get_next_token ($) { Line 1265  sub _get_next_token ($) {
1265    
1266          redo A;          redo A;
1267        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1268            !!!cp (42);
1269            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1270          !!!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  
1271          redo A;          redo A;
1272        } else {        } else {
1273          !!!cp (44);          !!!cp (44);
# Line 793  sub _get_next_token ($) { Line 1290  sub _get_next_token ($) {
1290        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1291          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1292            !!!cp (46);            !!!cp (46);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1293            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1294          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1295            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 816  sub _get_next_token ($) { Line 1311  sub _get_next_token ($) {
1311        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{next_char} and
1312                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1313          !!!cp (49);          !!!cp (49);
1314          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{current_attribute}
1315                                value => ''};              = {name => chr ($self->{next_char} + 0x0020),
1316                   value => '',
1317                   line => $self->{line}, column => $self->{column}};
1318          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1319          !!!next-input-character;          !!!next-input-character;
1320          redo A;          redo A;
1321        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1322            !!!cp (50);
1323            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1324          !!!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  
1325          redo A;          redo A;
1326        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1327          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1328          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1329            !!!cp (52);            !!!cp (52);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1330            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1331          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1332            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 871  sub _get_next_token ($) { Line 1356  sub _get_next_token ($) {
1356          } else {          } else {
1357            !!!cp (56);            !!!cp (56);
1358          }          }
1359          $self->{current_attribute} = {name => chr ($self->{next_char}),          $self->{current_attribute}
1360                                value => ''};              = {name => chr ($self->{next_char}),
1361                   value => '',
1362                   line => $self->{line}, column => $self->{column}};
1363          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1364          !!!next-input-character;          !!!next-input-character;
1365          redo A;          redo A;
# Line 882  sub _get_next_token ($) { Line 1369  sub _get_next_token ($) {
1369          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1370              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1371            !!!cp (57);            !!!cp (57);
1372            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});            !!!parse-error (type => 'duplicate attribute', text => $self->{current_attribute}->{name}, line => $self->{current_attribute}->{line}, column => $self->{current_attribute}->{column});
1373            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1374          } else {          } else {
1375            !!!cp (58);            !!!cp (58);
# Line 911  sub _get_next_token ($) { Line 1398  sub _get_next_token ($) {
1398          $before_leave->();          $before_leave->();
1399          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1400            !!!cp (61);            !!!cp (61);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1401            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1402          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1403            !!!cp (62);            !!!cp (62);
# Line 937  sub _get_next_token ($) { Line 1422  sub _get_next_token ($) {
1422          !!!next-input-character;          !!!next-input-character;
1423          redo A;          redo A;
1424        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1425            !!!cp (64);
1426          $before_leave->();          $before_leave->();
1427            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1428          !!!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  
1429          redo A;          redo A;
1430        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1431          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1432          $before_leave->();          $before_leave->();
1433          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1434            !!!cp (66);            !!!cp (66);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1435            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1436          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1437            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1009  sub _get_next_token ($) { Line 1482  sub _get_next_token ($) {
1482        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1483          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1484            !!!cp (73);            !!!cp (73);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1485            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1486          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1487            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1033  sub _get_next_token ($) { Line 1504  sub _get_next_token ($) {
1504        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{next_char} and
1505                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1506          !!!cp (76);          !!!cp (76);
1507          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{current_attribute}
1508                                value => ''};              = {name => chr ($self->{next_char} + 0x0020),
1509                   value => '',
1510                   line => $self->{line}, column => $self->{column}};
1511          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1512          !!!next-input-character;          !!!next-input-character;
1513          redo A;          redo A;
1514        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1515            !!!cp (77);
1516            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1517          !!!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  
1518          redo A;          redo A;
1519        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1520          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1521          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1522            !!!cp (79);            !!!cp (79);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1523            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1524          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1525            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1081  sub _get_next_token ($) { Line 1541  sub _get_next_token ($) {
1541          redo A;          redo A;
1542        } else {        } else {
1543          !!!cp (82);          !!!cp (82);
1544          $self->{current_attribute} = {name => chr ($self->{next_char}),          $self->{current_attribute}
1545                                value => ''};              = {name => chr ($self->{next_char}),
1546                   value => '',
1547                   line => $self->{line}, column => $self->{column}};
1548          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1549          !!!next-input-character;          !!!next-input-character;
1550          redo A;                  redo A;        
# Line 1115  sub _get_next_token ($) { Line 1577  sub _get_next_token ($) {
1577        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1578          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1579            !!!cp (87);            !!!cp (87);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1580            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1581          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1582            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1140  sub _get_next_token ($) { Line 1600  sub _get_next_token ($) {
1600          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1601          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1602            !!!cp (90);            !!!cp (90);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1603            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1604          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1605            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1189  sub _get_next_token ($) { Line 1647  sub _get_next_token ($) {
1647          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1648          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1649            !!!cp (97);            !!!cp (97);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1650            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1651          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1652            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1233  sub _get_next_token ($) { Line 1689  sub _get_next_token ($) {
1689          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1690          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1691            !!!cp (103);            !!!cp (103);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1692            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1693          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1694            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1280  sub _get_next_token ($) { Line 1734  sub _get_next_token ($) {
1734        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1735          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1736            !!!cp (109);            !!!cp (109);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1737            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1738          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1739            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1305  sub _get_next_token ($) { Line 1757  sub _get_next_token ($) {
1757          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1758          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1759            !!!cp (112);            !!!cp (112);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1760            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1761          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1762            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1377  sub _get_next_token ($) { Line 1827  sub _get_next_token ($) {
1827        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1828          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1829            !!!cp (119);            !!!cp (119);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1830            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1831          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1832            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1399  sub _get_next_token ($) { Line 1847  sub _get_next_token ($) {
1847    
1848          redo A;          redo A;
1849        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1850            !!!cp (122);
1851            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1852          !!!next-input-character;          !!!next-input-character;
1853          if ($self->{next_char} == 0x003E and # >          redo A;
1854              $self->{current_token}->{type} == START_TAG_TOKEN and        } elsif ($self->{next_char} == -1) {
1855              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {          !!!parse-error (type => 'unclosed tag');
1856            # permitted slash          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1857            !!!cp (122);            !!!cp (122.3);
1858            #            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1859            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1860              if ($self->{current_token}->{attributes}) {
1861                !!!cp (122.1);
1862                !!!parse-error (type => 'end tag attribute');
1863              } else {
1864                ## NOTE: This state should never be reached.
1865                !!!cp (122.2);
1866              }
1867          } else {          } else {
1868            !!!cp (123);            die "$0: $self->{current_token}->{type}: Unknown token type";
           !!!parse-error (type => 'nestc');  
1869          }          }
1870          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = DATA_STATE;
1871          # next-input-character is already done          ## Reconsume.
1872            !!!emit ($self->{current_token}); # start tag or end tag
1873          redo A;          redo A;
1874        } else {        } else {
1875          !!!cp (124);          !!!cp ('124.1');
1876          !!!parse-error (type => 'no space between attributes');          !!!parse-error (type => 'no space between attributes');
1877          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1878          ## reconsume          ## reconsume
1879          redo A;          redo A;
1880        }        }
1881        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1882          if ($self->{next_char} == 0x003E) { # >
1883            if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1884              !!!cp ('124.2');
1885              !!!parse-error (type => 'nestc', token => $self->{current_token});
1886              ## TODO: Different type than slash in start tag
1887              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1888              if ($self->{current_token}->{attributes}) {
1889                !!!cp ('124.4');
1890                !!!parse-error (type => 'end tag attribute');
1891              } else {
1892                !!!cp ('124.5');
1893              }
1894              ## TODO: Test |<title></title/>|
1895            } else {
1896              !!!cp ('124.3');
1897              $self->{self_closing} = 1;
1898            }
1899    
1900            $self->{state} = DATA_STATE;
1901            !!!next-input-character;
1902    
1903            !!!emit ($self->{current_token}); # start tag or end tag
1904    
1905            redo A;
1906          } elsif ($self->{next_char} == -1) {
1907            !!!parse-error (type => 'unclosed tag');
1908            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1909              !!!cp (124.7);
1910              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1911            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1912              if ($self->{current_token}->{attributes}) {
1913                !!!cp (124.5);
1914                !!!parse-error (type => 'end tag attribute');
1915              } else {
1916                ## NOTE: This state should never be reached.
1917                !!!cp (124.6);
1918              }
1919            } else {
1920              die "$0: $self->{current_token}->{type}: Unknown token type";
1921            }
1922            $self->{state} = DATA_STATE;
1923            ## Reconsume.
1924            !!!emit ($self->{current_token}); # start tag or end tag
1925            redo A;
1926          } else {
1927            !!!cp ('124.4');
1928            !!!parse-error (type => 'nestc');
1929            ## TODO: This error type is wrong.
1930            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1931            ## Reconsume.
1932            redo A;
1933          }
1934      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1935        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1936                
# Line 1466  sub _get_next_token ($) { Line 1977  sub _get_next_token ($) {
1977          if ($self->{next_char} == 0x002D) { # -          if ($self->{next_char} == 0x002D) { # -
1978            !!!cp (127);            !!!cp (127);
1979            $self->{current_token} = {type => COMMENT_TOKEN, data => '',            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1980                                      line => $l, column => $c};                                      line => $l, column => $c,
1981                                       };
1982            $self->{state} = COMMENT_START_STATE;            $self->{state} = COMMENT_START_STATE;
1983            !!!next-input-character;            !!!next-input-character;
1984            redo A;            redo A;
# Line 1504  sub _get_next_token ($) { Line 2016  sub _get_next_token ($) {
2016                      $self->{state} = DOCTYPE_STATE;                      $self->{state} = DOCTYPE_STATE;
2017                      $self->{current_token} = {type => DOCTYPE_TOKEN,                      $self->{current_token} = {type => DOCTYPE_TOKEN,
2018                                                quirks => 1,                                                quirks => 1,
2019                                                line => $l, column => $c};                                                line => $l, column => $c,
2020                                                 };
2021                      !!!next-input-character;                      !!!next-input-character;
2022                      redo A;                      redo A;
2023                    } else {                    } else {
# Line 1525  sub _get_next_token ($) { Line 2038  sub _get_next_token ($) {
2038          } else {          } else {
2039            !!!cp (135);            !!!cp (135);
2040          }          }
2041          } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
2042                   $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
2043                   $self->{next_char} == 0x005B) { # [
2044            !!!next-input-character;
2045            push @next_char, $self->{next_char};
2046            if ($self->{next_char} == 0x0043) { # C
2047              !!!next-input-character;
2048              push @next_char, $self->{next_char};
2049              if ($self->{next_char} == 0x0044) { # D
2050                !!!next-input-character;
2051                push @next_char, $self->{next_char};
2052                if ($self->{next_char} == 0x0041) { # A
2053                  !!!next-input-character;
2054                  push @next_char, $self->{next_char};
2055                  if ($self->{next_char} == 0x0054) { # T
2056                    !!!next-input-character;
2057                    push @next_char, $self->{next_char};
2058                    if ($self->{next_char} == 0x0041) { # A
2059                      !!!next-input-character;
2060                      push @next_char, $self->{next_char};
2061                      if ($self->{next_char} == 0x005B) { # [
2062                        !!!cp (135.1);
2063                        $self->{state} = CDATA_BLOCK_STATE;
2064                        !!!next-input-character;
2065                        redo A;
2066                      } else {
2067                        !!!cp (135.2);
2068                      }
2069                    } else {
2070                      !!!cp (135.3);
2071                    }
2072                  } else {
2073                    !!!cp (135.4);                
2074                  }
2075                } else {
2076                  !!!cp (135.5);
2077                }
2078              } else {
2079                !!!cp (135.6);
2080              }
2081            } else {
2082              !!!cp (135.7);
2083            }
2084        } else {        } else {
2085          !!!cp (136);          !!!cp (136);
2086        }        }
# Line 1534  sub _get_next_token ($) { Line 2090  sub _get_next_token ($) {
2090        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
2091        $self->{state} = BOGUS_COMMENT_STATE;        $self->{state} = BOGUS_COMMENT_STATE;
2092        $self->{current_token} = {type => COMMENT_TOKEN, data => '',        $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2093                                  line => $l, column => $c};                                  line => $l, column => $c,
2094                                   };
2095        redo A;        redo A;
2096                
2097        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
# Line 2123  sub _get_next_token ($) { Line 2680  sub _get_next_token ($) {
2680          redo A;          redo A;
2681        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2682          !!!cp (208);          !!!cp (208);
2683          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2684    
2685          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2686          !!!next-input-character;          !!!next-input-character;
# Line 2159  sub _get_next_token ($) { Line 2716  sub _get_next_token ($) {
2716          redo A;          redo A;
2717        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2718          !!!cp (212);          !!!cp (212);
2719          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2720    
2721          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2722          !!!next-input-character;          !!!next-input-character;
# Line 2207  sub _get_next_token ($) { Line 2764  sub _get_next_token ($) {
2764        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
2765          !!!cp (217);          !!!cp (217);
2766          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
   
2767          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2768          ## reconsume          ## reconsume
2769    
# Line 2248  sub _get_next_token ($) { Line 2804  sub _get_next_token ($) {
2804          !!!next-input-character;          !!!next-input-character;
2805          redo A;          redo A;
2806        }        }
2807        } elsif ($self->{state} == CDATA_BLOCK_STATE) {
2808          my $s = '';
2809          
2810          my ($l, $c) = ($self->{line}, $self->{column});
2811    
2812          CS: while ($self->{next_char} != -1) {
2813            if ($self->{next_char} == 0x005D) { # ]
2814              !!!next-input-character;
2815              if ($self->{next_char} == 0x005D) { # ]
2816                !!!next-input-character;
2817                MDC: {
2818                  if ($self->{next_char} == 0x003E) { # >
2819                    !!!cp (221.1);
2820                    !!!next-input-character;
2821                    last CS;
2822                  } elsif ($self->{next_char} == 0x005D) { # ]
2823                    !!!cp (221.2);
2824                    $s .= ']';
2825                    !!!next-input-character;
2826                    redo MDC;
2827                  } else {
2828                    !!!cp (221.3);
2829                    $s .= ']]';
2830                    #
2831                  }
2832                } # MDC
2833              } else {
2834                !!!cp (221.4);
2835                $s .= ']';
2836                #
2837              }
2838            } else {
2839              !!!cp (221.5);
2840              #
2841            }
2842            $s .= chr $self->{next_char};
2843            !!!next-input-character;
2844          } # CS
2845    
2846          $self->{state} = DATA_STATE;
2847          ## next-input-character done or EOF, which is reconsumed.
2848    
2849          if (length $s) {
2850            !!!cp (221.6);
2851            !!!emit ({type => CHARACTER_TOKEN, data => $s,
2852                      line => $l, column => $c});
2853          } else {
2854            !!!cp (221.7);
2855          }
2856    
2857          redo A;
2858    
2859          ## ISSUE: "text tokens" in spec.
2860          ## TODO: Streaming support
2861      } else {      } else {
2862        die "$0: $self->{state}: Unknown state";        die "$0: $self->{state}: Unknown state";
2863      }      }
# Line 2315  sub _tokenize_attempt_to_consume_an_enti Line 2925  sub _tokenize_attempt_to_consume_an_enti
2925    
2926          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2927            !!!cp (1008);            !!!cp (1008);
2928            !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);            !!!parse-error (type => 'invalid character reference',
2929                              text => (sprintf 'U+%04X', $code),
2930                              line => $l, column => $c);
2931            $code = 0xFFFD;            $code = 0xFFFD;
2932          } elsif ($code > 0x10FFFF) {          } elsif ($code > 0x10FFFF) {
2933            !!!cp (1009);            !!!cp (1009);
2934            !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);            !!!parse-error (type => 'invalid character reference',
2935                              text => (sprintf 'U-%08X', $code),
2936                              line => $l, column => $c);
2937            $code = 0xFFFD;            $code = 0xFFFD;
2938          } elsif ($code == 0x000D) {          } elsif ($code == 0x000D) {
2939            !!!cp (1010);            !!!cp (1010);
# Line 2327  sub _tokenize_attempt_to_consume_an_enti Line 2941  sub _tokenize_attempt_to_consume_an_enti
2941            $code = 0x000A;            $code = 0x000A;
2942          } elsif (0x80 <= $code and $code <= 0x9F) {          } elsif (0x80 <= $code and $code <= 0x9F) {
2943            !!!cp (1011);            !!!cp (1011);
2944            !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);            !!!parse-error (type => 'C1 character reference', text => (sprintf 'U+%04X', $code), line => $l, column => $c);
2945            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
2946          }          }
2947    
2948          return {type => CHARACTER_TOKEN, data => chr $code,          return {type => CHARACTER_TOKEN, data => chr $code,
2949                  has_reference => 1, line => $l, column => $c};                  has_reference => 1,
2950                    line => $l, column => $c,
2951                   };
2952        } # X        } # X
2953      } elsif (0x0030 <= $self->{next_char} and      } elsif (0x0030 <= $self->{next_char} and
2954               $self->{next_char} <= 0x0039) { # 0..9               $self->{next_char} <= 0x0039) { # 0..9
# Line 2358  sub _tokenize_attempt_to_consume_an_enti Line 2974  sub _tokenize_attempt_to_consume_an_enti
2974    
2975        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2976          !!!cp (1015);          !!!cp (1015);
2977          !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);          !!!parse-error (type => 'invalid character reference',
2978                            text => (sprintf 'U+%04X', $code),
2979                            line => $l, column => $c);
2980          $code = 0xFFFD;          $code = 0xFFFD;
2981        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
2982          !!!cp (1016);          !!!cp (1016);
2983          !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);          !!!parse-error (type => 'invalid character reference',
2984                            text => (sprintf 'U-%08X', $code),
2985                            line => $l, column => $c);
2986          $code = 0xFFFD;          $code = 0xFFFD;
2987        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
2988          !!!cp (1017);          !!!cp (1017);
2989          !!!parse-error (type => 'CR character reference', line => $l, column => $c);          !!!parse-error (type => 'CR character reference',
2990                            line => $l, column => $c);
2991          $code = 0x000A;          $code = 0x000A;
2992        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
2993          !!!cp (1018);          !!!cp (1018);
2994          !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);          !!!parse-error (type => 'C1 character reference',
2995                            text => (sprintf 'U+%04X', $code),
2996                            line => $l, column => $c);
2997          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
2998        }        }
2999                
3000        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,
3001                line => $l, column => $c};                line => $l, column => $c,
3002                 };
3003      } else {      } else {
3004        !!!cp (1019);        !!!cp (1019);
3005        !!!parse-error (type => 'bare nero', line => $l, column => $c);        !!!parse-error (type => 'bare nero', line => $l, column => $c);
# Line 2395  sub _tokenize_attempt_to_consume_an_enti Line 3019  sub _tokenize_attempt_to_consume_an_enti
3019      require Whatpm::_NamedEntityList;      require Whatpm::_NamedEntityList;
3020      our $EntityChar;      our $EntityChar;
3021    
3022      while (length $entity_name < 10 and      while (length $entity_name < 30 and
3023             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
3024             ((0x0041 <= $self->{next_char} and # a             ((0x0041 <= $self->{next_char} and # a
3025               $self->{next_char} <= 0x005A) or # x               $self->{next_char} <= 0x005A) or # x
# Line 2429  sub _tokenize_attempt_to_consume_an_enti Line 3053  sub _tokenize_attempt_to_consume_an_enti
3053      if ($match > 0) {      if ($match > 0) {
3054        !!!cp (1023);        !!!cp (1023);
3055        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
3056                line => $l, column => $c};                line => $l, column => $c,
3057                 };
3058      } elsif ($match < 0) {      } elsif ($match < 0) {
3059        !!!parse-error (type => 'no refc', line => $l, column => $c);        !!!parse-error (type => 'no refc', line => $l, column => $c);
3060        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
3061          !!!cp (1024);          !!!cp (1024);
3062          return {type => CHARACTER_TOKEN, data => '&'.$entity_name,          return {type => CHARACTER_TOKEN, data => '&'.$entity_name,
3063                  line => $l, column => $c};                  line => $l, column => $c,
3064                   };
3065        } else {        } else {
3066          !!!cp (1025);          !!!cp (1025);
3067          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
3068                  line => $l, column => $c};                  line => $l, column => $c,
3069                   };
3070        }        }
3071      } else {      } else {
3072        !!!cp (1026);        !!!cp (1026);
3073        !!!parse-error (type => 'bare ero', line => $l, column => $c);        !!!parse-error (type => 'bare ero', line => $l, column => $c);
3074        ## NOTE: "No characters are consumed" in the spec.        ## NOTE: "No characters are consumed" in the spec.
3075        return {type => CHARACTER_TOKEN, data => '&'.$value,        return {type => CHARACTER_TOKEN, data => '&'.$value,
3076                line => $l, column => $c};                line => $l, column => $c,
3077                 };
3078      }      }
3079    } else {    } else {
3080      !!!cp (1027);      !!!cp (1027);
# Line 2533  sub _tree_construction_initial ($) { Line 3161  sub _tree_construction_initial ($) {
3161                
3162        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
3163          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3164          ## NOTE: Default value for both |public_id| and |system_id| attributes
3165          ## are empty strings, so that we don't set any value in missing cases.
3166        $doctype->public_id ($token->{public_identifier})        $doctype->public_id ($token->{public_identifier})
3167            if defined $token->{public_identifier};            if defined $token->{public_identifier};
3168        $doctype->system_id ($token->{system_identifier})        $doctype->system_id ($token->{system_identifier})
# Line 2547  sub _tree_construction_initial ($) { Line 3177  sub _tree_construction_initial ($) {
3177        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{public_identifier}) {
3178          my $pubid = $token->{public_identifier};          my $pubid = $token->{public_identifier};
3179          $pubid =~ tr/a-z/A-z/;          $pubid =~ tr/a-z/A-z/;
3180          if ({          my $prefix = [
3181            "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,            "+//SILMARIL//DTD HTML PRO V0R11 19970101//",
3182            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3183            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3184            "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 1//",
3185            "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 2//",
3186            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//",
3187            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//",
3188            "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT//",
3189            "-//IETF//DTD HTML 2.0//EN" => 1,            "-//IETF//DTD HTML 2.0//",
3190            "-//IETF//DTD HTML 2.1E//EN" => 1,            "-//IETF//DTD HTML 2.1E//",
3191            "-//IETF//DTD HTML 3.0//EN" => 1,            "-//IETF//DTD HTML 3.0//",
3192            "-//IETF//DTD HTML 3.0//EN//" => 1,            "-//IETF//DTD HTML 3.2 FINAL//",
3193            "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,            "-//IETF//DTD HTML 3.2//",
3194            "-//IETF//DTD HTML 3.2//EN" => 1,            "-//IETF//DTD HTML 3//",
3195            "-//IETF//DTD HTML 3//EN" => 1,            "-//IETF//DTD HTML LEVEL 0//",
3196            "-//IETF//DTD HTML LEVEL 0//EN" => 1,            "-//IETF//DTD HTML LEVEL 1//",
3197            "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,            "-//IETF//DTD HTML LEVEL 2//",
3198            "-//IETF//DTD HTML LEVEL 1//EN" => 1,            "-//IETF//DTD HTML LEVEL 3//",
3199            "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 0//",
3200            "-//IETF//DTD HTML LEVEL 2//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 1//",
3201            "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 2//",
3202            "-//IETF//DTD HTML LEVEL 3//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 3//",
3203            "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,            "-//IETF//DTD HTML STRICT//",
3204            "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,            "-//IETF//DTD HTML//",
3205            "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,            "-//METRIUS//DTD METRIUS PRESENTATIONAL//",
3206            "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//",
3207            "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//",
3208            "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//",
3209            "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//",
3210            "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//",
3211            "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//",
3212            "-//IETF//DTD HTML STRICT//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD HTML//",
3213            "-//IETF//DTD HTML STRICT//EN//2.0" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//",
3214            "-//IETF//DTD HTML STRICT//EN//3.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//",
3215            "-//IETF//DTD HTML//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//",
3216            "-//IETF//DTD HTML//EN//2.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//",
3217            "-//IETF//DTD HTML//EN//3.0" => 1,            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//",
3218            "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//",
3219            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//",
3220            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//",
3221            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//",
3222            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//",
3223            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,            "-//W3C//DTD HTML 3 1995-03-24//",
3224            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,            "-//W3C//DTD HTML 3.2 DRAFT//",
3225            "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,            "-//W3C//DTD HTML 3.2 FINAL//",
3226            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//W3C//DTD HTML 3.2//",
3227            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//W3C//DTD HTML 3.2S DRAFT//",
3228            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 FRAMESET//",
3229            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 TRANSITIONAL//",
3230            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMETNAL 19960712//",
3231            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMENTAL 970421//",
3232            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//W3C//DTD W3 HTML//",
3233            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//W3O//DTD W3 HTML 3.0//",
3234            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML 2.0//",
3235            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML//",
3236            "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,          ]; # $prefix
3237            "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,          my $match;
3238            "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,          for (@$prefix) {
3239            "-//W3C//DTD HTML 3.2//EN" => 1,            if (substr ($prefix, 0, length $_) eq $_) {
3240            "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,              $match = 1;
3241            "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,              last;
3242            "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,            }
3243            "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,          }
3244            "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,          if ($match or
3245            "-//W3C//DTD W3 HTML//EN" => 1,              $pubid eq "-//W3O//DTD W3 HTML STRICT 3.0//EN//" or
3246            "-//W3O//DTD W3 HTML 3.0//EN" => 1,              $pubid eq "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" or
3247            "-//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}) {  
3248            !!!cp ('t5');            !!!cp ('t5');
3249            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3250          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD HTML 4.01 FRAMESET//] or
3251                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD HTML 4.01 TRANSITIONAL//]) {
3252            if (defined $token->{system_identifier}) {            if (defined $token->{system_identifier}) {
3253              !!!cp ('t6');              !!!cp ('t6');
3254              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
# Line 2632  sub _tree_construction_initial ($) { Line 3256  sub _tree_construction_initial ($) {
3256              !!!cp ('t7');              !!!cp ('t7');
3257              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
3258            }            }
3259          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD XHTML 1.0 FRAMESET//] or
3260                   $pubid eq "-//W3C//DTD XHTML 1.0 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD XHTML 1.0 TRANSITIONAL//]) {
3261            !!!cp ('t8');            !!!cp ('t8');
3262            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
3263          } else {          } else {
# Line 2646  sub _tree_construction_initial ($) { Line 3270  sub _tree_construction_initial ($) {
3270          my $sysid = $token->{system_identifier};          my $sysid = $token->{system_identifier};
3271          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
3272          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") {
3273            ## TODO: Check the spec: PUBLIC "(limited quirks)" "(quirks)"            ## NOTE: Ensure that |PUBLIC "(limited quirks)" "(quirks)"| is
3274              ## marked as quirks.
3275            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3276            !!!cp ('t11');            !!!cp ('t11');
3277          } else {          } else {
# Line 2669  sub _tree_construction_initial ($) { Line 3294  sub _tree_construction_initial ($) {
3294        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3295        ## Go to the "before html" insertion mode.        ## Go to the "before html" insertion mode.
3296        ## reprocess        ## reprocess
3297          !!!ack-later;
3298        return;        return;
3299      } elsif ($token->{type} == CHARACTER_TOKEN) {      } elsif ($token->{type} == CHARACTER_TOKEN) {
3300        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
# Line 2749  sub _tree_construction_root_element ($) Line 3375  sub _tree_construction_root_element ($)
3375        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
3376          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
3377            my $root_element;            my $root_element;
3378            !!!create-element ($root_element, $token->{tag_name}, $token->{attributes}, $token);            !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3379            $self->{document}->append_child ($root_element);            $self->{document}->append_child ($root_element);
3380            push @{$self->{open_elements}}, [$root_element, 'html'];            push @{$self->{open_elements}},
3381                  [$root_element, $el_category->{html}];
3382    
3383            if ($token->{attributes}->{manifest}) {            if ($token->{attributes}->{manifest}) {
3384              !!!cp ('t24');              !!!cp ('t24');
3385              $self->{application_cache_selection}              $self->{application_cache_selection}
3386                  ->($token->{attributes}->{manifest}->{value});                  ->($token->{attributes}->{manifest}->{value});
3387              ## ISSUE: No relative reference resolution?              ## ISSUE: Spec is unclear on relative references.
3388                ## According to Hixie (#whatwg 2008-03-19), it should be
3389                ## resolved against the base URI of the document in HTML
3390                ## or xml:base of the element in XHTML.
3391            } else {            } else {
3392              !!!cp ('t25');              !!!cp ('t25');
3393              $self->{application_cache_selection}->(undef);              $self->{application_cache_selection}->(undef);
3394            }            }
3395    
3396              !!!nack ('t25c');
3397    
3398            !!!next-token;            !!!next-token;
3399            return; ## Go to the "before head" insertion mode.            return; ## Go to the "before head" insertion mode.
3400          } else {          } else {
# Line 2779  sub _tree_construction_root_element ($) Line 3411  sub _tree_construction_root_element ($)
3411          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
3412        }        }
3413    
3414      my $root_element; !!!create-element ($root_element, 'html',, $token);      my $root_element;
3415        !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3416      $self->{document}->append_child ($root_element);      $self->{document}->append_child ($root_element);
3417      push @{$self->{open_elements}}, [$root_element, 'html'];      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3418    
3419      $self->{application_cache_selection}->(undef);      $self->{application_cache_selection}->(undef);
3420    
3421      ## NOTE: Reprocess the token.      ## NOTE: Reprocess the token.
3422        !!!ack-later;
3423      return; ## Go to the "before head" insertion mode.      return; ## Go to the "before head" insertion mode.
3424    
3425      ## ISSUE: There is an issue in the spec      ## ISSUE: There is an issue in the spec
# Line 2809  sub _reset_insertion_mode ($) { Line 3443  sub _reset_insertion_mode ($) {
3443        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3444          $last = 1;          $last = 1;
3445          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
3446            if ($self->{inner_html_node}->[1] eq 'td' or            !!!cp ('t28');
3447                $self->{inner_html_node}->[1] eq 'th') {            $node = $self->{inner_html_node};
3448              !!!cp ('t27');          } else {
3449              #            die "_reset_insertion_mode: t27";
           } else {  
             !!!cp ('t28');  
             $node = $self->{inner_html_node};  
           }  
3450          }          }
3451        }        }
3452              
3453        ## Step 4..13        ## Step 4..14
3454        my $new_mode = {        my $new_mode;
3455          if ($node->[1] & FOREIGN_EL) {
3456            !!!cp ('t28.1');
3457            ## NOTE: Strictly spaking, the line below only applies to MathML and
3458            ## SVG elements.  Currently the HTML syntax supports only MathML and
3459            ## SVG elements as foreigners.
3460            $new_mode = IN_BODY_IM | IN_FOREIGN_CONTENT_IM;
3461          } elsif ($node->[1] & TABLE_CELL_EL) {
3462            if ($last) {
3463              !!!cp ('t28.2');
3464              #
3465            } else {
3466              !!!cp ('t28.3');
3467              $new_mode = IN_CELL_IM;
3468            }
3469          } else {
3470            !!!cp ('t28.4');
3471            $new_mode = {
3472                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
3473                        ## NOTE: |option| and |optgroup| do not set                        ## NOTE: |option| and |optgroup| do not set
3474                        ## insertion mode to "in select" by themselves.                        ## insertion mode to "in select" by themselves.
                       td => IN_CELL_IM,  
                       th => IN_CELL_IM,  
3475                        tr => IN_ROW_IM,                        tr => IN_ROW_IM,
3476                        tbody => IN_TABLE_BODY_IM,                        tbody => IN_TABLE_BODY_IM,
3477                        thead => IN_TABLE_BODY_IM,                        thead => IN_TABLE_BODY_IM,
# Line 2837  sub _reset_insertion_mode ($) { Line 3482  sub _reset_insertion_mode ($) {
3482                        head => IN_BODY_IM, # not in head!                        head => IN_BODY_IM, # not in head!
3483                        body => IN_BODY_IM,                        body => IN_BODY_IM,
3484                        frameset => IN_FRAMESET_IM,                        frameset => IN_FRAMESET_IM,
3485                       }->{$node->[1]};                       }->{$node->[0]->manakai_local_name};
3486          }
3487        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3488                
3489        ## Step 14        ## Step 15
3490        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3491          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3492            !!!cp ('t29');            !!!cp ('t29');
3493            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
# Line 2855  sub _reset_insertion_mode ($) { Line 3501  sub _reset_insertion_mode ($) {
3501          !!!cp ('t31');          !!!cp ('t31');
3502        }        }
3503                
3504        ## Step 15        ## Step 16
3505        $self->{insertion_mode} = IN_BODY_IM and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
3506                
3507        ## Step 16        ## Step 17
3508        $i--;        $i--;
3509        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3510                
3511        ## Step 17        ## Step 18
3512        redo S3;        redo S3;
3513      } # S3      } # S3
3514    
# Line 2974  sub _tree_construction_main ($) { Line 3620  sub _tree_construction_main ($) {
3620      ## Step 1      ## Step 1
3621      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3622      my $el;      my $el;
3623      !!!create-element ($el, $start_tag_name, $token->{attributes}, $token);      !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3624    
3625      ## Step 2      ## Step 2
3626      $insert->($el);      $insert->($el);
# Line 2985  sub _tree_construction_main ($) { Line 3631  sub _tree_construction_main ($) {
3631    
3632      ## Step 4      ## Step 4
3633      my $text = '';      my $text = '';
3634        !!!nack ('t40.1');
3635      !!!next-token;      !!!next-token;
3636      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3637        !!!cp ('t40');        !!!cp ('t40');
# Line 3011  sub _tree_construction_main ($) { Line 3658  sub _tree_construction_main ($) {
3658        ## NOTE: An end-of-file token.        ## NOTE: An end-of-file token.
3659        if ($content_model_flag == CDATA_CONTENT_MODEL) {        if ($content_model_flag == CDATA_CONTENT_MODEL) {
3660          !!!cp ('t43');          !!!cp ('t43');
3661          !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);          !!!parse-error (type => 'in CDATA:#eof', token => $token);
3662        } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {        } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3663          !!!cp ('t44');          !!!cp ('t44');
3664          !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);          !!!parse-error (type => 'in RCDATA:#eof', token => $token);
3665        } else {        } else {
3666          die "$0: $content_model_flag in parse_rcdata";          die "$0: $content_model_flag in parse_rcdata";
3667        }        }
# Line 3024  sub _tree_construction_main ($) { Line 3671  sub _tree_construction_main ($) {
3671    
3672    my $script_start_tag = sub () {    my $script_start_tag = sub () {
3673      my $script_el;      my $script_el;
3674      !!!create-element ($script_el, 'script', $token->{attributes}, $token);      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
3675      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3676    
3677      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
3678      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
3679            
3680      my $text = '';      my $text = '';
3681        !!!nack ('t45.1');
3682      !!!next-token;      !!!next-token;
3683      while ($token->{type} == CHARACTER_TOKEN) {      while ($token->{type} == CHARACTER_TOKEN) {
3684        !!!cp ('t45');        !!!cp ('t45');
# Line 3050  sub _tree_construction_main ($) { Line 3698  sub _tree_construction_main ($) {
3698        ## Ignore the token        ## Ignore the token
3699      } else {      } else {
3700        !!!cp ('t48');        !!!cp ('t48');
3701        !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);        !!!parse-error (type => 'in CDATA:#eof', token => $token);
3702        ## ISSUE: And ignore?        ## ISSUE: And ignore?
3703        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3704      }      }
# Line 3088  sub _tree_construction_main ($) { Line 3736  sub _tree_construction_main ($) {
3736        my $formatting_element;        my $formatting_element;
3737        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3738        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3739          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3740              !!!cp ('t52');
3741              last AFE;
3742            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
3743                         eq $tag_name) {
3744            !!!cp ('t51');            !!!cp ('t51');
3745            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3746            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3747            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           !!!cp ('t52');  
           last AFE;  
3748          }          }
3749        } # AFE        } # AFE
3750        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3751          !!!cp ('t53');          !!!cp ('t53');
3752          !!!parse-error (type => 'unmatched end tag:'.$tag_name, token => $end_tag_token);          !!!parse-error (type => 'unmatched end tag', text => $tag_name, token => $end_tag_token);
3753          ## Ignore the token          ## Ignore the token
3754          !!!next-token;          !!!next-token;
3755          return;          return;
# Line 3117  sub _tree_construction_main ($) { Line 3766  sub _tree_construction_main ($) {
3766              last INSCOPE;              last INSCOPE;
3767            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3768              !!!cp ('t55');              !!!cp ('t55');
3769              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},              !!!parse-error (type => 'unmatched end tag',
3770                                text => $token->{tag_name},
3771                              token => $end_tag_token);                              token => $end_tag_token);
3772              ## Ignore the token              ## Ignore the token
3773              !!!next-token;              !!!next-token;
3774              return;              return;
3775            }            }
3776          } 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]}) {  
3777            !!!cp ('t56');            !!!cp ('t56');
3778            $in_scope = 0;            $in_scope = 0;
3779          }          }
3780        } # INSCOPE        } # INSCOPE
3781        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3782          !!!cp ('t57');          !!!cp ('t57');
3783          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},          !!!parse-error (type => 'unmatched end tag',
3784                            text => $token->{tag_name},
3785                          token => $end_tag_token);                          token => $end_tag_token);
3786          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3787          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
# Line 3141  sub _tree_construction_main ($) { Line 3789  sub _tree_construction_main ($) {
3789        }        }
3790        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3791          !!!cp ('t58');          !!!cp ('t58');
3792          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1],          !!!parse-error (type => 'not closed',
3793                            text => $self->{open_elements}->[-1]->[0]
3794                                ->manakai_local_name,
3795                          token => $end_tag_token);                          token => $end_tag_token);
3796        }        }
3797                
# Line 3150  sub _tree_construction_main ($) { Line 3800  sub _tree_construction_main ($) {
3800        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
3801        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3802          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3803          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
3804              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3805              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
3806               $scoping_category->{$node->[1]})) { ## Scoping is redundant, maybe               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
3807            !!!cp ('t59');            !!!cp ('t59');
3808            $furthest_block = $node;            $furthest_block = $node;
3809            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
# Line 3239  sub _tree_construction_main ($) { Line 3889  sub _tree_construction_main ($) {
3889        } # S7          } # S7  
3890                
3891        ## Step 8        ## Step 8
3892        if ({        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
            table => 1, tbody => 1, tfoot => 1, thead => 1, tr => 1,  
           }->{$common_ancestor_node->[1]}) {  
3893          my $foster_parent_element;          my $foster_parent_element;
3894          my $next_sibling;          my $next_sibling;
3895                           OE: for (reverse 0..$#{$self->{open_elements}}) {          OE: for (reverse 0..$#{$self->{open_elements}}) {
3896                             if ($self->{open_elements}->[$_]->[1] eq 'table') {            if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3897                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3898                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3899                                 !!!cp ('t65.1');                                 !!!cp ('t65.1');
# Line 3318  sub _tree_construction_main ($) { Line 3966  sub _tree_construction_main ($) {
3966    
3967    my $insert_to_foster = sub {    my $insert_to_foster = sub {
3968      my $child = shift;      my $child = shift;
3969      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]}) {  
3970        # MUST        # MUST
3971        my $foster_parent_element;        my $foster_parent_element;
3972        my $next_sibling;        my $next_sibling;
3973                           OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3974                             if ($self->{open_elements}->[$_]->[1] eq 'table') {          if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3975                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3976                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3977                                 !!!cp ('t70');                                 !!!cp ('t70');
# Line 3350  sub _tree_construction_main ($) { Line 3996  sub _tree_construction_main ($) {
3996      }      }
3997    }; # $insert_to_foster    }; # $insert_to_foster
3998    
3999    B: {    B: while (1) {
4000      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
4001        !!!cp ('t73');        !!!cp ('t73');
4002        !!!parse-error (type => 'DOCTYPE in the middle', token => $token);        !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
4003        ## Ignore the token        ## Ignore the token
4004        ## Stay in the phase        ## Stay in the phase
4005        !!!next-token;        !!!next-token;
4006        redo B;        next B;
4007      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
4008               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
4009        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4010          !!!cp ('t79');          !!!cp ('t79');
4011          !!!parse-error (type => 'after html:html', token => $token);          !!!parse-error (type => 'after html', text => 'html', token => $token);
4012          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
4013        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4014          !!!cp ('t80');          !!!cp ('t80');
4015          !!!parse-error (type => 'after html:html', token => $token);          !!!parse-error (type => 'after html', text => 'html', token => $token);
4016          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
4017        } else {        } else {
4018          !!!cp ('t81');          !!!cp ('t81');
# Line 3383  sub _tree_construction_main ($) { Line 4029  sub _tree_construction_main ($) {
4029               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
4030          }          }
4031        }        }
4032          !!!nack ('t84.1');
4033        !!!next-token;        !!!next-token;
4034        redo B;        next B;
4035      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
4036        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
4037        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
# Line 3398  sub _tree_construction_main ($) { Line 4045  sub _tree_construction_main ($) {
4045          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
4046        }        }
4047        !!!next-token;        !!!next-token;
4048        redo B;        next B;
4049      } elsif ($self->{insertion_mode} & HEAD_IMS) {      } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
4050          if ($token->{type} == CHARACTER_TOKEN) {
4051            !!!cp ('t87.1');
4052            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4053            !!!next-token;
4054            next B;
4055          } elsif ($token->{type} == START_TAG_TOKEN) {
4056            if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
4057                 $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
4058                not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
4059                ($token->{tag_name} eq 'svg' and
4060                 $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
4061              ## NOTE: "using the rules for secondary insertion mode"then"continue"
4062              !!!cp ('t87.2');
4063              #
4064            } elsif ({
4065                      b => 1, big => 1, blockquote => 1, body => 1, br => 1,
4066                      center => 1, code => 1, dd => 1, div => 1, dl => 1, dt => 1,
4067                      em => 1, embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1,
4068                      h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, i => 1,
4069                      img => 1, li => 1, listing => 1, menu => 1, meta => 1,
4070                      nobr => 1, ol => 1, p => 1, pre => 1, ruby => 1, s => 1,
4071                      small => 1, span => 1, strong => 1, strike => 1, sub => 1,
4072                      sup => 1, table => 1, tt => 1, u => 1, ul => 1, var => 1,
4073                     }->{$token->{tag_name}}) {
4074              !!!cp ('t87.2');
4075              !!!parse-error (type => 'not closed',
4076                              text => $self->{open_elements}->[-1]->[0]
4077                                  ->manakai_local_name,
4078                              token => $token);
4079    
4080              pop @{$self->{open_elements}}
4081                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4082    
4083              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4084              ## Reprocess.
4085              next B;
4086            } else {
4087              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
4088              my $tag_name = $token->{tag_name};
4089              if ($nsuri eq $SVG_NS) {
4090                $tag_name = {
4091                   altglyph => 'altGlyph',
4092                   altglyphdef => 'altGlyphDef',
4093                   altglyphitem => 'altGlyphItem',
4094                   animatecolor => 'animateColor',
4095                   animatemotion => 'animateMotion',
4096                   animatetransform => 'animateTransform',
4097                   clippath => 'clipPath',
4098                   feblend => 'feBlend',
4099                   fecolormatrix => 'feColorMatrix',
4100                   fecomponenttransfer => 'feComponentTransfer',
4101                   fecomposite => 'feComposite',
4102                   feconvolvematrix => 'feConvolveMatrix',
4103                   fediffuselighting => 'feDiffuseLighting',
4104                   fedisplacementmap => 'feDisplacementMap',
4105                   fedistantlight => 'feDistantLight',
4106                   feflood => 'feFlood',
4107                   fefunca => 'feFuncA',
4108                   fefuncb => 'feFuncB',
4109                   fefuncg => 'feFuncG',
4110                   fefuncr => 'feFuncR',
4111                   fegaussianblur => 'feGaussianBlur',
4112                   feimage => 'feImage',
4113                   femerge => 'feMerge',
4114                   femergenode => 'feMergeNode',
4115                   femorphology => 'feMorphology',
4116                   feoffset => 'feOffset',
4117                   fepointlight => 'fePointLight',
4118                   fespecularlighting => 'feSpecularLighting',
4119                   fespotlight => 'feSpotLight',
4120                   fetile => 'feTile',
4121                   feturbulence => 'feTurbulence',
4122                   foreignobject => 'foreignObject',
4123                   glyphref => 'glyphRef',
4124                   lineargradient => 'linearGradient',
4125                   radialgradient => 'radialGradient',
4126                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4127                   textpath => 'textPath',  
4128                }->{$tag_name} || $tag_name;
4129              }
4130    
4131              ## "adjust SVG attributes" (SVG only) - done in insert-element-f
4132    
4133              ## "adjust foreign attributes" - done in insert-element-f
4134    
4135              !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
4136    
4137              if ($self->{self_closing}) {
4138                pop @{$self->{open_elements}};
4139                !!!ack ('t87.3');
4140              } else {
4141                !!!cp ('t87.4');
4142              }
4143    
4144              !!!next-token;
4145              next B;
4146            }
4147          } elsif ($token->{type} == END_TAG_TOKEN) {
4148            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4149            !!!cp ('t87.5');
4150            #
4151          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4152            !!!cp ('t87.6');
4153            !!!parse-error (type => 'not closed',
4154                            text => $self->{open_elements}->[-1]->[0]
4155                                ->manakai_local_name,
4156                            token => $token);
4157    
4158            pop @{$self->{open_elements}}
4159                while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4160    
4161            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4162            ## Reprocess.
4163            next B;
4164          } else {
4165            die "$0: $token->{type}: Unknown token type";        
4166          }
4167        }
4168    
4169        if ($self->{insertion_mode} & HEAD_IMS) {
4170        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4171          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4172            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
# Line 3409  sub _tree_construction_main ($) { Line 4176  sub _tree_construction_main ($) {
4176              !!!cp ('t88.1');              !!!cp ('t88.1');
4177              ## Ignore the token.              ## Ignore the token.
4178              !!!next-token;              !!!next-token;
4179              redo B;              next B;
4180            }            }
4181            unless (length $token->{data}) {            unless (length $token->{data}) {
4182              !!!cp ('t88');              !!!cp ('t88');
4183              !!!next-token;              !!!next-token;
4184              redo B;              next B;
4185            }            }
4186          }          }
4187    
4188          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4189            !!!cp ('t89');            !!!cp ('t89');
4190            ## As if <head>            ## As if <head>
4191            !!!create-element ($self->{head_element}, 'head',, $token);            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4192            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4193            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            push @{$self->{open_elements}},
4194                  [$self->{head_element}, $el_category->{head}];
4195    
4196            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4197            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
# Line 3433  sub _tree_construction_main ($) { Line 4201  sub _tree_construction_main ($) {
4201            !!!cp ('t90');            !!!cp ('t90');
4202            ## As if </noscript>            ## As if </noscript>
4203            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4204            !!!parse-error (type => 'in noscript:#character', token => $token);            !!!parse-error (type => 'in noscript:#text', token => $token);
4205                        
4206            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4207            ## As if </head>            ## As if </head>
# Line 3449  sub _tree_construction_main ($) { Line 4217  sub _tree_construction_main ($) {
4217            !!!cp ('t92');            !!!cp ('t92');
4218          }          }
4219    
4220              ## "after head" insertion mode          ## "after head" insertion mode
4221              ## As if <body>          ## As if <body>
4222              !!!insert-element ('body',, $token);          !!!insert-element ('body',, $token);
4223              $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4224              ## reprocess          ## reprocess
4225              redo B;          next B;
4226            } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4227              if ($token->{tag_name} eq 'head') {          if ($token->{tag_name} eq 'head') {
4228                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4229                  !!!cp ('t93');              !!!cp ('t93');
4230                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes}, $token);              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4231                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              $self->{open_elements}->[-1]->[0]->append_child
4232                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];                  ($self->{head_element});
4233                  $self->{insertion_mode} = IN_HEAD_IM;              push @{$self->{open_elements}},
4234                  !!!next-token;                  [$self->{head_element}, $el_category->{head}];
4235                  redo B;              $self->{insertion_mode} = IN_HEAD_IM;
4236                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {              !!!nack ('t93.1');
4237                  !!!cp ('t94');              !!!next-token;
4238                  #              next B;
4239                } else {            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4240                  !!!cp ('t95');              !!!cp ('t93.2');
4241                  !!!parse-error (type => 'in head:head', token => $token); # or in head noscript              !!!parse-error (type => 'after head', text => 'head',
4242                  ## Ignore the token                              token => $token);
4243                  !!!next-token;              ## Ignore the token
4244                  redo B;              !!!nack ('t93.3');
4245                }              !!!next-token;
4246              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              next B;
4247                !!!cp ('t96');            } else {
4248                ## As if <head>              !!!cp ('t95');
4249                !!!create-element ($self->{head_element}, 'head',, $token);              !!!parse-error (type => 'in head:head',
4250                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                              token => $token); # or in head noscript
4251                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];              ## Ignore the token
4252                !!!nack ('t95.1');
4253                !!!next-token;
4254                next B;
4255              }
4256            } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4257              !!!cp ('t96');
4258              ## As if <head>
4259              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4260              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4261              push @{$self->{open_elements}},
4262                  [$self->{head_element}, $el_category->{head}];
4263    
4264                $self->{insertion_mode} = IN_HEAD_IM;            $self->{insertion_mode} = IN_HEAD_IM;
4265                ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4266              } else {          } else {
4267                !!!cp ('t97');            !!!cp ('t97');
4268              }          }
4269    
4270              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
4271                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4272                  !!!cp ('t98');                  !!!cp ('t98');
4273                  ## As if </noscript>                  ## As if </noscript>
4274                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4275                  !!!parse-error (type => 'in noscript:base', token => $token);                  !!!parse-error (type => 'in noscript', text => 'base',
4276                                    token => $token);
4277                                
4278                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4279                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
# Line 3504  sub _tree_construction_main ($) { Line 4284  sub _tree_construction_main ($) {
4284                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4285                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4286                  !!!cp ('t100');                  !!!cp ('t100');
4287                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4288                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4289                    push @{$self->{open_elements}},
4290                        [$self->{head_element}, $el_category->{head}];
4291                } else {                } else {
4292                  !!!cp ('t101');                  !!!cp ('t101');
4293                }                }
# Line 3513  sub _tree_construction_main ($) { Line 4295  sub _tree_construction_main ($) {
4295                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4296                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4297                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4298                  !!!nack ('t101.1');
4299                !!!next-token;                !!!next-token;
4300                redo B;                next B;
4301              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
4302                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4303                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4304                  !!!cp ('t102');                  !!!cp ('t102');
4305                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4306                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4307                    push @{$self->{open_elements}},
4308                        [$self->{head_element}, $el_category->{head}];
4309                } else {                } else {
4310                  !!!cp ('t103');                  !!!cp ('t103');
4311                }                }
# Line 3528  sub _tree_construction_main ($) { Line 4313  sub _tree_construction_main ($) {
4313                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4314                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4315                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4316                  !!!ack ('t103.1');
4317                !!!next-token;                !!!next-token;
4318                redo B;                next B;
4319              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
4320                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4321                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4322                  !!!cp ('t104');                  !!!cp ('t104');
4323                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4324                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4325                    push @{$self->{open_elements}},
4326                        [$self->{head_element}, $el_category->{head}];
4327                } else {                } else {
4328                  !!!cp ('t105');                  !!!cp ('t105');
4329                }                }
# Line 3543  sub _tree_construction_main ($) { Line 4331  sub _tree_construction_main ($) {
4331                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.
4332    
4333                unless ($self->{confident}) {                unless ($self->{confident}) {
4334                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) {
4335                    !!!cp ('t106');                    !!!cp ('t106');
4336                      ## NOTE: Whether the encoding is supported or not is handled
4337                      ## in the {change_encoding} callback.
4338                    $self->{change_encoding}                    $self->{change_encoding}
4339                        ->($self, $token->{attributes}->{charset}->{value},                        ->($self, $token->{attributes}->{charset}->{value},
4340                           $token);                           $token);
# Line 3554  sub _tree_construction_main ($) { Line 4344  sub _tree_construction_main ($) {
4344                                             $token->{attributes}->{charset}                                             $token->{attributes}->{charset}
4345                                                 ->{has_reference});                                                 ->{has_reference});
4346                  } elsif ($token->{attributes}->{content}) {                  } elsif ($token->{attributes}->{content}) {
                   ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
4347                    if ($token->{attributes}->{content}->{value}                    if ($token->{attributes}->{content}->{value}
4348                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                        =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4349                            [\x09-\x0D\x20]*=                            [\x09-\x0D\x20]*=
4350                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4351                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
4352                      !!!cp ('t107');                      !!!cp ('t107');
4353                        ## NOTE: Whether the encoding is supported or not is handled
4354                        ## in the {change_encoding} callback.
4355                      $self->{change_encoding}                      $self->{change_encoding}
4356                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4357                             $token);                             $token);
# Line 3591  sub _tree_construction_main ($) { Line 4382  sub _tree_construction_main ($) {
4382    
4383                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4384                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4385                  !!!ack ('t110.1');
4386                !!!next-token;                !!!next-token;
4387                redo B;                next B;
4388              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
4389                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4390                  !!!cp ('t111');                  !!!cp ('t111');
4391                  ## As if </noscript>                  ## As if </noscript>
4392                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4393                  !!!parse-error (type => 'in noscript:title', token => $token);                  !!!parse-error (type => 'in noscript', text => 'title',
4394                                    token => $token);
4395                                
4396                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4397                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4398                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4399                  !!!cp ('t112');                  !!!cp ('t112');
4400                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4401                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4402                    push @{$self->{open_elements}},
4403                        [$self->{head_element}, $el_category->{head}];
4404                } else {                } else {
4405                  !!!cp ('t113');                  !!!cp ('t113');
4406                }                }
# Line 3616  sub _tree_construction_main ($) { Line 4411  sub _tree_construction_main ($) {
4411                $parse_rcdata->(RCDATA_CONTENT_MODEL);                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4412                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4413                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4414                redo B;                next B;
4415              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style' or
4416                         $token->{tag_name} eq 'noframes') {
4417                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4418                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
4419                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4420                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4421                  !!!cp ('t114');                  !!!cp ('t114');
4422                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4423                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4424                    push @{$self->{open_elements}},
4425                        [$self->{head_element}, $el_category->{head}];
4426                } else {                } else {
4427                  !!!cp ('t115');                  !!!cp ('t115');
4428                }                }
4429                $parse_rcdata->(CDATA_CONTENT_MODEL);                $parse_rcdata->(CDATA_CONTENT_MODEL);
4430                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4431                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4432                redo B;                next B;
4433              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4434                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
4435                  !!!cp ('t116');                  !!!cp ('t116');
4436                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
4437                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4438                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4439                    !!!nack ('t116.1');
4440                  !!!next-token;                  !!!next-token;
4441                  redo B;                  next B;
4442                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4443                  !!!cp ('t117');                  !!!cp ('t117');
4444                  !!!parse-error (type => 'in noscript:noscript', token => $token);                  !!!parse-error (type => 'in noscript', text => 'noscript',
4445                                    token => $token);
4446                  ## Ignore the token                  ## Ignore the token
4447                    !!!nack ('t117.1');
4448                  !!!next-token;                  !!!next-token;
4449                  redo B;                  next B;
4450                } else {                } else {
4451                  !!!cp ('t118');                  !!!cp ('t118');
4452                  #                  #
# Line 3655  sub _tree_construction_main ($) { Line 4456  sub _tree_construction_main ($) {
4456                  !!!cp ('t119');                  !!!cp ('t119');
4457                  ## As if </noscript>                  ## As if </noscript>
4458                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4459                  !!!parse-error (type => 'in noscript:script', token => $token);                  !!!parse-error (type => 'in noscript', text => 'script',
4460                                    token => $token);
4461                                
4462                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4463                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4464                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4465                  !!!cp ('t120');                  !!!cp ('t120');
4466                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4467                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4468                    push @{$self->{open_elements}},
4469                        [$self->{head_element}, $el_category->{head}];
4470                } else {                } else {
4471                  !!!cp ('t121');                  !!!cp ('t121');
4472                }                }
# Line 3671  sub _tree_construction_main ($) { Line 4475  sub _tree_construction_main ($) {
4475                $script_start_tag->();                $script_start_tag->();
4476                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4477                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4478                redo B;                next B;
4479              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
4480                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
4481                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4482                  !!!cp ('t122');                  !!!cp ('t122');
4483                  ## As if </noscript>                  ## As if </noscript>
4484                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4485                  !!!parse-error (type => 'in noscript:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'in noscript',
4486                                    text => $token->{tag_name}, token => $token);
4487                                    
4488                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4489                  ## As if </head>                  ## As if </head>
# Line 3705  sub _tree_construction_main ($) { Line 4510  sub _tree_construction_main ($) {
4510                } else {                } else {
4511                  die "$0: tag name: $self->{tag_name}";                  die "$0: tag name: $self->{tag_name}";
4512                }                }
4513                  !!!nack ('t127.1');
4514                !!!next-token;                !!!next-token;
4515                redo B;                next B;
4516              } else {              } else {
4517                !!!cp ('t128');                !!!cp ('t128');
4518                #                #
# Line 3716  sub _tree_construction_main ($) { Line 4522  sub _tree_construction_main ($) {
4522                !!!cp ('t129');                !!!cp ('t129');
4523                ## As if </noscript>                ## As if </noscript>
4524                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4525                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'in noscript:/',
4526                                  text => $token->{tag_name}, token => $token);
4527                                
4528                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4529                ## As if </head>                ## As if </head>
# Line 3738  sub _tree_construction_main ($) { Line 4545  sub _tree_construction_main ($) {
4545              !!!insert-element ('body',, $token);              !!!insert-element ('body',, $token);
4546              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4547              ## reprocess              ## reprocess
4548              redo B;              !!!ack-later;
4549                next B;
4550            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
4551              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
4552                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4553                  !!!cp ('t132');                  !!!cp ('t132');
4554                  ## As if <head>                  ## As if <head>
4555                  !!!create-element ($self->{head_element}, 'head',, $token);                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4556                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4557                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4558                        [$self->{head_element}, $el_category->{head}];
4559    
4560                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4561                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4562                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4563                  !!!next-token;                  !!!next-token;
4564                  redo B;                  next B;
4565                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4566                  !!!cp ('t133');                  !!!cp ('t133');
4567                  ## As if </noscript>                  ## As if </noscript>
4568                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4569                  !!!parse-error (type => 'in noscript:/head', token => $token);                  !!!parse-error (type => 'in noscript:/',
4570                                    text => 'head', token => $token);
4571                                    
4572                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4573                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4574                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4575                  !!!next-token;                  !!!next-token;
4576                  redo B;                  next B;
4577                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4578                  !!!cp ('t134');                  !!!cp ('t134');
4579                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4580                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4581                  !!!next-token;                  !!!next-token;
4582                  redo B;                  next B;
4583                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4584                    !!!cp ('t134.1');
4585                    !!!parse-error (type => 'unmatched end tag', text => 'head',
4586                                    token => $token);
4587                    ## Ignore the token
4588                    !!!next-token;
4589                    next B;
4590                } else {                } else {
4591                  !!!cp ('t135');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
                 #  
4592                }                }
4593              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4594                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
# Line 3780  sub _tree_construction_main ($) { Line 4596  sub _tree_construction_main ($) {
4596                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4597                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4598                  !!!next-token;                  !!!next-token;
4599                  redo B;                  next B;
4600                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
4601                           $self->{insertion_mode} == AFTER_HEAD_IM) {
4602                  !!!cp ('t137');                  !!!cp ('t137');
4603                  !!!parse-error (type => 'unmatched end tag:noscript', token => $token);                  !!!parse-error (type => 'unmatched end tag',
4604                                    text => 'noscript', token => $token);
4605                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
4606                  !!!next-token;                  !!!next-token;
4607                  redo B;                  next B;
4608                } else {                } else {
4609                  !!!cp ('t138');                  !!!cp ('t138');
4610                  #                  #
# Line 3794  sub _tree_construction_main ($) { Line 4612  sub _tree_construction_main ($) {
4612              } elsif ({              } elsif ({
4613                        body => 1, html => 1,                        body => 1, html => 1,
4614                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4615                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM or
4616                  !!!cp ('t139');                    $self->{insertion_mode} == IN_HEAD_IM or
4617                  ## 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) {  
4618                  !!!cp ('t140');                  !!!cp ('t140');
4619                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
4620                                    text => $token->{tag_name}, token => $token);
4621                  ## Ignore the token                  ## Ignore the token
4622                  !!!next-token;                  !!!next-token;
4623                  redo B;                  next B;
4624                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4625                    !!!cp ('t140.1');
4626                    !!!parse-error (type => 'unmatched end tag',
4627                                    text => $token->{tag_name}, token => $token);
4628                    ## Ignore the token
4629                    !!!next-token;
4630                    next B;
4631                } else {                } else {
4632                  !!!cp ('t141');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
4633                }                }
4634                              } elsif ($token->{tag_name} eq 'p') {
4635                #                !!!cp ('t142');
4636              } elsif ({                !!!parse-error (type => 'unmatched end tag',
4637                        p => 1, br => 1,                                text => $token->{tag_name}, token => $token);
4638                       }->{$token->{tag_name}}) {                ## Ignore the token
4639                  !!!next-token;
4640                  next B;
4641                } elsif ($token->{tag_name} eq 'br') {
4642                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4643                  !!!cp ('t142');                  !!!cp ('t142.2');
4644                  ## As if <head>                  ## (before head) as if <head>, (in head) as if </head>
4645                  !!!create-element ($self->{head_element}, 'head',, $token);                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4646                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4647                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  $self->{insertion_mode} = AFTER_HEAD_IM;
4648      
4649                    ## Reprocess in the "after head" insertion mode...
4650                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4651                    !!!cp ('t143.2');
4652                    ## As if </head>
4653                    pop @{$self->{open_elements}};
4654                    $self->{insertion_mode} = AFTER_HEAD_IM;
4655      
4656                    ## Reprocess in the "after head" insertion mode...
4657                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4658                    !!!cp ('t143.3');
4659                    ## ISSUE: Two parse errors for <head><noscript></br>
4660                    !!!parse-error (type => 'unmatched end tag',
4661                                    text => 'br', token => $token);
4662                    ## As if </noscript>
4663                    pop @{$self->{open_elements}};
4664                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4665    
4666                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4667                } else {                  ## As if </head>
4668                  !!!cp ('t143');                  pop @{$self->{open_elements}};
4669                }                  $self->{insertion_mode} = AFTER_HEAD_IM;
4670    
4671                #                  ## Reprocess in the "after head" insertion mode...
4672              } else {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4673                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                  !!!cp ('t143.4');
                 !!!cp ('t144');  
4674                  #                  #
4675                } else {                } else {
4676                  !!!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;  
4677                }                }
4678    
4679                  ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
4680                  !!!parse-error (type => 'unmatched end tag',
4681                                  text => 'br', token => $token);
4682                  ## Ignore the token
4683                  !!!next-token;
4684                  next B;
4685                } else {
4686                  !!!cp ('t145');
4687                  !!!parse-error (type => 'unmatched end tag',
4688                                  text => $token->{tag_name}, token => $token);
4689                  ## Ignore the token
4690                  !!!next-token;
4691                  next B;
4692              }              }
4693    
4694              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4695                !!!cp ('t146');                !!!cp ('t146');
4696                ## As if </noscript>                ## As if </noscript>
4697                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4698                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'in noscript:/',
4699                                  text => $token->{tag_name}, token => $token);
4700                                
4701                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4702                ## As if </head>                ## As if </head>
# Line 3864  sub _tree_construction_main ($) { Line 4712  sub _tree_construction_main ($) {
4712              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4713  ## ISSUE: This case cannot be reached?  ## ISSUE: This case cannot be reached?
4714                !!!cp ('t148');                !!!cp ('t148');
4715                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag',
4716                                  text => $token->{tag_name}, token => $token);
4717                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
4718                !!!next-token;                !!!next-token;
4719                redo B;                next B;
4720              } else {              } else {
4721                !!!cp ('t149');                !!!cp ('t149');
4722              }              }
# Line 3877  sub _tree_construction_main ($) { Line 4726  sub _tree_construction_main ($) {
4726              !!!insert-element ('body',, $token);              !!!insert-element ('body',, $token);
4727              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4728              ## reprocess              ## reprocess
4729              redo B;              next B;
4730        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4731          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4732            !!!cp ('t149.1');            !!!cp ('t149.1');
4733    
4734            ## NOTE: As if <head>            ## NOTE: As if <head>
4735            !!!create-element ($self->{head_element}, 'head',, $token);            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4736            $self->{open_elements}->[-1]->[0]->append_child            $self->{open_elements}->[-1]->[0]->append_child
4737                ($self->{head_element});                ($self->{head_element});
4738            #push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            #push @{$self->{open_elements}},
4739              #    [$self->{head_element}, $el_category->{head}];
4740            #$self->{insertion_mode} = IN_HEAD_IM;            #$self->{insertion_mode} = IN_HEAD_IM;
4741            ## NOTE: Reprocess.            ## NOTE: Reprocess.
4742    
# Line 3930  sub _tree_construction_main ($) { Line 4780  sub _tree_construction_main ($) {
4780          !!!insert-element ('body',, $token);          !!!insert-element ('body',, $token);
4781          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4782          ## NOTE: Reprocess.          ## NOTE: Reprocess.
4783          redo B;          next B;
4784        } else {        } else {
4785          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
4786        }        }
# Line 3945  sub _tree_construction_main ($) { Line 4795  sub _tree_construction_main ($) {
4795              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4796    
4797              !!!next-token;              !!!next-token;
4798              redo B;              next B;
4799            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
4800              if ({              if ({
4801                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
# Line 3955  sub _tree_construction_main ($) { Line 4805  sub _tree_construction_main ($) {
4805                  ## have an element in table scope                  ## have an element in table scope
4806                  for (reverse 0..$#{$self->{open_elements}}) {                  for (reverse 0..$#{$self->{open_elements}}) {
4807                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4808                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] & TABLE_CELL_EL) {
4809                      !!!cp ('t151');                      !!!cp ('t151');
4810    
4811                      ## Close the cell                      ## Close the cell
4812                      !!!back-token; # <?>                      !!!back-token; # <x>
4813                      $token = {type => END_TAG_TOKEN, tag_name => $node->[1],                      $token = {type => END_TAG_TOKEN,
4814                                  tag_name => $node->[0]->manakai_local_name,
4815                                line => $token->{line},                                line => $token->{line},
4816                                column => $token->{column}};                                column => $token->{column}};
4817                      redo B;                      next B;
4818                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
4819                      !!!cp ('t152');                      !!!cp ('t152');
4820                      ## ISSUE: This case can never be reached, maybe.                      ## ISSUE: This case can never be reached, maybe.
4821                      last;                      last;
# Line 3975  sub _tree_construction_main ($) { Line 4824  sub _tree_construction_main ($) {
4824    
4825                  !!!cp ('t153');                  !!!cp ('t153');
4826                  !!!parse-error (type => 'start tag not allowed',                  !!!parse-error (type => 'start tag not allowed',
4827                      value => $token->{tag_name}, token => $token);                      text => $token->{tag_name}, token => $token);
4828                  ## Ignore the token                  ## Ignore the token
4829                    !!!nack ('t153.1');
4830                  !!!next-token;                  !!!next-token;
4831                  redo B;                  next B;
4832                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4833                  !!!parse-error (type => 'not closed:caption', token => $token);                  !!!parse-error (type => 'not closed', text => 'caption',
4834                                    token => $token);
4835                                    
4836                  ## NOTE: As if </caption>.                  ## NOTE: As if </caption>.
4837                  ## have a table element in table scope                  ## have a table element in table scope
# Line 3988  sub _tree_construction_main ($) { Line 4839  sub _tree_construction_main ($) {
4839                  INSCOPE: {                  INSCOPE: {
4840                    for (reverse 0..$#{$self->{open_elements}}) {                    for (reverse 0..$#{$self->{open_elements}}) {
4841                      my $node = $self->{open_elements}->[$_];                      my $node = $self->{open_elements}->[$_];
4842                      if ($node->[1] eq 'caption') {                      if ($node->[1] & CAPTION_EL) {
4843                        !!!cp ('t155');                        !!!cp ('t155');
4844                        $i = $_;                        $i = $_;
4845                        last INSCOPE;                        last INSCOPE;
4846                      } elsif ({                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
                               table => 1, html => 1,  
                              }->{$node->[1]}) {  
4847                        !!!cp ('t156');                        !!!cp ('t156');
4848                        last;                        last;
4849                      }                      }
# Line 4002  sub _tree_construction_main ($) { Line 4851  sub _tree_construction_main ($) {
4851    
4852                    !!!cp ('t157');                    !!!cp ('t157');
4853                    !!!parse-error (type => 'start tag not allowed',                    !!!parse-error (type => 'start tag not allowed',
4854                                    value => $token->{tag_name}, token => $token);                                    text => $token->{tag_name}, token => $token);
4855                    ## Ignore the token                    ## Ignore the token
4856                      !!!nack ('t157.1');
4857                    !!!next-token;                    !!!next-token;
4858                    redo B;                    next B;
4859                  } # INSCOPE                  } # INSCOPE
4860                                    
4861                  ## generate implied end tags                  ## generate implied end tags
4862                  while ({                  while ($self->{open_elements}->[-1]->[1]
4863                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4864                    !!!cp ('t158');                    !!!cp ('t158');
4865                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4866                  }                  }
4867    
4868                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4869                    !!!cp ('t159');                    !!!cp ('t159');
4870                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                    !!!parse-error (type => 'not closed',
4871                                      text => $self->{open_elements}->[-1]->[0]
4872                                          ->manakai_local_name,
4873                                      token => $token);
4874                  } else {                  } else {
4875                    !!!cp ('t160');                    !!!cp ('t160');
4876                  }                  }
# Line 4030  sub _tree_construction_main ($) { Line 4882  sub _tree_construction_main ($) {
4882                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4883                                    
4884                  ## reprocess                  ## reprocess
4885                  redo B;                  !!!ack-later;
4886                    next B;
4887                } else {                } else {
4888                  !!!cp ('t161');                  !!!cp ('t161');
4889                  #                  #
# Line 4046  sub _tree_construction_main ($) { Line 4899  sub _tree_construction_main ($) {
4899                  my $i;                  my $i;
4900                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4901                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4902                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4903                      !!!cp ('t163');                      !!!cp ('t163');
4904                      $i = $_;                      $i = $_;
4905                      last INSCOPE;                      last INSCOPE;
4906                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
4907                      !!!cp ('t164');                      !!!cp ('t164');
4908                      last INSCOPE;                      last INSCOPE;
4909                    }                    }
4910                  } # INSCOPE                  } # INSCOPE
4911                    unless (defined $i) {                    unless (defined $i) {
4912                      !!!cp ('t165');                      !!!cp ('t165');
4913                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                      !!!parse-error (type => 'unmatched end tag',
4914                                        text => $token->{tag_name},
4915                                        token => $token);
4916                      ## Ignore the token                      ## Ignore the token
4917                      !!!next-token;                      !!!next-token;
4918                      redo B;                      next B;
4919                    }                    }
4920                                    
4921                  ## generate implied end tags                  ## generate implied end tags
4922                  while ({                  while ($self->{open_elements}->[-1]->[1]
4923                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4924                    !!!cp ('t166');                    !!!cp ('t166');
4925                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4926                  }                  }
4927    
4928                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
4929                            ne $token->{tag_name}) {
4930                    !!!cp ('t167');                    !!!cp ('t167');
4931                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                    !!!parse-error (type => 'not closed',
4932                                      text => $self->{open_elements}->[-1]->[0]
4933                                          ->manakai_local_name,
4934                                      token => $token);
4935                  } else {                  } else {
4936                    !!!cp ('t168');                    !!!cp ('t168');
4937                  }                  }
# Line 4087  sub _tree_construction_main ($) { Line 4943  sub _tree_construction_main ($) {
4943                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
4944                                    
4945                  !!!next-token;                  !!!next-token;
4946                  redo B;                  next B;
4947                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4948                  !!!cp ('t169');                  !!!cp ('t169');
4949                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
4950                                    text => $token->{tag_name}, token => $token);
4951                  ## Ignore the token                  ## Ignore the token
4952                  !!!next-token;                  !!!next-token;
4953                  redo B;                  next B;
4954                } else {                } else {
4955                  !!!cp ('t170');                  !!!cp ('t170');
4956                  #                  #
# Line 4105  sub _tree_construction_main ($) { Line 4962  sub _tree_construction_main ($) {
4962                  INSCOPE: {                  INSCOPE: {
4963                    for (reverse 0..$#{$self->{open_elements}}) {                    for (reverse 0..$#{$self->{open_elements}}) {
4964                      my $node = $self->{open_elements}->[$_];                      my $node = $self->{open_elements}->[$_];
4965                      if ($node->[1] eq $token->{tag_name}) {                      if ($node->[1] & CAPTION_EL) {
4966                        !!!cp ('t171');                        !!!cp ('t171');
4967                        $i = $_;                        $i = $_;
4968                        last INSCOPE;                        last INSCOPE;
4969                      } elsif ({                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
                               table => 1, html => 1,  
                              }->{$node->[1]}) {  
4970                        !!!cp ('t172');                        !!!cp ('t172');
4971                        last;                        last;
4972                      }                      }
# Line 4119  sub _tree_construction_main ($) { Line 4974  sub _tree_construction_main ($) {
4974    
4975                    !!!cp ('t173');                    !!!cp ('t173');
4976                    !!!parse-error (type => 'unmatched end tag',                    !!!parse-error (type => 'unmatched end tag',
4977                                    value => $token->{tag_name}, token => $token);                                    text => $token->{tag_name}, token => $token);
4978                    ## Ignore the token                    ## Ignore the token
4979                    !!!next-token;                    !!!next-token;
4980                    redo B;                    next B;
4981                  } # INSCOPE                  } # INSCOPE
4982                                    
4983                  ## generate implied end tags                  ## generate implied end tags
4984                  while ({                  while ($self->{open_elements}->[-1]->[1]
4985                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4986                    !!!cp ('t174');                    !!!cp ('t174');
4987                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4988                  }                  }
4989                                    
4990                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4991                    !!!cp ('t175');                    !!!cp ('t175');
4992                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                    !!!parse-error (type => 'not closed',
4993                                      text => $self->{open_elements}->[-1]->[0]
4994                                          ->manakai_local_name,
4995                                      token => $token);
4996                  } else {                  } else {
4997                    !!!cp ('t176');                    !!!cp ('t176');
4998                  }                  }
# Line 4147  sub _tree_construction_main ($) { Line 5004  sub _tree_construction_main ($) {
5004                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5005                                    
5006                  !!!next-token;                  !!!next-token;
5007                  redo B;                  next B;
5008                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
5009                  !!!cp ('t177');                  !!!cp ('t177');
5010                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
5011                                    text => $token->{tag_name}, token => $token);
5012                  ## Ignore the token                  ## Ignore the token
5013                  !!!next-token;                  !!!next-token;
5014                  redo B;                  next B;
5015                } else {                } else {
5016                  !!!cp ('t178');                  !!!cp ('t178');
5017                  #                  #
# Line 4169  sub _tree_construction_main ($) { Line 5027  sub _tree_construction_main ($) {
5027                INSCOPE: {                INSCOPE: {
5028                  for (reverse 0..$#{$self->{open_elements}}) {                  for (reverse 0..$#{$self->{open_elements}}) {
5029                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5030                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5031                      !!!cp ('t179');                      !!!cp ('t179');
5032                      $i = $_;                      $i = $_;
5033    
5034                      ## Close the cell                      ## Close the cell
5035                      !!!back-token; # </?>                      !!!back-token; # </x>
5036                      $token = {type => END_TAG_TOKEN, tag_name => $tn,                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
5037                                line => $token->{line},                                line => $token->{line},
5038                                column => $token->{column}};                                column => $token->{column}};
5039                      redo B;                      next B;
5040                    } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {                    } elsif ($node->[1] & TABLE_CELL_EL) {
5041                      !!!cp ('t180');                      !!!cp ('t180');
5042                      $tn = $node->[1];                      $tn = $node->[0]->manakai_local_name;
5043                      ## NOTE: There is exactly one |td| or |th| element                      ## NOTE: There is exactly one |td| or |th| element
5044                      ## in scope in the stack of open elements by definition.                      ## in scope in the stack of open elements by definition.
5045                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5046                      ## ISSUE: Can this be reached?                      ## ISSUE: Can this be reached?
5047                      !!!cp ('t181');                      !!!cp ('t181');
5048                      last;                      last;
# Line 4195  sub _tree_construction_main ($) { Line 5051  sub _tree_construction_main ($) {
5051    
5052                  !!!cp ('t182');                  !!!cp ('t182');
5053                  !!!parse-error (type => 'unmatched end tag',                  !!!parse-error (type => 'unmatched end tag',
5054                      value => $token->{tag_name}, token => $token);                      text => $token->{tag_name}, token => $token);
5055                  ## Ignore the token                  ## Ignore the token
5056                  !!!next-token;                  !!!next-token;
5057                  redo B;                  next B;
5058                } # INSCOPE                } # INSCOPE
5059              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
5060                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5061                !!!parse-error (type => 'not closed:caption', token => $token);                !!!parse-error (type => 'not closed', text => 'caption',
5062                                  token => $token);
5063    
5064                ## As if </caption>                ## As if </caption>
5065                ## have a table element in table scope                ## have a table element in table scope
5066                my $i;                my $i;
5067                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5068                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5069                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
5070                    !!!cp ('t184');                    !!!cp ('t184');
5071                    $i = $_;                    $i = $_;
5072                    last INSCOPE;                    last INSCOPE;
5073                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5074                    !!!cp ('t185');                    !!!cp ('t185');
5075                    last INSCOPE;                    last INSCOPE;
5076                  }                  }
5077                } # INSCOPE                } # INSCOPE
5078                unless (defined $i) {                unless (defined $i) {
5079                  !!!cp ('t186');                  !!!cp ('t186');
5080                  !!!parse-error (type => 'unmatched end tag:caption', token => $token);                  !!!parse-error (type => 'unmatched end tag',
5081                                    text => 'caption', token => $token);
5082                  ## Ignore the token                  ## Ignore the token
5083                  !!!next-token;                  !!!next-token;
5084                  redo B;                  next B;
5085                }                }
5086                                
5087                ## generate implied end tags                ## generate implied end tags
5088                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
5089                  !!!cp ('t187');                  !!!cp ('t187');
5090                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5091                }                }
5092    
5093                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5094                  !!!cp ('t188');                  !!!cp ('t188');
5095                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                  !!!parse-error (type => 'not closed',
5096                                    text => $self->{open_elements}->[-1]->[0]
5097                                        ->manakai_local_name,
5098                                    token => $token);
5099                } else {                } else {
5100                  !!!cp ('t189');                  !!!cp ('t189');
5101                }                }
# Line 4250  sub _tree_construction_main ($) { Line 5107  sub _tree_construction_main ($) {
5107                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5108    
5109                ## reprocess                ## reprocess
5110                redo B;                next B;
5111              } elsif ({              } elsif ({
5112                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
5113                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5114                if ($self->{insertion_mode} & BODY_TABLE_IMS) {                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
5115                  !!!cp ('t190');                  !!!cp ('t190');
5116                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
5117                                    text => $token->{tag_name}, token => $token);
5118                  ## Ignore the token                  ## Ignore the token
5119                  !!!next-token;                  !!!next-token;
5120                  redo B;                  next B;
5121                } else {                } else {
5122                  !!!cp ('t191');                  !!!cp ('t191');
5123                  #                  #
# Line 4270  sub _tree_construction_main ($) { Line 5128  sub _tree_construction_main ($) {
5128                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
5129                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5130                !!!cp ('t192');                !!!cp ('t192');
5131                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag',
5132                                  text => $token->{tag_name}, token => $token);
5133                ## Ignore the token                ## Ignore the token
5134                !!!next-token;                !!!next-token;
5135                redo B;                next B;
5136              } else {              } else {
5137                !!!cp ('t193');                !!!cp ('t193');
5138                #                #
5139              }              }
5140        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5141          for my $entry (@{$self->{open_elements}}) {          for my $entry (@{$self->{open_elements}}) {
5142            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]}) {  
5143              !!!cp ('t75');              !!!cp ('t75');
5144              !!!parse-error (type => 'in body:#eof', token => $token);              !!!parse-error (type => 'in body:#eof', token => $token);
5145              last;              last;
# Line 4307  sub _tree_construction_main ($) { Line 5163  sub _tree_construction_main ($) {
5163            unless (length $token->{data}) {            unless (length $token->{data}) {
5164              !!!cp ('t194');              !!!cp ('t194');
5165              !!!next-token;              !!!next-token;
5166              redo B;              next B;
5167            } else {            } else {
5168              !!!cp ('t195');              !!!cp ('t195');
5169            }            }
5170          }          }
5171    
5172              !!!parse-error (type => 'in table:#character', token => $token);          !!!parse-error (type => 'in table:#text', token => $token);
5173    
5174              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
5175              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
# Line 4321  sub _tree_construction_main ($) { Line 5177  sub _tree_construction_main ($) {
5177              ## result in a new Text node.              ## result in a new Text node.
5178              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
5179                            
5180              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]}) {  
5181                # MUST                # MUST
5182                my $foster_parent_element;                my $foster_parent_element;
5183                my $next_sibling;                my $next_sibling;
5184                my $prev_sibling;                my $prev_sibling;
5185                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
5186                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
5187                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
5188                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
5189                      !!!cp ('t196');                      !!!cp ('t196');
# Line 4365  sub _tree_construction_main ($) { Line 5218  sub _tree_construction_main ($) {
5218          }          }
5219                            
5220          !!!next-token;          !!!next-token;
5221          redo B;          next B;
5222        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5223              if ({          if ({
5224                   tr => ($self->{insertion_mode} != IN_ROW_IM),               tr => ($self->{insertion_mode} != IN_ROW_IM),
5225                   th => 1, td => 1,               th => 1, td => 1,
5226                  }->{$token->{tag_name}}) {              }->{$token->{tag_name}}) {
5227                if ($self->{insertion_mode} == IN_TABLE_IM) {            if ($self->{insertion_mode} == IN_TABLE_IM) {
5228                  ## Clear back to table context              ## Clear back to table context
5229                  while ($self->{open_elements}->[-1]->[1] ne 'table' and              while (not ($self->{open_elements}->[-1]->[1]
5230                         $self->{open_elements}->[-1]->[1] ne 'html') {                              & TABLE_SCOPING_EL)) {
5231                    !!!cp ('t201');                !!!cp ('t201');
5232                    pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5233                  }              }
5234                                
5235                  !!!insert-element ('tbody',, $token);              !!!insert-element ('tbody',, $token);
5236                  $self->{insertion_mode} = IN_TABLE_BODY_IM;              $self->{insertion_mode} = IN_TABLE_BODY_IM;
5237                  ## reprocess in the "in table body" insertion mode...              ## reprocess in the "in table body" insertion mode...
5238                }            }
5239              
5240                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {            if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5241                  unless ($token->{tag_name} eq 'tr') {              unless ($token->{tag_name} eq 'tr') {
5242                    !!!cp ('t202');                !!!cp ('t202');
5243                    !!!parse-error (type => 'missing start tag:tr', token => $token);                !!!parse-error (type => 'missing start tag:tr', token => $token);
5244                  }              }
5245                                    
5246                  ## Clear back to table body context              ## Clear back to table body context
5247                  while (not {              while (not ($self->{open_elements}->[-1]->[1]
5248                    tbody => 1, tfoot => 1, thead => 1, html => 1,                              & TABLE_ROWS_SCOPING_EL)) {
5249                  }->{$self->{open_elements}->[-1]->[1]}) {                !!!cp ('t203');
5250                    !!!cp ('t203');                ## ISSUE: Can this case be reached?
5251                    ## ISSUE: Can this case be reached?                pop @{$self->{open_elements}};
5252                    pop @{$self->{open_elements}};              }
                 }  
5253                                    
5254                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
5255                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5256                    !!!cp ('t204');                    !!!cp ('t204');
5257                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5258                      !!!nack ('t204');
5259                    !!!next-token;                    !!!next-token;
5260                    redo B;                    next B;
5261                  } else {                  } else {
5262                    !!!cp ('t205');                    !!!cp ('t205');
5263                    !!!insert-element ('tr',, $token);                    !!!insert-element ('tr',, $token);
# Line 4415  sub _tree_construction_main ($) { Line 5268  sub _tree_construction_main ($) {
5268                }                }
5269    
5270                ## Clear back to table row context                ## Clear back to table row context
5271                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5272                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5273                  !!!cp ('t207');                  !!!cp ('t207');
5274                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5275                }                }
# Line 4427  sub _tree_construction_main ($) { Line 5279  sub _tree_construction_main ($) {
5279    
5280                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
5281                                
5282                  !!!nack ('t207.1');
5283                !!!next-token;                !!!next-token;
5284                redo B;                next B;
5285              } elsif ({              } elsif ({
5286                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5287                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
# Line 4440  sub _tree_construction_main ($) { Line 5293  sub _tree_construction_main ($) {
5293                  my $i;                  my $i;
5294                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5295                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5296                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5297                      !!!cp ('t208');                      !!!cp ('t208');
5298                      $i = $_;                      $i = $_;
5299                      last INSCOPE;                      last INSCOPE;
5300                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             html => 1,  
   
                             ## NOTE: This element does not appear here, maybe.  
                             table => 1,  
                            }->{$node->[1]}) {  
5301                      !!!cp ('t209');                      !!!cp ('t209');
5302                      last INSCOPE;                      last INSCOPE;
5303                    }                    }
5304                  } # INSCOPE                  } # INSCOPE
5305                  unless (defined $i) {                  unless (defined $i) {
5306                   !!!cp ('t210');                    !!!cp ('t210');
5307  ## TODO: This type is wrong.  ## TODO: This type is wrong.
5308                   !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);                    !!!parse-error (type => 'unmacthed end tag',
5309                                      text => $token->{tag_name}, token => $token);
5310                    ## Ignore the token                    ## Ignore the token
5311                      !!!nack ('t210.1');
5312                    !!!next-token;                    !!!next-token;
5313                    redo B;                    next B;
5314                  }                  }
5315                                    
5316                  ## Clear back to table row context                  ## Clear back to table row context
5317                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5318                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5319                    !!!cp ('t211');                    !!!cp ('t211');
5320                    ## ISSUE: Can this case be reached?                    ## ISSUE: Can this case be reached?
5321                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4477  sub _tree_construction_main ($) { Line 5326  sub _tree_construction_main ($) {
5326                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5327                    !!!cp ('t212');                    !!!cp ('t212');
5328                    ## reprocess                    ## reprocess
5329                    redo B;                    !!!ack-later;
5330                      next B;
5331                  } else {                  } else {
5332                    !!!cp ('t213');                    !!!cp ('t213');
5333                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
# Line 4489  sub _tree_construction_main ($) { Line 5339  sub _tree_construction_main ($) {
5339                  my $i;                  my $i;
5340                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5341                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5342                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5343                      !!!cp ('t214');                      !!!cp ('t214');
5344                      $i = $_;                      $i = $_;
5345                      last INSCOPE;                      last INSCOPE;
5346                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5347                      !!!cp ('t215');                      !!!cp ('t215');
5348                      last INSCOPE;                      last INSCOPE;
5349                    }                    }
5350                  } # INSCOPE                  } # INSCOPE
5351                  unless (defined $i) {                  unless (defined $i) {
5352                    !!!cp ('t216');                    !!!cp ('t216');
5353  ## TODO: This erorr type ios wrong.  ## TODO: This erorr type is wrong.
5354                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                    !!!parse-error (type => 'unmatched end tag',
5355                                      text => $token->{tag_name}, token => $token);
5356                    ## Ignore the token                    ## Ignore the token
5357                      !!!nack ('t216.1');
5358                    !!!next-token;                    !!!next-token;
5359                    redo B;                    next B;
5360                  }                  }
5361    
5362                  ## Clear back to table body context                  ## Clear back to table body context
5363                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5364                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5365                    !!!cp ('t217');                    !!!cp ('t217');
5366                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5367                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4536  sub _tree_construction_main ($) { Line 5383  sub _tree_construction_main ($) {
5383    
5384                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
5385                  ## Clear back to table context                  ## Clear back to table context
5386                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5387                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5388                    !!!cp ('t219');                    !!!cp ('t219');
5389                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5390                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4546  sub _tree_construction_main ($) { Line 5393  sub _tree_construction_main ($) {
5393                  !!!insert-element ('colgroup',, $token);                  !!!insert-element ('colgroup',, $token);
5394                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5395                  ## reprocess                  ## reprocess
5396                  redo B;                  !!!ack-later;
5397                    next B;
5398                } elsif ({                } elsif ({
5399                          caption => 1,                          caption => 1,
5400                          colgroup => 1,                          colgroup => 1,
5401                          tbody => 1, tfoot => 1, thead => 1,                          tbody => 1, tfoot => 1, thead => 1,
5402                         }->{$token->{tag_name}}) {                         }->{$token->{tag_name}}) {
5403                  ## Clear back to table context                  ## Clear back to table context
5404                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5405                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5406                    !!!cp ('t220');                    !!!cp ('t220');
5407                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5408                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4572  sub _tree_construction_main ($) { Line 5420  sub _tree_construction_main ($) {
5420                                             thead => IN_TABLE_BODY_IM,                                             thead => IN_TABLE_BODY_IM,
5421                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
5422                  !!!next-token;                  !!!next-token;
5423                  redo B;                  !!!nack ('t220.1');
5424                    next B;
5425                } else {                } else {
5426                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
5427                }                }
5428              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5429                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                !!!parse-error (type => 'not closed',
5430                                  text => $self->{open_elements}->[-1]->[0]
5431                                      ->manakai_local_name,
5432                                  token => $token);
5433    
5434                ## As if </table>                ## As if </table>
5435                ## have a table element in table scope                ## have a table element in table scope
5436                my $i;                my $i;
5437                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5438                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5439                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5440                    !!!cp ('t221');                    !!!cp ('t221');
5441                    $i = $_;                    $i = $_;
5442                    last INSCOPE;                    last INSCOPE;
5443                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           #table => 1,  
                           html => 1,  
                          }->{$node->[1]}) {  
5444                    !!!cp ('t222');                    !!!cp ('t222');
5445                    last INSCOPE;                    last INSCOPE;
5446                  }                  }
# Line 4599  sub _tree_construction_main ($) { Line 5448  sub _tree_construction_main ($) {
5448                unless (defined $i) {                unless (defined $i) {
5449                  !!!cp ('t223');                  !!!cp ('t223');
5450  ## TODO: The following is wrong, maybe.  ## TODO: The following is wrong, maybe.
5451                  !!!parse-error (type => 'unmatched end tag:table', token => $token);                  !!!parse-error (type => 'unmatched end tag', text => 'table',
5452                                    token => $token);
5453                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5454                    !!!nack ('t223.1');
5455                  !!!next-token;                  !!!next-token;
5456                  redo B;                  next B;
5457                }                }
5458                                
5459  ## TODO: Followings are removed from the latest spec.  ## TODO: Followings are removed from the latest spec.
5460                ## generate implied end tags                ## generate implied end tags
5461                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
5462                  !!!cp ('t224');                  !!!cp ('t224');
5463                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5464                }                }
5465    
5466                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5467                  !!!cp ('t225');                  !!!cp ('t225');
5468  ## ISSUE: Can this case be reached?                  ## NOTE: |<table><tr><table>|
5469                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                  !!!parse-error (type => 'not closed',
5470                                    text => $self->{open_elements}->[-1]->[0]
5471                                        ->manakai_local_name,
5472                                    token => $token);
5473                } else {                } else {
5474                  !!!cp ('t226');                  !!!cp ('t226');
5475                }                }
# Line 4627  sub _tree_construction_main ($) { Line 5479  sub _tree_construction_main ($) {
5479    
5480                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5481    
5482                ## reprocess            ## reprocess
5483                redo B;            !!!ack-later;
5484              next B;
5485          } elsif ($token->{tag_name} eq 'style') {          } elsif ($token->{tag_name} eq 'style') {
5486            if (not $open_tables->[-1]->[1]) { # tainted            if (not $open_tables->[-1]->[1]) { # tainted
5487              !!!cp ('t227.8');              !!!cp ('t227.8');
5488              ## NOTE: This is a "as if in head" code clone.              ## NOTE: This is a "as if in head" code clone.
5489              $parse_rcdata->(CDATA_CONTENT_MODEL);              $parse_rcdata->(CDATA_CONTENT_MODEL);
5490              redo B;              next B;
5491            } else {            } else {
5492              !!!cp ('t227.7');              !!!cp ('t227.7');
5493              #              #
# Line 4644  sub _tree_construction_main ($) { Line 5497  sub _tree_construction_main ($) {
5497              !!!cp ('t227.6');              !!!cp ('t227.6');
5498              ## NOTE: This is a "as if in head" code clone.              ## NOTE: This is a "as if in head" code clone.
5499              $script_start_tag->();              $script_start_tag->();
5500              redo B;              next B;
5501            } else {            } else {
5502              !!!cp ('t227.5');              !!!cp ('t227.5');
5503              #              #
# Line 4655  sub _tree_construction_main ($) { Line 5508  sub _tree_construction_main ($) {
5508                my $type = lc $token->{attributes}->{type}->{value};                my $type = lc $token->{attributes}->{type}->{value};
5509                if ($type eq 'hidden') {                if ($type eq 'hidden') {
5510                  !!!cp ('t227.3');                  !!!cp ('t227.3');
5511                  !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'in table',
5512                                    text => $token->{tag_name}, token => $token);
5513    
5514                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5515    
# Line 4664  sub _tree_construction_main ($) { Line 5518  sub _tree_construction_main ($) {
5518                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5519    
5520                  !!!next-token;                  !!!next-token;
5521                  redo B;                  !!!ack ('t227.2.1');
5522                    next B;
5523                } else {                } else {
5524                  !!!cp ('t227.2');                  !!!cp ('t227.2');
5525                  #                  #
# Line 4682  sub _tree_construction_main ($) { Line 5537  sub _tree_construction_main ($) {
5537            #            #
5538          }          }
5539    
5540          !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'in table', text => $token->{tag_name},
5541                            token => $token);
5542    
5543          $insert = $insert_to_foster;          $insert = $insert_to_foster;
5544          #          #
# Line 4693  sub _tree_construction_main ($) { Line 5549  sub _tree_construction_main ($) {
5549                my $i;                my $i;
5550                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5551                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5552                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_ROW_EL) {
5553                    !!!cp ('t228');                    !!!cp ('t228');
5554                    $i = $_;                    $i = $_;
5555                    last INSCOPE;                    last INSCOPE;
5556                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5557                    !!!cp ('t229');                    !!!cp ('t229');
5558                    last INSCOPE;                    last INSCOPE;
5559                  }                  }
5560                } # INSCOPE                } # INSCOPE
5561                unless (defined $i) {                unless (defined $i) {
5562                  !!!cp ('t230');                  !!!cp ('t230');
5563                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
5564                                    text => $token->{tag_name}, token => $token);
5565                  ## Ignore the token                  ## Ignore the token
5566                    !!!nack ('t230.1');
5567                  !!!next-token;                  !!!next-token;
5568                  redo B;                  next B;
5569                } else {                } else {
5570                  !!!cp ('t232');                  !!!cp ('t232');
5571                }                }
5572    
5573                ## Clear back to table row context                ## Clear back to table row context
5574                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5575                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5576                  !!!cp ('t231');                  !!!cp ('t231');
5577  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
5578                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 4726  sub _tree_construction_main ($) { Line 5581  sub _tree_construction_main ($) {
5581                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5582                $self->{insertion_mode} = IN_TABLE_BODY_IM;                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5583                !!!next-token;                !!!next-token;
5584                redo B;                !!!nack ('t231.1');
5585                  next B;
5586              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5587                if ($self->{insertion_mode} == IN_ROW_IM) {                if ($self->{insertion_mode} == IN_ROW_IM) {
5588                  ## As if </tr>                  ## As if </tr>
# Line 4734  sub _tree_construction_main ($) { Line 5590  sub _tree_construction_main ($) {
5590                  my $i;                  my $i;
5591                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5592                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5593                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5594                      !!!cp ('t233');                      !!!cp ('t233');
5595                      $i = $_;                      $i = $_;
5596                      last INSCOPE;                      last INSCOPE;
5597                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5598                      !!!cp ('t234');                      !!!cp ('t234');
5599                      last INSCOPE;                      last INSCOPE;
5600                    }                    }
# Line 4748  sub _tree_construction_main ($) { Line 5602  sub _tree_construction_main ($) {
5602                  unless (defined $i) {                  unless (defined $i) {
5603                    !!!cp ('t235');                    !!!cp ('t235');
5604  ## TODO: The following is wrong.  ## TODO: The following is wrong.
5605                    !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);                    !!!parse-error (type => 'unmatched end tag',
5606                                      text => $token->{type}, token => $token);
5607                    ## Ignore the token                    ## Ignore the token
5608                      !!!nack ('t236.1');
5609                    !!!next-token;                    !!!next-token;
5610                    redo B;                    next B;
5611                  }                  }
5612                                    
5613                  ## Clear back to table row context                  ## Clear back to table row context
5614                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5615                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5616                    !!!cp ('t236');                    !!!cp ('t236');
5617  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
5618                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4773  sub _tree_construction_main ($) { Line 5628  sub _tree_construction_main ($) {
5628                  my $i;                  my $i;
5629                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5630                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5631                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5632                      !!!cp ('t237');                      !!!cp ('t237');
5633                      $i = $_;                      $i = $_;
5634                      last INSCOPE;                      last INSCOPE;
5635                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5636                      !!!cp ('t238');                      !!!cp ('t238');
5637                      last INSCOPE;                      last INSCOPE;
5638                    }                    }
5639                  } # INSCOPE                  } # INSCOPE
5640                  unless (defined $i) {                  unless (defined $i) {
5641                    !!!cp ('t239');                    !!!cp ('t239');
5642                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                    !!!parse-error (type => 'unmatched end tag',
5643                                      text => $token->{tag_name}, token => $token);
5644                    ## Ignore the token                    ## Ignore the token
5645                      !!!nack ('t239.1');
5646                    !!!next-token;                    !!!next-token;
5647                    redo B;                    next B;
5648                  }                  }
5649                                    
5650                  ## Clear back to table body context                  ## Clear back to table body context
5651                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5652                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5653                    !!!cp ('t240');                    !!!cp ('t240');
5654                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5655                  }                  }
# Line 4823  sub _tree_construction_main ($) { Line 5675  sub _tree_construction_main ($) {
5675                my $i;                my $i;
5676                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5677                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5678                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_EL) {
5679                    !!!cp ('t241');                    !!!cp ('t241');
5680                    $i = $_;                    $i = $_;
5681                    last INSCOPE;                    last INSCOPE;
5682                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5683                    !!!cp ('t242');                    !!!cp ('t242');
5684                    last INSCOPE;                    last INSCOPE;
5685                  }                  }
5686                } # INSCOPE                } # INSCOPE
5687                unless (defined $i) {                unless (defined $i) {
5688                  !!!cp ('t243');                  !!!cp ('t243');
5689                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
5690                                    text => $token->{tag_name}, token => $token);
5691                  ## Ignore the token                  ## Ignore the token
5692                    !!!nack ('t243.1');
5693                  !!!next-token;                  !!!next-token;
5694                  redo B;                  next B;
5695                }                }
5696                                    
5697                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
# Line 4848  sub _tree_construction_main ($) { Line 5700  sub _tree_construction_main ($) {
5700                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5701                                
5702                !!!next-token;                !!!next-token;
5703                redo B;                next B;
5704              } elsif ({              } elsif ({
5705                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5706                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
# Line 4858  sub _tree_construction_main ($) { Line 5710  sub _tree_construction_main ($) {
5710                  my $i;                  my $i;
5711                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5712                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5713                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5714                      !!!cp ('t247');                      !!!cp ('t247');
5715                      $i = $_;                      $i = $_;
5716                      last INSCOPE;                      last INSCOPE;
5717                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5718                      !!!cp ('t248');                      !!!cp ('t248');
5719                      last INSCOPE;                      last INSCOPE;
5720                    }                    }
5721                  } # INSCOPE                  } # INSCOPE
5722                    unless (defined $i) {                    unless (defined $i) {
5723                      !!!cp ('t249');                      !!!cp ('t249');
5724                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                      !!!parse-error (type => 'unmatched end tag',
5725                                        text => $token->{tag_name}, token => $token);
5726                      ## Ignore the token                      ## Ignore the token
5727                        !!!nack ('t249.1');
5728                      !!!next-token;                      !!!next-token;
5729                      redo B;                      next B;
5730                    }                    }
5731                                    
5732                  ## As if </tr>                  ## As if </tr>
# Line 4882  sub _tree_construction_main ($) { Line 5734  sub _tree_construction_main ($) {
5734                  my $i;                  my $i;
5735                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5736                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5737                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5738                      !!!cp ('t250');                      !!!cp ('t250');
5739                      $i = $_;                      $i = $_;
5740                      last INSCOPE;                      last INSCOPE;
5741                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5742                      !!!cp ('t251');                      !!!cp ('t251');
5743                      last INSCOPE;                      last INSCOPE;
5744                    }                    }
5745                  } # INSCOPE                  } # INSCOPE
5746                    unless (defined $i) {                    unless (defined $i) {
5747                      !!!cp ('t252');                      !!!cp ('t252');
5748                      !!!parse-error (type => 'unmatched end tag:tr', token => $token);                      !!!parse-error (type => 'unmatched end tag',
5749                                        text => 'tr', token => $token);
5750                      ## Ignore the token                      ## Ignore the token
5751                        !!!nack ('t252.1');
5752                      !!!next-token;                      !!!next-token;
5753                      redo B;                      next B;
5754                    }                    }
5755                                    
5756                  ## Clear back to table row context                  ## Clear back to table row context
5757                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5758                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5759                    !!!cp ('t253');                    !!!cp ('t253');
5760  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
5761                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4919  sub _tree_construction_main ($) { Line 5770  sub _tree_construction_main ($) {
5770                my $i;                my $i;
5771                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5772                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5773                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5774                    !!!cp ('t254');                    !!!cp ('t254');
5775                    $i = $_;                    $i = $_;
5776                    last INSCOPE;                    last INSCOPE;
5777                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5778                    !!!cp ('t255');                    !!!cp ('t255');
5779                    last INSCOPE;                    last INSCOPE;
5780                  }                  }
5781                } # INSCOPE                } # INSCOPE
5782                unless (defined $i) {                unless (defined $i) {
5783                  !!!cp ('t256');                  !!!cp ('t256');
5784                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
5785                                    text => $token->{tag_name}, token => $token);
5786                  ## Ignore the token                  ## Ignore the token
5787                    !!!nack ('t256.1');
5788                  !!!next-token;                  !!!next-token;
5789                  redo B;                  next B;
5790                }                }
5791    
5792                ## Clear back to table body context                ## Clear back to table body context
5793                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5794                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5795                  !!!cp ('t257');                  !!!cp ('t257');
5796  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
5797                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 4949  sub _tree_construction_main ($) { Line 5799  sub _tree_construction_main ($) {
5799    
5800                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5801                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5802                  !!!nack ('t257.1');
5803                !!!next-token;                !!!next-token;
5804                redo B;                next B;
5805              } elsif ({              } elsif ({
5806                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
5807                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
5808                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5809                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
5810                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5811                !!!cp ('t258');            !!!cp ('t258');
5812                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
5813                ## Ignore the token                            text => $token->{tag_name}, token => $token);
5814                !!!next-token;            ## Ignore the token
5815                redo B;            !!!nack ('t258.1');
5816               !!!next-token;
5817              next B;
5818          } else {          } else {
5819            !!!cp ('t259');            !!!cp ('t259');
5820            !!!parse-error (type => 'in table:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'in table:/',
5821                              text => $token->{tag_name}, token => $token);
5822    
5823            $insert = $insert_to_foster;            $insert = $insert_to_foster;
5824            #            #
5825          }          }
5826        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5827          unless ($self->{open_elements}->[-1]->[1] eq 'html' and          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5828                  @{$self->{open_elements}} == 1) { # redundant, maybe                  @{$self->{open_elements}} == 1) { # redundant, maybe
5829            !!!parse-error (type => 'in body:#eof', token => $token);            !!!parse-error (type => 'in body:#eof', token => $token);
5830            !!!cp ('t259.1');            !!!cp ('t259.1');
# Line 4992  sub _tree_construction_main ($) { Line 5846  sub _tree_construction_main ($) {
5846                unless (length $token->{data}) {                unless (length $token->{data}) {
5847                  !!!cp ('t260');                  !!!cp ('t260');
5848                  !!!next-token;                  !!!next-token;
5849                  redo B;                  next B;
5850                }                }
5851              }              }
5852                            
# Line 5003  sub _tree_construction_main ($) { Line 5857  sub _tree_construction_main ($) {
5857                !!!cp ('t262');                !!!cp ('t262');
5858                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5859                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5860                  !!!ack ('t262.1');
5861                !!!next-token;                !!!next-token;
5862                redo B;                next B;
5863              } else {              } else {
5864                !!!cp ('t263');                !!!cp ('t263');
5865                #                #
5866              }              }
5867            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
5868              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
5869                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5870                  !!!cp ('t264');                  !!!cp ('t264');
5871                  !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);                  !!!parse-error (type => 'unmatched end tag',
5872                                    text => 'colgroup', token => $token);
5873                  ## Ignore the token                  ## Ignore the token
5874                  !!!next-token;                  !!!next-token;
5875                  redo B;                  next B;
5876                } else {                } else {
5877                  !!!cp ('t265');                  !!!cp ('t265');
5878                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
5879                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5880                  !!!next-token;                  !!!next-token;
5881                  redo B;                              next B;            
5882                }                }
5883              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
5884                !!!cp ('t266');                !!!cp ('t266');
5885                !!!parse-error (type => 'unmatched end tag:col', token => $token);                !!!parse-error (type => 'unmatched end tag',
5886                                  text => 'col', token => $token);
5887                ## Ignore the token                ## Ignore the token
5888                !!!next-token;                !!!next-token;
5889                redo B;                next B;
5890              } else {              } else {
5891                !!!cp ('t267');                !!!cp ('t267');
5892                #                #
5893              }              }
5894        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5895          if ($self->{open_elements}->[-1]->[1] eq 'html' or          if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5896              @{$self->{open_elements}} == 1) { # redundant, maybe              @{$self->{open_elements}} == 1) { # redundant, maybe
5897            !!!cp ('t270.2');            !!!cp ('t270.2');
5898            ## Stop parsing.            ## Stop parsing.
# Line 5046  sub _tree_construction_main ($) { Line 5903  sub _tree_construction_main ($) {
5903            pop @{$self->{open_elements}}; # colgroup            pop @{$self->{open_elements}}; # colgroup
5904            $self->{insertion_mode} = IN_TABLE_IM;            $self->{insertion_mode} = IN_TABLE_IM;
5905            ## Reprocess.            ## Reprocess.
5906            redo B;            next B;
5907          }          }
5908        } else {        } else {
5909          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5910        }        }
5911    
5912            ## As if </colgroup>            ## As if </colgroup>
5913            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5914              !!!cp ('t269');              !!!cp ('t269');
5915  ## TODO: Wrong error type?  ## TODO: Wrong error type?
5916              !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);              !!!parse-error (type => 'unmatched end tag',
5917                                text => 'colgroup', token => $token);
5918              ## Ignore the token              ## Ignore the token
5919                !!!nack ('t269.1');
5920              !!!next-token;              !!!next-token;
5921              redo B;              next B;
5922            } else {            } else {
5923              !!!cp ('t270');              !!!cp ('t270');
5924              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
5925              $self->{insertion_mode} = IN_TABLE_IM;              $self->{insertion_mode} = IN_TABLE_IM;
5926                !!!ack-later;
5927              ## reprocess              ## reprocess
5928              redo B;              next B;
5929            }            }
5930      } elsif ($self->{insertion_mode} & SELECT_IMS) {      } elsif ($self->{insertion_mode} & SELECT_IMS) {
5931        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5932          !!!cp ('t271');          !!!cp ('t271');
5933          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5934          !!!next-token;          !!!next-token;
5935          redo B;          next B;
5936        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5937              if ($token->{tag_name} eq 'option') {          if ($token->{tag_name} eq 'option') {
5938                if ($self->{open_elements}->[-1]->[1] eq 'option') {            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5939                  !!!cp ('t272');              !!!cp ('t272');
5940                  ## As if </option>              ## As if </option>
5941                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5942                } else {            } else {
5943                  !!!cp ('t273');              !!!cp ('t273');
5944                }            }
5945    
5946                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5947                !!!next-token;            !!!nack ('t273.1');
5948                redo B;            !!!next-token;
5949              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
5950                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
5951                  !!!cp ('t274');            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5952                  ## As if </option>              !!!cp ('t274');
5953                  pop @{$self->{open_elements}};              ## As if </option>
5954                } else {              pop @{$self->{open_elements}};
5955                  !!!cp ('t275');            } else {
5956                }              !!!cp ('t275');
5957              }
5958    
5959                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5960                  !!!cp ('t276');              !!!cp ('t276');
5961                  ## As if </optgroup>              ## As if </optgroup>
5962                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5963                } else {            } else {
5964                  !!!cp ('t277');              !!!cp ('t277');
5965                }            }
5966    
5967                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5968                !!!next-token;            !!!nack ('t277.1');
5969                redo B;            !!!next-token;
5970          } elsif ($token->{tag_name} eq 'select' or            next B;
5971                   $token->{tag_name} eq 'input' or          } elsif ({
5972                       select => 1, input => 1, textarea => 1,
5973                     }->{$token->{tag_name}} or
5974                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5975                    {                    {
5976                     caption => 1, table => 1,                     caption => 1, table => 1,
# Line 5115  sub _tree_construction_main ($) { Line 5978  sub _tree_construction_main ($) {
5978                     tr => 1, td => 1, th => 1,                     tr => 1, td => 1, th => 1,
5979                    }->{$token->{tag_name}})) {                    }->{$token->{tag_name}})) {
5980            ## TODO: The type below is not good - <select> is replaced by </select>            ## TODO: The type below is not good - <select> is replaced by </select>
5981            !!!parse-error (type => 'not closed:select', token => $token);            !!!parse-error (type => 'not closed', text => 'select',
5982                              token => $token);
5983            ## NOTE: As if the token were </select> (<select> case) or            ## NOTE: As if the token were </select> (<select> case) or
5984            ## as if there were </select> (otherwise).            ## as if there were </select> (otherwise).
5985                ## have an element in table scope            ## have an element in table scope
5986                my $i;            my $i;
5987                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5988                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
5989                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
5990                    !!!cp ('t278');                !!!cp ('t278');
5991                    $i = $_;                $i = $_;
5992                    last INSCOPE;                last INSCOPE;
5993                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5994                            table => 1, html => 1,                !!!cp ('t279');
5995                           }->{$node->[1]}) {                last INSCOPE;
5996                    !!!cp ('t279');              }
5997                    last INSCOPE;            } # INSCOPE
5998                  }            unless (defined $i) {
5999                } # INSCOPE              !!!cp ('t280');
6000                unless (defined $i) {              !!!parse-error (type => 'unmatched end tag',
6001                  !!!cp ('t280');                              text => 'select', token => $token);
6002                  !!!parse-error (type => 'unmatched end tag:select', token => $token);              ## Ignore the token
6003                  ## Ignore the token              !!!nack ('t280.1');
6004                  !!!next-token;              !!!next-token;
6005                  redo B;              next B;
6006                }            }
6007                                
6008                !!!cp ('t281');            !!!cp ('t281');
6009                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6010    
6011                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6012    
6013            if ($token->{tag_name} eq 'select') {            if ($token->{tag_name} eq 'select') {
6014              !!!cp ('t281.2');              !!!nack ('t281.2');
6015              !!!next-token;              !!!next-token;
6016              redo B;              next B;
6017            } else {            } else {
6018              !!!cp ('t281.1');              !!!cp ('t281.1');
6019                !!!ack-later;
6020              ## Reprocess the token.              ## Reprocess the token.
6021              redo B;              next B;
6022            }            }
6023          } else {          } else {
6024            !!!cp ('t282');            !!!cp ('t282');
6025            !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'in select',
6026                              text => $token->{tag_name}, token => $token);
6027            ## Ignore the token            ## Ignore the token
6028              !!!nack ('t282.1');
6029            !!!next-token;            !!!next-token;
6030            redo B;            next B;
6031          }          }
6032        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6033              if ($token->{tag_name} eq 'optgroup') {          if ($token->{tag_name} eq 'optgroup') {
6034                if ($self->{open_elements}->[-1]->[1] eq 'option' and            if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
6035                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
6036                  !!!cp ('t283');              !!!cp ('t283');
6037                  ## As if </option>              ## As if </option>
6038                  splice @{$self->{open_elements}}, -2;              splice @{$self->{open_elements}}, -2;
6039                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6040                  !!!cp ('t284');              !!!cp ('t284');
6041                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6042                } else {            } else {
6043                  !!!cp ('t285');              !!!cp ('t285');
6044                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag',
6045                  ## Ignore the token                              text => $token->{tag_name}, token => $token);
6046                }              ## Ignore the token
6047                !!!next-token;            }
6048                redo B;            !!!nack ('t285.1');
6049              } elsif ($token->{tag_name} eq 'option') {            !!!next-token;
6050                if ($self->{open_elements}->[-1]->[1] eq 'option') {            next B;
6051                  !!!cp ('t286');          } elsif ($token->{tag_name} eq 'option') {
6052                  pop @{$self->{open_elements}};            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6053                } else {              !!!cp ('t286');
6054                  !!!cp ('t287');              pop @{$self->{open_elements}};
6055                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            } else {
6056                  ## Ignore the token              !!!cp ('t287');
6057                }              !!!parse-error (type => 'unmatched end tag',
6058                !!!next-token;                              text => $token->{tag_name}, token => $token);
6059                redo B;              ## Ignore the token
6060              } elsif ($token->{tag_name} eq 'select') {            }
6061                ## have an element in table scope            !!!nack ('t287.1');
6062                my $i;            !!!next-token;
6063                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            next B;
6064                  my $node = $self->{open_elements}->[$_];          } elsif ($token->{tag_name} eq 'select') {
6065                  if ($node->[1] eq $token->{tag_name}) {            ## have an element in table scope
6066                    !!!cp ('t288');            my $i;
6067                    $i = $_;            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6068                    last INSCOPE;              my $node = $self->{open_elements}->[$_];
6069                  } elsif ({              if ($node->[1] & SELECT_EL) {
6070                            table => 1, html => 1,                !!!cp ('t288');
6071                           }->{$node->[1]}) {                $i = $_;
6072                    !!!cp ('t289');                last INSCOPE;
6073                    last INSCOPE;              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6074                  }                !!!cp ('t289');
6075                } # INSCOPE                last INSCOPE;
6076                unless (defined $i) {              }
6077                  !!!cp ('t290');            } # INSCOPE
6078                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            unless (defined $i) {
6079                  ## Ignore the token              !!!cp ('t290');
6080                  !!!next-token;              !!!parse-error (type => 'unmatched end tag',
6081                  redo B;                              text => $token->{tag_name}, token => $token);
6082                }              ## Ignore the token
6083                !!!nack ('t290.1');
6084                !!!next-token;
6085                next B;
6086              }
6087                                
6088                !!!cp ('t291');            !!!cp ('t291');
6089                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6090    
6091                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6092    
6093                !!!next-token;            !!!nack ('t291.1');
6094                redo B;            !!!next-token;
6095              next B;
6096          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6097                   {                   {
6098                    caption => 1, table => 1, tbody => 1,                    caption => 1, table => 1, tbody => 1,
6099                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
6100                   }->{$token->{tag_name}}) {                   }->{$token->{tag_name}}) {
6101  ## TODO: The following is wrong?  ## TODO: The following is wrong?
6102                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
6103                              text => $token->{tag_name}, token => $token);
6104                                
6105                ## have an element in table scope            ## have an element in table scope
6106                my $i;            my $i;
6107                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6108                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6109                  if ($node->[1] eq $token->{tag_name}) {              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6110                    !!!cp ('t292');                !!!cp ('t292');
6111                    $i = $_;                $i = $_;
6112                    last INSCOPE;                last INSCOPE;
6113                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6114                            table => 1, html => 1,                !!!cp ('t293');
6115                           }->{$node->[1]}) {                last INSCOPE;
6116                    !!!cp ('t293');              }
6117                    last INSCOPE;            } # INSCOPE
6118                  }            unless (defined $i) {
6119                } # INSCOPE              !!!cp ('t294');
6120                unless (defined $i) {              ## Ignore the token
6121                  !!!cp ('t294');              !!!nack ('t294.1');
6122                  ## Ignore the token              !!!next-token;
6123                  !!!next-token;              next B;
6124                  redo B;            }
               }  
6125                                
6126                ## As if </select>            ## As if </select>
6127                ## have an element in table scope            ## have an element in table scope
6128                undef $i;            undef $i;
6129                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6130                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6131                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
6132                    !!!cp ('t295');                !!!cp ('t295');
6133                    $i = $_;                $i = $_;
6134                    last INSCOPE;                last INSCOPE;
6135                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
6136  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
6137                    !!!cp ('t296');                !!!cp ('t296');
6138                    last INSCOPE;                last INSCOPE;
6139                  }              }
6140                } # INSCOPE            } # INSCOPE
6141                unless (defined $i) {            unless (defined $i) {
6142                  !!!cp ('t297');              !!!cp ('t297');
6143  ## TODO: The following error type is correct?  ## TODO: The following error type is correct?
6144                  !!!parse-error (type => 'unmatched end tag:select', token => $token);              !!!parse-error (type => 'unmatched end tag',
6145                  ## Ignore the </select> token                              text => 'select', token => $token);
6146                  !!!next-token; ## TODO: ok?              ## Ignore the </select> token
6147                  redo B;              !!!nack ('t297.1');
6148                }              !!!next-token; ## TODO: ok?
6149                next B;
6150              }
6151                                
6152                !!!cp ('t298');            !!!cp ('t298');
6153                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6154    
6155                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6156    
6157                ## reprocess            !!!ack-later;
6158                redo B;            ## reprocess
6159              next B;
6160          } else {          } else {
6161            !!!cp ('t299');            !!!cp ('t299');
6162            !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'in select:/',
6163                              text => $token->{tag_name}, token => $token);
6164            ## Ignore the token            ## Ignore the token
6165              !!!nack ('t299.3');
6166            !!!next-token;            !!!next-token;
6167            redo B;            next B;
6168          }          }
6169        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6170          unless ($self->{open_elements}->[-1]->[1] eq 'html' and          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6171                  @{$self->{open_elements}} == 1) { # redundant, maybe                  @{$self->{open_elements}} == 1) { # redundant, maybe
6172            !!!cp ('t299.1');            !!!cp ('t299.1');
6173            !!!parse-error (type => 'in body:#eof', token => $token);            !!!parse-error (type => 'in body:#eof', token => $token);
# Line 5317  sub _tree_construction_main ($) { Line 6192  sub _tree_construction_main ($) {
6192            unless (length $token->{data}) {            unless (length $token->{data}) {
6193              !!!cp ('t300');              !!!cp ('t300');
6194              !!!next-token;              !!!next-token;
6195              redo B;              next B;
6196            }            }
6197          }          }
6198                    
6199          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6200            !!!cp ('t301');            !!!cp ('t301');
6201            !!!parse-error (type => 'after html:#character', token => $token);            !!!parse-error (type => 'after html:#text', token => $token);
6202    
6203            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
6204          } else {          } else {
# Line 5331  sub _tree_construction_main ($) { Line 6206  sub _tree_construction_main ($) {
6206          }          }
6207                    
6208          ## "after body" insertion mode          ## "after body" insertion mode
6209          !!!parse-error (type => 'after body:#character', token => $token);          !!!parse-error (type => 'after body:#text', token => $token);
6210    
6211          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6212          ## reprocess          ## reprocess
6213          redo B;          next B;
6214        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6215          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6216            !!!cp ('t303');            !!!cp ('t303');
6217            !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'after html',
6218                              text => $token->{tag_name}, token => $token);
6219                        
6220            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
6221          } else {          } else {
# Line 5347  sub _tree_construction_main ($) { Line 6223  sub _tree_construction_main ($) {
6223          }          }
6224    
6225          ## "after body" insertion mode          ## "after body" insertion mode
6226          !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'after body',
6227                            text => $token->{tag_name}, token => $token);
6228    
6229          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6230            !!!ack-later;
6231          ## reprocess          ## reprocess
6232          redo B;          next B;
6233        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6234          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6235            !!!cp ('t305');            !!!cp ('t305');
6236            !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'after html:/',
6237                              text => $token->{tag_name}, token => $token);
6238                        
6239            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6240            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
# Line 5367  sub _tree_construction_main ($) { Line 6246  sub _tree_construction_main ($) {
6246          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
6247            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
6248              !!!cp ('t307');              !!!cp ('t307');
6249              !!!parse-error (type => 'unmatched end tag:html', token => $token);              !!!parse-error (type => 'unmatched end tag',
6250                                text => 'html', token => $token);
6251              ## Ignore the token              ## Ignore the token
6252              !!!next-token;              !!!next-token;
6253              redo B;              next B;
6254            } else {            } else {
6255              !!!cp ('t308');              !!!cp ('t308');
6256              $self->{insertion_mode} = AFTER_HTML_BODY_IM;              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6257              !!!next-token;              !!!next-token;
6258              redo B;              next B;
6259            }            }
6260          } else {          } else {
6261            !!!cp ('t309');            !!!cp ('t309');
6262            !!!parse-error (type => 'after body:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'after body:/',
6263                              text => $token->{tag_name}, token => $token);
6264    
6265            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
6266            ## reprocess            ## reprocess
6267            redo B;            next B;
6268          }          }
6269        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6270          !!!cp ('t309.2');          !!!cp ('t309.2');
# Line 5400  sub _tree_construction_main ($) { Line 6281  sub _tree_construction_main ($) {
6281            unless (length $token->{data}) {            unless (length $token->{data}) {
6282              !!!cp ('t310');              !!!cp ('t310');
6283              !!!next-token;              !!!next-token;
6284              redo B;              next B;
6285            }            }
6286          }          }
6287                    
6288          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
6289            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6290              !!!cp ('t311');              !!!cp ('t311');
6291              !!!parse-error (type => 'in frameset:#character', token => $token);              !!!parse-error (type => 'in frameset:#text', token => $token);
6292            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6293              !!!cp ('t312');              !!!cp ('t312');
6294              !!!parse-error (type => 'after frameset:#character', token => $token);              !!!parse-error (type => 'after frameset:#text', token => $token);
6295            } else { # "after html frameset"            } else { # "after html frameset"
6296              !!!cp ('t313');              !!!cp ('t313');
6297              !!!parse-error (type => 'after html:#character', token => $token);              !!!parse-error (type => 'after html:#text', token => $token);
6298    
6299              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6300              ## Reprocess in the "after frameset" insertion mode.              ## Reprocess in the "after frameset" insertion mode.
6301              !!!parse-error (type => 'after frameset:#character', token => $token);              !!!parse-error (type => 'after frameset:#text', token => $token);
6302            }            }
6303                        
6304            ## Ignore the token.            ## Ignore the token.
# Line 5428  sub _tree_construction_main ($) { Line 6309  sub _tree_construction_main ($) {
6309              !!!cp ('t315');              !!!cp ('t315');
6310              !!!next-token;              !!!next-token;
6311            }            }
6312            redo B;            next B;
6313          }          }
6314                    
6315          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
6316        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6317          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
6318            !!!cp ('t316');            !!!cp ('t316');
6319            !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'after html',
6320                              text => $token->{tag_name}, token => $token);
6321    
6322            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
6323            ## Process in the "after frameset" insertion mode.            ## Process in the "after frameset" insertion mode.
# Line 5447  sub _tree_construction_main ($) { Line 6329  sub _tree_construction_main ($) {
6329              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6330            !!!cp ('t318');            !!!cp ('t318');
6331            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6332              !!!nack ('t318.1');
6333            !!!next-token;            !!!next-token;
6334            redo B;            next B;
6335          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
6336                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
6337            !!!cp ('t319');            !!!cp ('t319');
6338            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6339            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6340              !!!ack ('t319.1');
6341            !!!next-token;            !!!next-token;
6342            redo B;            next B;
6343          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
6344            !!!cp ('t320');            !!!cp ('t320');
6345            ## NOTE: As if in body.            ## NOTE: As if in head.
6346            $parse_rcdata->(CDATA_CONTENT_MODEL);            $parse_rcdata->(CDATA_CONTENT_MODEL);
6347            redo B;            next B;
6348          } else {          } else {
6349            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6350              !!!cp ('t321');              !!!cp ('t321');
6351              !!!parse-error (type => 'in frameset:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'in frameset',
6352                                text => $token->{tag_name}, token => $token);
6353            } else {            } else {
6354              !!!cp ('t322');              !!!cp ('t322');
6355              !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'after frameset',
6356                                text => $token->{tag_name}, token => $token);
6357            }            }
6358            ## Ignore the token            ## Ignore the token
6359              !!!nack ('t322.1');
6360            !!!next-token;            !!!next-token;
6361            redo B;            next B;
6362          }          }
6363        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6364          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
6365            !!!cp ('t323');            !!!cp ('t323');
6366            !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'after html:/',
6367                              text => $token->{tag_name}, token => $token);
6368    
6369            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
6370            ## Process in the "after frameset" insertion mode.            ## Process in the "after frameset" insertion mode.
# Line 5486  sub _tree_construction_main ($) { Line 6374  sub _tree_construction_main ($) {
6374    
6375          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6376              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6377            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6378                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
6379              !!!cp ('t325');              !!!cp ('t325');
6380              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag',
6381                                text => $token->{tag_name}, token => $token);
6382              ## Ignore the token              ## Ignore the token
6383              !!!next-token;              !!!next-token;
6384            } else {            } else {
# Line 5499  sub _tree_construction_main ($) { Line 6388  sub _tree_construction_main ($) {
6388            }            }
6389    
6390            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
6391                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6392              !!!cp ('t327');              !!!cp ('t327');
6393              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6394            } else {            } else {
6395              !!!cp ('t328');              !!!cp ('t328');
6396            }            }
6397            redo B;            next B;
6398          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
6399                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6400            !!!cp ('t329');            !!!cp ('t329');
6401            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6402            !!!next-token;            !!!next-token;
6403            redo B;            next B;
6404          } else {          } else {
6405            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6406              !!!cp ('t330');              !!!cp ('t330');
6407              !!!parse-error (type => 'in frameset:/'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'in frameset:/',
6408                                text => $token->{tag_name}, token => $token);
6409            } else {            } else {
6410              !!!cp ('t331');              !!!cp ('t331');
6411              !!!parse-error (type => 'after frameset:/'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'after frameset:/',
6412                                text => $token->{tag_name}, token => $token);
6413            }            }
6414            ## Ignore the token            ## Ignore the token
6415            !!!next-token;            !!!next-token;
6416            redo B;            next B;
6417          }          }
6418        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6419          unless ($self->{open_elements}->[-1]->[1] eq 'html' and          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6420                  @{$self->{open_elements}} == 1) { # redundant, maybe                  @{$self->{open_elements}} == 1) { # redundant, maybe
6421            !!!cp ('t331.1');            !!!cp ('t331.1');
6422            !!!parse-error (type => 'in body:#eof', token => $token);            !!!parse-error (type => 'in body:#eof', token => $token);
# Line 5550  sub _tree_construction_main ($) { Line 6441  sub _tree_construction_main ($) {
6441          !!!cp ('t332');          !!!cp ('t332');
6442          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6443          $script_start_tag->();          $script_start_tag->();
6444          redo B;          next B;
6445        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
6446          !!!cp ('t333');          !!!cp ('t333');
6447          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6448          $parse_rcdata->(CDATA_CONTENT_MODEL);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6449          redo B;          next B;
6450        } elsif ({        } elsif ({
6451                  base => 1, link => 1,                  base => 1, link => 1,
6452                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 5563  sub _tree_construction_main ($) { Line 6454  sub _tree_construction_main ($) {
6454          ## 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
6455          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6456          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6457            !!!ack ('t334.1');
6458          !!!next-token;          !!!next-token;
6459          redo B;          next B;
6460        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
6461          ## 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
6462          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6463          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.
6464    
6465          unless ($self->{confident}) {          unless ($self->{confident}) {
6466            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) {
6467              !!!cp ('t335');              !!!cp ('t335');
6468                ## NOTE: Whether the encoding is supported or not is handled
6469                ## in the {change_encoding} callback.
6470              $self->{change_encoding}              $self->{change_encoding}
6471                  ->($self, $token->{attributes}->{charset}->{value}, $token);                  ->($self, $token->{attributes}->{charset}->{value}, $token);
6472                            
# Line 5581  sub _tree_construction_main ($) { Line 6475  sub _tree_construction_main ($) {
6475                                       $token->{attributes}->{charset}                                       $token->{attributes}->{charset}
6476                                           ->{has_reference});                                           ->{has_reference});
6477            } elsif ($token->{attributes}->{content}) {            } elsif ($token->{attributes}->{content}) {
             ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
6478              if ($token->{attributes}->{content}->{value}              if ($token->{attributes}->{content}->{value}
6479                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                  =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6480                      [\x09-\x0D\x20]*=                      [\x09-\x0D\x20]*=
6481                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6482                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
6483                !!!cp ('t336');                !!!cp ('t336');
6484                  ## NOTE: Whether the encoding is supported or not is handled
6485                  ## in the {change_encoding} callback.
6486                $self->{change_encoding}                $self->{change_encoding}
6487                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6488                $meta_el->[0]->get_attribute_node_ns (undef, 'content')                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
# Line 5613  sub _tree_construction_main ($) { Line 6508  sub _tree_construction_main ($) {
6508            }            }
6509          }          }
6510    
6511            !!!ack ('t338.1');
6512          !!!next-token;          !!!next-token;
6513          redo B;          next B;
6514        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
6515          !!!cp ('t341');          !!!cp ('t341');
6516          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6517          $parse_rcdata->(RCDATA_CONTENT_MODEL);          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6518          redo B;          next B;
6519        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
6520          !!!parse-error (type => 'in body:body', token => $token);          !!!parse-error (type => 'in body', text => 'body', token => $token);
6521                                
6522          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
6523              $self->{open_elements}->[1]->[1] ne 'body') {              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6524            !!!cp ('t342');            !!!cp ('t342');
6525            ## Ignore the token            ## Ignore the token
6526          } else {          } else {
# Line 5638  sub _tree_construction_main ($) { Line 6534  sub _tree_construction_main ($) {
6534              }              }
6535            }            }
6536          }          }
6537            !!!nack ('t343.1');
6538          !!!next-token;          !!!next-token;
6539          redo B;          next B;
6540        } elsif ({        } elsif ({
6541                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6542                  div => 1, dl => 1, fieldset => 1,                  div => 1, dl => 1, fieldset => 1,
# Line 5654  sub _tree_construction_main ($) { Line 6551  sub _tree_construction_main ($) {
6551            !!!cp ('t350');            !!!cp ('t350');
6552            !!!parse-error (type => 'in form:form', token => $token);            !!!parse-error (type => 'in form:form', token => $token);
6553            ## Ignore the token            ## Ignore the token
6554              !!!nack ('t350.1');
6555            !!!next-token;            !!!next-token;
6556            redo B;            next B;
6557          }          }
6558    
6559          ## has a p element in scope          ## has a p element in scope
6560          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6561            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6562              !!!cp ('t344');              !!!cp ('t344');
6563              !!!back-token;              !!!back-token; # <form>
6564              $token = {type => END_TAG_TOKEN, tag_name => 'p',              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6565                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6566              redo B;              next B;
6567            } elsif ({            } elsif ($_->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6568              !!!cp ('t345');              !!!cp ('t345');
6569              last INSCOPE;              last INSCOPE;
6570            }            }
# Line 5677  sub _tree_construction_main ($) { Line 6572  sub _tree_construction_main ($) {
6572                        
6573          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6574          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6575              !!!nack ('t346.1');
6576            !!!next-token;            !!!next-token;
6577            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
6578              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
# Line 5693  sub _tree_construction_main ($) { Line 6589  sub _tree_construction_main ($) {
6589            !!!cp ('t347.1');            !!!cp ('t347.1');
6590            $self->{form_element} = $self->{open_elements}->[-1]->[0];            $self->{form_element} = $self->{open_elements}->[-1]->[0];
6591    
6592              !!!nack ('t347.2');
6593            !!!next-token;            !!!next-token;
6594          } elsif ($token->{tag_name} eq 'table') {          } elsif ($token->{tag_name} eq 'table') {
6595            !!!cp ('t382');            !!!cp ('t382');
# Line 5700  sub _tree_construction_main ($) { Line 6597  sub _tree_construction_main ($) {
6597                        
6598            $self->{insertion_mode} = IN_TABLE_IM;            $self->{insertion_mode} = IN_TABLE_IM;
6599    
6600              !!!nack ('t382.1');
6601            !!!next-token;            !!!next-token;
6602          } elsif ($token->{tag_name} eq 'hr') {          } elsif ($token->{tag_name} eq 'hr') {
6603            !!!cp ('t386');            !!!cp ('t386');
6604            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6605                    
6606              !!!nack ('t386.1');
6607            !!!next-token;            !!!next-token;
6608          } else {          } else {
6609            !!!cp ('t347');            !!!nack ('t347.1');
6610            !!!next-token;            !!!next-token;
6611          }          }
6612          redo B;          next B;
6613        } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {        } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
6614          ## has a p element in scope          ## has a p element in scope
6615          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6616            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6617              !!!cp ('t353');              !!!cp ('t353');
6618              !!!back-token;              !!!back-token; # <x>
6619              $token = {type => END_TAG_TOKEN, tag_name => 'p',              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6620                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6621              redo B;              next B;
6622            } elsif ({            } elsif ($_->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6623              !!!cp ('t354');              !!!cp ('t354');
6624              last INSCOPE;              last INSCOPE;
6625            }            }
# Line 5737  sub _tree_construction_main ($) { Line 6633  sub _tree_construction_main ($) {
6633                            dd => {dt => 1, dd => 1}}->{$token->{tag_name}};                            dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6634          LI: {          LI: {
6635            ## Step 2            ## Step 2
6636            if ($li_or_dtdd->{$node->[1]}) {            if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6637              if ($i != -1) {              if ($i != -1) {
6638                !!!cp ('t355');                !!!cp ('t355');
6639                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'not closed',
6640                                $self->{open_elements}->[-1]->[1], token => $token);                                text => $self->{open_elements}->[-1]->[0]
6641                                      ->manakai_local_name,
6642                                  token => $token);
6643              } else {              } else {
6644                !!!cp ('t356');                !!!cp ('t356');
6645              }              }
# Line 5752  sub _tree_construction_main ($) { Line 6650  sub _tree_construction_main ($) {
6650            }            }
6651                        
6652            ## Step 3            ## Step 3
6653            if (not $formatting_category->{$node->[1]} and            if (not ($node->[1] & FORMATTING_EL) and
6654                #not $phrasing_category->{$node->[1]} and                #not $phrasing_category->{$node->[1]} and
6655                ($special_category->{$node->[1]} or                ($node->[1] & SPECIAL_EL or
6656                 $scoping_category->{$node->[1]}) and                 $node->[1] & SCOPING_EL) and
6657                $node->[1] ne 'address' and $node->[1] ne 'div') {                not ($node->[1] & ADDRESS_EL) and
6658                  not ($node->[1] & DIV_EL)) {
6659              !!!cp ('t358');              !!!cp ('t358');
6660              last LI;              last LI;
6661            }            }
# Line 5769  sub _tree_construction_main ($) { Line 6668  sub _tree_construction_main ($) {
6668          } # LI          } # LI
6669                        
6670          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6671            !!!nack ('t359.1');
6672          !!!next-token;          !!!next-token;
6673          redo B;          next B;
6674        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
6675          ## has a p element in scope          ## has a p element in scope
6676          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6677            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6678              !!!cp ('t367');              !!!cp ('t367');
6679              !!!back-token;              !!!back-token; # <plaintext>
6680              $token = {type => END_TAG_TOKEN, tag_name => 'p',              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6681                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6682              redo B;              next B;
6683            } elsif ({            } elsif ($_->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6684              !!!cp ('t368');              !!!cp ('t368');
6685              last INSCOPE;              last INSCOPE;
6686            }            }
# Line 5793  sub _tree_construction_main ($) { Line 6690  sub _tree_construction_main ($) {
6690                        
6691          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
6692                        
6693            !!!nack ('t368.1');
6694          !!!next-token;          !!!next-token;
6695          redo B;          next B;
6696        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
6697          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
6698            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
6699            if ($node->[1] eq 'a') {            if ($node->[1] & A_EL) {
6700              !!!cp ('t371');              !!!cp ('t371');
6701              !!!parse-error (type => 'in a:a', token => $token);              !!!parse-error (type => 'in a:a', token => $token);
6702                            
6703              !!!back-token;              !!!back-token; # <a>
6704              $token = {type => END_TAG_TOKEN, tag_name => 'a',              $token = {type => END_TAG_TOKEN, tag_name => 'a',
6705                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6706              $formatting_end_tag->($token);              $formatting_end_tag->($token);
# Line 5833  sub _tree_construction_main ($) { Line 6731  sub _tree_construction_main ($) {
6731          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6732          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6733    
6734            !!!nack ('t374.1');
6735          !!!next-token;          !!!next-token;
6736          redo B;          next B;
6737        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
6738          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6739    
6740          ## has a |nobr| element in scope          ## has a |nobr| element in scope
6741          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6742            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6743            if ($node->[1] eq 'nobr') {            if ($node->[1] & NOBR_EL) {
6744              !!!cp ('t376');              !!!cp ('t376');
6745              !!!parse-error (type => 'in nobr:nobr', token => $token);              !!!parse-error (type => 'in nobr:nobr', token => $token);
6746              !!!back-token;              !!!back-token; # <nobr>
6747              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
6748                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6749              redo B;              next B;
6750            } 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]}) {  
6751              !!!cp ('t377');              !!!cp ('t377');
6752              last INSCOPE;              last INSCOPE;
6753            }            }
# Line 5860  sub _tree_construction_main ($) { Line 6756  sub _tree_construction_main ($) {
6756          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6757          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6758                    
6759            !!!nack ('t377.1');
6760          !!!next-token;          !!!next-token;
6761          redo B;          next B;
6762        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
6763          ## has a button element in scope          ## has a button element in scope
6764          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6765            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6766            if ($node->[1] eq 'button') {            if ($node->[1] & BUTTON_EL) {
6767              !!!cp ('t378');              !!!cp ('t378');
6768              !!!parse-error (type => 'in button:button', token => $token);              !!!parse-error (type => 'in button:button', token => $token);
6769              !!!back-token;              !!!back-token; # <button>
6770              $token = {type => END_TAG_TOKEN, tag_name => 'button',              $token = {type => END_TAG_TOKEN, tag_name => 'button',
6771                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6772              redo B;              next B;
6773            } 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]}) {  
6774              !!!cp ('t379');              !!!cp ('t379');
6775              last INSCOPE;              last INSCOPE;
6776            }            }
# Line 5890  sub _tree_construction_main ($) { Line 6784  sub _tree_construction_main ($) {
6784    
6785          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
6786    
6787            !!!nack ('t379.1');
6788          !!!next-token;          !!!next-token;
6789          redo B;          next B;
6790        } elsif ({        } elsif ({
6791                  xmp => 1,                  xmp => 1,
6792                  iframe => 1,                  iframe => 1,
6793                  noembed => 1,                  noembed => 1,
6794                  noframes => 1,                  noframes => 1, ## NOTE: This is an "as if in head" code clone.
6795                  noscript => 0, ## TODO: 1 if scripting is enabled                  noscript => 0, ## TODO: 1 if scripting is enabled
6796                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6797          if ($token->{tag_name} eq 'xmp') {          if ($token->{tag_name} eq 'xmp') {
# Line 5907  sub _tree_construction_main ($) { Line 6802  sub _tree_construction_main ($) {
6802          }          }
6803          ## NOTE: There is an "as if in body" code clone.          ## NOTE: There is an "as if in body" code clone.
6804          $parse_rcdata->(CDATA_CONTENT_MODEL);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6805          redo B;          next B;
6806        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
6807          !!!parse-error (type => 'isindex', token => $token);          !!!parse-error (type => 'isindex', token => $token);
6808                    
6809          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
6810            !!!cp ('t389');            !!!cp ('t389');
6811            ## Ignore the token            ## Ignore the token
6812              !!!nack ('t389'); ## NOTE: Not acknowledged.
6813            !!!next-token;            !!!next-token;
6814            redo B;            next B;
6815          } else {          } else {
6816              !!!ack ('t391.1');
6817    
6818            my $at = $token->{attributes};            my $at = $token->{attributes};
6819            my $form_attrs;            my $form_attrs;
6820            $form_attrs->{action} = $at->{action} if $at->{action};            $form_attrs->{action} = $at->{action} if $at->{action};
# Line 5938  sub _tree_construction_main ($) { Line 6836  sub _tree_construction_main ($) {
6836            if ($prompt_attr) {            if ($prompt_attr) {
6837              !!!cp ('t390');              !!!cp ('t390');
6838              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
6839                             line => $token->{line}, column => $token->{column}};                             #line => $token->{line}, column => $token->{column},
6840                              };
6841            } else {            } else {
6842              !!!cp ('t391');              !!!cp ('t391');
6843              push @tokens, {type => CHARACTER_TOKEN,              push @tokens, {type => CHARACTER_TOKEN,
6844                             data => 'This is a searchable index. Insert your search keywords here: ',                             data => 'This is a searchable index. Insert your search keywords here: ',
6845                             line => $token->{line}, column => $token->{column}}; # SHOULD                             #line => $token->{line}, column => $token->{column},
6846                              }; # SHOULD
6847              ## TODO: make this configurable              ## TODO: make this configurable
6848            }            }
6849            push @tokens,            push @tokens,
# Line 5958  sub _tree_construction_main ($) { Line 6858  sub _tree_construction_main ($) {
6858                           line => $token->{line}, column => $token->{column}},                           line => $token->{line}, column => $token->{column}},
6859                          {type => END_TAG_TOKEN, tag_name => 'form',                          {type => END_TAG_TOKEN, tag_name => 'form',
6860                           line => $token->{line}, column => $token->{column}};                           line => $token->{line}, column => $token->{column}};
           $token = shift @tokens;  
6861            !!!back-token (@tokens);            !!!back-token (@tokens);
6862            redo B;            !!!next-token;
6863              next B;
6864          }          }
6865        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
6866          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
6867          my $el;          my $el;
6868          !!!create-element ($el, $token->{tag_name}, $token->{attributes}, $token);          !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
6869                    
6870          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
6871          $self->{content_model} = RCDATA_CONTENT_MODEL;          $self->{content_model} = RCDATA_CONTENT_MODEL;
# Line 5974  sub _tree_construction_main ($) { Line 6874  sub _tree_construction_main ($) {
6874          $insert->($el);          $insert->($el);
6875                    
6876          my $text = '';          my $text = '';
6877            !!!nack ('t392.1');
6878          !!!next-token;          !!!next-token;
6879          if ($token->{type} == CHARACTER_TOKEN) {          if ($token->{type} == CHARACTER_TOKEN) {
6880            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
# Line 6004  sub _tree_construction_main ($) { Line 6905  sub _tree_construction_main ($) {
6905            ## Ignore the token            ## Ignore the token
6906          } else {          } else {
6907            !!!cp ('t398');            !!!cp ('t398');
6908            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);            !!!parse-error (type => 'in RCDATA:#eof', token => $token);
6909          }          }
6910          !!!next-token;          !!!next-token;
6911            next B;
6912          } elsif ($token->{tag_name} eq 'rt' or
6913                   $token->{tag_name} eq 'rp') {
6914            ## has a |ruby| element in scope
6915            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6916              my $node = $self->{open_elements}->[$_];
6917              if ($node->[1] & RUBY_EL) {
6918                !!!cp ('t398.1');
6919                ## generate implied end tags
6920                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
6921                  !!!cp ('t398.2');
6922                  pop @{$self->{open_elements}};
6923                }
6924                unless ($self->{open_elements}->[-1]->[1] & RUBY_EL) {
6925                  !!!cp ('t398.3');
6926                  !!!parse-error (type => 'not closed',
6927                                  text => $self->{open_elements}->[-1]->[0]
6928                                      ->manakai_local_name,
6929                                  token => $token);
6930                  pop @{$self->{open_elements}}
6931                      while not $self->{open_elements}->[-1]->[1] & RUBY_EL;
6932                }
6933                last INSCOPE;
6934              } elsif ($node->[1] & SCOPING_EL) {
6935                !!!cp ('t398.4');
6936                last INSCOPE;
6937              }
6938            } # INSCOPE
6939    
6940            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6941    
6942            !!!nack ('t398.5');
6943            !!!next-token;
6944          redo B;          redo B;
6945          } elsif ($token->{tag_name} eq 'math' or
6946                   $token->{tag_name} eq 'svg') {
6947            $reconstruct_active_formatting_elements->($insert_to_current);
6948    
6949            ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
6950    
6951            ## "adjust foreign attributes" - done in insert-element-f
6952            
6953            !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
6954            
6955            if ($self->{self_closing}) {
6956              pop @{$self->{open_elements}};
6957              !!!ack ('t398.1');
6958            } else {
6959              !!!cp ('t398.2');
6960              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
6961              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
6962              ## mode, "in body" (not "in foreign content") secondary insertion
6963              ## mode, maybe.
6964            }
6965    
6966            !!!next-token;
6967            next B;
6968        } elsif ({        } elsif ({
6969                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
6970                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 6015  sub _tree_construction_main ($) { Line 6972  sub _tree_construction_main ($) {
6972                  thead => 1, tr => 1,                  thead => 1, tr => 1,
6973                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6974          !!!cp ('t401');          !!!cp ('t401');
6975          !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'in body',
6976                            text => $token->{tag_name}, token => $token);
6977          ## Ignore the token          ## Ignore the token
6978            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
6979          !!!next-token;          !!!next-token;
6980          redo B;          next B;
6981                    
6982          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
6983        } else {        } else {
# Line 6040  sub _tree_construction_main ($) { Line 6999  sub _tree_construction_main ($) {
6999              }->{$token->{tag_name}}) {              }->{$token->{tag_name}}) {
7000            !!!cp ('t380');            !!!cp ('t380');
7001            push @$active_formatting_elements, ['#marker', ''];            push @$active_formatting_elements, ['#marker', ''];
7002              !!!nack ('t380.1');
7003          } elsif ({          } elsif ({
7004                    b => 1, big => 1, em => 1, font => 1, i => 1,                    b => 1, big => 1, em => 1, font => 1, i => 1,
7005                    s => 1, small => 1, strile => 1,                    s => 1, small => 1, strile => 1,
# Line 6047  sub _tree_construction_main ($) { Line 7007  sub _tree_construction_main ($) {
7007                   }->{$token->{tag_name}}) {                   }->{$token->{tag_name}}) {
7008            !!!cp ('t375');            !!!cp ('t375');
7009            push @$active_formatting_elements, $self->{open_elements}->[-1];            push @$active_formatting_elements, $self->{open_elements}->[-1];
7010              !!!nack ('t375.1');
7011          } elsif ($token->{tag_name} eq 'input') {          } elsif ($token->{tag_name} eq 'input') {
7012            !!!cp ('t388');            !!!cp ('t388');
7013            ## TODO: associate with $self->{form_element} if defined            ## TODO: associate with $self->{form_element} if defined
7014            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
7015              !!!ack ('t388.2');
7016          } elsif ({          } elsif ({
7017                    area => 1, basefont => 1, bgsound => 1, br => 1,                    area => 1, basefont => 1, bgsound => 1, br => 1,
7018                    embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                    embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
# Line 6058  sub _tree_construction_main ($) { Line 7020  sub _tree_construction_main ($) {
7020                   }->{$token->{tag_name}}) {                   }->{$token->{tag_name}}) {
7021            !!!cp ('t388.1');            !!!cp ('t388.1');
7022            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
7023              !!!ack ('t388.3');
7024          } elsif ($token->{tag_name} eq 'select') {          } elsif ($token->{tag_name} eq 'select') {
7025            ## TODO: associate with $self->{form_element} if defined            ## TODO: associate with $self->{form_element} if defined
7026                    
# Line 6070  sub _tree_construction_main ($) { Line 7033  sub _tree_construction_main ($) {
7033              !!!cp ('t400.2');              !!!cp ('t400.2');
7034              $self->{insertion_mode} = IN_SELECT_IM;              $self->{insertion_mode} = IN_SELECT_IM;
7035            }            }
7036              !!!nack ('t400.3');
7037          } else {          } else {
7038            !!!cp ('t402');            !!!nack ('t402');
7039          }          }
7040                    
7041          !!!next-token;          !!!next-token;
7042          redo B;          next B;
7043        }        }
7044      } elsif ($token->{type} == END_TAG_TOKEN) {      } elsif ($token->{type} == END_TAG_TOKEN) {
7045        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
# Line 6083  sub _tree_construction_main ($) { Line 7047  sub _tree_construction_main ($) {
7047          my $i;          my $i;
7048          INSCOPE: {          INSCOPE: {
7049            for (reverse @{$self->{open_elements}}) {            for (reverse @{$self->{open_elements}}) {
7050              if ($_->[1] eq 'body') {              if ($_->[1] & BODY_EL) {
7051                !!!cp ('t405');                !!!cp ('t405');
7052                $i = $_;                $i = $_;
7053                last INSCOPE;                last INSCOPE;
7054              } elsif ({              } elsif ($_->[1] & SCOPING_EL) {
                       applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                       button => 1, marquee => 1, object => 1, html => 1,  
                      }->{$_->[1]}) {  
7055                !!!cp ('t405.1');                !!!cp ('t405.1');
7056                last;                last;
7057              }              }
7058            }            }
7059    
7060            !!!parse-error (type => 'start tag not allowed',            !!!parse-error (type => 'start tag not allowed',
7061                            value => $token->{tag_name}, token => $token);                            text => $token->{tag_name}, token => $token);
7062            ## NOTE: Ignore the token.            ## NOTE: Ignore the token.
7063            !!!next-token;            !!!next-token;
7064            redo B;            next B;
7065          } # INSCOPE          } # INSCOPE
7066    
7067          for (@{$self->{open_elements}}) {          for (@{$self->{open_elements}}) {
7068            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]}) {  
7069              !!!cp ('t403');              !!!cp ('t403');
7070              !!!parse-error (type => 'not closed:'.$_->[1], token => $token);              !!!parse-error (type => 'not closed',
7071                                text => $_->[0]->manakai_local_name,
7072                                token => $token);
7073              last;              last;
7074            } else {            } else {
7075              !!!cp ('t404');              !!!cp ('t404');
# Line 6119  sub _tree_construction_main ($) { Line 7078  sub _tree_construction_main ($) {
7078    
7079          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
7080          !!!next-token;          !!!next-token;
7081          redo B;          next B;
7082        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
7083          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
7084            ## up-to-date, though it has same effect as speced.
7085            if (@{$self->{open_elements}} > 1 and
7086                $self->{open_elements}->[1]->[1] & BODY_EL) {
7087            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
7088            if ($self->{open_elements}->[-1]->[1] ne 'body') {            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
7089              !!!cp ('t406');              !!!cp ('t406');
7090              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1], token => $token);              !!!parse-error (type => 'not closed',
7091                                text => $self->{open_elements}->[1]->[0]
7092                                    ->manakai_local_name,
7093                                token => $token);
7094            } else {            } else {
7095              !!!cp ('t407');              !!!cp ('t407');
7096            }            }
7097            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
7098            ## reprocess            ## reprocess
7099            redo B;            next B;
7100          } else {          } else {
7101            !!!cp ('t408');            !!!cp ('t408');
7102            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
7103                              text => $token->{tag_name}, token => $token);
7104            ## Ignore the token            ## Ignore the token
7105            !!!next-token;            !!!next-token;
7106            redo B;            next B;
7107          }          }
7108        } elsif ({        } elsif ({
7109                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
# Line 6150  sub _tree_construction_main ($) { Line 7116  sub _tree_construction_main ($) {
7116          my $i;          my $i;
7117          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7118            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7119            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7120              !!!cp ('t410');              !!!cp ('t410');
7121              $i = $_;              $i = $_;
7122              last INSCOPE;              last INSCOPE;
7123            } 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]}) {  
7124              !!!cp ('t411');              !!!cp ('t411');
7125              last INSCOPE;              last INSCOPE;
7126            }            }
# Line 6165  sub _tree_construction_main ($) { Line 7128  sub _tree_construction_main ($) {
7128    
7129          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
7130            !!!cp ('t413');            !!!cp ('t413');
7131            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
7132                              text => $token->{tag_name}, token => $token);
7133          } else {          } else {
7134            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7135            while ({            while ({
7136                      ## END_TAG_OPTIONAL_EL
7137                    dd => ($token->{tag_name} ne 'dd'),                    dd => ($token->{tag_name} ne 'dd'),
7138                    dt => ($token->{tag_name} ne 'dt'),                    dt => ($token->{tag_name} ne 'dt'),
7139                    li => ($token->{tag_name} ne 'li'),                    li => ($token->{tag_name} ne 'li'),
7140                    p => 1,                    p => 1,
7141                   }->{$self->{open_elements}->[-1]->[1]}) {                    rt => 1,
7142                      rp => 1,
7143                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
7144              !!!cp ('t409');              !!!cp ('t409');
7145              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
7146            }            }
7147    
7148            ## Step 2.            ## Step 2.
7149            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7150                      ne $token->{tag_name}) {
7151              !!!cp ('t412');              !!!cp ('t412');
7152              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);              !!!parse-error (type => 'not closed',
7153                                text => $self->{open_elements}->[-1]->[0]
7154                                    ->manakai_local_name,
7155                                token => $token);
7156            } else {            } else {
7157              !!!cp ('t414');              !!!cp ('t414');
7158            }            }
# Line 6196  sub _tree_construction_main ($) { Line 7167  sub _tree_construction_main ($) {
7167                }->{$token->{tag_name}};                }->{$token->{tag_name}};
7168          }          }
7169          !!!next-token;          !!!next-token;
7170          redo B;          next B;
7171        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
7172          undef $self->{form_element};          undef $self->{form_element};
7173    
# Line 6204  sub _tree_construction_main ($) { Line 7175  sub _tree_construction_main ($) {
7175          my $i;          my $i;
7176          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7177            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7178            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & FORM_EL) {
7179              !!!cp ('t418');              !!!cp ('t418');
7180              $i = $_;              $i = $_;
7181              last INSCOPE;              last INSCOPE;
7182            } 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]}) {  
7183              !!!cp ('t419');              !!!cp ('t419');
7184              last INSCOPE;              last INSCOPE;
7185            }            }
# Line 6219  sub _tree_construction_main ($) { Line 7187  sub _tree_construction_main ($) {
7187    
7188          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
7189            !!!cp ('t421');            !!!cp ('t421');
7190            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
7191                              text => $token->{tag_name}, token => $token);
7192          } else {          } else {
7193            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7194            while ({            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                   dd => 1, dt => 1, li => 1, p => 1,  
                  }->{$self->{open_elements}->[-1]->[1]}) {  
7195              !!!cp ('t417');              !!!cp ('t417');
7196              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
7197            }            }
7198                        
7199            ## Step 2.            ## Step 2.
7200            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7201                      ne $token->{tag_name}) {
7202              !!!cp ('t417.1');              !!!cp ('t417.1');
7203              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);              !!!parse-error (type => 'not closed',
7204                                text => $self->{open_elements}->[-1]->[0]
7205                                    ->manakai_local_name,
7206                                token => $token);
7207            } else {            } else {
7208              !!!cp ('t420');              !!!cp ('t420');
7209            }              }  
# Line 6242  sub _tree_construction_main ($) { Line 7213  sub _tree_construction_main ($) {
7213          }          }
7214    
7215          !!!next-token;          !!!next-token;
7216          redo B;          next B;
7217        } elsif ({        } elsif ({
7218                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
7219                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 6250  sub _tree_construction_main ($) { Line 7221  sub _tree_construction_main ($) {
7221          my $i;          my $i;
7222          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7223            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7224            if ({            if ($node->[1] & HEADING_EL) {
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
7225              !!!cp ('t423');              !!!cp ('t423');
7226              $i = $_;              $i = $_;
7227              last INSCOPE;              last INSCOPE;
7228            } 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]}) {  
7229              !!!cp ('t424');              !!!cp ('t424');
7230              last INSCOPE;              last INSCOPE;
7231            }            }
# Line 6267  sub _tree_construction_main ($) { Line 7233  sub _tree_construction_main ($) {
7233    
7234          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
7235            !!!cp ('t425.1');            !!!cp ('t425.1');
7236            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
7237                              text => $token->{tag_name}, token => $token);
7238          } else {          } else {
7239            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7240            while ({            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                   dd => 1, dt => 1, li => 1, p => 1,  
                  }->{$self->{open_elements}->[-1]->[1]}) {  
7241              !!!cp ('t422');              !!!cp ('t422');
7242              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
7243            }            }
7244                        
7245            ## Step 2.            ## Step 2.
7246            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7247                      ne $token->{tag_name}) {
7248              !!!cp ('t425');              !!!cp ('t425');
7249              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag',
7250                                text => $token->{tag_name}, token => $token);
7251            } else {            } else {
7252              !!!cp ('t426');              !!!cp ('t426');
7253            }            }
# Line 6290  sub _tree_construction_main ($) { Line 7257  sub _tree_construction_main ($) {
7257          }          }
7258                    
7259          !!!next-token;          !!!next-token;
7260          redo B;          next B;
7261        } elsif ($token->{tag_name} eq 'p') {        } elsif ($token->{tag_name} eq 'p') {
7262          ## has an element in scope          ## has an element in scope
7263          my $i;          my $i;
7264          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7265            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7266            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & P_EL) {
7267              !!!cp ('t410.1');              !!!cp ('t410.1');
7268              $i = $_;              $i = $_;
7269              last INSCOPE;              last INSCOPE;
7270            } 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]}) {  
7271              !!!cp ('t411.1');              !!!cp ('t411.1');
7272              last INSCOPE;              last INSCOPE;
7273            }            }
7274          } # INSCOPE          } # INSCOPE
7275    
7276          if (defined $i) {          if (defined $i) {
7277            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7278                      ne $token->{tag_name}) {
7279              !!!cp ('t412.1');              !!!cp ('t412.1');
7280              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);              !!!parse-error (type => 'not closed',
7281                                text => $self->{open_elements}->[-1]->[0]
7282                                    ->manakai_local_name,
7283                                token => $token);
7284            } else {            } else {
7285              !!!cp ('t414.1');              !!!cp ('t414.1');
7286            }            }
# Line 6320  sub _tree_construction_main ($) { Line 7288  sub _tree_construction_main ($) {
7288            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
7289          } else {          } else {
7290            !!!cp ('t413.1');            !!!cp ('t413.1');
7291            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
7292                              text => $token->{tag_name}, token => $token);
7293    
7294            !!!cp ('t415.1');            !!!cp ('t415.1');
7295            ## As if <p>, then reprocess the current token            ## As if <p>, then reprocess the current token
7296            my $el;            my $el;
7297            !!!create-element ($el, 'p',, $token);            !!!create-element ($el, $HTML_NS, 'p',, $token);
7298            $insert->($el);            $insert->($el);
7299            ## NOTE: Not inserted into |$self->{open_elements}|.            ## NOTE: Not inserted into |$self->{open_elements}|.
7300          }          }
7301    
7302          !!!next-token;          !!!next-token;
7303          redo B;          next B;
7304        } elsif ({        } elsif ({
7305                  a => 1,                  a => 1,
7306                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
# Line 6340  sub _tree_construction_main ($) { Line 7309  sub _tree_construction_main ($) {
7309                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7310          !!!cp ('t427');          !!!cp ('t427');
7311          $formatting_end_tag->($token);          $formatting_end_tag->($token);
7312          redo B;          next B;
7313        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
7314          !!!cp ('t428');          !!!cp ('t428');
7315          !!!parse-error (type => 'unmatched end tag:br', token => $token);          !!!parse-error (type => 'unmatched end tag',
7316                            text => 'br', token => $token);
7317    
7318          ## As if <br>          ## As if <br>
7319          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7320                    
7321          my $el;          my $el;
7322          !!!create-element ($el, 'br',, $token);          !!!create-element ($el, $HTML_NS, 'br',, $token);
7323          $insert->($el);          $insert->($el);
7324                    
7325          ## Ignore the token.          ## Ignore the token.
7326          !!!next-token;          !!!next-token;
7327          redo B;          next B;
7328        } elsif ({        } elsif ({
7329                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7330                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 6368  sub _tree_construction_main ($) { Line 7338  sub _tree_construction_main ($) {
7338                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
7339                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7340          !!!cp ('t429');          !!!cp ('t429');
7341          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'unmatched end tag',
7342                            text => $token->{tag_name}, token => $token);
7343          ## Ignore the token          ## Ignore the token
7344          !!!next-token;          !!!next-token;
7345          redo B;          next B;
7346                    
7347          ## ISSUE: Issue on HTML5 new elements in spec          ## ISSUE: Issue on HTML5 new elements in spec
7348                    
# Line 6382  sub _tree_construction_main ($) { Line 7353  sub _tree_construction_main ($) {
7353    
7354          ## Step 2          ## Step 2
7355          S2: {          S2: {
7356            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7357              ## Step 1              ## Step 1
7358              ## generate implied end tags              ## generate implied end tags
7359              while ({              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                     dd => 1, dt => 1, li => 1, p => 1,  
                    }->{$self->{open_elements}->[-1]->[1]}) {  
7360                !!!cp ('t430');                !!!cp ('t430');
7361                ## ISSUE: Can this case be reached?                ## NOTE: |<ruby><rt></ruby>|.
7362                  ## ISSUE: <ruby><rt></rt> will also take this code path,
7363                  ## which seems wrong.
7364                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
7365                  $node_i++;
7366              }              }
7367                    
7368              ## Step 2              ## Step 2
7369              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7370                        ne $token->{tag_name}) {
7371                !!!cp ('t431');                !!!cp ('t431');
7372                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
7373                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                !!!parse-error (type => 'not closed',
7374                                  text => $self->{open_elements}->[-1]->[0]
7375                                      ->manakai_local_name,
7376                                  token => $token);
7377              } else {              } else {
7378                !!!cp ('t432');                !!!cp ('t432');
7379              }              }
7380                            
7381              ## Step 3              ## Step 3
7382              splice @{$self->{open_elements}}, $node_i;              splice @{$self->{open_elements}}, $node_i if $node_i < 0;
7383    
7384              !!!next-token;              !!!next-token;
7385              last S2;              last S2;
7386            } else {            } else {
7387              ## Step 3              ## Step 3
7388              if (not $formatting_category->{$node->[1]} and              if (not ($node->[1] & FORMATTING_EL) and
7389                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
7390                  ($special_category->{$node->[1]} or                  ($node->[1] & SPECIAL_EL or
7391                   $scoping_category->{$node->[1]})) {                   $node->[1] & SCOPING_EL)) {
7392                !!!cp ('t433');                !!!cp ('t433');
7393                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag',
7394                                  text => $token->{tag_name}, token => $token);
7395                ## Ignore the token                ## Ignore the token
7396                !!!next-token;                !!!next-token;
7397                last S2;                last S2;
# Line 6430  sub _tree_construction_main ($) { Line 7407  sub _tree_construction_main ($) {
7407            ## Step 5;            ## Step 5;
7408            redo S2;            redo S2;
7409          } # S2          } # S2
7410          redo B;          next B;
7411        }        }
7412      }      }
7413      redo B;      next B;
7414      } continue { # B
7415        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7416          ## NOTE: The code below is executed in cases where it does not have
7417          ## to be, but it it is harmless even in those cases.
7418          ## has an element in scope
7419          INSCOPE: {
7420            for (reverse 0..$#{$self->{open_elements}}) {
7421              my $node = $self->{open_elements}->[$_];
7422              if ($node->[1] & FOREIGN_EL) {
7423                last INSCOPE;
7424              } elsif ($node->[1] & SCOPING_EL) {
7425                last;
7426              }
7427            }
7428            
7429            ## NOTE: No foreign element in scope.
7430            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7431          } # INSCOPE
7432        }
7433    } # B    } # B
7434    
7435    ## Stop parsing # MUST    ## Stop parsing # MUST
# Line 6479  sub set_inner_html ($$$) { Line 7475  sub set_inner_html ($$$) {
7475    
7476      ## Step 8 # MUST      ## Step 8 # MUST
7477      my $i = 0;      my $i = 0;
7478      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7479      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7480      $p->{set_next_char} = sub {      $p->{set_next_char} = sub {
7481        my $self = shift;        my $self = shift;
7482    
# Line 6489  sub set_inner_html ($$$) { Line 7485  sub set_inner_html ($$$) {
7485    
7486        $self->{next_char} = -1 and return if $i >= length $$s;        $self->{next_char} = -1 and return if $i >= length $$s;
7487        $self->{next_char} = ord substr $$s, $i++, 1;        $self->{next_char} = ord substr $$s, $i++, 1;
7488        $column++;  
7489          ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7490          $p->{column}++;
7491    
7492        if ($self->{next_char} == 0x000A) { # LF        if ($self->{next_char} == 0x000A) { # LF
7493          $line++;          $p->{line}++;
7494          $column = 0;          $p->{column} = 0;
7495          !!!cp ('i1');          !!!cp ('i1');
7496        } elsif ($self->{next_char} == 0x000D) { # CR        } elsif ($self->{next_char} == 0x000D) { # CR
7497          $i++ if substr ($$s, $i, 1) eq "\x0A";          $i++ if substr ($$s, $i, 1) eq "\x0A";
7498          $self->{next_char} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
7499          $line++;          $p->{line}++;
7500          $column = 0;          $p->{column} = 0;
7501          !!!cp ('i2');          !!!cp ('i2');
7502        } elsif ($self->{next_char} > 0x10FFFF) {        } elsif ($self->{next_char} > 0x10FFFF) {
7503          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
# Line 6508  sub set_inner_html ($$$) { Line 7506  sub set_inner_html ($$$) {
7506          !!!cp ('i4');          !!!cp ('i4');
7507          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
7508          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7509          } elsif ($self->{next_char} <= 0x0008 or
7510                   (0x000E <= $self->{next_char} and
7511                    $self->{next_char} <= 0x001F) or
7512                   (0x007F <= $self->{next_char} and
7513                    $self->{next_char} <= 0x009F) or
7514                   (0xD800 <= $self->{next_char} and
7515                    $self->{next_char} <= 0xDFFF) or
7516                   (0xFDD0 <= $self->{next_char} and
7517                    $self->{next_char} <= 0xFDDF) or
7518                   {
7519                    0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
7520                    0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
7521                    0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
7522                    0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
7523                    0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
7524                    0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
7525                    0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
7526                    0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
7527                    0x10FFFE => 1, 0x10FFFF => 1,
7528                   }->{$self->{next_char}}) {
7529            !!!cp ('i4.1');
7530            if ($self->{next_char} < 0x10000) {
7531              !!!parse-error (type => 'control char',
7532                              text => (sprintf 'U+%04X', $self->{next_char}));
7533            } else {
7534              !!!parse-error (type => 'control char',
7535                              text => (sprintf 'U-%08X', $self->{next_char}));
7536            }
7537        }        }
7538      };      };
7539      $p->{prev_char} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];
# Line 6515  sub set_inner_html ($$$) { Line 7541  sub set_inner_html ($$$) {
7541            
7542      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7543        my (%opt) = @_;        my (%opt) = @_;
7544        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7545          my $column = $opt{column};
7546          if (defined $opt{token} and defined $opt{token}->{line}) {
7547            $line = $opt{token}->{line};
7548            $column = $opt{token}->{column};
7549          }
7550          warn "Parse error ($opt{type}) at line $line column $column\n";
7551      };      };
7552      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7553        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7554      };      };
7555            
7556      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
# Line 6542  sub set_inner_html ($$$) { Line 7574  sub set_inner_html ($$$) {
7574          unless defined $p->{content_model};          unless defined $p->{content_model};
7575          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
7576    
7577      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7578          ## TODO: Foreign element OK?
7579    
7580      ## Step 3      ## Step 3
7581      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
# Line 6552  sub set_inner_html ($$$) { Line 7585  sub set_inner_html ($$$) {
7585      $doc->append_child ($root);      $doc->append_child ($root);
7586    
7587      ## Step 5 # MUST      ## Step 5 # MUST
7588      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7589    
7590      undef $p->{head_element};      undef $p->{head_element};
7591    
# Line 6598  sub set_inner_html ($$$) { Line 7631  sub set_inner_html ($$$) {
7631      ## ISSUE: mutation events?      ## ISSUE: mutation events?
7632    
7633      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
7634    
7635        delete $p->{parse_error}; # delete loop
7636    } else {    } else {
7637      die "$0: |set_inner_html| is not defined for node of type $nt";      die "$0: |set_inner_html| is not defined for node of type $nt";
7638    }    }

Legend:
Removed from v.1.116  
changed lines
  Added in v.1.153

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24