/[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.118 by wakaba, Thu Mar 20 01:34:00 2008 UTC revision 1.139 by wakaba, Sat May 24 04:26:27 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    
 ## TODO: Control charcters and noncharacters are not allowed (HTML5 revision 1263)  
11  ## TODO: 1252 parse error (revision 1264)  ## TODO: 1252 parse error (revision 1264)
12  ## TODO: 8859-11 = 874 (revision 1271)  ## TODO: 8859-11 = 874 (revision 1271)
13    
14  my $permitted_slash_tag_name = {  require IO::Handle;
15    base => 1,  
16    link => 1,  my $HTML_NS = q<http://www.w3.org/1999/xhtml>;
17    meta => 1,  my $MML_NS = q<http://www.w3.org/1998/Math/MathML>;
18    hr => 1,  my $SVG_NS = q<http://www.w3.org/2000/svg>;
19    br => 1,  my $XLINK_NS = q<http://www.w3.org/1999/xlink>;
20    img => 1,  my $XML_NS = q<http://www.w3.org/XML/1998/namespace>;
21    embed => 1,  my $XMLNS_NS = q<http://www.w3.org/2000/xmlns/>;
22    param => 1,  
23    area => 1,  sub A_EL () { 0b1 }
24    col => 1,  sub ADDRESS_EL () { 0b10 }
25    input => 1,  sub BODY_EL () { 0b100 }
26    sub BUTTON_EL () { 0b1000 }
27    sub CAPTION_EL () { 0b10000 }
28    sub DD_EL () { 0b100000 }
29    sub DIV_EL () { 0b1000000 }
30    sub DT_EL () { 0b10000000 }
31    sub FORM_EL () { 0b100000000 }
32    sub FORMATTING_EL () { 0b1000000000 }
33    sub FRAMESET_EL () { 0b10000000000 }
34    sub HEADING_EL () { 0b100000000000 }
35    sub HTML_EL () { 0b1000000000000 }
36    sub LI_EL () { 0b10000000000000 }
37    sub NOBR_EL () { 0b100000000000000 }
38    sub OPTION_EL () { 0b1000000000000000 }
39    sub OPTGROUP_EL () { 0b10000000000000000 }
40    sub P_EL () { 0b100000000000000000 }
41    sub SELECT_EL () { 0b1000000000000000000 }
42    sub TABLE_EL () { 0b10000000000000000000 }
43    sub TABLE_CELL_EL () { 0b100000000000000000000 }
44    sub TABLE_ROW_EL () { 0b1000000000000000000000 }
45    sub TABLE_ROW_GROUP_EL () { 0b10000000000000000000000 }
46    sub MISC_SCOPING_EL () { 0b100000000000000000000000 }
47    sub MISC_SPECIAL_EL () { 0b1000000000000000000000000 }
48    sub FOREIGN_EL () { 0b10000000000000000000000000 }
49    sub FOREIGN_FLOW_CONTENT_EL () { 0b100000000000000000000000000 }
50    sub MML_AXML_EL () { 0b1000000000000000000000000000 }
51    
52    sub TABLE_ROWS_EL () {
53      TABLE_EL |
54      TABLE_ROW_EL |
55      TABLE_ROW_GROUP_EL
56    }
57    
58    sub END_TAG_OPTIONAL_EL () {
59      DD_EL |
60      DT_EL |
61      LI_EL |
62      P_EL
63    }
64    
65    sub ALL_END_TAG_OPTIONAL_EL () {
66      END_TAG_OPTIONAL_EL |
67      BODY_EL |
68      HTML_EL |
69      TABLE_CELL_EL |
70      TABLE_ROW_EL |
71      TABLE_ROW_GROUP_EL
72    }
73    
74    sub SCOPING_EL () {
75      BUTTON_EL |
76      CAPTION_EL |
77      HTML_EL |
78      TABLE_EL |
79      TABLE_CELL_EL |
80      MISC_SCOPING_EL
81    }
82    
83    sub TABLE_SCOPING_EL () {
84      HTML_EL |
85      TABLE_EL
86    }
87    
88    sub TABLE_ROWS_SCOPING_EL () {
89      HTML_EL |
90      TABLE_ROW_GROUP_EL
91    }
92    
93    sub TABLE_ROW_SCOPING_EL () {
94      HTML_EL |
95      TABLE_ROW_EL
96    }
97    
98    sub SPECIAL_EL () {
99      ADDRESS_EL |
100      BODY_EL |
101      DIV_EL |
102      END_TAG_OPTIONAL_EL |
103      FORM_EL |
104      FRAMESET_EL |
105      HEADING_EL |
106      OPTION_EL |
107      OPTGROUP_EL |
108      SELECT_EL |
109      TABLE_ROW_EL |
110      TABLE_ROW_GROUP_EL |
111      MISC_SPECIAL_EL
112    }
113    
114    my $el_category = {
115      a => A_EL | FORMATTING_EL,
116      address => ADDRESS_EL,
117      applet => MISC_SCOPING_EL,
118      area => MISC_SPECIAL_EL,
119      b => FORMATTING_EL,
120      base => MISC_SPECIAL_EL,
121      basefont => MISC_SPECIAL_EL,
122      bgsound => MISC_SPECIAL_EL,
123      big => FORMATTING_EL,
124      blockquote => MISC_SPECIAL_EL,
125      body => BODY_EL,
126      br => MISC_SPECIAL_EL,
127      button => BUTTON_EL,
128      caption => CAPTION_EL,
129      center => MISC_SPECIAL_EL,
130      col => MISC_SPECIAL_EL,
131      colgroup => MISC_SPECIAL_EL,
132      dd => DD_EL,
133      dir => MISC_SPECIAL_EL,
134      div => DIV_EL,
135      dl => MISC_SPECIAL_EL,
136      dt => DT_EL,
137      em => FORMATTING_EL,
138      embed => MISC_SPECIAL_EL,
139      fieldset => MISC_SPECIAL_EL,
140      font => FORMATTING_EL,
141      form => FORM_EL,
142      frame => MISC_SPECIAL_EL,
143      frameset => FRAMESET_EL,
144      h1 => HEADING_EL,
145      h2 => HEADING_EL,
146      h3 => HEADING_EL,
147      h4 => HEADING_EL,
148      h5 => HEADING_EL,
149      h6 => HEADING_EL,
150      head => MISC_SPECIAL_EL,
151      hr => MISC_SPECIAL_EL,
152      html => HTML_EL,
153      i => FORMATTING_EL,
154      iframe => MISC_SPECIAL_EL,
155      img => MISC_SPECIAL_EL,
156      input => MISC_SPECIAL_EL,
157      isindex => MISC_SPECIAL_EL,
158      li => LI_EL,
159      link => MISC_SPECIAL_EL,
160      listing => MISC_SPECIAL_EL,
161      marquee => MISC_SCOPING_EL,
162      menu => MISC_SPECIAL_EL,
163      meta => MISC_SPECIAL_EL,
164      nobr => NOBR_EL | FORMATTING_EL,
165      noembed => MISC_SPECIAL_EL,
166      noframes => MISC_SPECIAL_EL,
167      noscript => MISC_SPECIAL_EL,
168      object => MISC_SCOPING_EL,
169      ol => MISC_SPECIAL_EL,
170      optgroup => OPTGROUP_EL,
171      option => OPTION_EL,
172      p => P_EL,
173      param => MISC_SPECIAL_EL,
174      plaintext => MISC_SPECIAL_EL,
175      pre => MISC_SPECIAL_EL,
176      s => FORMATTING_EL,
177      script => MISC_SPECIAL_EL,
178      select => SELECT_EL,
179      small => FORMATTING_EL,
180      spacer => MISC_SPECIAL_EL,
181      strike => FORMATTING_EL,
182      strong => FORMATTING_EL,
183      style => MISC_SPECIAL_EL,
184      table => TABLE_EL,
185      tbody => TABLE_ROW_GROUP_EL,
186      td => TABLE_CELL_EL,
187      textarea => MISC_SPECIAL_EL,
188      tfoot => TABLE_ROW_GROUP_EL,
189      th => TABLE_CELL_EL,
190      thead => TABLE_ROW_GROUP_EL,
191      title => MISC_SPECIAL_EL,
192      tr => TABLE_ROW_EL,
193      tt => FORMATTING_EL,
194      u => FORMATTING_EL,
195      ul => MISC_SPECIAL_EL,
196      wbr => MISC_SPECIAL_EL,
197    };
198    
199    my $el_category_f = {
200      $MML_NS => {
201        'annotation-xml' => MML_AXML_EL,
202        mi => FOREIGN_FLOW_CONTENT_EL,
203        mo => FOREIGN_FLOW_CONTENT_EL,
204        mn => FOREIGN_FLOW_CONTENT_EL,
205        ms => FOREIGN_FLOW_CONTENT_EL,
206        mtext => FOREIGN_FLOW_CONTENT_EL,
207      },
208      $SVG_NS => {
209        foreignObject => FOREIGN_FLOW_CONTENT_EL,
210        desc => FOREIGN_FLOW_CONTENT_EL,
211        title => FOREIGN_FLOW_CONTENT_EL,
212      },
213      ## NOTE: In addition, FOREIGN_EL is set to non-HTML elements.
214    };
215    
216    my $svg_attr_name = {
217      attributetype => 'attributeType',
218      basefrequency => 'baseFrequency',
219      baseprofile => 'baseProfile',
220      calcmode => 'calcMode',
221      clippathunits => 'clipPathUnits',
222      contentscripttype => 'contentScriptType',
223      contentstyletype => 'contentStyleType',
224      diffuseconstant => 'diffuseConstant',
225      edgemode => 'edgeMode',
226      externalresourcesrequired => 'externalResourcesRequired',
227      fecolormatrix => 'feColorMatrix',
228      fecomposite => 'feComposite',
229      fegaussianblur => 'feGaussianBlur',
230      femorphology => 'feMorphology',
231      fetile => 'feTile',
232      filterres => 'filterRes',
233      filterunits => 'filterUnits',
234      glyphref => 'glyphRef',
235      gradienttransform => 'gradientTransform',
236      gradientunits => 'gradientUnits',
237      kernelmatrix => 'kernelMatrix',
238      kernelunitlength => 'kernelUnitLength',
239      keypoints => 'keyPoints',
240      keysplines => 'keySplines',
241      keytimes => 'keyTimes',
242      lengthadjust => 'lengthAdjust',
243      limitingconeangle => 'limitingConeAngle',
244      markerheight => 'markerHeight',
245      markerunits => 'markerUnits',
246      markerwidth => 'markerWidth',
247      maskcontentunits => 'maskContentUnits',
248      maskunits => 'maskUnits',
249      numoctaves => 'numOctaves',
250      pathlength => 'pathLength',
251      patterncontentunits => 'patternContentUnits',
252      patterntransform => 'patternTransform',
253      patternunits => 'patternUnits',
254      pointsatx => 'pointsAtX',
255      pointsaty => 'pointsAtY',
256      pointsatz => 'pointsAtZ',
257      preservealpha => 'preserveAlpha',
258      preserveaspectratio => 'preserveAspectRatio',
259      primitiveunits => 'primitiveUnits',
260      refx => 'refX',
261      refy => 'refY',
262      repeatcount => 'repeatCount',
263      repeatdur => 'repeatDur',
264      requiredextensions => 'requiredExtensions',
265      specularconstant => 'specularConstant',
266      specularexponent => 'specularExponent',
267      spreadmethod => 'spreadMethod',
268      startoffset => 'startOffset',
269      stddeviation => 'stdDeviation',
270      stitchtiles => 'stitchTiles',
271      surfacescale => 'surfaceScale',
272      systemlanguage => 'systemLanguage',
273      tablevalues => 'tableValues',
274      targetx => 'targetX',
275      targety => 'targetY',
276      textlength => 'textLength',
277      viewbox => 'viewBox',
278      viewtarget => 'viewTarget',
279      xchannelselector => 'xChannelSelector',
280      ychannelselector => 'yChannelSelector',
281      zoomandpan => 'zoomAndPan',
282  };  };
283    
284    my $foreign_attr_xname = {
285      'xlink:actuate' => [$XLINK_NS, ['xlink', 'actuate']],
286      'xlink:arcrole' => [$XLINK_NS, ['xlink', 'arcrole']],
287      'xlink:href' => [$XLINK_NS, ['xlink', 'href']],
288      'xlink:role' => [$XLINK_NS, ['xlink', 'role']],
289      'xlink:show' => [$XLINK_NS, ['xlink', 'show']],
290      'xlink:title' => [$XLINK_NS, ['xlink', 'title']],
291      'xlink:type' => [$XLINK_NS, ['xlink', 'type']],
292      'xml:base' => [$XML_NS, ['xml', 'base']],
293      'xml:lang' => [$XML_NS, ['xml', 'lang']],
294      'xml:space' => [$XML_NS, ['xml', 'space']],
295      'xmlns' => [$XMLNS_NS, [undef, 'xmlns']],
296      'xmlns:xlink' => [$XMLNS_NS, ['xmlns', 'xlink']],
297    };
298    
299    ## ISSUE: xmlns:xlink="non-xlink-ns" is not an error.
300    
301  my $c1_entity_char = {  my $c1_entity_char = {
302    0x80 => 0x20AC,    0x80 => 0x20AC,
303    0x81 => 0xFFFD,    0x81 => 0xFFFD,
# Line 61  my $c1_entity_char = { Line 333  my $c1_entity_char = {
333    0x9F => 0x0178,    0x9F => 0x0178,
334  }; # $c1_entity_char  }; # $c1_entity_char
335    
 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  
   
336  sub parse_byte_string ($$$$;$) {  sub parse_byte_string ($$$$;$) {
337      my $self = shift;
338      my $charset_name = shift;
339      open my $input, '<', ref $_[0] ? $_[0] : \($_[0]);
340      return $self->parse_byte_stream ($charset_name, $input, @_[1..$#_]);
341    } # parse_byte_string
342    
343    sub parse_byte_stream ($$$$;$) {
344    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
345    my $charset = shift;    my $charset_name = shift;
346    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;  
   }  
347    
348    $self->{change_encoding} = sub {    my $onerror = $_[2] || sub {
349      my $self = shift;      my (%opt) = @_;
350      my $charset = lc shift;      warn "Parse error ($opt{type})\n";
351      my $token = shift;    };
352      ## TODO: if $charset is supported    $self->{parse_error} = $onerror; # updated later by parse_char_string
     ## TODO: normalize charset name  
353    
354      ## "Change the encoding" algorithm:    ## HTML5 encoding sniffing algorithm
355      require Message::Charset::Info;
356      my $charset;
357      my $buffer;
358      my ($char_stream, $e_status);
359    
360      ## Step 1        SNIFFING: {
361      if ($charset eq 'utf-16') { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?  
362        $charset = 'utf-8';      ## Step 1
363        if (defined $charset_name) {
364          $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
365    
366          ## ISSUE: Unsupported encoding is not ignored according to the spec.
367          ($char_stream, $e_status) = $charset->get_decode_handle
368              ($byte_stream, allow_error_reporting => 1,
369               allow_fallback => 1);
370          if ($char_stream) {
371            $self->{confident} = 1;
372            last SNIFFING;
373          } else {
374            ## TODO: unsupported error
375          }
376      }      }
377    
378      ## Step 2      ## Step 2
379      if (defined $self->{input_encoding} and      my $byte_buffer = '';
380          $self->{input_encoding} eq $charset) {      for (1..1024) {
381          my $char = $byte_stream->getc;
382          last unless defined $char;
383          $byte_buffer .= $char;
384        } ## TODO: timeout
385    
386        ## Step 3
387        if ($byte_buffer =~ /^\xFE\xFF/) {
388          $charset = Message::Charset::Info->get_by_iana_name ('utf-16be');
389          ($char_stream, $e_status) = $charset->get_decode_handle
390              ($byte_stream, allow_error_reporting => 1,
391               allow_fallback => 1, byte_buffer => \$byte_buffer);
392        $self->{confident} = 1;        $self->{confident} = 1;
393        return;        last SNIFFING;
394        } elsif ($byte_buffer =~ /^\xFF\xFE/) {
395          $charset = Message::Charset::Info->get_by_iana_name ('utf-16le');
396          ($char_stream, $e_status) = $charset->get_decode_handle
397              ($byte_stream, allow_error_reporting => 1,
398               allow_fallback => 1, byte_buffer => \$byte_buffer);
399          $self->{confident} = 1;
400          last SNIFFING;
401        } elsif ($byte_buffer =~ /^\xEF\xBB\xBF/) {
402          $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
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;
407          last SNIFFING;
408      }      }
409    
410      !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.      ## Step 4
411          ':'.$charset, level => 'w', token => $token);      ## TODO: <meta charset>
412    
413      ## Step 3      ## Step 5
414      # if (can) {      ## TODO: from history
       ## change the encoding on the fly.  
       #$self->{confident} = 1;  
       #return;  
     # }  
415    
416      ## Step 4      ## Step 6
417      throw Whatpm::HTML::RestartParser (charset => $charset);      require Whatpm::Charset::UniversalCharDet;
418        $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
419            ($byte_buffer);
420        if (defined $charset_name) {
421          $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
422    
423          ## ISSUE: Unsupported encoding is not ignored according to the spec.
424          require Whatpm::Charset::DecodeHandle;
425          $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
426              ($byte_stream);
427          ($char_stream, $e_status) = $charset->get_decode_handle
428              ($buffer, allow_error_reporting => 1,
429               allow_fallback => 1, byte_buffer => \$byte_buffer);
430          if ($char_stream) {
431            $buffer->{buffer} = $byte_buffer;
432            !!!parse-error (type => 'sniffing:chardet', ## TODO: type name
433                            value => $charset_name,
434                            level => $self->{info_level},
435                            line => 1, column => 1);
436            $self->{confident} = 0;
437            last SNIFFING;
438          }
439        }
440    
441        ## Step 7: default
442        ## TODO: Make this configurable.
443        $charset = Message::Charset::Info->get_by_iana_name ('windows-1252');
444            ## NOTE: We choose |windows-1252| here, since |utf-8| should be
445            ## detectable in the step 6.
446        require Whatpm::Charset::DecodeHandle;
447        $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
448            ($byte_stream);
449        ($char_stream, $e_status)
450            = $charset->get_decode_handle ($buffer,
451                                           allow_error_reporting => 1,
452                                           allow_fallback => 1,
453                                           byte_buffer => \$byte_buffer);
454        $buffer->{buffer} = $byte_buffer;
455        !!!parse-error (type => 'sniffing:default', ## TODO: type name
456                        value => 'windows-1252',
457                        level => $self->{info_level},
458                        line => 1, column => 1);
459        $self->{confident} = 0;
460      } # SNIFFING
461    
462      $self->{input_encoding} = $charset->get_iana_name;
463      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
464        !!!parse-error (type => 'chardecode:fallback', ## TODO: type name
465                        value => $self->{input_encoding},
466                        level => $self->{unsupported_level},
467                        line => 1, column => 1);
468      } elsif (not ($e_status &
469                    Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
470        !!!parse-error (type => 'chardecode:no error', ## TODO: type name
471                        value => $self->{input_encoding},
472                        level => $self->{unsupported_level},
473                        line => 1, column => 1);
474      }
475    
476      $self->{change_encoding} = sub {
477        my $self = shift;
478        $charset_name = shift;
479        my $token = shift;
480    
481        $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
482        ($char_stream, $e_status) = $charset->get_decode_handle
483            ($byte_stream, allow_error_reporting => 1, allow_fallback => 1,
484             byte_buffer => \ $buffer->{buffer});
485        
486        if ($char_stream) { # if supported
487          ## "Change the encoding" algorithm:
488    
489          ## Step 1    
490          if ($charset->{iana_names}->{'utf-16'}) { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?
491            $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
492            ($char_stream, $e_status) = $charset->get_decode_handle
493                ($byte_stream,
494                 byte_buffer => \ $buffer->{buffer});
495          }
496          $charset_name = $charset->get_iana_name;
497          
498          ## Step 2
499          if (defined $self->{input_encoding} and
500              $self->{input_encoding} eq $charset_name) {
501            !!!parse-error (type => 'charset label:matching', ## TODO: type
502                            value => $charset_name,
503                            level => $self->{info_level});
504            $self->{confident} = 1;
505            return;
506          }
507    
508          !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.
509              ':'.$charset_name, level => 'w', token => $token);
510          
511          ## Step 3
512          # if (can) {
513            ## change the encoding on the fly.
514            #$self->{confident} = 1;
515            #return;
516          # }
517          
518          ## Step 4
519          throw Whatpm::HTML::RestartParser ();
520        }
521    }; # $self->{change_encoding}    }; # $self->{change_encoding}
522    
523      my $char_onerror = sub {
524        my (undef, $type, %opt) = @_;
525        !!!parse-error (%opt, type => $type,
526                        line => $self->{line}, column => $self->{column} + 1);
527        if ($opt{octets}) {
528          ${$opt{octets}} = "\x{FFFD}"; # relacement character
529        }
530      };
531      $char_stream->onerror ($char_onerror);
532    
533    my @args = @_; shift @args; # $s    my @args = @_; shift @args; # $s
534    my $return;    my $return;
535    try {    try {
536      $return = $self->parse_char_string ($s, @args);        $return = $self->parse_char_stream ($char_stream, @args);  
537    } catch Whatpm::HTML::RestartParser with {    } catch Whatpm::HTML::RestartParser with {
538      my $charset = shift->{charset};      ## NOTE: Invoked after {change_encoding}.
539      $s = \ (Encode::decode ($charset, $$bytes_s));      
540      $self->{input_encoding} = $charset; ## TODO: normalize      $self->{input_encoding} = $charset->get_iana_name;
541        if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
542          !!!parse-error (type => 'chardecode:fallback', ## TODO: type name
543                          value => $self->{input_encoding},
544                          level => $self->{unsupported_level},
545                          line => 1, column => 1);
546        } elsif (not ($e_status &
547                      Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
548          !!!parse-error (type => 'chardecode:no error', ## TODO: type name
549                          value => $self->{input_encoding},
550                          level => $self->{unsupported_level},
551                          line => 1, column => 1);
552        }
553      $self->{confident} = 1;      $self->{confident} = 1;
554      $return = $self->parse_char_string ($s, @args);      $char_stream->onerror ($char_onerror);
555        $return = $self->parse_char_stream ($char_stream, @args);
556    };    };
557    return $return;    return $return;
558  } # parse_byte_string  } # parse_byte_stream
559    
560  ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM  ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
561  ## 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 566  sub parse_byte_string ($$$$;$) {
566  ## 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
567  ## strip the BOM and never strip any ZWNBSP.  ## strip the BOM and never strip any ZWNBSP.
568    
569  *parse_char_string = \&parse_string;  sub parse_char_string ($$$;$) {
570      my $self = shift;
571      require utf8;
572      my $s = ref $_[0] ? $_[0] : \($_[0]);
573      open my $input, '<' . (utf8::is_utf8 ($$s) ? ':utf8' : ''), $s;
574      return $self->parse_char_stream ($input, @_[1..$#_]);
575    } # parse_char_string
576    *parse_string = \&parse_char_string;
577    
578  sub parse_string ($$$;$) {  sub parse_char_stream ($$$;$) {
579    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
580    my $s = ref $_[0] ? $_[0] : \($_[0]);    my $input = $_[0];
581    $self->{document} = $_[1];    $self->{document} = $_[1];
582    @{$self->{document}->child_nodes} = ();    @{$self->{document}->child_nodes} = ();
583    
# Line 186  sub parse_string ($$$;$) { Line 596  sub parse_string ($$$;$) {
596      pop @{$self->{prev_char}};      pop @{$self->{prev_char}};
597      unshift @{$self->{prev_char}}, $self->{next_char};      unshift @{$self->{prev_char}}, $self->{next_char};
598    
599      $self->{next_char} = -1 and return if $i >= length $$s;      my $char;
600      $self->{next_char} = ord substr $$s, $i++, 1;      if (defined $self->{next_next_char}) {
601          $char = $self->{next_next_char};
602          delete $self->{next_next_char};
603        } else {
604          $char = $input->getc;
605        }
606        $self->{next_char} = -1 and return unless defined $char;
607        $self->{next_char} = ord $char;
608    
609      ($self->{line_prev}, $self->{column_prev})      ($self->{line_prev}, $self->{column_prev})
610          = ($self->{line}, $self->{column});          = ($self->{line}, $self->{column});
611      $self->{column}++;      $self->{column}++;
612            
613      if ($self->{next_char} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
614          !!!cp ('j1');
615        $self->{line}++;        $self->{line}++;
616        $self->{column} = 0;        $self->{column} = 0;
617      } elsif ($self->{next_char} == 0x000D) { # CR      } elsif ($self->{next_char} == 0x000D) { # CR
618        $i++ if substr ($$s, $i, 1) eq "\x0A";        !!!cp ('j2');
619          my $next = $input->getc;
620          if (defined $next and $next ne "\x0A") {
621            $self->{next_next_char} = $next;
622          }
623        $self->{next_char} = 0x000A; # LF # MUST        $self->{next_char} = 0x000A; # LF # MUST
624        $self->{line}++;        $self->{line}++;
625        $self->{column} = 0;        $self->{column} = 0;
626      } elsif ($self->{next_char} > 0x10FFFF) {      } elsif ($self->{next_char} > 0x10FFFF) {
627          !!!cp ('j3');
628        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
629      } elsif ($self->{next_char} == 0x0000) { # NULL      } elsif ($self->{next_char} == 0x0000) { # NULL
630          !!!cp ('j4');
631        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
632        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
633        } elsif ($self->{next_char} <= 0x0008 or
634                 (0x000E <= $self->{next_char} and $self->{next_char} <= 0x001F) or
635                 (0x007F <= $self->{next_char} and $self->{next_char} <= 0x009F) or
636                 (0xD800 <= $self->{next_char} and $self->{next_char} <= 0xDFFF) or
637                 (0xFDD0 <= $self->{next_char} and $self->{next_char} <= 0xFDDF) or
638                 {
639                  0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
640                  0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
641                  0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
642                  0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
643                  0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
644                  0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
645                  0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
646                  0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
647                  0x10FFFE => 1, 0x10FFFF => 1,
648                 }->{$self->{next_char}}) {
649          !!!cp ('j5');
650          !!!parse-error (type => 'control char', level => $self->{must_level});
651    ## TODO: error type documentation
652      }      }
653    };    };
654    $self->{prev_char} = [-1, -1, -1];    $self->{prev_char} = [-1, -1, -1];
# Line 229  sub parse_string ($$$;$) { Line 672  sub parse_string ($$$;$) {
672    delete $self->{parse_error}; # remove loop    delete $self->{parse_error}; # remove loop
673    
674    return $self->{document};    return $self->{document};
675  } # parse_string  } # parse_char_stream
676    
677  sub new ($) {  sub new ($) {
678    my $class = shift;    my $class = shift;
679    my $self = bless {}, $class;    my $self = bless {
680        must_level => 'm',
681        should_level => 's',
682        good_level => 'w',
683        warn_level => 'w',
684        info_level => 'i',
685        unsupported_level => 'u',
686      }, $class;
687    $self->{set_next_char} = sub {    $self->{set_next_char} = sub {
688      $self->{next_char} = -1;      $self->{next_char} = -1;
689    };    };
# Line 295  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO Line 745  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO
745  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
746  sub BOGUS_DOCTYPE_STATE () { 32 }  sub BOGUS_DOCTYPE_STATE () { 32 }
747  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
748    sub SELF_CLOSING_START_TAG_STATE () { 34 }
749    sub CDATA_BLOCK_STATE () { 35 }
750    
751  sub DOCTYPE_TOKEN () { 1 }  sub DOCTYPE_TOKEN () { 1 }
752  sub COMMENT_TOKEN () { 2 }  sub COMMENT_TOKEN () { 2 }
# Line 312  sub ROW_IMS ()        { 0b10000000 } Line 764  sub ROW_IMS ()        { 0b10000000 }
764  sub BODY_AFTER_IMS () { 0b100000000 }  sub BODY_AFTER_IMS () { 0b100000000 }
765  sub FRAME_IMS ()      { 0b1000000000 }  sub FRAME_IMS ()      { 0b1000000000 }
766  sub SELECT_IMS ()     { 0b10000000000 }  sub SELECT_IMS ()     { 0b10000000000 }
767    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
768        ## NOTE: "in foreign content" insertion mode is special; it is combined
769        ## with the secondary insertion mode.  In this parser, they are stored
770        ## together in the bit-or'ed form.
771    
772  ## NOTE: "initial" and "before html" insertion modes have no constants.  ## NOTE: "initial" and "before html" insertion modes have no constants.
773    
# Line 348  sub _initialize_tokenizer ($) { Line 804  sub _initialize_tokenizer ($) {
804    undef $self->{current_attribute};    undef $self->{current_attribute};
805    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
806    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
807      delete $self->{self_closing};
808    $self->{char} = [];    $self->{char} = [];
809    # $self->{next_char}    # $self->{next_char}
810    !!!next-input-character;    !!!next-input-character;
# Line 368  sub _initialize_tokenizer ($) { Line 825  sub _initialize_tokenizer ($) {
825  ##        ->{value}  ##        ->{value}
826  ##        ->{has_reference} == 1 or 0  ##        ->{has_reference} == 1 or 0
827  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
828    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
829    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
830    ##     while the token is pushed back to the stack.
831    
832    ## ISSUE: "When a DOCTYPE token is created, its
833    ## <i>self-closing flag</i> must be unset (its other state is that it
834    ## be set), and its attributes list must be empty.": Wrong subject?
835    
836  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
837    
# Line 394  sub _initialize_tokenizer ($) { Line 858  sub _initialize_tokenizer ($) {
858    
859  sub _get_next_token ($) {  sub _get_next_token ($) {
860    my $self = shift;    my $self = shift;
861    
862      if ($self->{self_closing}) {
863        !!!parse-error (type => 'nestc', token => $self->{current_token});
864        ## NOTE: The |self_closing| flag is only set by start tag token.
865        ## In addition, when a start tag token is emitted, it is always set to
866        ## |current_token|.
867        delete $self->{self_closing};
868      }
869    
870    if (@{$self->{token}}) {    if (@{$self->{token}}) {
871        $self->{self_closing} = $self->{token}->[0]->{self_closing};
872      return shift @{$self->{token}};      return shift @{$self->{token}};
873    }    }
874    
# Line 466  sub _get_next_token ($) { Line 940  sub _get_next_token ($) {
940        # Anything else        # Anything else
941        my $token = {type => CHARACTER_TOKEN,        my $token = {type => CHARACTER_TOKEN,
942                     data => chr $self->{next_char},                     data => chr $self->{next_char},
943                     #line => $self->{line}, column => $self->{column},                     line => $self->{line}, column => $self->{column},
944                    };                    };
945        ## Stay in the data state        ## Stay in the data state
946        !!!next-input-character;        !!!next-input-character;
# Line 477  sub _get_next_token ($) { Line 951  sub _get_next_token ($) {
951      } elsif ($self->{state} == ENTITY_DATA_STATE) {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
952        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
953    
954        #my ($l, $c) = ($self->{line_prev}, $self->{column_prev});        my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
955                
956        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);
957    
# Line 487  sub _get_next_token ($) { Line 961  sub _get_next_token ($) {
961        unless (defined $token) {        unless (defined $token) {
962          !!!cp (13);          !!!cp (13);
963          !!!emit ({type => CHARACTER_TOKEN, data => '&',          !!!emit ({type => CHARACTER_TOKEN, data => '&',
964                    #line => $l, column => $c,                    line => $l, column => $c,
965                   });                   });
966        } else {        } else {
967          !!!cp (14);          !!!cp (14);
# Line 508  sub _get_next_token ($) { Line 982  sub _get_next_token ($) {
982            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
983    
984            !!!emit ({type => CHARACTER_TOKEN, data => '<',            !!!emit ({type => CHARACTER_TOKEN, data => '<',
985                      #line => $self->{line_prev},                      line => $self->{line_prev},
986                      #column => $self->{column_prev},                      column => $self->{column_prev},
987                     });                     });
988    
989            redo A;            redo A;
# Line 555  sub _get_next_token ($) { Line 1029  sub _get_next_token ($) {
1029            !!!next-input-character;            !!!next-input-character;
1030    
1031            !!!emit ({type => CHARACTER_TOKEN, data => '<>',            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
1032                      #line => $self->{line_prev},                      line => $self->{line_prev},
1033                      #column => $self->{column_prev},                      column => $self->{column_prev},
1034                     });                     });
1035    
1036            redo A;            redo A;
# Line 567  sub _get_next_token ($) { Line 1041  sub _get_next_token ($) {
1041                            column => $self->{column_prev});                            column => $self->{column_prev});
1042            $self->{state} = BOGUS_COMMENT_STATE;            $self->{state} = BOGUS_COMMENT_STATE;
1043            $self->{current_token} = {type => COMMENT_TOKEN, data => '',            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1044                                      #line => $self->{line_prev},                                      line => $self->{line_prev},
1045                                      #column => $self->{column_prev},                                      column => $self->{column_prev},
1046                                     };                                     };
1047            ## $self->{next_char} is intentionally left as is            ## $self->{next_char} is intentionally left as is
1048            redo A;            redo A;
1049          } else {          } else {
1050            !!!cp (23);            !!!cp (23);
1051            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago',
1052                              line => $self->{line_prev},
1053                              column => $self->{column_prev});
1054            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1055            ## reconsume            ## reconsume
1056    
1057            !!!emit ({type => CHARACTER_TOKEN, data => '<',            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1058                      #line => $self->{line_prev},                      line => $self->{line_prev},
1059                      #column => $self->{column_prev},                      column => $self->{column_prev},
1060                     });                     });
1061    
1062            redo A;            redo A;
# Line 610  sub _get_next_token ($) { Line 1086  sub _get_next_token ($) {
1086                $self->{state} = DATA_STATE;                $self->{state} = DATA_STATE;
1087    
1088                !!!emit ({type => CHARACTER_TOKEN, data => '</',                !!!emit ({type => CHARACTER_TOKEN, data => '</',
1089                          #line => $l, column => $c,                          line => $l, column => $c,
1090                         });                         });
1091        
1092                redo A;                redo A;
# Line 631  sub _get_next_token ($) { Line 1107  sub _get_next_token ($) {
1107              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
1108              $self->{state} = DATA_STATE;              $self->{state} = DATA_STATE;
1109              !!!emit ({type => CHARACTER_TOKEN, data => '</',              !!!emit ({type => CHARACTER_TOKEN, data => '</',
1110                        #line => $l, column => $c,                        line => $l, column => $c,
1111                       });                       });
1112              redo A;              redo A;
1113            } else {            } else {
# Line 646  sub _get_next_token ($) { Line 1122  sub _get_next_token ($) {
1122            # next-input-character is already done            # next-input-character is already done
1123            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1124            !!!emit ({type => CHARACTER_TOKEN, data => '</',            !!!emit ({type => CHARACTER_TOKEN, data => '</',
1125                      #line => $l, column => $c,                      line => $l, column => $c,
1126                     });                     });
1127            redo A;            redo A;
1128          }          }
# Line 686  sub _get_next_token ($) { Line 1162  sub _get_next_token ($) {
1162          # reconsume          # reconsume
1163    
1164          !!!emit ({type => CHARACTER_TOKEN, data => '</',          !!!emit ({type => CHARACTER_TOKEN, data => '</',
1165                    #line => $l, column => $c,                    line => $l, column => $c,
1166                   });                   });
1167    
1168          redo A;          redo A;
# Line 695  sub _get_next_token ($) { Line 1171  sub _get_next_token ($) {
1171          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
1172          $self->{state} = BOGUS_COMMENT_STATE;          $self->{state} = BOGUS_COMMENT_STATE;
1173          $self->{current_token} = {type => COMMENT_TOKEN, data => '',          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1174                                    #line => $self->{line_prev}, # "<" of "</"                                    line => $self->{line_prev}, # "<" of "</"
1175                                    #column => $self->{column_prev} - 1,                                    column => $self->{column_prev} - 1,
1176                                   };                                   };
1177          ## $self->{next_char} is intentionally left as is          ## $self->{next_char} is intentionally left as is
1178          redo A;          redo A;
# Line 765  sub _get_next_token ($) { Line 1241  sub _get_next_token ($) {
1241    
1242          redo A;          redo A;
1243        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1244            !!!cp (42);
1245            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1246          !!!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  
1247          redo A;          redo A;
1248        } else {        } else {
1249          !!!cp (44);          !!!cp (44);
# Line 821  sub _get_next_token ($) { Line 1287  sub _get_next_token ($) {
1287        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{next_char} and
1288                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1289          !!!cp (49);          !!!cp (49);
1290          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{current_attribute}
1291                                value => ''};              = {name => chr ($self->{next_char} + 0x0020),
1292                   value => '',
1293                   line => $self->{line}, column => $self->{column}};
1294          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1295          !!!next-input-character;          !!!next-input-character;
1296          redo A;          redo A;
1297        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1298            !!!cp (50);
1299            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1300          !!!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  
1301          redo A;          redo A;
1302        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1303          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
# Line 874  sub _get_next_token ($) { Line 1332  sub _get_next_token ($) {
1332          } else {          } else {
1333            !!!cp (56);            !!!cp (56);
1334          }          }
1335          $self->{current_attribute} = {name => chr ($self->{next_char}),          $self->{current_attribute}
1336                                value => ''};              = {name => chr ($self->{next_char}),
1337                   value => '',
1338                   line => $self->{line}, column => $self->{column}};
1339          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1340          !!!next-input-character;          !!!next-input-character;
1341          redo A;          redo A;
# Line 885  sub _get_next_token ($) { Line 1345  sub _get_next_token ($) {
1345          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1346              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1347            !!!cp (57);            !!!cp (57);
1348            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name}, line => $self->{current_attribute}->{line}, column => $self->{current_attribute}->{column});
1349            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1350          } else {          } else {
1351            !!!cp (58);            !!!cp (58);
# Line 938  sub _get_next_token ($) { Line 1398  sub _get_next_token ($) {
1398          !!!next-input-character;          !!!next-input-character;
1399          redo A;          redo A;
1400        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1401            !!!cp (64);
1402          $before_leave->();          $before_leave->();
1403            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1404          !!!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  
1405          redo A;          redo A;
1406        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1407          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
# Line 1030  sub _get_next_token ($) { Line 1480  sub _get_next_token ($) {
1480        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{next_char} and
1481                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1482          !!!cp (76);          !!!cp (76);
1483          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{current_attribute}
1484                                value => ''};              = {name => chr ($self->{next_char} + 0x0020),
1485                   value => '',
1486                   line => $self->{line}, column => $self->{column}};
1487          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1488          !!!next-input-character;          !!!next-input-character;
1489          redo A;          redo A;
1490        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1491            !!!cp (77);
1492            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1493          !!!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  
1494          redo A;          redo A;
1495        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1496          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
# Line 1076  sub _get_next_token ($) { Line 1517  sub _get_next_token ($) {
1517          redo A;          redo A;
1518        } else {        } else {
1519          !!!cp (82);          !!!cp (82);
1520          $self->{current_attribute} = {name => chr ($self->{next_char}),          $self->{current_attribute}
1521                                value => ''};              = {name => chr ($self->{next_char}),
1522                   value => '',
1523                   line => $self->{line}, column => $self->{column}};
1524          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1525          !!!next-input-character;          !!!next-input-character;
1526          redo A;                  redo A;        
# Line 1380  sub _get_next_token ($) { Line 1823  sub _get_next_token ($) {
1823    
1824          redo A;          redo A;
1825        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1826            !!!cp (122);
1827            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1828          !!!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 (122);  
           #  
         } else {  
           !!!cp (123);  
           !!!parse-error (type => 'nestc');  
         }  
         $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;  
         # next-input-character is already done  
