/[suikacvs]/markup/html/whatpm/Whatpm/HTML.pm.src
Suika

Diff of /markup/html/whatpm/Whatpm/HTML.pm.src

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.121 by wakaba, Thu Mar 20 08:04:58 2008 UTC revision 1.140 by wakaba, Sat May 24 09:59:52 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 574  sub _get_next_token ($) { Line 1048  sub _get_next_token ($) {
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    
# 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 829  sub _get_next_token ($) { Line 1295  sub _get_next_token ($) {
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 942  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 1042  sub _get_next_token ($) { Line 1488  sub _get_next_token ($) {
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 1388  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 1516  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 2240  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 2390  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 2532  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 2668  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 2748  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 2765  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 2781  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 2811  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            !!!cp ('t28');
3378                $self->{inner_html_node}->[1] eq 'th') {            $node = $self->{inner_html_node};
3379              !!!cp ('t27');          } else {
3380              #            die "_reset_insertion_mode: t27";
           } else {  
             !!!cp ('t28');  
             $node = $self->{inner_html_node};  
           }  
3381          }          }
3382        }        }
3383              
3384        ## Step 4..13        ## Step 4..14
3385        my $new_mode = {        my $new_mode;
3386          if ($node->[1] & FOREIGN_EL) {
3387            !!!cp ('t28.1');
3388            ## NOTE: Strictly spaking, the line below only applies to MathML and
3389            ## SVG elements.  Currently the HTML syntax supports only MathML and
3390            ## SVG elements as foreigners.
3391            $new_mode = $self->{insertion_mode} | IN_FOREIGN_CONTENT_IM;
3392            ## ISSUE: What is set as the secondary insertion mode?
3393          } elsif ($node->[1] & TABLE_CELL_EL) {
3394            if ($last) {
3395              !!!cp ('t28.2');
3396              #
3397            } else {
3398              !!!cp ('t28.3');
3399              $new_mode = IN_CELL_IM;
3400            }
3401          } else {
3402            !!!cp ('t28.4');
3403            $new_mode = {
3404                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
3405                        ## NOTE: |option| and |optgroup| do not set                        ## NOTE: |option| and |optgroup| do not set
3406                        ## insertion mode to "in select" by themselves.                        ## insertion mode to "in select" by themselves.
                       td => IN_CELL_IM,  
                       th => IN_CELL_IM,  
3407                        tr => IN_ROW_IM,                        tr => IN_ROW_IM,
3408                        tbody => IN_TABLE_BODY_IM,                        tbody => IN_TABLE_BODY_IM,
3409                        thead => IN_TABLE_BODY_IM,                        thead => IN_TABLE_BODY_IM,
# Line 2839  sub _reset_insertion_mode ($) { Line 3414  sub _reset_insertion_mode ($) {
3414                        head => IN_BODY_IM, # not in head!                        head => IN_BODY_IM, # not in head!
3415                        body => IN_BODY_IM,                        body => IN_BODY_IM,
3416                        frameset => IN_FRAMESET_IM,                        frameset => IN_FRAMESET_IM,
3417                       }->{$node->[1]};                       }->{$node->[0]->manakai_local_name};
3418          }
3419        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3420                
3421        ## Step 14        ## Step 15
3422        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3423          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3424            !!!cp ('t29');            !!!cp ('t29');
3425            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
# Line 2857  sub _reset_insertion_mode ($) { Line 3433  sub _reset_insertion_mode ($) {
3433          !!!cp ('t31');          !!!cp ('t31');
3434        }        }
3435                
3436        ## Step 15        ## Step 16
3437        $self->{insertion_mode} = IN_BODY_IM and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
3438                
3439        ## Step 16        ## Step 17
3440        $i--;        $i--;
3441        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3442                
3443        ## Step 17        ## Step 18
3444        redo S3;        redo S3;
3445      } # S3      } # S3
3446    
# Line 2976  sub _tree_construction_main ($) { Line 3552  sub _tree_construction_main ($) {
3552      ## Step 1      ## Step 1
3553      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3554      my $el;      my $el;
3555      !!!create-element ($el, $start_tag_name, $token->{attributes}, $token);      !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3556    
3557      ## Step 2      ## Step 2
3558      $insert->($el);      $insert->($el);
# Line 2987  sub _tree_construction_main ($) { Line 3563  sub _tree_construction_main ($) {
3563    
3564      ## Step 4      ## Step 4
3565      my $text = '';      my $text = '';
3566        !!!nack ('t40.1');
3567      !!!next-token;      !!!next-token;
3568      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3569        !!!cp ('t40');        !!!cp ('t40');
# Line 3026  sub _tree_construction_main ($) { Line 3603  sub _tree_construction_main ($) {
3603    
3604    my $script_start_tag = sub () {    my $script_start_tag = sub () {
3605      my $script_el;      my $script_el;
3606      !!!create-element ($script_el, 'script', $token->{attributes}, $token);      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
3607      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3608    
3609      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
3610      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
3611            
3612      my $text = '';      my $text = '';
3613        !!!nack ('t45.1');
3614      !!!next-token;      !!!next-token;
3615      while ($token->{type} == CHARACTER_TOKEN) {      while ($token->{type} == CHARACTER_TOKEN) {
3616        !!!cp ('t45');        !!!cp ('t45');
# Line 3090  sub _tree_construction_main ($) { Line 3668  sub _tree_construction_main ($) {
3668        my $formatting_element;        my $formatting_element;
3669        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3670        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3671          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3672              !!!cp ('t52');
3673              last AFE;
3674            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
3675                         eq $tag_name) {
3676            !!!cp ('t51');            !!!cp ('t51');
3677            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3678            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3679            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           !!!cp ('t52');  
           last AFE;  
3680          }          }
3681        } # AFE        } # AFE
3682        unless (defined $formatting_element) {        unless (defined $formatting_element) {
# Line 3125  sub _tree_construction_main ($) { Line 3704  sub _tree_construction_main ($) {
3704              !!!next-token;              !!!next-token;
3705              return;              return;
3706            }            }
3707          } 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]}) {  
3708            !!!cp ('t56');            !!!cp ('t56');
3709            $in_scope = 0;            $in_scope = 0;
3710          }          }
# Line 3143  sub _tree_construction_main ($) { Line 3719  sub _tree_construction_main ($) {
3719        }        }
3720        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3721          !!!cp ('t58');          !!!cp ('t58');
3722          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1],          !!!parse-error (type => 'not closed',
3723                            value => $self->{open_elements}->[-1]->[0]
3724                                ->manakai_local_name,
3725                          token => $end_tag_token);                          token => $end_tag_token);
3726        }        }
3727                
# Line 3152  sub _tree_construction_main ($) { Line 3730  sub _tree_construction_main ($) {
3730        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
3731        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3732          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3733          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
3734              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3735              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
3736               $scoping_category->{$node->[1]})) { ## Scoping is redundant, maybe               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
3737            !!!cp ('t59');            !!!cp ('t59');
3738            $furthest_block = $node;            $furthest_block = $node;
3739            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
# Line 3241  sub _tree_construction_main ($) { Line 3819  sub _tree_construction_main ($) {
3819        } # S7          } # S7  
3820                
3821        ## Step 8        ## Step 8
3822        if ({        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
            table => 1, tbody => 1, tfoot => 1, thead => 1, tr => 1,  
           }->{$common_ancestor_node->[1]}) {  
3823          my $foster_parent_element;          my $foster_parent_element;
3824          my $next_sibling;          my $next_sibling;
3825                           OE: for (reverse 0..$#{$self->{open_elements}}) {          OE: for (reverse 0..$#{$self->{open_elements}}) {
3826                             if ($self->{open_elements}->[$_]->[1] eq 'table') {            if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3827                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3828                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3829                                 !!!cp ('t65.1');                                 !!!cp ('t65.1');
# Line 3320  sub _tree_construction_main ($) { Line 3896  sub _tree_construction_main ($) {
3896    
3897    my $insert_to_foster = sub {    my $insert_to_foster = sub {
3898      my $child = shift;      my $child = shift;
3899      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]}) {  
3900        # MUST        # MUST
3901        my $foster_parent_element;        my $foster_parent_element;
3902        my $next_sibling;        my $next_sibling;
3903                           OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3904                             if ($self->{open_elements}->[$_]->[1] eq 'table') {          if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3905                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3906                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3907                                 !!!cp ('t70');                                 !!!cp ('t70');
# Line 3352  sub _tree_construction_main ($) { Line 3926  sub _tree_construction_main ($) {
3926      }      }
3927    }; # $insert_to_foster    }; # $insert_to_foster
3928    
3929    B: {    B: while (1) {
3930      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
3931        !!!cp ('t73');        !!!cp ('t73');
3932        !!!parse-error (type => 'DOCTYPE in the middle', token => $token);        !!!parse-error (type => 'DOCTYPE in the middle', token => $token);
3933        ## Ignore the token        ## Ignore the token
3934        ## Stay in the phase        ## Stay in the phase
3935        !!!next-token;        !!!next-token;
3936        redo B;        next B;
3937      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
3938               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
3939        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
# Line 3385  sub _tree_construction_main ($) { Line 3959  sub _tree_construction_main ($) {
3959               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
3960          }          }
3961        }        }
3962          !!!nack ('t84.1');
3963        !!!next-token;        !!!next-token;
3964        redo B;        next B;
3965      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
3966        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
3967        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
# Line 3400  sub _tree_construction_main ($) { Line 3975  sub _tree_construction_main ($) {
3975          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
3976        }        }
3977        !!!next-token;        !!!next-token;
3978        redo B;        next B;
3979      } elsif ($self->{insertion_mode} & HEAD_IMS) {      } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
3980          if ($token->{type} == CHARACTER_TOKEN) {
3981            !!!cp ('t87.1');
3982            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3983            !!!next-token;
3984            next B;
3985          } elsif ($token->{type} == START_TAG_TOKEN) {
3986            if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
3987                 $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
3988                not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
3989                ($token->{tag_name} eq 'svg' and
3990                 $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
3991              ## NOTE: "using the rules for secondary insertion mode"then"continue"
3992              !!!cp ('t87.2');
3993              #
3994            } elsif ({
3995                      b => 1, big => 1, blockquote => 1, body => 1, br => 1,
3996                      center => 1, code => 1, dd => 1, div => 1, dl => 1, em => 1,
3997                      embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1, ## No h4!
3998                      h5 => 1, h6 => 1, head => 1, hr => 1, i => 1, img => 1,
3999                      li => 1, menu => 1, meta => 1, nobr => 1, p => 1, pre => 1,
4000                      ruby => 1, s => 1, small => 1, span => 1, strong => 1,
4001                      sub => 1, sup => 1, table => 1, tt => 1, u => 1, ul => 1,
4002                      var => 1,
4003                     }->{$token->{tag_name}}) {
4004              !!!cp ('t87.2');
4005              !!!parse-error (type => 'not closed',
4006                              value => $self->{open_elements}->[-1]->[0]
4007                                  ->manakai_local_name,
4008                              token => $token);
4009    
4010              pop @{$self->{open_elements}}
4011                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4012    
4013              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4014              ## Reprocess.
4015              next B;
4016            } else {
4017              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
4018              my $tag_name = $token->{tag_name};
4019              if ($nsuri eq $SVG_NS) {
4020                $tag_name = {
4021                   altglyph => 'altGlyph',
4022                   altglyphdef => 'altGlyphDef',
4023                   altglyphitem => 'altGlyphItem',
4024                   animatecolor => 'animateColor',
4025                   animatemotion => 'animateMotion',
4026                   animatetransform => 'animateTransform',
4027                   clippath => 'clipPath',
4028                   feblend => 'feBlend',
4029                   fecolormatrix => 'feColorMatrix',
4030                   fecomponenttransfer => 'feComponentTransfer',
4031                   fecomposite => 'feComposite',
4032                   feconvolvematrix => 'feConvolveMatrix',
4033                   fediffuselighting => 'feDiffuseLighting',
4034                   fedisplacementmap => 'feDisplacementMap',
4035                   fedistantlight => 'feDistantLight',
4036                   feflood => 'feFlood',
4037                   fefunca => 'feFuncA',
4038                   fefuncb => 'feFuncB',
4039                   fefuncg => 'feFuncG',
4040                   fefuncr => 'feFuncR',
4041                   fegaussianblur => 'feGaussianBlur',
4042                   feimage => 'feImage',
4043                   femerge => 'feMerge',
4044                   femergenode => 'feMergeNode',
4045                   femorphology => 'feMorphology',
4046                   feoffset => 'feOffset',
4047                   fepointlight => 'fePointLight',
4048                   fespecularlighting => 'feSpecularLighting',
4049                   fespotlight => 'feSpotLight',
4050                   fetile => 'feTile',
4051                   feturbulence => 'feTurbulence',
4052                   foreignobject => 'foreignObject',
4053                   glyphref => 'glyphRef',
4054                   lineargradient => 'linearGradient',
4055                   radialgradient => 'radialGradient',
4056                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4057                   textpath => 'textPath',  
4058                }->{$tag_name} || $tag_name;
4059              }
4060    
4061              ## "adjust SVG attributes" (SVG only) - done in insert-element-f
4062    
4063              ## "adjust foreign attributes" - done in insert-element-f
4064    
4065              !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
4066    
4067              if ($self->{self_closing}) {
4068                pop @{$self->{open_elements}};
4069                !!!ack ('t87.3');
4070              } else {
4071                !!!cp ('t87.4');
4072              }
4073    
4074              !!!next-token;
4075              next B;
4076            }
4077          } elsif ($token->{type} == END_TAG_TOKEN) {
4078            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4079            !!!cp ('t87.5');
4080            #
4081          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4082            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4083            !!!cp ('t87.6');
4084            #
4085            ## TODO: ...
4086          } else {
4087            die "$0: $token->{type}: Unknown token type";        
4088          }
4089        }
4090    
4091        if ($self->{insertion_mode} & HEAD_IMS) {
4092        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4093          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4094            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
# Line 3411  sub _tree_construction_main ($) { Line 4098  sub _tree_construction_main ($) {
4098              !!!cp ('t88.1');              !!!cp ('t88.1');
4099              ## Ignore the token.              ## Ignore the token.
4100              !!!next-token;              !!!next-token;
4101              redo B;              next B;
4102            }            }
4103            unless (length $token->{data}) {            unless (length $token->{data}) {
4104              !!!cp ('t88');              !!!cp ('t88');
4105              !!!next-token;              !!!next-token;
4106              redo B;              next B;
4107            }            }
4108          }          }
4109    
4110          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4111            !!!cp ('t89');            !!!cp ('t89');
4112            ## As if <head>            ## As if <head>
4113            !!!create-element ($self->{head_element}, 'head',, $token);            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4114            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4115            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            push @{$self->{open_elements}},
4116                  [$self->{head_element}, $el_category->{head}];
4117    
4118            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4119            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
# Line 3451  sub _tree_construction_main ($) { Line 4139  sub _tree_construction_main ($) {
4139            !!!cp ('t92');            !!!cp ('t92');
4140          }          }
4141    
4142              ## "after head" insertion mode          ## "after head" insertion mode
4143              ## As if <body>          ## As if <body>
4144              !!!insert-element ('body',, $token);          !!!insert-element ('body',, $token);
4145              $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4146              ## reprocess          ## reprocess
4147              redo B;          next B;
4148            } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4149              if ($token->{tag_name} eq 'head') {          if ($token->{tag_name} eq 'head') {
4150                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4151                  !!!cp ('t93');              !!!cp ('t93');
4152                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes}, $token);              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4153                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              $self->{open_elements}->[-1]->[0]->append_child
4154                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];                  ($self->{head_element});
4155                  $self->{insertion_mode} = IN_HEAD_IM;              push @{$self->{open_elements}},
4156                  !!!next-token;                  [$self->{head_element}, $el_category->{head}];
4157                  redo B;              $self->{insertion_mode} = IN_HEAD_IM;
4158                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {              !!!nack ('t93.1');
4159                  !!!cp ('t94');              !!!next-token;
4160                  #              next B;
4161                } else {            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4162                  !!!cp ('t95');              !!!cp ('t93.2');
4163                  !!!parse-error (type => 'in head:head', token => $token); # or in head noscript              !!!parse-error (type => 'after head:head', token => $token); ## TODO: error type
4164                  ## Ignore the token              ## Ignore the token
4165                  !!!next-token;              !!!nack ('t93.3');
4166                  redo B;              !!!next-token;
4167                }              next B;
4168              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {            } else {
4169                !!!cp ('t96');              !!!cp ('t95');
4170                ## As if <head>              !!!parse-error (type => 'in head:head', token => $token); # or in head noscript
4171                !!!create-element ($self->{head_element}, 'head',, $token);              ## Ignore the token
4172                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              !!!nack ('t95.1');
4173                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];              !!!next-token;
4174                next B;
4175              }
4176            } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4177              !!!cp ('t96');
4178              ## As if <head>
4179              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4180              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4181              push @{$self->{open_elements}},
4182                  [$self->{head_element}, $el_category->{head}];
4183    
4184                $self->{insertion_mode} = IN_HEAD_IM;            $self->{insertion_mode} = IN_HEAD_IM;
4185                ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4186              } else {          } else {
4187                !!!cp ('t97');            !!!cp ('t97');
4188              }          }
4189    
4190              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
4191                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
# Line 3507  sub _tree_construction_main ($) { Line 4204  sub _tree_construction_main ($) {
4204                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4205                  !!!cp ('t100');                  !!!cp ('t100');
4206                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4207                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4208                        [$self->{head_element}, $el_category->{head}];
4209                } else {                } else {
4210                  !!!cp ('t101');                  !!!cp ('t101');
4211                }                }
# Line 3515  sub _tree_construction_main ($) { Line 4213  sub _tree_construction_main ($) {
4213                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4214                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4215                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4216                  !!!nack ('t101.1');
4217                !!!next-token;                !!!next-token;
4218                redo B;                next B;
4219              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
4220                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4221                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4222                  !!!cp ('t102');                  !!!cp ('t102');
4223                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4224                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4225                        [$self->{head_element}, $el_category->{head}];
4226                } else {                } else {
4227                  !!!cp ('t103');                  !!!cp ('t103');
4228                }                }
# Line 3530  sub _tree_construction_main ($) { Line 4230  sub _tree_construction_main ($) {
4230                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4231                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4232                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4233                  !!!ack ('t103.1');
4234                !!!next-token;                !!!next-token;
4235                redo B;                next B;
4236              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
4237                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4238                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4239                  !!!cp ('t104');                  !!!cp ('t104');
4240                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4241                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4242                        [$self->{head_element}, $el_category->{head}];
4243                } else {                } else {
4244                  !!!cp ('t105');                  !!!cp ('t105');
4245                }                }
# Line 3545  sub _tree_construction_main ($) { Line 4247  sub _tree_construction_main ($) {
4247                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.
4248    
4249                unless ($self->{confident}) {                unless ($self->{confident}) {
4250                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) {
4251                    !!!cp ('t106');                    !!!cp ('t106');
4252                      ## NOTE: Whether the encoding is supported or not is handled
4253                      ## in the {change_encoding} callback.
4254                    $self->{change_encoding}                    $self->{change_encoding}
4255                        ->($self, $token->{attributes}->{charset}->{value},                        ->($self, $token->{attributes}->{charset}->{value},
4256                           $token);                           $token);
# Line 3556  sub _tree_construction_main ($) { Line 4260  sub _tree_construction_main ($) {
4260                                             $token->{attributes}->{charset}                                             $token->{attributes}->{charset}
4261                                                 ->{has_reference});                                                 ->{has_reference});
4262                  } elsif ($token->{attributes}->{content}) {                  } elsif ($token->{attributes}->{content}) {
                   ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
4263                    if ($token->{attributes}->{content}->{value}                    if ($token->{attributes}->{content}->{value}
4264                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4265                            [\x09-\x0D\x20]*=                            [\x09-\x0D\x20]*=
4266                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4267                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
4268                      !!!cp ('t107');                      !!!cp ('t107');
4269                        ## NOTE: Whether the encoding is supported or not is handled
4270                        ## in the {change_encoding} callback.
4271                      $self->{change_encoding}                      $self->{change_encoding}
4272                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4273                             $token);                             $token);
# Line 3593  sub _tree_construction_main ($) { Line 4298  sub _tree_construction_main ($) {
4298    
4299                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4300                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4301                  !!!ack ('t110.1');
4302                !!!next-token;                !!!next-token;
4303                redo B;                next B;
4304              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
4305                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4306                  !!!cp ('t111');                  !!!cp ('t111');
# Line 3607  sub _tree_construction_main ($) { Line 4313  sub _tree_construction_main ($) {
4313                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4314                  !!!cp ('t112');                  !!!cp ('t112');
4315                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4316                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4317                        [$self->{head_element}, $el_category->{head}];
4318                } else {                } else {
4319                  !!!cp ('t113');                  !!!cp ('t113');
4320                }                }
# Line 3618  sub _tree_construction_main ($) { Line 4325  sub _tree_construction_main ($) {
4325                $parse_rcdata->(RCDATA_CONTENT_MODEL);                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4326                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4327                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4328                redo B;                next B;
4329              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style') {
4330                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4331                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
# Line 3626  sub _tree_construction_main ($) { Line 4333  sub _tree_construction_main ($) {
4333                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4334                  !!!cp ('t114');                  !!!cp ('t114');
4335                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4336                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4337                        [$self->{head_element}, $el_category->{head}];
4338                } else {                } else {
4339                  !!!cp ('t115');                  !!!cp ('t115');
4340                }                }
4341                $parse_rcdata->(CDATA_CONTENT_MODEL);                $parse_rcdata->(CDATA_CONTENT_MODEL);
4342                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4343                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4344                redo B;                next B;
4345              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4346                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
4347                  !!!cp ('t116');                  !!!cp ('t116');
4348                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
4349                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4350                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4351                    !!!nack ('t116.1');
4352                  !!!next-token;                  !!!next-token;
4353                  redo B;                  next B;
4354                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4355                  !!!cp ('t117');                  !!!cp ('t117');
4356                  !!!parse-error (type => 'in noscript:noscript', token => $token);                  !!!parse-error (type => 'in noscript:noscript', token => $token);
4357                  ## Ignore the token                  ## Ignore the token
4358                    !!!nack ('t117.1');
4359                  !!!next-token;                  !!!next-token;
4360                  redo B;                  next B;
4361                } else {                } else {
4362                  !!!cp ('t118');                  !!!cp ('t118');
4363                  #                  #
# Line 3664  sub _tree_construction_main ($) { Line 4374  sub _tree_construction_main ($) {
4374                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4375                  !!!cp ('t120');                  !!!cp ('t120');
4376                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4377                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4378                        [$self->{head_element}, $el_category->{head}];
4379                } else {                } else {
4380                  !!!cp ('t121');                  !!!cp ('t121');
4381                }                }
# Line 3673  sub _tree_construction_main ($) { Line 4384  sub _tree_construction_main ($) {
4384                $script_start_tag->();                $script_start_tag->();
4385                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4386                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4387                redo B;                next B;
4388              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
4389                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
4390                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
# Line 3707  sub _tree_construction_main ($) { Line 4418  sub _tree_construction_main ($) {
4418                } else {                } else {
4419                  die "$0: tag name: $self->{tag_name}";                  die "$0: tag name: $self->{tag_name}";
4420                }                }
4421                  !!!nack ('t127.1');
4422                !!!next-token;                !!!next-token;
4423                redo B;                next B;
4424              } else {              } else {
4425                !!!cp ('t128');                !!!cp ('t128');
4426                #                #
# Line 3740  sub _tree_construction_main ($) { Line 4452  sub _tree_construction_main ($) {
4452              !!!insert-element ('body',, $token);              !!!insert-element ('body',, $token);
4453              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4454              ## reprocess              ## reprocess
4455              redo B;              !!!ack-later;
4456                next B;
4457            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
4458              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
4459                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4460                  !!!cp ('t132');                  !!!cp ('t132');
4461                  ## As if <head>                  ## As if <head>
4462                  !!!create-element ($self->{head_element}, 'head',, $token);                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4463                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4464                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4465                        [$self->{head_element}, $el_category->{head}];
4466    
4467                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4468                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4469                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4470                  !!!next-token;                  !!!next-token;
4471                  redo B;                  next B;
4472                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4473                  !!!cp ('t133');                  !!!cp ('t133');
4474                  ## As if </noscript>                  ## As if </noscript>
# Line 3765  sub _tree_construction_main ($) { Line 4479  sub _tree_construction_main ($) {
4479                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4480                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4481                  !!!next-token;                  !!!next-token;
4482                  redo B;                  next B;
4483                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4484                  !!!cp ('t134');                  !!!cp ('t134');
4485                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4486                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4487                  !!!next-token;                  !!!next-token;
4488                  redo B;                  next B;
4489                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4490                    !!!cp ('t134.1');
4491                    !!!parse-error (type => 'unmatched end tag:head', token => $token);
4492                    ## Ignore the token
4493                    !!!next-token;
4494                    next B;
4495                } else {                } else {
4496                  !!!cp ('t135');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
                 #  
4497                }                }
4498              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4499                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
# Line 3782  sub _tree_construction_main ($) { Line 4501  sub _tree_construction_main ($) {
4501                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4502                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4503                  !!!next-token;                  !!!next-token;
4504                  redo B;                  next B;
4505                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
4506                           $self->{insertion_mode} == AFTER_HEAD_IM) {
4507                  !!!cp ('t137');                  !!!cp ('t137');
4508                  !!!parse-error (type => 'unmatched end tag:noscript', token => $token);                  !!!parse-error (type => 'unmatched end tag:noscript', token => $token);
4509                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
4510                  !!!next-token;                  !!!next-token;
4511                  redo B;                  next B;
4512                } else {                } else {
4513                  !!!cp ('t138');                  !!!cp ('t138');
4514                  #                  #
# Line 3796  sub _tree_construction_main ($) { Line 4516  sub _tree_construction_main ($) {
4516              } elsif ({              } elsif ({
4517                        body => 1, html => 1,                        body => 1, html => 1,
4518                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4519                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM or
4520                  !!!cp ('t139');                    $self->{insertion_mode} == IN_HEAD_IM or
4521                  ## 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) {  
4522                  !!!cp ('t140');                  !!!cp ('t140');
4523                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4524                  ## Ignore the token                  ## Ignore the token
4525                  !!!next-token;                  !!!next-token;
4526                  redo B;                  next B;
4527                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4528                    !!!cp ('t140.1');
4529                    !!!parse-error (type => 'unmatched end tag:' . $token->{tag_name}, token => $token);
4530                    ## Ignore the token
4531                    !!!next-token;
4532                    next B;
4533                } else {                } else {
4534                  !!!cp ('t141');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
4535                }                }
4536                              } elsif ($token->{tag_name} eq 'p') {
4537                #                !!!cp ('t142');
4538              } elsif ({                !!!parse-error (type => 'unmatched end tag:p', token => $token);
4539                        p => 1, br => 1,                ## Ignore the token
4540                       }->{$token->{tag_name}}) {                !!!next-token;
4541                  next B;
4542                } elsif ($token->{tag_name} eq 'br') {
4543                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4544                  !!!cp ('t142');                  !!!cp ('t142.2');
4545                  ## As if <head>                  ## (before head) as if <head>, (in head) as if </head>
4546                  !!!create-element ($self->{head_element}, 'head',, $token);                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4547                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4548                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  $self->{insertion_mode} = AFTER_HEAD_IM;
4549      
4550                    ## Reprocess in the "after head" insertion mode...
4551                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4552                    !!!cp ('t143.2');
4553                    ## As if </head>
4554                    pop @{$self->{open_elements}};
4555                    $self->{insertion_mode} = AFTER_HEAD_IM;
4556      
4557                    ## Reprocess in the "after head" insertion mode...
4558                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4559                    !!!cp ('t143.3');
4560                    ## ISSUE: Two parse errors for <head><noscript></br>
4561                    !!!parse-error (type => 'unmatched end tag:br', token => $token);
4562                    ## As if </noscript>
4563                    pop @{$self->{open_elements}};
4564                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4565    
4566                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4567                } else {                  ## As if </head>
4568                  !!!cp ('t143');                  pop @{$self->{open_elements}};
4569                }                  $self->{insertion_mode} = AFTER_HEAD_IM;
4570    
4571                #                  ## Reprocess in the "after head" insertion mode...
4572              } else {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4573                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                  !!!cp ('t143.4');
                 !!!cp ('t144');  
4574                  #                  #
4575                } else {                } else {
4576                  !!!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;  
4577                }                }
4578    
4579                  ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
4580                  !!!parse-error (type => 'unmatched end tag:br', token => $token);
4581                  ## Ignore the token
4582                  !!!next-token;
4583                  next B;
4584                } else {
4585                  !!!cp ('t145');
4586                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4587                  ## Ignore the token
4588                  !!!next-token;
4589                  next B;
4590              }              }
4591    
4592              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
# Line 3869  sub _tree_construction_main ($) { Line 4612  sub _tree_construction_main ($) {
4612                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4613                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
4614                !!!next-token;                !!!next-token;
4615                redo B;                next B;
4616              } else {              } else {
4617                !!!cp ('t149');                !!!cp ('t149');
4618              }              }
# Line 3879  sub _tree_construction_main ($) { Line 4622  sub _tree_construction_main ($) {
4622              !!!insert-element ('body',, $token);              !!!insert-element ('body',, $token);
4623              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4624              ## reprocess              ## reprocess
4625              redo B;              next B;
4626        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4627          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4628            !!!cp ('t149.1');            !!!cp ('t149.1');
4629    
4630            ## NOTE: As if <head>            ## NOTE: As if <head>
4631            !!!create-element ($self->{head_element}, 'head',, $token);            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4632            $self->{open_elements}->[-1]->[0]->append_child            $self->{open_elements}->[-1]->[0]->append_child
4633                ($self->{head_element});                ($self->{head_element});
4634            #push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            #push @{$self->{open_elements}},
4635              #    [$self->{head_element}, $el_category->{head}];
4636            #$self->{insertion_mode} = IN_HEAD_IM;            #$self->{insertion_mode} = IN_HEAD_IM;
4637            ## NOTE: Reprocess.            ## NOTE: Reprocess.
4638    
# Line 3932  sub _tree_construction_main ($) { Line 4676  sub _tree_construction_main ($) {
4676          !!!insert-element ('body',, $token);          !!!insert-element ('body',, $token);
4677          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4678          ## NOTE: Reprocess.          ## NOTE: Reprocess.
4679          redo B;          next B;
4680        } else {        } else {
4681          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
4682        }        }
# Line 3947  sub _tree_construction_main ($) { Line 4691  sub _tree_construction_main ($) {
4691              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4692    
4693              !!!next-token;              !!!next-token;
4694              redo B;              next B;
4695            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
4696              if ({              if ({
4697                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
# Line 3957  sub _tree_construction_main ($) { Line 4701  sub _tree_construction_main ($) {
4701                  ## have an element in table scope                  ## have an element in table scope
4702                  for (reverse 0..$#{$self->{open_elements}}) {                  for (reverse 0..$#{$self->{open_elements}}) {
4703                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4704                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] & TABLE_CELL_EL) {
4705                      !!!cp ('t151');                      !!!cp ('t151');
4706    
4707                      ## Close the cell                      ## Close the cell
4708                      !!!back-token; # <?>                      !!!back-token; # <x>
4709                      $token = {type => END_TAG_TOKEN, tag_name => $node->[1],                      $token = {type => END_TAG_TOKEN,
4710                                  tag_name => $node->[0]->manakai_local_name,
4711                                line => $token->{line},                                line => $token->{line},
4712                                column => $token->{column}};                                column => $token->{column}};
4713                      redo B;                      next B;
4714                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
4715                      !!!cp ('t152');                      !!!cp ('t152');
4716                      ## ISSUE: This case can never be reached, maybe.                      ## ISSUE: This case can never be reached, maybe.
4717                      last;                      last;
# Line 3979  sub _tree_construction_main ($) { Line 4722  sub _tree_construction_main ($) {
4722                  !!!parse-error (type => 'start tag not allowed',                  !!!parse-error (type => 'start tag not allowed',
4723                      value => $token->{tag_name}, token => $token);                      value => $token->{tag_name}, token => $token);
4724                  ## Ignore the token                  ## Ignore the token
4725                    !!!nack ('t153.1');
4726                  !!!next-token;                  !!!next-token;
4727                  redo B;                  next B;
4728                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4729                  !!!parse-error (type => 'not closed:caption', token => $token);                  !!!parse-error (type => 'not closed:caption', token => $token);
4730                                    
# Line 3990  sub _tree_construction_main ($) { Line 4734  sub _tree_construction_main ($) {
4734                  INSCOPE: {                  INSCOPE: {
4735                    for (reverse 0..$#{$self->{open_elements}}) {                    for (reverse 0..$#{$self->{open_elements}}) {
4736                      my $node = $self->{open_elements}->[$_];                      my $node = $self->{open_elements}->[$_];
4737                      if ($node->[1] eq 'caption') {                      if ($node->[1] & CAPTION_EL) {
4738                        !!!cp ('t155');                        !!!cp ('t155');
4739                        $i = $_;                        $i = $_;
4740                        last INSCOPE;                        last INSCOPE;
4741                      } elsif ({                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
                               table => 1, html => 1,  
                              }->{$node->[1]}) {  
4742                        !!!cp ('t156');                        !!!cp ('t156');
4743                        last;                        last;
4744                      }                      }
# Line 4006  sub _tree_construction_main ($) { Line 4748  sub _tree_construction_main ($) {
4748                    !!!parse-error (type => 'start tag not allowed',                    !!!parse-error (type => 'start tag not allowed',
4749                                    value => $token->{tag_name}, token => $token);                                    value => $token->{tag_name}, token => $token);
4750                    ## Ignore the token                    ## Ignore the token
4751                      !!!nack ('t157.1');
4752                    !!!next-token;                    !!!next-token;
4753                    redo B;                    next B;
4754                  } # INSCOPE                  } # INSCOPE
4755                                    
4756                  ## generate implied end tags                  ## generate implied end tags
4757                  while ({                  while ($self->{open_elements}->[-1]->[1]
4758                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4759                    !!!cp ('t158');                    !!!cp ('t158');
4760                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4761                  }                  }
4762    
4763                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4764                    !!!cp ('t159');                    !!!cp ('t159');
4765                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                    !!!parse-error (type => 'not closed',
4766                                      value => $self->{open_elements}->[-1]->[0]
4767                                          ->manakai_local_name,
4768                                      token => $token);
4769                  } else {                  } else {
4770                    !!!cp ('t160');                    !!!cp ('t160');
4771                  }                  }
# Line 4032  sub _tree_construction_main ($) { Line 4777  sub _tree_construction_main ($) {
4777                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4778                                    
4779                  ## reprocess                  ## reprocess
4780                  redo B;                  !!!ack-later;
4781                    next B;
4782                } else {                } else {
4783                  !!!cp ('t161');                  !!!cp ('t161');
4784                  #                  #
# Line 4048  sub _tree_construction_main ($) { Line 4794  sub _tree_construction_main ($) {
4794                  my $i;                  my $i;
4795                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4796                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4797                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4798                      !!!cp ('t163');                      !!!cp ('t163');
4799                      $i = $_;                      $i = $_;
4800                      last INSCOPE;                      last INSCOPE;
4801                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
4802                      !!!cp ('t164');                      !!!cp ('t164');
4803                      last INSCOPE;                      last INSCOPE;
4804                    }                    }
# Line 4064  sub _tree_construction_main ($) { Line 4808  sub _tree_construction_main ($) {
4808                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4809                      ## Ignore the token                      ## Ignore the token
4810                      !!!next-token;                      !!!next-token;
4811                      redo B;                      next B;
4812                    }                    }
4813                                    
4814                  ## generate implied end tags                  ## generate implied end tags
4815                  while ({                  while ($self->{open_elements}->[-1]->[1]
4816                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4817                    !!!cp ('t166');                    !!!cp ('t166');
4818                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4819                  }                  }
4820    
4821                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
4822                            ne $token->{tag_name}) {
4823                    !!!cp ('t167');                    !!!cp ('t167');
4824                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                    !!!parse-error (type => 'not closed',
4825                                      value => $self->{open_elements}->[-1]->[0]
4826                                          ->manakai_local_name,
4827                                      token => $token);
4828                  } else {                  } else {
4829                    !!!cp ('t168');                    !!!cp ('t168');
4830                  }                  }
# Line 4089  sub _tree_construction_main ($) { Line 4836  sub _tree_construction_main ($) {
4836                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
4837                                    
4838                  !!!next-token;                  !!!next-token;
4839                  redo B;                  next B;
4840                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4841                  !!!cp ('t169');                  !!!cp ('t169');
4842                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4843                  ## Ignore the token                  ## Ignore the token
4844                  !!!next-token;                  !!!next-token;
4845                  redo B;                  next B;
4846                } else {                } else {
4847                  !!!cp ('t170');                  !!!cp ('t170');
4848                  #                  #
# Line 4107  sub _tree_construction_main ($) { Line 4854  sub _tree_construction_main ($) {
4854                  INSCOPE: {                  INSCOPE: {
4855                    for (reverse 0..$#{$self->{open_elements}}) {                    for (reverse 0..$#{$self->{open_elements}}) {
4856                      my $node = $self->{open_elements}->[$_];                      my $node = $self->{open_elements}->[$_];
4857                      if ($node->[1] eq $token->{tag_name}) {                      if ($node->[1] & CAPTION_EL) {
4858                        !!!cp ('t171');                        !!!cp ('t171');
4859                        $i = $_;                        $i = $_;
4860                        last INSCOPE;                        last INSCOPE;
4861                      } elsif ({                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
                               table => 1, html => 1,  
                              }->{$node->[1]}) {  
4862                        !!!cp ('t172');                        !!!cp ('t172');
4863                        last;                        last;
4864                      }                      }
# Line 4124  sub _tree_construction_main ($) { Line 4869  sub _tree_construction_main ($) {
4869                                    value => $token->{tag_name}, token => $token);                                    value => $token->{tag_name}, token => $token);
4870                    ## Ignore the token                    ## Ignore the token
4871                    !!!next-token;                    !!!next-token;
4872                    redo B;                    next B;
4873                  } # INSCOPE                  } # INSCOPE
4874                                    
4875                  ## generate implied end tags                  ## generate implied end tags
4876                  while ({                  while ($self->{open_elements}->[-1]->[1]
4877                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4878                    !!!cp ('t174');                    !!!cp ('t174');
4879                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4880                  }                  }
4881                                    
4882                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4883                    !!!cp ('t175');                    !!!cp ('t175');
4884                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                    !!!parse-error (type => 'not closed',
4885                                      value => $self->{open_elements}->[-1]->[0]
4886                                          ->manakai_local_name,
4887                                      token => $token);
4888                  } else {                  } else {
4889                    !!!cp ('t176');                    !!!cp ('t176');
4890                  }                  }
# Line 4149  sub _tree_construction_main ($) { Line 4896  sub _tree_construction_main ($) {
4896                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4897                                    
4898                  !!!next-token;                  !!!next-token;
4899                  redo B;                  next B;
4900                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
4901                  !!!cp ('t177');                  !!!cp ('t177');
4902                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4903                  ## Ignore the token                  ## Ignore the token
4904                  !!!next-token;                  !!!next-token;
4905                  redo B;                  next B;
4906                } else {                } else {
4907                  !!!cp ('t178');                  !!!cp ('t178');
4908                  #                  #
# Line 4171  sub _tree_construction_main ($) { Line 4918  sub _tree_construction_main ($) {
4918                INSCOPE: {                INSCOPE: {
4919                  for (reverse 0..$#{$self->{open_elements}}) {                  for (reverse 0..$#{$self->{open_elements}}) {
4920                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4921                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4922                      !!!cp ('t179');                      !!!cp ('t179');
4923                      $i = $_;                      $i = $_;
4924    
4925                      ## Close the cell                      ## Close the cell
4926                      !!!back-token; # </?>                      !!!back-token; # </x>
4927                      $token = {type => END_TAG_TOKEN, tag_name => $tn,                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
4928                                line => $token->{line},                                line => $token->{line},
4929                                column => $token->{column}};                                column => $token->{column}};
4930                      redo B;                      next B;
4931                    } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {                    } elsif ($node->[1] & TABLE_CELL_EL) {
4932                      !!!cp ('t180');                      !!!cp ('t180');
4933                      $tn = $node->[1];                      $tn = $node->[0]->manakai_local_name;
4934                      ## NOTE: There is exactly one |td| or |th| element                      ## NOTE: There is exactly one |td| or |th| element
4935                      ## in scope in the stack of open elements by definition.                      ## in scope in the stack of open elements by definition.
4936                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
4937                      ## ISSUE: Can this be reached?                      ## ISSUE: Can this be reached?
4938                      !!!cp ('t181');                      !!!cp ('t181');
4939                      last;                      last;
# Line 4200  sub _tree_construction_main ($) { Line 4945  sub _tree_construction_main ($) {
4945                      value => $token->{tag_name}, token => $token);                      value => $token->{tag_name}, token => $token);
4946                  ## Ignore the token                  ## Ignore the token
4947                  !!!next-token;                  !!!next-token;
4948                  redo B;                  next B;
4949                } # INSCOPE                } # INSCOPE
4950              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
4951                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
# Line 4211  sub _tree_construction_main ($) { Line 4956  sub _tree_construction_main ($) {
4956                my $i;                my $i;
4957                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4958                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4959                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
4960                    !!!cp ('t184');                    !!!cp ('t184');
4961                    $i = $_;                    $i = $_;
4962                    last INSCOPE;                    last INSCOPE;
4963                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
4964                    !!!cp ('t185');                    !!!cp ('t185');
4965                    last INSCOPE;                    last INSCOPE;
4966                  }                  }
# Line 4227  sub _tree_construction_main ($) { Line 4970  sub _tree_construction_main ($) {
4970                  !!!parse-error (type => 'unmatched end tag:caption', token => $token);                  !!!parse-error (type => 'unmatched end tag:caption', token => $token);
4971                  ## Ignore the token                  ## Ignore the token
4972                  !!!next-token;                  !!!next-token;
4973                  redo B;                  next B;
4974                }                }
4975                                
4976                ## generate implied end tags                ## generate implied end tags
4977                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
4978                  !!!cp ('t187');                  !!!cp ('t187');
4979                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4980                }                }
4981    
4982                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4983                  !!!cp ('t188');                  !!!cp ('t188');
4984                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                  !!!parse-error (type => 'not closed',
4985                                    value => $self->{open_elements}->[-1]->[0]
4986                                        ->manakai_local_name,
4987                                    token => $token);
4988                } else {                } else {
4989                  !!!cp ('t189');                  !!!cp ('t189');
4990                }                }
# Line 4252  sub _tree_construction_main ($) { Line 4996  sub _tree_construction_main ($) {
4996                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
4997    
4998                ## reprocess                ## reprocess
4999                redo B;                next B;
5000              } elsif ({              } elsif ({
5001                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
5002                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
# Line 4261  sub _tree_construction_main ($) { Line 5005  sub _tree_construction_main ($) {
5005                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5006                  ## Ignore the token                  ## Ignore the token
5007                  !!!next-token;                  !!!next-token;
5008                  redo B;                  next B;
5009                } else {                } else {
5010                  !!!cp ('t191');                  !!!cp ('t191');
5011                  #                  #
# Line 4275  sub _tree_construction_main ($) { Line 5019  sub _tree_construction_main ($) {
5019                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5020                ## Ignore the token                ## Ignore the token
5021                !!!next-token;                !!!next-token;
5022                redo B;                next B;
5023              } else {              } else {
5024                !!!cp ('t193');                !!!cp ('t193');
5025                #                #
5026              }              }
5027        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5028          for my $entry (@{$self->{open_elements}}) {          for my $entry (@{$self->{open_elements}}) {
5029            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]}) {  
5030              !!!cp ('t75');              !!!cp ('t75');
5031              !!!parse-error (type => 'in body:#eof', token => $token);              !!!parse-error (type => 'in body:#eof', token => $token);
5032              last;              last;
# Line 4309  sub _tree_construction_main ($) { Line 5050  sub _tree_construction_main ($) {
5050            unless (length $token->{data}) {            unless (length $token->{data}) {
5051              !!!cp ('t194');              !!!cp ('t194');
5052              !!!next-token;              !!!next-token;
5053              redo B;              next B;
5054            } else {            } else {
5055              !!!cp ('t195');              !!!cp ('t195');
5056            }            }
# Line 4323  sub _tree_construction_main ($) { Line 5064  sub _tree_construction_main ($) {
5064              ## result in a new Text node.              ## result in a new Text node.
5065              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
5066                            
5067              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]}) {  
5068                # MUST                # MUST
5069                my $foster_parent_element;                my $foster_parent_element;
5070                my $next_sibling;                my $next_sibling;
5071                my $prev_sibling;                my $prev_sibling;
5072                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
5073                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
5074                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
5075                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
5076                      !!!cp ('t196');                      !!!cp ('t196');
# Line 4367  sub _tree_construction_main ($) { Line 5105  sub _tree_construction_main ($) {
5105          }          }
5106                            
5107          !!!next-token;          !!!next-token;
5108          redo B;          next B;
5109        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5110              if ({              if ({
5111                   tr => ($self->{insertion_mode} != IN_ROW_IM),                   tr => ($self->{insertion_mode} != IN_ROW_IM),
# Line 4375  sub _tree_construction_main ($) { Line 5113  sub _tree_construction_main ($) {
5113                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
5114                if ($self->{insertion_mode} == IN_TABLE_IM) {                if ($self->{insertion_mode} == IN_TABLE_IM) {
5115                  ## Clear back to table context                  ## Clear back to table context
5116                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5117                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5118                    !!!cp ('t201');                    !!!cp ('t201');
5119                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5120                  }                  }
# Line 4393  sub _tree_construction_main ($) { Line 5131  sub _tree_construction_main ($) {
5131                  }                  }
5132                                    
5133                  ## Clear back to table body context                  ## Clear back to table body context
5134                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5135                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5136                    !!!cp ('t203');                    !!!cp ('t203');
5137                    ## ISSUE: Can this case be reached?                    ## ISSUE: Can this case be reached?
5138                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4405  sub _tree_construction_main ($) { Line 5142  sub _tree_construction_main ($) {
5142                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5143                    !!!cp ('t204');                    !!!cp ('t204');
5144                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5145                      !!!nack ('t204');
5146                    !!!next-token;                    !!!next-token;
5147                    redo B;                    next B;
5148                  } else {                  } else {
5149                    !!!cp ('t205');                    !!!cp ('t205');
5150                    !!!insert-element ('tr',, $token);                    !!!insert-element ('tr',, $token);
# Line 4417  sub _tree_construction_main ($) { Line 5155  sub _tree_construction_main ($) {
5155                }                }
5156    
5157                ## Clear back to table row context                ## Clear back to table row context
5158                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5159                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5160                  !!!cp ('t207');                  !!!cp ('t207');
5161                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5162                }                }
# Line 4429  sub _tree_construction_main ($) { Line 5166  sub _tree_construction_main ($) {
5166    
5167                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
5168                                
5169                  !!!nack ('t207.1');
5170                !!!next-token;                !!!next-token;
5171                redo B;                next B;
5172              } elsif ({              } elsif ({
5173                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5174                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
# Line 4442  sub _tree_construction_main ($) { Line 5180  sub _tree_construction_main ($) {
5180                  my $i;                  my $i;
5181                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5182                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5183                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5184                      !!!cp ('t208');                      !!!cp ('t208');
5185                      $i = $_;                      $i = $_;
5186                      last INSCOPE;                      last INSCOPE;
5187                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             html => 1,  
   
                             ## NOTE: This element does not appear here, maybe.  
                             table => 1,  
                            }->{$node->[1]}) {  
5188                      !!!cp ('t209');                      !!!cp ('t209');
5189                      last INSCOPE;                      last INSCOPE;
5190                    }                    }
5191                  } # INSCOPE                  } # INSCOPE
5192                  unless (defined $i) {                  unless (defined $i) {
5193                   !!!cp ('t210');                    !!!cp ('t210');
5194  ## TODO: This type is wrong.  ## TODO: This type is wrong.
5195                   !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);                    !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);
5196                    ## Ignore the token                    ## Ignore the token
5197                      !!!nack ('t210.1');
5198                    !!!next-token;                    !!!next-token;
5199                    redo B;                    next B;
5200                  }                  }
5201                                    
5202                  ## Clear back to table row context                  ## Clear back to table row context
5203                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5204                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5205                    !!!cp ('t211');                    !!!cp ('t211');
5206                    ## ISSUE: Can this case be reached?                    ## ISSUE: Can this case be reached?
5207                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4479  sub _tree_construction_main ($) { Line 5212  sub _tree_construction_main ($) {
5212                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5213                    !!!cp ('t212');                    !!!cp ('t212');
5214                    ## reprocess                    ## reprocess
5215                    redo B;                    !!!ack-later;
5216                      next B;
5217                  } else {                  } else {
5218                    !!!cp ('t213');                    !!!cp ('t213');
5219                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
# Line 4491  sub _tree_construction_main ($) { Line 5225  sub _tree_construction_main ($) {
5225                  my $i;                  my $i;
5226                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5227                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5228                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5229                      !!!cp ('t214');                      !!!cp ('t214');
5230                      $i = $_;                      $i = $_;
5231                      last INSCOPE;                      last INSCOPE;
5232                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5233                      !!!cp ('t215');                      !!!cp ('t215');
5234                      last INSCOPE;                      last INSCOPE;
5235                    }                    }
# Line 4509  sub _tree_construction_main ($) { Line 5239  sub _tree_construction_main ($) {
5239  ## TODO: This erorr type ios wrong.  ## TODO: This erorr type ios wrong.
5240                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5241                    ## Ignore the token                    ## Ignore the token
5242                      !!!nack ('t216.1');
5243                    !!!next-token;                    !!!next-token;
5244                    redo B;                    next B;
5245                  }                  }
5246    
5247                  ## Clear back to table body context                  ## Clear back to table body context
5248                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5249                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5250                    !!!cp ('t217');                    !!!cp ('t217');
5251                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5252                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4538  sub _tree_construction_main ($) { Line 5268  sub _tree_construction_main ($) {
5268    
5269                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
5270                  ## Clear back to table context                  ## Clear back to table context
5271                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5272                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5273                    !!!cp ('t219');                    !!!cp ('t219');
5274                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5275                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4548  sub _tree_construction_main ($) { Line 5278  sub _tree_construction_main ($) {
5278                  !!!insert-element ('colgroup',, $token);                  !!!insert-element ('colgroup',, $token);
5279                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5280                  ## reprocess                  ## reprocess
5281                  redo B;                  !!!ack-later;
5282                    next B;
5283                } elsif ({                } elsif ({
5284                          caption => 1,                          caption => 1,
5285                          colgroup => 1,                          colgroup => 1,
5286                          tbody => 1, tfoot => 1, thead => 1,                          tbody => 1, tfoot => 1, thead => 1,
5287                         }->{$token->{tag_name}}) {                         }->{$token->{tag_name}}) {
5288                  ## Clear back to table context                  ## Clear back to table context
5289                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5290                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5291                    !!!cp ('t220');                    !!!cp ('t220');
5292                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5293                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4574  sub _tree_construction_main ($) { Line 5305  sub _tree_construction_main ($) {
5305                                             thead => IN_TABLE_BODY_IM,                                             thead => IN_TABLE_BODY_IM,
5306                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
5307                  !!!next-token;                  !!!next-token;
5308                  redo B;                  !!!nack ('t220.1');
5309                    next B;
5310                } else {                } else {
5311                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
5312                }                }
5313              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5314                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                !!!parse-error (type => 'not closed',
5315                                  value => $self->{open_elements}->[-1]->[0]
5316                                      ->manakai_local_name,
5317                                  token => $token);
5318    
5319                ## As if </table>                ## As if </table>
5320                ## have a table element in table scope                ## have a table element in table scope
5321                my $i;                my $i;
5322                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5323                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5324                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5325                    !!!cp ('t221');                    !!!cp ('t221');
5326                    $i = $_;                    $i = $_;
5327                    last INSCOPE;                    last INSCOPE;
5328                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           #table => 1,  
                           html => 1,  
                          }->{$node->[1]}) {  
5329                    !!!cp ('t222');                    !!!cp ('t222');
5330                    last INSCOPE;                    last INSCOPE;
5331                  }                  }
# Line 4603  sub _tree_construction_main ($) { Line 5335  sub _tree_construction_main ($) {
5335  ## TODO: The following is wrong, maybe.  ## TODO: The following is wrong, maybe.
5336                  !!!parse-error (type => 'unmatched end tag:table', token => $token);                  !!!parse-error (type => 'unmatched end tag:table', token => $token);
5337                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5338                    !!!nack ('t223.1');
5339                  !!!next-token;                  !!!next-token;
5340                  redo B;                  next B;
5341                }                }
5342                                
5343  ## TODO: Followings are removed from the latest spec.  ## TODO: Followings are removed from the latest spec.
5344                ## generate implied end tags                ## generate implied end tags
5345                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
5346                  !!!cp ('t224');                  !!!cp ('t224');
5347                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5348                }                }
5349    
5350                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5351                  !!!cp ('t225');                  !!!cp ('t225');
5352  ## ISSUE: Can this case be reached?                  ## NOTE: |<table><tr><table>|
5353                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                  !!!parse-error (type => 'not closed',
5354                                    value => $self->{open_elements}->[-1]->[0]
5355                                        ->manakai_local_name,
5356                                    token => $token);
5357                } else {                } else {
5358                  !!!cp ('t226');                  !!!cp ('t226');
5359                }                }
# Line 4629  sub _tree_construction_main ($) { Line 5363  sub _tree_construction_main ($) {
5363    
5364                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5365    
5366                ## reprocess            ## reprocess
5367                redo B;            !!!ack-later;
5368              next B;
5369          } elsif ($token->{tag_name} eq 'style') {          } elsif ($token->{tag_name} eq 'style') {
5370            if (not $open_tables->[-1]->[1]) { # tainted            if (not $open_tables->[-1]->[1]) { # tainted
5371              !!!cp ('t227.8');              !!!cp ('t227.8');
5372              ## NOTE: This is a "as if in head" code clone.              ## NOTE: This is a "as if in head" code clone.
5373              $parse_rcdata->(CDATA_CONTENT_MODEL);              $parse_rcdata->(CDATA_CONTENT_MODEL);
5374              redo B;              next B;
5375            } else {            } else {
5376              !!!cp ('t227.7');              !!!cp ('t227.7');
5377              #              #
# Line 4646  sub _tree_construction_main ($) { Line 5381  sub _tree_construction_main ($) {
5381              !!!cp ('t227.6');              !!!cp ('t227.6');
5382              ## NOTE: This is a "as if in head" code clone.              ## NOTE: This is a "as if in head" code clone.
5383              $script_start_tag->();              $script_start_tag->();
5384              redo B;              next B;
5385            } else {            } else {
5386              !!!cp ('t227.5');              !!!cp ('t227.5');
5387              #              #
# Line 4666  sub _tree_construction_main ($) { Line 5401  sub _tree_construction_main ($) {
5401                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5402    
5403                  !!!next-token;                  !!!next-token;
5404                  redo B;                  !!!ack ('t227.2.1');
5405                    next B;
5406                } else {                } else {
5407                  !!!cp ('t227.2');                  !!!cp ('t227.2');
5408                  #                  #
# Line 4695  sub _tree_construction_main ($) { Line 5431  sub _tree_construction_main ($) {
5431                my $i;                my $i;
5432                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5433                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5434                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_ROW_EL) {
5435                    !!!cp ('t228');                    !!!cp ('t228');
5436                    $i = $_;                    $i = $_;
5437                    last INSCOPE;                    last INSCOPE;
5438                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5439                    !!!cp ('t229');                    !!!cp ('t229');
5440                    last INSCOPE;                    last INSCOPE;
5441                  }                  }
# Line 4710  sub _tree_construction_main ($) { Line 5444  sub _tree_construction_main ($) {
5444                  !!!cp ('t230');                  !!!cp ('t230');
5445                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5446                  ## Ignore the token                  ## Ignore the token
5447                    !!!nack ('t230.1');
5448                  !!!next-token;                  !!!next-token;
5449                  redo B;                  next B;
5450                } else {                } else {
5451                  !!!cp ('t232');                  !!!cp ('t232');
5452                }                }
5453    
5454                ## Clear back to table row context                ## Clear back to table row context
5455                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5456                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5457                  !!!cp ('t231');                  !!!cp ('t231');
5458  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
5459                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 4728  sub _tree_construction_main ($) { Line 5462  sub _tree_construction_main ($) {
5462                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5463                $self->{insertion_mode} = IN_TABLE_BODY_IM;                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5464                !!!next-token;                !!!next-token;
5465                redo B;                !!!nack ('t231.1');
5466                  next B;
5467              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5468                if ($self->{insertion_mode} == IN_ROW_IM) {                if ($self->{insertion_mode} == IN_ROW_IM) {
5469                  ## As if </tr>                  ## As if </tr>
# Line 4736  sub _tree_construction_main ($) { Line 5471  sub _tree_construction_main ($) {
5471                  my $i;                  my $i;
5472                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5473                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5474                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5475                      !!!cp ('t233');                      !!!cp ('t233');
5476                      $i = $_;                      $i = $_;
5477                      last INSCOPE;                      last INSCOPE;
5478                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5479                      !!!cp ('t234');                      !!!cp ('t234');
5480                      last INSCOPE;                      last INSCOPE;
5481                    }                    }
# Line 4752  sub _tree_construction_main ($) { Line 5485  sub _tree_construction_main ($) {
5485  ## TODO: The following is wrong.  ## TODO: The following is wrong.
5486                    !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);                    !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);
5487                    ## Ignore the token                    ## Ignore the token
5488                      !!!nack ('t236.1');
5489                    !!!next-token;                    !!!next-token;
5490                    redo B;                    next B;
5491                  }                  }
5492                                    
5493                  ## Clear back to table row context                  ## Clear back to table row context
5494                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5495                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5496                    !!!cp ('t236');                    !!!cp ('t236');
5497  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
5498                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4775  sub _tree_construction_main ($) { Line 5508  sub _tree_construction_main ($) {
5508                  my $i;                  my $i;
5509                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5510                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5511                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5512                      !!!cp ('t237');                      !!!cp ('t237');
5513                      $i = $_;                      $i = $_;
5514                      last INSCOPE;                      last INSCOPE;
5515                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5516                      !!!cp ('t238');                      !!!cp ('t238');
5517                      last INSCOPE;                      last INSCOPE;
5518                    }                    }
# Line 4792  sub _tree_construction_main ($) { Line 5521  sub _tree_construction_main ($) {
5521                    !!!cp ('t239');                    !!!cp ('t239');
5522                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5523                    ## Ignore the token                    ## Ignore the token
5524                      !!!nack ('t239.1');
5525                    !!!next-token;                    !!!next-token;
5526                    redo B;                    next B;
5527                  }                  }
5528                                    
5529                  ## Clear back to table body context                  ## Clear back to table body context
5530                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5531                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5532                    !!!cp ('t240');                    !!!cp ('t240');
5533                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5534                  }                  }
# Line 4825  sub _tree_construction_main ($) { Line 5554  sub _tree_construction_main ($) {
5554                my $i;                my $i;
5555                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5556                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5557                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_EL) {
5558                    !!!cp ('t241');                    !!!cp ('t241');
5559                    $i = $_;                    $i = $_;
5560                    last INSCOPE;                    last INSCOPE;
5561                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5562                    !!!cp ('t242');                    !!!cp ('t242');
5563                    last INSCOPE;                    last INSCOPE;
5564                  }                  }
# Line 4840  sub _tree_construction_main ($) { Line 5567  sub _tree_construction_main ($) {
5567                  !!!cp ('t243');                  !!!cp ('t243');
5568                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5569                  ## Ignore the token                  ## Ignore the token
5570                    !!!nack ('t243.1');
5571                  !!!next-token;                  !!!next-token;
5572                  redo B;                  next B;
5573                }                }
5574                                    
5575                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
# Line 4850  sub _tree_construction_main ($) { Line 5578  sub _tree_construction_main ($) {
5578                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5579                                
5580                !!!next-token;                !!!next-token;
5581                redo B;                next B;
5582              } elsif ({              } elsif ({
5583                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5584                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
# Line 4860  sub _tree_construction_main ($) { Line 5588  sub _tree_construction_main ($) {
5588                  my $i;                  my $i;
5589                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5590                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5591                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5592                      !!!cp ('t247');                      !!!cp ('t247');
5593                      $i = $_;                      $i = $_;
5594                      last INSCOPE;                      last INSCOPE;
5595                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5596                      !!!cp ('t248');                      !!!cp ('t248');
5597                      last INSCOPE;                      last INSCOPE;
5598                    }                    }
# Line 4875  sub _tree_construction_main ($) { Line 5601  sub _tree_construction_main ($) {
5601                      !!!cp ('t249');                      !!!cp ('t249');
5602                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5603                      ## Ignore the token                      ## Ignore the token
5604                        !!!nack ('t249.1');
5605                      !!!next-token;                      !!!next-token;
5606                      redo B;                      next B;
5607                    }                    }
5608                                    
5609                  ## As if </tr>                  ## As if </tr>
# Line 4884  sub _tree_construction_main ($) { Line 5611  sub _tree_construction_main ($) {
5611                  my $i;                  my $i;
5612                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5613                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5614                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5615                      !!!cp ('t250');                      !!!cp ('t250');
5616                      $i = $_;                      $i = $_;
5617                      last INSCOPE;                      last INSCOPE;
5618                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5619                      !!!cp ('t251');                      !!!cp ('t251');
5620                      last INSCOPE;                      last INSCOPE;
5621                    }                    }
# Line 4899  sub _tree_construction_main ($) { Line 5624  sub _tree_construction_main ($) {
5624                      !!!cp ('t252');                      !!!cp ('t252');
5625                      !!!parse-error (type => 'unmatched end tag:tr', token => $token);                      !!!parse-error (type => 'unmatched end tag:tr', token => $token);
5626                      ## Ignore the token                      ## Ignore the token
5627                        !!!nack ('t252.1');
5628                      !!!next-token;                      !!!next-token;
5629                      redo B;                      next B;
5630                    }                    }
5631                                    
5632                  ## Clear back to table row context                  ## Clear back to table row context
5633                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5634                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5635                    !!!cp ('t253');                    !!!cp ('t253');
5636  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
5637                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4921  sub _tree_construction_main ($) { Line 5646  sub _tree_construction_main ($) {
5646                my $i;                my $i;
5647                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5648                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5649                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5650                    !!!cp ('t254');                    !!!cp ('t254');
5651                    $i = $_;                    $i = $_;
5652                    last INSCOPE;                    last INSCOPE;
5653                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5654                    !!!cp ('t255');                    !!!cp ('t255');
5655                    last INSCOPE;                    last INSCOPE;
5656                  }                  }
# Line 4936  sub _tree_construction_main ($) { Line 5659  sub _tree_construction_main ($) {
5659                  !!!cp ('t256');                  !!!cp ('t256');
5660                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5661                  ## Ignore the token                  ## Ignore the token
5662                    !!!nack ('t256.1');
5663                  !!!next-token;                  !!!next-token;
5664                  redo B;                  next B;
5665                }                }
5666    
5667                ## Clear back to table body context                ## Clear back to table body context
5668                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5669                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5670                  !!!cp ('t257');                  !!!cp ('t257');
5671  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
5672                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 4951  sub _tree_construction_main ($) { Line 5674  sub _tree_construction_main ($) {
5674    
5675                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5676                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5677                  !!!nack ('t257.1');
5678                !!!next-token;                !!!next-token;
5679                redo B;                next B;
5680              } elsif ({              } elsif ({
5681                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
5682                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
5683                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5684                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
5685                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5686                !!!cp ('t258');            !!!cp ('t258');
5687                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5688                ## Ignore the token            ## Ignore the token
5689                !!!next-token;            !!!nack ('t258.1');
5690                redo B;             !!!next-token;
5691              next B;
5692          } else {          } else {
5693            !!!cp ('t259');            !!!cp ('t259');
5694            !!!parse-error (type => 'in table:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'in table:/'.$token->{tag_name}, token => $token);
# Line 4972  sub _tree_construction_main ($) { Line 5697  sub _tree_construction_main ($) {
5697            #            #
5698          }          }
5699        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5700          unless ($self->{open_elements}->[-1]->[1] eq 'html' and          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5701                  @{$self->{open_elements}} == 1) { # redundant, maybe                  @{$self->{open_elements}} == 1) { # redundant, maybe
5702            !!!parse-error (type => 'in body:#eof', token => $token);            !!!parse-error (type => 'in body:#eof', token => $token);
5703            !!!cp ('t259.1');            !!!cp ('t259.1');
# Line 4994  sub _tree_construction_main ($) { Line 5719  sub _tree_construction_main ($) {
5719                unless (length $token->{data}) {                unless (length $token->{data}) {
5720                  !!!cp ('t260');                  !!!cp ('t260');
5721                  !!!next-token;                  !!!next-token;
5722                  redo B;                  next B;
5723                }                }
5724              }              }
5725                            
# Line 5005  sub _tree_construction_main ($) { Line 5730  sub _tree_construction_main ($) {
5730                !!!cp ('t262');                !!!cp ('t262');
5731                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5732                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5733                  !!!ack ('t262.1');
5734                !!!next-token;                !!!next-token;
5735                redo B;                next B;
5736              } else {              } else {
5737                !!!cp ('t263');                !!!cp ('t263');
5738                #                #
5739              }              }
5740            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
5741              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
5742                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5743                  !!!cp ('t264');                  !!!cp ('t264');
5744                  !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);                  !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5745                  ## Ignore the token                  ## Ignore the token
5746                  !!!next-token;                  !!!next-token;
5747                  redo B;                  next B;
5748                } else {                } else {
5749                  !!!cp ('t265');                  !!!cp ('t265');
5750                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
5751                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5752                  !!!next-token;                  !!!next-token;
5753                  redo B;                              next B;            
5754                }                }
5755              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
5756                !!!cp ('t266');                !!!cp ('t266');
5757                !!!parse-error (type => 'unmatched end tag:col', token => $token);                !!!parse-error (type => 'unmatched end tag:col', token => $token);
5758                ## Ignore the token                ## Ignore the token
5759                !!!next-token;                !!!next-token;
5760                redo B;                next B;
5761              } else {              } else {
5762                !!!cp ('t267');                !!!cp ('t267');
5763                #                #
5764              }              }
5765        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5766          if ($self->{open_elements}->[-1]->[1] eq 'html' or          if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5767              @{$self->{open_elements}} == 1) { # redundant, maybe              @{$self->{open_elements}} == 1) { # redundant, maybe
5768            !!!cp ('t270.2');            !!!cp ('t270.2');
5769            ## Stop parsing.            ## Stop parsing.
# Line 5048  sub _tree_construction_main ($) { Line 5774  sub _tree_construction_main ($) {
5774            pop @{$self->{open_elements}}; # colgroup            pop @{$self->{open_elements}}; # colgroup
5775            $self->{insertion_mode} = IN_TABLE_IM;            $self->{insertion_mode} = IN_TABLE_IM;
5776            ## Reprocess.            ## Reprocess.
5777            redo B;            next B;
5778          }          }
5779        } else {        } else {
5780          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5781        }        }
5782    
5783            ## As if </colgroup>            ## As if </colgroup>
5784            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5785              !!!cp ('t269');              !!!cp ('t269');
5786  ## TODO: Wrong error type?  ## TODO: Wrong error type?
5787              !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);              !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5788              ## Ignore the token              ## Ignore the token
5789                !!!nack ('t269.1');
5790              !!!next-token;              !!!next-token;
5791              redo B;              next B;
5792            } else {            } else {
5793              !!!cp ('t270');              !!!cp ('t270');
5794              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
5795              $self->{insertion_mode} = IN_TABLE_IM;              $self->{insertion_mode} = IN_TABLE_IM;
5796                !!!ack-later;
5797              ## reprocess              ## reprocess
5798              redo B;              next B;
5799            }            }
5800      } elsif ($self->{insertion_mode} & SELECT_IMS) {      } elsif ($self->{insertion_mode} & SELECT_IMS) {
5801        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5802          !!!cp ('t271');          !!!cp ('t271');
5803          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5804          !!!next-token;          !!!next-token;
5805          redo B;          next B;
5806        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5807              if ($token->{tag_name} eq 'option') {          if ($token->{tag_name} eq 'option') {
5808                if ($self->{open_elements}->[-1]->[1] eq 'option') {            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5809                  !!!cp ('t272');              !!!cp ('t272');
5810                  ## As if </option>              ## As if </option>
5811                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5812                } else {            } else {
5813                  !!!cp ('t273');              !!!cp ('t273');
5814                }            }
5815    
5816                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5817                !!!next-token;            !!!nack ('t273.1');
5818                redo B;            !!!next-token;
5819              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
5820                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
5821                  !!!cp ('t274');            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5822                  ## As if </option>              !!!cp ('t274');
5823                  pop @{$self->{open_elements}};              ## As if </option>
5824                } else {              pop @{$self->{open_elements}};
5825                  !!!cp ('t275');            } else {
5826                }              !!!cp ('t275');
5827              }
5828    
5829                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5830                  !!!cp ('t276');              !!!cp ('t276');
5831                  ## As if </optgroup>              ## As if </optgroup>
5832                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5833                } else {            } else {
5834                  !!!cp ('t277');              !!!cp ('t277');
5835                }            }
5836    
5837                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5838                !!!next-token;            !!!nack ('t277.1');
5839                redo B;            !!!next-token;
5840              next B;
5841          } elsif ($token->{tag_name} eq 'select' or          } elsif ($token->{tag_name} eq 'select' or
5842                   $token->{tag_name} eq 'input' or                   $token->{tag_name} eq 'input' or
5843                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
# Line 5120  sub _tree_construction_main ($) { Line 5850  sub _tree_construction_main ($) {
5850            !!!parse-error (type => 'not closed:select', token => $token);            !!!parse-error (type => 'not closed:select', token => $token);
5851            ## NOTE: As if the token were </select> (<select> case) or            ## NOTE: As if the token were </select> (<select> case) or
5852            ## as if there were </select> (otherwise).            ## as if there were </select> (otherwise).
5853                ## have an element in table scope            ## have an element in table scope
5854                my $i;            my $i;
5855                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5856                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
5857                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
5858                    !!!cp ('t278');                !!!cp ('t278');
5859                    $i = $_;                $i = $_;
5860                    last INSCOPE;                last INSCOPE;
5861                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5862                            table => 1, html => 1,                !!!cp ('t279');
5863                           }->{$node->[1]}) {                last INSCOPE;
5864                    !!!cp ('t279');              }
5865                    last INSCOPE;            } # INSCOPE
5866                  }            unless (defined $i) {
5867                } # INSCOPE              !!!cp ('t280');
5868                unless (defined $i) {              !!!parse-error (type => 'unmatched end tag:select', token => $token);
5869                  !!!cp ('t280');              ## Ignore the token
5870                  !!!parse-error (type => 'unmatched end tag:select', token => $token);              !!!nack ('t280.1');
5871                  ## Ignore the token              !!!next-token;
5872                  !!!next-token;              next B;
5873                  redo B;            }
               }  
