/[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.120 by wakaba, Thu Mar 20 03:57:00 2008 UTC revision 1.159 by wakaba, Fri Sep 5 17:57:47 2008 UTC
# Line 8  use Error qw(:try); Line 8  use Error qw(:try);
8  ## doc.write ('');  ## doc.write ('');
9  ## alert (doc.compatMode);  ## alert (doc.compatMode);
10    
11  ## TODO: Control charcters and noncharacters are not allowed (HTML5 revision 1263)  require IO::Handle;
12  ## TODO: 1252 parse error (revision 1264)  
13  ## TODO: 8859-11 = 874 (revision 1271)  my $HTML_NS = q<http://www.w3.org/1999/xhtml>;
14    my $MML_NS = q<http://www.w3.org/1998/Math/MathML>;
15  my $permitted_slash_tag_name = {  my $SVG_NS = q<http://www.w3.org/2000/svg>;
16    base => 1,  my $XLINK_NS = q<http://www.w3.org/1999/xlink>;
17    link => 1,  my $XML_NS = q<http://www.w3.org/XML/1998/namespace>;
18    meta => 1,  my $XMLNS_NS = q<http://www.w3.org/2000/xmlns/>;
19    hr => 1,  
20    br => 1,  sub A_EL () { 0b1 }
21    img => 1,  sub ADDRESS_EL () { 0b10 }
22    embed => 1,  sub BODY_EL () { 0b100 }
23    param => 1,  sub BUTTON_EL () { 0b1000 }
24    area => 1,  sub CAPTION_EL () { 0b10000 }
25    col => 1,  sub DD_EL () { 0b100000 }
26    input => 1,  sub DIV_EL () { 0b1000000 }
27    sub DT_EL () { 0b10000000 }
28    sub FORM_EL () { 0b100000000 }
29    sub FORMATTING_EL () { 0b1000000000 }
30    sub FRAMESET_EL () { 0b10000000000 }
31    sub HEADING_EL () { 0b100000000000 }
32    sub HTML_EL () { 0b1000000000000 }
33    sub LI_EL () { 0b10000000000000 }
34    sub NOBR_EL () { 0b100000000000000 }
35    sub OPTION_EL () { 0b1000000000000000 }
36    sub OPTGROUP_EL () { 0b10000000000000000 }
37    sub P_EL () { 0b100000000000000000 }
38    sub SELECT_EL () { 0b1000000000000000000 }
39    sub TABLE_EL () { 0b10000000000000000000 }
40    sub TABLE_CELL_EL () { 0b100000000000000000000 }
41    sub TABLE_ROW_EL () { 0b1000000000000000000000 }
42    sub TABLE_ROW_GROUP_EL () { 0b10000000000000000000000 }
43    sub MISC_SCOPING_EL () { 0b100000000000000000000000 }
44    sub MISC_SPECIAL_EL () { 0b1000000000000000000000000 }
45    sub FOREIGN_EL () { 0b10000000000000000000000000 }
46    sub FOREIGN_FLOW_CONTENT_EL () { 0b100000000000000000000000000 }
47    sub MML_AXML_EL () { 0b1000000000000000000000000000 }
48    sub RUBY_EL () { 0b10000000000000000000000000000 }
49    sub RUBY_COMPONENT_EL () { 0b100000000000000000000000000000 }
50    
51    sub TABLE_ROWS_EL () {
52      TABLE_EL |
53      TABLE_ROW_EL |
54      TABLE_ROW_GROUP_EL
55    }
56    
57    ## NOTE: Used in "generate implied end tags" algorithm.
58    ## NOTE: There is a code where a modified version of END_TAG_OPTIONAL_EL
59    ## is used in "generate implied end tags" implementation (search for the
60    ## function mae).
61    sub END_TAG_OPTIONAL_EL () {
62      DD_EL |
63      DT_EL |
64      LI_EL |
65      P_EL |
66      RUBY_COMPONENT_EL
67    }
68    
69    ## NOTE: Used in </body> and EOF algorithms.
70    sub ALL_END_TAG_OPTIONAL_EL () {
71      DD_EL |
72      DT_EL |
73      LI_EL |
74      P_EL |
75    
76      BODY_EL |
77      HTML_EL |
78      TABLE_CELL_EL |
79      TABLE_ROW_EL |
80      TABLE_ROW_GROUP_EL
81    }
82    
83    sub SCOPING_EL () {
84      BUTTON_EL |
85      CAPTION_EL |
86      HTML_EL |
87      TABLE_EL |
88      TABLE_CELL_EL |
89      MISC_SCOPING_EL
90    }
91    
92    sub TABLE_SCOPING_EL () {
93      HTML_EL |
94      TABLE_EL
95    }
96    
97    sub TABLE_ROWS_SCOPING_EL () {
98      HTML_EL |
99      TABLE_ROW_GROUP_EL
100    }
101    
102    sub TABLE_ROW_SCOPING_EL () {
103      HTML_EL |
104      TABLE_ROW_EL
105    }
106    
107    sub SPECIAL_EL () {
108      ADDRESS_EL |
109      BODY_EL |
110      DIV_EL |
111    
112      DD_EL |
113      DT_EL |
114      LI_EL |
115      P_EL |
116    
117      FORM_EL |
118      FRAMESET_EL |
119      HEADING_EL |
120      OPTION_EL |
121      OPTGROUP_EL |
122      SELECT_EL |
123      TABLE_ROW_EL |
124      TABLE_ROW_GROUP_EL |
125      MISC_SPECIAL_EL
126    }
127    
128    my $el_category = {
129      a => A_EL | FORMATTING_EL,
130      address => ADDRESS_EL,
131      applet => MISC_SCOPING_EL,
132      area => MISC_SPECIAL_EL,
133      b => FORMATTING_EL,
134      base => MISC_SPECIAL_EL,
135      basefont => MISC_SPECIAL_EL,
136      bgsound => MISC_SPECIAL_EL,
137      big => FORMATTING_EL,
138      blockquote => MISC_SPECIAL_EL,
139      body => BODY_EL,
140      br => MISC_SPECIAL_EL,
141      button => BUTTON_EL,
142      caption => CAPTION_EL,
143      center => MISC_SPECIAL_EL,
144      col => MISC_SPECIAL_EL,
145      colgroup => MISC_SPECIAL_EL,
146      dd => DD_EL,
147      dir => MISC_SPECIAL_EL,
148      div => DIV_EL,
149      dl => MISC_SPECIAL_EL,
150      dt => DT_EL,
151      em => FORMATTING_EL,
152      embed => MISC_SPECIAL_EL,
153      fieldset => MISC_SPECIAL_EL,
154      font => FORMATTING_EL,
155      form => FORM_EL,
156      frame => MISC_SPECIAL_EL,
157      frameset => FRAMESET_EL,
158      h1 => HEADING_EL,
159      h2 => HEADING_EL,
160      h3 => HEADING_EL,
161      h4 => HEADING_EL,
162      h5 => HEADING_EL,
163      h6 => HEADING_EL,
164      head => MISC_SPECIAL_EL,
165      hr => MISC_SPECIAL_EL,
166      html => HTML_EL,
167      i => FORMATTING_EL,
168      iframe => MISC_SPECIAL_EL,
169      img => MISC_SPECIAL_EL,
170      input => MISC_SPECIAL_EL,
171      isindex => MISC_SPECIAL_EL,
172      li => LI_EL,
173      link => MISC_SPECIAL_EL,
174      listing => MISC_SPECIAL_EL,
175      marquee => MISC_SCOPING_EL,
176      menu => MISC_SPECIAL_EL,
177      meta => MISC_SPECIAL_EL,
178      nobr => NOBR_EL | FORMATTING_EL,
179      noembed => MISC_SPECIAL_EL,
180      noframes => MISC_SPECIAL_EL,
181      noscript => MISC_SPECIAL_EL,
182      object => MISC_SCOPING_EL,
183      ol => MISC_SPECIAL_EL,
184      optgroup => OPTGROUP_EL,
185      option => OPTION_EL,
186      p => P_EL,
187      param => MISC_SPECIAL_EL,
188      plaintext => MISC_SPECIAL_EL,
189      pre => MISC_SPECIAL_EL,
190      rp => RUBY_COMPONENT_EL,
191      rt => RUBY_COMPONENT_EL,
192      ruby => RUBY_EL,
193      s => FORMATTING_EL,
194      script => MISC_SPECIAL_EL,
195      select => SELECT_EL,
196      small => FORMATTING_EL,
197      spacer => MISC_SPECIAL_EL,
198      strike => FORMATTING_EL,
199      strong => FORMATTING_EL,
200      style => MISC_SPECIAL_EL,
201      table => TABLE_EL,
202      tbody => TABLE_ROW_GROUP_EL,
203      td => TABLE_CELL_EL,
204      textarea => MISC_SPECIAL_EL,
205      tfoot => TABLE_ROW_GROUP_EL,
206      th => TABLE_CELL_EL,
207      thead => TABLE_ROW_GROUP_EL,
208      title => MISC_SPECIAL_EL,
209      tr => TABLE_ROW_EL,
210      tt => FORMATTING_EL,
211      u => FORMATTING_EL,
212      ul => MISC_SPECIAL_EL,
213      wbr => MISC_SPECIAL_EL,
214    };
215    
216    my $el_category_f = {
217      $MML_NS => {
218        'annotation-xml' => MML_AXML_EL,
219        mi => FOREIGN_FLOW_CONTENT_EL,
220        mo => FOREIGN_FLOW_CONTENT_EL,
221        mn => FOREIGN_FLOW_CONTENT_EL,
222        ms => FOREIGN_FLOW_CONTENT_EL,
223        mtext => FOREIGN_FLOW_CONTENT_EL,
224      },
225      $SVG_NS => {
226        foreignObject => FOREIGN_FLOW_CONTENT_EL,
227        desc => FOREIGN_FLOW_CONTENT_EL,
228        title => FOREIGN_FLOW_CONTENT_EL,
229      },
230      ## NOTE: In addition, FOREIGN_EL is set to non-HTML elements.
231  };  };
232    
233    my $svg_attr_name = {
234      attributename => 'attributeName',
235      attributetype => 'attributeType',
236      basefrequency => 'baseFrequency',
237      baseprofile => 'baseProfile',
238      calcmode => 'calcMode',
239      clippathunits => 'clipPathUnits',
240      contentscripttype => 'contentScriptType',
241      contentstyletype => 'contentStyleType',
242      diffuseconstant => 'diffuseConstant',
243      edgemode => 'edgeMode',
244      externalresourcesrequired => 'externalResourcesRequired',
245      filterres => 'filterRes',
246      filterunits => 'filterUnits',
247      glyphref => 'glyphRef',
248      gradienttransform => 'gradientTransform',
249      gradientunits => 'gradientUnits',
250      kernelmatrix => 'kernelMatrix',
251      kernelunitlength => 'kernelUnitLength',
252      keypoints => 'keyPoints',
253      keysplines => 'keySplines',
254      keytimes => 'keyTimes',
255      lengthadjust => 'lengthAdjust',
256      limitingconeangle => 'limitingConeAngle',
257      markerheight => 'markerHeight',
258      markerunits => 'markerUnits',
259      markerwidth => 'markerWidth',
260      maskcontentunits => 'maskContentUnits',
261      maskunits => 'maskUnits',
262      numoctaves => 'numOctaves',
263      pathlength => 'pathLength',
264      patterncontentunits => 'patternContentUnits',
265      patterntransform => 'patternTransform',
266      patternunits => 'patternUnits',
267      pointsatx => 'pointsAtX',
268      pointsaty => 'pointsAtY',
269      pointsatz => 'pointsAtZ',
270      preservealpha => 'preserveAlpha',
271      preserveaspectratio => 'preserveAspectRatio',
272      primitiveunits => 'primitiveUnits',
273      refx => 'refX',
274      refy => 'refY',
275      repeatcount => 'repeatCount',
276      repeatdur => 'repeatDur',
277      requiredextensions => 'requiredExtensions',
278      requiredfeatures => 'requiredFeatures',
279      specularconstant => 'specularConstant',
280      specularexponent => 'specularExponent',
281      spreadmethod => 'spreadMethod',
282      startoffset => 'startOffset',
283      stddeviation => 'stdDeviation',
284      stitchtiles => 'stitchTiles',
285      surfacescale => 'surfaceScale',
286      systemlanguage => 'systemLanguage',
287      tablevalues => 'tableValues',
288      targetx => 'targetX',
289      targety => 'targetY',
290      textlength => 'textLength',
291      viewbox => 'viewBox',
292      viewtarget => 'viewTarget',
293      xchannelselector => 'xChannelSelector',
294      ychannelselector => 'yChannelSelector',
295      zoomandpan => 'zoomAndPan',
296    };
297    
298    my $foreign_attr_xname = {
299      'xlink:actuate' => [$XLINK_NS, ['xlink', 'actuate']],
300      'xlink:arcrole' => [$XLINK_NS, ['xlink', 'arcrole']],
301      'xlink:href' => [$XLINK_NS, ['xlink', 'href']],
302      'xlink:role' => [$XLINK_NS, ['xlink', 'role']],
303      'xlink:show' => [$XLINK_NS, ['xlink', 'show']],
304      'xlink:title' => [$XLINK_NS, ['xlink', 'title']],
305      'xlink:type' => [$XLINK_NS, ['xlink', 'type']],
306      'xml:base' => [$XML_NS, ['xml', 'base']],
307      'xml:lang' => [$XML_NS, ['xml', 'lang']],
308      'xml:space' => [$XML_NS, ['xml', 'space']],
309      'xmlns' => [$XMLNS_NS, [undef, 'xmlns']],
310      'xmlns:xlink' => [$XMLNS_NS, ['xmlns', 'xlink']],
311    };
312    
313    ## ISSUE: xmlns:xlink="non-xlink-ns" is not an error.
314    
315  my $c1_entity_char = {  my $c1_entity_char = {
316    0x80 => 0x20AC,    0x80 => 0x20AC,
317    0x81 => 0xFFFD,    0x81 => 0xFFFD,
# Line 61  my $c1_entity_char = { Line 347  my $c1_entity_char = {
347    0x9F => 0x0178,    0x9F => 0x0178,
348  }; # $c1_entity_char  }; # $c1_entity_char
349    
 my $special_category = {  
   address => 1, area => 1, base => 1, basefont => 1, bgsound => 1,  
   blockquote => 1, body => 1, br => 1, center => 1, col => 1, colgroup => 1,  
   dd => 1, dir => 1, div => 1, dl => 1, dt => 1, embed => 1, fieldset => 1,  
   form => 1, frame => 1, frameset => 1, h1 => 1, h2 => 1, h3 => 1,  
   h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, iframe => 1, image => 1,  
   img => 1, input => 1, isindex => 1, li => 1, link => 1, listing => 1,  
   menu => 1, meta => 1, noembed => 1, noframes => 1, noscript => 1,  
   ol => 1, optgroup => 1, option => 1, p => 1, param => 1, plaintext => 1,  
   pre => 1, script => 1, select => 1, spacer => 1, style => 1, tbody => 1,  
   textarea => 1, tfoot => 1, thead => 1, title => 1, tr => 1, ul => 1, wbr => 1,  
 };  
 my $scoping_category = {  
   applet => 1, button => 1, caption => 1, html => 1, marquee => 1, object => 1,  
   table => 1, td => 1, th => 1,  
 };  
 my $formatting_category = {  
   a => 1, b => 1, big => 1, em => 1, font => 1, i => 1, nobr => 1,  
   s => 1, small => 1, strile => 1, strong => 1, tt => 1, u => 1,  
 };  
 # $phrasing_category: all other elements  
   
350  sub parse_byte_string ($$$$;$) {  sub parse_byte_string ($$$$;$) {
351      my $self = shift;
352      my $charset_name = shift;
353      open my $input, '<', ref $_[0] ? $_[0] : \($_[0]);
354      return $self->parse_byte_stream ($charset_name, $input, @_[1..$#_]);
355    } # parse_byte_string
356    
357    sub parse_byte_stream ($$$$;$) {
358    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
359    my $charset = shift;    my $charset_name = shift;
360    my $bytes_s = ref $_[0] ? $_[0] : \($_[0]);    my $byte_stream = $_[0];
   my $s;  
     
   if (defined $charset) {  
     require Encode; ## TODO: decode(utf8) don't delete BOM  
     $s = \ (Encode::decode ($charset, $$bytes_s));  
     $self->{input_encoding} = lc $charset; ## TODO: normalize name  
     $self->{confident} = 1;  
   } else {  
     ## TODO: Implement HTML5 detection algorithm  
     require Whatpm::Charset::UniversalCharDet;  
     $charset = Whatpm::Charset::UniversalCharDet->detect_byte_string  
         (substr ($$bytes_s, 0, 1024));  
     $charset ||= 'windows-1252';  
     $s = \ (Encode::decode ($charset, $$bytes_s));  
     $self->{input_encoding} = $charset;  
     $self->{confident} = 0;  
   }  
361    
362    $self->{change_encoding} = sub {    my $onerror = $_[2] || sub {
363      my $self = shift;      my (%opt) = @_;
364      my $charset = lc shift;      warn "Parse error ($opt{type})\n";
365      my $token = shift;    };
366      ## TODO: if $charset is supported    $self->{parse_error} = $onerror; # updated later by parse_char_string
     ## TODO: normalize charset name  
367    
368      ## "Change the encoding" algorithm:    ## HTML5 encoding sniffing algorithm
369      require Message::Charset::Info;
370      my $charset;
371      my $buffer;
372      my ($char_stream, $e_status);
373    
374      ## Step 1        SNIFFING: {
375      if ($charset eq 'utf-16') { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?  
376        $charset = 'utf-8';      ## Step 1
377        if (defined $charset_name) {
378          $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
379    
380          ## ISSUE: Unsupported encoding is not ignored according to the spec.
381          ($char_stream, $e_status) = $charset->get_decode_handle
382              ($byte_stream, allow_error_reporting => 1,
383               allow_fallback => 1);
384          if ($char_stream) {
385            $self->{confident} = 1;
386            last SNIFFING;
387          } else {
388            ## TODO: unsupported error
389          }
390      }      }
391    
392      ## Step 2      ## Step 2
393      if (defined $self->{input_encoding} and      my $byte_buffer = '';
394          $self->{input_encoding} eq $charset) {      for (1..1024) {
395          my $char = $byte_stream->getc;
396          last unless defined $char;
397          $byte_buffer .= $char;
398        } ## TODO: timeout
399    
400        ## Step 3
401        if ($byte_buffer =~ /^\xFE\xFF/) {
402          $charset = Message::Charset::Info->get_by_iana_name ('utf-16be');
403          ($char_stream, $e_status) = $charset->get_decode_handle
404              ($byte_stream, allow_error_reporting => 1,
405               allow_fallback => 1, byte_buffer => \$byte_buffer);
406        $self->{confident} = 1;        $self->{confident} = 1;
407        return;        last SNIFFING;
408        } elsif ($byte_buffer =~ /^\xFF\xFE/) {
409          $charset = Message::Charset::Info->get_by_iana_name ('utf-16le');
410          ($char_stream, $e_status) = $charset->get_decode_handle
411              ($byte_stream, allow_error_reporting => 1,
412               allow_fallback => 1, byte_buffer => \$byte_buffer);
413          $self->{confident} = 1;
414          last SNIFFING;
415        } elsif ($byte_buffer =~ /^\xEF\xBB\xBF/) {
416          $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
417          ($char_stream, $e_status) = $charset->get_decode_handle
418              ($byte_stream, allow_error_reporting => 1,
419               allow_fallback => 1, byte_buffer => \$byte_buffer);
420          $self->{confident} = 1;
421          last SNIFFING;
422      }      }
423    
424      !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.      ## Step 4
425          ':'.$charset, level => 'w', token => $token);      ## TODO: <meta charset>
426    
427      ## Step 3      ## Step 5
428      # if (can) {      ## TODO: from history
       ## change the encoding on the fly.  
       #$self->{confident} = 1;  
       #return;  
     # }  
429    
430      ## Step 4      ## Step 6
431      throw Whatpm::HTML::RestartParser (charset => $charset);      require Whatpm::Charset::UniversalCharDet;
432        $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
433            ($byte_buffer);
434        if (defined $charset_name) {
435          $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
436    
437          ## ISSUE: Unsupported encoding is not ignored according to the spec.
438          require Whatpm::Charset::DecodeHandle;
439          $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
440              ($byte_stream);
441          ($char_stream, $e_status) = $charset->get_decode_handle
442              ($buffer, allow_error_reporting => 1,
443               allow_fallback => 1, byte_buffer => \$byte_buffer);
444          if ($char_stream) {
445            $buffer->{buffer} = $byte_buffer;
446            !!!parse-error (type => 'sniffing:chardet',
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                  should => 's',
712                  warn => 'w',
713                  info => 'i',
714                  uncertain => 'u'},
715      }, $class;
716    $self->{set_next_char} = sub {    $self->{set_next_char} = sub {
717      $self->{next_char} = -1;      $self->{next_char} = -1;
718    };    };
# Line 295  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO Line 774  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO
774  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
775  sub BOGUS_DOCTYPE_STATE () { 32 }  sub BOGUS_DOCTYPE_STATE () { 32 }
776  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
777    sub SELF_CLOSING_START_TAG_STATE () { 34 }
778    sub CDATA_BLOCK_STATE () { 35 }
779    
780  sub DOCTYPE_TOKEN () { 1 }  sub DOCTYPE_TOKEN () { 1 }
781  sub COMMENT_TOKEN () { 2 }  sub COMMENT_TOKEN () { 2 }
# Line 312  sub ROW_IMS ()        { 0b10000000 } Line 793  sub ROW_IMS ()        { 0b10000000 }
793  sub BODY_AFTER_IMS () { 0b100000000 }  sub BODY_AFTER_IMS () { 0b100000000 }
794  sub FRAME_IMS ()      { 0b1000000000 }  sub FRAME_IMS ()      { 0b1000000000 }
795  sub SELECT_IMS ()     { 0b10000000000 }  sub SELECT_IMS ()     { 0b10000000000 }
796    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
797        ## NOTE: "in foreign content" insertion mode is special; it is combined
798        ## with the secondary insertion mode.  In this parser, they are stored
799        ## together in the bit-or'ed form.
800    
801  ## NOTE: "initial" and "before html" insertion modes have no constants.  ## NOTE: "initial" and "before html" insertion modes have no constants.
802    
# Line 348  sub _initialize_tokenizer ($) { Line 833  sub _initialize_tokenizer ($) {
833    undef $self->{current_attribute};    undef $self->{current_attribute};
834    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
835    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
836      delete $self->{self_closing};
837    $self->{char} = [];    $self->{char} = [];
838    # $self->{next_char}    # $self->{next_char}
839    !!!next-input-character;    !!!next-input-character;
# Line 368  sub _initialize_tokenizer ($) { Line 854  sub _initialize_tokenizer ($) {
854  ##        ->{value}  ##        ->{value}
855  ##        ->{has_reference} == 1 or 0  ##        ->{has_reference} == 1 or 0
856  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
857    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
858    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
859    ##     while the token is pushed back to the stack.
860    
861  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
862    
# Line 394  sub _initialize_tokenizer ($) { Line 883  sub _initialize_tokenizer ($) {
883    
884  sub _get_next_token ($) {  sub _get_next_token ($) {
885    my $self = shift;    my $self = shift;
886    
887      if ($self->{self_closing}) {
888        !!!parse-error (type => 'nestc', token => $self->{current_token});
889        ## NOTE: The |self_closing| flag is only set by start tag token.
890        ## In addition, when a start tag token is emitted, it is always set to
891        ## |current_token|.
892        delete $self->{self_closing};
893      }
894    
895    if (@{$self->{token}}) {    if (@{$self->{token}}) {
896        $self->{self_closing} = $self->{token}->[0]->{self_closing};
897      return shift @{$self->{token}};      return shift @{$self->{token}};
898    }    }
899    
# Line 574  sub _get_next_token ($) { Line 1073  sub _get_next_token ($) {
1073            redo A;            redo A;
1074          } else {          } else {
1075            !!!cp (23);            !!!cp (23);
1076            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago',
1077                              line => $self->{line_prev},
1078                              column => $self->{column_prev});
1079            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1080            ## reconsume            ## reconsume
1081    
# Line 765  sub _get_next_token ($) { Line 1266  sub _get_next_token ($) {
1266    
1267          redo A;          redo A;
1268        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1269            !!!cp (42);
1270            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1271          !!!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  
1272          redo A;          redo A;
1273        } else {        } else {
1274          !!!cp (44);          !!!cp (44);
# Line 829  sub _get_next_token ($) { Line 1320  sub _get_next_token ($) {
1320          !!!next-input-character;          !!!next-input-character;
1321          redo A;          redo A;
1322        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1323            !!!cp (50);
1324            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1325          !!!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  
1326          redo A;          redo A;
1327        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1328          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
# Line 889  sub _get_next_token ($) { Line 1370  sub _get_next_token ($) {
1370          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1371              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1372            !!!cp (57);            !!!cp (57);
1373            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name}, line => $self->{current_attribute}->{line}, column => $self->{current_attribute}->{column});            !!!parse-error (type => 'duplicate attribute', text => $self->{current_attribute}->{name}, line => $self->{current_attribute}->{line}, column => $self->{current_attribute}->{column});
1374            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1375          } else {          } else {
1376            !!!cp (58);            !!!cp (58);
# Line 942  sub _get_next_token ($) { Line 1423  sub _get_next_token ($) {
1423          !!!next-input-character;          !!!next-input-character;
1424          redo A;          redo A;
1425        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1426            !!!cp (64);
1427          $before_leave->();          $before_leave->();
1428            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1429          !!!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  
1430          redo A;          redo A;
1431        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1432          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
# Line 1042  sub _get_next_token ($) { Line 1513  sub _get_next_token ($) {
1513          !!!next-input-character;          !!!next-input-character;
1514          redo A;          redo A;
1515        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1516            !!!cp (77);
1517            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1518          !!!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  
1519          redo A;          redo A;
1520        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1521          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
# Line 1081  sub _get_next_token ($) { Line 1541  sub _get_next_token ($) {
1541    
1542          redo A;          redo A;
1543        } else {        } else {
1544          !!!cp (82);          if ($self->{next_char} == 0x0022 or # "
1545                $self->{next_char} == 0x0027) { # '
1546              !!!cp (78);
1547              !!!parse-error (type => 'bad attribute name');
1548            } else {
1549              !!!cp (82);
1550            }
1551          $self->{current_attribute}          $self->{current_attribute}
1552              = {name => chr ($self->{next_char}),              = {name => chr ($self->{next_char}),
1553                 value => '',                 value => '',
# Line 1116  sub _get_next_token ($) { Line 1582  sub _get_next_token ($) {
1582          !!!next-input-character;          !!!next-input-character;
1583          redo A;          redo A;
1584        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1585            !!!parse-error (type => 'empty unquoted attribute value');
1586          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1587            !!!cp (87);            !!!cp (87);
1588            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
# Line 1388  sub _get_next_token ($) { Line 1855  sub _get_next_token ($) {
1855    
1856          redo A;          redo A;
1857        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1858            !!!cp (122);
1859            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1860          !!!next-input-character;          !!!next-input-character;
1861          if ($self->{next_char} == 0x003E and # >          redo A;
1862              $self->{current_token}->{type} == START_TAG_TOKEN and        } elsif ($self->{next_char} == -1) {
1863              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {          !!!parse-error (type => 'unclosed tag');
1864            # permitted slash          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1865            !!!cp (122);            !!!cp (122.3);
1866            #            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1867            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1868              if ($self->{current_token}->{attributes}) {
1869                !!!cp (122.1);
1870                !!!parse-error (type => 'end tag attribute');
1871              } else {
1872                ## NOTE: This state should never be reached.
1873                !!!cp (122.2);
1874              }
1875          } else {          } else {
1876            !!!cp (123);            die "$0: $self->{current_token}->{type}: Unknown token type";
           !!!parse-error (type => 'nestc');  
1877          }          }
1878          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = DATA_STATE;
1879          # next-input-character is already done          ## Reconsume.
1880            !!!emit ($self->{current_token}); # start tag or end tag
1881          redo A;          redo A;
1882        } else {        } else {
1883          !!!cp (124);          !!!cp ('124.1');
1884          !!!parse-error (type => 'no space between attributes');          !!!parse-error (type => 'no space between attributes');
1885          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1886          ## reconsume          ## reconsume
1887          redo A;          redo A;
1888        }        }
1889        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1890          if ($self->{next_char} == 0x003E) { # >
1891            if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1892              !!!cp ('124.2');
1893              !!!parse-error (type => 'nestc', token => $self->{current_token});
1894              ## TODO: Different type than slash in start tag
1895              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1896              if ($self->{current_token}->{attributes}) {
1897                !!!cp ('124.4');
1898                !!!parse-error (type => 'end tag attribute');
1899              } else {
1900                !!!cp ('124.5');
1901              }
1902              ## TODO: Test |<title></title/>|
1903            } else {
1904              !!!cp ('124.3');
1905              $self->{self_closing} = 1;
1906            }
1907    
1908            $self->{state} = DATA_STATE;
1909            !!!next-input-character;
1910    
1911            !!!emit ($self->{current_token}); # start tag or end tag
1912    
1913            redo A;
1914          } elsif ($self->{next_char} == -1) {
1915            !!!parse-error (type => 'unclosed tag');
1916            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1917              !!!cp (124.7);
1918              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1919            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1920              if ($self->{current_token}->{attributes}) {
1921                !!!cp (124.5);
1922                !!!parse-error (type => 'end tag attribute');
1923              } else {
1924                ## NOTE: This state should never be reached.
1925                !!!cp (124.6);
1926              }
1927            } else {
1928              die "$0: $self->{current_token}->{type}: Unknown token type";
1929            }
1930            $self->{state} = DATA_STATE;
1931            ## Reconsume.
1932            !!!emit ($self->{current_token}); # start tag or end tag
1933            redo A;
1934          } else {
1935            !!!cp ('124.4');
1936            !!!parse-error (type => 'nestc');
1937            ## TODO: This error type is wrong.
1938            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1939            ## Reconsume.
1940            redo A;
1941          }
1942      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1943        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1944                
# Line 1516  sub _get_next_token ($) { Line 2046  sub _get_next_token ($) {
2046          } else {          } else {
2047            !!!cp (135);            !!!cp (135);
2048          }          }
2049          } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
2050                   $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
2051                   $self->{next_char} == 0x005B) { # [
2052            !!!next-input-character;
2053            push @next_char, $self->{next_char};
2054            if ($self->{next_char} == 0x0043) { # C
2055              !!!next-input-character;
2056              push @next_char, $self->{next_char};
2057              if ($self->{next_char} == 0x0044) { # D
2058                !!!next-input-character;
2059                push @next_char, $self->{next_char};
2060                if ($self->{next_char} == 0x0041) { # A
2061                  !!!next-input-character;
2062                  push @next_char, $self->{next_char};
2063                  if ($self->{next_char} == 0x0054) { # T
2064                    !!!next-input-character;
2065                    push @next_char, $self->{next_char};
2066                    if ($self->{next_char} == 0x0041) { # A
2067                      !!!next-input-character;
2068                      push @next_char, $self->{next_char};
2069                      if ($self->{next_char} == 0x005B) { # [
2070                        !!!cp (135.1);
2071                        $self->{state} = CDATA_BLOCK_STATE;
2072                        !!!next-input-character;
2073                        redo A;
2074                      } else {
2075                        !!!cp (135.2);
2076                      }
2077                    } else {
2078                      !!!cp (135.3);
2079                    }
2080                  } else {
2081                    !!!cp (135.4);                
2082                  }
2083                } else {
2084                  !!!cp (135.5);
2085                }
2086              } else {
2087                !!!cp (135.6);
2088              }
2089            } else {
2090              !!!cp (135.7);
2091            }
2092        } else {        } else {
2093          !!!cp (136);          !!!cp (136);
2094        }        }
# Line 2115  sub _get_next_token ($) { Line 2688  sub _get_next_token ($) {
2688          redo A;          redo A;
2689        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2690          !!!cp (208);          !!!cp (208);
2691          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2692    
2693          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2694          !!!next-input-character;          !!!next-input-character;
# Line 2151  sub _get_next_token ($) { Line 2724  sub _get_next_token ($) {
2724          redo A;          redo A;
2725        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2726          !!!cp (212);          !!!cp (212);
2727          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2728    
2729          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2730          !!!next-input-character;          !!!next-input-character;
# Line 2199  sub _get_next_token ($) { Line 2772  sub _get_next_token ($) {
2772        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
2773          !!!cp (217);          !!!cp (217);
2774          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
   