1829          redo A;          redo A;
1830        } else {        } else {
1831          !!!cp (124);          !!!cp ('124.1');
1832          !!!parse-error (type => 'no space between attributes');          !!!parse-error (type => 'no space between attributes');
1833          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1834          ## reconsume          ## reconsume
1835          redo A;          redo A;
1836        }        }
1837        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1838          if ($self->{next_char} == 0x003E) { # >
1839            if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1840              !!!cp ('124.2');
1841              !!!parse-error (type => 'nestc', token => $self->{current_token});
1842              ## TODO: Different type than slash in start tag
1843              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1844              if ($self->{current_token}->{attributes}) {
1845                !!!cp ('124.4');
1846                !!!parse-error (type => 'end tag attribute');
1847              } else {
1848                !!!cp ('124.5');
1849              }
1850              ## TODO: Test |<title></title/>|
1851            } else {
1852              !!!cp ('124.3');
1853              $self->{self_closing} = 1;
1854            }
1855    
1856            $self->{state} = DATA_STATE;
1857            !!!next-input-character;
1858    
1859            !!!emit ($self->{current_token}); # start tag or end tag
1860    
1861            redo A;
1862          } else {
1863            !!!cp ('124.4');
1864            !!!parse-error (type => 'nestc');
1865            ## TODO: This error type is wrong.
1866            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1867            ## Reconsume.
1868            redo A;
1869          }
1870      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1871        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1872                
# Line 1436  sub _get_next_token ($) { Line 1902  sub _get_next_token ($) {
1902      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1903        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1904    
1905        #my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1);        my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1);
1906    
1907        my @next_char;        my @next_char;
1908        push @next_char, $self->{next_char};        push @next_char, $self->{next_char};
# Line 1447  sub _get_next_token ($) { Line 1913  sub _get_next_token ($) {
1913          if ($self->{next_char} == 0x002D) { # -          if ($self->{next_char} == 0x002D) { # -
1914            !!!cp (127);            !!!cp (127);
1915            $self->{current_token} = {type => COMMENT_TOKEN, data => '',            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1916                                      #line => $l, column => $c,                                      line => $l, column => $c,
1917                                     };                                     };
1918            $self->{state} = COMMENT_START_STATE;            $self->{state} = COMMENT_START_STATE;
1919            !!!next-input-character;            !!!next-input-character;
# Line 1486  sub _get_next_token ($) { Line 1952  sub _get_next_token ($) {
1952                      $self->{state} = DOCTYPE_STATE;                      $self->{state} = DOCTYPE_STATE;
1953                      $self->{current_token} = {type => DOCTYPE_TOKEN,                      $self->{current_token} = {type => DOCTYPE_TOKEN,
1954                                                quirks => 1,                                                quirks => 1,
1955                                                #line => $l, column => $c,                                                line => $l, column => $c,
1956                                               };                                               };
1957                      !!!next-input-character;                      !!!next-input-character;
1958                      redo A;                      redo A;
# Line 1508  sub _get_next_token ($) { Line 1974  sub _get_next_token ($) {
1974          } else {          } else {
1975            !!!cp (135);            !!!cp (135);
1976          }          }
1977          } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
1978                   $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
1979                   $self->{next_char} == 0x005B) { # [
1980            !!!next-input-character;
1981            push @next_char, $self->{next_char};
1982            if ($self->{next_char} == 0x0043) { # C
1983              !!!next-input-character;
1984              push @next_char, $self->{next_char};
1985              if ($self->{next_char} == 0x0044) { # D
1986                !!!next-input-character;
1987                push @next_char, $self->{next_char};
1988                if ($self->{next_char} == 0x0041) { # A
1989                  !!!next-input-character;
1990                  push @next_char, $self->{next_char};
1991                  if ($self->{next_char} == 0x0054) { # T
1992                    !!!next-input-character;
1993                    push @next_char, $self->{next_char};
1994                    if ($self->{next_char} == 0x0041) { # A
1995                      !!!next-input-character;
1996                      push @next_char, $self->{next_char};
1997                      if ($self->{next_char} == 0x005B) { # [
1998                        !!!cp (135.1);
1999                        $self->{state} = CDATA_BLOCK_STATE;
2000                        !!!next-input-character;
2001                        redo A;
2002                      } else {
2003                        !!!cp (135.2);
2004                      }
2005                    } else {
2006                      !!!cp (135.3);
2007                    }
2008                  } else {
2009                    !!!cp (135.4);                
2010                  }
2011                } else {
2012                  !!!cp (135.5);
2013                }
2014              } else {
2015                !!!cp (135.6);
2016              }
2017            } else {
2018              !!!cp (135.7);
2019            }
2020        } else {        } else {
2021          !!!cp (136);          !!!cp (136);
2022        }        }
# Line 1517  sub _get_next_token ($) { Line 2026  sub _get_next_token ($) {
2026        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
2027        $self->{state} = BOGUS_COMMENT_STATE;        $self->{state} = BOGUS_COMMENT_STATE;
2028        $self->{current_token} = {type => COMMENT_TOKEN, data => '',        $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2029                                  #line => $l, column => $c,                                  line => $l, column => $c,
2030                                 };                                 };
2031        redo A;        redo A;
2032                
# Line 2232  sub _get_next_token ($) { Line 2741  sub _get_next_token ($) {
2741          !!!next-input-character;          !!!next-input-character;
2742          redo A;          redo A;
2743        }        }
2744        } elsif ($self->{state} == CDATA_BLOCK_STATE) {
2745          my $s = '';
2746          
2747          my ($l, $c) = ($self->{line}, $self->{column});
2748    
2749          CS: while ($self->{next_char} != -1) {
2750            if ($self->{next_char} == 0x005D) { # ]
2751              !!!next-input-character;
2752              if ($self->{next_char} == 0x005D) { # ]
2753                !!!next-input-character;
2754                MDC: {
2755                  if ($self->{next_char} == 0x003E) { # >
2756                    !!!cp (221.1);
2757                    !!!next-input-character;
2758                    last CS;
2759                  } elsif ($self->{next_char} == 0x005D) { # ]
2760                    !!!cp (221.2);
2761                    $s .= ']';
2762                    !!!next-input-character;
2763                    redo MDC;
2764                  } else {
2765                    !!!cp (221.3);
2766                    $s .= ']]';
2767                    #
2768                  }
2769                } # MDC
2770              } else {
2771                !!!cp (221.4);
2772                $s .= ']';
2773                #
2774              }
2775            } else {
2776              !!!cp (221.5);
2777              #
2778            }
2779            $s .= chr $self->{next_char};
2780            !!!next-input-character;
2781          } # CS
2782    
2783          $self->{state} = DATA_STATE;
2784          ## next-input-character done or EOF, which is reconsumed.
2785    
2786          if (length $s) {
2787            !!!cp (221.6);
2788            !!!emit ({type => CHARACTER_TOKEN, data => $s,
2789                      line => $l, column => $c});
2790          } else {
2791            !!!cp (221.7);
2792          }
2793    
2794          redo A;
2795    
2796          ## ISSUE: "text tokens" in spec.
2797          ## TODO: Streaming support
2798      } else {      } else {
2799        die "$0: $self->{state}: Unknown state";        die "$0: $self->{state}: Unknown state";
2800      }      }
# Line 2317  sub _tokenize_attempt_to_consume_an_enti Line 2880  sub _tokenize_attempt_to_consume_an_enti
2880    
2881          return {type => CHARACTER_TOKEN, data => chr $code,          return {type => CHARACTER_TOKEN, data => chr $code,
2882                  has_reference => 1,                  has_reference => 1,
2883                  #line => $l, column => $c,                  line => $l, column => $c,
2884                 };                 };
2885        } # X        } # X
2886      } elsif (0x0030 <= $self->{next_char} and      } elsif (0x0030 <= $self->{next_char} and
# Line 2361  sub _tokenize_attempt_to_consume_an_enti Line 2924  sub _tokenize_attempt_to_consume_an_enti
2924        }        }
2925                
2926        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,
2927                #line => $l, column => $c,                line => $l, column => $c,
2928               };               };
2929      } else {      } else {
2930        !!!cp (1019);        !!!cp (1019);
# Line 2382  sub _tokenize_attempt_to_consume_an_enti Line 2945  sub _tokenize_attempt_to_consume_an_enti
2945      require Whatpm::_NamedEntityList;      require Whatpm::_NamedEntityList;
2946      our $EntityChar;      our $EntityChar;
2947    
2948      while (length $entity_name < 10 and      while (length $entity_name < 30 and
2949             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
2950             ((0x0041 <= $self->{next_char} and # a             ((0x0041 <= $self->{next_char} and # a
2951               $self->{next_char} <= 0x005A) or # x               $self->{next_char} <= 0x005A) or # x
# Line 2416  sub _tokenize_attempt_to_consume_an_enti Line 2979  sub _tokenize_attempt_to_consume_an_enti
2979      if ($match > 0) {      if ($match > 0) {
2980        !!!cp (1023);        !!!cp (1023);
2981        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
2982                #line => $l, column => $c,                line => $l, column => $c,
2983               };               };
2984      } elsif ($match < 0) {      } elsif ($match < 0) {
2985        !!!parse-error (type => 'no refc', line => $l, column => $c);        !!!parse-error (type => 'no refc', line => $l, column => $c);
2986        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
2987          !!!cp (1024);          !!!cp (1024);
2988          return {type => CHARACTER_TOKEN, data => '&'.$entity_name,          return {type => CHARACTER_TOKEN, data => '&'.$entity_name,
2989                  #line => $l, column => $c,                  line => $l, column => $c,
2990                 };                 };
2991        } else {        } else {
2992          !!!cp (1025);          !!!cp (1025);
2993          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
2994                  #line => $l, column => $c,                  line => $l, column => $c,
2995                 };                 };
2996        }        }
2997      } else {      } else {
# Line 2436  sub _tokenize_attempt_to_consume_an_enti Line 2999  sub _tokenize_attempt_to_consume_an_enti
2999        !!!parse-error (type => 'bare ero', line => $l, column => $c);        !!!parse-error (type => 'bare ero', line => $l, column => $c);
3000        ## NOTE: "No characters are consumed" in the spec.        ## NOTE: "No characters are consumed" in the spec.
3001        return {type => CHARACTER_TOKEN, data => '&'.$value,        return {type => CHARACTER_TOKEN, data => '&'.$value,
3002                #line => $l, column => $c,                line => $l, column => $c,
3003               };               };
3004      }      }
3005    } else {    } else {
# Line 2524  sub _tree_construction_initial ($) { Line 3087  sub _tree_construction_initial ($) {
3087                
3088        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
3089          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3090          ## NOTE: Default value for both |public_id| and |system_id| attributes
3091          ## are empty strings, so that we don't set any value in missing cases.
3092        $doctype->public_id ($token->{public_identifier})        $doctype->public_id ($token->{public_identifier})
3093            if defined $token->{public_identifier};            if defined $token->{public_identifier};
3094        $doctype->system_id ($token->{system_identifier})        $doctype->system_id ($token->{system_identifier})
# Line 2660  sub _tree_construction_initial ($) { Line 3225  sub _tree_construction_initial ($) {
3225        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3226        ## Go to the "before html" insertion mode.        ## Go to the "before html" insertion mode.
3227        ## reprocess        ## reprocess
3228          !!!ack-later;
3229        return;        return;
3230      } elsif ($token->{type} == CHARACTER_TOKEN) {      } elsif ($token->{type} == CHARACTER_TOKEN) {
3231        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
# Line 2740  sub _tree_construction_root_element ($) Line 3306  sub _tree_construction_root_element ($)
3306        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
3307          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
3308            my $root_element;            my $root_element;
3309            !!!create-element ($root_element, $token->{tag_name}, $token->{attributes}, $token);            !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3310            $self->{document}->append_child ($root_element);            $self->{document}->append_child ($root_element);
3311            push @{$self->{open_elements}}, [$root_element, 'html'];            push @{$self->{open_elements}},
3312                  [$root_element, $el_category->{html}];
3313    
3314            if ($token->{attributes}->{manifest}) {            if ($token->{attributes}->{manifest}) {
3315              !!!cp ('t24');              !!!cp ('t24');
# Line 2757  sub _tree_construction_root_element ($) Line 3324  sub _tree_construction_root_element ($)
3324              $self->{application_cache_selection}->(undef);              $self->{application_cache_selection}->(undef);
3325            }            }
3326    
3327              !!!nack ('t25c');
3328    
3329            !!!next-token;            !!!next-token;
3330            return; ## Go to the "before head" insertion mode.            return; ## Go to the "before head" insertion mode.
3331          } else {          } else {
# Line 2773  sub _tree_construction_root_element ($) Line 3342  sub _tree_construction_root_element ($)
3342          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
3343        }        }
3344    
3345      my $root_element; !!!create-element ($root_element, 'html',, $token);      my $root_element;
3346        !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3347      $self->{document}->append_child ($root_element);      $self->{document}->append_child ($root_element);
3348      push @{$self->{open_elements}}, [$root_element, 'html'];      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3349    
3350      $self->{application_cache_selection}->(undef);      $self->{application_cache_selection}->(undef);
3351    
3352      ## NOTE: Reprocess the token.      ## NOTE: Reprocess the token.
3353        !!!ack-later;
3354      return; ## Go to the "before head" insertion mode.      return; ## Go to the "before head" insertion mode.
3355    
3356      ## ISSUE: There is an issue in the spec      ## ISSUE: There is an issue in the spec
# Line 2803  sub _reset_insertion_mode ($) { Line 3374  sub _reset_insertion_mode ($) {
3374        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3375          $last = 1;          $last = 1;
3376          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
3377            if ($self->{inner_html_node}->[1] eq 'td' or            if ($self->{inner_html_node}->[1] & TABLE_CELL_EL) {
               $self->{inner_html_node}->[1] eq 'th') {  
3378              !!!cp ('t27');              !!!cp ('t27');
3379              #              #
3380            } else {            } else {
# Line 2814  sub _reset_insertion_mode ($) { Line 3384  sub _reset_insertion_mode ($) {
3384          }          }
3385        }        }
3386            
3387        ## Step 4..13      ## Step 4..14
3388        my $new_mode = {      my $new_mode;
3389        if ($node->[1] & FOREIGN_EL) {
3390          ## NOTE: Strictly spaking, the line below only applies to MathML and
3391          ## SVG elements.  Currently the HTML syntax supports only MathML and
3392          ## SVG elements as foreigners.
3393          $new_mode = $self->{insertion_mode} | IN_FOREIGN_CONTENT_IM;
3394          ## ISSUE: What is set as the secondary insertion mode?
3395        } else {
3396          $new_mode = {
3397                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
3398                        ## NOTE: |option| and |optgroup| do not set                        ## NOTE: |option| and |optgroup| do not set
3399                        ## insertion mode to "in select" by themselves.                        ## insertion mode to "in select" by themselves.
# Line 2831  sub _reset_insertion_mode ($) { Line 3409  sub _reset_insertion_mode ($) {
3409                        head => IN_BODY_IM, # not in head!                        head => IN_BODY_IM, # not in head!
3410                        body => IN_BODY_IM,                        body => IN_BODY_IM,
3411                        frameset => IN_FRAMESET_IM,                        frameset => IN_FRAMESET_IM,
3412                       }->{$node->[1]};                       }->{$node->[0]->manakai_local_name};
3413        $self->{insertion_mode} = $new_mode and return if defined $new_mode;      }
3414        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3415                
3416        ## Step 14        ## Step 15
3417        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3418          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3419            !!!cp ('t29');            !!!cp ('t29');
3420            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
# Line 2849  sub _reset_insertion_mode ($) { Line 3428  sub _reset_insertion_mode ($) {
3428          !!!cp ('t31');          !!!cp ('t31');
3429        }        }
3430                
3431        ## Step 15        ## Step 16
3432        $self->{insertion_mode} = IN_BODY_IM and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
3433                
3434        ## Step 16        ## Step 17
3435        $i--;        $i--;
3436        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3437                
3438        ## Step 17        ## Step 18
3439        redo S3;        redo S3;
3440      } # S3      } # S3
3441    
# Line 2968  sub _tree_construction_main ($) { Line 3547  sub _tree_construction_main ($) {
3547      ## Step 1      ## Step 1
3548      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3549      my $el;      my $el;
3550      !!!create-element ($el, $start_tag_name, $token->{attributes}, $token);      !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3551    
3552      ## Step 2      ## Step 2
3553      $insert->($el);      $insert->($el);
# Line 2979  sub _tree_construction_main ($) { Line 3558  sub _tree_construction_main ($) {
3558    
3559      ## Step 4      ## Step 4
3560      my $text = '';      my $text = '';
3561        !!!nack ('t40.1');
3562      !!!next-token;      !!!next-token;
3563      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3564        !!!cp ('t40');        !!!cp ('t40');
# Line 3018  sub _tree_construction_main ($) { Line 3598  sub _tree_construction_main ($) {
3598    
3599    my $script_start_tag = sub () {    my $script_start_tag = sub () {
3600      my $script_el;      my $script_el;
3601      !!!create-element ($script_el, 'script', $token->{attributes}, $token);      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
3602      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3603    
3604      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
3605      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
3606            
3607      my $text = '';      my $text = '';
3608        !!!nack ('t45.1');
3609      !!!next-token;      !!!next-token;
3610      while ($token->{type} == CHARACTER_TOKEN) {      while ($token->{type} == CHARACTER_TOKEN) {
3611        !!!cp ('t45');        !!!cp ('t45');
# Line 3082  sub _tree_construction_main ($) { Line 3663  sub _tree_construction_main ($) {
3663        my $formatting_element;        my $formatting_element;
3664        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3665        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3666          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3667              !!!cp ('t52');
3668              last AFE;
3669            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
3670                         eq $tag_name) {
3671            !!!cp ('t51');            !!!cp ('t51');
3672            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3673            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3674            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           !!!cp ('t52');  
           last AFE;  
3675          }          }
3676        } # AFE        } # AFE
3677        unless (defined $formatting_element) {        unless (defined $formatting_element) {
# Line 3117  sub _tree_construction_main ($) { Line 3699  sub _tree_construction_main ($) {
3699              !!!next-token;              !!!next-token;
3700              return;              return;
3701            }            }
3702          } 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]}) {  
3703            !!!cp ('t56');            !!!cp ('t56');
3704            $in_scope = 0;            $in_scope = 0;
3705          }          }
# Line 3135  sub _tree_construction_main ($) { Line 3714  sub _tree_construction_main ($) {
3714        }        }
3715        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3716          !!!cp ('t58');          !!!cp ('t58');
3717          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1],          !!!parse-error (type => 'not closed',
3718                            value => $self->{open_elements}->[-1]->[0]
3719                                ->manakai_local_name,
3720                          token => $end_tag_token);                          token => $end_tag_token);
3721        }        }
3722                
# Line 3144  sub _tree_construction_main ($) { Line 3725  sub _tree_construction_main ($) {
3725        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
3726        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3727          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3728          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
3729              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3730              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
3731               $scoping_category->{$node->[1]})) { ## Scoping is redundant, maybe               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
3732            !!!cp ('t59');            !!!cp ('t59');
3733            $furthest_block = $node;            $furthest_block = $node;
3734            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
# Line 3233  sub _tree_construction_main ($) { Line 3814  sub _tree_construction_main ($) {
3814        } # S7          } # S7  
3815                
3816        ## Step 8        ## Step 8
3817        if ({        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
            table => 1, tbody => 1, tfoot => 1, thead => 1, tr => 1,  
           }->{$common_ancestor_node->[1]}) {  
3818          my $foster_parent_element;          my $foster_parent_element;
3819          my $next_sibling;          my $next_sibling;
3820                           OE: for (reverse 0..$#{$self->{open_elements}}) {          OE: for (reverse 0..$#{$self->{open_elements}}) {
3821                             if ($self->{open_elements}->[$_]->[1] eq 'table') {            if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3822                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3823                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3824                                 !!!cp ('t65.1');                                 !!!cp ('t65.1');
# Line 3312  sub _tree_construction_main ($) { Line 3891  sub _tree_construction_main ($) {
3891    
3892    my $insert_to_foster = sub {    my $insert_to_foster = sub {
3893      my $child = shift;      my $child = shift;
3894      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]}) {  
3895        # MUST        # MUST
3896        my $foster_parent_element;        my $foster_parent_element;
3897        my $next_sibling;        my $next_sibling;
3898                           OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3899                             if ($self->{open_elements}->[$_]->[1] eq 'table') {          if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3900                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3901                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3902                                 !!!cp ('t70');                                 !!!cp ('t70');
# Line 3344  sub _tree_construction_main ($) { Line 3921  sub _tree_construction_main ($) {
3921      }      }
3922    }; # $insert_to_foster    }; # $insert_to_foster
3923    
3924    B: {    B: while (1) {
3925      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
3926        !!!cp ('t73');        !!!cp ('t73');
3927        !!!parse-error (type => 'DOCTYPE in the middle', token => $token);        !!!parse-error (type => 'DOCTYPE in the middle', token => $token);
3928        ## Ignore the token        ## Ignore the token
3929        ## Stay in the phase        ## Stay in the phase
3930        !!!next-token;        !!!next-token;
3931        redo B;        next B;
3932      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
3933               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
3934        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
# Line 3377  sub _tree_construction_main ($) { Line 3954  sub _tree_construction_main ($) {
3954               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
3955          }          }
3956        }        }
3957          !!!nack ('t84.1');
3958        !!!next-token;        !!!next-token;
3959        redo B;        next B;
3960      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
3961        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
3962        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
# Line 3392  sub _tree_construction_main ($) { Line 3970  sub _tree_construction_main ($) {
3970          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
3971        }        }
3972        !!!next-token;        !!!next-token;
3973        redo B;        next B;
3974      } elsif ($self->{insertion_mode} & HEAD_IMS) {      } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
3975          if ($token->{type} == CHARACTER_TOKEN) {
3976            !!!cp ('t87.1');
3977            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3978            !!!next-token;
3979            next B;
3980          } elsif ($token->{type} == START_TAG_TOKEN) {
3981            if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
3982                 $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
3983                not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
3984                ($token->{tag_name} eq 'svg' and
3985                 $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
3986              ## NOTE: "using the rules for secondary insertion mode"then"continue"
3987              !!!cp ('t87.2');
3988              #
3989            } elsif ({
3990                      b => 1, big => 1, blockquote => 1, body => 1, br => 1,
3991                      center => 1, code => 1, dd => 1, div => 1, dl => 1, em => 1,
3992                      embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1, ## No h4!
3993                      h5 => 1, h6 => 1, head => 1, hr => 1, i => 1, img => 1,
3994                      li => 1, menu => 1, meta => 1, nobr => 1, p => 1, pre => 1,
3995                      ruby => 1, s => 1, small => 1, span => 1, strong => 1,
3996                      sub => 1, sup => 1, table => 1, tt => 1, u => 1, ul => 1,
3997                      var => 1,
3998                     }->{$token->{tag_name}}) {
3999              !!!cp ('t87.2');
4000              !!!parse-error (type => 'not closed',
4001                              value => $self->{open_elements}->[-1]->[0]
4002                                  ->manakai_local_name,
4003                              token => $token);
4004    
4005              pop @{$self->{open_elements}}
4006                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4007    
4008              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4009              ## Reprocess.
4010              next B;
4011            } else {
4012              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
4013              my $tag_name = $token->{tag_name};
4014              if ($nsuri eq $SVG_NS) {
4015                $tag_name = {
4016                   altglyph => 'altGlyph',
4017                   altglyphdef => 'altGlyphDef',
4018                   altglyphitem => 'altGlyphItem',
4019                   animatecolor => 'animateColor',
4020                   animatemotion => 'animateMotion',
4021                   animatetransform => 'animateTransform',
4022                   clippath => 'clipPath',
4023                   feblend => 'feBlend',
4024                   fecolormatrix => 'feColorMatrix',
4025                   fecomponenttransfer => 'feComponentTransfer',
4026                   fecomposite => 'feComposite',
4027                   feconvolvematrix => 'feConvolveMatrix',
4028                   fediffuselighting => 'feDiffuseLighting',
4029                   fedisplacementmap => 'feDisplacementMap',
4030                   fedistantlight => 'feDistantLight',
4031                   feflood => 'feFlood',
4032                   fefunca => 'feFuncA',
4033                   fefuncb => 'feFuncB',
4034                   fefuncg => 'feFuncG',
4035                   fefuncr => 'feFuncR',
4036                   fegaussianblur => 'feGaussianBlur',
4037                   feimage => 'feImage',
4038                   femerge => 'feMerge',
4039                   femergenode => 'feMergeNode',
4040                   femorphology => 'feMorphology',
4041                   feoffset => 'feOffset',
4042                   fepointlight => 'fePointLight',
4043                   fespecularlighting => 'feSpecularLighting',
4044                   fespotlight => 'feSpotLight',
4045                   fetile => 'feTile',
4046                   feturbulence => 'feTurbulence',
4047                   foreignobject => 'foreignObject',
4048                   glyphref => 'glyphRef',
4049                   lineargradient => 'linearGradient',
4050                   radialgradient => 'radialGradient',
4051                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4052                   textpath => 'textPath',  
4053                }->{$tag_name} || $tag_name;
4054              }
4055    
4056              ## "adjust SVG attributes" (SVG only) - done in insert-element-f
4057    
4058              ## "adjust foreign attributes" - done in insert-element-f
4059    
4060              !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
4061    
4062              if ($self->{self_closing}) {
4063                pop @{$self->{open_elements}};
4064                !!!ack ('t87.3');
4065              } else {
4066                !!!cp ('t87.4');
4067              }
4068    
4069              !!!next-token;
4070              next B;
4071            }
4072          } elsif ($token->{type} == END_TAG_TOKEN) {
4073            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4074            !!!cp ('t87.5');
4075            #
4076          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4077            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4078            !!!cp ('t87.6');
4079            #
4080            ## TODO: ...
4081          } else {
4082            die "$0: $token->{type}: Unknown token type";        
4083          }
4084        }
4085    
4086        if ($self->{insertion_mode} & HEAD_IMS) {
4087        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4088          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4089            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
# Line 3403  sub _tree_construction_main ($) { Line 4093  sub _tree_construction_main ($) {
4093              !!!cp ('t88.1');              !!!cp ('t88.1');
4094              ## Ignore the token.              ## Ignore the token.
4095              !!!next-token;              !!!next-token;
4096              redo B;              next B;
4097            }            }
4098            unless (length $token->{data}) {            unless (length $token->{data}) {
4099              !!!cp ('t88');              !!!cp ('t88');
4100              !!!next-token;              !!!next-token;
4101              redo B;              next B;
4102            }            }
4103          }          }
4104    
4105          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4106            !!!cp ('t89');            !!!cp ('t89');
4107            ## As if <head>            ## As if <head>
4108            !!!create-element ($self->{head_element}, 'head',, $token);            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4109            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4110            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            push @{$self->{open_elements}},
4111                  [$self->{head_element}, $el_category->{head}];
4112    
4113            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4114            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
# Line 3443  sub _tree_construction_main ($) { Line 4134  sub _tree_construction_main ($) {
4134            !!!cp ('t92');            !!!cp ('t92');
4135          }          }
4136    
4137              ## "after head" insertion mode          ## "after head" insertion mode
4138              ## As if <body>          ## As if <body>
4139              !!!insert-element ('body',, $token);          !!!insert-element ('body',, $token);
4140              $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4141              ## reprocess          ## reprocess
4142              redo B;          next B;
4143            } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4144              if ($token->{tag_name} eq 'head') {          if ($token->{tag_name} eq 'head') {
4145                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4146                  !!!cp ('t93');              !!!cp ('t93');
4147                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes}, $token);              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4148                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              $self->{open_elements}->[-1]->[0]->append_child
4149                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];                  ($self->{head_element});
4150                  $self->{insertion_mode} = IN_HEAD_IM;              push @{$self->{open_elements}},
4151                  !!!next-token;                  [$self->{head_element}, $el_category->{head}];
4152                  redo B;              $self->{insertion_mode} = IN_HEAD_IM;
4153                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {              !!!nack ('t93.1');
4154                  !!!cp ('t94');              !!!next-token;
4155                  #              next B;
4156                } else {            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4157                  !!!cp ('t95');              !!!cp ('t93.2');
4158                  !!!parse-error (type => 'in head:head', token => $token); # or in head noscript              !!!parse-error (type => 'after head:head', token => $token); ## TODO: error type
4159                  ## Ignore the token              ## Ignore the token
4160                  !!!next-token;              !!!nack ('t93.3');
4161                  redo B;              !!!next-token;
4162                }              next B;
4163              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {            } else {
4164                !!!cp ('t96');              !!!cp ('t95');
4165                ## As if <head>              !!!parse-error (type => 'in head:head', token => $token); # or in head noscript
4166                !!!create-element ($self->{head_element}, 'head',, $token);              ## Ignore the token
4167                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              !!!nack ('t95.1');
4168                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];              !!!next-token;
4169                next B;
4170              }
4171            } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4172              !!!cp ('t96');
4173              ## As if <head>
4174              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4175              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4176              push @{$self->{open_elements}},
4177                  [$self->{head_element}, $el_category->{head}];
4178    
4179                $self->{insertion_mode} = IN_HEAD_IM;            $self->{insertion_mode} = IN_HEAD_IM;
4180                ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4181              } else {          } else {
4182                !!!cp ('t97');            !!!cp ('t97');
4183              }          }
4184    
4185              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
4186                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
# Line 3499  sub _tree_construction_main ($) { Line 4199  sub _tree_construction_main ($) {
4199                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4200                  !!!cp ('t100');                  !!!cp ('t100');
4201                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4202                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4203                        [$self->{head_element}, $el_category->{head}];
4204                } else {                } else {
4205                  !!!cp ('t101');                  !!!cp ('t101');
4206                }                }
# Line 3507  sub _tree_construction_main ($) { Line 4208  sub _tree_construction_main ($) {
4208                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4209                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4210                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4211                  !!!nack ('t101.1');
4212                !!!next-token;                !!!next-token;
4213                redo B;                next B;
4214              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
4215                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4216                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4217                  !!!cp ('t102');                  !!!cp ('t102');
4218                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4219                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4220                        [$self->{head_element}, $el_category->{head}];
4221                } else {                } else {
4222                  !!!cp ('t103');                  !!!cp ('t103');
4223                }                }
# Line 3522  sub _tree_construction_main ($) { Line 4225  sub _tree_construction_main ($) {
4225                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4226                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4227                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4228                  !!!ack ('t103.1');
4229                !!!next-token;                !!!next-token;
4230                redo B;                next B;
4231              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
4232                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4233                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4234                  !!!cp ('t104');                  !!!cp ('t104');
4235                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4236                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4237                        [$self->{head_element}, $el_category->{head}];
4238                } else {                } else {
4239                  !!!cp ('t105');                  !!!cp ('t105');
4240                }                }
# Line 3537  sub _tree_construction_main ($) { Line 4242  sub _tree_construction_main ($) {
4242                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.
4243    
4244                unless ($self->{confident}) {                unless ($self->{confident}) {
4245                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) {
4246                    !!!cp ('t106');                    !!!cp ('t106');
4247                      ## NOTE: Whether the encoding is supported or not is handled
4248                      ## in the {change_encoding} callback.
4249                    $self->{change_encoding}                    $self->{change_encoding}
4250                        ->($self, $token->{attributes}->{charset}->{value},                        ->($self, $token->{attributes}->{charset}->{value},
4251                           $token);                           $token);
# Line 3548  sub _tree_construction_main ($) { Line 4255  sub _tree_construction_main ($) {
4255                                             $token->{attributes}->{charset}                                             $token->{attributes}->{charset}
4256                                                 ->{has_reference});                                                 ->{has_reference});
4257                  } elsif ($token->{attributes}->{content}) {                  } elsif ($token->{attributes}->{content}) {
                   ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
4258                    if ($token->{attributes}->{content}->{value}                    if ($token->{attributes}->{content}->{value}
4259                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4260                            [\x09-\x0D\x20]*=                            [\x09-\x0D\x20]*=
4261                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4262                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
4263                      !!!cp ('t107');                      !!!cp ('t107');
4264                        ## NOTE: Whether the encoding is supported or not is handled
4265                        ## in the {change_encoding} callback.
4266                      $self->{change_encoding}                      $self->{change_encoding}
4267                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4268                             $token);                             $token);
# Line 3585  sub _tree_construction_main ($) { Line 4293  sub _tree_construction_main ($) {
4293    
4294                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4295                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4296                  !!!ack ('t110.1');
4297                !!!next-token;                !!!next-token;
4298                redo B;                next B;
4299              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
4300                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4301                  !!!cp ('t111');                  !!!cp ('t111');
# Line 3599  sub _tree_construction_main ($) { Line 4308  sub _tree_construction_main ($) {
4308                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4309                  !!!cp ('t112');                  !!!cp ('t112');
4310                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4311                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4312                        [$self->{head_element}, $el_category->{head}];
4313                } else {                } else {
4314                  !!!cp ('t113');                  !!!cp ('t113');
4315                }                }
# Line 3610  sub _tree_construction_main ($) { Line 4320  sub _tree_construction_main ($) {
4320                $parse_rcdata->(RCDATA_CONTENT_MODEL);                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4321                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4322                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4323                redo B;                next B;
4324              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style') {
4325                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4326                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
# Line 3618  sub _tree_construction_main ($) { Line 4328  sub _tree_construction_main ($) {
4328                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4329                  !!!cp ('t114');                  !!!cp ('t114');
4330                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4331                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4332                        [$self->{head_element}, $el_category->{head}];
4333                } else {                } else {
4334                  !!!cp ('t115');                  !!!cp ('t115');
4335                }                }
4336                $parse_rcdata->(CDATA_CONTENT_MODEL);                $parse_rcdata->(CDATA_CONTENT_MODEL);
4337                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4338                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4339                redo B;                next B;
4340              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4341                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
4342                  !!!cp ('t116');                  !!!cp ('t116');
4343                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
4344                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4345                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4346                    !!!nack ('t116.1');
4347                  !!!next-token;                  !!!next-token;
4348                  redo B;                  next B;
4349                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4350                  !!!cp ('t117');                  !!!cp ('t117');
4351                  !!!parse-error (type => 'in noscript:noscript', token => $token);                  !!!parse-error (type => 'in noscript:noscript', token => $token);
4352                  ## Ignore the token                  ## Ignore the token
4353                    !!!nack ('t117.1');
4354                  !!!next-token;                  !!!next-token;
4355                  redo B;                  next B;
4356                } else {                } else {
4357                  !!!cp ('t118');                  !!!cp ('t118');
4358                  #                  #
# Line 3656  sub _tree_construction_main ($) { Line 4369  sub _tree_construction_main ($) {
4369                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4370                  !!!cp ('t120');                  !!!cp ('t120');
4371                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4372                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4373                        [$self->{head_element}, $el_category->{head}];
4374                } else {                } else {
4375                  !!!cp ('t121');                  !!!cp ('t121');
4376                }                }
# Line 3665  sub _tree_construction_main ($) { Line 4379  sub _tree_construction_main ($) {
4379                $script_start_tag->();                $script_start_tag->();
4380                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4381                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4382                redo B;                next B;
4383              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
4384                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
4385                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
# Line 3699  sub _tree_construction_main ($) { Line 4413  sub _tree_construction_main ($) {
4413                } else {                } else {
4414                  die "$0: tag name: $self->{tag_name}";                  die "$0: tag name: $self->{tag_name}";
4415                }                }
4416                  !!!nack ('t127.1');
4417                !!!next-token;                !!!next-token;
4418                redo B;                next B;
4419              } else {              } else {
4420                !!!cp ('t128');                !!!cp ('t128');
4421                #                #
# Line 3732  sub _tree_construction_main ($) { Line 4447  sub _tree_construction_main ($) {
4447              !!!insert-element ('body',, $token);              !!!insert-element ('body',, $token);
4448              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4449              ## reprocess              ## reprocess
4450              redo B;              !!!ack-later;
4451                next B;
4452            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
4453              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
4454                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4455                  !!!cp ('t132');                  !!!cp ('t132');
4456                  ## As if <head>                  ## As if <head>
4457                  !!!create-element ($self->{head_element}, 'head',, $token);                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4458                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4459                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4460                        [$self->{head_element}, $el_category->{head}];
4461    
4462                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4463                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4464                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4465                  !!!next-token;                  !!!next-token;
4466                  redo B;                  next B;
4467                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4468                  !!!cp ('t133');                  !!!cp ('t133');
4469                  ## As if </noscript>                  ## As if </noscript>
# Line 3757  sub _tree_construction_main ($) { Line 4474  sub _tree_construction_main ($) {
4474                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4475                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4476                  !!!next-token;                  !!!next-token;
4477                  redo B;                  next B;
4478                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4479                  !!!cp ('t134');                  !!!cp ('t134');
4480                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4481                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4482                  !!!next-token;                  !!!next-token;
4483                  redo B;                  next B;
4484                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4485                    !!!cp ('t134.1');
4486                    !!!parse-error (type => 'unmatched end tag:head', token => $token);
4487                    ## Ignore the token
4488                    !!!next-token;
4489                    next B;
4490                } else {                } else {
4491                  !!!cp ('t135');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
                 #  
4492                }                }
4493              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4494                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
# Line 3774  sub _tree_construction_main ($) { Line 4496  sub _tree_construction_main ($) {
4496                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4497                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4498                  !!!next-token;                  !!!next-token;
4499                  redo B;                  next B;
4500                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
4501                           $self->{insertion_mode} == AFTER_HEAD_IM) {
4502                  !!!cp ('t137');                  !!!cp ('t137');
4503                  !!!parse-error (type => 'unmatched end tag:noscript', token => $token);                  !!!parse-error (type => 'unmatched end tag:noscript', token => $token);
4504                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
4505                  !!!next-token;                  !!!next-token;
4506                  redo B;                  next B;
4507                } else {                } else {
4508                  !!!cp ('t138');                  !!!cp ('t138');
4509                  #                  #
# Line 3788  sub _tree_construction_main ($) { Line 4511  sub _tree_construction_main ($) {
4511              } elsif ({              } elsif ({
4512                        body => 1, html => 1,                        body => 1, html => 1,
4513                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4514                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM or
4515                  !!!cp ('t139');                    $self->{insertion_mode} == IN_HEAD_IM or
4516                  ## 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) {  
4517                  !!!cp ('t140');                  !!!cp ('t140');
4518                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4519                  ## Ignore the token                  ## Ignore the token
4520                  !!!next-token;                  !!!next-token;
4521                  redo B;                  next B;
4522                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4523                    !!!cp ('t140.1');
4524                    !!!parse-error (type => 'unmatched end tag:' . $token->{tag_name}, token => $token);
4525                    ## Ignore the token
4526                    !!!next-token;
4527                    next B;
4528                } else {                } else {
4529                  !!!cp ('t141');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
4530                }                }
4531                              } elsif ($token->{tag_name} eq 'p') {
4532                #                !!!cp ('t142');
4533              } elsif ({                !!!parse-error (type => 'unmatched end tag:p', token => $token);
4534                        p => 1, br => 1,                ## Ignore the token
4535                       }->{$token->{tag_name}}) {                !!!next-token;
4536                  next B;
4537                } elsif ($token->{tag_name} eq 'br') {
4538                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4539                  !!!cp ('t142');                  !!!cp ('t142.2');
4540                  ## As if <head>                  ## (before head) as if <head>, (in head) as if </head>
4541                  !!!create-element ($self->{head_element}, 'head',, $token);                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4542                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4543                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  $self->{insertion_mode} = AFTER_HEAD_IM;
4544      
4545                    ## Reprocess in the "after head" insertion mode...
4546                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4547                    !!!cp ('t143.2');
4548                    ## As if </head>
4549                    pop @{$self->{open_elements}};
4550                    $self->{insertion_mode} = AFTER_HEAD_IM;
4551      
4552                    ## Reprocess in the "after head" insertion mode...
4553                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4554                    !!!cp ('t143.3');
4555                    ## ISSUE: Two parse errors for <head><noscript></br>
4556                    !!!parse-error (type => 'unmatched end tag:br', token => $token);
4557                    ## As if </noscript>
4558                    pop @{$self->{open_elements}};
4559                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4560    
4561                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4562                } else {                  ## As if </head>
4563                  !!!cp ('t143');                  pop @{$self->{open_elements}};
4564                }                  $self->{insertion_mode} = AFTER_HEAD_IM;
4565    
4566                #                  ## Reprocess in the "after head" insertion mode...
4567              } else {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4568                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                  !!!cp ('t143.4');
                 !!!cp ('t144');  
4569                  #                  #
4570                } else {                } else {
4571                  !!!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;  
4572                }                }
4573    
4574                  ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
4575                  !!!parse-error (type => 'unmatched end tag:br', token => $token);
4576                  ## Ignore the token
4577                  !!!next-token;
4578                  next B;
4579                } else {
4580                  !!!cp ('t145');
4581                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4582                  ## Ignore the token
4583                  !!!next-token;
4584                  next B;
4585              }              }
4586    
4587              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
# Line 3861  sub _tree_construction_main ($) { Line 4607  sub _tree_construction_main ($) {
4607                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4608                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
4609                !!!next-token;                !!!next-token;
4610                redo B;                next B;
4611              } else {              } else {
4612                !!!cp ('t149');                !!!cp ('t149');
4613              }              }
# Line 3871  sub _tree_construction_main ($) { Line 4617  sub _tree_construction_main ($) {
4617              !!!insert-element ('body',, $token);              !!!insert-element ('body',, $token);
4618              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4619              ## reprocess              ## reprocess
4620              redo B;              next B;
4621        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4622          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4623            !!!cp ('t149.1');            !!!cp ('t149.1');
4624    
4625            ## NOTE: As if <head>            ## NOTE: As if <head>
4626            !!!create-element ($self->{head_element}, 'head',, $token);            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4627            $self->{open_elements}->[-1]->[0]->append_child            $self->{open_elements}->[-1]->[0]->append_child
4628                ($self->{head_element});                ($self->{head_element});
4629            #push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            #push @{$self->{open_elements}},
4630              #    [$self->{head_element}, $el_category->{head}];
4631            #$self->{insertion_mode} = IN_HEAD_IM;            #$self->{insertion_mode} = IN_HEAD_IM;
4632            ## NOTE: Reprocess.            ## NOTE: Reprocess.
4633    
# Line 3924  sub _tree_construction_main ($) { Line 4671  sub _tree_construction_main ($) {
4671          !!!insert-element ('body',, $token);          !!!insert-element ('body',, $token);
4672          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4673          ## NOTE: Reprocess.          ## NOTE: Reprocess.
4674          redo B;          next B;
4675        } else {        } else {
4676          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
4677        }        }
# Line 3939  sub _tree_construction_main ($) { Line 4686  sub _tree_construction_main ($) {
4686              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4687    
4688              !!!next-token;              !!!next-token;
4689              redo B;              next B;
4690            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
4691              if ({              if ({
4692                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
# Line 3949  sub _tree_construction_main ($) { Line 4696  sub _tree_construction_main ($) {
4696                  ## have an element in table scope                  ## have an element in table scope
4697                  for (reverse 0..$#{$self->{open_elements}}) {                  for (reverse 0..$#{$self->{open_elements}}) {
4698                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4699                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] & TABLE_CELL_EL) {
4700                      !!!cp ('t151');                      !!!cp ('t151');
4701    
4702                      ## Close the cell                      ## Close the cell
4703                      !!!back-token; # <?>                      !!!back-token; # <x>
4704                      $token = {type => END_TAG_TOKEN, tag_name => $node->[1],                      $token = {type => END_TAG_TOKEN,
4705                                  tag_name => $node->[0]->manakai_local_name,
4706                                line => $token->{line},                                line => $token->{line},
4707                                column => $token->{column}};                                column => $token->{column}};
4708                      redo B;                      next B;
4709                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
4710                      !!!cp ('t152');                      !!!cp ('t152');
4711                      ## ISSUE: This case can never be reached, maybe.                      ## ISSUE: This case can never be reached, maybe.
4712                      last;                      last;
# Line 3971  sub _tree_construction_main ($) { Line 4717  sub _tree_construction_main ($) {
4717                  !!!parse-error (type => 'start tag not allowed',                  !!!parse-error (type => 'start tag not allowed',
4718                      value => $token->{tag_name}, token => $token);                      value => $token->{tag_name}, token => $token);
4719                  ## Ignore the token                  ## Ignore the token
4720                    !!!nack ('t153.1');
4721                  !!!next-token;                  !!!next-token;
4722                  redo B;                  next B;
4723                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4724                  !!!parse-error (type => 'not closed:caption', token => $token);                  !!!parse-error (type => 'not closed:caption', token => $token);
4725                                    
# Line 3982  sub _tree_construction_main ($) { Line 4729  sub _tree_construction_main ($) {
4729                  INSCOPE: {                  INSCOPE: {
4730                    for (reverse 0..$#{$self->{open_elements}}) {                    for (reverse 0..$#{$self->{open_elements}}) {
4731                      my $node = $self->{open_elements}->[$_];                      my $node = $self->{open_elements}->[$_];
4732                      if ($node->[1] eq 'caption') {                      if ($node->[1] & CAPTION_EL) {
4733                        !!!cp ('t155');                        !!!cp ('t155');
4734                        $i = $_;                        $i = $_;
4735                        last INSCOPE;                        last INSCOPE;
4736                      } elsif ({                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
                               table => 1, html => 1,  
                              }->{$node->[1]}) {  
4737                        !!!cp ('t156');                        !!!cp ('t156');
4738                        last;                        last;
4739                      }                      }
# Line 3998  sub _tree_construction_main ($) { Line 4743  sub _tree_construction_main ($) {
4743                    !!!parse-error (type => 'start tag not allowed',                    !!!parse-error (type => 'start tag not allowed',
4744                                    value => $token->{tag_name}, token => $token);                                    value => $token->{tag_name}, token => $token);
4745                    ## Ignore the token                    ## Ignore the token
4746                      !!!nack ('t157.1');
4747                    !!!next-token;                    !!!next-token;
4748                    redo B;                    next B;
4749                  } # INSCOPE                  } # INSCOPE
4750                                    
4751                  ## generate implied end tags                  ## generate implied end tags
4752                  while ({                  while ($self->{open_elements}->[-1]->[1]
4753                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4754                    !!!cp ('t158');                    !!!cp ('t158');
4755                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4756                  }                  }
4757    
4758                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4759                    !!!cp ('t159');                    !!!cp ('t159');
4760                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                    !!!parse-error (type => 'not closed',
4761                                      value => $self->{open_elements}->[-1]->[0]
4762                                          ->manakai_local_name,
4763                                      token => $token);
4764                  } else {                  } else {
4765                    !!!cp ('t160');                    !!!cp ('t160');
4766                  }                  }
# Line 4024  sub _tree_construction_main ($) { Line 4772  sub _tree_construction_main ($) {
4772                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4773                                    
4774                  ## reprocess                  ## reprocess
4775                  redo B;                  !!!ack-later;
4776                    next B;
4777                } else {                } else {
4778                  !!!cp ('t161');                  !!!cp ('t161');
4779                  #                  #
# Line 4040  sub _tree_construction_main ($) { Line 4789  sub _tree_construction_main ($) {
4789                  my $i;                  my $i;
4790                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4791                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4792                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4793                      !!!cp ('t163');                      !!!cp ('t163');
4794                      $i = $_;                      $i = $_;
4795                      last INSCOPE;                      last INSCOPE;
4796                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
4797                      !!!cp ('t164');                      !!!cp ('t164');
4798                      last INSCOPE;                      last INSCOPE;
4799                    }                    }
# Line 4056  sub _tree_construction_main ($) { Line 4803  sub _tree_construction_main ($) {
4803                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4804                      ## Ignore the token                      ## Ignore the token
4805                      !!!next-token;                      !!!next-token;
4806                      redo B;                      next B;
4807                    }                    }
4808                                    
4809                  ## generate implied end tags                  ## generate implied end tags
4810                  while ({                  while ($self->{open_elements}->[-1]->[1]
4811                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4812                    !!!cp ('t166');                    !!!cp ('t166');
4813                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4814                  }                  }
4815    
4816                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
4817                            ne $token->{tag_name}) {
4818                    !!!cp ('t167');                    !!!cp ('t167');
4819                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                    !!!parse-error (type => 'not closed',
4820                                      value => $self->{open_elements}->[-1]->[0]
4821                                          ->manakai_local_name,
4822                                      token => $token);
4823                  } else {                  } else {
4824                    !!!cp ('t168');                    !!!cp ('t168');
4825                  }                  }
# Line 4081  sub _tree_construction_main ($) { Line 4831  sub _tree_construction_main ($) {
4831                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
4832                                    
4833                  !!!next-token;                  !!!next-token;
4834                  redo B;                  next B;
4835                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4836                  !!!cp ('t169');                  !!!cp ('t169');
4837                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4838                  ## Ignore the token                  ## Ignore the token
4839                  !!!next-token;                  !!!next-token;
4840                  redo B;                  next B;
4841                } else {                } else {
4842                  !!!cp ('t170');                  !!!cp ('t170');
4843                  #                  #
# Line 4099  sub _tree_construction_main ($) { Line 4849  sub _tree_construction_main ($) {
4849                  INSCOPE: {                  INSCOPE: {
4850                    for (reverse 0..$#{$self->{open_elements}}) {                    for (reverse 0..$#{$self->{open_elements}}) {
4851                      my $node = $self->{open_elements}->[$_];                      my $node = $self->{open_elements}->[$_];
4852                      if ($node->[1] eq $token->{tag_name}) {                      if ($node->[1] & CAPTION_EL) {
4853                        !!!cp ('t171');                        !!!cp ('t171');
4854                        $i = $_;                        $i = $_;
4855                        last INSCOPE;                        last INSCOPE;
4856                      } elsif ({                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
                               table => 1, html => 1,  
                              }->{$node->[1]}) {  
4857                        !!!cp ('t172');                        !!!cp ('t172');
4858                        last;                        last;
4859                      }                      }
# Line 4116  sub _tree_construction_main ($) { Line 4864  sub _tree_construction_main ($) {
4864                                    value => $token->{tag_name}, token => $token);                                    value => $token->{tag_name}, token => $token);
4865                    ## Ignore the token                    ## Ignore the token
4866                    !!!next-token;                    !!!next-token;
4867                    redo B;                    next B;
4868                  } # INSCOPE                  } # INSCOPE
4869                                    
4870                  ## generate implied end tags                  ## generate implied end tags
4871                  while ({                  while ($self->{open_elements}->[-1]->[1]
4872                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4873                    !!!cp ('t174');                    !!!cp ('t174');
4874                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4875                  }                  }
4876                                    
4877                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4878                    !!!cp ('t175');                    !!!cp ('t175');
4879                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                    !!!parse-error (type => 'not closed',
4880                                      value => $self->{open_elements}->[-1]->[0]
4881                                          ->manakai_local_name,
4882                                      token => $token);
4883                  } else {                  } else {
4884                    !!!cp ('t176');                    !!!cp ('t176');
4885                  }                  }
# Line 4141  sub _tree_construction_main ($) { Line 4891  sub _tree_construction_main ($) {
4891                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4892                                    
4893                  !!!next-token;                  !!!next-token;
4894                  redo B;                  next B;
4895                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
4896                  !!!cp ('t177');                  !!!cp ('t177');
4897                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4898                  ## Ignore the token                  ## Ignore the token
4899                  !!!next-token;                  !!!next-token;
4900                  redo B;                  next B;
4901                } else {                } else {
4902                  !!!cp ('t178');                  !!!cp ('t178');
4903                  #                  #
# Line 4163  sub _tree_construction_main ($) { Line 4913  sub _tree_construction_main ($) {
4913                INSCOPE: {                INSCOPE: {
4914                  for (reverse 0..$#{$self->{open_elements}}) {                  for (reverse 0..$#{$self->{open_elements}}) {
4915                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4916                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4917                      !!!cp ('t179');                      !!!cp ('t179');
4918                      $i = $_;                      $i = $_;
4919    
4920                      ## Close the cell                      ## Close the cell
4921                      !!!back-token; # </?>                      !!!back-token; # </x>
4922                      $token = {type => END_TAG_TOKEN, tag_name => $tn,                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
4923                                line => $token->{line},                                line => $token->{line},
4924                                column => $token->{column}};                                column => $token->{column}};
4925                      redo B;                      next B;
4926                    } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {                    } elsif ($node->[1] & TABLE_CELL_EL) {
4927                      !!!cp ('t180');                      !!!cp ('t180');
4928                      $tn = $node->[1];                      $tn = $node->[0]->manakai_local_name;
4929                      ## NOTE: There is exactly one |td| or |th| element                      ## NOTE: There is exactly one |td| or |th| element
4930                      ## in scope in the stack of open elements by definition.                      ## in scope in the stack of open elements by definition.
4931                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
4932                      ## ISSUE: Can this be reached?                      ## ISSUE: Can this be reached?
4933                      !!!cp ('t181');                      !!!cp ('t181');
4934                      last;                      last;
# Line 4192  sub _tree_construction_main ($) { Line 4940  sub _tree_construction_main ($) {
4940                      value => $token->{tag_name}, token => $token);                      value => $token->{tag_name}, token => $token);
4941                  ## Ignore the token                  ## Ignore the token
4942                  !!!next-token;                  !!!next-token;
4943                  redo B;                  next B;
4944                } # INSCOPE                } # INSCOPE
4945              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
4946                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
# Line 4203  sub _tree_construction_main ($) { Line 4951  sub _tree_construction_main ($) {
4951                my $i;                my $i;
4952                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4953                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4954                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
4955                    !!!cp ('t184');                    !!!cp ('t184');
4956                    $i = $_;                    $i = $_;
4957                    last INSCOPE;                    last INSCOPE;
4958                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
4959                    !!!cp ('t185');                    !!!cp ('t185');
4960                    last INSCOPE;                    last INSCOPE;
4961                  }                  }
# Line 4219  sub _tree_construction_main ($) { Line 4965  sub _tree_construction_main ($) {
4965                  !!!parse-error (type => 'unmatched end tag:caption', token => $token);                  !!!parse-error (type => 'unmatched end tag:caption', token => $token);
4966                  ## Ignore the token                  ## Ignore the token
4967                  !!!next-token;                  !!!next-token;
4968                  redo B;                  next B;
4969                }                }
4970                                
4971                ## generate implied end tags                ## generate implied end tags
4972                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
4973                  !!!cp ('t187');                  !!!cp ('t187');
4974                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4975                }                }
4976    
4977                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4978                  !!!cp ('t188');                  !!!cp ('t188');
4979                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                  !!!parse-error (type => 'not closed',
4980                                    value => $self->{open_elements}->[-1]->[0]
4981                                        ->manakai_local_name,
4982                                    token => $token);
4983                } else {                } else {
4984                  !!!cp ('t189');                  !!!cp ('t189');
4985                }                }
# Line 4244  sub _tree_construction_main ($) { Line 4991  sub _tree_construction_main ($) {
4991                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
4992    
4993                ## reprocess                ## reprocess
4994                redo B;                next B;
4995              } elsif ({              } elsif ({
4996                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
4997                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
# Line 4253  sub _tree_construction_main ($) { Line 5000  sub _tree_construction_main ($) {
5000                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5001                  ## Ignore the token                  ## Ignore the token
5002                  !!!next-token;                  !!!next-token;
5003                  redo B;                  next B;
5004                } else {                } else {
5005                  !!!cp ('t191');                  !!!cp ('t191');
5006                  #                  #
# Line 4267  sub _tree_construction_main ($) { Line 5014  sub _tree_construction_main ($) {
5014                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5015                ## Ignore the token                ## Ignore the token
5016                !!!next-token;                !!!next-token;
5017                redo B;                next B;
5018              } else {              } else {
5019                !!!cp ('t193');                !!!cp ('t193');
5020                #                #
5021              }              }
5022        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5023          for my $entry (@{$self->{open_elements}}) {          for my $entry (@{$self->{open_elements}}) {
5024            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]}) {  
5025              !!!cp ('t75');              !!!cp ('t75');
5026              !!!parse-error (type => 'in body:#eof', token => $token);              !!!parse-error (type => 'in body:#eof', token => $token);
5027              last;              last;
# Line 4301  sub _tree_construction_main ($) { Line 5045  sub _tree_construction_main ($) {
5045            unless (length $token->{data}) {            unless (length $token->{data}) {
5046              !!!cp ('t194');              !!!cp ('t194');
5047              !!!next-token;              !!!next-token;
5048              redo B;              next B;
5049            } else {            } else {
5050              !!!cp ('t195');              !!!cp ('t195');
5051            }            }
# Line 4315  sub _tree_construction_main ($) { Line 5059  sub _tree_construction_main ($) {
5059              ## result in a new Text node.              ## result in a new Text node.
5060              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
5061                            
5062              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]}) {  
5063                # MUST                # MUST
5064                my $foster_parent_element;                my $foster_parent_element;
5065                my $next_sibling;                my $next_sibling;
5066                my $prev_sibling;                my $prev_sibling;
5067                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
5068                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
5069                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
5070                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
5071                      !!!cp ('t196');                      !!!cp ('t196');
# Line 4359  sub _tree_construction_main ($) { Line 5100  sub _tree_construction_main ($) {
5100          }          }
5101                            
5102          !!!next-token;          !!!next-token;
5103          redo B;          next B;
5104        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5105              if ({              if ({
5106                   tr => ($self->{insertion_mode} != IN_ROW_IM),                   tr => ($self->{insertion_mode} != IN_ROW_IM),
# Line 4367  sub _tree_construction_main ($) { Line 5108  sub _tree_construction_main ($) {
5108                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
5109                if ($self->{insertion_mode} == IN_TABLE_IM) {                if ($self->{insertion_mode} == IN_TABLE_IM) {
5110                  ## Clear back to table context                  ## Clear back to table context
5111                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5112                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5113                    !!!cp ('t201');                    !!!cp ('t201');
5114                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5115                  }                  }
# Line 4385  sub _tree_construction_main ($) { Line 5126  sub _tree_construction_main ($) {
5126                  }                  }
5127                                    
5128                  ## Clear back to table body context                  ## Clear back to table body context
5129                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5130                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5131                    !!!cp ('t203');                    !!!cp ('t203');
5132                    ## ISSUE: Can this case be reached?                    ## ISSUE: Can this case be reached?
5133                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4397  sub _tree_construction_main ($) { Line 5137  sub _tree_construction_main ($) {
5137                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5138                    !!!cp ('t204');                    !!!cp ('t204');
5139                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5140                      !!!nack ('t204');
5141                    !!!next-token;                    !!!next-token;
5142                    redo B;                    next B;
5143                  } else {                  } else {
5144                    !!!cp ('t205');                    !!!cp ('t205');
5145                    !!!insert-element ('tr',, $token);                    !!!insert-element ('tr',, $token);
# Line 4409  sub _tree_construction_main ($) { Line 5150  sub _tree_construction_main ($) {
5150                }                }
5151    
5152                ## Clear back to table row context                ## Clear back to table row context
5153                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5154                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5155                  !!!cp ('t207');                  !!!cp ('t207');
5156                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5157                }                }
# Line 4421  sub _tree_construction_main ($) { Line 5161  sub _tree_construction_main ($) {
5161    
5162                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
5163                                
5164                  !!!nack ('t207.1');
5165                !!!next-token;                !!!next-token;
5166                redo B;                next B;
5167              } elsif ({              } elsif ({
5168                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5169                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
# Line 4434  sub _tree_construction_main ($) { Line 5175  sub _tree_construction_main ($) {
5175                  my $i;                  my $i;
5176                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5177                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5178                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5179                      !!!cp ('t208');                      !!!cp ('t208');
5180                      $i = $_;                      $i = $_;
5181                      last INSCOPE;                      last INSCOPE;
5182                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             html => 1,  
   
                             ## NOTE: This element does not appear here, maybe.  
                             table => 1,  
                            }->{$node->[1]}) {  
5183                      !!!cp ('t209');                      !!!cp ('t209');
5184                      last INSCOPE;                      last INSCOPE;
5185                    }                    }
5186                  } # INSCOPE                  } # INSCOPE
5187                  unless (defined $i) {                  unless (defined $i) {
5188                   !!!cp ('t210');                    !!!cp ('t210');
5189  ## TODO: This type is wrong.  ## TODO: This type is wrong.
5190                   !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);                    !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);
5191                    ## Ignore the token                    ## Ignore the token
5192                      !!!nack ('t210.1');
5193                    !!!next-token;                    !!!next-token;
5194                    redo B;                    next B;
5195                  }                  }
5196                                    
5197                  ## Clear back to table row context                  ## Clear back to table row context
5198                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5199                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5200                    !!!cp ('t211');                    !!!cp ('t211');
5201                    ## ISSUE: Can this case be reached?                    ## ISSUE: Can this case be reached?
5202                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4471  sub _tree_construction_main ($) { Line 5207  sub _tree_construction_main ($) {
5207                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5208                    !!!cp ('t212');                    !!!cp ('t212');
5209                    ## reprocess                    ## reprocess
5210                    redo B;                    !!!ack-later;
5211                      next B;
5212                  } else {                  } else {
5213                    !!!cp ('t213');                    !!!cp ('t213');
5214                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
# Line 4483  sub _tree_construction_main ($) { Line 5220  sub _tree_construction_main ($) {
5220                  my $i;                  my $i;
5221                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5222                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5223                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5224                      !!!cp ('t214');                      !!!cp ('t214');
5225                      $i = $_;                      $i = $_;
5226                      last INSCOPE;                      last INSCOPE;
5227                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5228                      !!!cp ('t215');                      !!!cp ('t215');
5229                      last INSCOPE;                      last INSCOPE;
5230                    }                    }
# Line 4501  sub _tree_construction_main ($) { Line 5234  sub _tree_construction_main ($) {
5234  ## TODO: This erorr type ios wrong.  ## TODO: This erorr type ios wrong.
5235                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5236                    ## Ignore the token                    ## Ignore the token
5237                      !!!nack ('t216.1');
5238                    !!!next-token;                    !!!next-token;
5239                    redo B;                    next B;
5240                  }                  }
5241    
5242                  ## Clear back to table body context                  ## Clear back to table body context
5243                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5244                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5245                    !!!cp ('t217');                    !!!cp ('t217');
5246                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5247                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4530  sub _tree_construction_main ($) { Line 5263  sub _tree_construction_main ($) {
5263    
5264                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
5265                  ## Clear back to table context                  ## Clear back to table context
5266                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5267                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5268                    !!!cp ('t219');                    !!!cp ('t219');
5269                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5270                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4540  sub _tree_construction_main ($) { Line 5273  sub _tree_construction_main ($) {
5273                  !!!insert-element ('colgroup',, $token);                  !!!insert-element ('colgroup',, $token);
5274                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5275                  ## reprocess                  ## reprocess
5276                  redo B;                  !!!ack-later;
5277                    next B;
5278                } elsif ({                } elsif ({
5279                          caption => 1,                          caption => 1,
5280                          colgroup => 1,                          colgroup => 1,
5281                          tbody => 1, tfoot => 1, thead => 1,                          tbody => 1, tfoot => 1, thead => 1,
5282                         }->{$token->{tag_name}}) {                         }->{$token->{tag_name}}) {
5283                  ## Clear back to table context                  ## Clear back to table context
5284                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5285                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5286                    !!!cp ('t220');                    !!!cp ('t220');
5287                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5288                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4566  sub _tree_construction_main ($) { Line 5300  sub _tree_construction_main ($) {
5300                                             thead => IN_TABLE_BODY_IM,                                             thead => IN_TABLE_BODY_IM,
5301                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
5302                  !!!next-token;                  !!!next-token;
5303                  redo B;                  !!!nack ('t220.1');
5304                    next B;
5305                } else {                } else {
5306                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
5307                }                }
5308              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5309                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                !!!parse-error (type => 'not closed',
5310                                  value => $self->{open_elements}->[-1]->[0]
5311                                      ->manakai_local_name,
5312                                  token => $token);
5313    
5314                ## As if </table>                ## As if </table>
5315                ## have a table element in table scope                ## have a table element in table scope
5316                my $i;                my $i;
5317                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5318                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5319                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5320                    !!!cp ('t221');                    !!!cp ('t221');
5321                    $i = $_;                    $i = $_;
5322                    last INSCOPE;                    last INSCOPE;
5323                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           #table => 1,  
                           html => 1,  
                          }->{$node->[1]}) {  
5324                    !!!cp ('t222');                    !!!cp ('t222');
5325                    last INSCOPE;                    last INSCOPE;
5326                  }                  }
# Line 4595  sub _tree_construction_main ($) { Line 5330  sub _tree_construction_main ($) {
5330  ## TODO: The following is wrong, maybe.  ## TODO: The following is wrong, maybe.
5331                  !!!parse-error (type => 'unmatched end tag:table', token => $token);                  !!!parse-error (type => 'unmatched end tag:table', token => $token);
5332                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5333                    !!!nack ('t223.1');
5334                  !!!next-token;                  !!!next-token;
5335                  redo B;                  next B;
5336                }                }
5337                                
5338  ## TODO: Followings are removed from the latest spec.  ## TODO: Followings are removed from the latest spec.
5339                ## generate implied end tags                ## generate implied end tags
5340                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
5341                  !!!cp ('t224');                  !!!cp ('t224');
5342                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5343                }                }
5344    
5345                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5346                  !!!cp ('t225');                  !!!cp ('t225');
5347  ## ISSUE: Can this case be reached?                  ## NOTE: |<table><tr><table>|
5348                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                  !!!parse-error (type => 'not closed',
5349                                    value => $self->{open_elements}->[-1]->[0]
5350                                        ->manakai_local_name,
5351                                    token => $token);
5352                } else {                } else {
5353                  !!!cp ('t226');                  !!!cp ('t226');
5354                }                }
# Line 4621  sub _tree_construction_main ($) { Line 5358  sub _tree_construction_main ($) {
5358    
5359                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5360    
5361                ## reprocess            ## reprocess
5362                redo B;            !!!ack-later;
5363              next B;
5364          } elsif ($token->{tag_name} eq 'style') {          } elsif ($token->{tag_name} eq 'style') {
5365            if (not $open_tables->[-1]->[1]) { # tainted            if (not $open_tables->[-1]->[1]) { # tainted
5366              !!!cp ('t227.8');              !!!cp ('t227.8');
5367              ## NOTE: This is a "as if in head" code clone.              ## NOTE: This is a "as if in head" code clone.
5368              $parse_rcdata->(CDATA_CONTENT_MODEL);              $parse_rcdata->(CDATA_CONTENT_MODEL);
5369              redo B;              next B;
5370            } else {            } else {
5371              !!!cp ('t227.7');              !!!cp ('t227.7');
5372              #              #
# Line 4638  sub _tree_construction_main ($) { Line 5376  sub _tree_construction_main ($) {
5376              !!!cp ('t227.6');              !!!cp ('t227.6');
5377              ## NOTE: This is a "as if in head" code clone.              ## NOTE: This is a "as if in head" code clone.
5378              $script_start_tag->();              $script_start_tag->();
5379              redo B;              next B;
5380            } else {            } else {
5381              !!!cp ('t227.5');              !!!cp ('t227.5');
5382              #              #
# Line 4658  sub _tree_construction_main ($) { Line 5396  sub _tree_construction_main ($) {
5396                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5397    
5398                  !!!next-token;                  !!!next-token;
5399                  redo B;                  !!!ack ('t227.2.1');
5400                    next B;
5401                } else {                } else {
5402                  !!!cp ('t227.2');                  !!!cp ('t227.2');
5403                  #                  #
# Line 4687  sub _tree_construction_main ($) { Line 5426  sub _tree_construction_main ($) {
5426                my $i;                my $i;
5427                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5428                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5429                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_ROW_EL) {
5430                    !!!cp ('t228');                    !!!cp ('t228');
5431                    $i = $_;                    $i = $_;
5432                    last INSCOPE;                    last INSCOPE;
5433                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5434                    !!!cp ('t229');                    !!!cp ('t229');
5435                    last INSCOPE;                    last INSCOPE;
5436                  }                  }
# Line 4702  sub _tree_construction_main ($) { Line 5439  sub _tree_construction_main ($) {
5439                  !!!cp ('t230');                  !!!cp ('t230');
5440                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5441                  ## Ignore the token                  ## Ignore the token
5442                    !!!nack ('t230.1');
5443                  !!!next-token;                  !!!next-token;
5444                  redo B;                  next B;
5445                } else {                } else {
5446                  !!!cp ('t232');                  !!!cp ('t232');
5447                }                }
5448    
5449                ## Clear back to table row context                ## Clear back to table row context
5450                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5451                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5452                  !!!cp ('t231');                  !!!cp ('t231');
5453  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
5454                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 4720  sub _tree_construction_main ($) { Line 5457  sub _tree_construction_main ($) {
5457                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5458                $self->{insertion_mode} = IN_TABLE_BODY_IM;                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5459                !!!next-token;                !!!next-token;
5460                redo B;                !!!nack ('t231.1');
5461                  next B;
5462              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5463                if ($self->{insertion_mode} == IN_ROW_IM) {                if ($self->{insertion_mode} == IN_ROW_IM) {
5464                  ## As if </tr>                  ## As if </tr>
# Line 4728  sub _tree_construction_main ($) { Line 5466  sub _tree_construction_main ($) {
5466                  my $i;                  my $i;
5467                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5468                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5469                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5470                      !!!cp ('t233');                      !!!cp ('t233');
5471                      $i = $_;                      $i = $_;
5472                      last INSCOPE;                      last INSCOPE;
5473                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5474                      !!!cp ('t234');                      !!!cp ('t234');
5475                      last INSCOPE;                      last INSCOPE;
5476                    }                    }
# Line 4744  sub _tree_construction_main ($) { Line 5480  sub _tree_construction_main ($) {
5480  ## TODO: The following is wrong.  ## TODO: The following is wrong.
5481                    !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);                    !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);
5482                    ## Ignore the token                    ## Ignore the token
5483                      !!!nack ('t236.1');
5484                    !!!next-token;                    !!!next-token;
5485                    redo B;                    next B;
5486                  }                  }
5487                                    
5488                  ## Clear back to table row context                  ## Clear back to table row context
5489                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5490                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5491                    !!!cp ('t236');                    !!!cp ('t236');
5492  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
5493                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4767  sub _tree_construction_main ($) { Line 5503  sub _tree_construction_main ($) {
5503                  my $i;                  my $i;
5504                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5505                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5506                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5507                      !!!cp ('t237');                      !!!cp ('t237');
5508                      $i = $_;                      $i = $_;
5509                      last INSCOPE;                      last INSCOPE;
5510                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5511                      !!!cp ('t238');                      !!!cp ('t238');
5512                      last INSCOPE;                      last INSCOPE;
5513                    }                    }
# Line 4784  sub _tree_construction_main ($) { Line 5516  sub _tree_construction_main ($) {
5516                    !!!cp ('t239');                    !!!cp ('t239');
5517                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5518                    ## Ignore the token                    ## Ignore the token
5519                      !!!nack ('t239.1');
5520                    !!!next-token;                    !!!next-token;
5521                    redo B;                    next B;
5522                  }                  }
5523                                    
5524                  ## Clear back to table body context                  ## Clear back to table body context
5525                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5526                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5527                    !!!cp ('t240');                    !!!cp ('t240');
5528                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5529                  }                  }
# Line 4817  sub _tree_construction_main ($) { Line 5549  sub _tree_construction_main ($) {
5549                my $i;                my $i;
5550                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5551                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5552                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_EL) {
5553                    !!!cp ('t241');                    !!!cp ('t241');
5554                    $i = $_;                    $i = $_;
5555                    last INSCOPE;                    last INSCOPE;
5556                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5557                    !!!cp ('t242');                    !!!cp ('t242');
5558                    last INSCOPE;                    last INSCOPE;
5559                  }                  }
# Line 4832  sub _tree_construction_main ($) { Line 5562  sub _tree_construction_main ($) {
5562                  !!!cp ('t243');                  !!!cp ('t243');
5563                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5564                  ## Ignore the token                  ## Ignore the token
5565                    !!!nack ('t243.1');
5566                  !!!next-token;                  !!!next-token;
5567                  redo B;                  next B;
5568                }                }
5569                                    
5570                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
# Line 4842  sub _tree_construction_main ($) { Line 5573  sub _tree_construction_main ($) {
5573                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5574                                
5575                !!!next-token;                !!!next-token;
5576                redo B;                next B;
5577              } elsif ({              } elsif ({
5578                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5579                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
# Line 4852  sub _tree_construction_main ($) { Line 5583  sub _tree_construction_main ($) {
5583                  my $i;                  my $i;
5584                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5585                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5586                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5587                      !!!cp ('t247');                      !!!cp ('t247');
5588                      $i = $_;                      $i = $_;
5589                      last INSCOPE;                      last INSCOPE;
5590                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5591                      !!!cp ('t248');                      !!!cp ('t248');
5592                      last INSCOPE;                      last INSCOPE;
5593                    }                    }
# Line 4867  sub _tree_construction_main ($) { Line 5596  sub _tree_construction_main ($) {
5596                      !!!cp ('t249');                      !!!cp ('t249');
5597                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5598                      ## Ignore the token                      ## Ignore the token
5599                        !!!nack ('t249.1');
5600                      !!!next-token;                      !!!next-token;
5601                      redo B;                      next B;
5602                    }                    }
5603                                    
5604                  ## As if </tr>                  ## As if </tr>
# Line 4876  sub _tree_construction_main ($) { Line 5606  sub _tree_construction_main ($) {
5606                  my $i;                  my $i;
5607                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5608                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5609                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5610                      !!!cp ('t250');                      !!!cp ('t250');
5611                      $i = $_;                      $i = $_;
5612                      last INSCOPE;                      last INSCOPE;
5613                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5614                      !!!cp ('t251');                      !!!cp ('t251');
5615                      last INSCOPE;                      last INSCOPE;
5616                    }                    }
# Line 4891  sub _tree_construction_main ($) { Line 5619  sub _tree_construction_main ($) {
5619                      !!!cp ('t252');                      !!!cp ('t252');
5620                      !!!parse-error (type => 'unmatched end tag:tr', token => $token);                      !!!parse-error (type => 'unmatched end tag:tr', token => $token);
5621                      ## Ignore the token                      ## Ignore the token
5622                        !!!nack ('t252.1');
5623                      !!!next-token;                      !!!next-token;
5624                      redo B;                      next B;
5625                    }                    }
5626                                    
5627                  ## Clear back to table row context                  ## Clear back to table row context
5628                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5629                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5630                    !!!cp ('t253');                    !!!cp ('t253');
5631  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
5632                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4913  sub _tree_construction_main ($) { Line 5641  sub _tree_construction_main ($) {
5641                my $i;                my $i;
5642                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5643                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5644                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5645                    !!!cp ('t254');                    !!!cp ('t254');
5646                    $i = $_;                    $i = $_;
5647                    last INSCOPE;                    last INSCOPE;
5648                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5649                    !!!cp ('t255');                    !!!cp ('t255');
5650                    last INSCOPE;                    last INSCOPE;
5651                  }                  }
# Line 4928  sub _tree_construction_main ($) { Line 5654  sub _tree_construction_main ($) {
5654                  !!!cp ('t256');                  !!!cp ('t256');
5655                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5656                  ## Ignore the token                  ## Ignore the token
5657                    !!!nack ('t256.1');
5658                  !!!next-token;                  !!!next-token;
5659                  redo B;                  next B;
5660                }                }
5661    
5662                ## Clear back to table body context                ## Clear back to table body context
5663                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5664                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5665                  !!!cp ('t257');                  !!!cp ('t257');
5666  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
5667                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 4943  sub _tree_construction_main ($) { Line 5669  sub _tree_construction_main ($) {
5669    
5670                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5671                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5672                  !!!nack ('t257.1');
5673                !!!next-token;                !!!next-token;
5674                redo B;                next B;
5675              } elsif ({              } elsif ({
5676                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
5677                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
5678                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5679                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
5680                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5681                !!!cp ('t258');            !!!cp ('t258');
5682                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5683                ## Ignore the token            ## Ignore the token
5684                !!!next-token;            !!!nack ('t258.1');
5685                redo B;             !!!next-token;
5686              next B;
5687          } else {          } else {
5688            !!!cp ('t259');            !!!cp ('t259');
5689            !!!parse-error (type => 'in table:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'in table:/'.$token->{tag_name}, token => $token);
# Line 4964  sub _tree_construction_main ($) { Line 5692  sub _tree_construction_main ($) {
5692            #            #
5693          }          }
5694        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5695          unless ($self->{open_elements}->[-1]->[1] eq 'html' and          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5696                  @{$self->{open_elements}} == 1) { # redundant, maybe                  @{$self->{open_elements}} == 1) { # redundant, maybe
5697            !!!parse-error (type => 'in body:#eof', token => $token);            !!!parse-error (type => 'in body:#eof', token => $token);
5698            !!!cp ('t259.1');            !!!cp ('t259.1');
# Line 4986  sub _tree_construction_main ($) { Line 5714  sub _tree_construction_main ($) {
5714                unless (length $token->{data}) {                unless (length $token->{data}) {
5715                  !!!cp ('t260');                  !!!cp ('t260');
5716                  !!!next-token;                  !!!next-token;
5717                  redo B;                  next B;
5718                }                }
5719              }              }
5720                            
# Line 4997  sub _tree_construction_main ($) { Line 5725  sub _tree_construction_main ($) {
5725                !!!cp ('t262');                !!!cp ('t262');
5726                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5727                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5728                  !!!ack ('t262.1');
5729                !!!next-token;                !!!next-token;
5730                redo B;                next B;
5731              } else {              } else {
5732                !!!cp ('t263');                !!!cp ('t263');
5733                #                #
5734              }              }
5735            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
5736              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
5737                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5738                  !!!cp ('t264');                  !!!cp ('t264');
5739                  !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);                  !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5740                  ## Ignore the token                  ## Ignore the token
5741                  !!!next-token;                  !!!next-token;
5742                  redo B;                  next B;
5743                } else {                } else {
5744                  !!!cp ('t265');                  !!!cp ('t265');
5745                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
5746                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5747                  !!!next-token;                  !!!next-token;
5748                  redo B;                              next B;            
5749                }                }
5750              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
5751                !!!cp ('t266');                !!!cp ('t266');
5752                !!!parse-error (type => 'unmatched end tag:col', token => $token);                !!!parse-error (type => 'unmatched end tag:col', token => $token);
5753                ## Ignore the token                ## Ignore the token
5754                !!!next-token;                !!!next-token;
5755                redo B;                next B;
5756              } else {              } else {
5757                !!!cp ('t267');                !!!cp ('t267');
5758                #                #
5759              }              }
5760        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5761          if ($self->{open_elements}->[-1]->[1] eq 'html' or          if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5762              @{$self->{open_elements}} == 1) { # redundant, maybe              @{$self->{open_elements}} == 1) { # redundant, maybe
5763            !!!cp ('t270.2');            !!!cp ('t270.2');
5764            ## Stop parsing.            ## Stop parsing.
# Line 5040  sub _tree_construction_main ($) { Line 5769  sub _tree_construction_main ($) {
5769            pop @{$self->{open_elements}}; # colgroup            pop @{$self->{open_elements}}; # colgroup
5770            $self->{insertion_mode} = IN_TABLE_IM;            $self->{insertion_mode} = IN_TABLE_IM;
5771            ## Reprocess.            ## Reprocess.
5772            redo B;            next B;
5773          }          }
5774        } else {        } else {
5775          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5776        }        }
5777    
5778            ## As if </colgroup>            ## As if </colgroup>
5779            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5780              !!!cp ('t269');              !!!cp ('t269');
5781  ## TODO: Wrong error type?  ## TODO: Wrong error type?
5782              !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);              !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5783              ## Ignore the token              ## Ignore the token
5784                !!!nack ('t269.1');
5785              !!!next-token;              !!!next-token;
5786              redo B;              next B;
5787            } else {            } else {
5788              !!!cp ('t270');              !!!cp ('t270');
5789              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
5790              $self->{insertion_mode} = IN_TABLE_IM;              $self->{insertion_mode} = IN_TABLE_IM;
5791                !!!ack-later;
5792              ## reprocess              ## reprocess
5793              redo B;              next B;
5794            }            }
5795      } elsif ($self->{insertion_mode} & SELECT_IMS) {      } elsif ($self->{insertion_mode} & SELECT_IMS) {
5796        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5797          !!!cp ('t271');          !!!cp ('t271');
5798          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5799          !!!next-token;          !!!next-token;
5800          redo B;          next B;
5801        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5802              if ($token->{tag_name} eq 'option') {          if ($token->{tag_name} eq 'option') {
5803                if ($self->{open_elements}->[-1]->[1] eq 'option') {            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5804                  !!!cp ('t272');              !!!cp ('t272');
5805                  ## As if </option>              ## As if </option>
5806                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5807                } else {            } else {
5808                  !!!cp ('t273');              !!!cp ('t273');
5809                }            }
5810    
5811                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5812                !!!next-token;            !!!nack ('t273.1');
5813                redo B;            !!!next-token;
5814              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
5815                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
5816                  !!!cp ('t274');            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5817                  ## As if </option>              !!!cp ('t274');
5818                  pop @{$self->{open_elements}};              ## As if </option>
5819                } else {              pop @{$self->{open_elements}};
5820                  !!!cp ('t275');            } else {
5821                }              !!!cp ('t275');
5822              }
5823    
5824                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5825                  !!!cp ('t276');              !!!cp ('t276');
5826                  ## As if </optgroup>              ## As if </optgroup>
5827                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5828                } else {            } else {
5829                  !!!cp ('t277');              !!!cp ('t277');
5830                }            }
5831    
5832                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5833                !!!next-token;            !!!nack ('t277.1');
5834                redo B;            !!!next-token;
5835              next B;
5836          } elsif ($token->{tag_name} eq 'select' or          } elsif ($token->{tag_name} eq 'select' or
5837                   $token->{tag_name} eq 'input' or                   $token->{tag_name} eq 'input' or
5838                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
# Line 5112  sub _tree_construction_main ($) { Line 5845  sub _tree_construction_main ($) {
5845            !!!parse-error (type => 'not closed:select', token => $token);            !!!parse-error (type => 'not closed:select', token => $token);
5846            ## NOTE: As if the token were </select> (<select> case) or            ## NOTE: As if the token were </select> (<select> case) or
5847            ## as if there were </select> (otherwise).            ## as if there were </select> (otherwise).
5848                ## have an element in table scope            ## have an element in table scope
5849                my $i;            my $i;
5850                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5851                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
5852                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
5853                    !!!cp ('t278');                !!!cp ('t278');
5854                    $i = $_;                $i = $_;
5855                    last INSCOPE;                last INSCOPE;
5856                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5857                            table => 1, html => 1,                !!!cp ('t279');
5858                           }->{$node->[1]}) {                last INSCOPE;
5859                    !!!cp ('t279');              }
5860                    last INSCOPE;            } # INSCOPE
5861                  }            unless (defined $i) {
5862                } # INSCOPE              !!!cp ('t280');
5863                unless (defined $i) {              !!!parse-error (type => 'unmatched end tag:select', token => $token);
5864                  !!!cp ('t280');              ## Ignore the token
5865                  !!!parse-error (type => 'unmatched end tag:select', token => $token);              !!!nack ('t280.1');
5866                  ## Ignore the token              !!!next-token;
5867                  !!!next-token;              next B;
5868                  redo B;            }
               }  