5874                                
5875                !!!cp ('t281');            !!!cp ('t281');
5876                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
5877    
5878                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5879    
5880            if ($token->{tag_name} eq 'select') {            if ($token->{tag_name} eq 'select') {
5881              !!!cp ('t281.2');              !!!nack ('t281.2');
5882              !!!next-token;              !!!next-token;
5883              redo B;              next B;
5884            } else {            } else {
5885              !!!cp ('t281.1');              !!!cp ('t281.1');
5886                !!!ack-later;
5887              ## Reprocess the token.              ## Reprocess the token.
5888              redo B;              next B;
5889            }            }
5890          } else {          } else {
5891            !!!cp ('t282');            !!!cp ('t282');
5892            !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);
5893            ## Ignore the token            ## Ignore the token
5894              !!!nack ('t282.1');
5895            !!!next-token;            !!!next-token;
5896            redo B;            next B;
5897          }          }
5898        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5899              if ($token->{tag_name} eq 'optgroup') {          if ($token->{tag_name} eq 'optgroup') {
5900                if ($self->{open_elements}->[-1]->[1] eq 'option' and            if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
5901                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
5902                  !!!cp ('t283');              !!!cp ('t283');
5903                  ## As if </option>              ## As if </option>
5904                  splice @{$self->{open_elements}}, -2;              splice @{$self->{open_elements}}, -2;
5905                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5906                  !!!cp ('t284');              !!!cp ('t284');
5907                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5908                } else {            } else {
5909                  !!!cp ('t285');              !!!cp ('t285');
5910                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5911                  ## Ignore the token              ## Ignore the token
5912                }            }
5913                !!!next-token;            !!!nack ('t285.1');
5914                redo B;            !!!next-token;
5915              } elsif ($token->{tag_name} eq 'option') {            next B;
5916                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'option') {
5917                  !!!cp ('t286');            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5918                  pop @{$self->{open_elements}};              !!!cp ('t286');
5919                } else {              pop @{$self->{open_elements}};
5920                  !!!cp ('t287');            } else {
5921                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!cp ('t287');
5922                  ## Ignore the token              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5923                }              ## Ignore the token
5924                !!!next-token;            }
5925                redo B;            !!!nack ('t287.1');
5926              } elsif ($token->{tag_name} eq 'select') {            !!!next-token;
5927                ## have an element in table scope            next B;
5928                my $i;          } elsif ($token->{tag_name} eq 'select') {
5929                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            ## have an element in table scope
5930                  my $node = $self->{open_elements}->[$_];            my $i;
5931                  if ($node->[1] eq $token->{tag_name}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5932                    !!!cp ('t288');              my $node = $self->{open_elements}->[$_];
5933                    $i = $_;              if ($node->[1] & SELECT_EL) {
5934                    last INSCOPE;                !!!cp ('t288');
5935                  } elsif ({                $i = $_;
5936                            table => 1, html => 1,                last INSCOPE;
5937                           }->{$node->[1]}) {              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5938                    !!!cp ('t289');                !!!cp ('t289');
5939                    last INSCOPE;                last INSCOPE;
5940                  }              }
5941                } # INSCOPE            } # INSCOPE
5942                unless (defined $i) {            unless (defined $i) {
5943                  !!!cp ('t290');              !!!cp ('t290');
5944                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5945                  ## Ignore the token              ## Ignore the token
5946                  !!!next-token;              !!!nack ('t290.1');
5947                  redo B;              !!!next-token;
5948                }              next B;
5949              }
5950                                
5951                !!!cp ('t291');            !!!cp ('t291');
5952                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
5953    
5954                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5955    
5956                !!!next-token;            !!!nack ('t291.1');
5957                redo B;            !!!next-token;
5958              next B;
5959          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5960                   {                   {
5961                    caption => 1, table => 1, tbody => 1,                    caption => 1, table => 1, tbody => 1,
5962                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
5963                   }->{$token->{tag_name}}) {                   }->{$token->{tag_name}}) {
5964  ## TODO: The following is wrong?  ## TODO: The following is wrong?
5965                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5966                                
5967                ## have an element in table scope            ## have an element in table scope
5968                my $i;            my $i;
5969                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5970                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
5971                  if ($node->[1] eq $token->{tag_name}) {              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5972                    !!!cp ('t292');                !!!cp ('t292');
5973                    $i = $_;                $i = $_;
5974                    last INSCOPE;                last INSCOPE;
5975                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5976                            table => 1, html => 1,                !!!cp ('t293');
5977                           }->{$node->[1]}) {                last INSCOPE;
5978                    !!!cp ('t293');              }
5979                    last INSCOPE;            } # INSCOPE
5980                  }            unless (defined $i) {
5981                } # INSCOPE              !!!cp ('t294');
5982                unless (defined $i) {              ## Ignore the token
5983                  !!!cp ('t294');              !!!nack ('t294.1');
5984                  ## Ignore the token              !!!next-token;
5985                  !!!next-token;              next B;
5986                  redo B;            }
               }  