2775          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2776          ## reconsume          ## reconsume
2777    
# Line 2240  sub _get_next_token ($) { Line 2812  sub _get_next_token ($) {
2812          !!!next-input-character;          !!!next-input-character;
2813          redo A;          redo A;
2814        }        }
2815        } elsif ($self->{state} == CDATA_BLOCK_STATE) {
2816          my $s = '';
2817          
2818          my ($l, $c) = ($self->{line}, $self->{column});
2819    
2820          CS: while ($self->{next_char} != -1) {
2821            if ($self->{next_char} == 0x005D) { # ]
2822              !!!next-input-character;
2823              if ($self->{next_char} == 0x005D) { # ]
2824                !!!next-input-character;
2825                MDC: {
2826                  if ($self->{next_char} == 0x003E) { # >
2827                    !!!cp (221.1);
2828                    !!!next-input-character;
2829                    last CS;
2830                  } elsif ($self->{next_char} == 0x005D) { # ]
2831                    !!!cp (221.2);
2832                    $s .= ']';
2833                    !!!next-input-character;
2834                    redo MDC;
2835                  } else {
2836                    !!!cp (221.3);
2837                    $s .= ']]';
2838                    #
2839                  }
2840                } # MDC
2841              } else {
2842                !!!cp (221.4);
2843                $s .= ']';
2844                #
2845              }
2846            } else {
2847              !!!cp (221.5);
2848              #
2849            }
2850            $s .= chr $self->{next_char};
2851            !!!next-input-character;
2852          } # CS
2853    
2854          $self->{state} = DATA_STATE;
2855          ## next-input-character done or EOF, which is reconsumed.
2856    
2857          if (length $s) {
2858            !!!cp (221.6);
2859            !!!emit ({type => CHARACTER_TOKEN, data => $s,
2860                      line => $l, column => $c});
2861          } else {
2862            !!!cp (221.7);
2863          }
2864    
2865          redo A;
2866    
2867          ## ISSUE: "text tokens" in spec.
2868          ## TODO: Streaming support
2869      } else {      } else {
2870        die "$0: $self->{state}: Unknown state";        die "$0: $self->{state}: Unknown state";
2871      }      }
# Line 2307  sub _tokenize_attempt_to_consume_an_enti Line 2933  sub _tokenize_attempt_to_consume_an_enti
2933    
2934          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2935            !!!cp (1008);            !!!cp (1008);
2936            !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);            !!!parse-error (type => 'invalid character reference',
2937                              text => (sprintf 'U+%04X', $code),
2938                              line => $l, column => $c);
2939            $code = 0xFFFD;            $code = 0xFFFD;
2940          } elsif ($code > 0x10FFFF) {          } elsif ($code > 0x10FFFF) {
2941            !!!cp (1009);            !!!cp (1009);
2942            !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);            !!!parse-error (type => 'invalid character reference',
2943                              text => (sprintf 'U-%08X', $code),
2944                              line => $l, column => $c);
2945            $code = 0xFFFD;            $code = 0xFFFD;
2946          } elsif ($code == 0x000D) {          } elsif ($code == 0x000D) {
2947            !!!cp (1010);            !!!cp (1010);
# Line 2319  sub _tokenize_attempt_to_consume_an_enti Line 2949  sub _tokenize_attempt_to_consume_an_enti
2949            $code = 0x000A;            $code = 0x000A;
2950          } elsif (0x80 <= $code and $code <= 0x9F) {          } elsif (0x80 <= $code and $code <= 0x9F) {
2951            !!!cp (1011);            !!!cp (1011);
2952            !!!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);
2953            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
2954          }          }
2955    
# Line 2352  sub _tokenize_attempt_to_consume_an_enti Line 2982  sub _tokenize_attempt_to_consume_an_enti
2982    
2983        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2984          !!!cp (1015);          !!!cp (1015);
2985          !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);          !!!parse-error (type => 'invalid character reference',
2986                            text => (sprintf 'U+%04X', $code),
2987                            line => $l, column => $c);
2988          $code = 0xFFFD;          $code = 0xFFFD;
2989        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
2990          !!!cp (1016);          !!!cp (1016);
2991          !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);          !!!parse-error (type => 'invalid character reference',
2992                            text => (sprintf 'U-%08X', $code),
2993                            line => $l, column => $c);
2994          $code = 0xFFFD;          $code = 0xFFFD;
2995        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
2996          !!!cp (1017);          !!!cp (1017);
2997          !!!parse-error (type => 'CR character reference', line => $l, column => $c);          !!!parse-error (type => 'CR character reference',
2998                            line => $l, column => $c);
2999          $code = 0x000A;          $code = 0x000A;
3000        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
3001          !!!cp (1018);          !!!cp (1018);
3002          !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);          !!!parse-error (type => 'C1 character reference',
3003                            text => (sprintf 'U+%04X', $code),
3004                            line => $l, column => $c);
3005          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
3006        }        }
3007                
# Line 2390  sub _tokenize_attempt_to_consume_an_enti Line 3027  sub _tokenize_attempt_to_consume_an_enti
3027      require Whatpm::_NamedEntityList;      require Whatpm::_NamedEntityList;
3028      our $EntityChar;      our $EntityChar;
3029    
3030      while (length $entity_name < 10 and      while (length $entity_name < 30 and
3031             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
3032             ((0x0041 <= $self->{next_char} and # a             ((0x0041 <= $self->{next_char} and # a
3033               $self->{next_char} <= 0x005A) or # x               $self->{next_char} <= 0x005A) or # x
# Line 2462  sub _initialize_tree_constructor ($) { Line 3099  sub _initialize_tree_constructor ($) {
3099    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
3100    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
3101    $self->{document}->manakai_is_html (1); # MUST    $self->{document}->manakai_is_html (1); # MUST
3102      $self->{document}->set_user_data (manakai_source_line => 1);
3103      $self->{document}->set_user_data (manakai_source_column => 1);
3104  } # _initialize_tree_constructor  } # _initialize_tree_constructor
3105    
3106  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 2516  sub _tree_construction_initial ($) { Line 3155  sub _tree_construction_initial ($) {
3155        ## language.        ## language.
3156        my $doctype_name = $token->{name};        my $doctype_name = $token->{name};
3157        $doctype_name = '' unless defined $doctype_name;        $doctype_name = '' unless defined $doctype_name;
3158        $doctype_name =~ tr/a-z/A-Z/;        $doctype_name =~ tr/a-z/A-Z/; # ASCII case-insensitive
3159        if (not defined $token->{name} or # <!DOCTYPE>        if (not defined $token->{name} or # <!DOCTYPE>
           defined $token->{public_identifier} or  
3160            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
3161          !!!cp ('t1');          !!!cp ('t1');
3162          !!!parse-error (type => 'not HTML5', token => $token);          !!!parse-error (type => 'not HTML5', token => $token);
3163        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
3164          !!!cp ('t2');          !!!cp ('t2');
         ## ISSUE: ASCII case-insensitive? (in fact it does not matter)  
3165          !!!parse-error (type => 'not HTML5', token => $token);          !!!parse-error (type => 'not HTML5', token => $token);
3166          } elsif (defined $token->{public_identifier}) {
3167            if ($token->{public_identifier} eq 'XSLT-compat') {
3168              !!!cp ('t1.2');
3169              !!!parse-error (type => 'XSLT-compat', token => $token,
3170                              level => $self->{level}->{should});
3171            } else {
3172              !!!parse-error (type => 'not HTML5', token => $token);
3173            }
3174        } else {        } else {
3175          !!!cp ('t3');          !!!cp ('t3');
3176            #
3177        }        }
3178                
3179        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
3180          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3181          ## NOTE: Default value for both |public_id| and |system_id| attributes
3182          ## are empty strings, so that we don't set any value in missing cases.
3183        $doctype->public_id ($token->{public_identifier})        $doctype->public_id ($token->{public_identifier})
3184            if defined $token->{public_identifier};            if defined $token->{public_identifier};
3185        $doctype->system_id ($token->{system_identifier})        $doctype->system_id ($token->{system_identifier})
# Line 2546  sub _tree_construction_initial ($) { Line 3194  sub _tree_construction_initial ($) {
3194        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{public_identifier}) {
3195          my $pubid = $token->{public_identifier};          my $pubid = $token->{public_identifier};
3196          $pubid =~ tr/a-z/A-z/;          $pubid =~ tr/a-z/A-z/;
3197          if ({          my $prefix = [
3198            "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,            "+//SILMARIL//DTD HTML PRO V0R11 19970101//",
3199            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3200            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3201            "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 1//",
3202            "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 2//",
3203            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//",
3204            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//",
3205            "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT//",
3206            "-//IETF//DTD HTML 2.0//EN" => 1,            "-//IETF//DTD HTML 2.0//",
3207            "-//IETF//DTD HTML 2.1E//EN" => 1,            "-//IETF//DTD HTML 2.1E//",
3208            "-//IETF//DTD HTML 3.0//EN" => 1,            "-//IETF//DTD HTML 3.0//",
3209            "-//IETF//DTD HTML 3.0//EN//" => 1,            "-//IETF//DTD HTML 3.2 FINAL//",
3210            "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,            "-//IETF//DTD HTML 3.2//",
3211            "-//IETF//DTD HTML 3.2//EN" => 1,            "-//IETF//DTD HTML 3//",
3212            "-//IETF//DTD HTML 3//EN" => 1,            "-//IETF//DTD HTML LEVEL 0//",
3213            "-//IETF//DTD HTML LEVEL 0//EN" => 1,            "-//IETF//DTD HTML LEVEL 1//",
3214            "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,            "-//IETF//DTD HTML LEVEL 2//",
3215            "-//IETF//DTD HTML LEVEL 1//EN" => 1,            "-//IETF//DTD HTML LEVEL 3//",
3216            "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 0//",
3217            "-//IETF//DTD HTML LEVEL 2//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 1//",
3218            "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 2//",
3219            "-//IETF//DTD HTML LEVEL 3//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 3//",
3220            "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,            "-//IETF//DTD HTML STRICT//",
3221            "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,            "-//IETF//DTD HTML//",
3222            "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,            "-//METRIUS//DTD METRIUS PRESENTATIONAL//",
3223            "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//",
3224            "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//",
3225            "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//",
3226            "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//",
3227            "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//",
3228            "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//",
3229            "-//IETF//DTD HTML STRICT//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD HTML//",
3230            "-//IETF//DTD HTML STRICT//EN//2.0" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//",
3231            "-//IETF//DTD HTML STRICT//EN//3.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//",
3232            "-//IETF//DTD HTML//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//",
3233            "-//IETF//DTD HTML//EN//2.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//",
3234            "-//IETF//DTD HTML//EN//3.0" => 1,            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//",
3235            "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//",
3236            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//",
3237            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//",
3238            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//",
3239            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//",
3240            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,            "-//W3C//DTD HTML 3 1995-03-24//",
3241            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,            "-//W3C//DTD HTML 3.2 DRAFT//",
3242            "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,            "-//W3C//DTD HTML 3.2 FINAL//",
3243            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//W3C//DTD HTML 3.2//",
3244            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//W3C//DTD HTML 3.2S DRAFT//",
3245            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 FRAMESET//",
3246            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 TRANSITIONAL//",
3247            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMETNAL 19960712//",
3248            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMENTAL 970421//",
3249            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//W3C//DTD W3 HTML//",
3250            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//W3O//DTD W3 HTML 3.0//",
3251            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML 2.0//",
3252            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML//",
3253            "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,          ]; # $prefix
3254            "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,          my $match;
3255            "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,          for (@$prefix) {
3256            "-//W3C//DTD HTML 3.2//EN" => 1,            if (substr ($prefix, 0, length $_) eq $_) {
3257            "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,              $match = 1;
3258            "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,              last;
3259            "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,            }
3260            "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,          }
3261            "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,          if ($match or
3262            "-//W3C//DTD W3 HTML//EN" => 1,              $pubid eq "-//W3O//DTD W3 HTML STRICT 3.0//EN//" or
3263            "-//W3O//DTD W3 HTML 3.0//EN" => 1,              $pubid eq "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" or
3264            "-//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}) {  
3265            !!!cp ('t5');            !!!cp ('t5');
3266            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3267          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD HTML 4.01 FRAMESET//] or
3268                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD HTML 4.01 TRANSITIONAL//]) {
3269            if (defined $token->{system_identifier}) {            if (defined $token->{system_identifier}) {
3270              !!!cp ('t6');              !!!cp ('t6');
3271              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
# Line 2631  sub _tree_construction_initial ($) { Line 3273  sub _tree_construction_initial ($) {
3273              !!!cp ('t7');              !!!cp ('t7');
3274              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
3275            }            }
3276          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD XHTML 1.0 FRAMESET//] or
3277                   $pubid eq "-//W3C//DTD XHTML 1.0 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD XHTML 1.0 TRANSITIONAL//]) {
3278            !!!cp ('t8');            !!!cp ('t8');
3279            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
3280          } else {          } else {
# Line 2645  sub _tree_construction_initial ($) { Line 3287  sub _tree_construction_initial ($) {
3287          my $sysid = $token->{system_identifier};          my $sysid = $token->{system_identifier};
3288          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
3289          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") {
3290            ## TODO: Check the spec: PUBLIC "(limited quirks)" "(quirks)"            ## NOTE: Ensure that |PUBLIC "(limited quirks)" "(quirks)"| is
3291              ## marked as quirks.
3292            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3293            !!!cp ('t11');            !!!cp ('t11');
3294          } else {          } else {
# Line 2668  sub _tree_construction_initial ($) { Line 3311  sub _tree_construction_initial ($) {
3311        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3312        ## Go to the "before html" insertion mode.        ## Go to the "before html" insertion mode.
3313        ## reprocess        ## reprocess
3314          !!!ack-later;
3315        return;        return;
3316      } elsif ($token->{type} == CHARACTER_TOKEN) {      } elsif ($token->{type} == CHARACTER_TOKEN) {
3317        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
# Line 2748  sub _tree_construction_root_element ($) Line 3392  sub _tree_construction_root_element ($)
3392        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
3393          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
3394            my $root_element;            my $root_element;
3395            !!!create-element ($root_element, $token->{tag_name}, $token->{attributes}, $token);            !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3396            $self->{document}->append_child ($root_element);            $self->{document}->append_child ($root_element);
3397            push @{$self->{open_elements}}, [$root_element, 'html'];            push @{$self->{open_elements}},
3398                  [$root_element, $el_category->{html}];
3399    
3400            if ($token->{attributes}->{manifest}) {            if ($token->{attributes}->{manifest}) {
3401              !!!cp ('t24');              !!!cp ('t24');
# Line 2765  sub _tree_construction_root_element ($) Line 3410  sub _tree_construction_root_element ($)
3410              $self->{application_cache_selection}->(undef);              $self->{application_cache_selection}->(undef);
3411            }            }
3412    
3413              !!!nack ('t25c');
3414    
3415            !!!next-token;            !!!next-token;
3416            return; ## Go to the "before head" insertion mode.            return; ## Go to the "before head" insertion mode.
3417          } else {          } else {
# Line 2781  sub _tree_construction_root_element ($) Line 3428  sub _tree_construction_root_element ($)
3428          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
3429        }        }
3430    
3431      my $root_element; !!!create-element ($root_element, 'html',, $token);      my $root_element;
3432        !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3433      $self->{document}->append_child ($root_element);      $self->{document}->append_child ($root_element);
3434      push @{$self->{open_elements}}, [$root_element, 'html'];      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3435    
3436      $self->{application_cache_selection}->(undef);      $self->{application_cache_selection}->(undef);
3437    
3438      ## NOTE: Reprocess the token.      ## NOTE: Reprocess the token.
3439        !!!ack-later;
3440      return; ## Go to the "before head" insertion mode.      return; ## Go to the "before head" insertion mode.
3441    
3442      ## ISSUE: There is an issue in the spec      ## ISSUE: There is an issue in the spec
# Line 2811  sub _reset_insertion_mode ($) { Line 3460  sub _reset_insertion_mode ($) {
3460        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3461          $last = 1;          $last = 1;
3462          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
3463            if ($self->{inner_html_node}->[1] eq 'td' or            !!!cp ('t28');
3464                $self->{inner_html_node}->[1] eq 'th') {            $node = $self->{inner_html_node};
3465              !!!cp ('t27');          } else {
3466              #            die "_reset_insertion_mode: t27";
           } else {  
             !!!cp ('t28');  
             $node = $self->{inner_html_node};  
           }  
3467          }          }
3468        }        }
3469              
3470        ## Step 4..13        ## Step 4..14
3471        my $new_mode = {        my $new_mode;
3472          if ($node->[1] & FOREIGN_EL) {
3473            !!!cp ('t28.1');
3474            ## NOTE: Strictly spaking, the line below only applies to MathML and
3475            ## SVG elements.  Currently the HTML syntax supports only MathML and
3476            ## SVG elements as foreigners.
3477            $new_mode = IN_BODY_IM | IN_FOREIGN_CONTENT_IM;
3478          } elsif ($node->[1] & TABLE_CELL_EL) {
3479            if ($last) {
3480              !!!cp ('t28.2');
3481              #
3482            } else {
3483              !!!cp ('t28.3');
3484              $new_mode = IN_CELL_IM;
3485            }
3486          } else {
3487            !!!cp ('t28.4');
3488            $new_mode = {
3489                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
3490                        ## NOTE: |option| and |optgroup| do not set                        ## NOTE: |option| and |optgroup| do not set
3491                        ## insertion mode to "in select" by themselves.                        ## insertion mode to "in select" by themselves.
                       td => IN_CELL_IM,  
                       th => IN_CELL_IM,  
3492                        tr => IN_ROW_IM,                        tr => IN_ROW_IM,
3493                        tbody => IN_TABLE_BODY_IM,                        tbody => IN_TABLE_BODY_IM,
3494                        thead => IN_TABLE_BODY_IM,                        thead => IN_TABLE_BODY_IM,
# Line 2839  sub _reset_insertion_mode ($) { Line 3499  sub _reset_insertion_mode ($) {
3499                        head => IN_BODY_IM, # not in head!                        head => IN_BODY_IM, # not in head!
3500                        body => IN_BODY_IM,                        body => IN_BODY_IM,
3501                        frameset => IN_FRAMESET_IM,                        frameset => IN_FRAMESET_IM,
3502                       }->{$node->[1]};                       }->{$node->[0]->manakai_local_name};
3503          }
3504        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3505                
3506        ## Step 14        ## Step 15
3507        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3508          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3509            !!!cp ('t29');            !!!cp ('t29');
3510            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
# Line 2857  sub _reset_insertion_mode ($) { Line 3518  sub _reset_insertion_mode ($) {
3518          !!!cp ('t31');          !!!cp ('t31');
3519        }        }
3520                
3521        ## Step 15        ## Step 16
3522        $self->{insertion_mode} = IN_BODY_IM and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
3523                
3524        ## Step 16        ## Step 17
3525        $i--;        $i--;
3526        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3527                
3528        ## Step 17        ## Step 18
3529        redo S3;        redo S3;
3530      } # S3      } # S3
3531    
# Line 2976  sub _tree_construction_main ($) { Line 3637  sub _tree_construction_main ($) {
3637      ## Step 1      ## Step 1
3638      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3639      my $el;      my $el;
3640      !!!create-element ($el, $start_tag_name, $token->{attributes}, $token);      !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3641    
3642      ## Step 2      ## Step 2
3643      $insert->($el);      $insert->($el);
# Line 2987  sub _tree_construction_main ($) { Line 3648  sub _tree_construction_main ($) {
3648    
3649      ## Step 4      ## Step 4
3650      my $text = '';      my $text = '';
3651        !!!nack ('t40.1');
3652      !!!next-token;      !!!next-token;
3653      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3654        !!!cp ('t40');        !!!cp ('t40');
# Line 3013  sub _tree_construction_main ($) { Line 3675  sub _tree_construction_main ($) {
3675        ## NOTE: An end-of-file token.        ## NOTE: An end-of-file token.
3676        if ($content_model_flag == CDATA_CONTENT_MODEL) {        if ($content_model_flag == CDATA_CONTENT_MODEL) {
3677          !!!cp ('t43');          !!!cp ('t43');
3678          !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);          !!!parse-error (type => 'in CDATA:#eof', token => $token);
3679        } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {        } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3680          !!!cp ('t44');          !!!cp ('t44');
3681          !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);          !!!parse-error (type => 'in RCDATA:#eof', token => $token);
3682        } else {        } else {
3683          die "$0: $content_model_flag in parse_rcdata";          die "$0: $content_model_flag in parse_rcdata";
3684        }        }
# Line 3026  sub _tree_construction_main ($) { Line 3688  sub _tree_construction_main ($) {
3688    
3689    my $script_start_tag = sub () {    my $script_start_tag = sub () {
3690      my $script_el;      my $script_el;
3691      !!!create-element ($script_el, 'script', $token->{attributes}, $token);      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
3692      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3693    
3694      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
3695      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
3696            
3697      my $text = '';      my $text = '';
3698        !!!nack ('t45.1');
3699      !!!next-token;      !!!next-token;
3700      while ($token->{type} == CHARACTER_TOKEN) {      while ($token->{type} == CHARACTER_TOKEN) {
3701        !!!cp ('t45');        !!!cp ('t45');
# Line 3052  sub _tree_construction_main ($) { Line 3715  sub _tree_construction_main ($) {
3715        ## Ignore the token        ## Ignore the token
3716      } else {      } else {
3717        !!!cp ('t48');        !!!cp ('t48');
3718        !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);        !!!parse-error (type => 'in CDATA:#eof', token => $token);
3719        ## ISSUE: And ignore?        ## ISSUE: And ignore?
3720        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3721      }      }
# Line 3090  sub _tree_construction_main ($) { Line 3753  sub _tree_construction_main ($) {
3753        my $formatting_element;        my $formatting_element;
3754        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3755        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3756          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3757              !!!cp ('t52');
3758              last AFE;
3759            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
3760                         eq $tag_name) {
3761            !!!cp ('t51');            !!!cp ('t51');
3762            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3763            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3764            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           !!!cp ('t52');  
           last AFE;  
3765          }          }
3766        } # AFE        } # AFE
3767        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3768          !!!cp ('t53');          !!!cp ('t53');
3769          !!!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);
3770          ## Ignore the token          ## Ignore the token
3771          !!!next-token;          !!!next-token;
3772          return;          return;
# Line 3119  sub _tree_construction_main ($) { Line 3783  sub _tree_construction_main ($) {
3783              last INSCOPE;              last INSCOPE;
3784            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3785              !!!cp ('t55');              !!!cp ('t55');
3786              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},              !!!parse-error (type => 'unmatched end tag',
3787                                text => $token->{tag_name},
3788                              token => $end_tag_token);                              token => $end_tag_token);
3789              ## Ignore the token              ## Ignore the token
3790              !!!next-token;              !!!next-token;
3791              return;              return;
3792            }            }
3793          } 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]}) {  
3794            !!!cp ('t56');            !!!cp ('t56');
3795            $in_scope = 0;            $in_scope = 0;
3796          }          }
3797        } # INSCOPE        } # INSCOPE
3798        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3799          !!!cp ('t57');          !!!cp ('t57');
3800          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},          !!!parse-error (type => 'unmatched end tag',
3801                            text => $token->{tag_name},
3802                          token => $end_tag_token);                          token => $end_tag_token);
3803          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3804          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
# Line 3143  sub _tree_construction_main ($) { Line 3806  sub _tree_construction_main ($) {
3806        }        }
3807        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3808          !!!cp ('t58');          !!!cp ('t58');
3809          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1],          !!!parse-error (type => 'not closed',
3810                            text => $self->{open_elements}->[-1]->[0]
3811                                ->manakai_local_name,
3812                          token => $end_tag_token);                          token => $end_tag_token);
3813        }        }
3814                
# Line 3152  sub _tree_construction_main ($) { Line 3817  sub _tree_construction_main ($) {
3817        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
3818        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3819          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3820          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
3821              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3822              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
3823               $scoping_category->{$node->[1]})) { ## Scoping is redundant, maybe               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
3824            !!!cp ('t59');            !!!cp ('t59');
3825            $furthest_block = $node;            $furthest_block = $node;
3826            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
# Line 3241  sub _tree_construction_main ($) { Line 3906  sub _tree_construction_main ($) {
3906        } # S7          } # S7  
3907                
3908        ## Step 8        ## Step 8
3909        if ({        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
            table => 1, tbody => 1, tfoot => 1, thead => 1, tr => 1,  
           }->{$common_ancestor_node->[1]}) {  
3910          my $foster_parent_element;          my $foster_parent_element;
3911          my $next_sibling;          my $next_sibling;
3912                           OE: for (reverse 0..$#{$self->{open_elements}}) {          OE: for (reverse 0..$#{$self->{open_elements}}) {
3913                             if ($self->{open_elements}->[$_]->[1] eq 'table') {            if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3914                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3915                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3916                                 !!!cp ('t65.1');                                 !!!cp ('t65.1');
# Line 3320  sub _tree_construction_main ($) { Line 3983  sub _tree_construction_main ($) {
3983    
3984    my $insert_to_foster = sub {    my $insert_to_foster = sub {
3985      my $child = shift;      my $child = shift;
3986      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]}) {  
3987        # MUST        # MUST
3988        my $foster_parent_element;        my $foster_parent_element;
3989        my $next_sibling;        my $next_sibling;
3990                           OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3991                             if ($self->{open_elements}->[$_]->[1] eq 'table') {          if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3992                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3993                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3994                                 !!!cp ('t70');                                 !!!cp ('t70');
# Line 3352  sub _tree_construction_main ($) { Line 4013  sub _tree_construction_main ($) {
4013      }      }
4014    }; # $insert_to_foster    }; # $insert_to_foster
4015    
4016    B: {    B: while (1) {
4017      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
4018        !!!cp ('t73');        !!!cp ('t73');
4019        !!!parse-error (type => 'DOCTYPE in the middle', token => $token);        !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
4020        ## Ignore the token        ## Ignore the token
4021        ## Stay in the phase        ## Stay in the phase
4022        !!!next-token;        !!!next-token;
4023        redo B;        next B;
4024      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
4025               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
4026        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4027          !!!cp ('t79');          !!!cp ('t79');
4028          !!!parse-error (type => 'after html:html', token => $token);          !!!parse-error (type => 'after html', text => 'html', token => $token);
4029          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
4030        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4031          !!!cp ('t80');          !!!cp ('t80');
4032          !!!parse-error (type => 'after html:html', token => $token);          !!!parse-error (type => 'after html', text => 'html', token => $token);
4033          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
4034        } else {        } else {
4035          !!!cp ('t81');          !!!cp ('t81');
# Line 3385  sub _tree_construction_main ($) { Line 4046  sub _tree_construction_main ($) {
4046               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
4047          }          }
4048        }        }
4049          !!!nack ('t84.1');
4050        !!!next-token;        !!!next-token;
4051        redo B;        next B;
4052      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
4053        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
4054        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
# Line 3400  sub _tree_construction_main ($) { Line 4062  sub _tree_construction_main ($) {
4062          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
4063        }        }
4064        !!!next-token;        !!!next-token;
4065        redo B;        next B;
4066      } elsif ($self->{insertion_mode} & HEAD_IMS) {      } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
4067          if ($token->{type} == CHARACTER_TOKEN) {
4068            !!!cp ('t87.1');
4069            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4070            !!!next-token;
4071            next B;
4072          } elsif ($token->{type} == START_TAG_TOKEN) {
4073            if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
4074                 $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
4075                not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
4076                ($token->{tag_name} eq 'svg' and
4077                 $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
4078              ## NOTE: "using the rules for secondary insertion mode"then"continue"
4079              !!!cp ('t87.2');
4080              #
4081            } elsif ({
4082                      b => 1, big => 1, blockquote => 1, body => 1, br => 1,
4083                      center => 1, code => 1, dd => 1, div => 1, dl => 1, dt => 1,
4084                      em => 1, embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1,
4085                      h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, i => 1,
4086                      img => 1, li => 1, listing => 1, menu => 1, meta => 1,
4087                      nobr => 1, ol => 1, p => 1, pre => 1, ruby => 1, s => 1,
4088                      small => 1, span => 1, strong => 1, strike => 1, sub => 1,
4089                      sup => 1, table => 1, tt => 1, u => 1, ul => 1, var => 1,
4090                     }->{$token->{tag_name}}) {
4091              !!!cp ('t87.2');
4092              !!!parse-error (type => 'not closed',
4093                              text => $self->{open_elements}->[-1]->[0]
4094                                  ->manakai_local_name,
4095                              token => $token);
4096    
4097              pop @{$self->{open_elements}}
4098                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4099    
4100              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4101              ## Reprocess.
4102              next B;
4103            } else {
4104              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
4105              my $tag_name = $token->{tag_name};
4106              if ($nsuri eq $SVG_NS) {
4107                $tag_name = {
4108                   altglyph => 'altGlyph',
4109                   altglyphdef => 'altGlyphDef',
4110                   altglyphitem => 'altGlyphItem',
4111                   animatecolor => 'animateColor',
4112                   animatemotion => 'animateMotion',
4113                   animatetransform => 'animateTransform',
4114                   clippath => 'clipPath',
4115                   feblend => 'feBlend',
4116                   fecolormatrix => 'feColorMatrix',
4117                   fecomponenttransfer => 'feComponentTransfer',
4118                   fecomposite => 'feComposite',
4119                   feconvolvematrix => 'feConvolveMatrix',
4120                   fediffuselighting => 'feDiffuseLighting',
4121                   fedisplacementmap => 'feDisplacementMap',
4122                   fedistantlight => 'feDistantLight',
4123                   feflood => 'feFlood',
4124                   fefunca => 'feFuncA',
4125                   fefuncb => 'feFuncB',
4126                   fefuncg => 'feFuncG',
4127                   fefuncr => 'feFuncR',
4128                   fegaussianblur => 'feGaussianBlur',
4129                   feimage => 'feImage',
4130                   femerge => 'feMerge',
4131                   femergenode => 'feMergeNode',
4132                   femorphology => 'feMorphology',
4133                   feoffset => 'feOffset',
4134                   fepointlight => 'fePointLight',
4135                   fespecularlighting => 'feSpecularLighting',
4136                   fespotlight => 'feSpotLight',
4137                   fetile => 'feTile',
4138                   feturbulence => 'feTurbulence',
4139                   foreignobject => 'foreignObject',
4140                   glyphref => 'glyphRef',
4141                   lineargradient => 'linearGradient',
4142                   radialgradient => 'radialGradient',
4143                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4144                   textpath => 'textPath',  
4145                }->{$tag_name} || $tag_name;
4146              }
4147    
4148              ## "adjust SVG attributes" (SVG only) - done in insert-element-f
4149    
4150              ## "adjust foreign attributes" - done in insert-element-f
4151    
4152              !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
4153    
4154              if ($self->{self_closing}) {
4155                pop @{$self->{open_elements}};
4156                !!!ack ('t87.3');
4157              } else {
4158                !!!cp ('t87.4');
4159              }
4160    
4161              !!!next-token;
4162              next B;
4163            }
4164          } elsif ($token->{type} == END_TAG_TOKEN) {
4165            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4166            !!!cp ('t87.5');
4167            #
4168          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4169            !!!cp ('t87.6');
4170            !!!parse-error (type => 'not closed',
4171                            text => $self->{open_elements}->[-1]->[0]
4172                                ->manakai_local_name,
4173                            token => $token);
4174    
4175            pop @{$self->{open_elements}}
4176                while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4177    
4178            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4179            ## Reprocess.
4180            next B;
4181          } else {
4182            die "$0: $token->{type}: Unknown token type";        
4183          }
4184        }
4185    
4186        if ($self->{insertion_mode} & HEAD_IMS) {
4187        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4188          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4189            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
# Line 3411  sub _tree_construction_main ($) { Line 4193  sub _tree_construction_main ($) {
4193              !!!cp ('t88.1');              !!!cp ('t88.1');
4194              ## Ignore the token.              ## Ignore the token.
4195              !!!next-token;              !!!next-token;
4196              redo B;              next B;
4197            }            }
4198            unless (length $token->{data}) {            unless (length $token->{data}) {
4199              !!!cp ('t88');              !!!cp ('t88');
4200              !!!next-token;              !!!next-token;
4201              redo B;              next B;
4202            }            }
4203          }          }
4204    
4205          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4206            !!!cp ('t89');            !!!cp ('t89');
4207            ## As if <head>            ## As if <head>
4208            !!!create-element ($self->{head_element}, 'head',, $token);            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4209            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4210            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            push @{$self->{open_elements}},
4211                  [$self->{head_element}, $el_category->{head}];
4212    
4213            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4214            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
# Line 3435  sub _tree_construction_main ($) { Line 4218  sub _tree_construction_main ($) {
4218            !!!cp ('t90');            !!!cp ('t90');
4219            ## As if </noscript>            ## As if </noscript>
4220            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4221            !!!parse-error (type => 'in noscript:#character', token => $token);            !!!parse-error (type => 'in noscript:#text', token => $token);
4222                        
4223            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4224            ## As if </head>            ## As if </head>
# Line 3451  sub _tree_construction_main ($) { Line 4234  sub _tree_construction_main ($) {
4234            !!!cp ('t92');            !!!cp ('t92');
4235          }          }
4236    
4237              ## "after head" insertion mode          ## "after head" insertion mode
4238              ## As if <body>          ## As if <body>
4239              !!!insert-element ('body',, $token);          !!!insert-element ('body',, $token);
4240              $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4241              ## reprocess          ## reprocess
4242              redo B;          next B;
4243            } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4244              if ($token->{tag_name} eq 'head') {          if ($token->{tag_name} eq 'head') {
4245                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4246                  !!!cp ('t93');              !!!cp ('t93');
4247                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes}, $token);              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4248                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              $self->{open_elements}->[-1]->[0]->append_child
4249                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];                  ($self->{head_element});
4250                  $self->{insertion_mode} = IN_HEAD_IM;              push @{$self->{open_elements}},
4251                  !!!next-token;                  [$self->{head_element}, $el_category->{head}];
4252                  redo B;              $self->{insertion_mode} = IN_HEAD_IM;
4253                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {              !!!nack ('t93.1');
4254                  !!!cp ('t94');              !!!next-token;
4255                  #              next B;
4256                } else {            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4257                  !!!cp ('t95');              !!!cp ('t93.2');
4258                  !!!parse-error (type => 'in head:head', token => $token); # or in head noscript              !!!parse-error (type => 'after head', text => 'head',
4259                  ## Ignore the token                              token => $token);
4260                  !!!next-token;              ## Ignore the token
4261                  redo B;              !!!nack ('t93.3');
4262                }              !!!next-token;
4263              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              next B;
4264                !!!cp ('t96');            } else {
4265                ## As if <head>              !!!cp ('t95');
4266                !!!create-element ($self->{head_element}, 'head',, $token);              !!!parse-error (type => 'in head:head',
4267                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                              token => $token); # or in head noscript
4268                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];              ## Ignore the token
4269                !!!nack ('t95.1');
4270                !!!next-token;
4271                next B;
4272              }
4273            } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4274              !!!cp ('t96');
4275              ## As if <head>
4276              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4277              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4278              push @{$self->{open_elements}},
4279                  [$self->{head_element}, $el_category->{head}];
4280    
4281                $self->{insertion_mode} = IN_HEAD_IM;            $self->{insertion_mode} = IN_HEAD_IM;
4282                ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4283              } else {          } else {
4284                !!!cp ('t97');            !!!cp ('t97');
4285              }          }
4286    
4287              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
4288                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4289                  !!!cp ('t98');                  !!!cp ('t98');
4290                  ## As if </noscript>                  ## As if </noscript>
4291                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4292                  !!!parse-error (type => 'in noscript:base', token => $token);                  !!!parse-error (type => 'in noscript', text => 'base',
4293                                    token => $token);
4294                                
4295                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4296                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
# Line 3506  sub _tree_construction_main ($) { Line 4301  sub _tree_construction_main ($) {
4301                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4302                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4303                  !!!cp ('t100');                  !!!cp ('t100');
4304                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4305                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4306                    push @{$self->{open_elements}},
4307                        [$self->{head_element}, $el_category->{head}];
4308                } else {                } else {
4309                  !!!cp ('t101');                  !!!cp ('t101');
4310                }                }
# Line 3515  sub _tree_construction_main ($) { Line 4312  sub _tree_construction_main ($) {
4312                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4313                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4314                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4315                  !!!nack ('t101.1');
4316                !!!next-token;                !!!next-token;
4317                redo B;                next B;
4318              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
4319                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4320                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4321                  !!!cp ('t102');                  !!!cp ('t102');
4322                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4323                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4324                    push @{$self->{open_elements}},
4325                        [$self->{head_element}, $el_category->{head}];
4326                } else {                } else {
4327                  !!!cp ('t103');                  !!!cp ('t103');
4328                }                }
# Line 3530  sub _tree_construction_main ($) { Line 4330  sub _tree_construction_main ($) {
4330                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4331                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4332                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4333                  !!!ack ('t103.1');
4334                !!!next-token;                !!!next-token;
4335                redo B;                next B;
4336              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
4337                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4338                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4339                  !!!cp ('t104');                  !!!cp ('t104');
4340                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4341                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4342                    push @{$self->{open_elements}},
4343                        [$self->{head_element}, $el_category->{head}];
4344                } else {                } else {
4345                  !!!cp ('t105');                  !!!cp ('t105');
4346                }                }
# Line 3545  sub _tree_construction_main ($) { Line 4348  sub _tree_construction_main ($) {
4348                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.
4349    
4350                unless ($self->{confident}) {                unless ($self->{confident}) {
4351                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) {
4352                    !!!cp ('t106');                    !!!cp ('t106');
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, $token->{attributes}->{charset}->{value},                        ->($self, $token->{attributes}->{charset}->{value},
4357                           $token);                           $token);
# Line 3556  sub _tree_construction_main ($) { Line 4361  sub _tree_construction_main ($) {
4361                                             $token->{attributes}->{charset}                                             $token->{attributes}->{charset}
4362                                                 ->{has_reference});                                                 ->{has_reference});
4363                  } elsif ($token->{attributes}->{content}) {                  } elsif ($token->{attributes}->{content}) {
                   ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
4364                    if ($token->{attributes}->{content}->{value}                    if ($token->{attributes}->{content}->{value}
4365                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                        =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4366                            [\x09-\x0D\x20]*=                            [\x09-\x0D\x20]*=
4367                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4368                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
4369                      !!!cp ('t107');                      !!!cp ('t107');
4370                        ## NOTE: Whether the encoding is supported or not is handled
4371                        ## in the {change_encoding} callback.
4372                      $self->{change_encoding}                      $self->{change_encoding}
4373                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4374                             $token);                             $token);
# Line 3593  sub _tree_construction_main ($) { Line 4399  sub _tree_construction_main ($) {
4399    
4400                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4401                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4402                  !!!ack ('t110.1');
4403                !!!next-token;                !!!next-token;
4404                redo B;                next B;
4405              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
4406                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4407                  !!!cp ('t111');                  !!!cp ('t111');
4408                  ## As if </noscript>                  ## As if </noscript>
4409                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4410                  !!!parse-error (type => 'in noscript:title', token => $token);                  !!!parse-error (type => 'in noscript', text => 'title',
4411                                    token => $token);
4412                                
4413                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4414                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4415                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4416                  !!!cp ('t112');                  !!!cp ('t112');
4417                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4418                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4419                    push @{$self->{open_elements}},
4420                        [$self->{head_element}, $el_category->{head}];
4421                } else {                } else {
4422                  !!!cp ('t113');                  !!!cp ('t113');
4423                }                }
# Line 3618  sub _tree_construction_main ($) { Line 4428  sub _tree_construction_main ($) {
4428                $parse_rcdata->(RCDATA_CONTENT_MODEL);                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4429                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4430                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4431                redo B;                next B;
4432              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style' or
4433                         $token->{tag_name} eq 'noframes') {
4434                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4435                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
4436                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4437                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4438                  !!!cp ('t114');                  !!!cp ('t114');
4439                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4440                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4441                    push @{$self->{open_elements}},
4442                        [$self->{head_element}, $el_category->{head}];
4443                } else {                } else {
4444                  !!!cp ('t115');                  !!!cp ('t115');
4445                }                }
4446                $parse_rcdata->(CDATA_CONTENT_MODEL);                $parse_rcdata->(CDATA_CONTENT_MODEL);
4447                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4448                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4449                redo B;                next B;
4450              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4451                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
4452                  !!!cp ('t116');                  !!!cp ('t116');
4453                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
4454                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4455                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4456                    !!!nack ('t116.1');
4457                  !!!next-token;                  !!!next-token;
4458                  redo B;                  next B;
4459                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4460                  !!!cp ('t117');                  !!!cp ('t117');
4461                  !!!parse-error (type => 'in noscript:noscript', token => $token);                  !!!parse-error (type => 'in noscript', text => 'noscript',
4462                                    token => $token);
4463                  ## Ignore the token                  ## Ignore the token
4464                    !!!nack ('t117.1');
4465                  !!!next-token;                  !!!next-token;
4466                  redo B;                  next B;
4467                } else {                } else {
4468                  !!!cp ('t118');                  !!!cp ('t118');
4469                  #                  #
# Line 3657  sub _tree_construction_main ($) { Line 4473  sub _tree_construction_main ($) {
4473                  !!!cp ('t119');                  !!!cp ('t119');
4474                  ## As if </noscript>                  ## As if </noscript>
4475                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4476                  !!!parse-error (type => 'in noscript:script', token => $token);                  !!!parse-error (type => 'in noscript', text => 'script',
4477                                    token => $token);
4478                                
4479                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4480                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4481                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4482                  !!!cp ('t120');                  !!!cp ('t120');
4483                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4484                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4485                    push @{$self->{open_elements}},
4486                        [$self->{head_element}, $el_category->{head}];
4487                } else {                } else {
4488                  !!!cp ('t121');                  !!!cp ('t121');
4489                }                }
# Line 3673  sub _tree_construction_main ($) { Line 4492  sub _tree_construction_main ($) {
4492                $script_start_tag->();                $script_start_tag->();
4493                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4494                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4495                redo B;                next B;
4496              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
4497                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
4498                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4499                  !!!cp ('t122');                  !!!cp ('t122');
4500                  ## As if </noscript>                  ## As if </noscript>
4501                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4502                  !!!parse-error (type => 'in noscript:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'in noscript',
4503                                    text => $token->{tag_name}, token => $token);
4504                                    
4505                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4506                  ## As if </head>                  ## As if </head>
# Line 3707  sub _tree_construction_main ($) { Line 4527  sub _tree_construction_main ($) {
4527                } else {                } else {
4528                  die "$0: tag name: $self->{tag_name}";                  die "$0: tag name: $self->{tag_name}";
4529                }                }
4530                  !!!nack ('t127.1');
4531                !!!next-token;                !!!next-token;
4532                redo B;                next B;
4533              } else {              } else {
4534                !!!cp ('t128');                !!!cp ('t128');
4535                #                #
# Line 3718  sub _tree_construction_main ($) { Line 4539  sub _tree_construction_main ($) {
4539                !!!cp ('t129');                !!!cp ('t129');
4540                ## As if </noscript>                ## As if </noscript>
4541                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4542                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'in noscript:/',
4543                                  text => $token->{tag_name}, token => $token);
4544                                
4545                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4546                ## As if </head>                ## As if </head>
# Line 3740  sub _tree_construction_main ($) { Line 4562  sub _tree_construction_main ($) {
4562              !!!insert-element ('body',, $token);              !!!insert-element ('body',, $token);
4563              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4564              ## reprocess              ## reprocess
4565              redo B;              !!!ack-later;
4566                next B;
4567            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
4568              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
4569                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4570                  !!!cp ('t132');                  !!!cp ('t132');
4571                  ## As if <head>                  ## As if <head>
4572                  !!!create-element ($self->{head_element}, 'head',, $token);                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4573                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4574                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4575                        [$self->{head_element}, $el_category->{head}];
4576    
4577                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4578                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4579                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4580                  !!!next-token;                  !!!next-token;
4581                  redo B;                  next B;
4582                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4583                  !!!cp ('t133');                  !!!cp ('t133');
4584                  ## As if </noscript>                  ## As if </noscript>
4585                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4586                  !!!parse-error (type => 'in noscript:/head', token => $token);                  !!!parse-error (type => 'in noscript:/',
4587                                    text => 'head', token => $token);
4588                                    
4589                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4590                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4591                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4592                  !!!next-token;                  !!!next-token;
4593                  redo B;                  next B;
4594                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4595                  !!!cp ('t134');                  !!!cp ('t134');
4596                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4597                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4598                  !!!next-token;                  !!!next-token;
4599                  redo B;                  next B;
4600                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4601                    !!!cp ('t134.1');
4602                    !!!parse-error (type => 'unmatched end tag', text => 'head',
4603                                    token => $token);
4604                    ## Ignore the token
4605                    !!!next-token;
4606                    next B;
4607                } else {                } else {
4608                  !!!cp ('t135');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
                 #  
4609                }                }
4610              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4611                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
# Line 3782  sub _tree_construction_main ($) { Line 4613  sub _tree_construction_main ($) {
4613                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4614                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4615                  !!!next-token;                  !!!next-token;
4616                  redo B;                  next B;
4617                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
4618                           $self->{insertion_mode} == AFTER_HEAD_IM) {
4619                  !!!cp ('t137');                  !!!cp ('t137');
4620                  !!!parse-error (type => 'unmatched end tag:noscript', token => $token);                  !!!parse-error (type => 'unmatched end tag',
4621                                    text => 'noscript', token => $token);
4622                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
4623                  !!!next-token;                  !!!next-token;
4624                  redo B;                  next B;
4625                } else {                } else {
4626                  !!!cp ('t138');                  !!!cp ('t138');
4627                  #                  #
# Line 3796  sub _tree_construction_main ($) { Line 4629  sub _tree_construction_main ($) {
4629              } elsif ({              } elsif ({
4630                        body => 1, html => 1,                        body => 1, html => 1,
4631                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4632                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM or
4633                  !!!cp ('t139');                    $self->{insertion_mode} == IN_HEAD_IM or
4634                  ## 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) {  
4635                  !!!cp ('t140');                  !!!cp ('t140');
4636                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
4637                                    text => $token->{tag_name}, token => $token);
4638                  ## Ignore the token                  ## Ignore the token
4639                  !!!next-token;                  !!!next-token;
4640                  redo B;                  next B;
4641                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4642                    !!!cp ('t140.1');
4643                    !!!parse-error (type => 'unmatched end tag',
4644                                    text => $token->{tag_name}, token => $token);
4645                    ## Ignore the token
4646                    !!!next-token;
4647                    next B;
4648                } else {                } else {
4649                  !!!cp ('t141');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
4650                }                }
4651                              } elsif ($token->{tag_name} eq 'p') {
4652                #                !!!cp ('t142');
4653              } elsif ({                !!!parse-error (type => 'unmatched end tag',
4654                        p => 1, br => 1,                                text => $token->{tag_name}, token => $token);
4655                       }->{$token->{tag_name}}) {                ## Ignore the token
4656                  !!!next-token;
4657                  next B;
4658                } elsif ($token->{tag_name} eq 'br') {
4659                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4660                  !!!cp ('t142');                  !!!cp ('t142.2');
4661                  ## As if <head>                  ## (before head) as if <head>, (in head) as if </head>
4662                  !!!create-element ($self->{head_element}, 'head',, $token);                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4663                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4664                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  $self->{insertion_mode} = AFTER_HEAD_IM;
4665      
4666                    ## Reprocess in the "after head" insertion mode...
4667                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4668                    !!!cp ('t143.2');
4669                    ## As if </head>
4670                    pop @{$self->{open_elements}};
4671                    $self->{insertion_mode} = AFTER_HEAD_IM;
4672      
4673                    ## Reprocess in the "after head" insertion mode...
4674                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4675                    !!!cp ('t143.3');
4676                    ## ISSUE: Two parse errors for <head><noscript></br>
4677                    !!!parse-error (type => 'unmatched end tag',
4678                                    text => 'br', token => $token);
4679                    ## As if </noscript>
4680                    pop @{$self->{open_elements}};
4681                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4682    
4683                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4684                } else {                  ## As if </head>
4685                  !!!cp ('t143');                  pop @{$self->{open_elements}};
4686                }                  $self->{insertion_mode} = AFTER_HEAD_IM;
4687    
4688                #                  ## Reprocess in the "after head" insertion mode...
4689              } else {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4690                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                  !!!cp ('t143.4');
                 !!!cp ('t144');  
4691                  #                  #
4692                } else {                } else {
4693                  !!!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;  
4694                }                }
4695    
4696                  ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
4697                  !!!parse-error (type => 'unmatched end tag',
4698                                  text => 'br', token => $token);
4699                  ## Ignore the token
4700                  !!!next-token;
4701                  next B;
4702                } else {
4703                  !!!cp ('t145');
4704                  !!!parse-error (type => 'unmatched end tag',
4705                                  text => $token->{tag_name}, token => $token);
4706                  ## Ignore the token
4707                  !!!next-token;
4708                  next B;
4709              }              }
4710    
4711              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4712                !!!cp ('t146');                !!!cp ('t146');
4713                ## As if </noscript>                ## As if </noscript>
4714                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4715                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'in noscript:/',
4716                                  text => $token->{tag_name}, token => $token);
4717                                
4718                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4719                ## As if </head>                ## As if </head>
# Line 3866  sub _tree_construction_main ($) { Line 4729  sub _tree_construction_main ($) {
4729              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4730  ## ISSUE: This case cannot be reached?  ## ISSUE: This case cannot be reached?
4731                !!!cp ('t148');                !!!cp ('t148');
4732                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag',
4733                                  text => $token->{tag_name}, token => $token);
4734                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
4735                !!!next-token;                !!!next-token;
4736                redo B;                next B;
4737              } else {              } else {
4738                !!!cp ('t149');                !!!cp ('t149');
4739              }              }
# Line 3879  sub _tree_construction_main ($) { Line 4743  sub _tree_construction_main ($) {
4743              !!!insert-element ('body',, $token);              !!!insert-element ('body',, $token);
4744              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4745              ## reprocess              ## reprocess
4746              redo B;              next B;
4747        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4748          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4749            !!!cp ('t149.1');            !!!cp ('t149.1');
4750    
4751            ## NOTE: As if <head>            ## NOTE: As if <head>
4752            !!!create-element ($self->{head_element}, 'head',, $token);            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4753            $self->{open_elements}->[-1]->[0]->append_child            $self->{open_elements}->[-1]->[0]->append_child
4754                ($self->{head_element});                ($self->{head_element});
4755            #push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            #push @{$self->{open_elements}},
4756              #    [$self->{head_element}, $el_category->{head}];
4757            #$self->{insertion_mode} = IN_HEAD_IM;            #$self->{insertion_mode} = IN_HEAD_IM;
4758            ## NOTE: Reprocess.            ## NOTE: Reprocess.
4759    
# Line 3932  sub _tree_construction_main ($) { Line 4797  sub _tree_construction_main ($) {
4797          !!!insert-element ('body',, $token);          !!!insert-element ('body',, $token);
4798          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4799          ## NOTE: Reprocess.          ## NOTE: Reprocess.
4800          redo B;          next B;
4801        } else {        } else {
4802          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
4803        }        }
# Line 3947  sub _tree_construction_main ($) { Line 4812  sub _tree_construction_main ($) {
4812              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4813    
4814              !!!next-token;              !!!next-token;
4815              redo B;              next B;
4816            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
4817              if ({              if ({
4818                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
# Line 3957  sub _tree_construction_main ($) { Line 4822  sub _tree_construction_main ($) {
4822                  ## have an element in table scope                  ## have an element in table scope
4823                  for (reverse 0..$#{$self->{open_elements}}) {                  for (reverse 0..$#{$self->{open_elements}}) {
4824                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4825                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] & TABLE_CELL_EL) {
4826                      !!!cp ('t151');                      !!!cp ('t151');
4827    
4828                      ## Close the cell                      ## Close the cell
4829                      !!!back-token; # <?>                      !!!back-token; # <x>
4830                      $token = {type => END_TAG_TOKEN, tag_name => $node->[1],                      $token = {type => END_TAG_TOKEN,
4831                                  tag_name => $node->[0]->manakai_local_name,
4832                                line => $token->{line},                                line => $token->{line},
4833                                column => $token->{column}};                                column => $token->{column}};
4834                      redo B;                      next B;
4835                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
4836                      !!!cp ('t152');                      !!!cp ('t152');
4837                      ## ISSUE: This case can never be reached, maybe.                      ## ISSUE: This case can never be reached, maybe.
4838                      last;                      last;
# Line 3977  sub _tree_construction_main ($) { Line 4841  sub _tree_construction_main ($) {
4841    
4842                  !!!cp ('t153');                  !!!cp ('t153');
4843                  !!!parse-error (type => 'start tag not allowed',                  !!!parse-error (type => 'start tag not allowed',
4844                      value => $token->{tag_name}, token => $token);                      text => $token->{tag_name}, token => $token);
4845                  ## Ignore the token                  ## Ignore the token
4846                    !!!nack ('t153.1');
4847                  !!!next-token;                  !!!next-token;
4848                  redo B;                  next B;
4849                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4850                  !!!parse-error (type => 'not closed:caption', token => $token);                  !!!parse-error (type => 'not closed', text => 'caption',
4851                                    token => $token);
4852                                    
4853                  ## NOTE: As if </caption>.                  ## NOTE: As if </caption>.
4854                  ## have a table element in table scope                  ## have a table element in table scope
# Line 3990  sub _tree_construction_main ($) { Line 4856  sub _tree_construction_main ($) {
4856                  INSCOPE: {                  INSCOPE: {
4857                    for (reverse 0..$#{$self->{open_elements}}) {                    for (reverse 0..$#{$self->{open_elements}}) {
4858                      my $node = $self->{open_elements}->[$_];                      my $node = $self->{open_elements}->[$_];
4859                      if ($node->[1] eq 'caption') {                      if ($node->[1] & CAPTION_EL) {
4860                        !!!cp ('t155');                        !!!cp ('t155');
4861                        $i = $_;                        $i = $_;
4862                        last INSCOPE;                        last INSCOPE;
4863                      } elsif ({                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
                               table => 1, html => 1,  
                              }->{$node->[1]}) {  
4864                        !!!cp ('t156');                        !!!cp ('t156');
4865                        last;                        last;
4866                      }                      }
# Line 4004  sub _tree_construction_main ($) { Line 4868  sub _tree_construction_main ($) {
4868    
4869                    !!!cp ('t157');                    !!!cp ('t157');
4870                    !!!parse-error (type => 'start tag not allowed',                    !!!parse-error (type => 'start tag not allowed',
4871                                    value => $token->{tag_name}, token => $token);                                    text => $token->{tag_name}, token => $token);
4872                    ## Ignore the token                    ## Ignore the token
4873                      !!!nack ('t157.1');
4874                    !!!next-token;                    !!!next-token;
4875                    redo B;                    next B;
4876                  } # INSCOPE                  } # INSCOPE
4877                                    
4878                  ## generate implied end tags                  ## generate implied end tags
4879                  while ({                  while ($self->{open_elements}->[-1]->[1]
4880                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4881                    !!!cp ('t158');                    !!!cp ('t158');
4882                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4883                  }                  }
4884    
4885                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4886                    !!!cp ('t159');                    !!!cp ('t159');
4887                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                    !!!parse-error (type => 'not closed',
4888                                      text => $self->{open_elements}->[-1]->[0]
4889                                          ->manakai_local_name,
4890                                      token => $token);
4891                  } else {                  } else {
4892                    !!!cp ('t160');                    !!!cp ('t160');
4893                  }                  }
# Line 4032  sub _tree_construction_main ($) { Line 4899  sub _tree_construction_main ($) {
4899                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4900                                    
4901                  ## reprocess                  ## reprocess
4902                  redo B;                  !!!ack-later;
4903                    next B;
4904                } else {                } else {
4905                  !!!cp ('t161');                  !!!cp ('t161');
4906                  #                  #
# Line 4048  sub _tree_construction_main ($) { Line 4916  sub _tree_construction_main ($) {
4916                  my $i;                  my $i;
4917                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4918                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4919                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4920                      !!!cp ('t163');                      !!!cp ('t163');
4921                      $i = $_;                      $i = $_;
4922                      last INSCOPE;                      last INSCOPE;
4923                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
4924                      !!!cp ('t164');                      !!!cp ('t164');
4925                      last INSCOPE;                      last INSCOPE;
4926                    }                    }
4927                  } # INSCOPE                  } # INSCOPE
4928                    unless (defined $i) {                    unless (defined $i) {
4929                      !!!cp ('t165');                      !!!cp ('t165');
4930                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                      !!!parse-error (type => 'unmatched end tag',
4931                                        text => $token->{tag_name},
4932                                        token => $token);
4933                      ## Ignore the token                      ## Ignore the token
4934                      !!!next-token;                      !!!next-token;
4935                      redo B;                      next B;
4936                    }                    }
4937                                    
4938                  ## generate implied end tags                  ## generate implied end tags
4939                  while ({                  while ($self->{open_elements}->[-1]->[1]
4940                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4941                    !!!cp ('t166');                    !!!cp ('t166');
4942                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4943                  }                  }
4944    
4945                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
4946                            ne $token->{tag_name}) {
4947                    !!!cp ('t167');                    !!!cp ('t167');
4948                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                    !!!parse-error (type => 'not closed',
4949                                      text => $self->{open_elements}->[-1]->[0]
4950                                          ->manakai_local_name,
4951                                      token => $token);
4952                  } else {                  } else {
4953                    !!!cp ('t168');                    !!!cp ('t168');
4954                  }                  }
# Line 4089  sub _tree_construction_main ($) { Line 4960  sub _tree_construction_main ($) {
4960                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
4961                                    
4962                  !!!next-token;                  !!!next-token;
4963                  redo B;                  next B;
4964                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4965                  !!!cp ('t169');                  !!!cp ('t169');
4966                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
4967                                    text => $token->{tag_name}, token => $token);
4968                  ## Ignore the token                  ## Ignore the token
4969                  !!!next-token;                  !!!next-token;
4970                  redo B;                  next B;
4971                } else {                } else {
4972                  !!!cp ('t170');                  !!!cp ('t170');
4973                  #                  #
# Line 4107  sub _tree_construction_main ($) { Line 4979  sub _tree_construction_main ($) {
4979                  INSCOPE: {                  INSCOPE: {
4980                    for (reverse 0..$#{$self->{open_elements}}) {                    for (reverse 0..$#{$self->{open_elements}}) {
4981                      my $node = $self->{open_elements}->[$_];                      my $node = $self->{open_elements}->[$_];
4982                      if ($node->[1] eq $token->{tag_name}) {                      if ($node->[1] & CAPTION_EL) {
4983                        !!!cp ('t171');                        !!!cp ('t171');
4984                        $i = $_;                        $i = $_;
4985                        last INSCOPE;                        last INSCOPE;
4986                      } elsif ({                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
                               table => 1, html => 1,  
                              }->{$node->[1]}) {  
4987                        !!!cp ('t172');                        !!!cp ('t172');
4988                        last;                        last;
4989                      }                      }
# Line 4121  sub _tree_construction_main ($) { Line 4991  sub _tree_construction_main ($) {
4991    
4992                    !!!cp ('t173');                    !!!cp ('t173');
4993                    !!!parse-error (type => 'unmatched end tag',                    !!!parse-error (type => 'unmatched end tag',
4994                                    value => $token->{tag_name}, token => $token);                                    text => $token->{tag_name}, token => $token);
4995                    ## Ignore the token                    ## Ignore the token
4996                    !!!next-token;                    !!!next-token;
4997                    redo B;                    next B;
4998                  } # INSCOPE                  } # INSCOPE
4999                                    
5000                  ## generate implied end tags                  ## generate implied end tags
5001                  while ({                  while ($self->{open_elements}->[-1]->[1]
5002                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
5003                    !!!cp ('t174');                    !!!cp ('t174');
5004                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5005                  }                  }
5006                                    
5007                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5008                    !!!cp ('t175');                    !!!cp ('t175');
5009                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                    !!!parse-error (type => 'not closed',
5010                                      text => $self->{open_elements}->[-1]->[0]
5011                                          ->manakai_local_name,
5012                                      token => $token);
5013                  } else {                  } else {
5014                    !!!cp ('t176');                    !!!cp ('t176');
5015                  }                  }
# Line 4149  sub _tree_construction_main ($) { Line 5021  sub _tree_construction_main ($) {
5021                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5022                                    
5023                  !!!next-token;                  !!!next-token;
5024                  redo B;                  next B;
5025                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
5026                  !!!cp ('t177');                  !!!cp ('t177');
5027                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
5028                                    text => $token->{tag_name}, token => $token);
5029                  ## Ignore the token                  ## Ignore the token
5030                  !!!next-token;                  !!!next-token;
5031                  redo B;                  next B;
5032                } else {                } else {
5033                  !!!cp ('t178');                  !!!cp ('t178');
5034                  #                  #
# Line 4171  sub _tree_construction_main ($) { Line 5044  sub _tree_construction_main ($) {
5044                INSCOPE: {                INSCOPE: {
5045                  for (reverse 0..$#{$self->{open_elements}}) {                  for (reverse 0..$#{$self->{open_elements}}) {
5046                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5047                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5048                      !!!cp ('t179');                      !!!cp ('t179');
5049                      $i = $_;                      $i = $_;
5050    
5051                      ## Close the cell                      ## Close the cell
5052                      !!!back-token; # </?>                      !!!back-token; # </x>
5053                      $token = {type => END_TAG_TOKEN, tag_name => $tn,                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
5054                                line => $token->{line},                                line => $token->{line},
5055                                column => $token->{column}};                                column => $token->{column}};
5056                      redo B;                      next B;
5057                    } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {                    } elsif ($node->[1] & TABLE_CELL_EL) {
5058                      !!!cp ('t180');                      !!!cp ('t180');
5059                      $tn = $node->[1];                      $tn = $node->[0]->manakai_local_name;
5060                      ## NOTE: There is exactly one |td| or |th| element                      ## NOTE: There is exactly one |td| or |th| element
5061                      ## in scope in the stack of open elements by definition.                      ## in scope in the stack of open elements by definition.
5062                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5063                      ## ISSUE: Can this be reached?                      ## ISSUE: Can this be reached?
5064                      !!!cp ('t181');                      !!!cp ('t181');
5065                      last;                      last;
# Line 4197  sub _tree_construction_main ($) { Line 5068  sub _tree_construction_main ($) {
5068    
5069                  !!!cp ('t182');                  !!!cp ('t182');
5070                  !!!parse-error (type => 'unmatched end tag',                  !!!parse-error (type => 'unmatched end tag',
5071                      value => $token->{tag_name}, token => $token);                      text => $token->{tag_name}, token => $token);
5072                  ## Ignore the token                  ## Ignore the token
5073                  !!!next-token;                  !!!next-token;
5074                  redo B;                  next B;
5075                } # INSCOPE                } # INSCOPE
5076              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
5077                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5078                !!!parse-error (type => 'not closed:caption', token => $token);                !!!parse-error (type => 'not closed', text => 'caption',
5079                                  token => $token);
5080    
5081                ## As if </caption>                ## As if </caption>
5082                ## have a table element in table scope                ## have a table element in table scope
5083                my $i;                my $i;
5084                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5085                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5086                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
5087                    !!!cp ('t184');                    !!!cp ('t184');
5088                    $i = $_;                    $i = $_;
5089                    last INSCOPE;                    last INSCOPE;
5090                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5091                    !!!cp ('t185');                    !!!cp ('t185');
5092                    last INSCOPE;                    last INSCOPE;
5093                  }                  }
5094                } # INSCOPE                } # INSCOPE
5095                unless (defined $i) {                unless (defined $i) {
5096                  !!!cp ('t186');                  !!!cp ('t186');
5097                  !!!parse-error (type => 'unmatched end tag:caption', token => $token);                  !!!parse-error (type => 'unmatched end tag',
5098                                    text => 'caption', token => $token);
5099                  ## Ignore the token                  ## Ignore the token
5100                  !!!next-token;                  !!!next-token;
5101                  redo B;                  next B;
5102                }                }
5103                                
5104                ## generate implied end tags                ## generate implied end tags
5105                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
5106                  !!!cp ('t187');                  !!!cp ('t187');
5107                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5108                }                }
5109    
5110                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5111                  !!!cp ('t188');                  !!!cp ('t188');
5112                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                  !!!parse-error (type => 'not closed',
5113                                    text => $self->{open_elements}->[-1]->[0]
5114                                        ->manakai_local_name,
5115                                    token => $token);
5116                } else {                } else {
5117                  !!!cp ('t189');                  !!!cp ('t189');
5118                }                }
# Line 4252  sub _tree_construction_main ($) { Line 5124  sub _tree_construction_main ($) {
5124                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5125    
5126                ## reprocess                ## reprocess
5127                redo B;                next B;
5128              } elsif ({              } elsif ({
5129                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
5130                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5131                if ($self->{insertion_mode} & BODY_TABLE_IMS) {                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
5132                  !!!cp ('t190');                  !!!cp ('t190');
5133                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
5134                                    text => $token->{tag_name}, token => $token);
5135                  ## Ignore the token                  ## Ignore the token
5136                  !!!next-token;                  !!!next-token;
5137                  redo B;                  next B;
5138                } else {                } else {
5139                  !!!cp ('t191');                  !!!cp ('t191');
5140                  #                  #
# Line 4272  sub _tree_construction_main ($) { Line 5145  sub _tree_construction_main ($) {
5145                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
5146                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5147                !!!cp ('t192');                !!!cp ('t192');
5148                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag',
5149                                  text => $token->{tag_name}, token => $token);
5150                ## Ignore the token                ## Ignore the token
5151                !!!next-token;                !!!next-token;
5152                redo B;                next B;
5153              } else {              } else {
5154                !!!cp ('t193');                !!!cp ('t193');
5155                #                #
5156              }              }
5157        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5158          for my $entry (@{$self->{open_elements}}) {          for my $entry (@{$self->{open_elements}}) {
5159            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]}) {  
5160              !!!cp ('t75');              !!!cp ('t75');
5161              !!!parse-error (type => 'in body:#eof', token => $token);              !!!parse-error (type => 'in body:#eof', token => $token);
5162              last;              last;
# Line 4309  sub _tree_construction_main ($) { Line 5180  sub _tree_construction_main ($) {
5180            unless (length $token->{data}) {            unless (length $token->{data}) {
5181              !!!cp ('t194');              !!!cp ('t194');
5182              !!!next-token;              !!!next-token;
5183              redo B;              next B;
5184            } else {            } else {
5185              !!!cp ('t195');              !!!cp ('t195');
5186            }            }
5187          }          }
5188    
5189              !!!parse-error (type => 'in table:#character', token => $token);          !!!parse-error (type => 'in table:#text', token => $token);
5190    
5191              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
5192              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
# Line 4323  sub _tree_construction_main ($) { Line 5194  sub _tree_construction_main ($) {
5194              ## result in a new Text node.              ## result in a new Text node.
5195              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
5196                            
5197              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]}) {  
5198                # MUST                # MUST
5199                my $foster_parent_element;                my $foster_parent_element;
5200                my $next_sibling;                my $next_sibling;
5201                my $prev_sibling;                my $prev_sibling;
5202                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
5203                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
5204                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
5205                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
5206                      !!!cp ('t196');                      !!!cp ('t196');
# Line 4367  sub _tree_construction_main ($) { Line 5235  sub _tree_construction_main ($) {
5235          }          }
5236                            
5237          !!!next-token;          !!!next-token;
5238          redo B;          next B;
5239        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5240              if ({          if ({
5241                   tr => ($self->{insertion_mode} != IN_ROW_IM),               tr => ($self->{insertion_mode} != IN_ROW_IM),
5242                   th => 1, td => 1,               th => 1, td => 1,
5243                  }->{$token->{tag_name}}) {              }->{$token->{tag_name}}) {
5244                if ($self->{insertion_mode} == IN_TABLE_IM) {            if ($self->{insertion_mode} == IN_TABLE_IM) {
5245                  ## Clear back to table context              ## Clear back to table context
5246                  while ($self->{open_elements}->[-1]->[1] ne 'table' and              while (not ($self->{open_elements}->[-1]->[1]
5247                         $self->{open_elements}->[-1]->[1] ne 'html') {                              & TABLE_SCOPING_EL)) {
5248                    !!!cp ('t201');                !!!cp ('t201');
5249                    pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5250                  }              }
5251                                
5252                  !!!insert-element ('tbody',, $token);              !!!insert-element ('tbody',, $token);
5253                  $self->{insertion_mode} = IN_TABLE_BODY_IM;              $self->{insertion_mode} = IN_TABLE_BODY_IM;
5254                  ## reprocess in the "in table body" insertion mode...              ## reprocess in the "in table body" insertion mode...
5255                }            }
5256              
5257                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {            if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5258                  unless ($token->{tag_name} eq 'tr') {              unless ($token->{tag_name} eq 'tr') {
5259                    !!!cp ('t202');                !!!cp ('t202');
5260                    !!!parse-error (type => 'missing start tag:tr', token => $token);                !!!parse-error (type => 'missing start tag:tr', token => $token);
5261                  }              }
5262                                    
5263                  ## Clear back to table body context              ## Clear back to table body context
5264                  while (not {              while (not ($self->{open_elements}->[-1]->[1]
5265                    tbody => 1, tfoot => 1, thead => 1, html => 1,                              & TABLE_ROWS_SCOPING_EL)) {
5266                  }->{$self->{open_elements}->[-1]->[1]}) {                !!!cp ('t203');
5267                    !!!cp ('t203');                ## ISSUE: Can this case be reached?
5268                    ## ISSUE: Can this case be reached?                pop @{$self->{open_elements}};
5269                    pop @{$self->{open_elements}};              }
                 }  
5270                                    
5271                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
5272                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5273                    !!!cp ('t204');                    !!!cp ('t204');
5274                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5275                      !!!nack ('t204');
5276                    !!!next-token;                    !!!next-token;
5277                    redo B;                    next B;
5278                  } else {                  } else {
5279                    !!!cp ('t205');                    !!!cp ('t205');
5280                    !!!insert-element ('tr',, $token);                    !!!insert-element ('tr',, $token);
# Line 4417  sub _tree_construction_main ($) { Line 5285  sub _tree_construction_main ($) {
5285                }                }
5286    
5287                ## Clear back to table row context                ## Clear back to table row context
5288                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5289                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5290                  !!!cp ('t207');                  !!!cp ('t207');
5291                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5292                }                }
# Line 4429  sub _tree_construction_main ($) { Line 5296  sub _tree_construction_main ($) {
5296    
5297                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
5298                                
5299                  !!!nack ('t207.1');
5300                !!!next-token;                !!!next-token;
5301                redo B;                next B;
5302              } elsif ({              } elsif ({
5303                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5304                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
# Line 4442  sub _tree_construction_main ($) { Line 5310  sub _tree_construction_main ($) {
5310                  my $i;                  my $i;
5311                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5312                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5313                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5314                      !!!cp ('t208');                      !!!cp ('t208');
5315                      $i = $_;                      $i = $_;
5316                      last INSCOPE;                      last INSCOPE;
5317                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             html => 1,  
   
                             ## NOTE: This element does not appear here, maybe.  
                             table => 1,  
                            }->{$node->[1]}) {  
5318                      !!!cp ('t209');                      !!!cp ('t209');
5319                      last INSCOPE;                      last INSCOPE;
5320                    }                    }
5321                  } # INSCOPE                  } # INSCOPE
5322                  unless (defined $i) {                  unless (defined $i) {
5323                   !!!cp ('t210');                    !!!cp ('t210');
5324  ## TODO: This type is wrong.  ## TODO: This type is wrong.
5325                   !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);                    !!!parse-error (type => 'unmacthed end tag',
5326                                      text => $token->{tag_name}, token => $token);
5327                    ## Ignore the token                    ## Ignore the token
5328                      !!!nack ('t210.1');
5329                    !!!next-token;                    !!!next-token;
5330                    redo B;                    next B;
5331                  }                  }
5332                                    
5333                  ## Clear back to table row context                  ## Clear back to table row context
5334                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5335                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5336                    !!!cp ('t211');                    !!!cp ('t211');
5337                    ## ISSUE: Can this case be reached?                    ## ISSUE: Can this case be reached?
5338                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4479  sub _tree_construction_main ($) { Line 5343  sub _tree_construction_main ($) {
5343                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5344                    !!!cp ('t212');                    !!!cp ('t212');
5345                    ## reprocess                    ## reprocess
5346                    redo B;                    !!!ack-later;
5347                      next B;
5348                  } else {                  } else {
5349                    !!!cp ('t213');                    !!!cp ('t213');
5350                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
# Line 4491  sub _tree_construction_main ($) { Line 5356  sub _tree_construction_main ($) {
5356                  my $i;                  my $i;
5357                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5358                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5359                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5360                      !!!cp ('t214');                      !!!cp ('t214');
5361                      $i = $_;                      $i = $_;
5362                      last INSCOPE;                      last INSCOPE;
5363                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5364                      !!!cp ('t215');                      !!!cp ('t215');
5365                      last INSCOPE;                      last INSCOPE;
5366                    }                    }
5367                  } # INSCOPE                  } # INSCOPE
5368                  unless (defined $i) {                  unless (defined $i) {
5369                    !!!cp ('t216');                    !!!cp ('t216');
5370  ## TODO: This erorr type ios wrong.  ## TODO: This erorr type is wrong.
5371                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                    !!!parse-error (type => 'unmatched end tag',
5372                                      text => $token->{tag_name}, token => $token);
5373                    ## Ignore the token                    ## Ignore the token
5374                      !!!nack ('t216.1');
5375                    !!!next-token;                    !!!next-token;
5376                    redo B;                    next B;
5377                  }                  }
5378    
5379                  ## Clear back to table body context                  ## Clear back to table body context
5380                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5381                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5382                    !!!cp ('t217');                    !!!cp ('t217');
5383                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5384                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4538  sub _tree_construction_main ($) { Line 5400  sub _tree_construction_main ($) {
5400    
5401                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
5402                  ## Clear back to table context                  ## Clear back to table context
5403                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5404                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5405                    !!!cp ('t219');                    !!!cp ('t219');
5406                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5407                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4548  sub _tree_construction_main ($) { Line 5410  sub _tree_construction_main ($) {
5410                  !!!insert-element ('colgroup',, $token);                  !!!insert-element ('colgroup',, $token);
5411                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5412                  ## reprocess                  ## reprocess
5413                  redo B;                  !!!ack-later;
5414                    next B;
5415                } elsif ({                } elsif ({
5416                          caption => 1,                          caption => 1,
5417                          colgroup => 1,                          colgroup => 1,
5418                          tbody => 1, tfoot => 1, thead => 1,                          tbody => 1, tfoot => 1, thead => 1,
5419                         }->{$token->{tag_name}}) {                         }->{$token->{tag_name}}) {
5420                  ## Clear back to table context                  ## Clear back to table context
5421                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5422                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5423                    !!!cp ('t220');                    !!!cp ('t220');
5424                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5425                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4574  sub _tree_construction_main ($) { Line 5437  sub _tree_construction_main ($) {
5437                                             thead => IN_TABLE_BODY_IM,                                             thead => IN_TABLE_BODY_IM,
5438                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
5439                  !!!next-token;                  !!!next-token;
5440                  redo B;                  !!!nack ('t220.1');
5441                    next B;
5442                } else {                } else {
5443                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
5444                }                }
5445              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5446                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                !!!parse-error (type => 'not closed',
5447                                  text => $self->{open_elements}->[-1]->[0]
5448                                      ->manakai_local_name,
5449                                  token => $token);
5450    
5451                ## As if </table>                ## As if </table>
5452                ## have a table element in table scope                ## have a table element in table scope
5453                my $i;                my $i;
5454                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5455                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5456                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5457                    !!!cp ('t221');                    !!!cp ('t221');
5458                    $i = $_;                    $i = $_;
5459                    last INSCOPE;                    last INSCOPE;
5460                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           #table => 1,  
                           html => 1,  
                          }->{$node->[1]}) {  
5461                    !!!cp ('t222');                    !!!cp ('t222');
5462                    last INSCOPE;                    last INSCOPE;
5463                  }                  }
# Line 4601  sub _tree_construction_main ($) { Line 5465  sub _tree_construction_main ($) {
5465                unless (defined $i) {                unless (defined $i) {
5466                  !!!cp ('t223');                  !!!cp ('t223');
5467  ## TODO: The following is wrong, maybe.  ## TODO: The following is wrong, maybe.
5468                  !!!parse-error (type => 'unmatched end tag:table', token => $token);                  !!!parse-error (type => 'unmatched end tag', text => 'table',
5469                                    token => $token);
5470                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5471                    !!!nack ('t223.1');
5472                  !!!next-token;                  !!!next-token;
5473                  redo B;                  next B;
5474                }                }
5475                                
5476  ## TODO: Followings are removed from the latest spec.  ## TODO: Followings are removed from the latest spec.
5477                ## generate implied end tags                ## generate implied end tags
5478                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
5479                  !!!cp ('t224');                  !!!cp ('t224');
5480                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5481                }                }
5482    
5483                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5484                  !!!cp ('t225');                  !!!cp ('t225');
5485  ## ISSUE: Can this case be reached?                  ## NOTE: |<table><tr><table>|
5486                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                  !!!parse-error (type => 'not closed',
5487                                    text => $self->{open_elements}->[-1]->[0]
5488                                        ->manakai_local_name,
5489                                    token => $token);
5490                } else {                } else {
5491                  !!!cp ('t226');                  !!!cp ('t226');
5492                }                }
# Line 4629  sub _tree_construction_main ($) { Line 5496  sub _tree_construction_main ($) {
5496    
5497                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5498    
5499                ## reprocess            ## reprocess
5500                redo B;            !!!ack-later;
5501              next B;
5502          } elsif ($token->{tag_name} eq 'style') {          } elsif ($token->{tag_name} eq 'style') {
5503            if (not $open_tables->[-1]->[1]) { # tainted            if (not $open_tables->[-1]->[1]) { # tainted
5504              !!!cp ('t227.8');              !!!cp ('t227.8');
5505              ## NOTE: This is a "as if in head" code clone.              ## NOTE: This is a "as if in head" code clone.
5506              $parse_rcdata->(CDATA_CONTENT_MODEL);              $parse_rcdata->(CDATA_CONTENT_MODEL);
5507              redo B;              next B;
5508            } else {            } else {
5509              !!!cp ('t227.7');              !!!cp ('t227.7');
5510              #              #
# Line 4646  sub _tree_construction_main ($) { Line 5514  sub _tree_construction_main ($) {
5514              !!!cp ('t227.6');              !!!cp ('t227.6');
5515              ## NOTE: This is a "as if in head" code clone.              ## NOTE: This is a "as if in head" code clone.
5516              $script_start_tag->();              $script_start_tag->();
5517              redo B;              next B;
5518            } else {            } else {
5519              !!!cp ('t227.5');              !!!cp ('t227.5');
5520              #              #
# Line 4657  sub _tree_construction_main ($) { Line 5525  sub _tree_construction_main ($) {
5525                my $type = lc $token->{attributes}->{type}->{value};                my $type = lc $token->{attributes}->{type}->{value};
5526                if ($type eq 'hidden') {                if ($type eq 'hidden') {
5527                  !!!cp ('t227.3');                  !!!cp ('t227.3');
5528                  !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'in table',
5529                                    text => $token->{tag_name}, token => $token);
5530    
5531                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5532    
# Line 4666  sub _tree_construction_main ($) { Line 5535  sub _tree_construction_main ($) {
5535                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5536    
5537                  !!!next-token;                  !!!next-token;
5538                  redo B;                  !!!ack ('t227.2.1');
5539                    next B;
5540                } else {                } else {
5541                  !!!cp ('t227.2');                  !!!cp ('t227.2');
5542                  #                  #
# Line 4684  sub _tree_construction_main ($) { Line 5554  sub _tree_construction_main ($) {
5554            #            #
5555          }          }
5556    
5557          !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'in table', text => $token->{tag_name},
5558                            token => $token);
5559    
5560          $insert = $insert_to_foster;          $insert = $insert_to_foster;
5561          #          #
# Line 4695  sub _tree_construction_main ($) { Line 5566  sub _tree_construction_main ($) {
5566                my $i;                my $i;
5567                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5568                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5569                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_ROW_EL) {
5570                    !!!cp ('t228');                    !!!cp ('t228');
5571                    $i = $_;                    $i = $_;
5572                    last INSCOPE;                    last INSCOPE;
5573                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5574                    !!!cp ('t229');                    !!!cp ('t229');
5575                    last INSCOPE;                    last INSCOPE;
5576                  }                  }
5577                } # INSCOPE                } # INSCOPE
5578                unless (defined $i) {                unless (defined $i) {
5579                  !!!cp ('t230');                  !!!cp ('t230');
5580                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
5581                                    text => $token->{tag_name}, token => $token);
5582                  ## Ignore the token                  ## Ignore the token
5583                    !!!nack ('t230.1');
5584                  !!!next-token;                  !!!next-token;
5585                  redo B;                  next B;
5586                } else {                } else {
5587                  !!!cp ('t232');                  !!!cp ('t232');
5588                }                }
5589    
5590                ## Clear back to table row context                ## Clear back to table row context
5591                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5592                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5593                  !!!cp ('t231');                  !!!cp ('t231');
5594  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
5595                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 4728  sub _tree_construction_main ($) { Line 5598  sub _tree_construction_main ($) {
5598                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5599                $self->{insertion_mode} = IN_TABLE_BODY_IM;                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5600                !!!next-token;                !!!next-token;
5601                redo B;                !!!nack ('t231.1');
5602                  next B;
5603              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5604                if ($self->{insertion_mode} == IN_ROW_IM) {                if ($self->{insertion_mode} == IN_ROW_IM) {
5605                  ## As if </tr>                  ## As if </tr>
# Line 4736  sub _tree_construction_main ($) { Line 5607  sub _tree_construction_main ($) {
5607                  my $i;                  my $i;
5608                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5609                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5610                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5611                      !!!cp ('t233');                      !!!cp ('t233');
5612                      $i = $_;                      $i = $_;
5613                      last INSCOPE;                      last INSCOPE;
5614                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5615                      !!!cp ('t234');                      !!!cp ('t234');
5616                      last INSCOPE;                      last INSCOPE;
5617                    }                    }
# Line 4750  sub _tree_construction_main ($) { Line 5619  sub _tree_construction_main ($) {
5619                  unless (defined $i) {                  unless (defined $i) {
5620                    !!!cp ('t235');                    !!!cp ('t235');
5621  ## TODO: The following is wrong.  ## TODO: The following is wrong.
5622                    !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);                    !!!parse-error (type => 'unmatched end tag',
5623                                      text => $token->{type}, token => $token);
5624                    ## Ignore the token                    ## Ignore the token
5625                      !!!nack ('t236.1');
5626                    !!!next-token;                    !!!next-token;
5627                    redo B;                    next B;
5628                  }                  }
5629                                    
5630                  ## Clear back to table row context                  ## Clear back to table row context
5631                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5632                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5633                    !!!cp ('t236');                    !!!cp ('t236');
5634  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
5635                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4775  sub _tree_construction_main ($) { Line 5645  sub _tree_construction_main ($) {
5645                  my $i;                  my $i;
5646                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5647                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5648                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5649                      !!!cp ('t237');                      !!!cp ('t237');
5650                      $i = $_;                      $i = $_;
5651                      last INSCOPE;                      last INSCOPE;
5652                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5653                      !!!cp ('t238');                      !!!cp ('t238');
5654                      last INSCOPE;                      last INSCOPE;
5655                    }                    }
5656                  } # INSCOPE                  } # INSCOPE
5657                  unless (defined $i) {                  unless (defined $i) {
5658                    !!!cp ('t239');                    !!!cp ('t239');
5659                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                    !!!parse-error (type => 'unmatched end tag',
5660                                      text => $token->{tag_name}, token => $token);
5661                    ## Ignore the token                    ## Ignore the token
5662                      !!!nack ('t239.1');
5663                    !!!next-token;                    !!!next-token;
5664                    redo B;                    next B;
5665                  }                  }
5666                                    
5667                  ## Clear back to table body context                  ## Clear back to table body context
5668                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5669                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5670                    !!!cp ('t240');                    !!!cp ('t240');
5671                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5672                  }                  }
# Line 4825  sub _tree_construction_main ($) { Line 5692  sub _tree_construction_main ($) {
5692                my $i;                my $i;
5693                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5694                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5695                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_EL) {
5696                    !!!cp ('t241');                    !!!cp ('t241');
5697                    $i = $_;                    $i = $_;
5698                    last INSCOPE;                    last INSCOPE;
5699                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5700                    !!!cp ('t242');                    !!!cp ('t242');
5701                    last INSCOPE;                    last INSCOPE;
5702                  }                  }
5703                } # INSCOPE                } # INSCOPE
5704                unless (defined $i) {                unless (defined $i) {
5705                  !!!cp ('t243');                  !!!cp ('t243');
5706                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
5707                                    text => $token->{tag_name}, token => $token);
5708                  ## Ignore the token                  ## Ignore the token
5709                    !!!nack ('t243.1');
5710                  !!!next-token;                  !!!next-token;
5711                  redo B;                  next B;
5712                }                }
5713                                    
5714                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
# Line 4850  sub _tree_construction_main ($) { Line 5717  sub _tree_construction_main ($) {
5717                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5718                                
5719                !!!next-token;                !!!next-token;
5720                redo B;                next B;
5721              } elsif ({              } elsif ({
5722                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5723                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
# Line 4860  sub _tree_construction_main ($) { Line 5727  sub _tree_construction_main ($) {
5727                  my $i;                  my $i;
5728                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5729                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5730                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5731                      !!!cp ('t247');                      !!!cp ('t247');
5732                      $i = $_;                      $i = $_;
5733                      last INSCOPE;                      last INSCOPE;
5734                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5735                      !!!cp ('t248');                      !!!cp ('t248');
5736                      last INSCOPE;                      last INSCOPE;
5737                    }                    }
5738                  } # INSCOPE                  } # INSCOPE
5739                    unless (defined $i) {                    unless (defined $i) {
5740                      !!!cp ('t249');                      !!!cp ('t249');
5741                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                      !!!parse-error (type => 'unmatched end tag',
5742                                        text => $token->{tag_name}, token => $token);
5743                      ## Ignore the token                      ## Ignore the token
5744                        !!!nack ('t249.1');
5745                      !!!next-token;                      !!!next-token;
5746                      redo B;                      next B;
5747                    }                    }
5748                                    
5749                  ## As if </tr>                  ## As if </tr>
# Line 4884  sub _tree_construction_main ($) { Line 5751  sub _tree_construction_main ($) {
5751                  my $i;                  my $i;
5752                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5753                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5754                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5755                      !!!cp ('t250');                      !!!cp ('t250');
5756                      $i = $_;                      $i = $_;
5757                      last INSCOPE;                      last INSCOPE;
5758                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5759                      !!!cp ('t251');                      !!!cp ('t251');
5760                      last INSCOPE;                      last INSCOPE;
5761                    }                    }
5762                  } # INSCOPE                  } # INSCOPE
5763                    unless (defined $i) {                    unless (defined $i) {
5764                      !!!cp ('t252');                      !!!cp ('t252');
5765                      !!!parse-error (type => 'unmatched end tag:tr', token => $token);                      !!!parse-error (type => 'unmatched end tag',
5766                                        text => 'tr', token => $token);
5767                      ## Ignore the token                      ## Ignore the token
5768                        !!!nack ('t252.1');
5769                      !!!next-token;                      !!!next-token;
5770                      redo B;                      next B;
5771                    }                    }
5772                                    
5773                  ## Clear back to table row context                  ## Clear back to table row context
5774                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5775                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5776                    !!!cp ('t253');                    !!!cp ('t253');
5777  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
5778                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4921  sub _tree_construction_main ($) { Line 5787  sub _tree_construction_main ($) {
5787                my $i;                my $i;
5788                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5789                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5790                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5791                    !!!cp ('t254');                    !!!cp ('t254');
5792                    $i = $_;                    $i = $_;
5793                    last INSCOPE;                    last INSCOPE;
5794                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5795                    !!!cp ('t255');                    !!!cp ('t255');
5796                    last INSCOPE;                    last INSCOPE;
5797                  }                  }
5798                } # INSCOPE                } # INSCOPE
5799                unless (defined $i) {                unless (defined $i) {
5800                  !!!cp ('t256');                  !!!cp ('t256');
5801                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
5802                                    text => $token->{tag_name}, token => $token);
5803                  ## Ignore the token                  ## Ignore the token
5804                    !!!nack ('t256.1');
5805                  !!!next-token;                  !!!next-token;
5806                  redo B;                  next B;
5807                }                }
5808    
5809                ## Clear back to table body context                ## Clear back to table body context
5810                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5811                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5812                  !!!cp ('t257');                  !!!cp ('t257');
5813  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
5814                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 4951  sub _tree_construction_main ($) { Line 5816  sub _tree_construction_main ($) {
5816    
5817                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5818                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5819                  !!!nack ('t257.1');
5820                !!!next-token;                !!!next-token;
5821                redo B;                next B;
5822              } elsif ({              } elsif ({
5823                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
5824                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
5825                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5826                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
5827                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5828                !!!cp ('t258');            !!!cp ('t258');
5829                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
5830                ## Ignore the token                            text => $token->{tag_name}, token => $token);
5831                !!!next-token;            ## Ignore the token
5832                redo B;            !!!nack ('t258.1');
5833               !!!next-token;
5834              next B;
5835          } else {          } else {
5836            !!!cp ('t259');            !!!cp ('t259');
5837            !!!parse-error (type => 'in table:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'in table:/',
5838                              text => $token->{tag_name}, token => $token);
5839    
5840            $insert = $insert_to_foster;            $insert = $insert_to_foster;
5841            #            #
5842          }          }
5843        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5844          unless ($self->{open_elements}->[-1]->[1] eq 'html' and          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5845                  @{$self->{open_elements}} == 1) { # redundant, maybe                  @{$self->{open_elements}} == 1) { # redundant, maybe
5846            !!!parse-error (type => 'in body:#eof', token => $token);            !!!parse-error (type => 'in body:#eof', token => $token);
5847            !!!cp ('t259.1');            !!!cp ('t259.1');
# Line 4994  sub _tree_construction_main ($) { Line 5863  sub _tree_construction_main ($) {
5863                unless (length $token->{data}) {                unless (length $token->{data}) {
5864                  !!!cp ('t260');                  !!!cp ('t260');
5865                  !!!next-token;                  !!!next-token;
5866                  redo B;                  next B;
5867                }                }
5868              }              }
5869                            
# Line 5005  sub _tree_construction_main ($) { Line 5874  sub _tree_construction_main ($) {
5874                !!!cp ('t262');                !!!cp ('t262');
5875                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5876                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5877                  !!!ack ('t262.1');
5878                !!!next-token;                !!!next-token;
5879                redo B;                next B;
5880              } else {              } else {
5881                !!!cp ('t263');                !!!cp ('t263');
5882                #                #
5883              }              }
5884            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
5885              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
5886                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5887                  !!!cp ('t264');                  !!!cp ('t264');
5888                  !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);                  !!!parse-error (type => 'unmatched end tag',
5889                                    text => 'colgroup', token => $token);
5890                  ## Ignore the token                  ## Ignore the token
5891                  !!!next-token;                  !!!next-token;
5892                  redo B;                  next B;
5893                } else {                } else {
5894                  !!!cp ('t265');                  !!!cp ('t265');
5895                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
5896                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5897                  !!!next-token;                  !!!next-token;
5898                  redo B;                              next B;            
5899                }                }
5900              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
5901                !!!cp ('t266');                !!!cp ('t266');
5902                !!!parse-error (type => 'unmatched end tag:col', token => $token);                !!!parse-error (type => 'unmatched end tag',
5903                                  text => 'col', token => $token);
5904                ## Ignore the token                ## Ignore the token
5905                !!!next-token;                !!!next-token;
5906                redo B;                next B;
5907              } else {              } else {
5908                !!!cp ('t267');                !!!cp ('t267');
5909                #                #
5910              }              }
5911        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5912          if ($self->{open_elements}->[-1]->[1] eq 'html' or          if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5913              @{$self->{open_elements}} == 1) { # redundant, maybe              @{$self->{open_elements}} == 1) { # redundant, maybe
5914            !!!cp ('t270.2');            !!!cp ('t270.2');
5915            ## Stop parsing.            ## Stop parsing.
# Line 5048  sub _tree_construction_main ($) { Line 5920  sub _tree_construction_main ($) {
5920            pop @{$self->{open_elements}}; # colgroup            pop @{$self->{open_elements}}; # colgroup
5921            $self->{insertion_mode} = IN_TABLE_IM;            $self->{insertion_mode} = IN_TABLE_IM;
5922            ## Reprocess.            ## Reprocess.
5923            redo B;            next B;
5924          }          }
5925        } else {        } else {
5926          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5927        }        }
5928    
5929            ## As if </colgroup>            ## As if </colgroup>
5930            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5931              !!!cp ('t269');              !!!cp ('t269');
5932  ## TODO: Wrong error type?  ## TODO: Wrong error type?
5933              !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);              !!!parse-error (type => 'unmatched end tag',
5934                                text => 'colgroup', token => $token);
5935              ## Ignore the token              ## Ignore the token
5936                !!!nack ('t269.1');
5937              !!!next-token;              !!!next-token;
5938              redo B;              next B;
5939            } else {            } else {
5940              !!!cp ('t270');              !!!cp ('t270');
5941              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
5942              $self->{insertion_mode} = IN_TABLE_IM;              $self->{insertion_mode} = IN_TABLE_IM;
5943                !!!ack-later;
5944              ## reprocess              ## reprocess
5945              redo B;              next B;
5946            }            }
5947      } elsif ($self->{insertion_mode} & SELECT_IMS) {      } elsif ($self->{insertion_mode} & SELECT_IMS) {
5948        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5949          !!!cp ('t271');          !!!cp ('t271');
5950          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5951          !!!next-token;          !!!next-token;
5952          redo B;          next B;
5953        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5954              if ($token->{tag_name} eq 'option') {          if ($token->{tag_name} eq 'option') {
5955                if ($self->{open_elements}->[-1]->[1] eq 'option') {            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5956                  !!!cp ('t272');              !!!cp ('t272');
5957                  ## As if </option>              ## As if </option>
5958                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5959                } else {            } else {
5960                  !!!cp ('t273');              !!!cp ('t273');
5961                }            }
5962    
5963                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5964                !!!next-token;            !!!nack ('t273.1');
5965                redo B;            !!!next-token;
5966              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
5967                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
5968                  !!!cp ('t274');            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5969                  ## As if </option>              !!!cp ('t274');
5970                  pop @{$self->{open_elements}};              ## As if </option>
5971                } else {              pop @{$self->{open_elements}};
5972                  !!!cp ('t275');            } else {
5973                }              !!!cp ('t275');
5974              }
5975    
5976                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5977                  !!!cp ('t276');              !!!cp ('t276');
5978                  ## As if </optgroup>              ## As if </optgroup>
5979                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5980                } else {            } else {
5981                  !!!cp ('t277');              !!!cp ('t277');
5982                }            }
5983    
5984                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5985                !!!next-token;            !!!nack ('t277.1');
5986                redo B;            !!!next-token;
5987          } elsif ($token->{tag_name} eq 'select' or            next B;
5988                   $token->{tag_name} eq 'input' or          } elsif ({
5989                       select => 1, input => 1, textarea => 1,
5990                     }->{$token->{tag_name}} or
5991                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5992                    {                    {
5993                     caption => 1, table => 1,                     caption => 1, table => 1,
# Line 5117  sub _tree_construction_main ($) { Line 5995  sub _tree_construction_main ($) {
5995                     tr => 1, td => 1, th => 1,                     tr => 1, td => 1, th => 1,
5996                    }->{$token->{tag_name}})) {                    }->{$token->{tag_name}})) {
5997            ## TODO: The type below is not good - <select> is replaced by </select>            ## TODO: The type below is not good - <select> is replaced by </select>
5998            !!!parse-error (type => 'not closed:select', token => $token);            !!!parse-error (type => 'not closed', text => 'select',
5999                              token => $token);
6000            ## NOTE: As if the token were </select> (<select> case) or            ## NOTE: As if the token were </select> (<select> case) or
6001            ## as if there were </select> (otherwise).            ## as if there were </select> (otherwise).
6002                ## have an element in table scope            ## have an element in table scope
6003                my $i;            my $i;
6004                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6005                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6006                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
6007                    !!!cp ('t278');                !!!cp ('t278');
6008                    $i = $_;                $i = $_;
6009                    last INSCOPE;                last INSCOPE;
6010                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6011                            table => 1, html => 1,                !!!cp ('t279');
6012                           }->{$node->[1]}) {                last INSCOPE;
6013                    !!!cp ('t279');              }
6014                    last INSCOPE;            } # INSCOPE
6015                  }            unless (defined $i) {
6016                } # INSCOPE              !!!cp ('t280');
6017                unless (defined $i) {              !!!parse-error (type => 'unmatched end tag',
6018                  !!!cp ('t280');                              text => 'select', token => $token);
6019                  !!!parse-error (type => 'unmatched end tag:select', token => $token);              ## Ignore the token
6020                  ## Ignore the token              !!!nack ('t280.1');
6021                  !!!next-token;              !!!next-token;
6022                  redo B;              next B;
6023                }            }
6024                                
6025                !!!cp ('t281');            !!!cp ('t281');
6026                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6027    
6028                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6029    
6030            if ($token->{tag_name} eq 'select') {            if ($token->{tag_name} eq 'select') {
6031              !!!cp ('t281.2');              !!!nack ('t281.2');
6032              !!!next-token;              !!!next-token;
6033              redo B;              next B;
6034            } else {            } else {
6035              !!!cp ('t281.1');              !!!cp ('t281.1');
6036                !!!ack-later;
6037              ## Reprocess the token.              ## Reprocess the token.
6038              redo B;              next B;
6039            }            }
6040          } else {          } else {
6041            !!!cp ('t282');            !!!cp ('t282');
6042            !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'in select',
6043                              text => $token->{tag_name}, token => $token);
6044            ## Ignore the token            ## Ignore the token
6045              !!!nack ('t282.1');
6046            !!!next-token;            !!!next-token;
6047            redo B;            next B;
6048          }          }
6049        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6050              if ($token->{tag_name} eq 'optgroup') {          if ($token->{tag_name} eq 'optgroup') {
6051                if ($self->{open_elements}->[-1]->[1] eq 'option' and            if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
6052                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
6053                  !!!cp ('t283');              !!!cp ('t283');
6054                  ## As if </option>              ## As if </option>
6055                  splice @{$self->{open_elements}}, -2;              splice @{$self->{open_elements}}, -2;
6056                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6057                  !!!cp ('t284');              !!!cp ('t284');
6058                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6059                } else {            } else {
6060                  !!!cp ('t285');              !!!cp ('t285');
6061                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag',
6062                  ## Ignore the token                              text => $token->{tag_name}, token => $token);
6063                }              ## Ignore the token
6064                !!!next-token;            }
6065                redo B;            !!!nack ('t285.1');
6066              } elsif ($token->{tag_name} eq 'option') {            !!!next-token;
6067                if ($self->{open_elements}->[-1]->[1] eq 'option') {            next B;
6068                  !!!cp ('t286');          } elsif ($token->{tag_name} eq 'option') {
6069                  pop @{$self->{open_elements}};            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6070                } else {              !!!cp ('t286');
6071                  !!!cp ('t287');              pop @{$self->{open_elements}};
6072                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            } else {
6073                  ## Ignore the token              !!!cp ('t287');
6074                }              !!!parse-error (type => 'unmatched end tag',
6075                !!!next-token;                              text => $token->{tag_name}, token => $token);
6076                redo B;              ## Ignore the token
6077              } elsif ($token->{tag_name} eq 'select') {            }
6078                ## have an element in table scope            !!!nack ('t287.1');
6079                my $i;            !!!next-token;
6080                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            next B;
6081                  my $node = $self->{open_elements}->[$_];          } elsif ($token->{tag_name} eq 'select') {
6082                  if ($node->[1] eq $token->{tag_name}) {            ## have an element in table scope
6083                    !!!cp ('t288');            my $i;
6084                    $i = $_;            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6085                    last INSCOPE;              my $node = $self->{open_elements}->[$_];
6086                  } elsif ({              if ($node->[1] & SELECT_EL) {
6087                            table => 1, html => 1,                !!!cp ('t288');
6088                           }->{$node->[1]}) {                $i = $_;
6089                    !!!cp ('t289');                last INSCOPE;
6090                    last INSCOPE;              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6091                  }                !!!cp ('t289');
6092                } # INSCOPE                last INSCOPE;
6093                unless (defined $i) {              }
6094                  !!!cp ('t290');            } # INSCOPE
6095                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            unless (defined $i) {
6096                  ## Ignore the token              !!!cp ('t290');
6097                  !!!next-token;              !!!parse-error (type => 'unmatched end tag',
6098                  redo B;                              text => $token->{tag_name}, token => $token);
6099                }              ## Ignore the token
6100                !!!nack ('t290.1');
6101                !!!next-token;
6102                next B;
6103              }
6104                                
6105                !!!cp ('t291');            !!!cp ('t291');
6106                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6107    
6108                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6109    
6110                !!!next-token;            !!!nack ('t291.1');
6111                redo B;            !!!next-token;
6112              next B;
6113          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6114                   {                   {
6115                    caption => 1, table => 1, tbody => 1,                    caption => 1, table => 1, tbody => 1,
6116                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
6117                   }->{$token->{tag_name}}) {                   }->{$token->{tag_name}}) {
6118  ## TODO: The following is wrong?  ## TODO: The following is wrong?
6119                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
6120                              text => $token->{tag_name}, token => $token);
6121                                
6122                ## have an element in table scope            ## have an element in table scope
6123                my $i;            my $i;
6124                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6125                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6126                  if ($node->[1] eq $token->{tag_name}) {              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6127                    !!!cp ('t292');                !!!cp ('t292');
6128                    $i = $_;                $i = $_;
6129                    last INSCOPE;                last INSCOPE;
6130                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6131                            table => 1, html => 1,                !!!cp ('t293');
6132                           }->{$node->[1]}) {                last INSCOPE;
6133                    !!!cp ('t293');              }
6134                    last INSCOPE;            } # INSCOPE
6135                  }            unless (defined $i) {
6136                } # INSCOPE              !!!cp ('t294');
6137                unless (defined $i) {              ## Ignore the token
6138                  !!!cp ('t294');              !!!nack ('t294.1');
6139                  ## Ignore the token              !!!next-token;
6140                  !!!next-token;              next B;
6141                  redo B;            }
               }  