5869                                
5870                !!!cp ('t281');            !!!cp ('t281');
5871                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
5872    
5873                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5874    
5875            if ($token->{tag_name} eq 'select') {            if ($token->{tag_name} eq 'select') {
5876              !!!cp ('t281.2');              !!!nack ('t281.2');
5877              !!!next-token;              !!!next-token;
5878              redo B;              next B;
5879            } else {            } else {
5880              !!!cp ('t281.1');              !!!cp ('t281.1');
5881                !!!ack-later;
5882              ## Reprocess the token.              ## Reprocess the token.
5883              redo B;              next B;
5884            }            }
5885          } else {          } else {
5886            !!!cp ('t282');            !!!cp ('t282');
5887            !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);
5888            ## Ignore the token            ## Ignore the token
5889              !!!nack ('t282.1');
5890            !!!next-token;            !!!next-token;
5891            redo B;            next B;
5892          }          }
5893        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5894              if ($token->{tag_name} eq 'optgroup') {          if ($token->{tag_name} eq 'optgroup') {
5895                if ($self->{open_elements}->[-1]->[1] eq 'option' and            if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
5896                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
5897                  !!!cp ('t283');              !!!cp ('t283');
5898                  ## As if </option>              ## As if </option>
5899                  splice @{$self->{open_elements}}, -2;              splice @{$self->{open_elements}}, -2;
5900                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5901                  !!!cp ('t284');              !!!cp ('t284');
5902                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5903                } else {            } else {
5904                  !!!cp ('t285');              !!!cp ('t285');
5905                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5906                  ## Ignore the token              ## Ignore the token
5907                }            }
5908                !!!next-token;            !!!nack ('t285.1');
5909                redo B;            !!!next-token;
5910              } elsif ($token->{tag_name} eq 'option') {            next B;
5911                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'option') {
5912                  !!!cp ('t286');            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5913                  pop @{$self->{open_elements}};              !!!cp ('t286');
5914                } else {              pop @{$self->{open_elements}};
5915                  !!!cp ('t287');            } else {
5916                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!cp ('t287');
5917                  ## Ignore the token              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5918                }              ## Ignore the token
5919                !!!next-token;            }
5920                redo B;            !!!nack ('t287.1');
5921              } elsif ($token->{tag_name} eq 'select') {            !!!next-token;
5922                ## have an element in table scope            next B;
5923                my $i;          } elsif ($token->{tag_name} eq 'select') {
5924                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            ## have an element in table scope
5925                  my $node = $self->{open_elements}->[$_];            my $i;
5926                  if ($node->[1] eq $token->{tag_name}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5927                    !!!cp ('t288');              my $node = $self->{open_elements}->[$_];
5928                    $i = $_;              if ($node->[1] & SELECT_EL) {
5929                    last INSCOPE;                !!!cp ('t288');
5930                  } elsif ({                $i = $_;
5931                            table => 1, html => 1,                last INSCOPE;
5932                           }->{$node->[1]}) {              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5933                    !!!cp ('t289');                !!!cp ('t289');
5934                    last INSCOPE;                last INSCOPE;
5935                  }              }
5936                } # INSCOPE            } # INSCOPE
5937                unless (defined $i) {            unless (defined $i) {
5938                  !!!cp ('t290');              !!!cp ('t290');
5939                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5940                  ## Ignore the token              ## Ignore the token
5941                  !!!next-token;              !!!nack ('t290.1');
5942                  redo B;              !!!next-token;
5943                }              next B;
5944              }
5945                                
5946                !!!cp ('t291');            !!!cp ('t291');
5947                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
5948    
5949                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5950    
5951                !!!next-token;            !!!nack ('t291.1');
5952                redo B;            !!!next-token;
5953              next B;
5954          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5955                   {                   {
5956                    caption => 1, table => 1, tbody => 1,                    caption => 1, table => 1, tbody => 1,
5957                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
5958                   }->{$token->{tag_name}}) {                   }->{$token->{tag_name}}) {
5959  ## TODO: The following is wrong?  ## TODO: The following is wrong?
5960                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5961                                
5962                ## have an element in table scope            ## have an element in table scope
5963                my $i;            my $i;
5964                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5965                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
5966                  if ($node->[1] eq $token->{tag_name}) {              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5967                    !!!cp ('t292');                !!!cp ('t292');
5968                    $i = $_;                $i = $_;
5969                    last INSCOPE;                last INSCOPE;
5970                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5971                            table => 1, html => 1,                !!!cp ('t293');
5972                           }->{$node->[1]}) {                last INSCOPE;
5973                    !!!cp ('t293');              }
5974                    last INSCOPE;            } # INSCOPE
5975                  }            unless (defined $i) {
5976                } # INSCOPE              !!!cp ('t294');
5977                unless (defined $i) {              ## Ignore the token
5978                  !!!cp ('t294');              !!!nack ('t294.1');
5979                  ## Ignore the token              !!!next-token;
5980                  !!!next-token;              next B;
5981                  redo B;            }
               }  