5987                                
5988                ## As if </select>            ## As if </select>
5989                ## have an element in table scope            ## have an element in table scope
5990                undef $i;            undef $i;
5991                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5992                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
5993                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
5994                    !!!cp ('t295');                !!!cp ('t295');
5995                    $i = $_;                $i = $_;
5996                    last INSCOPE;                last INSCOPE;
5997                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5998  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
5999                    !!!cp ('t296');                !!!cp ('t296');
6000                    last INSCOPE;                last INSCOPE;
6001                  }              }
6002                } # INSCOPE            } # INSCOPE
6003                unless (defined $i) {            unless (defined $i) {
6004                  !!!cp ('t297');              !!!cp ('t297');
6005  ## TODO: The following error type is correct?  ## TODO: The following error type is correct?
6006                  !!!parse-error (type => 'unmatched end tag:select', token => $token);              !!!parse-error (type => 'unmatched end tag:select', token => $token);
6007                  ## Ignore the </select> token              ## Ignore the </select> token
6008                  !!!next-token; ## TODO: ok?              !!!nack ('t297.1');
6009                  redo B;              !!!next-token; ## TODO: ok?
6010                }              next B;
6011              }
6012                                
6013                !!!cp ('t298');            !!!cp ('t298');
6014                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6015    
6016                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6017    
6018                ## reprocess            !!!ack-later;
6019                redo B;            ## reprocess
6020              next B;
6021          } else {          } else {
6022            !!!cp ('t299');            !!!cp ('t299');
6023            !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);
6024            ## Ignore the token            ## Ignore the token
6025              !!!nack ('t299.3');
6026            !!!next-token;            !!!next-token;
6027            redo B;            next B;
6028          }          }
6029        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6030          unless ($self->{open_elements}->[-1]->[1] eq 'html' and          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6031                  @{$self->{open_elements}} == 1) { # redundant, maybe                  @{$self->{open_elements}} == 1) { # redundant, maybe
6032            !!!cp ('t299.1');            !!!cp ('t299.1');
6033            !!!parse-error (type => 'in body:#eof', token => $token);            !!!parse-error (type => 'in body:#eof', token => $token);
# Line 5319  sub _tree_construction_main ($) { Line 6052  sub _tree_construction_main ($) {
6052            unless (length $token->{data}) {            unless (length $token->{data}) {
6053              !!!cp ('t300');              !!!cp ('t300');
6054              !!!next-token;              !!!next-token;
6055              redo B;              next B;
6056            }            }
6057          }          }
6058                    
# Line 5337  sub _tree_construction_main ($) { Line 6070  sub _tree_construction_main ($) {
6070    
6071          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6072          ## reprocess          ## reprocess
6073          redo B;          next B;
6074        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6075          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6076            !!!cp ('t303');            !!!cp ('t303');
# Line 5352  sub _tree_construction_main ($) { Line 6085  sub _tree_construction_main ($) {
6085          !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);
6086    
6087          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6088            !!!ack-later;
6089          ## reprocess          ## reprocess
6090          redo B;          next B;
6091        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6092          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6093            !!!cp ('t305');            !!!cp ('t305');
# Line 5372  sub _tree_construction_main ($) { Line 6106  sub _tree_construction_main ($) {
6106              !!!parse-error (type => 'unmatched end tag:html', token => $token);              !!!parse-error (type => 'unmatched end tag:html', token => $token);
6107              ## Ignore the token              ## Ignore the token
6108              !!!next-token;              !!!next-token;
6109              redo B;              next B;
6110            } else {            } else {
6111              !!!cp ('t308');              !!!cp ('t308');
6112              $self->{insertion_mode} = AFTER_HTML_BODY_IM;              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6113              !!!next-token;              !!!next-token;
6114              redo B;              next B;
6115            }            }
6116          } else {          } else {
6117            !!!cp ('t309');            !!!cp ('t309');
# Line 5385  sub _tree_construction_main ($) { Line 6119  sub _tree_construction_main ($) {
6119    
6120            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
6121            ## reprocess            ## reprocess
6122            redo B;            next B;
6123          }          }
6124        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6125          !!!cp ('t309.2');          !!!cp ('t309.2');
# Line 5402  sub _tree_construction_main ($) { Line 6136  sub _tree_construction_main ($) {
6136            unless (length $token->{data}) {            unless (length $token->{data}) {
6137              !!!cp ('t310');              !!!cp ('t310');
6138              !!!next-token;              !!!next-token;
6139              redo B;              next B;
6140            }            }
6141          }          }
6142                    
# Line 5430  sub _tree_construction_main ($) { Line 6164  sub _tree_construction_main ($) {
6164              !!!cp ('t315');              !!!cp ('t315');
6165              !!!next-token;              !!!next-token;
6166            }            }
6167            redo B;            next B;
6168          }          }
6169                    
6170          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
# Line 5449  sub _tree_construction_main ($) { Line 6183  sub _tree_construction_main ($) {
6183              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6184            !!!cp ('t318');            !!!cp ('t318');
6185            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6186              !!!nack ('t318.1');
6187            !!!next-token;            !!!next-token;
6188            redo B;            next B;
6189          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
6190                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
6191            !!!cp ('t319');            !!!cp ('t319');
6192            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6193            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6194              !!!ack ('t319.1');
6195            !!!next-token;            !!!next-token;
6196            redo B;            next B;
6197          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
6198            !!!cp ('t320');            !!!cp ('t320');
6199            ## NOTE: As if in body.            ## NOTE: As if in body.
6200            $parse_rcdata->(CDATA_CONTENT_MODEL);            $parse_rcdata->(CDATA_CONTENT_MODEL);
6201            redo B;            next B;
6202          } else {          } else {
6203            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6204              !!!cp ('t321');              !!!cp ('t321');
# Line 5472  sub _tree_construction_main ($) { Line 6208  sub _tree_construction_main ($) {
6208              !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);
6209            }            }
6210            ## Ignore the token            ## Ignore the token
6211              !!!nack ('t322.1');
6212            !!!next-token;            !!!next-token;
6213            redo B;            next B;
6214          }          }
6215        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6216          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
# Line 5488  sub _tree_construction_main ($) { Line 6225  sub _tree_construction_main ($) {
6225    
6226          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6227              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6228            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6229                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
6230              !!!cp ('t325');              !!!cp ('t325');
6231              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
# Line 5501  sub _tree_construction_main ($) { Line 6238  sub _tree_construction_main ($) {
6238            }            }
6239    
6240            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
6241                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6242              !!!cp ('t327');              !!!cp ('t327');
6243              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6244            } else {            } else {
6245              !!!cp ('t328');              !!!cp ('t328');
6246            }            }
6247            redo B;            next B;
6248          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
6249                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6250            !!!cp ('t329');            !!!cp ('t329');
6251            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6252            !!!next-token;            !!!next-token;
6253            redo B;            next B;
6254          } else {          } else {
6255            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6256              !!!cp ('t330');              !!!cp ('t330');
# Line 5524  sub _tree_construction_main ($) { Line 6261  sub _tree_construction_main ($) {
6261            }            }
6262            ## Ignore the token            ## Ignore the token
6263            !!!next-token;            !!!next-token;
6264            redo B;            next B;
6265          }          }
6266        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6267          unless ($self->{open_elements}->[-1]->[1] eq 'html' and          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6268                  @{$self->{open_elements}} == 1) { # redundant, maybe                  @{$self->{open_elements}} == 1) { # redundant, maybe
6269            !!!cp ('t331.1');            !!!cp ('t331.1');
6270            !!!parse-error (type => 'in body:#eof', token => $token);            !!!parse-error (type => 'in body:#eof', token => $token);
# Line 5552  sub _tree_construction_main ($) { Line 6289  sub _tree_construction_main ($) {
6289          !!!cp ('t332');          !!!cp ('t332');
6290          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6291          $script_start_tag->();          $script_start_tag->();
6292          redo B;          next B;
6293        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
6294          !!!cp ('t333');          !!!cp ('t333');
6295          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6296          $parse_rcdata->(CDATA_CONTENT_MODEL);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6297          redo B;          next B;
6298        } elsif ({        } elsif ({
6299                  base => 1, link => 1,                  base => 1, link => 1,
6300                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 5565  sub _tree_construction_main ($) { Line 6302  sub _tree_construction_main ($) {
6302          ## 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
6303          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6304          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6305            !!!ack ('t334.1');
6306          !!!next-token;          !!!next-token;
6307          redo B;          next B;
6308        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
6309          ## 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
6310          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6311          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.
6312    
6313          unless ($self->{confident}) {          unless ($self->{confident}) {
6314            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) {
6315              !!!cp ('t335');              !!!cp ('t335');
6316                ## NOTE: Whether the encoding is supported or not is handled
6317                ## in the {change_encoding} callback.
6318              $self->{change_encoding}              $self->{change_encoding}
6319                  ->($self, $token->{attributes}->{charset}->{value}, $token);                  ->($self, $token->{attributes}->{charset}->{value}, $token);
6320                            
# Line 5583  sub _tree_construction_main ($) { Line 6323  sub _tree_construction_main ($) {
6323                                       $token->{attributes}->{charset}                                       $token->{attributes}->{charset}
6324                                           ->{has_reference});                                           ->{has_reference});
6325            } elsif ($token->{attributes}->{content}) {            } elsif ($token->{attributes}->{content}) {
             ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
6326              if ($token->{attributes}->{content}->{value}              if ($token->{attributes}->{content}->{value}
6327                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6328                      [\x09-\x0D\x20]*=                      [\x09-\x0D\x20]*=
6329                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6330                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
6331                !!!cp ('t336');                !!!cp ('t336');
6332                  ## NOTE: Whether the encoding is supported or not is handled
6333                  ## in the {change_encoding} callback.
6334                $self->{change_encoding}                $self->{change_encoding}
6335                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6336                $meta_el->[0]->get_attribute_node_ns (undef, 'content')                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
# Line 5615  sub _tree_construction_main ($) { Line 6356  sub _tree_construction_main ($) {
6356            }            }
6357          }          }
6358    
6359            !!!ack ('t338.1');
6360          !!!next-token;          !!!next-token;
6361          redo B;          next B;
6362        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
6363          !!!cp ('t341');          !!!cp ('t341');
6364          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6365          $parse_rcdata->(RCDATA_CONTENT_MODEL);          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6366          redo B;          next B;
6367        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
6368          !!!parse-error (type => 'in body:body', token => $token);          !!!parse-error (type => 'in body:body', token => $token);
6369                                
6370          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
6371              $self->{open_elements}->[1]->[1] ne 'body') {              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6372            !!!cp ('t342');            !!!cp ('t342');
6373            ## Ignore the token            ## Ignore the token
6374          } else {          } else {
# Line 5640  sub _tree_construction_main ($) { Line 6382  sub _tree_construction_main ($) {
6382              }              }
6383            }            }
6384          }          }
6385            !!!nack ('t343.1');
6386          !!!next-token;          !!!next-token;
6387          redo B;          next B;
6388        } elsif ({        } elsif ({
6389                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6390                  div => 1, dl => 1, fieldset => 1,                  div => 1, dl => 1, fieldset => 1,
# Line 5656  sub _tree_construction_main ($) { Line 6399  sub _tree_construction_main ($) {
6399            !!!cp ('t350');            !!!cp ('t350');
6400            !!!parse-error (type => 'in form:form', token => $token);            !!!parse-error (type => 'in form:form', token => $token);
6401            ## Ignore the token            ## Ignore the token
6402              !!!nack ('t350.1');
6403            !!!next-token;            !!!next-token;
6404            redo B;            next B;
6405          }          }
6406    
6407          ## has a p element in scope          ## has a p element in scope
6408          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6409            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6410              !!!cp ('t344');              !!!cp ('t344');
6411              !!!back-token;              !!!back-token; # <form>
6412              $token = {type => END_TAG_TOKEN, tag_name => 'p',              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6413                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6414              redo B;              next B;
6415            } elsif ({            } elsif ($_->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6416              !!!cp ('t345');              !!!cp ('t345');
6417              last INSCOPE;              last INSCOPE;
6418            }            }
# Line 5679  sub _tree_construction_main ($) { Line 6420  sub _tree_construction_main ($) {
6420                        
6421          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6422          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6423              !!!nack ('t346.1');
6424            !!!next-token;            !!!next-token;
6425            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
6426              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
# Line 5695  sub _tree_construction_main ($) { Line 6437  sub _tree_construction_main ($) {
6437            !!!cp ('t347.1');            !!!cp ('t347.1');
6438            $self->{form_element} = $self->{open_elements}->[-1]->[0];            $self->{form_element} = $self->{open_elements}->[-1]->[0];
6439    
6440              !!!nack ('t347.2');
6441            !!!next-token;            !!!next-token;
6442          } elsif ($token->{tag_name} eq 'table') {          } elsif ($token->{tag_name} eq 'table') {
6443            !!!cp ('t382');            !!!cp ('t382');
# Line 5702  sub _tree_construction_main ($) { Line 6445  sub _tree_construction_main ($) {
6445                        
6446            $self->{insertion_mode} = IN_TABLE_IM;            $self->{insertion_mode} = IN_TABLE_IM;
6447    
6448              !!!nack ('t382.1');
6449            !!!next-token;            !!!next-token;
6450          } elsif ($token->{tag_name} eq 'hr') {          } elsif ($token->{tag_name} eq 'hr') {
6451            !!!cp ('t386');            !!!cp ('t386');
6452            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6453                    
6454              !!!nack ('t386.1');
6455            !!!next-token;            !!!next-token;
6456          } else {          } else {
6457            !!!cp ('t347');            !!!nack ('t347.1');
6458            !!!next-token;            !!!next-token;
6459          }          }
6460          redo B;          next B;
6461        } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {        } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
6462          ## has a p element in scope          ## has a p element in scope
6463          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6464            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6465              !!!cp ('t353');              !!!cp ('t353');
6466              !!!back-token;              !!!back-token; # <x>
6467              $token = {type => END_TAG_TOKEN, tag_name => 'p',              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6468                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6469              redo B;              next B;
6470            } elsif ({            } elsif ($_->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6471              !!!cp ('t354');              !!!cp ('t354');
6472              last INSCOPE;              last INSCOPE;
6473            }            }
# Line 5739  sub _tree_construction_main ($) { Line 6481  sub _tree_construction_main ($) {
6481                            dd => {dt => 1, dd => 1}}->{$token->{tag_name}};                            dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6482          LI: {          LI: {
6483            ## Step 2            ## Step 2
6484            if ($li_or_dtdd->{$node->[1]}) {            if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6485              if ($i != -1) {              if ($i != -1) {
6486                !!!cp ('t355');                !!!cp ('t355');
6487                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'not closed',
6488                                $self->{open_elements}->[-1]->[1], token => $token);                                value => $self->{open_elements}->[-1]->[0]
6489                                      ->manakai_local_name,
6490                                  token => $token);
6491              } else {              } else {
6492                !!!cp ('t356');                !!!cp ('t356');
6493              }              }
# Line 5754  sub _tree_construction_main ($) { Line 6498  sub _tree_construction_main ($) {
6498            }            }
6499                        
6500            ## Step 3            ## Step 3
6501            if (not $formatting_category->{$node->[1]} and            if (not ($node->[1] & FORMATTING_EL) and
6502                #not $phrasing_category->{$node->[1]} and                #not $phrasing_category->{$node->[1]} and
6503                ($special_category->{$node->[1]} or                ($node->[1] & SPECIAL_EL or
6504                 $scoping_category->{$node->[1]}) and                 $node->[1] & SCOPING_EL) and
6505                $node->[1] ne 'address' and $node->[1] ne 'div') {                not ($node->[1] & ADDRESS_EL) and
6506                  not ($node->[1] & DIV_EL)) {
6507              !!!cp ('t358');              !!!cp ('t358');
6508              last LI;              last LI;
6509            }            }
# Line 5771  sub _tree_construction_main ($) { Line 6516  sub _tree_construction_main ($) {
6516          } # LI          } # LI
6517                        
6518          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6519            !!!nack ('t359.1');
6520          !!!next-token;          !!!next-token;
6521          redo B;          next B;
6522        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
6523          ## has a p element in scope          ## has a p element in scope
6524          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6525            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6526              !!!cp ('t367');              !!!cp ('t367');
6527              !!!back-token;              !!!back-token; # <plaintext>
6528              $token = {type => END_TAG_TOKEN, tag_name => 'p',              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6529                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6530              redo B;              next B;
6531            } elsif ({            } elsif ($_->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6532              !!!cp ('t368');              !!!cp ('t368');
6533              last INSCOPE;              last INSCOPE;
6534            }            }
# Line 5795  sub _tree_construction_main ($) { Line 6538  sub _tree_construction_main ($) {
6538                        
6539          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
6540                        
6541            !!!nack ('t368.1');
6542          !!!next-token;          !!!next-token;
6543          redo B;          next B;
6544        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
6545          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
6546            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
6547            if ($node->[1] eq 'a') {            if ($node->[1] & A_EL) {
6548              !!!cp ('t371');              !!!cp ('t371');
6549              !!!parse-error (type => 'in a:a', token => $token);              !!!parse-error (type => 'in a:a', token => $token);
6550                            
6551              !!!back-token;              !!!back-token; # <a>
6552              $token = {type => END_TAG_TOKEN, tag_name => 'a',              $token = {type => END_TAG_TOKEN, tag_name => 'a',
6553                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6554              $formatting_end_tag->($token);              $formatting_end_tag->($token);
# Line 5835  sub _tree_construction_main ($) { Line 6579  sub _tree_construction_main ($) {
6579          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6580          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6581    
6582            !!!nack ('t374.1');
6583          !!!next-token;          !!!next-token;
6584          redo B;          next B;
6585        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
6586          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6587    
6588          ## has a |nobr| element in scope          ## has a |nobr| element in scope
6589          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6590            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6591            if ($node->[1] eq 'nobr') {            if ($node->[1] & NOBR_EL) {
6592              !!!cp ('t376');              !!!cp ('t376');
6593              !!!parse-error (type => 'in nobr:nobr', token => $token);              !!!parse-error (type => 'in nobr:nobr', token => $token);
6594              !!!back-token;              !!!back-token; # <nobr>
6595              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
6596                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6597              redo B;              next B;
6598            } 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]}) {  
6599              !!!cp ('t377');              !!!cp ('t377');
6600              last INSCOPE;              last INSCOPE;
6601            }            }
# Line 5862  sub _tree_construction_main ($) { Line 6604  sub _tree_construction_main ($) {
6604          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6605          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6606                    
6607            !!!nack ('t377.1');
6608          !!!next-token;          !!!next-token;
6609          redo B;          next B;
6610        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
6611          ## has a button element in scope          ## has a button element in scope
6612          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6613            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6614            if ($node->[1] eq 'button') {            if ($node->[1] & BUTTON_EL) {
6615              !!!cp ('t378');              !!!cp ('t378');
6616              !!!parse-error (type => 'in button:button', token => $token);              !!!parse-error (type => 'in button:button', token => $token);
6617              !!!back-token;              !!!back-token; # <button>
6618              $token = {type => END_TAG_TOKEN, tag_name => 'button',              $token = {type => END_TAG_TOKEN, tag_name => 'button',
6619                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6620              redo B;              next B;
6621            } 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]}) {  
6622              !!!cp ('t379');              !!!cp ('t379');
6623              last INSCOPE;              last INSCOPE;
6624            }            }
# Line 5892  sub _tree_construction_main ($) { Line 6632  sub _tree_construction_main ($) {
6632    
6633          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
6634    
6635            !!!nack ('t379.1');
6636          !!!next-token;          !!!next-token;
6637          redo B;          next B;
6638        } elsif ({        } elsif ({
6639                  xmp => 1,                  xmp => 1,
6640                  iframe => 1,                  iframe => 1,
# Line 5909  sub _tree_construction_main ($) { Line 6650  sub _tree_construction_main ($) {
6650          }          }
6651          ## NOTE: There is an "as if in body" code clone.          ## NOTE: There is an "as if in body" code clone.
6652          $parse_rcdata->(CDATA_CONTENT_MODEL);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6653          redo B;          next B;
6654        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
6655          !!!parse-error (type => 'isindex', token => $token);          !!!parse-error (type => 'isindex', token => $token);
6656                    
6657          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
6658            !!!cp ('t389');            !!!cp ('t389');
6659            ## Ignore the token            ## Ignore the token
6660              !!!nack ('t389'); ## NOTE: Not acknowledged.
6661            !!!next-token;            !!!next-token;
6662            redo B;            next B;
6663          } else {          } else {
6664            my $at = $token->{attributes};            my $at = $token->{attributes};
6665            my $form_attrs;            my $form_attrs;
# Line 5962  sub _tree_construction_main ($) { Line 6704  sub _tree_construction_main ($) {
6704                           line => $token->{line}, column => $token->{column}},                           line => $token->{line}, column => $token->{column}},
6705                          {type => END_TAG_TOKEN, tag_name => 'form',                          {type => END_TAG_TOKEN, tag_name => 'form',
6706                           line => $token->{line}, column => $token->{column}};                           line => $token->{line}, column => $token->{column}};
6707            $token = shift @tokens;            !!!nack ('t391.1'); ## NOTE: Not acknowledged.
6708            !!!back-token (@tokens);            !!!back-token (@tokens);
6709            redo B;            !!!next-token;
6710              next B;
6711          }          }
6712        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
6713          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
6714          my $el;          my $el;
6715          !!!create-element ($el, $token->{tag_name}, $token->{attributes}, $token);          !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
6716                    
6717          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
6718          $self->{content_model} = RCDATA_CONTENT_MODEL;          $self->{content_model} = RCDATA_CONTENT_MODEL;
# Line 5978  sub _tree_construction_main ($) { Line 6721  sub _tree_construction_main ($) {
6721          $insert->($el);          $insert->($el);
6722                    
6723          my $text = '';          my $text = '';
6724            !!!nack ('t392.1');
6725          !!!next-token;          !!!next-token;
6726          if ($token->{type} == CHARACTER_TOKEN) {          if ($token->{type} == CHARACTER_TOKEN) {
6727            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
# Line 6011  sub _tree_construction_main ($) { Line 6755  sub _tree_construction_main ($) {
6755            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
6756          }          }
6757          !!!next-token;          !!!next-token;
6758          redo B;          next B;
6759          } elsif ($token->{tag_name} eq 'math' or
6760                   $token->{tag_name} eq 'svg') {
6761            $reconstruct_active_formatting_elements->($insert_to_current);
6762    
6763            ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
6764    
6765            ## "adjust foreign attributes" - done in insert-element-f
6766            
6767            !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
6768            
6769            if ($self->{self_closing}) {
6770              pop @{$self->{open_elements}};
6771              !!!ack ('t398.1');
6772            } else {
6773              !!!cp ('t398.2');
6774              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
6775              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
6776              ## mode, "in body" (not "in foreign content") secondary insertion
6777              ## mode, maybe.
6778            }
6779    
6780            !!!next-token;
6781            next B;
6782        } elsif ({        } elsif ({
6783                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
6784                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 6021  sub _tree_construction_main ($) { Line 6788  sub _tree_construction_main ($) {
6788          !!!cp ('t401');          !!!cp ('t401');
6789          !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);
6790          ## Ignore the token          ## Ignore the token
6791            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
6792          !!!next-token;          !!!next-token;
6793          redo B;          next B;
6794                    
6795          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
6796        } else {        } else {
# Line 6044  sub _tree_construction_main ($) { Line 6812  sub _tree_construction_main ($) {
6812              }->{$token->{tag_name}}) {              }->{$token->{tag_name}}) {
6813            !!!cp ('t380');            !!!cp ('t380');
6814            push @$active_formatting_elements, ['#marker', ''];            push @$active_formatting_elements, ['#marker', ''];
6815              !!!nack ('t380.1');
6816          } elsif ({          } elsif ({
6817                    b => 1, big => 1, em => 1, font => 1, i => 1,                    b => 1, big => 1, em => 1, font => 1, i => 1,
6818                    s => 1, small => 1, strile => 1,                    s => 1, small => 1, strile => 1,
# Line 6051  sub _tree_construction_main ($) { Line 6820  sub _tree_construction_main ($) {
6820                   }->{$token->{tag_name}}) {                   }->{$token->{tag_name}}) {
6821            !!!cp ('t375');            !!!cp ('t375');
6822            push @$active_formatting_elements, $self->{open_elements}->[-1];            push @$active_formatting_elements, $self->{open_elements}->[-1];
6823              !!!nack ('t375.1');
6824          } elsif ($token->{tag_name} eq 'input') {          } elsif ($token->{tag_name} eq 'input') {
6825            !!!cp ('t388');            !!!cp ('t388');
6826            ## TODO: associate with $self->{form_element} if defined            ## TODO: associate with $self->{form_element} if defined
6827            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6828              !!!ack ('t388.2');
6829          } elsif ({          } elsif ({
6830                    area => 1, basefont => 1, bgsound => 1, br => 1,                    area => 1, basefont => 1, bgsound => 1, br => 1,
6831                    embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                    embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
# Line 6062  sub _tree_construction_main ($) { Line 6833  sub _tree_construction_main ($) {
6833                   }->{$token->{tag_name}}) {                   }->{$token->{tag_name}}) {
6834            !!!cp ('t388.1');            !!!cp ('t388.1');
6835            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6836              !!!ack ('t388.3');
6837          } elsif ($token->{tag_name} eq 'select') {          } elsif ($token->{tag_name} eq 'select') {
6838            ## TODO: associate with $self->{form_element} if defined            ## TODO: associate with $self->{form_element} if defined
6839                    
# Line 6074  sub _tree_construction_main ($) { Line 6846  sub _tree_construction_main ($) {
6846              !!!cp ('t400.2');              !!!cp ('t400.2');
6847              $self->{insertion_mode} = IN_SELECT_IM;              $self->{insertion_mode} = IN_SELECT_IM;
6848            }            }
6849              !!!nack ('t400.3');
6850          } else {          } else {
6851            !!!cp ('t402');            !!!nack ('t402');
6852          }          }
6853                    
6854          !!!next-token;          !!!next-token;
6855          redo B;          next B;
6856        }        }
6857      } elsif ($token->{type} == END_TAG_TOKEN) {      } elsif ($token->{type} == END_TAG_TOKEN) {
6858        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
# Line 6087  sub _tree_construction_main ($) { Line 6860  sub _tree_construction_main ($) {
6860          my $i;          my $i;
6861          INSCOPE: {          INSCOPE: {
6862            for (reverse @{$self->{open_elements}}) {            for (reverse @{$self->{open_elements}}) {
6863              if ($_->[1] eq 'body') {              if ($_->[1] & BODY_EL) {
6864                !!!cp ('t405');                !!!cp ('t405');
6865                $i = $_;                $i = $_;
6866                last INSCOPE;                last INSCOPE;
6867              } elsif ({              } elsif ($_->[1] & SCOPING_EL) {
                       applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                       button => 1, marquee => 1, object => 1, html => 1,  
                      }->{$_->[1]}) {  
6868                !!!cp ('t405.1');                !!!cp ('t405.1');
6869                last;                last;
6870              }              }
# Line 6104  sub _tree_construction_main ($) { Line 6874  sub _tree_construction_main ($) {
6874                            value => $token->{tag_name}, token => $token);                            value => $token->{tag_name}, token => $token);
6875            ## NOTE: Ignore the token.            ## NOTE: Ignore the token.
6876            !!!next-token;            !!!next-token;
6877            redo B;            next B;
6878          } # INSCOPE          } # INSCOPE
6879    
6880          for (@{$self->{open_elements}}) {          for (@{$self->{open_elements}}) {
6881            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]}) {  
6882              !!!cp ('t403');              !!!cp ('t403');
6883              !!!parse-error (type => 'not closed:'.$_->[1], token => $token);              !!!parse-error (type => 'not closed',
6884                                value => $_->[0]->manakai_local_name,
6885                                token => $token);
6886              last;              last;
6887            } else {            } else {
6888              !!!cp ('t404');              !!!cp ('t404');
# Line 6123  sub _tree_construction_main ($) { Line 6891  sub _tree_construction_main ($) {
6891    
6892          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
6893          !!!next-token;          !!!next-token;
6894          redo B;          next B;
6895        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
6896          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
6897            ## up-to-date, though it has same effect as speced.
6898            if (@{$self->{open_elements}} > 1 and
6899                $self->{open_elements}->[1]->[1] & BODY_EL) {
6900            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
6901            if ($self->{open_elements}->[-1]->[1] ne 'body') {            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
6902              !!!cp ('t406');              !!!cp ('t406');
6903              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1], token => $token);              !!!parse-error (type => 'not closed',
6904                                value => $self->{open_elements}->[1]->[0]
6905                                    ->manakai_local_name,
6906                                token => $token);
6907            } else {            } else {
6908              !!!cp ('t407');              !!!cp ('t407');
6909            }            }
6910            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6911            ## reprocess            ## reprocess
6912            redo B;            next B;
6913          } else {          } else {
6914            !!!cp ('t408');            !!!cp ('t408');
6915            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6916            ## Ignore the token            ## Ignore the token
6917            !!!next-token;            !!!next-token;
6918            redo B;            next B;
6919          }          }
6920        } elsif ({        } elsif ({
6921                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
# Line 6154  sub _tree_construction_main ($) { Line 6928  sub _tree_construction_main ($) {
6928          my $i;          my $i;
6929          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6930            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6931            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6932              !!!cp ('t410');              !!!cp ('t410');
6933              $i = $_;              $i = $_;
6934              last INSCOPE;              last INSCOPE;
6935            } 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]}) {  
6936              !!!cp ('t411');              !!!cp ('t411');
6937              last INSCOPE;              last INSCOPE;
6938            }            }
# Line 6177  sub _tree_construction_main ($) { Line 6948  sub _tree_construction_main ($) {
6948                    dt => ($token->{tag_name} ne 'dt'),                    dt => ($token->{tag_name} ne 'dt'),
6949                    li => ($token->{tag_name} ne 'li'),                    li => ($token->{tag_name} ne 'li'),
6950                    p => 1,                    p => 1,
6951                   }->{$self->{open_elements}->[-1]->[1]}) {                   }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
6952              !!!cp ('t409');              !!!cp ('t409');
6953              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6954            }            }
6955    
6956            ## Step 2.            ## Step 2.
6957            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6958                      ne $token->{tag_name}) {
6959              !!!cp ('t412');              !!!cp ('t412');
6960              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);              !!!parse-error (type => 'not closed',
6961                                value => $self->{open_elements}->[-1]->[0]
6962                                    ->manakai_local_name,
6963                                token => $token);
6964            } else {            } else {
6965              !!!cp ('t414');              !!!cp ('t414');
6966            }            }
# Line 6200  sub _tree_construction_main ($) { Line 6975  sub _tree_construction_main ($) {
6975                }->{$token->{tag_name}};                }->{$token->{tag_name}};
6976          }          }
6977          !!!next-token;          !!!next-token;
6978          redo B;          next B;
6979        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
6980          undef $self->{form_element};          undef $self->{form_element};
6981    
# Line 6208  sub _tree_construction_main ($) { Line 6983  sub _tree_construction_main ($) {
6983          my $i;          my $i;
6984          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6985            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6986            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & FORM_EL) {
6987              !!!cp ('t418');              !!!cp ('t418');
6988              $i = $_;              $i = $_;
6989              last INSCOPE;              last INSCOPE;
6990            } 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]}) {  
6991              !!!cp ('t419');              !!!cp ('t419');
6992              last INSCOPE;              last INSCOPE;
6993            }            }
# Line 6226  sub _tree_construction_main ($) { Line 6998  sub _tree_construction_main ($) {
6998            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6999          } else {          } else {
7000            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7001            while ({            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                   dd => 1, dt => 1, li => 1, p => 1,  
                  }->{$self->{open_elements}->[-1]->[1]}) {  
7002              !!!cp ('t417');              !!!cp ('t417');
7003              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
7004            }            }
7005                        
7006            ## Step 2.            ## Step 2.
7007            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7008                      ne $token->{tag_name}) {
7009              !!!cp ('t417.1');              !!!cp ('t417.1');
7010              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);              !!!parse-error (type => 'not closed',
7011                                value => $self->{open_elements}->[-1]->[0]
7012                                    ->manakai_local_name,
7013                                token => $token);
7014            } else {            } else {
7015              !!!cp ('t420');              !!!cp ('t420');
7016            }              }  
# Line 6246  sub _tree_construction_main ($) { Line 7020  sub _tree_construction_main ($) {
7020          }          }
7021    
7022          !!!next-token;          !!!next-token;
7023          redo B;          next B;
7024        } elsif ({        } elsif ({
7025                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
7026                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 6254  sub _tree_construction_main ($) { Line 7028  sub _tree_construction_main ($) {
7028          my $i;          my $i;
7029          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7030            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7031            if ({            if ($node->[1] & HEADING_EL) {
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
7032              !!!cp ('t423');              !!!cp ('t423');
7033              $i = $_;              $i = $_;
7034              last INSCOPE;              last INSCOPE;
7035            } 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]}) {  
7036              !!!cp ('t424');              !!!cp ('t424');
7037              last INSCOPE;              last INSCOPE;
7038            }            }
# Line 6274  sub _tree_construction_main ($) { Line 7043  sub _tree_construction_main ($) {
7043            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7044          } else {          } else {
7045            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7046            while ({            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                   dd => 1, dt => 1, li => 1, p => 1,  
                  }->{$self->{open_elements}->[-1]->[1]}) {  
7047              !!!cp ('t422');              !!!cp ('t422');
7048              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
7049            }            }
7050                        
7051            ## Step 2.            ## Step 2.
7052            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7053                      ne $token->{tag_name}) {
7054              !!!cp ('t425');              !!!cp ('t425');
7055              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7056            } else {            } else {
# Line 6294  sub _tree_construction_main ($) { Line 7062  sub _tree_construction_main ($) {
7062          }          }
7063                    
7064          !!!next-token;          !!!next-token;
7065          redo B;          next B;
7066        } elsif ($token->{tag_name} eq 'p') {        } elsif ($token->{tag_name} eq 'p') {
7067          ## has an element in scope          ## has an element in scope
7068          my $i;          my $i;
7069          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7070            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7071            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & P_EL) {
7072              !!!cp ('t410.1');              !!!cp ('t410.1');
7073              $i = $_;              $i = $_;
7074              last INSCOPE;              last INSCOPE;
7075            } 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]}) {  
7076              !!!cp ('t411.1');              !!!cp ('t411.1');
7077              last INSCOPE;              last INSCOPE;
7078            }            }
7079          } # INSCOPE          } # INSCOPE
7080    
7081          if (defined $i) {          if (defined $i) {
7082            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7083                      ne $token->{tag_name}) {
7084              !!!cp ('t412.1');              !!!cp ('t412.1');
7085              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);              !!!parse-error (type => 'not closed',
7086                                value => $self->{open_elements}->[-1]->[0]
7087                                    ->manakai_local_name,
7088                                token => $token);
7089            } else {            } else {
7090              !!!cp ('t414.1');              !!!cp ('t414.1');
7091            }            }
# Line 6329  sub _tree_construction_main ($) { Line 7098  sub _tree_construction_main ($) {
7098            !!!cp ('t415.1');            !!!cp ('t415.1');
7099            ## As if <p>, then reprocess the current token            ## As if <p>, then reprocess the current token
7100            my $el;            my $el;
7101            !!!create-element ($el, 'p',, $token);            !!!create-element ($el, $HTML_NS, 'p',, $token);
7102            $insert->($el);            $insert->($el);
7103            ## NOTE: Not inserted into |$self->{open_elements}|.            ## NOTE: Not inserted into |$self->{open_elements}|.
7104          }          }
7105    
7106          !!!next-token;          !!!next-token;
7107          redo B;          next B;
7108        } elsif ({        } elsif ({
7109                  a => 1,                  a => 1,
7110                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
# Line 6344  sub _tree_construction_main ($) { Line 7113  sub _tree_construction_main ($) {
7113                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7114          !!!cp ('t427');          !!!cp ('t427');
7115          $formatting_end_tag->($token);          $formatting_end_tag->($token);
7116          redo B;          next B;
7117        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
7118          !!!cp ('t428');          !!!cp ('t428');
7119          !!!parse-error (type => 'unmatched end tag:br', token => $token);          !!!parse-error (type => 'unmatched end tag:br', token => $token);
# Line 6353  sub _tree_construction_main ($) { Line 7122  sub _tree_construction_main ($) {
7122          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7123                    
7124          my $el;          my $el;
7125          !!!create-element ($el, 'br',, $token);          !!!create-element ($el, $HTML_NS, 'br',, $token);
7126          $insert->($el);          $insert->($el);
7127                    
7128          ## Ignore the token.          ## Ignore the token.
7129          !!!next-token;          !!!next-token;
7130          redo B;          next B;
7131        } elsif ({        } elsif ({
7132                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7133                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 6375  sub _tree_construction_main ($) { Line 7144  sub _tree_construction_main ($) {
7144          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7145          ## Ignore the token          ## Ignore the token
7146          !!!next-token;          !!!next-token;
7147          redo B;          next B;
7148                    
7149          ## ISSUE: Issue on HTML5 new elements in spec          ## ISSUE: Issue on HTML5 new elements in spec
7150                    
# Line 6386  sub _tree_construction_main ($) { Line 7155  sub _tree_construction_main ($) {
7155    
7156          ## Step 2          ## Step 2
7157          S2: {          S2: {
7158            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7159              ## Step 1              ## Step 1
7160              ## generate implied end tags              ## generate implied end tags
7161              while ({              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                     dd => 1, dt => 1, li => 1, p => 1,  
                    }->{$self->{open_elements}->[-1]->[1]}) {  
7162                !!!cp ('t430');                !!!cp ('t430');
7163                ## ISSUE: Can this case be reached?                ## ISSUE: Can this case be reached?
7164                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
7165              }              }
7166                    
7167              ## Step 2              ## Step 2
7168              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7169                        ne $token->{tag_name}) {
7170                !!!cp ('t431');                !!!cp ('t431');
7171                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
7172                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                !!!parse-error (type => 'not closed',
7173                                  value => $self->{open_elements}->[-1]->[0]
7174                                      ->manakai_local_name,
7175                                  token => $token);
7176              } else {              } else {
7177                !!!cp ('t432');                !!!cp ('t432');
7178              }              }
# Line 6413  sub _tree_construction_main ($) { Line 7184  sub _tree_construction_main ($) {
7184              last S2;              last S2;
7185            } else {            } else {
7186              ## Step 3              ## Step 3
7187              if (not $formatting_category->{$node->[1]} and              if (not ($node->[1] & FORMATTING_EL) and
7188                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
7189                  ($special_category->{$node->[1]} or                  ($node->[1] & SPECIAL_EL or
7190                   $scoping_category->{$node->[1]})) {                   $node->[1] & SCOPING_EL)) {
7191                !!!cp ('t433');                !!!cp ('t433');
7192                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7193                ## Ignore the token                ## Ignore the token
# Line 6434  sub _tree_construction_main ($) { Line 7205  sub _tree_construction_main ($) {
7205            ## Step 5;            ## Step 5;
7206            redo S2;            redo S2;
7207          } # S2          } # S2
7208          redo B;          next B;
7209        }        }
7210      }      }
7211      redo B;      next B;
7212      } continue { # B
7213        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7214          ## NOTE: The code below is executed in cases where it does not have
7215          ## to be, but it it is harmless even in those cases.
7216          ## has an element in scope
7217          INSCOPE: {
7218            for (reverse 0..$#{$self->{open_elements}}) {
7219              my $node = $self->{open_elements}->[$_];
7220              if ($node->[1] & FOREIGN_EL) {
7221                last INSCOPE;
7222              } elsif ($node->[1] & SCOPING_EL) {
7223                last;
7224              }
7225            }
7226            
7227            ## NOTE: No foreign element in scope.
7228            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7229          } # INSCOPE
7230        }
7231    } # B    } # B
7232    
7233    ## Stop parsing # MUST    ## Stop parsing # MUST
# Line 6514  sub set_inner_html ($$$) { Line 7304  sub set_inner_html ($$$) {
7304          !!!cp ('i4');          !!!cp ('i4');
7305          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
7306          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7307          } elsif ($self->{next_char} <= 0x0008 or
7308                   (0x000E <= $self->{next_char} and
7309                    $self->{next_char} <= 0x001F) or
7310                   (0x007F <= $self->{next_char} and
7311                    $self->{next_char} <= 0x009F) or
7312                   (0xD800 <= $self->{next_char} and
7313                    $self->{next_char} <= 0xDFFF) or
7314                   (0xFDD0 <= $self->{next_char} and
7315                    $self->{next_char} <= 0xFDDF) or
7316                   {
7317                    0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
7318                    0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
7319                    0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
7320                    0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
7321                    0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
7322                    0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
7323                    0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
7324                    0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
7325                    0x10FFFE => 1, 0x10FFFF => 1,
7326                   }->{$self->{next_char}}) {
7327            !!!cp ('i4.1');
7328            !!!parse-error (type => 'control char', level => $self->{must_level});
7329    ## TODO: error type documentation
7330        }        }
7331      };      };
7332      $p->{prev_char} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];
# Line 6554  sub set_inner_html ($$$) { Line 7367  sub set_inner_html ($$$) {
7367          unless defined $p->{content_model};          unless defined $p->{content_model};
7368          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
7369    
7370      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7371          ## TODO: Foreign element OK?
7372    
7373      ## Step 3      ## Step 3
7374      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
# Line 6564  sub set_inner_html ($$$) { Line 7378  sub set_inner_html ($$$) {
7378      $doc->append_child ($root);      $doc->append_child ($root);
7379    
7380      ## Step 5 # MUST      ## Step 5 # MUST
7381      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7382    
7383      undef $p->{head_element};      undef $p->{head_element};
7384    

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24