6142                                
6143                ## As if </select>            ## As if </select>
6144                ## have an element in table scope            ## have an element in table scope
6145                undef $i;            undef $i;
6146                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6147                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6148                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
6149                    !!!cp ('t295');                !!!cp ('t295');
6150                    $i = $_;                $i = $_;
6151                    last INSCOPE;                last INSCOPE;
6152                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
6153  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
6154                    !!!cp ('t296');                !!!cp ('t296');
6155                    last INSCOPE;                last INSCOPE;
6156                  }              }
6157                } # INSCOPE            } # INSCOPE
6158                unless (defined $i) {            unless (defined $i) {
6159                  !!!cp ('t297');              !!!cp ('t297');
6160  ## TODO: The following error type is correct?  ## TODO: The following error type is correct?
6161                  !!!parse-error (type => 'unmatched end tag:select', token => $token);              !!!parse-error (type => 'unmatched end tag',
6162                  ## Ignore the </select> token                              text => 'select', token => $token);
6163                  !!!next-token; ## TODO: ok?              ## Ignore the </select> token
6164                  redo B;              !!!nack ('t297.1');
6165                }              !!!next-token; ## TODO: ok?
6166                next B;
6167              }
6168                                
6169                !!!cp ('t298');            !!!cp ('t298');
6170                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6171    
6172                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6173    
6174                ## reprocess            !!!ack-later;
6175                redo B;            ## reprocess
6176              next B;
6177          } else {          } else {
6178            !!!cp ('t299');            !!!cp ('t299');
6179            !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'in select:/',
6180                              text => $token->{tag_name}, token => $token);
6181            ## Ignore the token            ## Ignore the token
6182              !!!nack ('t299.3');
6183            !!!next-token;            !!!next-token;
6184            redo B;            next B;
6185          }          }
6186        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6187          unless ($self->{open_elements}->[-1]->[1] eq 'html' and          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6188                  @{$self->{open_elements}} == 1) { # redundant, maybe                  @{$self->{open_elements}} == 1) { # redundant, maybe
6189            !!!cp ('t299.1');            !!!cp ('t299.1');
6190            !!!parse-error (type => 'in body:#eof', token => $token);            !!!parse-error (type => 'in body:#eof', token => $token);
# Line 5319  sub _tree_construction_main ($) { Line 6209  sub _tree_construction_main ($) {
6209            unless (length $token->{data}) {            unless (length $token->{data}) {
6210              !!!cp ('t300');              !!!cp ('t300');
6211              !!!next-token;              !!!next-token;
6212              redo B;              next B;
6213            }            }
6214          }          }
6215                    
6216          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6217            !!!cp ('t301');            !!!cp ('t301');
6218            !!!parse-error (type => 'after html:#character', token => $token);            !!!parse-error (type => 'after html:#text', token => $token);
6219    
6220            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
6221          } else {          } else {
# Line 5333  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:#character', token => $token);          !!!parse-error (type => 'after body:#text', token => $token);
6227    
6228          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6229          ## reprocess          ## reprocess
6230          redo B;          next B;
6231        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6232          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6233            !!!cp ('t303');            !!!cp ('t303');
6234            !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'after html',
6235                              text => $token->{tag_name}, token => $token);
6236                        
6237            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
6238          } else {          } else {
# Line 5349  sub _tree_construction_main ($) { Line 6240  sub _tree_construction_main ($) {
6240          }          }
6241    
6242          ## "after body" insertion mode          ## "after body" insertion mode
6243          !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'after body',
6244                            text => $token->{tag_name}, token => $token);
6245    
6246          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6247            !!!ack-later;
6248          ## reprocess          ## reprocess
6249          redo B;          next B;
6250        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6251          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6252            !!!cp ('t305');            !!!cp ('t305');
6253            !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'after html:/',
6254                              text => $token->{tag_name}, token => $token);
6255                        
6256            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6257            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
# Line 5369  sub _tree_construction_main ($) { Line 6263  sub _tree_construction_main ($) {
6263          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
6264            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
6265              !!!cp ('t307');              !!!cp ('t307');
6266              !!!parse-error (type => 'unmatched end tag:html', token => $token);              !!!parse-error (type => 'unmatched end tag',
6267                                text => 'html', token => $token);
6268              ## Ignore the token              ## Ignore the token
6269              !!!next-token;              !!!next-token;
6270              redo B;              next B;
6271            } else {            } else {
6272              !!!cp ('t308');              !!!cp ('t308');
6273              $self->{insertion_mode} = AFTER_HTML_BODY_IM;              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6274              !!!next-token;              !!!next-token;
6275              redo B;              next B;
6276            }            }
6277          } else {          } else {
6278            !!!cp ('t309');            !!!cp ('t309');
6279            !!!parse-error (type => 'after body:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'after body:/',
6280                              text => $token->{tag_name}, token => $token);
6281    
6282            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
6283            ## reprocess            ## reprocess
6284            redo B;            next B;
6285          }          }
6286        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6287          !!!cp ('t309.2');          !!!cp ('t309.2');
# Line 5402  sub _tree_construction_main ($) { Line 6298  sub _tree_construction_main ($) {
6298            unless (length $token->{data}) {            unless (length $token->{data}) {
6299              !!!cp ('t310');              !!!cp ('t310');
6300              !!!next-token;              !!!next-token;
6301              redo B;              next B;
6302            }            }
6303          }          }
6304                    
6305          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
6306            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6307              !!!cp ('t311');              !!!cp ('t311');
6308              !!!parse-error (type => 'in frameset:#character', token => $token);              !!!parse-error (type => 'in frameset:#text', token => $token);
6309            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6310              !!!cp ('t312');              !!!cp ('t312');
6311              !!!parse-error (type => 'after frameset:#character', token => $token);              !!!parse-error (type => 'after frameset:#text', token => $token);
6312            } else { # "after html frameset"            } else { # "after after frameset"
6313              !!!cp ('t313');              !!!cp ('t313');
6314              !!!parse-error (type => 'after html:#character', token => $token);              !!!parse-error (type => 'after html:#text', token => $token);
   
             $self->{insertion_mode} = AFTER_FRAMESET_IM;  
             ## Reprocess in the "after frameset" insertion mode.  
             !!!parse-error (type => 'after frameset:#character', token => $token);  
6315            }            }
6316                        
6317            ## Ignore the token.            ## Ignore the token.
# Line 5430  sub _tree_construction_main ($) { Line 6322  sub _tree_construction_main ($) {
6322              !!!cp ('t315');              !!!cp ('t315');
6323              !!!next-token;              !!!next-token;
6324            }            }
6325            redo B;            next B;
6326          }          }
6327                    
6328          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
6329        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
         if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {  
           !!!cp ('t316');  
           !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);  
   
           $self->{insertion_mode} = AFTER_FRAMESET_IM;  
           ## Process in the "after frameset" insertion mode.  
         } else {  
           !!!cp ('t317');  
         }  
   