5982                                
5983                ## As if </select>            ## As if </select>
5984                ## have an element in table scope            ## have an element in table scope
5985                undef $i;            undef $i;
5986                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5987                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
5988                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
5989                    !!!cp ('t295');                !!!cp ('t295');
5990                    $i = $_;                $i = $_;
5991                    last INSCOPE;                last INSCOPE;
5992                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5993  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
5994                    !!!cp ('t296');                !!!cp ('t296');
5995                    last INSCOPE;                last INSCOPE;
5996                  }              }
5997                } # INSCOPE            } # INSCOPE
5998                unless (defined $i) {            unless (defined $i) {
5999                  !!!cp ('t297');              !!!cp ('t297');
6000  ## TODO: The following error type is correct?  ## TODO: The following error type is correct?
6001                  !!!parse-error (type => 'unmatched end tag:select', token => $token);              !!!parse-error (type => 'unmatched end tag:select', token => $token);
6002                  ## Ignore the </select> token              ## Ignore the </select> token
6003                  !!!next-token; ## TODO: ok?              !!!nack ('t297.1');
6004                  redo B;              !!!next-token; ## TODO: ok?
6005                }              next B;
6006              }
6007                                
6008                !!!cp ('t298');            !!!cp ('t298');
6009                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6010    
6011                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6012    
6013                ## reprocess            !!!ack-later;
6014                redo B;            ## reprocess
6015              next B;
6016          } else {          } else {
6017            !!!cp ('t299');            !!!cp ('t299');
6018            !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);
6019            ## Ignore the token            ## Ignore the token
6020              !!!nack ('t299.3');
6021            !!!next-token;            !!!next-token;
6022            redo B;            next B;
6023          }          }
6024        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6025          unless ($self->{open_elements}->[-1]->[1] eq 'html' and          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6026                  @{$self->{open_elements}} == 1) { # redundant, maybe                  @{$self->{open_elements}} == 1) { # redundant, maybe
6027            !!!cp ('t299.1');            !!!cp ('t299.1');
6028            !!!parse-error (type => 'in body:#eof', token => $token);            !!!parse-error (type => 'in body:#eof', token => $token);
# Line 5311  sub _tree_construction_main ($) { Line 6047  sub _tree_construction_main ($) {
6047            unless (length $token->{data}) {            unless (length $token->{data}) {
6048              !!!cp ('t300');              !!!cp ('t300');
6049              !!!next-token;              !!!next-token;
6050              redo B;              next B;
6051            }            }
6052          }          }
6053                    
# Line 5329  sub _tree_construction_main ($) { Line 6065  sub _tree_construction_main ($) {
6065    
6066          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6067          ## reprocess          ## reprocess
6068          redo B;          next B;
6069        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6070          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6071            !!!cp ('t303');            !!!cp ('t303');
# Line 5344  sub _tree_construction_main ($) { Line 6080  sub _tree_construction_main ($) {
6080          !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);
6081    
6082          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6083            !!!ack-later;
6084          ## reprocess          ## reprocess
6085          redo B;          next B;
6086        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6087          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6088            !!!cp ('t305');            !!!cp ('t305');
# Line 5364  sub _tree_construction_main ($) { Line 6101  sub _tree_construction_main ($) {
6101              !!!parse-error (type => 'unmatched end tag:html', token => $token);              !!!parse-error (type => 'unmatched end tag:html', token => $token);
6102              ## Ignore the token              ## Ignore the token
6103              !!!next-token;              !!!next-token;
6104              redo B;              next B;
6105            } else {            } else {
6106              !!!cp ('t308');              !!!cp ('t308');
6107              $self->{insertion_mode} = AFTER_HTML_BODY_IM;              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6108              !!!next-token;              !!!next-token;
6109              redo B;              next B;
6110            }            }
6111          } else {          } else {
6112            !!!cp ('t309');            !!!cp ('t309');
# Line 5377  sub _tree_construction_main ($) { Line 6114  sub _tree_construction_main ($) {
6114    
6115            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
6116            ## reprocess            ## reprocess
6117            redo B;            next B;
6118          }          }
6119        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6120          !!!cp ('t309.2');          !!!cp ('t309.2');
# Line 5394  sub _tree_construction_main ($) { Line 6131  sub _tree_construction_main ($) {
6131            unless (length $token->{data}) {            unless (length $token->{data}) {
6132              !!!cp ('t310');              !!!cp ('t310');
6133              !!!next-token;              !!!next-token;
6134              redo B;              next B;
6135            }            }
6136          }          }
6137                    
# Line 5422  sub _tree_construction_main ($) { Line 6159  sub _tree_construction_main ($) {
6159              !!!cp ('t315');              !!!cp ('t315');
6160              !!!next-token;              !!!next-token;
6161            }            }
6162            redo B;            next B;
6163          }          }
6164                    
6165          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
# Line 5441  sub _tree_construction_main ($) { Line 6178  sub _tree_construction_main ($) {
6178              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6179            !!!cp ('t318');            !!!cp ('t318');
6180            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6181              !!!nack ('t318.1');
6182            !!!next-token;            !!!next-token;
6183            redo B;            next B;
6184          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
6185                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
6186            !!!cp ('t319');            !!!cp ('t319');
6187            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6188            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6189              !!!ack ('t319.1');
6190            !!!next-token;            !!!next-token;
6191            redo B;            next B;
6192          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
6193            !!!cp ('t320');            !!!cp ('t320');
6194            ## NOTE: As if in body.            ## NOTE: As if in body.
6195            $parse_rcdata->(CDATA_CONTENT_MODEL);            $parse_rcdata->(CDATA_CONTENT_MODEL);
6196            redo B;            next B;
6197          } else {          } else {
6198            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6199              !!!cp ('t321');              !!!cp ('t321');
# Line 5464  sub _tree_construction_main ($) { Line 6203  sub _tree_construction_main ($) {
6203              !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);
6204            }            }
6205            ## Ignore the token            ## Ignore the token
6206              !!!nack ('t322.1');
6207            !!!next-token;            !!!next-token;
6208            redo B;            next B;
6209          }          }
6210        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6211          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
# Line 5480  sub _tree_construction_main ($) { Line 6220  sub _tree_construction_main ($) {
6220    
6221          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6222              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6223            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6224                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
6225              !!!cp ('t325');              !!!cp ('t325');
6226              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
# Line 5493  sub _tree_construction_main ($) { Line 6233  sub _tree_construction_main ($) {
6233            }            }
6234    
6235            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
6236                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6237              !!!cp ('t327');              !!!cp ('t327');
6238              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6239            } else {            } else {
6240              !!!cp ('t328');              !!!cp ('t328');
6241            }            }
6242            redo B;            next B;
6243          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
6244                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6245            !!!cp ('t329');            !!!cp ('t329');
6246            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6247            !!!next-token;            !!!next-token;
6248            redo B;            next B;
6249          } else {          } else {
6250            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6251              !!!cp ('t330');              !!!cp ('t330');
# Line 5516  sub _tree_construction_main ($) { Line 6256  sub _tree_construction_main ($) {
6256            }            }
6257            ## Ignore the token            ## Ignore the token
6258            !!!next-token;            !!!next-token;
6259            redo B;            next B;
6260          }          }
6261        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6262          unless ($self->{open_elements}->[-1]->[1] eq 'html' and          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6263                  @{$self->{open_elements}} == 1) { # redundant, maybe                  @{$self->{open_elements}} == 1) { # redundant, maybe
6264            !!!cp ('t331.1');            !!!cp ('t331.1');
6265            !!!parse-error (type => 'in body:#eof', token => $token);            !!!parse-error (type => 'in body:#eof', token => $token);
# Line 5544  sub _tree_construction_main ($) { Line 6284  sub _tree_construction_main ($) {
6284          !!!cp ('t332');          !!!cp ('t332');
6285          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6286          $script_start_tag->();          $script_start_tag->();
6287          redo B;          next B;
6288        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
6289          !!!cp ('t333');          !!!cp ('t333');
6290          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6291          $parse_rcdata->(CDATA_CONTENT_MODEL);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6292          redo B;          next B;
6293        } elsif ({        } elsif ({
6294                  base => 1, link => 1,                  base => 1, link => 1,
6295                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 5557  sub _tree_construction_main ($) { Line 6297  sub _tree_construction_main ($) {
6297          ## 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
6298          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6299          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6300            !!!ack ('t334.1');
6301          !!!next-token;          !!!next-token;
6302          redo B;          next B;
6303        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
6304          ## 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
6305          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6306          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.
6307    
6308          unless ($self->{confident}) {          unless ($self->{confident}) {
6309            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) {
6310              !!!cp ('t335');              !!!cp ('t335');
6311                ## NOTE: Whether the encoding is supported or not is handled
6312                ## in the {change_encoding} callback.
6313              $self->{change_encoding}              $self->{change_encoding}
6314                  ->($self, $token->{attributes}->{charset}->{value}, $token);                  ->($self, $token->{attributes}->{charset}->{value}, $token);
6315                            
# Line 5575  sub _tree_construction_main ($) { Line 6318  sub _tree_construction_main ($) {
6318                                       $token->{attributes}->{charset}                                       $token->{attributes}->{charset}
6319                                           ->{has_reference});                                           ->{has_reference});
6320            } elsif ($token->{attributes}->{content}) {            } elsif ($token->{attributes}->{content}) {
             ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
6321              if ($token->{attributes}->{content}->{value}              if ($token->{attributes}->{content}->{value}
6322                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6323                      [\x09-\x0D\x20]*=                      [\x09-\x0D\x20]*=
6324                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6325                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
6326                !!!cp ('t336');                !!!cp ('t336');
6327                  ## NOTE: Whether the encoding is supported or not is handled
6328                  ## in the {change_encoding} callback.
6329                $self->{change_encoding}                $self->{change_encoding}
6330                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6331                $meta_el->[0]->get_attribute_node_ns (undef, 'content')                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
# Line 5607  sub _tree_construction_main ($) { Line 6351  sub _tree_construction_main ($) {
6351            }            }
6352          }          }
6353    
6354            !!!ack ('t338.1');
6355          !!!next-token;          !!!next-token;
6356          redo B;          next B;
6357        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
6358          !!!cp ('t341');          !!!cp ('t341');
6359          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6360          $parse_rcdata->(RCDATA_CONTENT_MODEL);          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6361          redo B;          next B;
6362        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
6363          !!!parse-error (type => 'in body:body', token => $token);          !!!parse-error (type => 'in body:body', token => $token);
6364                                
6365          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
6366              $self->{open_elements}->[1]->[1] ne 'body') {              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6367            !!!cp ('t342');            !!!cp ('t342');
6368            ## Ignore the token            ## Ignore the token
6369          } else {          } else {
# Line 5632  sub _tree_construction_main ($) { Line 6377  sub _tree_construction_main ($) {
6377              }              }
6378            }            }
6379          }          }
6380            !!!nack ('t343.1');
6381          !!!next-token;          !!!next-token;
6382          redo B;          next B;
6383        } elsif ({        } elsif ({
6384                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6385                  div => 1, dl => 1, fieldset => 1,                  div => 1, dl => 1, fieldset => 1,
# Line 5648  sub _tree_construction_main ($) { Line 6394  sub _tree_construction_main ($) {
6394            !!!cp ('t350');            !!!cp ('t350');
6395            !!!parse-error (type => 'in form:form', token => $token);            !!!parse-error (type => 'in form:form', token => $token);
6396            ## Ignore the token            ## Ignore the token
6397              !!!nack ('t350.1');
6398            !!!next-token;            !!!next-token;
6399            redo B;            next B;
6400          }          }
6401    
6402          ## has a p element in scope          ## has a p element in scope
6403          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6404            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6405              !!!cp ('t344');              !!!cp ('t344');
6406              !!!back-token;              !!!back-token; # <form>
6407              $token = {type => END_TAG_TOKEN, tag_name => 'p',              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6408                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6409              redo B;              next B;
6410            } elsif ({            } elsif ($_->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6411              !!!cp ('t345');              !!!cp ('t345');
6412              last INSCOPE;              last INSCOPE;
6413            }            }
# Line 5671  sub _tree_construction_main ($) { Line 6415  sub _tree_construction_main ($) {
6415                        
6416          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6417          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6418              !!!nack ('t346.1');
6419            !!!next-token;            !!!next-token;
6420            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
6421              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
# Line 5687  sub _tree_construction_main ($) { Line 6432  sub _tree_construction_main ($) {
6432            !!!cp ('t347.1');            !!!cp ('t347.1');
6433            $self->{form_element} = $self->{open_elements}->[-1]->[0];            $self->{form_element} = $self->{open_elements}->[-1]->[0];
6434    
6435              !!!nack ('t347.2');
6436            !!!next-token;            !!!next-token;
6437          } elsif ($token->{tag_name} eq 'table') {          } elsif ($token->{tag_name} eq 'table') {
6438            !!!cp ('t382');            !!!cp ('t382');
# Line 5694  sub _tree_construction_main ($) { Line 6440  sub _tree_construction_main ($) {
6440                        
6441            $self->{insertion_mode} = IN_TABLE_IM;            $self->{insertion_mode} = IN_TABLE_IM;
6442    
6443              !!!nack ('t382.1');
6444            !!!next-token;            !!!next-token;
6445          } elsif ($token->{tag_name} eq 'hr') {          } elsif ($token->{tag_name} eq 'hr') {
6446            !!!cp ('t386');            !!!cp ('t386');
6447            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6448                    
6449              !!!nack ('t386.1');
6450            !!!next-token;            !!!next-token;
6451          } else {          } else {
6452            !!!cp ('t347');            !!!nack ('t347.1');
6453            !!!next-token;            !!!next-token;
6454          }          }
6455          redo B;          next B;
6456        } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {        } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
6457          ## has a p element in scope          ## has a p element in scope
6458          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6459            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6460              !!!cp ('t353');              !!!cp ('t353');
6461              !!!back-token;              !!!back-token; # <x>
6462              $token = {type => END_TAG_TOKEN, tag_name => 'p',              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6463                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6464              redo B;              next B;
6465            } elsif ({            } elsif ($_->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6466              !!!cp ('t354');              !!!cp ('t354');
6467              last INSCOPE;              last INSCOPE;
6468            }            }
# Line 5731  sub _tree_construction_main ($) { Line 6476  sub _tree_construction_main ($) {
6476                            dd => {dt => 1, dd => 1}}->{$token->{tag_name}};                            dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6477          LI: {          LI: {
6478            ## Step 2            ## Step 2
6479            if ($li_or_dtdd->{$node->[1]}) {            if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6480              if ($i != -1) {              if ($i != -1) {
6481                !!!cp ('t355');                !!!cp ('t355');
6482                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'not closed',
6483                                $self->{open_elements}->[-1]->[1], token => $token);                                value => $self->{open_elements}->[-1]->[0]
6484                                      ->manakai_local_name,
6485                                  token => $token);
6486              } else {              } else {
6487                !!!cp ('t356');                !!!cp ('t356');
6488              }              }
# Line 5746  sub _tree_construction_main ($) { Line 6493  sub _tree_construction_main ($) {
6493            }            }
6494                        
6495            ## Step 3            ## Step 3
6496            if (not $formatting_category->{$node->[1]} and            if (not ($node->[1] & FORMATTING_EL) and
6497                #not $phrasing_category->{$node->[1]} and                #not $phrasing_category->{$node->[1]} and
6498                ($special_category->{$node->[1]} or                ($node->[1] & SPECIAL_EL or
6499                 $scoping_category->{$node->[1]}) and                 $node->[1] & SCOPING_EL) and
6500                $node->[1] ne 'address' and $node->[1] ne 'div') {                not ($node->[1] & ADDRESS_EL) and
6501                  not ($node->[1] & DIV_EL)) {
6502              !!!cp ('t358');              !!!cp ('t358');
6503              last LI;              last LI;
6504            }            }
# Line 5763  sub _tree_construction_main ($) { Line 6511  sub _tree_construction_main ($) {
6511          } # LI          } # LI
6512                        
6513          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6514            !!!nack ('t359.1');
6515          !!!next-token;          !!!next-token;
6516          redo B;          next B;
6517        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
6518          ## has a p element in scope          ## has a p element in scope
6519          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6520            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6521              !!!cp ('t367');              !!!cp ('t367');
6522              !!!back-token;              !!!back-token; # <plaintext>
6523              $token = {type => END_TAG_TOKEN, tag_name => 'p',              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6524                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6525              redo B;              next B;
6526            } elsif ({            } elsif ($_->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6527              !!!cp ('t368');              !!!cp ('t368');
6528              last INSCOPE;              last INSCOPE;
6529            }            }
# Line 5787  sub _tree_construction_main ($) { Line 6533  sub _tree_construction_main ($) {
6533                        
6534          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
6535                        
6536            !!!nack ('t368.1');
6537          !!!next-token;          !!!next-token;
6538          redo B;          next B;
6539        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
6540          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
6541            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
6542            if ($node->[1] eq 'a') {            if ($node->[1] & A_EL) {
6543              !!!cp ('t371');              !!!cp ('t371');
6544              !!!parse-error (type => 'in a:a', token => $token);              !!!parse-error (type => 'in a:a', token => $token);
6545                            
6546              !!!back-token;              !!!back-token; # <a>
6547              $token = {type => END_TAG_TOKEN, tag_name => 'a',              $token = {type => END_TAG_TOKEN, tag_name => 'a',
6548                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6549              $formatting_end_tag->($token);              $formatting_end_tag->($token);
# Line 5827  sub _tree_construction_main ($) { Line 6574  sub _tree_construction_main ($) {
6574          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6575          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6576    
6577            !!!nack ('t374.1');
6578          !!!next-token;          !!!next-token;
6579          redo B;          next B;
6580        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
6581          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6582    
6583          ## has a |nobr| element in scope          ## has a |nobr| element in scope
6584          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6585            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6586            if ($node->[1] eq 'nobr') {            if ($node->[1] & NOBR_EL) {
6587              !!!cp ('t376');              !!!cp ('t376');
6588              !!!parse-error (type => 'in nobr:nobr', token => $token);              !!!parse-error (type => 'in nobr:nobr', token => $token);
6589              !!!back-token;              !!!back-token; # <nobr>
6590              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
6591                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6592              redo B;              next B;
6593            } 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]}) {  
6594              !!!cp ('t377');              !!!cp ('t377');
6595              last INSCOPE;              last INSCOPE;
6596            }            }
# Line 5854  sub _tree_construction_main ($) { Line 6599  sub _tree_construction_main ($) {
6599          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6600          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6601                    
6602            !!!nack ('t377.1');
6603          !!!next-token;          !!!next-token;
6604          redo B;          next B;
6605        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
6606          ## has a button element in scope          ## has a button element in scope
6607          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6608            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6609            if ($node->[1] eq 'button') {            if ($node->[1] & BUTTON_EL) {
6610              !!!cp ('t378');              !!!cp ('t378');
6611              !!!parse-error (type => 'in button:button', token => $token);              !!!parse-error (type => 'in button:button', token => $token);
6612              !!!back-token;              !!!back-token; # <button>
6613              $token = {type => END_TAG_TOKEN, tag_name => 'button',              $token = {type => END_TAG_TOKEN, tag_name => 'button',
6614                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6615              redo B;              next B;
6616            } 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]}) {  
6617              !!!cp ('t379');              !!!cp ('t379');
6618              last INSCOPE;              last INSCOPE;
6619            }            }
# Line 5884  sub _tree_construction_main ($) { Line 6627  sub _tree_construction_main ($) {
6627    
6628          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
6629    
6630            !!!nack ('t379.1');
6631          !!!next-token;          !!!next-token;
6632          redo B;          next B;
6633        } elsif ({        } elsif ({
6634                  xmp => 1,                  xmp => 1,
6635                  iframe => 1,                  iframe => 1,
# Line 5901  sub _tree_construction_main ($) { Line 6645  sub _tree_construction_main ($) {
6645          }          }
6646          ## NOTE: There is an "as if in body" code clone.          ## NOTE: There is an "as if in body" code clone.
6647          $parse_rcdata->(CDATA_CONTENT_MODEL);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6648          redo B;          next B;
6649        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
6650          !!!parse-error (type => 'isindex', token => $token);          !!!parse-error (type => 'isindex', token => $token);
6651                    
6652          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
6653            !!!cp ('t389');            !!!cp ('t389');
6654            ## Ignore the token            ## Ignore the token
6655              !!!nack ('t389'); ## NOTE: Not acknowledged.
6656            !!!next-token;            !!!next-token;
6657            redo B;            next B;
6658          } else {          } else {
6659            my $at = $token->{attributes};            my $at = $token->{attributes};
6660            my $form_attrs;            my $form_attrs;
# Line 5954  sub _tree_construction_main ($) { Line 6699  sub _tree_construction_main ($) {
6699                           line => $token->{line}, column => $token->{column}},                           line => $token->{line}, column => $token->{column}},
6700                          {type => END_TAG_TOKEN, tag_name => 'form',                          {type => END_TAG_TOKEN, tag_name => 'form',
6701                           line => $token->{line}, column => $token->{column}};                           line => $token->{line}, column => $token->{column}};
6702            $token = shift @tokens;            !!!nack ('t391.1'); ## NOTE: Not acknowledged.
6703            !!!back-token (@tokens);            !!!back-token (@tokens);
6704            redo B;            !!!next-token;
6705              next B;
6706          }          }
6707        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
6708          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
6709          my $el;          my $el;
6710          !!!create-element ($el, $token->{tag_name}, $token->{attributes}, $token);          !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
6711                    
6712          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
6713          $self->{content_model} = RCDATA_CONTENT_MODEL;          $self->{content_model} = RCDATA_CONTENT_MODEL;
# Line 5970  sub _tree_construction_main ($) { Line 6716  sub _tree_construction_main ($) {
6716          $insert->($el);          $insert->($el);
6717                    
6718          my $text = '';          my $text = '';
6719            !!!nack ('t392.1');
6720          !!!next-token;          !!!next-token;
6721          if ($token->{type} == CHARACTER_TOKEN) {          if ($token->{type} == CHARACTER_TOKEN) {
6722            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
# Line 6003  sub _tree_construction_main ($) { Line 6750  sub _tree_construction_main ($) {
6750            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
6751          }          }
6752          !!!next-token;          !!!next-token;
6753          redo B;          next B;
6754          } elsif ($token->{tag_name} eq 'math' or
6755                   $token->{tag_name} eq 'svg') {
6756            $reconstruct_active_formatting_elements->($insert_to_current);
6757    
6758            ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
6759    
6760            ## "adjust foreign attributes" - done in insert-element-f
6761            
6762            !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
6763            
6764            if ($self->{self_closing}) {
6765              pop @{$self->{open_elements}};
6766              !!!ack ('t398.1');
6767            } else {
6768              !!!cp ('t398.2');
6769              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
6770              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
6771              ## mode, "in body" (not "in foreign content") secondary insertion
6772              ## mode, maybe.
6773            }
6774    
6775            !!!next-token;
6776            next B;
6777        } elsif ({        } elsif ({
6778                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
6779                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 6013  sub _tree_construction_main ($) { Line 6783  sub _tree_construction_main ($) {
6783          !!!cp ('t401');          !!!cp ('t401');
6784          !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);
6785          ## Ignore the token          ## Ignore the token
6786            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
6787          !!!next-token;          !!!next-token;
6788          redo B;          next B;
6789                    
6790          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
6791        } else {        } else {
# Line 6036  sub _tree_construction_main ($) { Line 6807  sub _tree_construction_main ($) {
6807              }->{$token->{tag_name}}) {              }->{$token->{tag_name}}) {
6808            !!!cp ('t380');            !!!cp ('t380');
6809            push @$active_formatting_elements, ['#marker', ''];            push @$active_formatting_elements, ['#marker', ''];
6810              !!!nack ('t380.1');
6811          } elsif ({          } elsif ({
6812                    b => 1, big => 1, em => 1, font => 1, i => 1,                    b => 1, big => 1, em => 1, font => 1, i => 1,
6813                    s => 1, small => 1, strile => 1,                    s => 1, small => 1, strile => 1,
# Line 6043  sub _tree_construction_main ($) { Line 6815  sub _tree_construction_main ($) {
6815                   }->{$token->{tag_name}}) {                   }->{$token->{tag_name}}) {
6816            !!!cp ('t375');            !!!cp ('t375');
6817            push @$active_formatting_elements, $self->{open_elements}->[-1];            push @$active_formatting_elements, $self->{open_elements}->[-1];
6818              !!!nack ('t375.1');
6819          } elsif ($token->{tag_name} eq 'input') {          } elsif ($token->{tag_name} eq 'input') {
6820            !!!cp ('t388');            !!!cp ('t388');
6821            ## TODO: associate with $self->{form_element} if defined            ## TODO: associate with $self->{form_element} if defined
6822            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6823              !!!ack ('t388.2');
6824          } elsif ({          } elsif ({
6825                    area => 1, basefont => 1, bgsound => 1, br => 1,                    area => 1, basefont => 1, bgsound => 1, br => 1,
6826                    embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                    embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
# Line 6054  sub _tree_construction_main ($) { Line 6828  sub _tree_construction_main ($) {
6828                   }->{$token->{tag_name}}) {                   }->{$token->{tag_name}}) {
6829            !!!cp ('t388.1');            !!!cp ('t388.1');
6830            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6831              !!!ack ('t388.3');
6832          } elsif ($token->{tag_name} eq 'select') {          } elsif ($token->{tag_name} eq 'select') {
6833            ## TODO: associate with $self->{form_element} if defined            ## TODO: associate with $self->{form_element} if defined
6834                    
# Line 6066  sub _tree_construction_main ($) { Line 6841  sub _tree_construction_main ($) {
6841              !!!cp ('t400.2');              !!!cp ('t400.2');
6842              $self->{insertion_mode} = IN_SELECT_IM;              $self->{insertion_mode} = IN_SELECT_IM;
6843            }            }
6844              !!!nack ('t400.3');
6845          } else {          } else {
6846            !!!cp ('t402');            !!!nack ('t402');
6847          }          }
6848                    
6849          !!!next-token;          !!!next-token;
6850          redo B;          next B;
6851        }        }
6852      } elsif ($token->{type} == END_TAG_TOKEN) {      } elsif ($token->{type} == END_TAG_TOKEN) {
6853        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
# Line 6079  sub _tree_construction_main ($) { Line 6855  sub _tree_construction_main ($) {
6855          my $i;          my $i;
6856          INSCOPE: {          INSCOPE: {
6857            for (reverse @{$self->{open_elements}}) {            for (reverse @{$self->{open_elements}}) {
6858              if ($_->[1] eq 'body') {              if ($_->[1] & BODY_EL) {
6859                !!!cp ('t405');                !!!cp ('t405');
6860                $i = $_;                $i = $_;
6861                last INSCOPE;                last INSCOPE;
6862              } elsif ({              } elsif ($_->[1] & SCOPING_EL) {
                       applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                       button => 1, marquee => 1, object => 1, html => 1,  
                      }->{$_->[1]}) {  
6863                !!!cp ('t405.1');                !!!cp ('t405.1');
6864                last;                last;
6865              }              }
# Line 6096  sub _tree_construction_main ($) { Line 6869  sub _tree_construction_main ($) {
6869                            value => $token->{tag_name}, token => $token);                            value => $token->{tag_name}, token => $token);
6870            ## NOTE: Ignore the token.            ## NOTE: Ignore the token.
6871            !!!next-token;            !!!next-token;
6872            redo B;            next B;
6873          } # INSCOPE          } # INSCOPE
6874    
6875          for (@{$self->{open_elements}}) {          for (@{$self->{open_elements}}) {
6876            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]}) {  
6877              !!!cp ('t403');              !!!cp ('t403');
6878              !!!parse-error (type => 'not closed:'.$_->[1], token => $token);              !!!parse-error (type => 'not closed',
6879                                value => $_->[0]->manakai_local_name,
6880                                token => $token);
6881              last;              last;
6882            } else {            } else {
6883              !!!cp ('t404');              !!!cp ('t404');
# Line 6115  sub _tree_construction_main ($) { Line 6886  sub _tree_construction_main ($) {
6886    
6887          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
6888          !!!next-token;          !!!next-token;
6889          redo B;          next B;
6890        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
6891          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
6892            ## up-to-date, though it has same effect as speced.
6893            if (@{$self->{open_elements}} > 1 and
6894                $self->{open_elements}->[1]->[1] & BODY_EL) {
6895            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
6896            if ($self->{open_elements}->[-1]->[1] ne 'body') {            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
6897              !!!cp ('t406');              !!!cp ('t406');
6898              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1], token => $token);              !!!parse-error (type => 'not closed',
6899                                value => $self->{open_elements}->[1]->[0]
6900                                    ->manakai_local_name,
6901                                token => $token);
6902            } else {            } else {
6903              !!!cp ('t407');              !!!cp ('t407');
6904            }            }
6905            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6906            ## reprocess            ## reprocess
6907            redo B;            next B;
6908          } else {          } else {
6909            !!!cp ('t408');            !!!cp ('t408');
6910            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6911            ## Ignore the token            ## Ignore the token
6912            !!!next-token;            !!!next-token;
6913            redo B;            next B;
6914          }          }
6915        } elsif ({        } elsif ({
6916                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
# Line 6146  sub _tree_construction_main ($) { Line 6923  sub _tree_construction_main ($) {
6923          my $i;          my $i;
6924          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6925            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6926            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6927              !!!cp ('t410');              !!!cp ('t410');
6928              $i = $_;              $i = $_;
6929              last INSCOPE;              last INSCOPE;
6930            } 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]}) {  
6931              !!!cp ('t411');              !!!cp ('t411');
6932              last INSCOPE;              last INSCOPE;
6933            }            }
# Line 6169  sub _tree_construction_main ($) { Line 6943  sub _tree_construction_main ($) {
6943                    dt => ($token->{tag_name} ne 'dt'),                    dt => ($token->{tag_name} ne 'dt'),
6944                    li => ($token->{tag_name} ne 'li'),                    li => ($token->{tag_name} ne 'li'),
6945                    p => 1,                    p => 1,
6946                   }->{$self->{open_elements}->[-1]->[1]}) {                   }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
6947              !!!cp ('t409');              !!!cp ('t409');
6948              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6949            }            }
6950    
6951            ## Step 2.            ## Step 2.
6952            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6953                      ne $token->{tag_name}) {
6954              !!!cp ('t412');              !!!cp ('t412');
6955              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);              !!!parse-error (type => 'not closed',
6956                                value => $self->{open_elements}->[-1]->[0]
6957                                    ->manakai_local_name,
6958                                token => $token);
6959            } else {            } else {
6960              !!!cp ('t414');              !!!cp ('t414');
6961            }            }
# Line 6192  sub _tree_construction_main ($) { Line 6970  sub _tree_construction_main ($) {
6970                }->{$token->{tag_name}};                }->{$token->{tag_name}};
6971          }          }
6972          !!!next-token;          !!!next-token;
6973          redo B;          next B;
6974        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
6975          undef $self->{form_element};          undef $self->{form_element};
6976    
# Line 6200  sub _tree_construction_main ($) { Line 6978  sub _tree_construction_main ($) {
6978          my $i;          my $i;
6979          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6980            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6981            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & FORM_EL) {
6982              !!!cp ('t418');              !!!cp ('t418');
6983              $i = $_;              $i = $_;
6984              last INSCOPE;              last INSCOPE;
6985            } 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]}) {  
6986              !!!cp ('t419');              !!!cp ('t419');
6987              last INSCOPE;              last INSCOPE;
6988            }            }
# Line 6218  sub _tree_construction_main ($) { Line 6993  sub _tree_construction_main ($) {
6993            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6994          } else {          } else {
6995            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
6996            while ({            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                   dd => 1, dt => 1, li => 1, p => 1,  
                  }->{$self->{open_elements}->[-1]->[1]}) {  
6997              !!!cp ('t417');              !!!cp ('t417');
6998              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6999            }            }
7000                        
7001            ## Step 2.            ## Step 2.
7002            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7003                      ne $token->{tag_name}) {
7004              !!!cp ('t417.1');              !!!cp ('t417.1');
7005              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);              !!!parse-error (type => 'not closed',
7006                                value => $self->{open_elements}->[-1]->[0]
7007                                    ->manakai_local_name,
7008                                token => $token);
7009            } else {            } else {
7010              !!!cp ('t420');              !!!cp ('t420');
7011            }              }  
# Line 6238  sub _tree_construction_main ($) { Line 7015  sub _tree_construction_main ($) {
7015          }          }
7016    
7017          !!!next-token;          !!!next-token;
7018          redo B;          next B;
7019        } elsif ({        } elsif ({
7020                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
7021                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 6246  sub _tree_construction_main ($) { Line 7023  sub _tree_construction_main ($) {
7023          my $i;          my $i;
7024          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7025            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7026            if ({            if ($node->[1] & HEADING_EL) {
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
7027              !!!cp ('t423');              !!!cp ('t423');
7028              $i = $_;              $i = $_;
7029              last INSCOPE;              last INSCOPE;
7030            } 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]}) {  
7031              !!!cp ('t424');              !!!cp ('t424');
7032              last INSCOPE;              last INSCOPE;
7033            }            }
# Line 6266  sub _tree_construction_main ($) { Line 7038  sub _tree_construction_main ($) {
7038            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7039          } else {          } else {
7040            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7041            while ({            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                   dd => 1, dt => 1, li => 1, p => 1,  
                  }->{$self->{open_elements}->[-1]->[1]}) {  
7042              !!!cp ('t422');              !!!cp ('t422');
7043              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
7044            }            }
7045                        
7046            ## Step 2.            ## Step 2.
7047            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7048                      ne $token->{tag_name}) {
7049              !!!cp ('t425');              !!!cp ('t425');
7050              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7051            } else {            } else {
# Line 6286  sub _tree_construction_main ($) { Line 7057  sub _tree_construction_main ($) {
7057          }          }
7058                    
7059          !!!next-token;          !!!next-token;
7060          redo B;          next B;
7061        } elsif ($token->{tag_name} eq 'p') {        } elsif ($token->{tag_name} eq 'p') {
7062          ## has an element in scope          ## has an element in scope
7063          my $i;          my $i;
7064          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7065            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7066            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & P_EL) {
7067              !!!cp ('t410.1');              !!!cp ('t410.1');
7068              $i = $_;              $i = $_;
7069              last INSCOPE;              last INSCOPE;
7070            } 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]}) {  
7071              !!!cp ('t411.1');              !!!cp ('t411.1');
7072              last INSCOPE;              last INSCOPE;
7073            }            }
7074          } # INSCOPE          } # INSCOPE
7075    
7076          if (defined $i) {          if (defined $i) {
7077            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7078                      ne $token->{tag_name}) {
7079              !!!cp ('t412.1');              !!!cp ('t412.1');
7080              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);              !!!parse-error (type => 'not closed',
7081                                value => $self->{open_elements}->[-1]->[0]
7082                                    ->manakai_local_name,
7083                                token => $token);
7084            } else {            } else {
7085              !!!cp ('t414.1');              !!!cp ('t414.1');
7086            }            }
# Line 6321  sub _tree_construction_main ($) { Line 7093  sub _tree_construction_main ($) {
7093            !!!cp ('t415.1');            !!!cp ('t415.1');
7094            ## As if <p>, then reprocess the current token            ## As if <p>, then reprocess the current token
7095            my $el;            my $el;
7096            !!!create-element ($el, 'p',, $token);            !!!create-element ($el, $HTML_NS, 'p',, $token);
7097            $insert->($el);            $insert->($el);
7098            ## NOTE: Not inserted into |$self->{open_elements}|.            ## NOTE: Not inserted into |$self->{open_elements}|.
7099          }          }
7100    
7101          !!!next-token;          !!!next-token;
7102          redo B;          next B;
7103        } elsif ({        } elsif ({
7104                  a => 1,                  a => 1,
7105                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
# Line 6336  sub _tree_construction_main ($) { Line 7108  sub _tree_construction_main ($) {
7108                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7109          !!!cp ('t427');          !!!cp ('t427');
7110          $formatting_end_tag->($token);          $formatting_end_tag->($token);
7111          redo B;          next B;
7112        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
7113          !!!cp ('t428');          !!!cp ('t428');
7114          !!!parse-error (type => 'unmatched end tag:br', token => $token);          !!!parse-error (type => 'unmatched end tag:br', token => $token);
# Line 6345  sub _tree_construction_main ($) { Line 7117  sub _tree_construction_main ($) {
7117          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7118                    
7119          my $el;          my $el;
7120          !!!create-element ($el, 'br',, $token);          !!!create-element ($el, $HTML_NS, 'br',, $token);
7121          $insert->($el);          $insert->($el);
7122                    
7123          ## Ignore the token.          ## Ignore the token.
7124          !!!next-token;          !!!next-token;
7125          redo B;          next B;
7126        } elsif ({        } elsif ({
7127                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7128                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 6367  sub _tree_construction_main ($) { Line 7139  sub _tree_construction_main ($) {
7139          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7140          ## Ignore the token          ## Ignore the token
7141          !!!next-token;          !!!next-token;
7142          redo B;          next B;
7143                    
7144          ## ISSUE: Issue on HTML5 new elements in spec          ## ISSUE: Issue on HTML5 new elements in spec
7145                    
# Line 6378  sub _tree_construction_main ($) { Line 7150  sub _tree_construction_main ($) {
7150    
7151          ## Step 2          ## Step 2
7152          S2: {          S2: {
7153            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7154              ## Step 1              ## Step 1
7155              ## generate implied end tags              ## generate implied end tags
7156              while ({              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                     dd => 1, dt => 1, li => 1, p => 1,  
                    }->{$self->{open_elements}->[-1]->[1]}) {  
7157                !!!cp ('t430');                !!!cp ('t430');
7158                ## ISSUE: Can this case be reached?                ## ISSUE: Can this case be reached?
7159                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
7160              }              }
7161                    
7162              ## Step 2              ## Step 2
7163              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7164                        ne $token->{tag_name}) {
7165                !!!cp ('t431');                !!!cp ('t431');
7166                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
7167                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                !!!parse-error (type => 'not closed',
7168                                  value => $self->{open_elements}->[-1]->[0]
7169                                      ->manakai_local_name,
7170                                  token => $token);
7171              } else {              } else {
7172                !!!cp ('t432');                !!!cp ('t432');
7173              }              }
# Line 6405  sub _tree_construction_main ($) { Line 7179  sub _tree_construction_main ($) {
7179              last S2;              last S2;
7180            } else {            } else {
7181              ## Step 3              ## Step 3
7182              if (not $formatting_category->{$node->[1]} and              if (not ($node->[1] & FORMATTING_EL) and
7183                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
7184                  ($special_category->{$node->[1]} or                  ($node->[1] & SPECIAL_EL or
7185                   $scoping_category->{$node->[1]})) {                   $node->[1] & SCOPING_EL)) {
7186                !!!cp ('t433');                !!!cp ('t433');
7187                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7188                ## Ignore the token                ## Ignore the token
# Line 6426  sub _tree_construction_main ($) { Line 7200  sub _tree_construction_main ($) {
7200            ## Step 5;            ## Step 5;
7201            redo S2;            redo S2;
7202          } # S2          } # S2
7203          redo B;          next B;
7204        }        }
7205      }      }
7206      redo B;      next B;
7207      } continue { # B
7208        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7209          ## NOTE: The code below is executed in cases where it does not have
7210          ## to be, but it it is harmless even in those cases.
7211          ## has an element in scope
7212          INSCOPE: {
7213            for (reverse 0..$#{$self->{open_elements}}) {
7214              my $node = $self->{open_elements}->[$_];
7215              if ($node->[1] & FOREIGN_EL) {
7216                last INSCOPE;
7217              } elsif ($node->[1] & SCOPING_EL) {
7218                last;
7219              }
7220            }
7221            
7222            ## NOTE: No foreign element in scope.
7223            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7224          } # INSCOPE
7225        }
7226    } # B    } # B
7227    
7228    ## Stop parsing # MUST    ## Stop parsing # MUST
# Line 6475  sub set_inner_html ($$$) { Line 7268  sub set_inner_html ($$$) {
7268    
7269      ## Step 8 # MUST      ## Step 8 # MUST
7270      my $i = 0;      my $i = 0;
7271      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7272      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7273      $p->{set_next_char} = sub {      $p->{set_next_char} = sub {
7274        my $self = shift;        my $self = shift;
7275    
# Line 6485  sub set_inner_html ($$$) { Line 7278  sub set_inner_html ($$$) {
7278    
7279        $self->{next_char} = -1 and return if $i >= length $$s;        $self->{next_char} = -1 and return if $i >= length $$s;
7280        $self->{next_char} = ord substr $$s, $i++, 1;        $self->{next_char} = ord substr $$s, $i++, 1;
7281        $column++;  
7282          ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7283          $p->{column}++;
7284    
7285        if ($self->{next_char} == 0x000A) { # LF        if ($self->{next_char} == 0x000A) { # LF
7286          $line++;          $p->{line}++;
7287          $column = 0;          $p->{column} = 0;
7288          !!!cp ('i1');          !!!cp ('i1');
7289        } elsif ($self->{next_char} == 0x000D) { # CR        } elsif ($self->{next_char} == 0x000D) { # CR
7290          $i++ if substr ($$s, $i, 1) eq "\x0A";          $i++ if substr ($$s, $i, 1) eq "\x0A";
7291          $self->{next_char} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
7292          $line++;          $p->{line}++;
7293          $column = 0;          $p->{column} = 0;
7294          !!!cp ('i2');          !!!cp ('i2');
7295        } elsif ($self->{next_char} > 0x10FFFF) {        } elsif ($self->{next_char} > 0x10FFFF) {
7296          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
# Line 6504  sub set_inner_html ($$$) { Line 7299  sub set_inner_html ($$$) {
7299          !!!cp ('i4');          !!!cp ('i4');
7300          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
7301          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7302          } elsif ($self->{next_char} <= 0x0008 or
7303                   (0x000E <= $self->{next_char} and
7304                    $self->{next_char} <= 0x001F) or
7305                   (0x007F <= $self->{next_char} and
7306                    $self->{next_char} <= 0x009F) or
7307                   (0xD800 <= $self->{next_char} and
7308                    $self->{next_char} <= 0xDFFF) or
7309                   (0xFDD0 <= $self->{next_char} and
7310                    $self->{next_char} <= 0xFDDF) or
7311                   {
7312                    0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
7313                    0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
7314                    0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
7315                    0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
7316                    0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
7317                    0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
7318                    0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
7319                    0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
7320                    0x10FFFE => 1, 0x10FFFF => 1,
7321                   }->{$self->{next_char}}) {
7322            !!!cp ('i4.1');
7323            !!!parse-error (type => 'control char', level => $self->{must_level});
7324    ## TODO: error type documentation
7325        }        }
7326      };      };
7327      $p->{prev_char} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];
# Line 6511  sub set_inner_html ($$$) { Line 7329  sub set_inner_html ($$$) {
7329            
7330      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7331        my (%opt) = @_;        my (%opt) = @_;
7332        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7333          my $column = $opt{column};
7334          if (defined $opt{token} and defined $opt{token}->{line}) {
7335            $line = $opt{token}->{line};
7336            $column = $opt{token}->{column};
7337          }
7338          warn "Parse error ($opt{type}) at line $line column $column\n";
7339      };      };
7340      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7341        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7342      };      };
7343            
7344      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
# Line 6538  sub set_inner_html ($$$) { Line 7362  sub set_inner_html ($$$) {
7362          unless defined $p->{content_model};          unless defined $p->{content_model};
7363          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
7364    
7365      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7366          ## TODO: Foreign element OK?
7367    
7368      ## Step 3      ## Step 3
7369      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
# Line 6548  sub set_inner_html ($$$) { Line 7373  sub set_inner_html ($$$) {
7373      $doc->append_child ($root);      $doc->append_child ($root);
7374    
7375      ## Step 5 # MUST      ## Step 5 # MUST
7376      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7377    
7378      undef $p->{head_element};      undef $p->{head_element};
7379    
# Line 6594  sub set_inner_html ($$$) { Line 7419  sub set_inner_html ($$$) {
7419      ## ISSUE: mutation events?      ## ISSUE: mutation events?
7420    
7421      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
7422    
7423        delete $p->{parse_error}; # delete loop
7424    } else {    } else {
7425      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";
7426    }    }

Legend:
Removed from v.1.118  
changed lines
  Added in v.1.139

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24