6330          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6331              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6332            !!!cp ('t318');            !!!cp ('t318');
6333            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6334              !!!nack ('t318.1');
6335            !!!next-token;            !!!next-token;
6336            redo B;            next B;
6337          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
6338                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
6339            !!!cp ('t319');            !!!cp ('t319');
6340            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6341            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6342              !!!ack ('t319.1');
6343            !!!next-token;            !!!next-token;
6344            redo B;            next B;
6345          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
6346            !!!cp ('t320');            !!!cp ('t320');
6347            ## NOTE: As if in body.            ## NOTE: As if in head.
6348            $parse_rcdata->(CDATA_CONTENT_MODEL);            $parse_rcdata->(CDATA_CONTENT_MODEL);
6349            redo B;            next B;
6350    
6351              ## NOTE: |<!DOCTYPE HTML><frameset></frameset></html><noframes></noframes>|
6352              ## has no parse error.
6353          } else {          } else {
6354            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6355              !!!cp ('t321');              !!!cp ('t321');
6356              !!!parse-error (type => 'in frameset:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'in frameset',
6357            } else {                              text => $token->{tag_name}, token => $token);
6358              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6359              !!!cp ('t322');              !!!cp ('t322');
6360              !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'after frameset',
6361                                text => $token->{tag_name}, token => $token);
6362              } else { # "after after frameset"
6363                !!!cp ('t322.2');
6364                !!!parse-error (type => 'after after frameset',
6365                                text => $token->{tag_name}, token => $token);
6366            }            }
6367            ## Ignore the token            ## Ignore the token
6368              !!!nack ('t322.1');
6369            !!!next-token;            !!!next-token;
6370            redo B;            next B;
6371          }          }
6372        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
         if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {  
           !!!cp ('t323');  
           !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);  
   
           $self->{insertion_mode} = AFTER_FRAMESET_IM;  
           ## Process in the "after frameset" insertion mode.  
         } else {  
           !!!cp ('t324');  
         }  
   
6373          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6374              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6375            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6376                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
6377              !!!cp ('t325');              !!!cp ('t325');
6378              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag',
6379                                text => $token->{tag_name}, token => $token);
6380              ## Ignore the token              ## Ignore the token
6381              !!!next-token;              !!!next-token;
6382            } else {            } else {
# Line 5501  sub _tree_construction_main ($) { Line 6386  sub _tree_construction_main ($) {
6386            }            }
6387    
6388            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
6389                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6390              !!!cp ('t327');              !!!cp ('t327');
6391              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6392            } else {            } else {
6393              !!!cp ('t328');              !!!cp ('t328');
6394            }            }
6395            redo B;            next B;
6396          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
6397                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6398            !!!cp ('t329');            !!!cp ('t329');
6399            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6400            !!!next-token;            !!!next-token;
6401            redo B;            next B;
6402          } else {          } else {
6403            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6404              !!!cp ('t330');              !!!cp ('t330');
6405              !!!parse-error (type => 'in frameset:/'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'in frameset:/',
6406            } else {                              text => $token->{tag_name}, token => $token);
6407              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6408                !!!cp ('t330.1');
6409                !!!parse-error (type => 'after frameset:/',
6410                                text => $token->{tag_name}, token => $token);
6411              } else { # "after after html"
6412              !!!cp ('t331');              !!!cp ('t331');
6413              !!!parse-error (type => 'after frameset:/'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'after after frameset:/',
6414                                text => $token->{tag_name}, token => $token);
6415            }            }
6416            ## Ignore the token            ## Ignore the token
6417            !!!next-token;            !!!next-token;
6418            redo B;            next B;
6419          }          }
6420        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6421          unless ($self->{open_elements}->[-1]->[1] eq 'html' and          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6422                  @{$self->{open_elements}} == 1) { # redundant, maybe                  @{$self->{open_elements}} == 1) { # redundant, maybe
6423            !!!cp ('t331.1');            !!!cp ('t331.1');
6424            !!!parse-error (type => 'in body:#eof', token => $token);            !!!parse-error (type => 'in body:#eof', token => $token);
# Line 5552  sub _tree_construction_main ($) { Line 6443  sub _tree_construction_main ($) {
6443          !!!cp ('t332');          !!!cp ('t332');
6444          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6445          $script_start_tag->();          $script_start_tag->();
6446          redo B;          next B;
6447        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
6448          !!!cp ('t333');          !!!cp ('t333');
6449          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6450          $parse_rcdata->(CDATA_CONTENT_MODEL);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6451          redo B;          next B;
6452        } elsif ({        } elsif ({
6453                  base => 1, link => 1,                  base => 1, link => 1,
6454                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 5565  sub _tree_construction_main ($) { Line 6456  sub _tree_construction_main ($) {
6456          ## 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
6457          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6458          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6459            !!!ack ('t334.1');
6460          !!!next-token;          !!!next-token;
6461          redo B;          next B;
6462        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
6463          ## 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
6464          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6465          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.
6466    
6467          unless ($self->{confident}) {          unless ($self->{confident}) {
6468            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) {
6469              !!!cp ('t335');              !!!cp ('t335');
6470                ## NOTE: Whether the encoding is supported or not is handled
6471                ## in the {change_encoding} callback.
6472              $self->{change_encoding}              $self->{change_encoding}
6473                  ->($self, $token->{attributes}->{charset}->{value}, $token);                  ->($self, $token->{attributes}->{charset}->{value}, $token);
6474                            
# Line 5583  sub _tree_construction_main ($) { Line 6477  sub _tree_construction_main ($) {
6477                                       $token->{attributes}->{charset}                                       $token->{attributes}->{charset}
6478                                           ->{has_reference});                                           ->{has_reference});
6479            } elsif ($token->{attributes}->{content}) {            } elsif ($token->{attributes}->{content}) {
             ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
6480              if ($token->{attributes}->{content}->{value}              if ($token->{attributes}->{content}->{value}
6481                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                  =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6482                      [\x09-\x0D\x20]*=                      [\x09-\x0D\x20]*=
6483                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6484                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
6485                !!!cp ('t336');                !!!cp ('t336');
6486                  ## NOTE: Whether the encoding is supported or not is handled
6487                  ## in the {change_encoding} callback.
6488                $self->{change_encoding}                $self->{change_encoding}
6489                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6490                $meta_el->[0]->get_attribute_node_ns (undef, 'content')                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
# Line 5615  sub _tree_construction_main ($) { Line 6510  sub _tree_construction_main ($) {
6510            }            }
6511          }          }
6512    
6513            !!!ack ('t338.1');
6514          !!!next-token;          !!!next-token;
6515          redo B;          next B;
6516        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
6517          !!!cp ('t341');          !!!cp ('t341');
6518          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6519          $parse_rcdata->(RCDATA_CONTENT_MODEL);          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6520          redo B;          next B;
6521        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
6522          !!!parse-error (type => 'in body:body', token => $token);          !!!parse-error (type => 'in body', text => 'body', token => $token);
6523                                
6524          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
6525              $self->{open_elements}->[1]->[1] ne 'body') {              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6526            !!!cp ('t342');            !!!cp ('t342');
6527            ## Ignore the token            ## Ignore the token
6528          } else {          } else {
# Line 5640  sub _tree_construction_main ($) { Line 6536  sub _tree_construction_main ($) {
6536              }              }
6537            }            }
6538          }          }
6539            !!!nack ('t343.1');
6540          !!!next-token;          !!!next-token;
6541          redo B;          next B;
6542        } elsif ({        } elsif ({
6543                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6544                  div => 1, dl => 1, fieldset => 1,                  div => 1, dl => 1, fieldset => 1,
# Line 5656  sub _tree_construction_main ($) { Line 6553  sub _tree_construction_main ($) {
6553            !!!cp ('t350');            !!!cp ('t350');
6554            !!!parse-error (type => 'in form:form', token => $token);            !!!parse-error (type => 'in form:form', token => $token);
6555            ## Ignore the token            ## Ignore the token
6556              !!!nack ('t350.1');
6557            !!!next-token;            !!!next-token;
6558            redo B;            next B;
6559          }          }
6560    
6561          ## has a p element in scope          ## has a p element in scope
6562          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6563            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6564              !!!cp ('t344');              !!!cp ('t344');
6565              !!!back-token;              !!!back-token; # <form>
6566              $token = {type => END_TAG_TOKEN, tag_name => 'p',              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6567                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6568              redo B;              next B;
6569            } elsif ({            } elsif ($_->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6570              !!!cp ('t345');              !!!cp ('t345');
6571              last INSCOPE;              last INSCOPE;
6572            }            }
# Line 5679  sub _tree_construction_main ($) { Line 6574  sub _tree_construction_main ($) {
6574                        
6575          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6576          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6577              !!!nack ('t346.1');
6578            !!!next-token;            !!!next-token;
6579            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
6580              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
# Line 5695  sub _tree_construction_main ($) { Line 6591  sub _tree_construction_main ($) {
6591            !!!cp ('t347.1');            !!!cp ('t347.1');
6592            $self->{form_element} = $self->{open_elements}->[-1]->[0];            $self->{form_element} = $self->{open_elements}->[-1]->[0];
6593    
6594              !!!nack ('t347.2');
6595            !!!next-token;            !!!next-token;
6596          } elsif ($token->{tag_name} eq 'table') {          } elsif ($token->{tag_name} eq 'table') {
6597            !!!cp ('t382');            !!!cp ('t382');
# Line 5702  sub _tree_construction_main ($) { Line 6599  sub _tree_construction_main ($) {
6599                        
6600            $self->{insertion_mode} = IN_TABLE_IM;            $self->{insertion_mode} = IN_TABLE_IM;
6601    
6602              !!!nack ('t382.1');
6603            !!!next-token;            !!!next-token;
6604          } elsif ($token->{tag_name} eq 'hr') {          } elsif ($token->{tag_name} eq 'hr') {
6605            !!!cp ('t386');            !!!cp ('t386');
6606            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6607                    
6608              !!!nack ('t386.1');
6609            !!!next-token;            !!!next-token;
6610          } else {          } else {
6611            !!!cp ('t347');            !!!nack ('t347.1');
6612            !!!next-token;            !!!next-token;
6613          }          }
6614          redo B;          next B;
6615        } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {        } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
6616          ## has a p element in scope          ## has a p element in scope
6617          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6618            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6619              !!!cp ('t353');              !!!cp ('t353');
6620              !!!back-token;              !!!back-token; # <x>
6621              $token = {type => END_TAG_TOKEN, tag_name => 'p',              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6622                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6623              redo B;              next B;
6624            } elsif ({            } elsif ($_->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6625              !!!cp ('t354');              !!!cp ('t354');
6626              last INSCOPE;              last INSCOPE;
6627            }            }
# Line 5739  sub _tree_construction_main ($) { Line 6635  sub _tree_construction_main ($) {
6635                            dd => {dt => 1, dd => 1}}->{$token->{tag_name}};                            dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6636          LI: {          LI: {
6637            ## Step 2            ## Step 2
6638            if ($li_or_dtdd->{$node->[1]}) {            if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6639              if ($i != -1) {              if ($i != -1) {
6640                !!!cp ('t355');                !!!cp ('t355');
6641                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'not closed',
6642                                $self->{open_elements}->[-1]->[1], token => $token);                                text => $self->{open_elements}->[-1]->[0]
6643                                      ->manakai_local_name,
6644                                  token => $token);
6645              } else {              } else {
6646                !!!cp ('t356');                !!!cp ('t356');
6647              }              }
# Line 5754  sub _tree_construction_main ($) { Line 6652  sub _tree_construction_main ($) {
6652            }            }
6653                        
6654            ## Step 3            ## Step 3
6655            if (not $formatting_category->{$node->[1]} and            if (not ($node->[1] & FORMATTING_EL) and
6656                #not $phrasing_category->{$node->[1]} and                #not $phrasing_category->{$node->[1]} and
6657                ($special_category->{$node->[1]} or                ($node->[1] & SPECIAL_EL or
6658                 $scoping_category->{$node->[1]}) and                 $node->[1] & SCOPING_EL) and
6659                $node->[1] ne 'address' and $node->[1] ne 'div') {                not ($node->[1] & ADDRESS_EL) and
6660                  not ($node->[1] & DIV_EL)) {
6661              !!!cp ('t358');              !!!cp ('t358');
6662              last LI;              last LI;
6663            }            }
# Line 5771  sub _tree_construction_main ($) { Line 6670  sub _tree_construction_main ($) {
6670          } # LI          } # LI
6671                        
6672          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6673            !!!nack ('t359.1');
6674          !!!next-token;          !!!next-token;
6675          redo B;          next B;
6676        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
6677          ## has a p element in scope          ## has a p element in scope
6678          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6679            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6680              !!!cp ('t367');              !!!cp ('t367');
6681              !!!back-token;              !!!back-token; # <plaintext>
6682              $token = {type => END_TAG_TOKEN, tag_name => 'p',              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6683                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6684              redo B;              next B;
6685            } elsif ({            } elsif ($_->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6686              !!!cp ('t368');              !!!cp ('t368');
6687              last INSCOPE;              last INSCOPE;
6688            }            }
# Line 5795  sub _tree_construction_main ($) { Line 6692  sub _tree_construction_main ($) {
6692                        
6693          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
6694                        
6695            !!!nack ('t368.1');
6696          !!!next-token;          !!!next-token;
6697          redo B;          next B;
6698        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
6699          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
6700            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
6701            if ($node->[1] eq 'a') {            if ($node->[1] & A_EL) {
6702              !!!cp ('t371');              !!!cp ('t371');
6703              !!!parse-error (type => 'in a:a', token => $token);              !!!parse-error (type => 'in a:a', token => $token);
6704                            
6705              !!!back-token;              !!!back-token; # <a>
6706              $token = {type => END_TAG_TOKEN, tag_name => 'a',              $token = {type => END_TAG_TOKEN, tag_name => 'a',
6707                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6708              $formatting_end_tag->($token);              $formatting_end_tag->($token);
# Line 5835  sub _tree_construction_main ($) { Line 6733  sub _tree_construction_main ($) {
6733          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6734          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6735    
6736            !!!nack ('t374.1');
6737          !!!next-token;          !!!next-token;
6738          redo B;          next B;
6739        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
6740          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6741    
6742          ## has a |nobr| element in scope          ## has a |nobr| element in scope
6743          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6744            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6745            if ($node->[1] eq 'nobr') {            if ($node->[1] & NOBR_EL) {
6746              !!!cp ('t376');              !!!cp ('t376');
6747              !!!parse-error (type => 'in nobr:nobr', token => $token);              !!!parse-error (type => 'in nobr:nobr', token => $token);
6748              !!!back-token;              !!!back-token; # <nobr>
6749              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
6750                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6751              redo B;              next B;
6752            } 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]}) {  
6753              !!!cp ('t377');              !!!cp ('t377');
6754              last INSCOPE;              last INSCOPE;
6755            }            }
# Line 5862  sub _tree_construction_main ($) { Line 6758  sub _tree_construction_main ($) {
6758          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6759          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6760                    
6761            !!!nack ('t377.1');
6762          !!!next-token;          !!!next-token;
6763          redo B;          next B;
6764        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
6765          ## has a button element in scope          ## has a button element in scope
6766          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6767            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6768            if ($node->[1] eq 'button') {            if ($node->[1] & BUTTON_EL) {
6769              !!!cp ('t378');              !!!cp ('t378');
6770              !!!parse-error (type => 'in button:button', token => $token);              !!!parse-error (type => 'in button:button', token => $token);
6771              !!!back-token;              !!!back-token; # <button>
6772              $token = {type => END_TAG_TOKEN, tag_name => 'button',              $token = {type => END_TAG_TOKEN, tag_name => 'button',
6773                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6774              redo B;              next B;
6775            } 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]}) {  
6776              !!!cp ('t379');              !!!cp ('t379');
6777              last INSCOPE;              last INSCOPE;
6778            }            }
# Line 5892  sub _tree_construction_main ($) { Line 6786  sub _tree_construction_main ($) {
6786    
6787          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
6788    
6789            !!!nack ('t379.1');
6790          !!!next-token;          !!!next-token;
6791          redo B;          next B;
6792        } elsif ({        } elsif ({
6793                  xmp => 1,                  xmp => 1,
6794                  iframe => 1,                  iframe => 1,
6795                  noembed => 1,                  noembed => 1,
6796                  noframes => 1,                  noframes => 1, ## NOTE: This is an "as if in head" code clone.
6797                  noscript => 0, ## TODO: 1 if scripting is enabled                  noscript => 0, ## TODO: 1 if scripting is enabled
6798                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6799          if ($token->{tag_name} eq 'xmp') {          if ($token->{tag_name} eq 'xmp') {
# Line 5909  sub _tree_construction_main ($) { Line 6804  sub _tree_construction_main ($) {
6804          }          }
6805          ## NOTE: There is an "as if in body" code clone.          ## NOTE: There is an "as if in body" code clone.
6806          $parse_rcdata->(CDATA_CONTENT_MODEL);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6807          redo B;          next B;
6808        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
6809          !!!parse-error (type => 'isindex', token => $token);          !!!parse-error (type => 'isindex', token => $token);
6810                    
6811          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
6812            !!!cp ('t389');            !!!cp ('t389');
6813            ## Ignore the token            ## Ignore the token
6814              !!!nack ('t389'); ## NOTE: Not acknowledged.
6815            !!!next-token;            !!!next-token;
6816            redo B;            next B;
6817          } else {          } else {
6818              !!!ack ('t391.1');
6819    
6820            my $at = $token->{attributes};            my $at = $token->{attributes};
6821            my $form_attrs;            my $form_attrs;
6822            $form_attrs->{action} = $at->{action} if $at->{action};            $form_attrs->{action} = $at->{action} if $at->{action};
# Line 5962  sub _tree_construction_main ($) { Line 6860  sub _tree_construction_main ($) {
6860                           line => $token->{line}, column => $token->{column}},                           line => $token->{line}, column => $token->{column}},
6861                          {type => END_TAG_TOKEN, tag_name => 'form',                          {type => END_TAG_TOKEN, tag_name => 'form',
6862                           line => $token->{line}, column => $token->{column}};                           line => $token->{line}, column => $token->{column}};
           $token = shift @tokens;  
6863            !!!back-token (@tokens);            !!!back-token (@tokens);
6864            redo B;            !!!next-token;
6865              next B;
6866          }          }
6867        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
6868          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
6869          my $el;          my $el;
6870          !!!create-element ($el, $token->{tag_name}, $token->{attributes}, $token);          !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
6871                    
6872          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
6873          $self->{content_model} = RCDATA_CONTENT_MODEL;          $self->{content_model} = RCDATA_CONTENT_MODEL;
# Line 5978  sub _tree_construction_main ($) { Line 6876  sub _tree_construction_main ($) {
6876          $insert->($el);          $insert->($el);
6877                    
6878          my $text = '';          my $text = '';
6879            !!!nack ('t392.1');
6880          !!!next-token;          !!!next-token;
6881          if ($token->{type} == CHARACTER_TOKEN) {          if ($token->{type} == CHARACTER_TOKEN) {
6882            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
# Line 6008  sub _tree_construction_main ($) { Line 6907  sub _tree_construction_main ($) {
6907            ## Ignore the token            ## Ignore the token
6908          } else {          } else {
6909            !!!cp ('t398');            !!!cp ('t398');
6910            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);            !!!parse-error (type => 'in RCDATA:#eof', token => $token);
6911          }          }
6912          !!!next-token;          !!!next-token;
6913            next B;
6914          } elsif ($token->{tag_name} eq 'rt' or
6915                   $token->{tag_name} eq 'rp') {
6916            ## has a |ruby| element in scope
6917            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6918              my $node = $self->{open_elements}->[$_];
6919              if ($node->[1] & RUBY_EL) {
6920                !!!cp ('t398.1');
6921                ## generate implied end tags
6922                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
6923                  !!!cp ('t398.2');
6924                  pop @{$self->{open_elements}};
6925                }
6926                unless ($self->{open_elements}->[-1]->[1] & RUBY_EL) {
6927                  !!!cp ('t398.3');
6928                  !!!parse-error (type => 'not closed',
6929                                  text => $self->{open_elements}->[-1]->[0]
6930                                      ->manakai_local_name,
6931                                  token => $token);
6932                  pop @{$self->{open_elements}}
6933                      while not $self->{open_elements}->[-1]->[1] & RUBY_EL;
6934                }
6935                last INSCOPE;
6936              } elsif ($node->[1] & SCOPING_EL) {
6937                !!!cp ('t398.4');
6938                last INSCOPE;
6939              }
6940            } # INSCOPE
6941    
6942            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6943    
6944            !!!nack ('t398.5');
6945            !!!next-token;
6946          redo B;          redo B;
6947          } elsif ($token->{tag_name} eq 'math' or
6948                   $token->{tag_name} eq 'svg') {
6949            $reconstruct_active_formatting_elements->($insert_to_current);
6950    
6951            ## "Adjust MathML attributes" ('math' only) - done in insert-element-f
6952    
6953            ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
6954    
6955            ## "adjust foreign attributes" - done in insert-element-f
6956            
6957            !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
6958            
6959            if ($self->{self_closing}) {
6960              pop @{$self->{open_elements}};
6961              !!!ack ('t398.1');
6962            } else {
6963              !!!cp ('t398.2');
6964              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
6965              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
6966              ## mode, "in body" (not "in foreign content") secondary insertion
6967              ## mode, maybe.
6968            }
6969    
6970            !!!next-token;
6971            next B;
6972        } elsif ({        } elsif ({
6973                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
6974                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 6019  sub _tree_construction_main ($) { Line 6976  sub _tree_construction_main ($) {
6976                  thead => 1, tr => 1,                  thead => 1, tr => 1,
6977                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6978          !!!cp ('t401');          !!!cp ('t401');
6979          !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'in body',
6980                            text => $token->{tag_name}, token => $token);
6981          ## Ignore the token          ## Ignore the token
6982            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
6983          !!!next-token;          !!!next-token;
6984          redo B;          next B;
6985                    
6986          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
6987        } else {        } else {
# Line 6044  sub _tree_construction_main ($) { Line 7003  sub _tree_construction_main ($) {
7003              }->{$token->{tag_name}}) {              }->{$token->{tag_name}}) {
7004            !!!cp ('t380');            !!!cp ('t380');
7005            push @$active_formatting_elements, ['#marker', ''];            push @$active_formatting_elements, ['#marker', ''];
7006              !!!nack ('t380.1');
7007          } elsif ({          } elsif ({
7008                    b => 1, big => 1, em => 1, font => 1, i => 1,                    b => 1, big => 1, em => 1, font => 1, i => 1,
7009                    s => 1, small => 1, strile => 1,                    s => 1, small => 1, strile => 1,
# Line 6051  sub _tree_construction_main ($) { Line 7011  sub _tree_construction_main ($) {
7011                   }->{$token->{tag_name}}) {                   }->{$token->{tag_name}}) {
7012            !!!cp ('t375');            !!!cp ('t375');
7013            push @$active_formatting_elements, $self->{open_elements}->[-1];            push @$active_formatting_elements, $self->{open_elements}->[-1];
7014              !!!nack ('t375.1');
7015          } elsif ($token->{tag_name} eq 'input') {          } elsif ($token->{tag_name} eq 'input') {
7016            !!!cp ('t388');            !!!cp ('t388');
7017            ## TODO: associate with $self->{form_element} if defined            ## TODO: associate with $self->{form_element} if defined
7018            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
7019              !!!ack ('t388.2');
7020          } elsif ({          } elsif ({
7021                    area => 1, basefont => 1, bgsound => 1, br => 1,                    area => 1, basefont => 1, bgsound => 1, br => 1,
7022                    embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                    embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
# Line 6062  sub _tree_construction_main ($) { Line 7024  sub _tree_construction_main ($) {
7024                   }->{$token->{tag_name}}) {                   }->{$token->{tag_name}}) {
7025            !!!cp ('t388.1');            !!!cp ('t388.1');
7026            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
7027              !!!ack ('t388.3');
7028          } elsif ($token->{tag_name} eq 'select') {          } elsif ($token->{tag_name} eq 'select') {
7029            ## TODO: associate with $self->{form_element} if defined            ## TODO: associate with $self->{form_element} if defined
7030                    
# Line 6074  sub _tree_construction_main ($) { Line 7037  sub _tree_construction_main ($) {
7037              !!!cp ('t400.2');              !!!cp ('t400.2');
7038              $self->{insertion_mode} = IN_SELECT_IM;              $self->{insertion_mode} = IN_SELECT_IM;
7039            }            }
7040              !!!nack ('t400.3');
7041          } else {          } else {
7042            !!!cp ('t402');            !!!nack ('t402');
7043          }          }
7044                    
7045          !!!next-token;          !!!next-token;
7046          redo B;          next B;
7047        }        }
7048      } elsif ($token->{type} == END_TAG_TOKEN) {      } elsif ($token->{type} == END_TAG_TOKEN) {
7049        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
# Line 6087  sub _tree_construction_main ($) { Line 7051  sub _tree_construction_main ($) {
7051          my $i;          my $i;
7052          INSCOPE: {          INSCOPE: {
7053            for (reverse @{$self->{open_elements}}) {            for (reverse @{$self->{open_elements}}) {
7054              if ($_->[1] eq 'body') {              if ($_->[1] & BODY_EL) {
7055                !!!cp ('t405');                !!!cp ('t405');
7056                $i = $_;                $i = $_;
7057                last INSCOPE;                last INSCOPE;
7058              } elsif ({              } elsif ($_->[1] & SCOPING_EL) {
                       applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                       button => 1, marquee => 1, object => 1, html => 1,  
                      }->{$_->[1]}) {  
7059                !!!cp ('t405.1');                !!!cp ('t405.1');
7060                last;                last;
7061              }              }
7062            }            }
7063    
7064            !!!parse-error (type => 'start tag not allowed',            !!!parse-error (type => 'start tag not allowed',
7065                            value => $token->{tag_name}, token => $token);                            text => $token->{tag_name}, token => $token);
7066            ## NOTE: Ignore the token.            ## NOTE: Ignore the token.
7067            !!!next-token;            !!!next-token;
7068            redo B;            next B;
7069          } # INSCOPE          } # INSCOPE
7070    
7071          for (@{$self->{open_elements}}) {          for (@{$self->{open_elements}}) {
7072            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]}) {  
7073              !!!cp ('t403');              !!!cp ('t403');
7074              !!!parse-error (type => 'not closed:'.$_->[1], token => $token);              !!!parse-error (type => 'not closed',
7075                                text => $_->[0]->manakai_local_name,
7076                                token => $token);
7077              last;              last;
7078            } else {            } else {
7079              !!!cp ('t404');              !!!cp ('t404');
# Line 6123  sub _tree_construction_main ($) { Line 7082  sub _tree_construction_main ($) {
7082    
7083          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
7084          !!!next-token;          !!!next-token;
7085          redo B;          next B;
7086        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
7087          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
7088            ## up-to-date, though it has same effect as speced.
7089            if (@{$self->{open_elements}} > 1 and
7090                $self->{open_elements}->[1]->[1] & BODY_EL) {
7091            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
7092            if ($self->{open_elements}->[-1]->[1] ne 'body') {            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
7093              !!!cp ('t406');              !!!cp ('t406');
7094              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1], token => $token);              !!!parse-error (type => 'not closed',
7095                                text => $self->{open_elements}->[1]->[0]
7096                                    ->manakai_local_name,
7097                                token => $token);
7098            } else {            } else {
7099              !!!cp ('t407');              !!!cp ('t407');
7100            }            }
7101            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
7102            ## reprocess            ## reprocess
7103            redo B;            next B;
7104          } else {          } else {
7105            !!!cp ('t408');            !!!cp ('t408');
7106            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
7107                              text => $token->{tag_name}, token => $token);
7108            ## Ignore the token            ## Ignore the token
7109            !!!next-token;            !!!next-token;
7110            redo B;            next B;
7111          }          }
7112        } elsif ({        } elsif ({
7113                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
# Line 6154  sub _tree_construction_main ($) { Line 7120  sub _tree_construction_main ($) {
7120          my $i;          my $i;
7121          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7122            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7123            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7124              !!!cp ('t410');              !!!cp ('t410');
7125              $i = $_;              $i = $_;
7126              last INSCOPE;              last INSCOPE;
7127            } 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]}) {  
7128              !!!cp ('t411');              !!!cp ('t411');
7129              last INSCOPE;              last INSCOPE;
7130            }            }
# Line 6169  sub _tree_construction_main ($) { Line 7132  sub _tree_construction_main ($) {
7132    
7133          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
7134            !!!cp ('t413');            !!!cp ('t413');
7135            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
7136                              text => $token->{tag_name}, token => $token);
7137              ## NOTE: Ignore the token.
7138          } else {          } else {
7139            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7140            while ({            while ({
7141                      ## END_TAG_OPTIONAL_EL
7142                    dd => ($token->{tag_name} ne 'dd'),                    dd => ($token->{tag_name} ne 'dd'),
7143                    dt => ($token->{tag_name} ne 'dt'),                    dt => ($token->{tag_name} ne 'dt'),
7144                    li => ($token->{tag_name} ne 'li'),                    li => ($token->{tag_name} ne 'li'),
7145                    p => 1,                    p => 1,
7146                   }->{$self->{open_elements}->[-1]->[1]}) {                    rt => 1,
7147                      rp => 1,
7148                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
7149              !!!cp ('t409');              !!!cp ('t409');
7150              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
7151            }            }
7152    
7153            ## Step 2.            ## Step 2.
7154            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7155                      ne $token->{tag_name}) {
7156              !!!cp ('t412');              !!!cp ('t412');
7157              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);              !!!parse-error (type => 'not closed',
7158                                text => $self->{open_elements}->[-1]->[0]
7159                                    ->manakai_local_name,
7160                                token => $token);
7161            } else {            } else {
7162              !!!cp ('t414');              !!!cp ('t414');
7163            }            }
# Line 6200  sub _tree_construction_main ($) { Line 7172  sub _tree_construction_main ($) {
7172                }->{$token->{tag_name}};                }->{$token->{tag_name}};
7173          }          }
7174          !!!next-token;          !!!next-token;
7175          redo B;          next B;
7176        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
7177          undef $self->{form_element};          undef $self->{form_element};
7178    
# Line 6208  sub _tree_construction_main ($) { Line 7180  sub _tree_construction_main ($) {
7180          my $i;          my $i;
7181          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7182            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7183            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & FORM_EL) {
7184              !!!cp ('t418');              !!!cp ('t418');
7185              $i = $_;              $i = $_;
7186              last INSCOPE;              last INSCOPE;
7187            } 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]}) {  
7188              !!!cp ('t419');              !!!cp ('t419');
7189              last INSCOPE;              last INSCOPE;
7190            }            }
# Line 6223  sub _tree_construction_main ($) { Line 7192  sub _tree_construction_main ($) {
7192    
7193          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
7194            !!!cp ('t421');            !!!cp ('t421');
7195            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
7196                              text => $token->{tag_name}, token => $token);
7197              ## NOTE: Ignore the token.
7198          } else {          } else {
7199            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7200            while ({            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                   dd => 1, dt => 1, li => 1, p => 1,  
                  }->{$self->{open_elements}->[-1]->[1]}) {  
7201              !!!cp ('t417');              !!!cp ('t417');
7202              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
7203            }            }
7204                        
7205            ## Step 2.            ## Step 2.
7206            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7207                      ne $token->{tag_name}) {
7208              !!!cp ('t417.1');              !!!cp ('t417.1');
7209              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);              !!!parse-error (type => 'not closed',
7210                                text => $self->{open_elements}->[-1]->[0]
7211                                    ->manakai_local_name,
7212                                token => $token);
7213            } else {            } else {
7214              !!!cp ('t420');              !!!cp ('t420');
7215            }              }  
# Line 6246  sub _tree_construction_main ($) { Line 7219  sub _tree_construction_main ($) {
7219          }          }
7220    
7221          !!!next-token;          !!!next-token;
7222          redo B;          next B;
7223        } elsif ({        } elsif ({
7224                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
7225                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 6254  sub _tree_construction_main ($) { Line 7227  sub _tree_construction_main ($) {
7227          my $i;          my $i;
7228          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7229            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7230            if ({            if ($node->[1] & HEADING_EL) {
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
7231              !!!cp ('t423');              !!!cp ('t423');
7232              $i = $_;              $i = $_;
7233              last INSCOPE;              last INSCOPE;
7234            } 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]}) {  
7235              !!!cp ('t424');              !!!cp ('t424');
7236              last INSCOPE;              last INSCOPE;
7237            }            }
# Line 6271  sub _tree_construction_main ($) { Line 7239  sub _tree_construction_main ($) {
7239    
7240          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
7241            !!!cp ('t425.1');            !!!cp ('t425.1');
7242            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
7243                              text => $token->{tag_name}, token => $token);
7244              ## NOTE: Ignore the token.
7245          } else {          } else {
7246            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7247            while ({            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                   dd => 1, dt => 1, li => 1, p => 1,  
                  }->{$self->{open_elements}->[-1]->[1]}) {  
7248              !!!cp ('t422');              !!!cp ('t422');
7249              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
7250            }            }
7251                        
7252            ## Step 2.            ## Step 2.
7253            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7254                      ne $token->{tag_name}) {
7255              !!!cp ('t425');              !!!cp ('t425');
7256              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag',
7257                                text => $token->{tag_name}, token => $token);
7258            } else {            } else {
7259              !!!cp ('t426');              !!!cp ('t426');
7260            }            }
# Line 6294  sub _tree_construction_main ($) { Line 7264  sub _tree_construction_main ($) {
7264          }          }
7265                    
7266          !!!next-token;          !!!next-token;
7267          redo B;          next B;
7268        } elsif ($token->{tag_name} eq 'p') {        } elsif ($token->{tag_name} eq 'p') {
7269          ## has an element in scope          ## has an element in scope
7270          my $i;          my $i;
7271          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7272            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7273            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & P_EL) {
7274              !!!cp ('t410.1');              !!!cp ('t410.1');
7275              $i = $_;              $i = $_;
7276              last INSCOPE;              last INSCOPE;
7277            } 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]}) {  
7278              !!!cp ('t411.1');              !!!cp ('t411.1');
7279              last INSCOPE;              last INSCOPE;
7280            }            }
7281          } # INSCOPE          } # INSCOPE
7282    
7283          if (defined $i) {          if (defined $i) {
7284            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7285                      ne $token->{tag_name}) {
7286              !!!cp ('t412.1');              !!!cp ('t412.1');
7287              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);              !!!parse-error (type => 'not closed',
7288                                text => $self->{open_elements}->[-1]->[0]
7289                                    ->manakai_local_name,
7290                                token => $token);
7291            } else {            } else {
7292              !!!cp ('t414.1');              !!!cp ('t414.1');
7293            }            }
# Line 6324  sub _tree_construction_main ($) { Line 7295  sub _tree_construction_main ($) {
7295            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
7296          } else {          } else {
7297            !!!cp ('t413.1');            !!!cp ('t413.1');
7298            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
7299                              text => $token->{tag_name}, token => $token);
7300    
7301            !!!cp ('t415.1');            !!!cp ('t415.1');
7302            ## As if <p>, then reprocess the current token            ## As if <p>, then reprocess the current token
7303            my $el;            my $el;
7304            !!!create-element ($el, 'p',, $token);            !!!create-element ($el, $HTML_NS, 'p',, $token);
7305            $insert->($el);            $insert->($el);
7306            ## NOTE: Not inserted into |$self->{open_elements}|.            ## NOTE: Not inserted into |$self->{open_elements}|.
7307          }          }
7308    
7309          !!!next-token;          !!!next-token;
7310          redo B;          next B;
7311        } elsif ({        } elsif ({
7312                  a => 1,                  a => 1,
7313                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
# Line 6344  sub _tree_construction_main ($) { Line 7316  sub _tree_construction_main ($) {
7316                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7317          !!!cp ('t427');          !!!cp ('t427');
7318          $formatting_end_tag->($token);          $formatting_end_tag->($token);
7319          redo B;          next B;
7320        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
7321          !!!cp ('t428');          !!!cp ('t428');
7322          !!!parse-error (type => 'unmatched end tag:br', token => $token);          !!!parse-error (type => 'unmatched end tag',
7323                            text => 'br', token => $token);
7324    
7325          ## As if <br>          ## As if <br>
7326          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7327                    
7328          my $el;          my $el;
7329          !!!create-element ($el, 'br',, $token);          !!!create-element ($el, $HTML_NS, 'br',, $token);
7330          $insert->($el);          $insert->($el);
7331                    
7332          ## Ignore the token.          ## Ignore the token.
7333          !!!next-token;          !!!next-token;
7334          redo B;          next B;
7335        } elsif ({        } elsif ({
7336                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7337                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 6372  sub _tree_construction_main ($) { Line 7345  sub _tree_construction_main ($) {
7345                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
7346                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7347          !!!cp ('t429');          !!!cp ('t429');
7348          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'unmatched end tag',
7349                            text => $token->{tag_name}, token => $token);
7350          ## Ignore the token          ## Ignore the token
7351          !!!next-token;          !!!next-token;
7352          redo B;          next B;
7353                    
7354          ## ISSUE: Issue on HTML5 new elements in spec          ## ISSUE: Issue on HTML5 new elements in spec
7355                    
# Line 6386  sub _tree_construction_main ($) { Line 7360  sub _tree_construction_main ($) {
7360    
7361          ## Step 2          ## Step 2
7362          S2: {          S2: {
7363            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7364              ## Step 1              ## Step 1
7365              ## generate implied end tags              ## generate implied end tags
7366              while ({              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                     dd => 1, dt => 1, li => 1, p => 1,  
                    }->{$self->{open_elements}->[-1]->[1]}) {  
7367                !!!cp ('t430');                !!!cp ('t430');
7368                ## ISSUE: Can this case be reached?                ## NOTE: |<ruby><rt></ruby>|.
7369                  ## ISSUE: <ruby><rt></rt> will also take this code path,
7370                  ## which seems wrong.
7371                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
7372                  $node_i++;
7373              }              }
7374                    
7375              ## Step 2              ## Step 2
7376              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7377                        ne $token->{tag_name}) {
7378                !!!cp ('t431');                !!!cp ('t431');
7379                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
7380                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                !!!parse-error (type => 'not closed',
7381                                  text => $self->{open_elements}->[-1]->[0]
7382                                      ->manakai_local_name,
7383                                  token => $token);
7384              } else {              } else {
7385                !!!cp ('t432');                !!!cp ('t432');
7386              }              }
7387                            
7388              ## Step 3              ## Step 3
7389              splice @{$self->{open_elements}}, $node_i;              splice @{$self->{open_elements}}, $node_i if $node_i < 0;
7390    
7391              !!!next-token;              !!!next-token;
7392              last S2;              last S2;
7393            } else {            } else {
7394              ## Step 3              ## Step 3
7395              if (not $formatting_category->{$node->[1]} and              if (not ($node->[1] & FORMATTING_EL) and
7396                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
7397                  ($special_category->{$node->[1]} or                  ($node->[1] & SPECIAL_EL or
7398                   $scoping_category->{$node->[1]})) {                   $node->[1] & SCOPING_EL)) {
7399                !!!cp ('t433');                !!!cp ('t433');
7400                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag',
7401                                  text => $token->{tag_name}, token => $token);
7402                ## Ignore the token                ## Ignore the token
7403                !!!next-token;                !!!next-token;
7404                last S2;                last S2;
# Line 6434  sub _tree_construction_main ($) { Line 7414  sub _tree_construction_main ($) {
7414            ## Step 5;            ## Step 5;
7415            redo S2;            redo S2;
7416          } # S2          } # S2
7417          redo B;          next B;
7418        }        }
7419      }      }
7420      redo B;      next B;
7421      } continue { # B
7422        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7423          ## NOTE: The code below is executed in cases where it does not have
7424          ## to be, but it it is harmless even in those cases.
7425          ## has an element in scope
7426          INSCOPE: {
7427            for (reverse 0..$#{$self->{open_elements}}) {
7428              my $node = $self->{open_elements}->[$_];
7429              if ($node->[1] & FOREIGN_EL) {
7430                last INSCOPE;
7431              } elsif ($node->[1] & SCOPING_EL) {
7432                last;
7433              }
7434            }
7435            
7436            ## NOTE: No foreign element in scope.
7437            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7438          } # INSCOPE
7439        }
7440    } # B    } # B
7441    
7442    ## Stop parsing # MUST    ## Stop parsing # MUST
# Line 6483  sub set_inner_html ($$$) { Line 7482  sub set_inner_html ($$$) {
7482    
7483      ## Step 8 # MUST      ## Step 8 # MUST
7484      my $i = 0;      my $i = 0;
7485      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7486      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7487      $p->{set_next_char} = sub {      $p->{set_next_char} = sub {
7488        my $self = shift;        my $self = shift;
7489    
# Line 6493  sub set_inner_html ($$$) { Line 7492  sub set_inner_html ($$$) {
7492    
7493        $self->{next_char} = -1 and return if $i >= length $$s;        $self->{next_char} = -1 and return if $i >= length $$s;
7494        $self->{next_char} = ord substr $$s, $i++, 1;        $self->{next_char} = ord substr $$s, $i++, 1;
7495        $column++;  
7496          ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7497          $p->{column}++;
7498    
7499        if ($self->{next_char} == 0x000A) { # LF        if ($self->{next_char} == 0x000A) { # LF
7500          $line++;          $p->{line}++;
7501          $column = 0;          $p->{column} = 0;
7502          !!!cp ('i1');          !!!cp ('i1');
7503        } elsif ($self->{next_char} == 0x000D) { # CR        } elsif ($self->{next_char} == 0x000D) { # CR
7504          $i++ if substr ($$s, $i, 1) eq "\x0A";          $i++ if substr ($$s, $i, 1) eq "\x0A";
7505          $self->{next_char} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
7506          $line++;          $p->{line}++;
7507          $column = 0;          $p->{column} = 0;
7508          !!!cp ('i2');          !!!cp ('i2');
7509        } elsif ($self->{next_char} > 0x10FFFF) {        } elsif ($self->{next_char} > 0x10FFFF) {
7510          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
# Line 6512  sub set_inner_html ($$$) { Line 7513  sub set_inner_html ($$$) {
7513          !!!cp ('i4');          !!!cp ('i4');
7514          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
7515          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7516          } elsif ($self->{next_char} <= 0x0008 or
7517                   (0x000E <= $self->{next_char} and
7518                    $self->{next_char} <= 0x001F) or
7519                   (0x007F <= $self->{next_char} and
7520                    $self->{next_char} <= 0x009F) or
7521                   (0xD800 <= $self->{next_char} and
7522                    $self->{next_char} <= 0xDFFF) or
7523                   (0xFDD0 <= $self->{next_char} and
7524                    $self->{next_char} <= 0xFDDF) or
7525                   {
7526                    0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
7527                    0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
7528                    0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
7529                    0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
7530                    0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
7531                    0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
7532                    0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
7533                    0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
7534                    0x10FFFE => 1, 0x10FFFF => 1,
7535                   }->{$self->{next_char}}) {
7536            !!!cp ('i4.1');
7537            if ($self->{next_char} < 0x10000) {
7538              !!!parse-error (type => 'control char',
7539                              text => (sprintf 'U+%04X', $self->{next_char}));
7540            } else {
7541              !!!parse-error (type => 'control char',
7542                              text => (sprintf 'U-%08X', $self->{next_char}));
7543            }
7544        }        }
7545      };      };
7546      $p->{prev_char} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];
# Line 6519  sub set_inner_html ($$$) { Line 7548  sub set_inner_html ($$$) {
7548            
7549      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7550        my (%opt) = @_;        my (%opt) = @_;
7551        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7552          my $column = $opt{column};
7553          if (defined $opt{token} and defined $opt{token}->{line}) {
7554            $line = $opt{token}->{line};
7555            $column = $opt{token}->{column};
7556          }
7557          warn "Parse error ($opt{type}) at line $line column $column\n";
7558      };      };
7559      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7560        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7561      };      };
7562            
7563      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
# Line 6546  sub set_inner_html ($$$) { Line 7581  sub set_inner_html ($$$) {
7581          unless defined $p->{content_model};          unless defined $p->{content_model};
7582          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
7583    
7584      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7585          ## TODO: Foreign element OK?
7586    
7587      ## Step 3      ## Step 3
7588      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
# Line 6556  sub set_inner_html ($$$) { Line 7592  sub set_inner_html ($$$) {
7592      $doc->append_child ($root);      $doc->append_child ($root);
7593    
7594      ## Step 5 # MUST      ## Step 5 # MUST
7595      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7596    
7597      undef $p->{head_element};      undef $p->{head_element};
7598    
# Line 6602  sub set_inner_html ($$$) { Line 7638  sub set_inner_html ($$$) {
7638      ## ISSUE: mutation events?      ## ISSUE: mutation events?
7639    
7640      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
7641    
7642        delete $p->{parse_error}; # delete loop
7643    } else {    } else {
7644      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";
7645    }    }

Legend:
Removed from v.1.120  
changed lines
  Added in v.1.159

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24