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

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

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

revision 1.120 by wakaba, Thu Mar 20 03:57:00 2008 UTC revision 1.161 by wakaba, Wed Sep 10 10:46:50 2008 UTC
# Line 8  use Error qw(:try); Line 8  use Error qw(:try);
8  ## doc.write ('');  ## doc.write ('');
9  ## alert (doc.compatMode);  ## alert (doc.compatMode);
10    
11  ## TODO: Control charcters and noncharacters are not allowed (HTML5 revision 1263)  require IO::Handle;
12  ## TODO: 1252 parse error (revision 1264)  
13  ## TODO: 8859-11 = 874 (revision 1271)  my $HTML_NS = q<http://www.w3.org/1999/xhtml>;
14    my $MML_NS = q<http://www.w3.org/1998/Math/MathML>;
15  my $permitted_slash_tag_name = {  my $SVG_NS = q<http://www.w3.org/2000/svg>;
16    base => 1,  my $XLINK_NS = q<http://www.w3.org/1999/xlink>;
17    link => 1,  my $XML_NS = q<http://www.w3.org/XML/1998/namespace>;
18    meta => 1,  my $XMLNS_NS = q<http://www.w3.org/2000/xmlns/>;
19    hr => 1,  
20    br => 1,  sub A_EL () { 0b1 }
21    img => 1,  sub ADDRESS_EL () { 0b10 }
22    embed => 1,  sub BODY_EL () { 0b100 }
23    param => 1,  sub BUTTON_EL () { 0b1000 }
24    area => 1,  sub CAPTION_EL () { 0b10000 }
25    col => 1,  sub DD_EL () { 0b100000 }
26    input => 1,  sub DIV_EL () { 0b1000000 }
27    sub DT_EL () { 0b10000000 }
28    sub FORM_EL () { 0b100000000 }
29    sub FORMATTING_EL () { 0b1000000000 }
30    sub FRAMESET_EL () { 0b10000000000 }
31    sub HEADING_EL () { 0b100000000000 }
32    sub HTML_EL () { 0b1000000000000 }
33    sub LI_EL () { 0b10000000000000 }
34    sub NOBR_EL () { 0b100000000000000 }
35    sub OPTION_EL () { 0b1000000000000000 }
36    sub OPTGROUP_EL () { 0b10000000000000000 }
37    sub P_EL () { 0b100000000000000000 }
38    sub SELECT_EL () { 0b1000000000000000000 }
39    sub TABLE_EL () { 0b10000000000000000000 }
40    sub TABLE_CELL_EL () { 0b100000000000000000000 }
41    sub TABLE_ROW_EL () { 0b1000000000000000000000 }
42    sub TABLE_ROW_GROUP_EL () { 0b10000000000000000000000 }
43    sub MISC_SCOPING_EL () { 0b100000000000000000000000 }
44    sub MISC_SPECIAL_EL () { 0b1000000000000000000000000 }
45    sub FOREIGN_EL () { 0b10000000000000000000000000 }
46    sub FOREIGN_FLOW_CONTENT_EL () { 0b100000000000000000000000000 }
47    sub MML_AXML_EL () { 0b1000000000000000000000000000 }
48    sub RUBY_EL () { 0b10000000000000000000000000000 }
49    sub RUBY_COMPONENT_EL () { 0b100000000000000000000000000000 }
50    
51    sub TABLE_ROWS_EL () {
52      TABLE_EL |
53      TABLE_ROW_EL |
54      TABLE_ROW_GROUP_EL
55    }
56    
57    ## NOTE: Used in "generate implied end tags" algorithm.
58    ## NOTE: There is a code where a modified version of END_TAG_OPTIONAL_EL
59    ## is used in "generate implied end tags" implementation (search for the
60    ## function mae).
61    sub END_TAG_OPTIONAL_EL () {
62      DD_EL |
63      DT_EL |
64      LI_EL |
65      P_EL |
66      RUBY_COMPONENT_EL
67    }
68    
69    ## NOTE: Used in </body> and EOF algorithms.
70    sub ALL_END_TAG_OPTIONAL_EL () {
71      DD_EL |
72      DT_EL |
73      LI_EL |
74      P_EL |
75    
76      BODY_EL |
77      HTML_EL |
78      TABLE_CELL_EL |
79      TABLE_ROW_EL |
80      TABLE_ROW_GROUP_EL
81    }
82    
83    sub SCOPING_EL () {
84      BUTTON_EL |
85      CAPTION_EL |
86      HTML_EL |
87      TABLE_EL |
88      TABLE_CELL_EL |
89      MISC_SCOPING_EL
90    }
91    
92    sub TABLE_SCOPING_EL () {
93      HTML_EL |
94      TABLE_EL
95    }
96    
97    sub TABLE_ROWS_SCOPING_EL () {
98      HTML_EL |
99      TABLE_ROW_GROUP_EL
100    }
101    
102    sub TABLE_ROW_SCOPING_EL () {
103      HTML_EL |
104      TABLE_ROW_EL
105    }
106    
107    sub SPECIAL_EL () {
108      ADDRESS_EL |
109      BODY_EL |
110      DIV_EL |
111    
112      DD_EL |
113      DT_EL |
114      LI_EL |
115      P_EL |
116    
117      FORM_EL |
118      FRAMESET_EL |
119      HEADING_EL |
120      OPTION_EL |
121      OPTGROUP_EL |
122      SELECT_EL |
123      TABLE_ROW_EL |
124      TABLE_ROW_GROUP_EL |
125      MISC_SPECIAL_EL
126    }
127    
128    my $el_category = {
129      a => A_EL | FORMATTING_EL,
130      address => ADDRESS_EL,
131      applet => MISC_SCOPING_EL,
132      area => MISC_SPECIAL_EL,
133      b => FORMATTING_EL,
134      base => MISC_SPECIAL_EL,
135      basefont => MISC_SPECIAL_EL,
136      bgsound => MISC_SPECIAL_EL,
137      big => FORMATTING_EL,
138      blockquote => MISC_SPECIAL_EL,
139      body => BODY_EL,
140      br => MISC_SPECIAL_EL,
141      button => BUTTON_EL,
142      caption => CAPTION_EL,
143      center => MISC_SPECIAL_EL,
144      col => MISC_SPECIAL_EL,
145      colgroup => MISC_SPECIAL_EL,
146      dd => DD_EL,
147      dir => MISC_SPECIAL_EL,
148      div => DIV_EL,
149      dl => MISC_SPECIAL_EL,
150      dt => DT_EL,
151      em => FORMATTING_EL,
152      embed => MISC_SPECIAL_EL,
153      fieldset => MISC_SPECIAL_EL,
154      font => FORMATTING_EL,
155      form => FORM_EL,
156      frame => MISC_SPECIAL_EL,
157      frameset => FRAMESET_EL,
158      h1 => HEADING_EL,
159      h2 => HEADING_EL,
160      h3 => HEADING_EL,
161      h4 => HEADING_EL,
162      h5 => HEADING_EL,
163      h6 => HEADING_EL,
164      head => MISC_SPECIAL_EL,
165      hr => MISC_SPECIAL_EL,
166      html => HTML_EL,
167      i => FORMATTING_EL,
168      iframe => MISC_SPECIAL_EL,
169      img => MISC_SPECIAL_EL,
170      input => MISC_SPECIAL_EL,
171      isindex => MISC_SPECIAL_EL,
172      li => LI_EL,
173      link => MISC_SPECIAL_EL,
174      listing => MISC_SPECIAL_EL,
175      marquee => MISC_SCOPING_EL,
176      menu => MISC_SPECIAL_EL,
177      meta => MISC_SPECIAL_EL,
178      nobr => NOBR_EL | FORMATTING_EL,
179      noembed => MISC_SPECIAL_EL,
180      noframes => MISC_SPECIAL_EL,
181      noscript => MISC_SPECIAL_EL,
182      object => MISC_SCOPING_EL,
183      ol => MISC_SPECIAL_EL,
184      optgroup => OPTGROUP_EL,
185      option => OPTION_EL,
186      p => P_EL,
187      param => MISC_SPECIAL_EL,
188      plaintext => MISC_SPECIAL_EL,
189      pre => MISC_SPECIAL_EL,
190      rp => RUBY_COMPONENT_EL,
191      rt => RUBY_COMPONENT_EL,
192      ruby => RUBY_EL,
193      s => FORMATTING_EL,
194      script => MISC_SPECIAL_EL,
195      select => SELECT_EL,
196      small => FORMATTING_EL,
197      spacer => MISC_SPECIAL_EL,
198      strike => FORMATTING_EL,
199      strong => FORMATTING_EL,
200      style => MISC_SPECIAL_EL,
201      table => TABLE_EL,
202      tbody => TABLE_ROW_GROUP_EL,
203      td => TABLE_CELL_EL,
204      textarea => MISC_SPECIAL_EL,
205      tfoot => TABLE_ROW_GROUP_EL,
206      th => TABLE_CELL_EL,
207      thead => TABLE_ROW_GROUP_EL,
208      title => MISC_SPECIAL_EL,
209      tr => TABLE_ROW_EL,
210      tt => FORMATTING_EL,
211      u => FORMATTING_EL,
212      ul => MISC_SPECIAL_EL,
213      wbr => MISC_SPECIAL_EL,
214    };
215    
216    my $el_category_f = {
217      $MML_NS => {
218        'annotation-xml' => MML_AXML_EL,
219        mi => FOREIGN_FLOW_CONTENT_EL,
220        mo => FOREIGN_FLOW_CONTENT_EL,
221        mn => FOREIGN_FLOW_CONTENT_EL,
222        ms => FOREIGN_FLOW_CONTENT_EL,
223        mtext => FOREIGN_FLOW_CONTENT_EL,
224      },
225      $SVG_NS => {
226        foreignObject => FOREIGN_FLOW_CONTENT_EL,
227        desc => FOREIGN_FLOW_CONTENT_EL,
228        title => FOREIGN_FLOW_CONTENT_EL,
229      },
230      ## NOTE: In addition, FOREIGN_EL is set to non-HTML elements.
231  };  };
232    
233    my $svg_attr_name = {
234      attributename => 'attributeName',
235      attributetype => 'attributeType',
236      basefrequency => 'baseFrequency',
237      baseprofile => 'baseProfile',
238      calcmode => 'calcMode',
239      clippathunits => 'clipPathUnits',
240      contentscripttype => 'contentScriptType',
241      contentstyletype => 'contentStyleType',
242      diffuseconstant => 'diffuseConstant',
243      edgemode => 'edgeMode',
244      externalresourcesrequired => 'externalResourcesRequired',
245      filterres => 'filterRes',
246      filterunits => 'filterUnits',
247      glyphref => 'glyphRef',
248      gradienttransform => 'gradientTransform',
249      gradientunits => 'gradientUnits',
250      kernelmatrix => 'kernelMatrix',
251      kernelunitlength => 'kernelUnitLength',
252      keypoints => 'keyPoints',
253      keysplines => 'keySplines',
254      keytimes => 'keyTimes',
255      lengthadjust => 'lengthAdjust',
256      limitingconeangle => 'limitingConeAngle',
257      markerheight => 'markerHeight',
258      markerunits => 'markerUnits',
259      markerwidth => 'markerWidth',
260      maskcontentunits => 'maskContentUnits',
261      maskunits => 'maskUnits',
262      numoctaves => 'numOctaves',
263      pathlength => 'pathLength',
264      patterncontentunits => 'patternContentUnits',
265      patterntransform => 'patternTransform',
266      patternunits => 'patternUnits',
267      pointsatx => 'pointsAtX',
268      pointsaty => 'pointsAtY',
269      pointsatz => 'pointsAtZ',
270      preservealpha => 'preserveAlpha',
271      preserveaspectratio => 'preserveAspectRatio',
272      primitiveunits => 'primitiveUnits',
273      refx => 'refX',
274      refy => 'refY',
275      repeatcount => 'repeatCount',
276      repeatdur => 'repeatDur',
277      requiredextensions => 'requiredExtensions',
278      requiredfeatures => 'requiredFeatures',
279      specularconstant => 'specularConstant',
280      specularexponent => 'specularExponent',
281      spreadmethod => 'spreadMethod',
282      startoffset => 'startOffset',
283      stddeviation => 'stdDeviation',
284      stitchtiles => 'stitchTiles',
285      surfacescale => 'surfaceScale',
286      systemlanguage => 'systemLanguage',
287      tablevalues => 'tableValues',
288      targetx => 'targetX',
289      targety => 'targetY',
290      textlength => 'textLength',
291      viewbox => 'viewBox',
292      viewtarget => 'viewTarget',
293      xchannelselector => 'xChannelSelector',
294      ychannelselector => 'yChannelSelector',
295      zoomandpan => 'zoomAndPan',
296    };
297    
298    my $foreign_attr_xname = {
299      'xlink:actuate' => [$XLINK_NS, ['xlink', 'actuate']],
300      'xlink:arcrole' => [$XLINK_NS, ['xlink', 'arcrole']],
301      'xlink:href' => [$XLINK_NS, ['xlink', 'href']],
302      'xlink:role' => [$XLINK_NS, ['xlink', 'role']],
303      'xlink:show' => [$XLINK_NS, ['xlink', 'show']],
304      'xlink:title' => [$XLINK_NS, ['xlink', 'title']],
305      'xlink:type' => [$XLINK_NS, ['xlink', 'type']],
306      'xml:base' => [$XML_NS, ['xml', 'base']],
307      'xml:lang' => [$XML_NS, ['xml', 'lang']],
308      'xml:space' => [$XML_NS, ['xml', 'space']],
309      'xmlns' => [$XMLNS_NS, [undef, 'xmlns']],
310      'xmlns:xlink' => [$XMLNS_NS, ['xmlns', 'xlink']],
311    };
312    
313    ## ISSUE: xmlns:xlink="non-xlink-ns" is not an error.
314    
315  my $c1_entity_char = {  my $c1_entity_char = {
316    0x80 => 0x20AC,    0x80 => 0x20AC,
317    0x81 => 0xFFFD,    0x81 => 0xFFFD,
# Line 61  my $c1_entity_char = { Line 347  my $c1_entity_char = {
347    0x9F => 0x0178,    0x9F => 0x0178,
348  }; # $c1_entity_char  }; # $c1_entity_char
349    
 my $special_category = {  
   address => 1, area => 1, base => 1, basefont => 1, bgsound => 1,  
   blockquote => 1, body => 1, br => 1, center => 1, col => 1, colgroup => 1,  
   dd => 1, dir => 1, div => 1, dl => 1, dt => 1, embed => 1, fieldset => 1,  
   form => 1, frame => 1, frameset => 1, h1 => 1, h2 => 1, h3 => 1,  
   h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, iframe => 1, image => 1,  
   img => 1, input => 1, isindex => 1, li => 1, link => 1, listing => 1,  
   menu => 1, meta => 1, noembed => 1, noframes => 1, noscript => 1,  
   ol => 1, optgroup => 1, option => 1, p => 1, param => 1, plaintext => 1,  
   pre => 1, script => 1, select => 1, spacer => 1, style => 1, tbody => 1,  
   textarea => 1, tfoot => 1, thead => 1, title => 1, tr => 1, ul => 1, wbr => 1,  
 };  
 my $scoping_category = {  
   applet => 1, button => 1, caption => 1, html => 1, marquee => 1, object => 1,  
   table => 1, td => 1, th => 1,  
 };  
 my $formatting_category = {  
   a => 1, b => 1, big => 1, em => 1, font => 1, i => 1, nobr => 1,  
   s => 1, small => 1, strile => 1, strong => 1, tt => 1, u => 1,  
 };  
 # $phrasing_category: all other elements  
   
350  sub parse_byte_string ($$$$;$) {  sub parse_byte_string ($$$$;$) {
351      my $self = shift;
352      my $charset_name = shift;
353      open my $input, '<', ref $_[0] ? $_[0] : \($_[0]);
354      return $self->parse_byte_stream ($charset_name, $input, @_[1..$#_]);
355    } # parse_byte_string
356    
357    sub parse_byte_stream ($$$$;$) {
358    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
359    my $charset = shift;    my $charset_name = shift;
360    my $bytes_s = ref $_[0] ? $_[0] : \($_[0]);    my $byte_stream = $_[0];
   my $s;  
     
   if (defined $charset) {  
     require Encode; ## TODO: decode(utf8) don't delete BOM  
     $s = \ (Encode::decode ($charset, $$bytes_s));  
     $self->{input_encoding} = lc $charset; ## TODO: normalize name  
     $self->{confident} = 1;  
   } else {  
     ## TODO: Implement HTML5 detection algorithm  
     require Whatpm::Charset::UniversalCharDet;  
     $charset = Whatpm::Charset::UniversalCharDet->detect_byte_string  
         (substr ($$bytes_s, 0, 1024));  
     $charset ||= 'windows-1252';  
     $s = \ (Encode::decode ($charset, $$bytes_s));  
     $self->{input_encoding} = $charset;  
     $self->{confident} = 0;  
   }  
361    
362    $self->{change_encoding} = sub {    my $onerror = $_[2] || sub {
363      my $self = shift;      my (%opt) = @_;
364      my $charset = lc shift;      warn "Parse error ($opt{type})\n";
365      my $token = shift;    };
366      ## TODO: if $charset is supported    $self->{parse_error} = $onerror; # updated later by parse_char_string
     ## TODO: normalize charset name  
367    
368      ## "Change the encoding" algorithm:    ## HTML5 encoding sniffing algorithm
369      require Message::Charset::Info;
370      my $charset;
371      my $buffer;
372      my ($char_stream, $e_status);
373    
374      SNIFFING: {
375        ## NOTE: By setting |allow_fallback| option true when the
376        ## |get_decode_handle| method is invoked, we ignore what the HTML5
377        ## spec requires, i.e. unsupported encoding should be ignored.
378          ## TODO: We should not do this unless the parser is invoked
379          ## in the conformance checking mode, in which this behavior
380          ## would be useful.
381    
382      ## Step 1          ## Step 1
383      if ($charset eq 'utf-16') { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?      if (defined $charset_name) {
384        $charset = 'utf-8';        $charset = Message::Charset::Info->get_by_html_name ($charset_name);
385              ## TODO: Is this ok?  Transfer protocol's parameter should be
386              ## interpreted in its semantics?
387    
388          ## ISSUE: Unsupported encoding is not ignored according to the spec.
389          ($char_stream, $e_status) = $charset->get_decode_handle
390              ($byte_stream, allow_error_reporting => 1,
391               allow_fallback => 1);
392          if ($char_stream) {
393            $self->{confident} = 1;
394            last SNIFFING;
395          } else {
396            ## TODO: unsupported error
397          }
398      }      }
399    
400      ## Step 2      ## Step 2
401      if (defined $self->{input_encoding} and      my $byte_buffer = '';
402          $self->{input_encoding} eq $charset) {      for (1..1024) {
403          my $char = $byte_stream->getc;
404          last unless defined $char;
405          $byte_buffer .= $char;
406        } ## TODO: timeout
407    
408        ## Step 3
409        if ($byte_buffer =~ /^\xFE\xFF/) {
410          $charset = Message::Charset::Info->get_by_html_name ('utf-16be');
411          ($char_stream, $e_status) = $charset->get_decode_handle
412              ($byte_stream, allow_error_reporting => 1,
413               allow_fallback => 1, byte_buffer => \$byte_buffer);
414        $self->{confident} = 1;        $self->{confident} = 1;
415        return;        last SNIFFING;
416        } elsif ($byte_buffer =~ /^\xFF\xFE/) {
417          $charset = Message::Charset::Info->get_by_html_name ('utf-16le');
418          ($char_stream, $e_status) = $charset->get_decode_handle
419              ($byte_stream, allow_error_reporting => 1,
420               allow_fallback => 1, byte_buffer => \$byte_buffer);
421          $self->{confident} = 1;
422          last SNIFFING;
423        } elsif ($byte_buffer =~ /^\xEF\xBB\xBF/) {
424          $charset = Message::Charset::Info->get_by_html_name ('utf-8');
425          ($char_stream, $e_status) = $charset->get_decode_handle
426              ($byte_stream, allow_error_reporting => 1,
427               allow_fallback => 1, byte_buffer => \$byte_buffer);
428          $self->{confident} = 1;
429          last SNIFFING;
430      }      }
431    
432      !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.      ## Step 4
433          ':'.$charset, level => 'w', token => $token);      ## TODO: <meta charset>
434    
435      ## Step 3      ## Step 5
436      # if (can) {      ## TODO: from history
       ## change the encoding on the fly.  
       #$self->{confident} = 1;  
       #return;  
     # }  
437    
438      ## Step 4      ## Step 6
439      throw Whatpm::HTML::RestartParser (charset => $charset);      require Whatpm::Charset::UniversalCharDet;
440        $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
441            ($byte_buffer);
442        if (defined $charset_name) {
443          $charset = Message::Charset::Info->get_by_html_name ($charset_name);
444    
445          ## ISSUE: Unsupported encoding is not ignored according to the spec.
446          require Whatpm::Charset::DecodeHandle;
447          $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
448              ($byte_stream);
449          ($char_stream, $e_status) = $charset->get_decode_handle
450              ($buffer, allow_error_reporting => 1,
451               allow_fallback => 1, byte_buffer => \$byte_buffer);
452          if ($char_stream) {
453            $buffer->{buffer} = $byte_buffer;
454            !!!parse-error (type => 'sniffing:chardet',
455                            text => $charset_name,
456                            level => $self->{level}->{info},
457                            layer => 'encode',
458                            line => 1, column => 1);
459            $self->{confident} = 0;
460            last SNIFFING;
461          }
462        }
463    
464        ## Step 7: default
465        ## TODO: Make this configurable.
466        $charset = Message::Charset::Info->get_by_html_name ('windows-1252');
467            ## NOTE: We choose |windows-1252| here, since |utf-8| should be
468            ## detectable in the step 6.
469        require Whatpm::Charset::DecodeHandle;
470        $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
471            ($byte_stream);
472        ($char_stream, $e_status)
473            = $charset->get_decode_handle ($buffer,
474                                           allow_error_reporting => 1,
475                                           allow_fallback => 1,
476                                           byte_buffer => \$byte_buffer);
477        $buffer->{buffer} = $byte_buffer;
478        !!!parse-error (type => 'sniffing:default',
479                        text => 'windows-1252',
480                        level => $self->{level}->{info},
481                        line => 1, column => 1,
482                        layer => 'encode');
483        $self->{confident} = 0;
484      } # SNIFFING
485    
486      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
487        $self->{input_encoding} = $charset->get_iana_name; ## TODO: Should we set actual charset decoder's encoding name?
488        !!!parse-error (type => 'chardecode:fallback',
489                        #text => $self->{input_encoding},
490                        level => $self->{level}->{uncertain},
491                        line => 1, column => 1,
492                        layer => 'encode');
493      } elsif (not ($e_status &
494                    Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
495        $self->{input_encoding} = $charset->get_iana_name;
496        !!!parse-error (type => 'chardecode:no error',
497                        text => $self->{input_encoding},
498                        level => $self->{level}->{uncertain},
499                        line => 1, column => 1,
500                        layer => 'encode');
501      } else {
502        $self->{input_encoding} = $charset->get_iana_name;
503      }
504    
505      $self->{change_encoding} = sub {
506        my $self = shift;
507        $charset_name = shift;
508        my $token = shift;
509    
510        $charset = Message::Charset::Info->get_by_html_name ($charset_name);
511        ($char_stream, $e_status) = $charset->get_decode_handle
512            ($byte_stream, allow_error_reporting => 1, allow_fallback => 1,
513             byte_buffer => \ $buffer->{buffer});
514        
515        if ($char_stream) { # if supported
516          ## "Change the encoding" algorithm:
517    
518          ## Step 1    
519          if ($charset->{category} &
520              Message::Charset::Info::CHARSET_CATEGORY_UTF16 ()) {
521            $charset = Message::Charset::Info->get_by_html_name ('utf-8');
522            ($char_stream, $e_status) = $charset->get_decode_handle
523                ($byte_stream,
524                 byte_buffer => \ $buffer->{buffer});
525          }
526          $charset_name = $charset->get_iana_name;
527          
528          ## Step 2
529          if (defined $self->{input_encoding} and
530              $self->{input_encoding} eq $charset_name) {
531            !!!parse-error (type => 'charset label:matching',
532                            text => $charset_name,
533                            level => $self->{level}->{info});
534            $self->{confident} = 1;
535            return;
536          }
537    
538          !!!parse-error (type => 'charset label detected',
539                          text => $self->{input_encoding},
540                          value => $charset_name,
541                          level => $self->{level}->{warn},
542                          token => $token);
543          
544          ## Step 3
545          # if (can) {
546            ## change the encoding on the fly.
547            #$self->{confident} = 1;
548            #return;
549          # }
550          
551          ## Step 4
552          throw Whatpm::HTML::RestartParser ();
553        }
554    }; # $self->{change_encoding}    }; # $self->{change_encoding}
555    
556      my $char_onerror = sub {
557        my (undef, $type, %opt) = @_;
558        !!!parse-error (layer => 'encode',
559                        %opt, type => $type,
560                        line => $self->{line}, column => $self->{column} + 1);
561        if ($opt{octets}) {
562          ${$opt{octets}} = "\x{FFFD}"; # relacement character
563        }
564      };
565      $char_stream->onerror ($char_onerror);
566    
567    my @args = @_; shift @args; # $s    my @args = @_; shift @args; # $s
568    my $return;    my $return;
569    try {    try {
570      $return = $self->parse_char_string ($s, @args);        $return = $self->parse_char_stream ($char_stream, @args);  
571    } catch Whatpm::HTML::RestartParser with {    } catch Whatpm::HTML::RestartParser with {
572      my $charset = shift->{charset};      ## NOTE: Invoked after {change_encoding}.
573      $s = \ (Encode::decode ($charset, $$bytes_s));      
574      $self->{input_encoding} = $charset; ## TODO: normalize      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
575          $self->{input_encoding} = $charset->get_iana_name; ## TODO: Should we set actual charset decoder's encoding name?
576          !!!parse-error (type => 'chardecode:fallback',
577                          level => $self->{level}->{uncertain},
578                          #text => $self->{input_encoding},
579                          line => 1, column => 1,
580                          layer => 'encode');
581        } elsif (not ($e_status &
582                      Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
583          $self->{input_encoding} = $charset->get_iana_name;
584          !!!parse-error (type => 'chardecode:no error',
585                          text => $self->{input_encoding},
586                          level => $self->{level}->{uncertain},
587                          line => 1, column => 1,
588                          layer => 'encode');
589        } else {
590          $self->{input_encoding} = $charset->get_iana_name;
591        }
592      $self->{confident} = 1;      $self->{confident} = 1;
593      $return = $self->parse_char_string ($s, @args);      $char_stream->onerror ($char_onerror);
594        $return = $self->parse_char_stream ($char_stream, @args);
595    };    };
596    return $return;    return $return;
597  } # parse_byte_string  } # parse_byte_stream
598    
599  ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM  ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
600  ## 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 605  sub parse_byte_string ($$$$;$) {
605  ## 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
606  ## strip the BOM and never strip any ZWNBSP.  ## strip the BOM and never strip any ZWNBSP.
607    
608  *parse_char_string = \&parse_string;  sub parse_char_string ($$$;$) {
609      my $self = shift;
610      require utf8;
611      my $s = ref $_[0] ? $_[0] : \($_[0]);
612      open my $input, '<' . (utf8::is_utf8 ($$s) ? ':utf8' : ''), $s;
613      return $self->parse_char_stream ($input, @_[1..$#_]);
614    } # parse_char_string
615    *parse_string = \&parse_char_string;
616    
617  sub parse_string ($$$;$) {  sub parse_char_stream ($$$;$) {
618    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
619    my $s = ref $_[0] ? $_[0] : \($_[0]);    my $input = $_[0];
620    $self->{document} = $_[1];    $self->{document} = $_[1];
621    @{$self->{document}->child_nodes} = ();    @{$self->{document}->child_nodes} = ();
622    
# Line 186  sub parse_string ($$$;$) { Line 635  sub parse_string ($$$;$) {
635      pop @{$self->{prev_char}};      pop @{$self->{prev_char}};
636      unshift @{$self->{prev_char}}, $self->{next_char};      unshift @{$self->{prev_char}}, $self->{next_char};
637    
638      $self->{next_char} = -1 and return if $i >= length $$s;      my $char;
639      $self->{next_char} = ord substr $$s, $i++, 1;      if (defined $self->{next_next_char}) {
640          $char = $self->{next_next_char};
641          delete $self->{next_next_char};
642        } else {
643          $char = $input->getc;
644        }
645        $self->{next_char} = -1 and return unless defined $char;
646        $self->{next_char} = ord $char;
647    
648      ($self->{line_prev}, $self->{column_prev})      ($self->{line_prev}, $self->{column_prev})
649          = ($self->{line}, $self->{column});          = ($self->{line}, $self->{column});
650      $self->{column}++;      $self->{column}++;
651            
652      if ($self->{next_char} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
653          !!!cp ('j1');
654        $self->{line}++;        $self->{line}++;
655        $self->{column} = 0;        $self->{column} = 0;
656      } elsif ($self->{next_char} == 0x000D) { # CR      } elsif ($self->{next_char} == 0x000D) { # CR
657        $i++ if substr ($$s, $i, 1) eq "\x0A";        !!!cp ('j2');
658          my $next = $input->getc;
659          if (defined $next and $next ne "\x0A") {
660            $self->{next_next_char} = $next;
661          }
662        $self->{next_char} = 0x000A; # LF # MUST        $self->{next_char} = 0x000A; # LF # MUST
663        $self->{line}++;        $self->{line}++;
664        $self->{column} = 0;        $self->{column} = 0;
665      } elsif ($self->{next_char} > 0x10FFFF) {      } elsif ($self->{next_char} > 0x10FFFF) {
666          !!!cp ('j3');
667        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
668      } elsif ($self->{next_char} == 0x0000) { # NULL      } elsif ($self->{next_char} == 0x0000) { # NULL
669          !!!cp ('j4');
670        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
671        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
672        } elsif ($self->{next_char} <= 0x0008 or
673                 (0x000E <= $self->{next_char} and $self->{next_char} <= 0x001F) or
674                 (0x007F <= $self->{next_char} and $self->{next_char} <= 0x009F) or
675                 (0xD800 <= $self->{next_char} and $self->{next_char} <= 0xDFFF) or
676                 (0xFDD0 <= $self->{next_char} and $self->{next_char} <= 0xFDDF) or
677                 {
678                  0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
679                  0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
680                  0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
681                  0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
682                  0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
683                  0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
684                  0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
685                  0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
686                  0x10FFFE => 1, 0x10FFFF => 1,
687                 }->{$self->{next_char}}) {
688          !!!cp ('j5');
689          if ($self->{next_char} < 0x10000) {
690            !!!parse-error (type => 'control char',
691                            text => (sprintf 'U+%04X', $self->{next_char}));
692          } else {
693            !!!parse-error (type => 'control char',
694                            text => (sprintf 'U-%08X', $self->{next_char}));
695          }
696      }      }
697    };    };
698    $self->{prev_char} = [-1, -1, -1];    $self->{prev_char} = [-1, -1, -1];
# Line 229  sub parse_string ($$$;$) { Line 716  sub parse_string ($$$;$) {
716    delete $self->{parse_error}; # remove loop    delete $self->{parse_error}; # remove loop
717    
718    return $self->{document};    return $self->{document};
719  } # parse_string  } # parse_char_stream
720    
721  sub new ($) {  sub new ($) {
722    my $class = shift;    my $class = shift;
723    my $self = bless {}, $class;    my $self = bless {
724        level => {must => 'm',
725                  should => 's',
726                  warn => 'w',
727                  info => 'i',
728                  uncertain => 'u'},
729      }, $class;
730    $self->{set_next_char} = sub {    $self->{set_next_char} = sub {
731      $self->{next_char} = -1;      $self->{next_char} = -1;
732    };    };
# Line 295  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO Line 788  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO
788  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
789  sub BOGUS_DOCTYPE_STATE () { 32 }  sub BOGUS_DOCTYPE_STATE () { 32 }
790  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
791    sub SELF_CLOSING_START_TAG_STATE () { 34 }
792    sub CDATA_BLOCK_STATE () { 35 }
793    
794  sub DOCTYPE_TOKEN () { 1 }  sub DOCTYPE_TOKEN () { 1 }
795  sub COMMENT_TOKEN () { 2 }  sub COMMENT_TOKEN () { 2 }
# Line 312  sub ROW_IMS ()        { 0b10000000 } Line 807  sub ROW_IMS ()        { 0b10000000 }
807  sub BODY_AFTER_IMS () { 0b100000000 }  sub BODY_AFTER_IMS () { 0b100000000 }
808  sub FRAME_IMS ()      { 0b1000000000 }  sub FRAME_IMS ()      { 0b1000000000 }
809  sub SELECT_IMS ()     { 0b10000000000 }  sub SELECT_IMS ()     { 0b10000000000 }
810    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
811        ## NOTE: "in foreign content" insertion mode is special; it is combined
812        ## with the secondary insertion mode.  In this parser, they are stored
813        ## together in the bit-or'ed form.
814    
815  ## NOTE: "initial" and "before html" insertion modes have no constants.  ## NOTE: "initial" and "before html" insertion modes have no constants.
816    
# Line 348  sub _initialize_tokenizer ($) { Line 847  sub _initialize_tokenizer ($) {
847    undef $self->{current_attribute};    undef $self->{current_attribute};
848    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
849    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
850      delete $self->{self_closing};
851    $self->{char} = [];    $self->{char} = [];
852    # $self->{next_char}    # $self->{next_char}
853    !!!next-input-character;    !!!next-input-character;
# Line 368  sub _initialize_tokenizer ($) { Line 868  sub _initialize_tokenizer ($) {
868  ##        ->{value}  ##        ->{value}
869  ##        ->{has_reference} == 1 or 0  ##        ->{has_reference} == 1 or 0
870  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
871    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
872    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
873    ##     while the token is pushed back to the stack.
874    
875  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
876    
# Line 394  sub _initialize_tokenizer ($) { Line 897  sub _initialize_tokenizer ($) {
897    
898  sub _get_next_token ($) {  sub _get_next_token ($) {
899    my $self = shift;    my $self = shift;
900    
901      if ($self->{self_closing}) {
902        !!!parse-error (type => 'nestc', token => $self->{current_token});
903        ## NOTE: The |self_closing| flag is only set by start tag token.
904        ## In addition, when a start tag token is emitted, it is always set to
905        ## |current_token|.
906        delete $self->{self_closing};
907      }
908    
909    if (@{$self->{token}}) {    if (@{$self->{token}}) {
910        $self->{self_closing} = $self->{token}->[0]->{self_closing};
911      return shift @{$self->{token}};      return shift @{$self->{token}};
912    }    }
913    
# Line 574  sub _get_next_token ($) { Line 1087  sub _get_next_token ($) {
1087            redo A;            redo A;
1088          } else {          } else {
1089            !!!cp (23);            !!!cp (23);
1090            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago',
1091                              line => $self->{line_prev},
1092                              column => $self->{column_prev});
1093            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1094            ## reconsume            ## reconsume
1095    
# Line 765  sub _get_next_token ($) { Line 1280  sub _get_next_token ($) {
1280    
1281          redo A;          redo A;
1282        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1283            !!!cp (42);
1284            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1285          !!!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  
1286          redo A;          redo A;
1287        } else {        } else {
1288          !!!cp (44);          !!!cp (44);
# Line 829  sub _get_next_token ($) { Line 1334  sub _get_next_token ($) {
1334          !!!next-input-character;          !!!next-input-character;
1335          redo A;          redo A;
1336        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1337            !!!cp (50);
1338            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1339          !!!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  
1340          redo A;          redo A;
1341        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1342          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
# Line 889  sub _get_next_token ($) { Line 1384  sub _get_next_token ($) {
1384          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1385              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1386            !!!cp (57);            !!!cp (57);
1387            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name}, line => $self->{current_attribute}->{line}, column => $self->{current_attribute}->{column});            !!!parse-error (type => 'duplicate attribute', text => $self->{current_attribute}->{name}, line => $self->{current_attribute}->{line}, column => $self->{current_attribute}->{column});
1388            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1389          } else {          } else {
1390            !!!cp (58);            !!!cp (58);
# Line 942  sub _get_next_token ($) { Line 1437  sub _get_next_token ($) {
1437          !!!next-input-character;          !!!next-input-character;
1438          redo A;          redo A;
1439        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1440            !!!cp (64);
1441          $before_leave->();          $before_leave->();
1442            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1443          !!!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  
1444          redo A;          redo A;
1445        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1446          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
# Line 1042  sub _get_next_token ($) { Line 1527  sub _get_next_token ($) {
1527          !!!next-input-character;          !!!next-input-character;
1528          redo A;          redo A;
1529        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1530            !!!cp (77);
1531            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1532          !!!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  
1533          redo A;          redo A;
1534        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1535          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
# Line 1081  sub _get_next_token ($) { Line 1555  sub _get_next_token ($) {
1555    
1556          redo A;          redo A;
1557        } else {        } else {
1558          !!!cp (82);          if ($self->{next_char} == 0x0022 or # "
1559                $self->{next_char} == 0x0027) { # '
1560              !!!cp (78);
1561              !!!parse-error (type => 'bad attribute name');
1562            } else {
1563              !!!cp (82);
1564            }
1565          $self->{current_attribute}          $self->{current_attribute}
1566              = {name => chr ($self->{next_char}),              = {name => chr ($self->{next_char}),
1567                 value => '',                 value => '',
# Line 1116  sub _get_next_token ($) { Line 1596  sub _get_next_token ($) {
1596          !!!next-input-character;          !!!next-input-character;
1597          redo A;          redo A;
1598        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1599            !!!parse-error (type => 'empty unquoted attribute value');
1600          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1601            !!!cp (87);            !!!cp (87);
1602            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
# Line 1388  sub _get_next_token ($) { Line 1869  sub _get_next_token ($) {
1869    
1870          redo A;          redo A;
1871        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1872            !!!cp (122);
1873            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1874          !!!next-input-character;          !!!next-input-character;
1875          if ($self->{next_char} == 0x003E and # >          redo A;
1876              $self->{current_token}->{type} == START_TAG_TOKEN and        } elsif ($self->{next_char} == -1) {
1877              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {          !!!parse-error (type => 'unclosed tag');
1878            # permitted slash          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1879            !!!cp (122);            !!!cp (122.3);
1880            #            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1881            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1882              if ($self->{current_token}->{attributes}) {
1883                !!!cp (122.1);
1884                !!!parse-error (type => 'end tag attribute');
1885              } else {
1886                ## NOTE: This state should never be reached.
1887                !!!cp (122.2);
1888              }
1889          } else {          } else {
1890            !!!cp (123);            die "$0: $self->{current_token}->{type}: Unknown token type";
           !!!parse-error (type => 'nestc');  
1891          }          }
1892          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = DATA_STATE;
1893          # next-input-character is already done          ## Reconsume.
1894            !!!emit ($self->{current_token}); # start tag or end tag
1895          redo A;          redo A;
1896        } else {        } else {
1897          !!!cp (124);          !!!cp ('124.1');
1898          !!!parse-error (type => 'no space between attributes');          !!!parse-error (type => 'no space between attributes');
1899          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1900          ## reconsume          ## reconsume
1901          redo A;          redo A;
1902        }        }
1903        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1904          if ($self->{next_char} == 0x003E) { # >
1905            if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1906              !!!cp ('124.2');
1907              !!!parse-error (type => 'nestc', token => $self->{current_token});
1908              ## TODO: Different type than slash in start tag
1909              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1910              if ($self->{current_token}->{attributes}) {
1911                !!!cp ('124.4');
1912                !!!parse-error (type => 'end tag attribute');
1913              } else {
1914                !!!cp ('124.5');
1915              }
1916              ## TODO: Test |<title></title/>|
1917            } else {
1918              !!!cp ('124.3');
1919              $self->{self_closing} = 1;
1920            }
1921    
1922            $self->{state} = DATA_STATE;
1923            !!!next-input-character;
1924    
1925            !!!emit ($self->{current_token}); # start tag or end tag
1926    
1927            redo A;
1928          } elsif ($self->{next_char} == -1) {
1929            !!!parse-error (type => 'unclosed tag');
1930            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1931              !!!cp (124.7);
1932              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1933            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1934              if ($self->{current_token}->{attributes}) {
1935                !!!cp (124.5);
1936                !!!parse-error (type => 'end tag attribute');
1937              } else {
1938                ## NOTE: This state should never be reached.
1939                !!!cp (124.6);
1940              }
1941            } else {
1942              die "$0: $self->{current_token}->{type}: Unknown token type";
1943            }
1944            $self->{state} = DATA_STATE;
1945            ## Reconsume.
1946            !!!emit ($self->{current_token}); # start tag or end tag
1947            redo A;
1948          } else {
1949            !!!cp ('124.4');
1950            !!!parse-error (type => 'nestc');
1951            ## TODO: This error type is wrong.
1952            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1953            ## Reconsume.
1954            redo A;
1955          }
1956      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1957        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1958                
# Line 1516  sub _get_next_token ($) { Line 2060  sub _get_next_token ($) {
2060          } else {          } else {
2061            !!!cp (135);            !!!cp (135);
2062          }          }
2063          } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
2064                   $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
2065                   $self->{next_char} == 0x005B) { # [
2066            !!!next-input-character;
2067            push @next_char, $self->{next_char};
2068            if ($self->{next_char} == 0x0043) { # C
2069              !!!next-input-character;
2070              push @next_char, $self->{next_char};
2071              if ($self->{next_char} == 0x0044) { # D
2072                !!!next-input-character;
2073                push @next_char, $self->{next_char};
2074                if ($self->{next_char} == 0x0041) { # A
2075                  !!!next-input-character;
2076                  push @next_char, $self->{next_char};
2077                  if ($self->{next_char} == 0x0054) { # T
2078                    !!!next-input-character;
2079                    push @next_char, $self->{next_char};
2080                    if ($self->{next_char} == 0x0041) { # A
2081                      !!!next-input-character;
2082                      push @next_char, $self->{next_char};
2083                      if ($self->{next_char} == 0x005B) { # [
2084                        !!!cp (135.1);
2085                        $self->{state} = CDATA_BLOCK_STATE;
2086                        !!!next-input-character;
2087                        redo A;
2088                      } else {
2089                        !!!cp (135.2);
2090                      }
2091                    } else {
2092                      !!!cp (135.3);
2093                    }
2094                  } else {
2095                    !!!cp (135.4);                
2096                  }
2097                } else {
2098                  !!!cp (135.5);
2099                }
2100              } else {
2101                !!!cp (135.6);
2102              }
2103            } else {
2104              !!!cp (135.7);
2105            }
2106        } else {        } else {
2107          !!!cp (136);          !!!cp (136);
2108        }        }
# Line 2115  sub _get_next_token ($) { Line 2702  sub _get_next_token ($) {
2702          redo A;          redo A;
2703        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2704          !!!cp (208);          !!!cp (208);
2705          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2706    
2707          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2708          !!!next-input-character;          !!!next-input-character;
# Line 2151  sub _get_next_token ($) { Line 2738  sub _get_next_token ($) {
2738          redo A;          redo A;
2739        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2740          !!!cp (212);          !!!cp (212);
2741          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2742    
2743          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2744          !!!next-input-character;          !!!next-input-character;
# Line 2199  sub _get_next_token ($) { Line 2786  sub _get_next_token ($) {
2786        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
2787          !!!cp (217);          !!!cp (217);
2788          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
   
2789          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2790          ## reconsume          ## reconsume
2791    
# Line 2240  sub _get_next_token ($) { Line 2826  sub _get_next_token ($) {
2826          !!!next-input-character;          !!!next-input-character;
2827          redo A;          redo A;
2828        }        }
2829        } elsif ($self->{state} == CDATA_BLOCK_STATE) {
2830          my $s = '';
2831          
2832          my ($l, $c) = ($self->{line}, $self->{column});
2833    
2834          CS: while ($self->{next_char} != -1) {
2835            if ($self->{next_char} == 0x005D) { # ]
2836              !!!next-input-character;
2837              if ($self->{next_char} == 0x005D) { # ]
2838                !!!next-input-character;
2839                MDC: {
2840                  if ($self->{next_char} == 0x003E) { # >
2841                    !!!cp (221.1);
2842                    !!!next-input-character;
2843                    last CS;
2844                  } elsif ($self->{next_char} == 0x005D) { # ]
2845                    !!!cp (221.2);
2846                    $s .= ']';
2847                    !!!next-input-character;
2848                    redo MDC;
2849                  } else {
2850                    !!!cp (221.3);
2851                    $s .= ']]';
2852                    #
2853                  }
2854                } # MDC
2855              } else {
2856                !!!cp (221.4);
2857                $s .= ']';
2858                #
2859              }
2860            } else {
2861              !!!cp (221.5);
2862              #
2863            }
2864            $s .= chr $self->{next_char};
2865            !!!next-input-character;
2866          } # CS
2867    
2868          $self->{state} = DATA_STATE;
2869          ## next-input-character done or EOF, which is reconsumed.
2870    
2871          if (length $s) {
2872            !!!cp (221.6);
2873            !!!emit ({type => CHARACTER_TOKEN, data => $s,
2874                      line => $l, column => $c});
2875          } else {
2876            !!!cp (221.7);
2877          }
2878    
2879          redo A;
2880    
2881          ## ISSUE: "text tokens" in spec.
2882          ## TODO: Streaming support
2883      } else {      } else {
2884        die "$0: $self->{state}: Unknown state";        die "$0: $self->{state}: Unknown state";
2885      }      }
# Line 2307  sub _tokenize_attempt_to_consume_an_enti Line 2947  sub _tokenize_attempt_to_consume_an_enti
2947    
2948          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2949            !!!cp (1008);            !!!cp (1008);
2950            !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);            !!!parse-error (type => 'invalid character reference',
2951                              text => (sprintf 'U+%04X', $code),
2952                              line => $l, column => $c);
2953            $code = 0xFFFD;            $code = 0xFFFD;
2954          } elsif ($code > 0x10FFFF) {          } elsif ($code > 0x10FFFF) {
2955            !!!cp (1009);            !!!cp (1009);
2956            !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);            !!!parse-error (type => 'invalid character reference',
2957                              text => (sprintf 'U-%08X', $code),
2958                              line => $l, column => $c);
2959            $code = 0xFFFD;            $code = 0xFFFD;
2960          } elsif ($code == 0x000D) {          } elsif ($code == 0x000D) {
2961            !!!cp (1010);            !!!cp (1010);
# Line 2319  sub _tokenize_attempt_to_consume_an_enti Line 2963  sub _tokenize_attempt_to_consume_an_enti
2963            $code = 0x000A;            $code = 0x000A;
2964          } elsif (0x80 <= $code and $code <= 0x9F) {          } elsif (0x80 <= $code and $code <= 0x9F) {
2965            !!!cp (1011);            !!!cp (1011);
2966            !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);            !!!parse-error (type => 'C1 character reference', text => (sprintf 'U+%04X', $code), line => $l, column => $c);
2967            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
2968          }          }
2969    
# Line 2352  sub _tokenize_attempt_to_consume_an_enti Line 2996  sub _tokenize_attempt_to_consume_an_enti
2996    
2997        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2998          !!!cp (1015);          !!!cp (1015);
2999          !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);          !!!parse-error (type => 'invalid character reference',
3000                            text => (sprintf 'U+%04X', $code),
3001                            line => $l, column => $c);
3002          $code = 0xFFFD;          $code = 0xFFFD;
3003        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
3004          !!!cp (1016);          !!!cp (1016);
3005          !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);          !!!parse-error (type => 'invalid character reference',
3006                            text => (sprintf 'U-%08X', $code),
3007                            line => $l, column => $c);
3008          $code = 0xFFFD;          $code = 0xFFFD;
3009        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
3010          !!!cp (1017);          !!!cp (1017);
3011          !!!parse-error (type => 'CR character reference', line => $l, column => $c);          !!!parse-error (type => 'CR character reference',
3012                            line => $l, column => $c);
3013          $code = 0x000A;          $code = 0x000A;
3014        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
3015          !!!cp (1018);          !!!cp (1018);
3016          !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);          !!!parse-error (type => 'C1 character reference',
3017                            text => (sprintf 'U+%04X', $code),
3018                            line => $l, column => $c);
3019          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
3020        }        }
3021                
# Line 2390  sub _tokenize_attempt_to_consume_an_enti Line 3041  sub _tokenize_attempt_to_consume_an_enti
3041      require Whatpm::_NamedEntityList;      require Whatpm::_NamedEntityList;
3042      our $EntityChar;      our $EntityChar;
3043    
3044      while (length $entity_name < 10 and      while (length $entity_name < 30 and
3045             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
3046             ((0x0041 <= $self->{next_char} and # a             ((0x0041 <= $self->{next_char} and # a
3047               $self->{next_char} <= 0x005A) or # x               $self->{next_char} <= 0x005A) or # x
# Line 2462  sub _initialize_tree_constructor ($) { Line 3113  sub _initialize_tree_constructor ($) {
3113    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
3114    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
3115    $self->{document}->manakai_is_html (1); # MUST    $self->{document}->manakai_is_html (1); # MUST
3116      $self->{document}->set_user_data (manakai_source_line => 1);
3117      $self->{document}->set_user_data (manakai_source_column => 1);
3118  } # _initialize_tree_constructor  } # _initialize_tree_constructor
3119    
3120  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 2516  sub _tree_construction_initial ($) { Line 3169  sub _tree_construction_initial ($) {
3169        ## language.        ## language.
3170        my $doctype_name = $token->{name};        my $doctype_name = $token->{name};
3171        $doctype_name = '' unless defined $doctype_name;        $doctype_name = '' unless defined $doctype_name;
3172        $doctype_name =~ tr/a-z/A-Z/;        $doctype_name =~ tr/a-z/A-Z/; # ASCII case-insensitive
3173        if (not defined $token->{name} or # <!DOCTYPE>        if (not defined $token->{name} or # <!DOCTYPE>
           defined $token->{public_identifier} or  
3174            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
3175          !!!cp ('t1');          !!!cp ('t1');
3176          !!!parse-error (type => 'not HTML5', token => $token);          !!!parse-error (type => 'not HTML5', token => $token);
3177        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
3178          !!!cp ('t2');          !!!cp ('t2');
         ## ISSUE: ASCII case-insensitive? (in fact it does not matter)  
3179          !!!parse-error (type => 'not HTML5', token => $token);          !!!parse-error (type => 'not HTML5', token => $token);
3180          } elsif (defined $token->{public_identifier}) {
3181            if ($token->{public_identifier} eq 'XSLT-compat') {
3182              !!!cp ('t1.2');
3183              !!!parse-error (type => 'XSLT-compat', token => $token,
3184                              level => $self->{level}->{should});
3185            } else {
3186              !!!parse-error (type => 'not HTML5', token => $token);
3187            }
3188        } else {        } else {
3189          !!!cp ('t3');          !!!cp ('t3');
3190            #
3191        }        }
3192                
3193        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
3194          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3195          ## NOTE: Default value for both |public_id| and |system_id| attributes
3196          ## are empty strings, so that we don't set any value in missing cases.
3197        $doctype->public_id ($token->{public_identifier})        $doctype->public_id ($token->{public_identifier})
3198            if defined $token->{public_identifier};            if defined $token->{public_identifier};
3199        $doctype->system_id ($token->{system_identifier})        $doctype->system_id ($token->{system_identifier})
# Line 2546  sub _tree_construction_initial ($) { Line 3208  sub _tree_construction_initial ($) {
3208        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{public_identifier}) {
3209          my $pubid = $token->{public_identifier};          my $pubid = $token->{public_identifier};
3210          $pubid =~ tr/a-z/A-z/;          $pubid =~ tr/a-z/A-z/;
3211          if ({          my $prefix = [
3212            "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,            "+//SILMARIL//DTD HTML PRO V0R11 19970101//",
3213            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3214            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3215            "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 1//",
3216            "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 2//",
3217            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//",
3218            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//",
3219            "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT//",
3220            "-//IETF//DTD HTML 2.0//EN" => 1,            "-//IETF//DTD HTML 2.0//",
3221            "-//IETF//DTD HTML 2.1E//EN" => 1,            "-//IETF//DTD HTML 2.1E//",
3222            "-//IETF//DTD HTML 3.0//EN" => 1,            "-//IETF//DTD HTML 3.0//",
3223            "-//IETF//DTD HTML 3.0//EN//" => 1,            "-//IETF//DTD HTML 3.2 FINAL//",
3224            "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,            "-//IETF//DTD HTML 3.2//",
3225            "-//IETF//DTD HTML 3.2//EN" => 1,            "-//IETF//DTD HTML 3//",
3226            "-//IETF//DTD HTML 3//EN" => 1,            "-//IETF//DTD HTML LEVEL 0//",
3227            "-//IETF//DTD HTML LEVEL 0//EN" => 1,            "-//IETF//DTD HTML LEVEL 1//",
3228            "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,            "-//IETF//DTD HTML LEVEL 2//",
3229            "-//IETF//DTD HTML LEVEL 1//EN" => 1,            "-//IETF//DTD HTML LEVEL 3//",
3230            "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 0//",
3231            "-//IETF//DTD HTML LEVEL 2//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 1//",
3232            "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 2//",
3233            "-//IETF//DTD HTML LEVEL 3//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 3//",
3234            "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,            "-//IETF//DTD HTML STRICT//",
3235            "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,            "-//IETF//DTD HTML//",
3236            "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,            "-//METRIUS//DTD METRIUS PRESENTATIONAL//",
3237            "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//",
3238            "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//",
3239            "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//",
3240            "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//",
3241            "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//",
3242            "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//",
3243            "-//IETF//DTD HTML STRICT//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD HTML//",
3244            "-//IETF//DTD HTML STRICT//EN//2.0" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//",
3245            "-//IETF//DTD HTML STRICT//EN//3.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//",
3246            "-//IETF//DTD HTML//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//",
3247            "-//IETF//DTD HTML//EN//2.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//",
3248            "-//IETF//DTD HTML//EN//3.0" => 1,            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//",
3249            "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//",
3250            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//",
3251            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//",
3252            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//",
3253            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//",
3254            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,            "-//W3C//DTD HTML 3 1995-03-24//",
3255            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,            "-//W3C//DTD HTML 3.2 DRAFT//",
3256            "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,            "-//W3C//DTD HTML 3.2 FINAL//",
3257            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//W3C//DTD HTML 3.2//",
3258            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//W3C//DTD HTML 3.2S DRAFT//",
3259            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 FRAMESET//",
3260            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 TRANSITIONAL//",
3261            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMETNAL 19960712//",
3262            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMENTAL 970421//",
3263            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//W3C//DTD W3 HTML//",
3264            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//W3O//DTD W3 HTML 3.0//",
3265            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML 2.0//",
3266            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML//",
3267            "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,          ]; # $prefix
3268            "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,          my $match;
3269            "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,          for (@$prefix) {
3270            "-//W3C//DTD HTML 3.2//EN" => 1,            if (substr ($prefix, 0, length $_) eq $_) {
3271            "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,              $match = 1;
3272            "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,              last;
3273            "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,            }
3274            "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,          }
3275            "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,          if ($match or
3276            "-//W3C//DTD W3 HTML//EN" => 1,              $pubid eq "-//W3O//DTD W3 HTML STRICT 3.0//EN//" or
3277            "-//W3O//DTD W3 HTML 3.0//EN" => 1,              $pubid eq "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" or
3278            "-//W3O//DTD W3 HTML 3.0//EN//" => 1,              $pubid eq "HTML") {
           "-//W3O//DTD W3 HTML STRICT 3.0//EN//" => 1,  
           "-//WEBTECHS//DTD MOZILLA HTML 2.0//EN" => 1,  
           "-//WEBTECHS//DTD MOZILLA HTML//EN" => 1,  
           "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,  
           "HTML" => 1,  
         }->{$pubid}) {  
3279            !!!cp ('t5');            !!!cp ('t5');
3280            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3281          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD HTML 4.01 FRAMESET//] or
3282                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD HTML 4.01 TRANSITIONAL//]) {
3283            if (defined $token->{system_identifier}) {            if (defined $token->{system_identifier}) {
3284              !!!cp ('t6');              !!!cp ('t6');
3285              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
# Line 2631  sub _tree_construction_initial ($) { Line 3287  sub _tree_construction_initial ($) {
3287              !!!cp ('t7');              !!!cp ('t7');
3288              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
3289            }            }
3290          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD XHTML 1.0 FRAMESET//] or
3291                   $pubid eq "-//W3C//DTD XHTML 1.0 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD XHTML 1.0 TRANSITIONAL//]) {
3292            !!!cp ('t8');            !!!cp ('t8');
3293            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
3294          } else {          } else {
# Line 2645  sub _tree_construction_initial ($) { Line 3301  sub _tree_construction_initial ($) {
3301          my $sysid = $token->{system_identifier};          my $sysid = $token->{system_identifier};
3302          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
3303          if ($sysid eq "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd") {          if ($sysid eq "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd") {
3304            ## TODO: Check the spec: PUBLIC "(limited quirks)" "(quirks)"            ## NOTE: Ensure that |PUBLIC "(limited quirks)" "(quirks)"| is
3305              ## marked as quirks.
3306            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3307            !!!cp ('t11');            !!!cp ('t11');
3308          } else {          } else {
# Line 2668  sub _tree_construction_initial ($) { Line 3325  sub _tree_construction_initial ($) {
3325        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3326        ## Go to the "before html" insertion mode.        ## Go to the "before html" insertion mode.
3327        ## reprocess        ## reprocess
3328          !!!ack-later;
3329        return;        return;
3330      } elsif ($token->{type} == CHARACTER_TOKEN) {      } elsif ($token->{type} == CHARACTER_TOKEN) {
3331        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 3406  sub _tree_construction_root_element ($)
3406        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
3407          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
3408            my $root_element;            my $root_element;
3409            !!!create-element ($root_element, $token->{tag_name}, $token->{attributes}, $token);            !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3410            $self->{document}->append_child ($root_element);            $self->{document}->append_child ($root_element);
3411            push @{$self->{open_elements}}, [$root_element, 'html'];            push @{$self->{open_elements}},
3412                  [$root_element, $el_category->{html}];
3413    
3414            if ($token->{attributes}->{manifest}) {            if ($token->{attributes}->{manifest}) {
3415              !!!cp ('t24');              !!!cp ('t24');
# Line 2765  sub _tree_construction_root_element ($) Line 3424  sub _tree_construction_root_element ($)
3424              $self->{application_cache_selection}->(undef);              $self->{application_cache_selection}->(undef);
3425            }            }
3426    
3427              !!!nack ('t25c');
3428    
3429            !!!next-token;            !!!next-token;
3430            return; ## Go to the "before head" insertion mode.            return; ## Go to the "before head" insertion mode.
3431          } else {          } else {
# Line 2781  sub _tree_construction_root_element ($) Line 3442  sub _tree_construction_root_element ($)
3442          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
3443        }        }
3444    
3445      my $root_element; !!!create-element ($root_element, 'html',, $token);      my $root_element;
3446        !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3447      $self->{document}->append_child ($root_element);      $self->{document}->append_child ($root_element);
3448      push @{$self->{open_elements}}, [$root_element, 'html'];      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3449    
3450      $self->{application_cache_selection}->(undef);      $self->{application_cache_selection}->(undef);
3451    
3452      ## NOTE: Reprocess the token.      ## NOTE: Reprocess the token.
3453        !!!ack-later;
3454      return; ## Go to the "before head" insertion mode.      return; ## Go to the "before head" insertion mode.
3455    
3456      ## ISSUE: There is an issue in the spec      ## ISSUE: There is an issue in the spec
# Line 2811  sub _reset_insertion_mode ($) { Line 3474  sub _reset_insertion_mode ($) {
3474        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3475          $last = 1;          $last = 1;
3476          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
3477            if ($self->{inner_html_node}->[1] eq 'td' or            !!!cp ('t28');
3478                $self->{inner_html_node}->[1] eq 'th') {            $node = $self->{inner_html_node};
3479              !!!cp ('t27');          } else {
3480              #            die "_reset_insertion_mode: t27";
           } else {  
             !!!cp ('t28');  
             $node = $self->{inner_html_node};  
           }  
3481          }          }
3482        }        }
3483              
3484        ## Step 4..13        ## Step 4..14
3485        my $new_mode = {        my $new_mode;
3486          if ($node->[1] & FOREIGN_EL) {
3487            !!!cp ('t28.1');
3488            ## NOTE: Strictly spaking, the line below only applies to MathML and
3489            ## SVG elements.  Currently the HTML syntax supports only MathML and
3490            ## SVG elements as foreigners.
3491            $new_mode = IN_BODY_IM | IN_FOREIGN_CONTENT_IM;
3492          } elsif ($node->[1] & TABLE_CELL_EL) {
3493            if ($last) {
3494              !!!cp ('t28.2');
3495              #
3496            } else {
3497              !!!cp ('t28.3');
3498              $new_mode = IN_CELL_IM;
3499            }
3500          } else {
3501            !!!cp ('t28.4');
3502            $new_mode = {
3503                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
3504                        ## NOTE: |option| and |optgroup| do not set                        ## NOTE: |option| and |optgroup| do not set
3505                        ## insertion mode to "in select" by themselves.                        ## insertion mode to "in select" by themselves.
                       td => IN_CELL_IM,  
                       th => IN_CELL_IM,  
3506                        tr => IN_ROW_IM,                        tr => IN_ROW_IM,
3507                        tbody => IN_TABLE_BODY_IM,                        tbody => IN_TABLE_BODY_IM,
3508                        thead => IN_TABLE_BODY_IM,                        thead => IN_TABLE_BODY_IM,
# Line 2839  sub _reset_insertion_mode ($) { Line 3513  sub _reset_insertion_mode ($) {
3513                        head => IN_BODY_IM, # not in head!                        head => IN_BODY_IM, # not in head!
3514                        body => IN_BODY_IM,                        body => IN_BODY_IM,
3515                        frameset => IN_FRAMESET_IM,                        frameset => IN_FRAMESET_IM,
3516                       }->{$node->[1]};                       }->{$node->[0]->manakai_local_name};
3517          }
3518        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3519                
3520        ## Step 14        ## Step 15
3521        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3522          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3523            !!!cp ('t29');            !!!cp ('t29');
3524            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
# Line 2857  sub _reset_insertion_mode ($) { Line 3532  sub _reset_insertion_mode ($) {
3532          !!!cp ('t31');          !!!cp ('t31');
3533        }        }
3534                
3535        ## Step 15        ## Step 16
3536        $self->{insertion_mode} = IN_BODY_IM and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
3537                
3538        ## Step 16        ## Step 17
3539        $i--;        $i--;
3540        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3541                
3542        ## Step 17        ## Step 18
3543        redo S3;        redo S3;
3544      } # S3      } # S3
3545    
# Line 2976  sub _tree_construction_main ($) { Line 3651  sub _tree_construction_main ($) {
3651      ## Step 1      ## Step 1
3652      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3653      my $el;      my $el;
3654      !!!create-element ($el, $start_tag_name, $token->{attributes}, $token);      !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3655    
3656      ## Step 2      ## Step 2
3657      $insert->($el);      $insert->($el);
# Line 2987  sub _tree_construction_main ($) { Line 3662  sub _tree_construction_main ($) {
3662    
3663      ## Step 4      ## Step 4
3664      my $text = '';      my $text = '';
3665        !!!nack ('t40.1');
3666      !!!next-token;      !!!next-token;
3667      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3668        !!!cp ('t40');        !!!cp ('t40');
# Line 3013  sub _tree_construction_main ($) { Line 3689  sub _tree_construction_main ($) {
3689        ## NOTE: An end-of-file token.        ## NOTE: An end-of-file token.
3690        if ($content_model_flag == CDATA_CONTENT_MODEL) {        if ($content_model_flag == CDATA_CONTENT_MODEL) {
3691          !!!cp ('t43');          !!!cp ('t43');
3692          !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);          !!!parse-error (type => 'in CDATA:#eof', token => $token);
3693        } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {        } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3694          !!!cp ('t44');          !!!cp ('t44');
3695          !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);          !!!parse-error (type => 'in RCDATA:#eof', token => $token);
3696        } else {        } else {
3697          die "$0: $content_model_flag in parse_rcdata";          die "$0: $content_model_flag in parse_rcdata";
3698        }        }
# Line 3026  sub _tree_construction_main ($) { Line 3702  sub _tree_construction_main ($) {
3702    
3703    my $script_start_tag = sub () {    my $script_start_tag = sub () {
3704      my $script_el;      my $script_el;
3705      !!!create-element ($script_el, 'script', $token->{attributes}, $token);      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
3706      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3707    
3708      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
3709      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
3710            
3711      my $text = '';      my $text = '';
3712        !!!nack ('t45.1');
3713      !!!next-token;      !!!next-token;
3714      while ($token->{type} == CHARACTER_TOKEN) {      while ($token->{type} == CHARACTER_TOKEN) {
3715        !!!cp ('t45');        !!!cp ('t45');
# Line 3052  sub _tree_construction_main ($) { Line 3729  sub _tree_construction_main ($) {
3729        ## Ignore the token        ## Ignore the token
3730      } else {      } else {
3731        !!!cp ('t48');        !!!cp ('t48');
3732        !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);        !!!parse-error (type => 'in CDATA:#eof', token => $token);
3733        ## ISSUE: And ignore?        ## ISSUE: And ignore?
3734        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3735      }      }
# Line 3090  sub _tree_construction_main ($) { Line 3767  sub _tree_construction_main ($) {
3767        my $formatting_element;        my $formatting_element;
3768        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3769        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3770          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3771              !!!cp ('t52');
3772              last AFE;
3773            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
3774                         eq $tag_name) {
3775            !!!cp ('t51');            !!!cp ('t51');
3776            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3777            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3778            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           !!!cp ('t52');  
           last AFE;  
3779          }          }
3780        } # AFE        } # AFE
3781        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3782          !!!cp ('t53');          !!!cp ('t53');
3783          !!!parse-error (type => 'unmatched end tag:'.$tag_name, token => $end_tag_token);          !!!parse-error (type => 'unmatched end tag', text => $tag_name, token => $end_tag_token);
3784          ## Ignore the token          ## Ignore the token
3785          !!!next-token;          !!!next-token;
3786          return;          return;
# Line 3119  sub _tree_construction_main ($) { Line 3797  sub _tree_construction_main ($) {
3797              last INSCOPE;              last INSCOPE;
3798            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3799              !!!cp ('t55');              !!!cp ('t55');
3800              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},              !!!parse-error (type => 'unmatched end tag',
3801                                text => $token->{tag_name},
3802                              token => $end_tag_token);                              token => $end_tag_token);
3803              ## Ignore the token              ## Ignore the token
3804              !!!next-token;              !!!next-token;
3805              return;              return;
3806            }            }
3807          } 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]}) {  
3808            !!!cp ('t56');            !!!cp ('t56');
3809            $in_scope = 0;            $in_scope = 0;
3810          }          }
3811        } # INSCOPE        } # INSCOPE
3812        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3813          !!!cp ('t57');          !!!cp ('t57');
3814          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},          !!!parse-error (type => 'unmatched end tag',
3815                            text => $token->{tag_name},
3816                          token => $end_tag_token);                          token => $end_tag_token);
3817          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3818          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
# Line 3143  sub _tree_construction_main ($) { Line 3820  sub _tree_construction_main ($) {
3820        }        }
3821        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3822          !!!cp ('t58');          !!!cp ('t58');
3823          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1],          !!!parse-error (type => 'not closed',
3824                            text => $self->{open_elements}->[-1]->[0]
3825                                ->manakai_local_name,
3826                          token => $end_tag_token);                          token => $end_tag_token);
3827        }        }
3828                
# Line 3152  sub _tree_construction_main ($) { Line 3831  sub _tree_construction_main ($) {
3831        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
3832        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3833          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3834          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
3835              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3836              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
3837               $scoping_category->{$node->[1]})) { ## Scoping is redundant, maybe               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
3838            !!!cp ('t59');            !!!cp ('t59');
3839            $furthest_block = $node;            $furthest_block = $node;
3840            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
# Line 3241  sub _tree_construction_main ($) { Line 3920  sub _tree_construction_main ($) {
3920        } # S7          } # S7  
3921                
3922        ## Step 8        ## Step 8
3923        if ({        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
            table => 1, tbody => 1, tfoot => 1, thead => 1, tr => 1,  
           }->{$common_ancestor_node->[1]}) {  
3924          my $foster_parent_element;          my $foster_parent_element;
3925          my $next_sibling;          my $next_sibling;
3926                           OE: for (reverse 0..$#{$self->{open_elements}}) {          OE: for (reverse 0..$#{$self->{open_elements}}) {
3927                             if ($self->{open_elements}->[$_]->[1] eq 'table') {            if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3928                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3929                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3930                                 !!!cp ('t65.1');                                 !!!cp ('t65.1');
# Line 3320  sub _tree_construction_main ($) { Line 3997  sub _tree_construction_main ($) {
3997    
3998    my $insert_to_foster = sub {    my $insert_to_foster = sub {
3999      my $child = shift;      my $child = shift;
4000      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]}) {  
4001        # MUST        # MUST
4002        my $foster_parent_element;        my $foster_parent_element;
4003        my $next_sibling;        my $next_sibling;
4004                           OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
4005                             if ($self->{open_elements}->[$_]->[1] eq 'table') {          if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
4006                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4007                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
4008                                 !!!cp ('t70');                                 !!!cp ('t70');
# Line 3352  sub _tree_construction_main ($) { Line 4027  sub _tree_construction_main ($) {
4027      }      }
4028    }; # $insert_to_foster    }; # $insert_to_foster
4029    
4030    B: {    B: while (1) {
4031      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
4032        !!!cp ('t73');        !!!cp ('t73');
4033        !!!parse-error (type => 'DOCTYPE in the middle', token => $token);        !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
4034        ## Ignore the token        ## Ignore the token
4035        ## Stay in the phase        ## Stay in the phase
4036        !!!next-token;        !!!next-token;
4037        redo B;        next B;
4038      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
4039               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
4040        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4041          !!!cp ('t79');          !!!cp ('t79');
4042          !!!parse-error (type => 'after html:html', token => $token);          !!!parse-error (type => 'after html', text => 'html', token => $token);
4043          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
4044        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4045          !!!cp ('t80');          !!!cp ('t80');
4046          !!!parse-error (type => 'after html:html', token => $token);          !!!parse-error (type => 'after html', text => 'html', token => $token);
4047          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
4048        } else {        } else {
4049          !!!cp ('t81');          !!!cp ('t81');
# Line 3385  sub _tree_construction_main ($) { Line 4060  sub _tree_construction_main ($) {
4060               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
4061          }          }
4062        }        }
4063          !!!nack ('t84.1');
4064        !!!next-token;        !!!next-token;
4065        redo B;        next B;
4066      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
4067        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
4068        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
# Line 3400  sub _tree_construction_main ($) { Line 4076  sub _tree_construction_main ($) {
4076          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
4077        }        }
4078        !!!next-token;        !!!next-token;
4079        redo B;        next B;
4080      } elsif ($self->{insertion_mode} & HEAD_IMS) {      } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
4081          if ($token->{type} == CHARACTER_TOKEN) {
4082            !!!cp ('t87.1');
4083            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4084            !!!next-token;
4085            next B;
4086          } elsif ($token->{type} == START_TAG_TOKEN) {
4087            if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
4088                 $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
4089                not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
4090                ($token->{tag_name} eq 'svg' and
4091                 $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
4092              ## NOTE: "using the rules for secondary insertion mode"then"continue"
4093              !!!cp ('t87.2');
4094              #
4095            } elsif ({
4096                      b => 1, big => 1, blockquote => 1, body => 1, br => 1,
4097                      center => 1, code => 1, dd => 1, div => 1, dl => 1, dt => 1,
4098                      em => 1, embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1,
4099                      h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, i => 1,
4100                      img => 1, li => 1, listing => 1, menu => 1, meta => 1,
4101                      nobr => 1, ol => 1, p => 1, pre => 1, ruby => 1, s => 1,
4102                      small => 1, span => 1, strong => 1, strike => 1, sub => 1,
4103                      sup => 1, table => 1, tt => 1, u => 1, ul => 1, var => 1,
4104                     }->{$token->{tag_name}}) {
4105              !!!cp ('t87.2');
4106              !!!parse-error (type => 'not closed',
4107                              text => $self->{open_elements}->[-1]->[0]
4108                                  ->manakai_local_name,
4109                              token => $token);
4110    
4111              pop @{$self->{open_elements}}
4112                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4113    
4114              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4115              ## Reprocess.
4116              next B;
4117            } else {
4118              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
4119              my $tag_name = $token->{tag_name};
4120              if ($nsuri eq $SVG_NS) {
4121                $tag_name = {
4122                   altglyph => 'altGlyph',
4123                   altglyphdef => 'altGlyphDef',
4124                   altglyphitem => 'altGlyphItem',
4125                   animatecolor => 'animateColor',
4126                   animatemotion => 'animateMotion',
4127                   animatetransform => 'animateTransform',
4128                   clippath => 'clipPath',
4129                   feblend => 'feBlend',
4130                   fecolormatrix => 'feColorMatrix',
4131                   fecomponenttransfer => 'feComponentTransfer',
4132                   fecomposite => 'feComposite',
4133                   feconvolvematrix => 'feConvolveMatrix',
4134                   fediffuselighting => 'feDiffuseLighting',
4135                   fedisplacementmap => 'feDisplacementMap',
4136                   fedistantlight => 'feDistantLight',
4137                   feflood => 'feFlood',
4138                   fefunca => 'feFuncA',
4139                   fefuncb => 'feFuncB',
4140                   fefuncg => 'feFuncG',
4141                   fefuncr => 'feFuncR',
4142                   fegaussianblur => 'feGaussianBlur',
4143                   feimage => 'feImage',
4144                   femerge => 'feMerge',
4145                   femergenode => 'feMergeNode',
4146                   femorphology => 'feMorphology',
4147                   feoffset => 'feOffset',
4148                   fepointlight => 'fePointLight',
4149                   fespecularlighting => 'feSpecularLighting',
4150                   fespotlight => 'feSpotLight',
4151                   fetile => 'feTile',
4152                   feturbulence => 'feTurbulence',
4153                   foreignobject => 'foreignObject',
4154                   glyphref => 'glyphRef',
4155                   lineargradient => 'linearGradient',
4156                   radialgradient => 'radialGradient',
4157                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4158                   textpath => 'textPath',  
4159                }->{$tag_name} || $tag_name;
4160              }
4161    
4162              ## "adjust SVG attributes" (SVG only) - done in insert-element-f
4163    
4164              ## "adjust foreign attributes" - done in insert-element-f
4165    
4166              !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
4167    
4168              if ($self->{self_closing}) {
4169                pop @{$self->{open_elements}};
4170                !!!ack ('t87.3');
4171              } else {
4172                !!!cp ('t87.4');
4173              }
4174    
4175              !!!next-token;
4176              next B;
4177            }
4178          } elsif ($token->{type} == END_TAG_TOKEN) {
4179            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4180            !!!cp ('t87.5');
4181            #
4182          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4183            !!!cp ('t87.6');
4184            !!!parse-error (type => 'not closed',
4185                            text => $self->{open_elements}->[-1]->[0]
4186                                ->manakai_local_name,
4187                            token => $token);
4188    
4189            pop @{$self->{open_elements}}
4190                while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4191    
4192            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4193            ## Reprocess.
4194            next B;
4195          } else {
4196            die "$0: $token->{type}: Unknown token type";        
4197          }
4198        }
4199    
4200        if ($self->{insertion_mode} & HEAD_IMS) {
4201        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4202          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4203            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
# Line 3411  sub _tree_construction_main ($) { Line 4207  sub _tree_construction_main ($) {
4207              !!!cp ('t88.1');              !!!cp ('t88.1');
4208              ## Ignore the token.              ## Ignore the token.
4209              !!!next-token;              !!!next-token;
4210              redo B;              next B;
4211            }            }
4212            unless (length $token->{data}) {            unless (length $token->{data}) {
4213              !!!cp ('t88');              !!!cp ('t88');
4214              !!!next-token;              !!!next-token;
4215              redo B;              next B;
4216            }            }
4217          }          }
4218    
4219          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4220            !!!cp ('t89');            !!!cp ('t89');
4221            ## As if <head>            ## As if <head>
4222            !!!create-element ($self->{head_element}, 'head',, $token);            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4223            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4224            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            push @{$self->{open_elements}},
4225                  [$self->{head_element}, $el_category->{head}];
4226    
4227            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4228            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
# Line 3435  sub _tree_construction_main ($) { Line 4232  sub _tree_construction_main ($) {
4232            !!!cp ('t90');            !!!cp ('t90');
4233            ## As if </noscript>            ## As if </noscript>
4234            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4235            !!!parse-error (type => 'in noscript:#character', token => $token);            !!!parse-error (type => 'in noscript:#text', token => $token);
4236                        
4237            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4238            ## As if </head>            ## As if </head>
# Line 3451  sub _tree_construction_main ($) { Line 4248  sub _tree_construction_main ($) {
4248            !!!cp ('t92');            !!!cp ('t92');
4249          }          }
4250    
4251              ## "after head" insertion mode          ## "after head" insertion mode
4252              ## As if <body>          ## As if <body>
4253              !!!insert-element ('body',, $token);          !!!insert-element ('body',, $token);
4254              $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4255              ## reprocess          ## reprocess
4256              redo B;          next B;
4257            } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4258              if ($token->{tag_name} eq 'head') {          if ($token->{tag_name} eq 'head') {
4259                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4260                  !!!cp ('t93');              !!!cp ('t93');
4261                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes}, $token);              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4262                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              $self->{open_elements}->[-1]->[0]->append_child
4263                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];                  ($self->{head_element});
4264                  $self->{insertion_mode} = IN_HEAD_IM;              push @{$self->{open_elements}},
4265                  !!!next-token;                  [$self->{head_element}, $el_category->{head}];
4266                  redo B;              $self->{insertion_mode} = IN_HEAD_IM;
4267                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {              !!!nack ('t93.1');
4268                  !!!cp ('t94');              !!!next-token;
4269                  #              next B;
4270                } else {            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4271                  !!!cp ('t95');              !!!cp ('t93.2');
4272                  !!!parse-error (type => 'in head:head', token => $token); # or in head noscript              !!!parse-error (type => 'after head', text => 'head',
4273                  ## Ignore the token                              token => $token);
4274                  !!!next-token;              ## Ignore the token
4275                  redo B;              !!!nack ('t93.3');
4276                }              !!!next-token;
4277              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              next B;
4278                !!!cp ('t96');            } else {
4279                ## As if <head>              !!!cp ('t95');
4280                !!!create-element ($self->{head_element}, 'head',, $token);              !!!parse-error (type => 'in head:head',
4281                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                              token => $token); # or in head noscript
4282                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];              ## Ignore the token
4283                !!!nack ('t95.1');
4284                !!!next-token;
4285                next B;
4286              }
4287            } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4288              !!!cp ('t96');
4289              ## As if <head>
4290              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4291              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4292              push @{$self->{open_elements}},
4293                  [$self->{head_element}, $el_category->{head}];
4294    
4295                $self->{insertion_mode} = IN_HEAD_IM;            $self->{insertion_mode} = IN_HEAD_IM;
4296                ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4297              } else {          } else {
4298                !!!cp ('t97');            !!!cp ('t97');
4299              }          }
4300    
4301              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
4302                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4303                  !!!cp ('t98');                  !!!cp ('t98');
4304                  ## As if </noscript>                  ## As if </noscript>
4305                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4306                  !!!parse-error (type => 'in noscript:base', token => $token);                  !!!parse-error (type => 'in noscript', text => 'base',
4307                                    token => $token);
4308                                
4309                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4310                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
# Line 3506  sub _tree_construction_main ($) { Line 4315  sub _tree_construction_main ($) {
4315                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4316                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4317                  !!!cp ('t100');                  !!!cp ('t100');
4318                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4319                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4320                    push @{$self->{open_elements}},
4321                        [$self->{head_element}, $el_category->{head}];
4322                } else {                } else {
4323                  !!!cp ('t101');                  !!!cp ('t101');
4324                }                }
# Line 3515  sub _tree_construction_main ($) { Line 4326  sub _tree_construction_main ($) {
4326                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4327                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4328                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4329                  !!!nack ('t101.1');
4330                !!!next-token;                !!!next-token;
4331                redo B;                next B;
4332              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
4333                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4334                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4335                  !!!cp ('t102');                  !!!cp ('t102');
4336                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4337                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4338                    push @{$self->{open_elements}},
4339                        [$self->{head_element}, $el_category->{head}];
4340                } else {                } else {
4341                  !!!cp ('t103');                  !!!cp ('t103');
4342                }                }
# Line 3530  sub _tree_construction_main ($) { Line 4344  sub _tree_construction_main ($) {
4344                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4345                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4346                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4347                  !!!ack ('t103.1');
4348                !!!next-token;                !!!next-token;
4349                redo B;                next B;
4350              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
4351                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4352                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4353                  !!!cp ('t104');                  !!!cp ('t104');
4354                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4355                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4356                    push @{$self->{open_elements}},
4357                        [$self->{head_element}, $el_category->{head}];
4358                } else {                } else {
4359                  !!!cp ('t105');                  !!!cp ('t105');
4360                }                }
# Line 3545  sub _tree_construction_main ($) { Line 4362  sub _tree_construction_main ($) {
4362                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.
4363    
4364                unless ($self->{confident}) {                unless ($self->{confident}) {
4365                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) {
4366                    !!!cp ('t106');                    !!!cp ('t106');
4367                      ## NOTE: Whether the encoding is supported or not is handled
4368                      ## in the {change_encoding} callback.
4369                    $self->{change_encoding}                    $self->{change_encoding}
4370                        ->($self, $token->{attributes}->{charset}->{value},                        ->($self, $token->{attributes}->{charset}->{value},
4371                           $token);                           $token);
# Line 3556  sub _tree_construction_main ($) { Line 4375  sub _tree_construction_main ($) {
4375                                             $token->{attributes}->{charset}                                             $token->{attributes}->{charset}
4376                                                 ->{has_reference});                                                 ->{has_reference});
4377                  } elsif ($token->{attributes}->{content}) {                  } elsif ($token->{attributes}->{content}) {
                   ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
4378                    if ($token->{attributes}->{content}->{value}                    if ($token->{attributes}->{content}->{value}
4379                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                        =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4380                            [\x09-\x0D\x20]*=                            [\x09-\x0D\x20]*=
4381                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4382                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
4383                      !!!cp ('t107');                      !!!cp ('t107');
4384                        ## NOTE: Whether the encoding is supported or not is handled
4385                        ## in the {change_encoding} callback.
4386                      $self->{change_encoding}                      $self->{change_encoding}
4387                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4388                             $token);                             $token);
# Line 3593  sub _tree_construction_main ($) { Line 4413  sub _tree_construction_main ($) {
4413    
4414                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4415                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4416                  !!!ack ('t110.1');
4417                !!!next-token;                !!!next-token;
4418                redo B;                next B;
4419              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
4420                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4421                  !!!cp ('t111');                  !!!cp ('t111');
4422                  ## As if </noscript>                  ## As if </noscript>
4423                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4424                  !!!parse-error (type => 'in noscript:title', token => $token);                  !!!parse-error (type => 'in noscript', text => 'title',
4425                                    token => $token);
4426                                
4427                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4428                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4429                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4430                  !!!cp ('t112');                  !!!cp ('t112');
4431                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4432                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4433                    push @{$self->{open_elements}},
4434                        [$self->{head_element}, $el_category->{head}];
4435                } else {                } else {
4436                  !!!cp ('t113');                  !!!cp ('t113');
4437                }                }
# Line 3618  sub _tree_construction_main ($) { Line 4442  sub _tree_construction_main ($) {
4442                $parse_rcdata->(RCDATA_CONTENT_MODEL);                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4443                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4444                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4445                redo B;                next B;
4446              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style' or
4447                         $token->{tag_name} eq 'noframes') {
4448                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4449                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
4450                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4451                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4452                  !!!cp ('t114');                  !!!cp ('t114');
4453                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4454                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4455                    push @{$self->{open_elements}},
4456                        [$self->{head_element}, $el_category->{head}];
4457                } else {                } else {
4458                  !!!cp ('t115');                  !!!cp ('t115');
4459                }                }
4460                $parse_rcdata->(CDATA_CONTENT_MODEL);                $parse_rcdata->(CDATA_CONTENT_MODEL);
4461                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4462                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4463                redo B;                next B;
4464              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4465                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
4466                  !!!cp ('t116');                  !!!cp ('t116');
4467                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
4468                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4469                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4470                    !!!nack ('t116.1');
4471                  !!!next-token;                  !!!next-token;
4472                  redo B;                  next B;
4473                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4474                  !!!cp ('t117');                  !!!cp ('t117');
4475                  !!!parse-error (type => 'in noscript:noscript', token => $token);                  !!!parse-error (type => 'in noscript', text => 'noscript',
4476                                    token => $token);
4477                  ## Ignore the token                  ## Ignore the token
4478                    !!!nack ('t117.1');
4479                  !!!next-token;                  !!!next-token;
4480                  redo B;                  next B;
4481                } else {                } else {
4482                  !!!cp ('t118');                  !!!cp ('t118');
4483                  #                  #
# Line 3657  sub _tree_construction_main ($) { Line 4487  sub _tree_construction_main ($) {
4487                  !!!cp ('t119');                  !!!cp ('t119');
4488                  ## As if </noscript>                  ## As if </noscript>
4489                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4490                  !!!parse-error (type => 'in noscript:script', token => $token);                  !!!parse-error (type => 'in noscript', text => 'script',
4491                                    token => $token);
4492                                
4493                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4494                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4495                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4496                  !!!cp ('t120');                  !!!cp ('t120');
4497                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4498                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4499                    push @{$self->{open_elements}},
4500                        [$self->{head_element}, $el_category->{head}];
4501                } else {                } else {
4502                  !!!cp ('t121');                  !!!cp ('t121');
4503                }                }
# Line 3673  sub _tree_construction_main ($) { Line 4506  sub _tree_construction_main ($) {
4506                $script_start_tag->();                $script_start_tag->();
4507                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4508                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4509                redo B;                next B;
4510              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
4511                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
4512                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4513                  !!!cp ('t122');                  !!!cp ('t122');
4514                  ## As if </noscript>                  ## As if </noscript>
4515                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4516                  !!!parse-error (type => 'in noscript:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'in noscript',
4517                                    text => $token->{tag_name}, token => $token);
4518                                    
4519                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4520                  ## As if </head>                  ## As if </head>
# Line 3707  sub _tree_construction_main ($) { Line 4541  sub _tree_construction_main ($) {
4541                } else {                } else {
4542                  die "$0: tag name: $self->{tag_name}";                  die "$0: tag name: $self->{tag_name}";
4543                }                }
4544                  !!!nack ('t127.1');
4545                !!!next-token;                !!!next-token;
4546                redo B;                next B;
4547              } else {              } else {
4548                !!!cp ('t128');                !!!cp ('t128');
4549                #                #
# Line 3718  sub _tree_construction_main ($) { Line 4553  sub _tree_construction_main ($) {
4553                !!!cp ('t129');                !!!cp ('t129');
4554                ## As if </noscript>                ## As if </noscript>
4555                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4556                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'in noscript:/',
4557                                  text => $token->{tag_name}, token => $token);
4558                                
4559                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4560                ## As if </head>                ## As if </head>
# Line 3740  sub _tree_construction_main ($) { Line 4576  sub _tree_construction_main ($) {
4576              !!!insert-element ('body',, $token);              !!!insert-element ('body',, $token);
4577              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4578              ## reprocess              ## reprocess
4579              redo B;              !!!ack-later;
4580                next B;
4581            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
4582              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
4583                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4584                  !!!cp ('t132');                  !!!cp ('t132');
4585                  ## As if <head>                  ## As if <head>
4586                  !!!create-element ($self->{head_element}, 'head',, $token);                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4587                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4588                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4589                        [$self->{head_element}, $el_category->{head}];
4590    
4591                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4592                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4593                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4594                  !!!next-token;                  !!!next-token;
4595                  redo B;                  next B;
4596                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4597                  !!!cp ('t133');                  !!!cp ('t133');
4598                  ## As if </noscript>                  ## As if </noscript>
4599                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4600                  !!!parse-error (type => 'in noscript:/head', token => $token);                  !!!parse-error (type => 'in noscript:/',
4601                                    text => 'head', token => $token);
4602                                    
4603                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4604                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4605                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4606                  !!!next-token;                  !!!next-token;
4607                  redo B;                  next B;
4608                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4609                  !!!cp ('t134');                  !!!cp ('t134');
4610                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4611                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4612                  !!!next-token;                  !!!next-token;
4613                  redo B;                  next B;
4614                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4615                    !!!cp ('t134.1');
4616                    !!!parse-error (type => 'unmatched end tag', text => 'head',
4617                                    token => $token);
4618                    ## Ignore the token
4619                    !!!next-token;
4620                    next B;
4621                } else {                } else {
4622                  !!!cp ('t135');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
                 #  
4623                }                }
4624              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4625                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
# Line 3782  sub _tree_construction_main ($) { Line 4627  sub _tree_construction_main ($) {
4627                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4628                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4629                  !!!next-token;                  !!!next-token;
4630                  redo B;                  next B;
4631                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
4632                           $self->{insertion_mode} == AFTER_HEAD_IM) {
4633                  !!!cp ('t137');                  !!!cp ('t137');
4634                  !!!parse-error (type => 'unmatched end tag:noscript', token => $token);                  !!!parse-error (type => 'unmatched end tag',
4635                                    text => 'noscript', token => $token);
4636                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
4637                  !!!next-token;                  !!!next-token;
4638                  redo B;                  next B;
4639                } else {                } else {
4640                  !!!cp ('t138');                  !!!cp ('t138');
4641                  #                  #
# Line 3796  sub _tree_construction_main ($) { Line 4643  sub _tree_construction_main ($) {
4643              } elsif ({              } elsif ({
4644                        body => 1, html => 1,                        body => 1, html => 1,
4645                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4646                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM or
4647                  !!!cp ('t139');                    $self->{insertion_mode} == IN_HEAD_IM or
4648                  ## 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) {  
4649                  !!!cp ('t140');                  !!!cp ('t140');
4650                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
4651                                    text => $token->{tag_name}, token => $token);
4652                  ## Ignore the token                  ## Ignore the token
4653                  !!!next-token;                  !!!next-token;
4654                  redo B;                  next B;
4655                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4656                    !!!cp ('t140.1');
4657                    !!!parse-error (type => 'unmatched end tag',
4658                                    text => $token->{tag_name}, token => $token);
4659                    ## Ignore the token
4660                    !!!next-token;
4661                    next B;
4662                } else {                } else {
4663                  !!!cp ('t141');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
4664                }                }
4665                              } elsif ($token->{tag_name} eq 'p') {
4666                #                !!!cp ('t142');
4667              } elsif ({                !!!parse-error (type => 'unmatched end tag',
4668                        p => 1, br => 1,                                text => $token->{tag_name}, token => $token);
4669                       }->{$token->{tag_name}}) {                ## Ignore the token
4670                  !!!next-token;
4671                  next B;
4672                } elsif ($token->{tag_name} eq 'br') {
4673                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4674                  !!!cp ('t142');                  !!!cp ('t142.2');
4675                  ## As if <head>                  ## (before head) as if <head>, (in head) as if </head>
4676                  !!!create-element ($self->{head_element}, 'head',, $token);                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4677                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4678                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  $self->{insertion_mode} = AFTER_HEAD_IM;
4679      
4680                    ## Reprocess in the "after head" insertion mode...
4681                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4682                    !!!cp ('t143.2');
4683                    ## As if </head>
4684                    pop @{$self->{open_elements}};
4685                    $self->{insertion_mode} = AFTER_HEAD_IM;
4686      
4687                    ## Reprocess in the "after head" insertion mode...
4688                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4689                    !!!cp ('t143.3');
4690                    ## ISSUE: Two parse errors for <head><noscript></br>
4691                    !!!parse-error (type => 'unmatched end tag',
4692                                    text => 'br', token => $token);
4693                    ## As if </noscript>
4694                    pop @{$self->{open_elements}};
4695                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4696    
4697                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4698                } else {                  ## As if </head>
4699                  !!!cp ('t143');                  pop @{$self->{open_elements}};
4700                }                  $self->{insertion_mode} = AFTER_HEAD_IM;
4701    
4702                #                  ## Reprocess in the "after head" insertion mode...
4703              } else {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4704                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                  !!!cp ('t143.4');
                 !!!cp ('t144');  
4705                  #                  #
4706                } else {                } else {
4707                  !!!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;  
4708                }                }
4709    
4710                  ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
4711                  !!!parse-error (type => 'unmatched end tag',
4712                                  text => 'br', token => $token);
4713                  ## Ignore the token
4714                  !!!next-token;
4715                  next B;
4716                } else {
4717                  !!!cp ('t145');
4718                  !!!parse-error (type => 'unmatched end tag',
4719                                  text => $token->{tag_name}, token => $token);
4720                  ## Ignore the token
4721                  !!!next-token;
4722                  next B;
4723              }              }
4724    
4725              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4726                !!!cp ('t146');                !!!cp ('t146');
4727                ## As if </noscript>                ## As if </noscript>
4728                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4729                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'in noscript:/',
4730                                  text => $token->{tag_name}, token => $token);
4731                                
4732                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4733                ## As if </head>                ## As if </head>
# Line 3866  sub _tree_construction_main ($) { Line 4743  sub _tree_construction_main ($) {
4743              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4744  ## ISSUE: This case cannot be reached?  ## ISSUE: This case cannot be reached?
4745                !!!cp ('t148');                !!!cp ('t148');
4746                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag',
4747                                  text => $token->{tag_name}, token => $token);
4748                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
4749                !!!next-token;                !!!next-token;
4750                redo B;                next B;
4751              } else {              } else {
4752                !!!cp ('t149');                !!!cp ('t149');
4753              }              }
# Line 3879  sub _tree_construction_main ($) { Line 4757  sub _tree_construction_main ($) {
4757              !!!insert-element ('body',, $token);              !!!insert-element ('body',, $token);
4758              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4759              ## reprocess              ## reprocess
4760              redo B;              next B;
4761        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4762          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4763            !!!cp ('t149.1');            !!!cp ('t149.1');
4764    
4765            ## NOTE: As if <head>            ## NOTE: As if <head>
4766            !!!create-element ($self->{head_element}, 'head',, $token);            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4767            $self->{open_elements}->[-1]->[0]->append_child            $self->{open_elements}->[-1]->[0]->append_child
4768                ($self->{head_element});                ($self->{head_element});
4769            #push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            #push @{$self->{open_elements}},
4770              #    [$self->{head_element}, $el_category->{head}];
4771            #$self->{insertion_mode} = IN_HEAD_IM;            #$self->{insertion_mode} = IN_HEAD_IM;
4772            ## NOTE: Reprocess.            ## NOTE: Reprocess.
4773    
# Line 3932  sub _tree_construction_main ($) { Line 4811  sub _tree_construction_main ($) {
4811          !!!insert-element ('body',, $token);          !!!insert-element ('body',, $token);
4812          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4813          ## NOTE: Reprocess.          ## NOTE: Reprocess.
4814          redo B;          next B;
4815        } else {        } else {
4816          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
4817        }        }
# Line 3947  sub _tree_construction_main ($) { Line 4826  sub _tree_construction_main ($) {
4826              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4827    
4828              !!!next-token;              !!!next-token;
4829              redo B;              next B;
4830            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
4831              if ({              if ({
4832                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
# Line 3957  sub _tree_construction_main ($) { Line 4836  sub _tree_construction_main ($) {
4836                  ## have an element in table scope                  ## have an element in table scope
4837                  for (reverse 0..$#{$self->{open_elements}}) {                  for (reverse 0..$#{$self->{open_elements}}) {
4838                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4839                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] & TABLE_CELL_EL) {
4840                      !!!cp ('t151');                      !!!cp ('t151');
4841    
4842                      ## Close the cell                      ## Close the cell
4843                      !!!back-token; # <?>                      !!!back-token; # <x>
4844                      $token = {type => END_TAG_TOKEN, tag_name => $node->[1],                      $token = {type => END_TAG_TOKEN,
4845                                  tag_name => $node->[0]->manakai_local_name,
4846                                line => $token->{line},                                line => $token->{line},
4847                                column => $token->{column}};                                column => $token->{column}};
4848                      redo B;                      next B;
4849                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
4850                      !!!cp ('t152');                      !!!cp ('t152');
4851                      ## ISSUE: This case can never be reached, maybe.                      ## ISSUE: This case can never be reached, maybe.
4852                      last;                      last;
# Line 3977  sub _tree_construction_main ($) { Line 4855  sub _tree_construction_main ($) {
4855    
4856                  !!!cp ('t153');                  !!!cp ('t153');
4857                  !!!parse-error (type => 'start tag not allowed',                  !!!parse-error (type => 'start tag not allowed',
4858                      value => $token->{tag_name}, token => $token);                      text => $token->{tag_name}, token => $token);
4859                  ## Ignore the token                  ## Ignore the token
4860                    !!!nack ('t153.1');
4861                  !!!next-token;                  !!!next-token;
4862                  redo B;                  next B;
4863                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4864                  !!!parse-error (type => 'not closed:caption', token => $token);                  !!!parse-error (type => 'not closed', text => 'caption',
4865                                    token => $token);
4866                                    
4867                  ## NOTE: As if </caption>.                  ## NOTE: As if </caption>.
4868                  ## have a table element in table scope                  ## have a table element in table scope
# Line 3990  sub _tree_construction_main ($) { Line 4870  sub _tree_construction_main ($) {
4870                  INSCOPE: {                  INSCOPE: {
4871                    for (reverse 0..$#{$self->{open_elements}}) {                    for (reverse 0..$#{$self->{open_elements}}) {
4872                      my $node = $self->{open_elements}->[$_];                      my $node = $self->{open_elements}->[$_];
4873                      if ($node->[1] eq 'caption') {                      if ($node->[1] & CAPTION_EL) {
4874                        !!!cp ('t155');                        !!!cp ('t155');
4875                        $i = $_;                        $i = $_;
4876                        last INSCOPE;                        last INSCOPE;
4877                      } elsif ({                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
                               table => 1, html => 1,  
                              }->{$node->[1]}) {  
4878                        !!!cp ('t156');                        !!!cp ('t156');
4879                        last;                        last;
4880                      }                      }
# Line 4004  sub _tree_construction_main ($) { Line 4882  sub _tree_construction_main ($) {
4882    
4883                    !!!cp ('t157');                    !!!cp ('t157');
4884                    !!!parse-error (type => 'start tag not allowed',                    !!!parse-error (type => 'start tag not allowed',
4885                                    value => $token->{tag_name}, token => $token);                                    text => $token->{tag_name}, token => $token);
4886                    ## Ignore the token                    ## Ignore the token
4887                      !!!nack ('t157.1');
4888                    !!!next-token;                    !!!next-token;
4889                    redo B;                    next B;
4890                  } # INSCOPE                  } # INSCOPE
4891                                    
4892                  ## generate implied end tags                  ## generate implied end tags
4893                  while ({                  while ($self->{open_elements}->[-1]->[1]
4894                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4895                    !!!cp ('t158');                    !!!cp ('t158');
4896                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4897                  }                  }
4898    
4899                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4900                    !!!cp ('t159');                    !!!cp ('t159');
4901                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                    !!!parse-error (type => 'not closed',
4902                                      text => $self->{open_elements}->[-1]->[0]
4903                                          ->manakai_local_name,
4904                                      token => $token);
4905                  } else {                  } else {
4906                    !!!cp ('t160');                    !!!cp ('t160');
4907                  }                  }
# Line 4032  sub _tree_construction_main ($) { Line 4913  sub _tree_construction_main ($) {
4913                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4914                                    
4915                  ## reprocess                  ## reprocess
4916                  redo B;                  !!!ack-later;
4917                    next B;
4918                } else {                } else {
4919                  !!!cp ('t161');                  !!!cp ('t161');
4920                  #                  #
# Line 4048  sub _tree_construction_main ($) { Line 4930  sub _tree_construction_main ($) {
4930                  my $i;                  my $i;
4931                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4932                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4933                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4934                      !!!cp ('t163');                      !!!cp ('t163');
4935                      $i = $_;                      $i = $_;
4936                      last INSCOPE;                      last INSCOPE;
4937                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
4938                      !!!cp ('t164');                      !!!cp ('t164');
4939                      last INSCOPE;                      last INSCOPE;
4940                    }                    }
4941                  } # INSCOPE                  } # INSCOPE
4942                    unless (defined $i) {                    unless (defined $i) {
4943                      !!!cp ('t165');                      !!!cp ('t165');
4944                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                      !!!parse-error (type => 'unmatched end tag',
4945                                        text => $token->{tag_name},
4946                                        token => $token);
4947                      ## Ignore the token                      ## Ignore the token
4948                      !!!next-token;                      !!!next-token;
4949                      redo B;                      next B;
4950                    }                    }
4951                                    
4952                  ## generate implied end tags                  ## generate implied end tags
4953                  while ({                  while ($self->{open_elements}->[-1]->[1]
4954                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4955                    !!!cp ('t166');                    !!!cp ('t166');
4956                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4957                  }                  }
4958    
4959                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
4960                            ne $token->{tag_name}) {
4961                    !!!cp ('t167');                    !!!cp ('t167');
4962                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                    !!!parse-error (type => 'not closed',
4963                                      text => $self->{open_elements}->[-1]->[0]
4964                                          ->manakai_local_name,
4965                                      token => $token);
4966                  } else {                  } else {
4967                    !!!cp ('t168');                    !!!cp ('t168');
4968                  }                  }
# Line 4089  sub _tree_construction_main ($) { Line 4974  sub _tree_construction_main ($) {
4974                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
4975                                    
4976                  !!!next-token;                  !!!next-token;
4977                  redo B;                  next B;
4978                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4979                  !!!cp ('t169');                  !!!cp ('t169');
4980                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
4981                                    text => $token->{tag_name}, token => $token);
4982                  ## Ignore the token                  ## Ignore the token
4983                  !!!next-token;                  !!!next-token;
4984                  redo B;                  next B;
4985                } else {                } else {
4986                  !!!cp ('t170');                  !!!cp ('t170');
4987                  #                  #
# Line 4107  sub _tree_construction_main ($) { Line 4993  sub _tree_construction_main ($) {
4993                  INSCOPE: {                  INSCOPE: {
4994                    for (reverse 0..$#{$self->{open_elements}}) {                    for (reverse 0..$#{$self->{open_elements}}) {
4995                      my $node = $self->{open_elements}->[$_];                      my $node = $self->{open_elements}->[$_];
4996                      if ($node->[1] eq $token->{tag_name}) {                      if ($node->[1] & CAPTION_EL) {
4997                        !!!cp ('t171');                        !!!cp ('t171');
4998                        $i = $_;                        $i = $_;
4999                        last INSCOPE;                        last INSCOPE;
5000                      } elsif ({                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
                               table => 1, html => 1,  
                              }->{$node->[1]}) {  
5001                        !!!cp ('t172');                        !!!cp ('t172');
5002                        last;                        last;
5003                      }                      }
# Line 4121  sub _tree_construction_main ($) { Line 5005  sub _tree_construction_main ($) {
5005    
5006                    !!!cp ('t173');                    !!!cp ('t173');
5007                    !!!parse-error (type => 'unmatched end tag',                    !!!parse-error (type => 'unmatched end tag',
5008                                    value => $token->{tag_name}, token => $token);                                    text => $token->{tag_name}, token => $token);
5009                    ## Ignore the token                    ## Ignore the token
5010                    !!!next-token;                    !!!next-token;
5011                    redo B;                    next B;
5012                  } # INSCOPE                  } # INSCOPE
5013                                    
5014                  ## generate implied end tags                  ## generate implied end tags
5015                  while ({                  while ($self->{open_elements}->[-1]->[1]
5016                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
5017                    !!!cp ('t174');                    !!!cp ('t174');
5018                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5019                  }                  }
5020                                    
5021                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5022                    !!!cp ('t175');                    !!!cp ('t175');
5023                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                    !!!parse-error (type => 'not closed',
5024                                      text => $self->{open_elements}->[-1]->[0]
5025                                          ->manakai_local_name,
5026                                      token => $token);
5027                  } else {                  } else {
5028                    !!!cp ('t176');                    !!!cp ('t176');
5029                  }                  }
# Line 4149  sub _tree_construction_main ($) { Line 5035  sub _tree_construction_main ($) {
5035                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5036                                    
5037                  !!!next-token;                  !!!next-token;
5038                  redo B;                  next B;
5039                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
5040                  !!!cp ('t177');                  !!!cp ('t177');
5041                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
5042                                    text => $token->{tag_name}, token => $token);
5043                  ## Ignore the token                  ## Ignore the token
5044                  !!!next-token;                  !!!next-token;
5045                  redo B;                  next B;
5046                } else {                } else {
5047                  !!!cp ('t178');                  !!!cp ('t178');
5048                  #                  #
# Line 4171  sub _tree_construction_main ($) { Line 5058  sub _tree_construction_main ($) {
5058                INSCOPE: {                INSCOPE: {
5059                  for (reverse 0..$#{$self->{open_elements}}) {                  for (reverse 0..$#{$self->{open_elements}}) {
5060                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5061                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5062                      !!!cp ('t179');                      !!!cp ('t179');
5063                      $i = $_;                      $i = $_;
5064    
5065                      ## Close the cell                      ## Close the cell
5066                      !!!back-token; # </?>                      !!!back-token; # </x>
5067                      $token = {type => END_TAG_TOKEN, tag_name => $tn,                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
5068                                line => $token->{line},                                line => $token->{line},
5069                                column => $token->{column}};                                column => $token->{column}};
5070                      redo B;                      next B;
5071                    } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {                    } elsif ($node->[1] & TABLE_CELL_EL) {
5072                      !!!cp ('t180');                      !!!cp ('t180');
5073                      $tn = $node->[1];                      $tn = $node->[0]->manakai_local_name;
5074                      ## NOTE: There is exactly one |td| or |th| element                      ## NOTE: There is exactly one |td| or |th| element
5075                      ## in scope in the stack of open elements by definition.                      ## in scope in the stack of open elements by definition.
5076                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5077                      ## ISSUE: Can this be reached?                      ## ISSUE: Can this be reached?
5078                      !!!cp ('t181');                      !!!cp ('t181');
5079                      last;                      last;
# Line 4197  sub _tree_construction_main ($) { Line 5082  sub _tree_construction_main ($) {
5082    
5083                  !!!cp ('t182');                  !!!cp ('t182');
5084                  !!!parse-error (type => 'unmatched end tag',                  !!!parse-error (type => 'unmatched end tag',
5085                      value => $token->{tag_name}, token => $token);                      text => $token->{tag_name}, token => $token);
5086                  ## Ignore the token                  ## Ignore the token
5087                  !!!next-token;                  !!!next-token;
5088                  redo B;                  next B;
5089                } # INSCOPE                } # INSCOPE
5090              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
5091                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5092                !!!parse-error (type => 'not closed:caption', token => $token);                !!!parse-error (type => 'not closed', text => 'caption',
5093                                  token => $token);
5094    
5095                ## As if </caption>                ## As if </caption>
5096                ## have a table element in table scope                ## have a table element in table scope
5097                my $i;                my $i;
5098                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5099                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5100                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
5101                    !!!cp ('t184');                    !!!cp ('t184');
5102                    $i = $_;                    $i = $_;
5103                    last INSCOPE;                    last INSCOPE;
5104                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5105                    !!!cp ('t185');                    !!!cp ('t185');
5106                    last INSCOPE;                    last INSCOPE;
5107                  }                  }
5108                } # INSCOPE                } # INSCOPE
5109                unless (defined $i) {                unless (defined $i) {
5110                  !!!cp ('t186');                  !!!cp ('t186');
5111                  !!!parse-error (type => 'unmatched end tag:caption', token => $token);                  !!!parse-error (type => 'unmatched end tag',
5112                                    text => 'caption', token => $token);
5113                  ## Ignore the token                  ## Ignore the token
5114                  !!!next-token;                  !!!next-token;
5115                  redo B;                  next B;
5116                }                }
5117                                
5118                ## generate implied end tags                ## generate implied end tags
5119                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
5120                  !!!cp ('t187');                  !!!cp ('t187');
5121                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5122                }                }
5123    
5124                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5125                  !!!cp ('t188');                  !!!cp ('t188');
5126                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                  !!!parse-error (type => 'not closed',
5127                                    text => $self->{open_elements}->[-1]->[0]
5128                                        ->manakai_local_name,
5129                                    token => $token);
5130                } else {                } else {
5131                  !!!cp ('t189');                  !!!cp ('t189');
5132                }                }
# Line 4252  sub _tree_construction_main ($) { Line 5138  sub _tree_construction_main ($) {
5138                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5139    
5140                ## reprocess                ## reprocess
5141                redo B;                next B;
5142              } elsif ({              } elsif ({
5143                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
5144                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5145                if ($self->{insertion_mode} & BODY_TABLE_IMS) {                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
5146                  !!!cp ('t190');                  !!!cp ('t190');
5147                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
5148                                    text => $token->{tag_name}, token => $token);
5149                  ## Ignore the token                  ## Ignore the token
5150                  !!!next-token;                  !!!next-token;
5151                  redo B;                  next B;
5152                } else {                } else {
5153                  !!!cp ('t191');                  !!!cp ('t191');
5154                  #                  #
# Line 4272  sub _tree_construction_main ($) { Line 5159  sub _tree_construction_main ($) {
5159                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
5160                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5161                !!!cp ('t192');                !!!cp ('t192');
5162                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag',
5163                                  text => $token->{tag_name}, token => $token);
5164                ## Ignore the token                ## Ignore the token
5165                !!!next-token;                !!!next-token;
5166                redo B;                next B;
5167              } else {              } else {
5168                !!!cp ('t193');                !!!cp ('t193');
5169                #                #
5170              }              }
5171        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5172          for my $entry (@{$self->{open_elements}}) {          for my $entry (@{$self->{open_elements}}) {
5173            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]}) {  
5174              !!!cp ('t75');              !!!cp ('t75');
5175              !!!parse-error (type => 'in body:#eof', token => $token);              !!!parse-error (type => 'in body:#eof', token => $token);
5176              last;              last;
# Line 4309  sub _tree_construction_main ($) { Line 5194  sub _tree_construction_main ($) {
5194            unless (length $token->{data}) {            unless (length $token->{data}) {
5195              !!!cp ('t194');              !!!cp ('t194');
5196              !!!next-token;              !!!next-token;
5197              redo B;              next B;
5198            } else {            } else {
5199              !!!cp ('t195');              !!!cp ('t195');
5200            }            }
5201          }          }
5202    
5203              !!!parse-error (type => 'in table:#character', token => $token);          !!!parse-error (type => 'in table:#text', token => $token);
5204    
5205              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
5206              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
# Line 4323  sub _tree_construction_main ($) { Line 5208  sub _tree_construction_main ($) {
5208              ## result in a new Text node.              ## result in a new Text node.
5209              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
5210                            
5211              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]}) {  
5212                # MUST                # MUST
5213                my $foster_parent_element;                my $foster_parent_element;
5214                my $next_sibling;                my $next_sibling;
5215                my $prev_sibling;                my $prev_sibling;
5216                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
5217                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
5218                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
5219                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
5220                      !!!cp ('t196');                      !!!cp ('t196');
# Line 4367  sub _tree_construction_main ($) { Line 5249  sub _tree_construction_main ($) {
5249          }          }
5250                            
5251          !!!next-token;          !!!next-token;
5252          redo B;          next B;
5253        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5254              if ({          if ({
5255                   tr => ($self->{insertion_mode} != IN_ROW_IM),               tr => ($self->{insertion_mode} != IN_ROW_IM),
5256                   th => 1, td => 1,               th => 1, td => 1,
5257                  }->{$token->{tag_name}}) {              }->{$token->{tag_name}}) {
5258                if ($self->{insertion_mode} == IN_TABLE_IM) {            if ($self->{insertion_mode} == IN_TABLE_IM) {
5259                  ## Clear back to table context              ## Clear back to table context
5260                  while ($self->{open_elements}->[-1]->[1] ne 'table' and              while (not ($self->{open_elements}->[-1]->[1]
5261                         $self->{open_elements}->[-1]->[1] ne 'html') {                              & TABLE_SCOPING_EL)) {
5262                    !!!cp ('t201');                !!!cp ('t201');
5263                    pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5264                  }              }
5265                                
5266                  !!!insert-element ('tbody',, $token);              !!!insert-element ('tbody',, $token);
5267                  $self->{insertion_mode} = IN_TABLE_BODY_IM;              $self->{insertion_mode} = IN_TABLE_BODY_IM;
5268                  ## reprocess in the "in table body" insertion mode...              ## reprocess in the "in table body" insertion mode...
5269                }            }
5270              
5271                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {            if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5272                  unless ($token->{tag_name} eq 'tr') {              unless ($token->{tag_name} eq 'tr') {
5273                    !!!cp ('t202');                !!!cp ('t202');
5274                    !!!parse-error (type => 'missing start tag:tr', token => $token);                !!!parse-error (type => 'missing start tag:tr', token => $token);
5275                  }              }
5276                                    
5277                  ## Clear back to table body context              ## Clear back to table body context
5278                  while (not {              while (not ($self->{open_elements}->[-1]->[1]
5279                    tbody => 1, tfoot => 1, thead => 1, html => 1,                              & TABLE_ROWS_SCOPING_EL)) {
5280                  }->{$self->{open_elements}->[-1]->[1]}) {                !!!cp ('t203');
5281                    !!!cp ('t203');                ## ISSUE: Can this case be reached?
5282                    ## ISSUE: Can this case be reached?                pop @{$self->{open_elements}};
5283                    pop @{$self->{open_elements}};              }
                 }  
5284                                    
5285                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
5286                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5287                    !!!cp ('t204');                    !!!cp ('t204');
5288                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5289                      !!!nack ('t204');
5290                    !!!next-token;                    !!!next-token;
5291                    redo B;                    next B;
5292                  } else {                  } else {
5293                    !!!cp ('t205');                    !!!cp ('t205');
5294                    !!!insert-element ('tr',, $token);                    !!!insert-element ('tr',, $token);
# Line 4417  sub _tree_construction_main ($) { Line 5299  sub _tree_construction_main ($) {
5299                }                }
5300    
5301                ## Clear back to table row context                ## Clear back to table row context
5302                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5303                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5304                  !!!cp ('t207');                  !!!cp ('t207');
5305                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5306                }                }
# Line 4429  sub _tree_construction_main ($) { Line 5310  sub _tree_construction_main ($) {
5310    
5311                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
5312                                
5313                  !!!nack ('t207.1');
5314                !!!next-token;                !!!next-token;
5315                redo B;                next B;
5316              } elsif ({              } elsif ({
5317                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5318                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
# Line 4442  sub _tree_construction_main ($) { Line 5324  sub _tree_construction_main ($) {
5324                  my $i;                  my $i;
5325                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5326                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5327                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5328                      !!!cp ('t208');                      !!!cp ('t208');
5329                      $i = $_;                      $i = $_;
5330                      last INSCOPE;                      last INSCOPE;
5331                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             html => 1,  
   
                             ## NOTE: This element does not appear here, maybe.  
                             table => 1,  
                            }->{$node->[1]}) {  
5332                      !!!cp ('t209');                      !!!cp ('t209');
5333                      last INSCOPE;                      last INSCOPE;
5334                    }                    }
5335                  } # INSCOPE                  } # INSCOPE
5336                  unless (defined $i) {                  unless (defined $i) {
5337                   !!!cp ('t210');                    !!!cp ('t210');
5338  ## TODO: This type is wrong.  ## TODO: This type is wrong.
5339                   !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);                    !!!parse-error (type => 'unmacthed end tag',
5340                                      text => $token->{tag_name}, token => $token);
5341                    ## Ignore the token                    ## Ignore the token
5342                      !!!nack ('t210.1');
5343                    !!!next-token;                    !!!next-token;
5344                    redo B;                    next B;
5345                  }                  }
5346                                    
5347                  ## Clear back to table row context                  ## Clear back to table row context
5348                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5349                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5350                    !!!cp ('t211');                    !!!cp ('t211');
5351                    ## ISSUE: Can this case be reached?                    ## ISSUE: Can this case be reached?
5352                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4479  sub _tree_construction_main ($) { Line 5357  sub _tree_construction_main ($) {
5357                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5358                    !!!cp ('t212');                    !!!cp ('t212');
5359                    ## reprocess                    ## reprocess
5360                    redo B;                    !!!ack-later;
5361                      next B;
5362                  } else {                  } else {
5363                    !!!cp ('t213');                    !!!cp ('t213');
5364                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
# Line 4491  sub _tree_construction_main ($) { Line 5370  sub _tree_construction_main ($) {
5370                  my $i;                  my $i;
5371                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5372                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5373                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5374                      !!!cp ('t214');                      !!!cp ('t214');
5375                      $i = $_;                      $i = $_;
5376                      last INSCOPE;                      last INSCOPE;
5377                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5378                      !!!cp ('t215');                      !!!cp ('t215');
5379                      last INSCOPE;                      last INSCOPE;
5380                    }                    }
5381                  } # INSCOPE                  } # INSCOPE
5382                  unless (defined $i) {                  unless (defined $i) {
5383                    !!!cp ('t216');                    !!!cp ('t216');
5384  ## TODO: This erorr type ios wrong.  ## TODO: This erorr type is wrong.
5385                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                    !!!parse-error (type => 'unmatched end tag',
5386                                      text => $token->{tag_name}, token => $token);
5387                    ## Ignore the token                    ## Ignore the token
5388                      !!!nack ('t216.1');
5389                    !!!next-token;                    !!!next-token;
5390                    redo B;                    next B;
5391                  }                  }
5392    
5393                  ## Clear back to table body context                  ## Clear back to table body context
5394                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5395                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5396                    !!!cp ('t217');                    !!!cp ('t217');
5397                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5398                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4538  sub _tree_construction_main ($) { Line 5414  sub _tree_construction_main ($) {
5414    
5415                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
5416                  ## Clear back to table context                  ## Clear back to table context
5417                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5418                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5419                    !!!cp ('t219');                    !!!cp ('t219');
5420                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5421                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4548  sub _tree_construction_main ($) { Line 5424  sub _tree_construction_main ($) {
5424                  !!!insert-element ('colgroup',, $token);                  !!!insert-element ('colgroup',, $token);
5425                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5426                  ## reprocess                  ## reprocess
5427                  redo B;                  !!!ack-later;
5428                    next B;
5429                } elsif ({                } elsif ({
5430                          caption => 1,                          caption => 1,
5431                          colgroup => 1,                          colgroup => 1,
5432                          tbody => 1, tfoot => 1, thead => 1,                          tbody => 1, tfoot => 1, thead => 1,
5433                         }->{$token->{tag_name}}) {                         }->{$token->{tag_name}}) {
5434                  ## Clear back to table context                  ## Clear back to table context
5435                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5436                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5437                    !!!cp ('t220');                    !!!cp ('t220');
5438                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5439                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4574  sub _tree_construction_main ($) { Line 5451  sub _tree_construction_main ($) {
5451                                             thead => IN_TABLE_BODY_IM,                                             thead => IN_TABLE_BODY_IM,
5452                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
5453                  !!!next-token;                  !!!next-token;
5454                  redo B;                  !!!nack ('t220.1');
5455                    next B;
5456                } else {                } else {
5457                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
5458                }                }
5459              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5460                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                !!!parse-error (type => 'not closed',
5461                                  text => $self->{open_elements}->[-1]->[0]
5462                                      ->manakai_local_name,
5463                                  token => $token);
5464    
5465                ## As if </table>                ## As if </table>
5466                ## have a table element in table scope                ## have a table element in table scope
5467                my $i;                my $i;
5468                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5469                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5470                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5471                    !!!cp ('t221');                    !!!cp ('t221');
5472                    $i = $_;                    $i = $_;
5473                    last INSCOPE;                    last INSCOPE;
5474                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           #table => 1,  
                           html => 1,  
                          }->{$node->[1]}) {  
5475                    !!!cp ('t222');                    !!!cp ('t222');
5476                    last INSCOPE;                    last INSCOPE;
5477                  }                  }
# Line 4601  sub _tree_construction_main ($) { Line 5479  sub _tree_construction_main ($) {
5479                unless (defined $i) {                unless (defined $i) {
5480                  !!!cp ('t223');                  !!!cp ('t223');
5481  ## TODO: The following is wrong, maybe.  ## TODO: The following is wrong, maybe.
5482                  !!!parse-error (type => 'unmatched end tag:table', token => $token);                  !!!parse-error (type => 'unmatched end tag', text => 'table',
5483                                    token => $token);
5484                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5485                    !!!nack ('t223.1');
5486                  !!!next-token;                  !!!next-token;
5487                  redo B;                  next B;
5488                }                }
5489                                
5490  ## TODO: Followings are removed from the latest spec.  ## TODO: Followings are removed from the latest spec.
5491                ## generate implied end tags                ## generate implied end tags
5492                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
5493                  !!!cp ('t224');                  !!!cp ('t224');
5494                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5495                }                }
5496    
5497                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5498                  !!!cp ('t225');                  !!!cp ('t225');
5499  ## ISSUE: Can this case be reached?                  ## NOTE: |<table><tr><table>|
5500                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                  !!!parse-error (type => 'not closed',
5501                                    text => $self->{open_elements}->[-1]->[0]
5502                                        ->manakai_local_name,
5503                                    token => $token);
5504                } else {                } else {
5505                  !!!cp ('t226');                  !!!cp ('t226');
5506                }                }
# Line 4629  sub _tree_construction_main ($) { Line 5510  sub _tree_construction_main ($) {
5510    
5511                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5512    
5513                ## reprocess            ## reprocess
5514                redo B;            !!!ack-later;
5515              next B;
5516          } elsif ($token->{tag_name} eq 'style') {          } elsif ($token->{tag_name} eq 'style') {
5517            if (not $open_tables->[-1]->[1]) { # tainted            if (not $open_tables->[-1]->[1]) { # tainted
5518              !!!cp ('t227.8');              !!!cp ('t227.8');
5519              ## NOTE: This is a "as if in head" code clone.              ## NOTE: This is a "as if in head" code clone.
5520              $parse_rcdata->(CDATA_CONTENT_MODEL);              $parse_rcdata->(CDATA_CONTENT_MODEL);
5521              redo B;              next B;
5522            } else {            } else {
5523              !!!cp ('t227.7');              !!!cp ('t227.7');
5524              #              #
# Line 4646  sub _tree_construction_main ($) { Line 5528  sub _tree_construction_main ($) {
5528              !!!cp ('t227.6');              !!!cp ('t227.6');
5529              ## NOTE: This is a "as if in head" code clone.              ## NOTE: This is a "as if in head" code clone.
5530              $script_start_tag->();              $script_start_tag->();
5531              redo B;              next B;
5532            } else {            } else {
5533              !!!cp ('t227.5');              !!!cp ('t227.5');
5534              #              #
# Line 4657  sub _tree_construction_main ($) { Line 5539  sub _tree_construction_main ($) {
5539                my $type = lc $token->{attributes}->{type}->{value};                my $type = lc $token->{attributes}->{type}->{value};
5540                if ($type eq 'hidden') {                if ($type eq 'hidden') {
5541                  !!!cp ('t227.3');                  !!!cp ('t227.3');
5542                  !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'in table',
5543                                    text => $token->{tag_name}, token => $token);
5544    
5545                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5546    
# Line 4666  sub _tree_construction_main ($) { Line 5549  sub _tree_construction_main ($) {
5549                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5550    
5551                  !!!next-token;                  !!!next-token;
5552                  redo B;                  !!!ack ('t227.2.1');
5553                    next B;
5554                } else {                } else {
5555                  !!!cp ('t227.2');                  !!!cp ('t227.2');
5556                  #                  #
# Line 4684  sub _tree_construction_main ($) { Line 5568  sub _tree_construction_main ($) {
5568            #            #
5569          }          }
5570    
5571          !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'in table', text => $token->{tag_name},
5572                            token => $token);
5573    
5574          $insert = $insert_to_foster;          $insert = $insert_to_foster;
5575          #          #
# Line 4695  sub _tree_construction_main ($) { Line 5580  sub _tree_construction_main ($) {
5580                my $i;                my $i;
5581                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5582                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5583                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_ROW_EL) {
5584                    !!!cp ('t228');                    !!!cp ('t228');
5585                    $i = $_;                    $i = $_;
5586                    last INSCOPE;                    last INSCOPE;
5587                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5588                    !!!cp ('t229');                    !!!cp ('t229');
5589                    last INSCOPE;                    last INSCOPE;
5590                  }                  }
5591                } # INSCOPE                } # INSCOPE
5592                unless (defined $i) {                unless (defined $i) {
5593                  !!!cp ('t230');                  !!!cp ('t230');
5594                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
5595                                    text => $token->{tag_name}, token => $token);
5596                  ## Ignore the token                  ## Ignore the token
5597                    !!!nack ('t230.1');
5598                  !!!next-token;                  !!!next-token;
5599                  redo B;                  next B;
5600                } else {                } else {
5601                  !!!cp ('t232');                  !!!cp ('t232');
5602                }                }
5603    
5604                ## Clear back to table row context                ## Clear back to table row context
5605                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5606                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5607                  !!!cp ('t231');                  !!!cp ('t231');
5608  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
5609                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 4728  sub _tree_construction_main ($) { Line 5612  sub _tree_construction_main ($) {
5612                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5613                $self->{insertion_mode} = IN_TABLE_BODY_IM;                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5614                !!!next-token;                !!!next-token;
5615                redo B;                !!!nack ('t231.1');
5616                  next B;
5617              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5618                if ($self->{insertion_mode} == IN_ROW_IM) {                if ($self->{insertion_mode} == IN_ROW_IM) {
5619                  ## As if </tr>                  ## As if </tr>
# Line 4736  sub _tree_construction_main ($) { Line 5621  sub _tree_construction_main ($) {
5621                  my $i;                  my $i;
5622                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5623                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5624                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5625                      !!!cp ('t233');                      !!!cp ('t233');
5626                      $i = $_;                      $i = $_;
5627                      last INSCOPE;                      last INSCOPE;
5628                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5629                      !!!cp ('t234');                      !!!cp ('t234');
5630                      last INSCOPE;                      last INSCOPE;
5631                    }                    }
# Line 4750  sub _tree_construction_main ($) { Line 5633  sub _tree_construction_main ($) {
5633                  unless (defined $i) {                  unless (defined $i) {
5634                    !!!cp ('t235');                    !!!cp ('t235');
5635  ## TODO: The following is wrong.  ## TODO: The following is wrong.
5636                    !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);                    !!!parse-error (type => 'unmatched end tag',
5637                                      text => $token->{type}, token => $token);
5638                    ## Ignore the token                    ## Ignore the token
5639                      !!!nack ('t236.1');
5640                    !!!next-token;                    !!!next-token;
5641                    redo B;                    next B;
5642                  }                  }
5643                                    
5644                  ## Clear back to table row context                  ## Clear back to table row context
5645                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5646                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5647                    !!!cp ('t236');                    !!!cp ('t236');
5648  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
5649                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4775  sub _tree_construction_main ($) { Line 5659  sub _tree_construction_main ($) {
5659                  my $i;                  my $i;
5660                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5661                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5662                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5663                      !!!cp ('t237');                      !!!cp ('t237');
5664                      $i = $_;                      $i = $_;
5665                      last INSCOPE;                      last INSCOPE;
5666                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5667                      !!!cp ('t238');                      !!!cp ('t238');
5668                      last INSCOPE;                      last INSCOPE;
5669                    }                    }
5670                  } # INSCOPE                  } # INSCOPE
5671                  unless (defined $i) {                  unless (defined $i) {
5672                    !!!cp ('t239');                    !!!cp ('t239');
5673                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                    !!!parse-error (type => 'unmatched end tag',
5674                                      text => $token->{tag_name}, token => $token);
5675                    ## Ignore the token                    ## Ignore the token
5676                      !!!nack ('t239.1');
5677                    !!!next-token;                    !!!next-token;
5678                    redo B;                    next B;
5679                  }                  }
5680                                    
5681                  ## Clear back to table body context                  ## Clear back to table body context
5682                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5683                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5684                    !!!cp ('t240');                    !!!cp ('t240');
5685                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5686                  }                  }
# Line 4825  sub _tree_construction_main ($) { Line 5706  sub _tree_construction_main ($) {
5706                my $i;                my $i;
5707                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5708                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5709                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_EL) {
5710                    !!!cp ('t241');                    !!!cp ('t241');
5711                    $i = $_;                    $i = $_;
5712                    last INSCOPE;                    last INSCOPE;
5713                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5714                    !!!cp ('t242');                    !!!cp ('t242');
5715                    last INSCOPE;                    last INSCOPE;
5716                  }                  }
5717                } # INSCOPE                } # INSCOPE
5718                unless (defined $i) {                unless (defined $i) {
5719                  !!!cp ('t243');                  !!!cp ('t243');
5720                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
5721                                    text => $token->{tag_name}, token => $token);
5722                  ## Ignore the token                  ## Ignore the token
5723                    !!!nack ('t243.1');
5724                  !!!next-token;                  !!!next-token;
5725                  redo B;                  next B;
5726                }                }
5727                                    
5728                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
# Line 4850  sub _tree_construction_main ($) { Line 5731  sub _tree_construction_main ($) {
5731                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5732                                
5733                !!!next-token;                !!!next-token;
5734                redo B;                next B;
5735              } elsif ({              } elsif ({
5736                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5737                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
# Line 4860  sub _tree_construction_main ($) { Line 5741  sub _tree_construction_main ($) {
5741                  my $i;                  my $i;
5742                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5743                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5744                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5745                      !!!cp ('t247');                      !!!cp ('t247');
5746                      $i = $_;                      $i = $_;
5747                      last INSCOPE;                      last INSCOPE;
5748                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5749                      !!!cp ('t248');                      !!!cp ('t248');
5750                      last INSCOPE;                      last INSCOPE;
5751                    }                    }
5752                  } # INSCOPE                  } # INSCOPE
5753                    unless (defined $i) {                    unless (defined $i) {
5754                      !!!cp ('t249');                      !!!cp ('t249');
5755                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                      !!!parse-error (type => 'unmatched end tag',
5756                                        text => $token->{tag_name}, token => $token);
5757                      ## Ignore the token                      ## Ignore the token
5758                        !!!nack ('t249.1');
5759                      !!!next-token;                      !!!next-token;
5760                      redo B;                      next B;
5761                    }                    }
5762                                    
5763                  ## As if </tr>                  ## As if </tr>
# Line 4884  sub _tree_construction_main ($) { Line 5765  sub _tree_construction_main ($) {
5765                  my $i;                  my $i;
5766                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5767                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5768                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5769                      !!!cp ('t250');                      !!!cp ('t250');
5770                      $i = $_;                      $i = $_;
5771                      last INSCOPE;                      last INSCOPE;
5772                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5773                      !!!cp ('t251');                      !!!cp ('t251');
5774                      last INSCOPE;                      last INSCOPE;
5775                    }                    }
5776                  } # INSCOPE                  } # INSCOPE
5777                    unless (defined $i) {                    unless (defined $i) {
5778                      !!!cp ('t252');                      !!!cp ('t252');
5779                      !!!parse-error (type => 'unmatched end tag:tr', token => $token);                      !!!parse-error (type => 'unmatched end tag',
5780                                        text => 'tr', token => $token);
5781                      ## Ignore the token                      ## Ignore the token
5782                        !!!nack ('t252.1');
5783                      !!!next-token;                      !!!next-token;
5784                      redo B;                      next B;
5785                    }                    }
5786                                    
5787                  ## Clear back to table row context                  ## Clear back to table row context
5788                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5789                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5790                    !!!cp ('t253');                    !!!cp ('t253');
5791  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
5792                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4921  sub _tree_construction_main ($) { Line 5801  sub _tree_construction_main ($) {
5801                my $i;                my $i;
5802                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5803                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5804                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5805                    !!!cp ('t254');                    !!!cp ('t254');
5806                    $i = $_;                    $i = $_;
5807                    last INSCOPE;                    last INSCOPE;
5808                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5809                    !!!cp ('t255');                    !!!cp ('t255');
5810                    last INSCOPE;                    last INSCOPE;
5811                  }                  }
5812                } # INSCOPE                } # INSCOPE
5813                unless (defined $i) {                unless (defined $i) {
5814                  !!!cp ('t256');                  !!!cp ('t256');
5815                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
5816                                    text => $token->{tag_name}, token => $token);
5817                  ## Ignore the token                  ## Ignore the token
5818                    !!!nack ('t256.1');
5819                  !!!next-token;                  !!!next-token;
5820                  redo B;                  next B;
5821                }                }
5822    
5823                ## Clear back to table body context                ## Clear back to table body context
5824                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5825                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5826                  !!!cp ('t257');                  !!!cp ('t257');
5827  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
5828                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 4951  sub _tree_construction_main ($) { Line 5830  sub _tree_construction_main ($) {
5830    
5831                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5832                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5833                  !!!nack ('t257.1');
5834                !!!next-token;                !!!next-token;
5835                redo B;                next B;
5836              } elsif ({              } elsif ({
5837                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
5838                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
5839                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5840                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
5841                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5842                !!!cp ('t258');            !!!cp ('t258');
5843                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
5844                ## Ignore the token                            text => $token->{tag_name}, token => $token);
5845                !!!next-token;            ## Ignore the token
5846                redo B;            !!!nack ('t258.1');
5847               !!!next-token;
5848              next B;
5849          } else {          } else {
5850            !!!cp ('t259');            !!!cp ('t259');
5851            !!!parse-error (type => 'in table:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'in table:/',
5852                              text => $token->{tag_name}, token => $token);
5853    
5854            $insert = $insert_to_foster;            $insert = $insert_to_foster;
5855            #            #
5856          }          }
5857        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5858          unless ($self->{open_elements}->[-1]->[1] eq 'html' and          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5859                  @{$self->{open_elements}} == 1) { # redundant, maybe                  @{$self->{open_elements}} == 1) { # redundant, maybe
5860            !!!parse-error (type => 'in body:#eof', token => $token);            !!!parse-error (type => 'in body:#eof', token => $token);
5861            !!!cp ('t259.1');            !!!cp ('t259.1');
# Line 4994  sub _tree_construction_main ($) { Line 5877  sub _tree_construction_main ($) {
5877                unless (length $token->{data}) {                unless (length $token->{data}) {
5878                  !!!cp ('t260');                  !!!cp ('t260');
5879                  !!!next-token;                  !!!next-token;
5880                  redo B;                  next B;
5881                }                }
5882              }              }
5883                            
# Line 5005  sub _tree_construction_main ($) { Line 5888  sub _tree_construction_main ($) {
5888                !!!cp ('t262');                !!!cp ('t262');
5889                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5890                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5891                  !!!ack ('t262.1');
5892                !!!next-token;                !!!next-token;
5893                redo B;                next B;
5894              } else {              } else {
5895                !!!cp ('t263');                !!!cp ('t263');
5896                #                #
5897              }              }
5898            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
5899              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
5900                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5901                  !!!cp ('t264');                  !!!cp ('t264');
5902                  !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);                  !!!parse-error (type => 'unmatched end tag',
5903                                    text => 'colgroup', token => $token);
5904                  ## Ignore the token                  ## Ignore the token
5905                  !!!next-token;                  !!!next-token;
5906                  redo B;                  next B;
5907                } else {                } else {
5908                  !!!cp ('t265');                  !!!cp ('t265');
5909                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
5910                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5911                  !!!next-token;                  !!!next-token;
5912                  redo B;                              next B;            
5913                }                }
5914              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
5915                !!!cp ('t266');                !!!cp ('t266');
5916                !!!parse-error (type => 'unmatched end tag:col', token => $token);                !!!parse-error (type => 'unmatched end tag',
5917                                  text => 'col', token => $token);
5918                ## Ignore the token                ## Ignore the token
5919                !!!next-token;                !!!next-token;
5920                redo B;                next B;
5921              } else {              } else {
5922                !!!cp ('t267');                !!!cp ('t267');
5923                #                #
5924              }              }
5925        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5926          if ($self->{open_elements}->[-1]->[1] eq 'html' or          if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5927              @{$self->{open_elements}} == 1) { # redundant, maybe              @{$self->{open_elements}} == 1) { # redundant, maybe
5928            !!!cp ('t270.2');            !!!cp ('t270.2');
5929            ## Stop parsing.            ## Stop parsing.
# Line 5048  sub _tree_construction_main ($) { Line 5934  sub _tree_construction_main ($) {
5934            pop @{$self->{open_elements}}; # colgroup            pop @{$self->{open_elements}}; # colgroup
5935            $self->{insertion_mode} = IN_TABLE_IM;            $self->{insertion_mode} = IN_TABLE_IM;
5936            ## Reprocess.            ## Reprocess.
5937            redo B;            next B;
5938          }          }
5939        } else {        } else {
5940          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5941        }        }
5942    
5943            ## As if </colgroup>            ## As if </colgroup>
5944            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5945              !!!cp ('t269');              !!!cp ('t269');
5946  ## TODO: Wrong error type?  ## TODO: Wrong error type?
5947              !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);              !!!parse-error (type => 'unmatched end tag',
5948                                text => 'colgroup', token => $token);
5949              ## Ignore the token              ## Ignore the token
5950                !!!nack ('t269.1');
5951              !!!next-token;              !!!next-token;
5952              redo B;              next B;
5953            } else {            } else {
5954              !!!cp ('t270');              !!!cp ('t270');
5955              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
5956              $self->{insertion_mode} = IN_TABLE_IM;              $self->{insertion_mode} = IN_TABLE_IM;
5957                !!!ack-later;
5958              ## reprocess              ## reprocess
5959              redo B;              next B;
5960            }            }
5961      } elsif ($self->{insertion_mode} & SELECT_IMS) {      } elsif ($self->{insertion_mode} & SELECT_IMS) {
5962        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5963          !!!cp ('t271');          !!!cp ('t271');
5964          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5965          !!!next-token;          !!!next-token;
5966          redo B;          next B;
5967        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5968              if ($token->{tag_name} eq 'option') {          if ($token->{tag_name} eq 'option') {
5969                if ($self->{open_elements}->[-1]->[1] eq 'option') {            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5970                  !!!cp ('t272');              !!!cp ('t272');
5971                  ## As if </option>              ## As if </option>
5972                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5973                } else {            } else {
5974                  !!!cp ('t273');              !!!cp ('t273');
5975                }            }
5976    
5977                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5978                !!!next-token;            !!!nack ('t273.1');
5979                redo B;            !!!next-token;
5980              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
5981                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
5982                  !!!cp ('t274');            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5983                  ## As if </option>              !!!cp ('t274');
5984                  pop @{$self->{open_elements}};              ## As if </option>
5985                } else {              pop @{$self->{open_elements}};
5986                  !!!cp ('t275');            } else {
5987                }              !!!cp ('t275');
5988              }
5989    
5990                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5991                  !!!cp ('t276');              !!!cp ('t276');
5992                  ## As if </optgroup>              ## As if </optgroup>
5993                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5994                } else {            } else {
5995                  !!!cp ('t277');              !!!cp ('t277');
5996                }            }
5997    
5998                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5999                !!!next-token;            !!!nack ('t277.1');
6000                redo B;            !!!next-token;
6001          } elsif ($token->{tag_name} eq 'select' or            next B;
6002                   $token->{tag_name} eq 'input' or          } elsif ({
6003                       select => 1, input => 1, textarea => 1,
6004                     }->{$token->{tag_name}} or
6005                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6006                    {                    {
6007                     caption => 1, table => 1,                     caption => 1, table => 1,
# Line 5117  sub _tree_construction_main ($) { Line 6009  sub _tree_construction_main ($) {
6009                     tr => 1, td => 1, th => 1,                     tr => 1, td => 1, th => 1,
6010                    }->{$token->{tag_name}})) {                    }->{$token->{tag_name}})) {
6011            ## TODO: The type below is not good - <select> is replaced by </select>            ## TODO: The type below is not good - <select> is replaced by </select>
6012            !!!parse-error (type => 'not closed:select', token => $token);            !!!parse-error (type => 'not closed', text => 'select',
6013                              token => $token);
6014            ## NOTE: As if the token were </select> (<select> case) or            ## NOTE: As if the token were </select> (<select> case) or
6015            ## as if there were </select> (otherwise).            ## as if there were </select> (otherwise).
6016                ## have an element in table scope            ## have an element in table scope
6017                my $i;            my $i;
6018                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6019                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6020                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
6021                    !!!cp ('t278');                !!!cp ('t278');
6022                    $i = $_;                $i = $_;
6023                    last INSCOPE;                last INSCOPE;
6024                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6025                            table => 1, html => 1,                !!!cp ('t279');
6026                           }->{$node->[1]}) {                last INSCOPE;
6027                    !!!cp ('t279');              }
6028                    last INSCOPE;            } # INSCOPE
6029                  }            unless (defined $i) {
6030                } # INSCOPE              !!!cp ('t280');
6031                unless (defined $i) {              !!!parse-error (type => 'unmatched end tag',
6032                  !!!cp ('t280');                              text => 'select', token => $token);
6033                  !!!parse-error (type => 'unmatched end tag:select', token => $token);              ## Ignore the token
6034                  ## Ignore the token              !!!nack ('t280.1');
6035                  !!!next-token;              !!!next-token;
6036                  redo B;              next B;
6037                }            }
6038                                
6039                !!!cp ('t281');            !!!cp ('t281');
6040                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6041    
6042                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6043    
6044            if ($token->{tag_name} eq 'select') {            if ($token->{tag_name} eq 'select') {
6045              !!!cp ('t281.2');              !!!nack ('t281.2');
6046              !!!next-token;              !!!next-token;
6047              redo B;              next B;
6048            } else {            } else {
6049              !!!cp ('t281.1');              !!!cp ('t281.1');
6050                !!!ack-later;
6051              ## Reprocess the token.              ## Reprocess the token.
6052              redo B;              next B;
6053            }            }
6054          } else {          } else {
6055            !!!cp ('t282');            !!!cp ('t282');
6056            !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'in select',
6057                              text => $token->{tag_name}, token => $token);
6058            ## Ignore the token            ## Ignore the token
6059              !!!nack ('t282.1');
6060            !!!next-token;            !!!next-token;
6061            redo B;            next B;
6062          }          }
6063        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6064              if ($token->{tag_name} eq 'optgroup') {          if ($token->{tag_name} eq 'optgroup') {
6065                if ($self->{open_elements}->[-1]->[1] eq 'option' and            if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
6066                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
6067                  !!!cp ('t283');              !!!cp ('t283');
6068                  ## As if </option>              ## As if </option>
6069                  splice @{$self->{open_elements}}, -2;              splice @{$self->{open_elements}}, -2;
6070                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6071                  !!!cp ('t284');              !!!cp ('t284');
6072                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6073                } else {            } else {
6074                  !!!cp ('t285');              !!!cp ('t285');
6075                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag',
6076                  ## Ignore the token                              text => $token->{tag_name}, token => $token);
6077                }              ## Ignore the token
6078                !!!next-token;            }
6079                redo B;            !!!nack ('t285.1');
6080              } elsif ($token->{tag_name} eq 'option') {            !!!next-token;
6081                if ($self->{open_elements}->[-1]->[1] eq 'option') {            next B;
6082                  !!!cp ('t286');          } elsif ($token->{tag_name} eq 'option') {
6083                  pop @{$self->{open_elements}};            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6084                } else {              !!!cp ('t286');
6085                  !!!cp ('t287');              pop @{$self->{open_elements}};
6086                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            } else {
6087                  ## Ignore the token              !!!cp ('t287');
6088                }              !!!parse-error (type => 'unmatched end tag',
6089                !!!next-token;                              text => $token->{tag_name}, token => $token);
6090                redo B;              ## Ignore the token
6091              } elsif ($token->{tag_name} eq 'select') {            }
6092                ## have an element in table scope            !!!nack ('t287.1');
6093                my $i;            !!!next-token;
6094                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            next B;
6095                  my $node = $self->{open_elements}->[$_];          } elsif ($token->{tag_name} eq 'select') {
6096                  if ($node->[1] eq $token->{tag_name}) {            ## have an element in table scope
6097                    !!!cp ('t288');            my $i;
6098                    $i = $_;            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6099                    last INSCOPE;              my $node = $self->{open_elements}->[$_];
6100                  } elsif ({              if ($node->[1] & SELECT_EL) {
6101                            table => 1, html => 1,                !!!cp ('t288');
6102                           }->{$node->[1]}) {                $i = $_;
6103                    !!!cp ('t289');                last INSCOPE;
6104                    last INSCOPE;              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6105                  }                !!!cp ('t289');
6106                } # INSCOPE                last INSCOPE;
6107                unless (defined $i) {              }
6108                  !!!cp ('t290');            } # INSCOPE
6109                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            unless (defined $i) {
6110                  ## Ignore the token              !!!cp ('t290');
6111                  !!!next-token;              !!!parse-error (type => 'unmatched end tag',
6112                  redo B;                              text => $token->{tag_name}, token => $token);
6113                }              ## Ignore the token
6114                !!!nack ('t290.1');
6115                !!!next-token;
6116                next B;
6117              }
6118                                
6119                !!!cp ('t291');            !!!cp ('t291');
6120                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6121    
6122                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6123    
6124                !!!next-token;            !!!nack ('t291.1');
6125                redo B;            !!!next-token;
6126              next B;
6127          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6128                   {                   {
6129                    caption => 1, table => 1, tbody => 1,                    caption => 1, table => 1, tbody => 1,
6130                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
6131                   }->{$token->{tag_name}}) {                   }->{$token->{tag_name}}) {
6132  ## TODO: The following is wrong?  ## TODO: The following is wrong?
6133                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
6134                              text => $token->{tag_name}, token => $token);
6135                                
6136                ## have an element in table scope            ## have an element in table scope
6137                my $i;            my $i;
6138                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6139                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6140                  if ($node->[1] eq $token->{tag_name}) {              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6141                    !!!cp ('t292');                !!!cp ('t292');
6142                    $i = $_;                $i = $_;
6143                    last INSCOPE;                last INSCOPE;
6144                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6145                            table => 1, html => 1,                !!!cp ('t293');
6146                           }->{$node->[1]}) {                last INSCOPE;
6147                    !!!cp ('t293');              }
6148                    last INSCOPE;            } # INSCOPE
6149                  }            unless (defined $i) {
6150                } # INSCOPE              !!!cp ('t294');
6151                unless (defined $i) {              ## Ignore the token
6152                  !!!cp ('t294');              !!!nack ('t294.1');
6153                  ## Ignore the token              !!!next-token;
6154                  !!!next-token;              next B;
6155                  redo B;            }
               }  
6156                                
6157                ## As if </select>            ## As if </select>
6158                ## have an element in table scope            ## have an element in table scope
6159                undef $i;            undef $i;
6160                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6161                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6162                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
6163                    !!!cp ('t295');                !!!cp ('t295');
6164                    $i = $_;                $i = $_;
6165                    last INSCOPE;                last INSCOPE;
6166                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
6167  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
6168                    !!!cp ('t296');                !!!cp ('t296');
6169                    last INSCOPE;                last INSCOPE;
6170                  }              }
6171                } # INSCOPE            } # INSCOPE
6172                unless (defined $i) {            unless (defined $i) {
6173                  !!!cp ('t297');              !!!cp ('t297');
6174  ## TODO: The following error type is correct?  ## TODO: The following error type is correct?
6175                  !!!parse-error (type => 'unmatched end tag:select', token => $token);              !!!parse-error (type => 'unmatched end tag',
6176                  ## Ignore the </select> token                              text => 'select', token => $token);
6177                  !!!next-token; ## TODO: ok?              ## Ignore the </select> token
6178                  redo B;              !!!nack ('t297.1');
6179                }              !!!next-token; ## TODO: ok?
6180                next B;
6181              }
6182                                
6183                !!!cp ('t298');            !!!cp ('t298');
6184                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6185    
6186                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6187    
6188                ## reprocess            !!!ack-later;
6189                redo B;            ## reprocess
6190              next B;
6191          } else {          } else {
6192            !!!cp ('t299');            !!!cp ('t299');
6193            !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'in select:/',
6194                              text => $token->{tag_name}, token => $token);
6195            ## Ignore the token            ## Ignore the token
6196              !!!nack ('t299.3');
6197            !!!next-token;            !!!next-token;
6198            redo B;            next B;
6199          }          }
6200        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6201          unless ($self->{open_elements}->[-1]->[1] eq 'html' and          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6202                  @{$self->{open_elements}} == 1) { # redundant, maybe                  @{$self->{open_elements}} == 1) { # redundant, maybe
6203            !!!cp ('t299.1');            !!!cp ('t299.1');
6204            !!!parse-error (type => 'in body:#eof', token => $token);            !!!parse-error (type => 'in body:#eof', token => $token);
# Line 5319  sub _tree_construction_main ($) { Line 6223  sub _tree_construction_main ($) {
6223            unless (length $token->{data}) {            unless (length $token->{data}) {
6224              !!!cp ('t300');              !!!cp ('t300');
6225              !!!next-token;              !!!next-token;
6226              redo B;              next B;
6227            }            }
6228          }          }
6229                    
6230          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6231            !!!cp ('t301');            !!!cp ('t301');
6232            !!!parse-error (type => 'after html:#character', token => $token);            !!!parse-error (type => 'after html:#text', token => $token);
6233    
6234            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
6235          } else {          } else {
# Line 5333  sub _tree_construction_main ($) { Line 6237  sub _tree_construction_main ($) {
6237          }          }
6238                    
6239          ## "after body" insertion mode          ## "after body" insertion mode
6240          !!!parse-error (type => 'after body:#character', token => $token);          !!!parse-error (type => 'after body:#text', token => $token);
6241    
6242          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6243          ## reprocess          ## reprocess
6244          redo B;          next B;
6245        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6246          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6247            !!!cp ('t303');            !!!cp ('t303');
6248            !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'after html',
6249                              text => $token->{tag_name}, token => $token);
6250                        
6251            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
6252          } else {          } else {
# Line 5349  sub _tree_construction_main ($) { Line 6254  sub _tree_construction_main ($) {
6254          }          }
6255    
6256          ## "after body" insertion mode          ## "after body" insertion mode
6257          !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'after body',
6258                            text => $token->{tag_name}, token => $token);
6259    
6260          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6261            !!!ack-later;
6262          ## reprocess          ## reprocess
6263          redo B;          next B;
6264        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6265          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6266            !!!cp ('t305');            !!!cp ('t305');
6267            !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'after html:/',
6268                              text => $token->{tag_name}, token => $token);
6269                        
6270            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6271            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
# Line 5369  sub _tree_construction_main ($) { Line 6277  sub _tree_construction_main ($) {
6277          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
6278            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
6279              !!!cp ('t307');              !!!cp ('t307');
6280              !!!parse-error (type => 'unmatched end tag:html', token => $token);              !!!parse-error (type => 'unmatched end tag',
6281                                text => 'html', token => $token);
6282              ## Ignore the token              ## Ignore the token
6283              !!!next-token;              !!!next-token;
6284              redo B;              next B;
6285            } else {            } else {
6286              !!!cp ('t308');              !!!cp ('t308');
6287              $self->{insertion_mode} = AFTER_HTML_BODY_IM;              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6288              !!!next-token;              !!!next-token;
6289              redo B;              next B;
6290            }            }
6291          } else {          } else {
6292            !!!cp ('t309');            !!!cp ('t309');
6293            !!!parse-error (type => 'after body:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'after body:/',
6294                              text => $token->{tag_name}, token => $token);
6295    
6296            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
6297            ## reprocess            ## reprocess
6298            redo B;            next B;
6299          }          }
6300        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6301          !!!cp ('t309.2');          !!!cp ('t309.2');
# Line 5402  sub _tree_construction_main ($) { Line 6312  sub _tree_construction_main ($) {
6312            unless (length $token->{data}) {            unless (length $token->{data}) {
6313              !!!cp ('t310');              !!!cp ('t310');
6314              !!!next-token;              !!!next-token;
6315              redo B;              next B;
6316            }            }
6317          }          }
6318                    
6319          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
6320            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6321              !!!cp ('t311');              !!!cp ('t311');
6322              !!!parse-error (type => 'in frameset:#character', token => $token);              !!!parse-error (type => 'in frameset:#text', token => $token);
6323            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6324              !!!cp ('t312');              !!!cp ('t312');
6325              !!!parse-error (type => 'after frameset:#character', token => $token);              !!!parse-error (type => 'after frameset:#text', token => $token);
6326            } else { # "after html frameset"            } else { # "after after frameset"
6327              !!!cp ('t313');              !!!cp ('t313');
6328              !!!parse-error (type => 'after html:#character', token => $token);              !!!parse-error (type => 'after html:#text', token => $token);
   
             $self->{insertion_mode} = AFTER_FRAMESET_IM;  
             ## Reprocess in the "after frameset" insertion mode.  
             !!!parse-error (type => 'after frameset:#character', token => $token);  
6329            }            }
6330                        
6331            ## Ignore the token.            ## Ignore the token.
# Line 5430  sub _tree_construction_main ($) { Line 6336  sub _tree_construction_main ($) {
6336              !!!cp ('t315');              !!!cp ('t315');
6337              !!!next-token;              !!!next-token;
6338            }            }
6339            redo B;            next B;
6340          }          }
6341                    
6342          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
6343        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
         if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {  
           !!!cp ('t316');  
           !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);  
   
           $self->{insertion_mode} = AFTER_FRAMESET_IM;  
           ## Process in the "after frameset" insertion mode.  
         } else {  
           !!!cp ('t317');  
         }  
   
6344          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6345              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6346            !!!cp ('t318');            !!!cp ('t318');
6347            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6348              !!!nack ('t318.1');
6349            !!!next-token;            !!!next-token;
6350            redo B;            next B;
6351          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
6352                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
6353            !!!cp ('t319');            !!!cp ('t319');
6354            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6355            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6356              !!!ack ('t319.1');
6357            !!!next-token;            !!!next-token;
6358            redo B;            next B;
6359          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
6360            !!!cp ('t320');            !!!cp ('t320');
6361            ## NOTE: As if in body.            ## NOTE: As if in head.
6362            $parse_rcdata->(CDATA_CONTENT_MODEL);            $parse_rcdata->(CDATA_CONTENT_MODEL);
6363            redo B;            next B;
6364    
6365              ## NOTE: |<!DOCTYPE HTML><frameset></frameset></html><noframes></noframes>|
6366              ## has no parse error.
6367          } else {          } else {
6368            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6369              !!!cp ('t321');              !!!cp ('t321');
6370              !!!parse-error (type => 'in frameset:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'in frameset',
6371            } else {                              text => $token->{tag_name}, token => $token);
6372              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6373              !!!cp ('t322');              !!!cp ('t322');
6374              !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'after frameset',
6375                                text => $token->{tag_name}, token => $token);
6376              } else { # "after after frameset"
6377                !!!cp ('t322.2');
6378                !!!parse-error (type => 'after after frameset',
6379                                text => $token->{tag_name}, token => $token);
6380            }            }
6381            ## Ignore the token            ## Ignore the token
6382              !!!nack ('t322.1');
6383            !!!next-token;            !!!next-token;
6384            redo B;            next B;
6385          }          }
6386        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
         if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {  
           !!!cp ('t323');  
           !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);  
   
           $self->{insertion_mode} = AFTER_FRAMESET_IM;  
           ## Process in the "after frameset" insertion mode.  
         } else {  
           !!!cp ('t324');  
         }  
   
6387          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6388              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6389            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6390                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
6391              !!!cp ('t325');              !!!cp ('t325');
6392              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag',
6393                                text => $token->{tag_name}, token => $token);
6394              ## Ignore the token              ## Ignore the token
6395              !!!next-token;              !!!next-token;
6396            } else {            } else {
# Line 5501  sub _tree_construction_main ($) { Line 6400  sub _tree_construction_main ($) {
6400            }            }
6401    
6402            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
6403                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6404              !!!cp ('t327');              !!!cp ('t327');
6405              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6406            } else {            } else {
6407              !!!cp ('t328');              !!!cp ('t328');
6408            }            }
6409            redo B;            next B;
6410          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
6411                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6412            !!!cp ('t329');            !!!cp ('t329');
6413            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6414            !!!next-token;            !!!next-token;
6415            redo B;            next B;
6416          } else {          } else {
6417            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6418              !!!cp ('t330');              !!!cp ('t330');
6419              !!!parse-error (type => 'in frameset:/'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'in frameset:/',
6420            } else {                              text => $token->{tag_name}, token => $token);
6421              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6422                !!!cp ('t330.1');
6423                !!!parse-error (type => 'after frameset:/',
6424                                text => $token->{tag_name}, token => $token);
6425              } else { # "after after html"
6426              !!!cp ('t331');              !!!cp ('t331');
6427              !!!parse-error (type => 'after frameset:/'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'after after frameset:/',
6428                                text => $token->{tag_name}, token => $token);
6429            }            }
6430            ## Ignore the token            ## Ignore the token
6431            !!!next-token;            !!!next-token;
6432            redo B;            next B;
6433          }          }
6434        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6435          unless ($self->{open_elements}->[-1]->[1] eq 'html' and          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6436                  @{$self->{open_elements}} == 1) { # redundant, maybe                  @{$self->{open_elements}} == 1) { # redundant, maybe
6437            !!!cp ('t331.1');            !!!cp ('t331.1');
6438            !!!parse-error (type => 'in body:#eof', token => $token);            !!!parse-error (type => 'in body:#eof', token => $token);
# Line 5552  sub _tree_construction_main ($) { Line 6457  sub _tree_construction_main ($) {
6457          !!!cp ('t332');          !!!cp ('t332');
6458          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6459          $script_start_tag->();          $script_start_tag->();
6460          redo B;          next B;
6461        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
6462          !!!cp ('t333');          !!!cp ('t333');
6463          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6464          $parse_rcdata->(CDATA_CONTENT_MODEL);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6465          redo B;          next B;
6466        } elsif ({        } elsif ({
6467                  base => 1, link => 1,                  base => 1, link => 1,
6468                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 5565  sub _tree_construction_main ($) { Line 6470  sub _tree_construction_main ($) {
6470          ## 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
6471          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6472          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6473            !!!ack ('t334.1');
6474          !!!next-token;          !!!next-token;
6475          redo B;          next B;
6476        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
6477          ## 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
6478          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6479          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.
6480    
6481          unless ($self->{confident}) {          unless ($self->{confident}) {
6482            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) {
6483              !!!cp ('t335');              !!!cp ('t335');
6484                ## NOTE: Whether the encoding is supported or not is handled
6485                ## in the {change_encoding} callback.
6486              $self->{change_encoding}              $self->{change_encoding}
6487                  ->($self, $token->{attributes}->{charset}->{value}, $token);                  ->($self, $token->{attributes}->{charset}->{value}, $token);
6488                            
# Line 5583  sub _tree_construction_main ($) { Line 6491  sub _tree_construction_main ($) {
6491                                       $token->{attributes}->{charset}                                       $token->{attributes}->{charset}
6492                                           ->{has_reference});                                           ->{has_reference});
6493            } elsif ($token->{attributes}->{content}) {            } elsif ($token->{attributes}->{content}) {
             ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
6494              if ($token->{attributes}->{content}->{value}              if ($token->{attributes}->{content}->{value}
6495                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                  =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6496                      [\x09-\x0D\x20]*=                      [\x09-\x0D\x20]*=
6497                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6498                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
6499                !!!cp ('t336');                !!!cp ('t336');
6500                  ## NOTE: Whether the encoding is supported or not is handled
6501                  ## in the {change_encoding} callback.
6502                $self->{change_encoding}                $self->{change_encoding}
6503                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6504                $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 6524  sub _tree_construction_main ($) {
6524            }            }
6525          }          }
6526    
6527            !!!ack ('t338.1');
6528          !!!next-token;          !!!next-token;
6529          redo B;          next B;
6530        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
6531          !!!cp ('t341');          !!!cp ('t341');
6532          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6533          $parse_rcdata->(RCDATA_CONTENT_MODEL);          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6534          redo B;          next B;
6535        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
6536          !!!parse-error (type => 'in body:body', token => $token);          !!!parse-error (type => 'in body', text => 'body', token => $token);
6537                                
6538          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
6539              $self->{open_elements}->[1]->[1] ne 'body') {              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6540            !!!cp ('t342');            !!!cp ('t342');
6541            ## Ignore the token            ## Ignore the token
6542          } else {          } else {
# Line 5640  sub _tree_construction_main ($) { Line 6550  sub _tree_construction_main ($) {
6550              }              }
6551            }            }
6552          }          }
6553            !!!nack ('t343.1');
6554          !!!next-token;          !!!next-token;
6555          redo B;          next B;
6556        } elsif ({        } elsif ({
6557                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6558                  div => 1, dl => 1, fieldset => 1,                  div => 1, dl => 1, fieldset => 1,
# Line 5656  sub _tree_construction_main ($) { Line 6567  sub _tree_construction_main ($) {
6567            !!!cp ('t350');            !!!cp ('t350');
6568            !!!parse-error (type => 'in form:form', token => $token);            !!!parse-error (type => 'in form:form', token => $token);
6569            ## Ignore the token            ## Ignore the token
6570              !!!nack ('t350.1');
6571            !!!next-token;            !!!next-token;
6572            redo B;            next B;
6573          }          }
6574    
6575          ## has a p element in scope          ## has a p element in scope
6576          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6577            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6578              !!!cp ('t344');              !!!cp ('t344');
6579              !!!back-token;              !!!back-token; # <form>
6580              $token = {type => END_TAG_TOKEN, tag_name => 'p',              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6581                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6582              redo B;              next B;
6583            } elsif ({            } elsif ($_->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6584              !!!cp ('t345');              !!!cp ('t345');
6585              last INSCOPE;              last INSCOPE;
6586            }            }
# Line 5679  sub _tree_construction_main ($) { Line 6588  sub _tree_construction_main ($) {
6588                        
6589          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6590          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6591              !!!nack ('t346.1');
6592            !!!next-token;            !!!next-token;
6593            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
6594              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
# Line 5695  sub _tree_construction_main ($) { Line 6605  sub _tree_construction_main ($) {
6605            !!!cp ('t347.1');            !!!cp ('t347.1');
6606            $self->{form_element} = $self->{open_elements}->[-1]->[0];            $self->{form_element} = $self->{open_elements}->[-1]->[0];
6607    
6608              !!!nack ('t347.2');
6609            !!!next-token;            !!!next-token;
6610          } elsif ($token->{tag_name} eq 'table') {          } elsif ($token->{tag_name} eq 'table') {
6611            !!!cp ('t382');            !!!cp ('t382');
# Line 5702  sub _tree_construction_main ($) { Line 6613  sub _tree_construction_main ($) {
6613                        
6614            $self->{insertion_mode} = IN_TABLE_IM;            $self->{insertion_mode} = IN_TABLE_IM;
6615    
6616              !!!nack ('t382.1');
6617            !!!next-token;            !!!next-token;
6618          } elsif ($token->{tag_name} eq 'hr') {          } elsif ($token->{tag_name} eq 'hr') {
6619            !!!cp ('t386');            !!!cp ('t386');
6620            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6621                    
6622              !!!nack ('t386.1');
6623            !!!next-token;            !!!next-token;
6624          } else {          } else {
6625            !!!cp ('t347');            !!!nack ('t347.1');
6626            !!!next-token;            !!!next-token;
6627          }          }
6628          redo B;          next B;
6629        } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {        } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
6630          ## has a p element in scope          ## has a p element in scope
6631          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6632            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6633              !!!cp ('t353');              !!!cp ('t353');
6634              !!!back-token;              !!!back-token; # <x>
6635              $token = {type => END_TAG_TOKEN, tag_name => 'p',              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6636                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6637              redo B;              next B;
6638            } elsif ({            } elsif ($_->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6639              !!!cp ('t354');              !!!cp ('t354');
6640              last INSCOPE;              last INSCOPE;
6641            }            }
# Line 5739  sub _tree_construction_main ($) { Line 6649  sub _tree_construction_main ($) {
6649                            dd => {dt => 1, dd => 1}}->{$token->{tag_name}};                            dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6650          LI: {          LI: {
6651            ## Step 2            ## Step 2
6652            if ($li_or_dtdd->{$node->[1]}) {            if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6653              if ($i != -1) {              if ($i != -1) {
6654                !!!cp ('t355');                !!!cp ('t355');
6655                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'not closed',
6656                                $self->{open_elements}->[-1]->[1], token => $token);                                text => $self->{open_elements}->[-1]->[0]
6657                                      ->manakai_local_name,
6658                                  token => $token);
6659              } else {              } else {
6660                !!!cp ('t356');                !!!cp ('t356');
6661              }              }
# Line 5754  sub _tree_construction_main ($) { Line 6666  sub _tree_construction_main ($) {
6666            }            }
6667                        
6668            ## Step 3            ## Step 3
6669            if (not $formatting_category->{$node->[1]} and            if (not ($node->[1] & FORMATTING_EL) and
6670                #not $phrasing_category->{$node->[1]} and                #not $phrasing_category->{$node->[1]} and
6671                ($special_category->{$node->[1]} or                ($node->[1] & SPECIAL_EL or
6672                 $scoping_category->{$node->[1]}) and                 $node->[1] & SCOPING_EL) and
6673                $node->[1] ne 'address' and $node->[1] ne 'div') {                not ($node->[1] & ADDRESS_EL) and
6674                  not ($node->[1] & DIV_EL)) {
6675              !!!cp ('t358');              !!!cp ('t358');
6676              last LI;              last LI;
6677            }            }
# Line 5771  sub _tree_construction_main ($) { Line 6684  sub _tree_construction_main ($) {
6684          } # LI          } # LI
6685                        
6686          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6687            !!!nack ('t359.1');
6688          !!!next-token;          !!!next-token;
6689          redo B;          next B;
6690        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
6691          ## has a p element in scope          ## has a p element in scope
6692          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6693            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6694              !!!cp ('t367');              !!!cp ('t367');
6695              !!!back-token;              !!!back-token; # <plaintext>
6696              $token = {type => END_TAG_TOKEN, tag_name => 'p',              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6697                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6698              redo B;              next B;
6699            } elsif ({            } elsif ($_->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6700              !!!cp ('t368');              !!!cp ('t368');
6701              last INSCOPE;              last INSCOPE;
6702            }            }
# Line 5795  sub _tree_construction_main ($) { Line 6706  sub _tree_construction_main ($) {
6706                        
6707          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
6708                        
6709            !!!nack ('t368.1');
6710          !!!next-token;          !!!next-token;
6711          redo B;          next B;
6712        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
6713          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
6714            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
6715            if ($node->[1] eq 'a') {            if ($node->[1] & A_EL) {
6716              !!!cp ('t371');              !!!cp ('t371');
6717              !!!parse-error (type => 'in a:a', token => $token);              !!!parse-error (type => 'in a:a', token => $token);
6718                            
6719              !!!back-token;              !!!back-token; # <a>
6720              $token = {type => END_TAG_TOKEN, tag_name => 'a',              $token = {type => END_TAG_TOKEN, tag_name => 'a',
6721                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6722              $formatting_end_tag->($token);              $formatting_end_tag->($token);
# Line 5835  sub _tree_construction_main ($) { Line 6747  sub _tree_construction_main ($) {
6747          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6748          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6749    
6750            !!!nack ('t374.1');
6751          !!!next-token;          !!!next-token;
6752          redo B;          next B;
6753        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
6754          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6755    
6756          ## has a |nobr| element in scope          ## has a |nobr| element in scope
6757          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6758            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6759            if ($node->[1] eq 'nobr') {            if ($node->[1] & NOBR_EL) {
6760              !!!cp ('t376');              !!!cp ('t376');
6761              !!!parse-error (type => 'in nobr:nobr', token => $token);              !!!parse-error (type => 'in nobr:nobr', token => $token);
6762              !!!back-token;              !!!back-token; # <nobr>
6763              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
6764                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6765              redo B;              next B;
6766            } 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]}) {  
6767              !!!cp ('t377');              !!!cp ('t377');
6768              last INSCOPE;              last INSCOPE;
6769            }            }
# Line 5862  sub _tree_construction_main ($) { Line 6772  sub _tree_construction_main ($) {
6772          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6773          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6774                    
6775            !!!nack ('t377.1');
6776          !!!next-token;          !!!next-token;
6777          redo B;          next B;
6778        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
6779          ## has a button element in scope          ## has a button element in scope
6780          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6781            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6782            if ($node->[1] eq 'button') {            if ($node->[1] & BUTTON_EL) {
6783              !!!cp ('t378');              !!!cp ('t378');
6784              !!!parse-error (type => 'in button:button', token => $token);              !!!parse-error (type => 'in button:button', token => $token);
6785              !!!back-token;              !!!back-token; # <button>
6786              $token = {type => END_TAG_TOKEN, tag_name => 'button',              $token = {type => END_TAG_TOKEN, tag_name => 'button',
6787                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6788              redo B;              next B;
6789            } 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]}) {  
6790              !!!cp ('t379');              !!!cp ('t379');
6791              last INSCOPE;              last INSCOPE;
6792            }            }
# Line 5892  sub _tree_construction_main ($) { Line 6800  sub _tree_construction_main ($) {
6800    
6801          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
6802    
6803            !!!nack ('t379.1');
6804          !!!next-token;          !!!next-token;
6805          redo B;          next B;
6806        } elsif ({        } elsif ({
6807                  xmp => 1,                  xmp => 1,
6808                  iframe => 1,                  iframe => 1,
6809                  noembed => 1,                  noembed => 1,
6810                  noframes => 1,                  noframes => 1, ## NOTE: This is an "as if in head" code clone.
6811                  noscript => 0, ## TODO: 1 if scripting is enabled                  noscript => 0, ## TODO: 1 if scripting is enabled
6812                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6813          if ($token->{tag_name} eq 'xmp') {          if ($token->{tag_name} eq 'xmp') {
# Line 5909  sub _tree_construction_main ($) { Line 6818  sub _tree_construction_main ($) {
6818          }          }
6819          ## NOTE: There is an "as if in body" code clone.          ## NOTE: There is an "as if in body" code clone.
6820          $parse_rcdata->(CDATA_CONTENT_MODEL);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6821          redo B;          next B;
6822        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
6823          !!!parse-error (type => 'isindex', token => $token);          !!!parse-error (type => 'isindex', token => $token);
6824                    
6825          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
6826            !!!cp ('t389');            !!!cp ('t389');
6827            ## Ignore the token            ## Ignore the token
6828              !!!nack ('t389'); ## NOTE: Not acknowledged.
6829            !!!next-token;            !!!next-token;
6830            redo B;            next B;
6831          } else {          } else {
6832              !!!ack ('t391.1');
6833    
6834            my $at = $token->{attributes};            my $at = $token->{attributes};
6835            my $form_attrs;            my $form_attrs;
6836            $form_attrs->{action} = $at->{action} if $at->{action};            $form_attrs->{action} = $at->{action} if $at->{action};
# Line 5962  sub _tree_construction_main ($) { Line 6874  sub _tree_construction_main ($) {
6874                           line => $token->{line}, column => $token->{column}},                           line => $token->{line}, column => $token->{column}},
6875                          {type => END_TAG_TOKEN, tag_name => 'form',                          {type => END_TAG_TOKEN, tag_name => 'form',
6876                           line => $token->{line}, column => $token->{column}};                           line => $token->{line}, column => $token->{column}};
           $token = shift @tokens;  
6877            !!!back-token (@tokens);            !!!back-token (@tokens);
6878            redo B;            !!!next-token;
6879              next B;
6880          }          }
6881        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
6882          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
6883          my $el;          my $el;
6884          !!!create-element ($el, $token->{tag_name}, $token->{attributes}, $token);          !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
6885                    
6886          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
6887          $self->{content_model} = RCDATA_CONTENT_MODEL;          $self->{content_model} = RCDATA_CONTENT_MODEL;
# Line 5978  sub _tree_construction_main ($) { Line 6890  sub _tree_construction_main ($) {
6890          $insert->($el);          $insert->($el);
6891                    
6892          my $text = '';          my $text = '';
6893            !!!nack ('t392.1');
6894          !!!next-token;          !!!next-token;
6895          if ($token->{type} == CHARACTER_TOKEN) {          if ($token->{type} == CHARACTER_TOKEN) {
6896            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
# Line 6008  sub _tree_construction_main ($) { Line 6921  sub _tree_construction_main ($) {
6921            ## Ignore the token            ## Ignore the token
6922          } else {          } else {
6923            !!!cp ('t398');            !!!cp ('t398');
6924            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);            !!!parse-error (type => 'in RCDATA:#eof', token => $token);
6925          }          }
6926          !!!next-token;          !!!next-token;
6927            next B;
6928          } elsif ($token->{tag_name} eq 'rt' or
6929                   $token->{tag_name} eq 'rp') {
6930            ## has a |ruby| element in scope
6931            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6932              my $node = $self->{open_elements}->[$_];
6933              if ($node->[1] & RUBY_EL) {
6934                !!!cp ('t398.1');
6935                ## generate implied end tags
6936                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
6937                  !!!cp ('t398.2');
6938                  pop @{$self->{open_elements}};
6939                }
6940                unless ($self->{open_elements}->[-1]->[1] & RUBY_EL) {
6941                  !!!cp ('t398.3');
6942                  !!!parse-error (type => 'not closed',
6943                                  text => $self->{open_elements}->[-1]->[0]
6944                                      ->manakai_local_name,
6945                                  token => $token);
6946                  pop @{$self->{open_elements}}
6947                      while not $self->{open_elements}->[-1]->[1] & RUBY_EL;
6948                }
6949                last INSCOPE;
6950              } elsif ($node->[1] & SCOPING_EL) {
6951                !!!cp ('t398.4');
6952                last INSCOPE;
6953              }
6954            } # INSCOPE
6955    
6956            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6957    
6958            !!!nack ('t398.5');
6959            !!!next-token;
6960          redo B;          redo B;
6961          } elsif ($token->{tag_name} eq 'math' or
6962                   $token->{tag_name} eq 'svg') {
6963            $reconstruct_active_formatting_elements->($insert_to_current);
6964    
6965            ## "Adjust MathML attributes" ('math' only) - done in insert-element-f
6966    
6967            ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
6968    
6969            ## "adjust foreign attributes" - done in insert-element-f
6970            
6971            !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
6972            
6973            if ($self->{self_closing}) {
6974              pop @{$self->{open_elements}};
6975              !!!ack ('t398.1');
6976            } else {
6977              !!!cp ('t398.2');
6978              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
6979              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
6980              ## mode, "in body" (not "in foreign content") secondary insertion
6981              ## mode, maybe.
6982            }
6983    
6984            !!!next-token;
6985            next B;
6986        } elsif ({        } elsif ({
6987                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
6988                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 6019  sub _tree_construction_main ($) { Line 6990  sub _tree_construction_main ($) {
6990                  thead => 1, tr => 1,                  thead => 1, tr => 1,
6991                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6992          !!!cp ('t401');          !!!cp ('t401');
6993          !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'in body',
6994                            text => $token->{tag_name}, token => $token);
6995          ## Ignore the token          ## Ignore the token
6996            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
6997          !!!next-token;          !!!next-token;
6998          redo B;          next B;
6999                    
7000          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
7001        } else {        } else {
# Line 6044  sub _tree_construction_main ($) { Line 7017  sub _tree_construction_main ($) {
7017              }->{$token->{tag_name}}) {              }->{$token->{tag_name}}) {
7018            !!!cp ('t380');            !!!cp ('t380');
7019            push @$active_formatting_elements, ['#marker', ''];            push @$active_formatting_elements, ['#marker', ''];
7020              !!!nack ('t380.1');
7021          } elsif ({          } elsif ({
7022                    b => 1, big => 1, em => 1, font => 1, i => 1,                    b => 1, big => 1, em => 1, font => 1, i => 1,
7023                    s => 1, small => 1, strile => 1,                    s => 1, small => 1, strile => 1,
# Line 6051  sub _tree_construction_main ($) { Line 7025  sub _tree_construction_main ($) {
7025                   }->{$token->{tag_name}}) {                   }->{$token->{tag_name}}) {
7026            !!!cp ('t375');            !!!cp ('t375');
7027            push @$active_formatting_elements, $self->{open_elements}->[-1];            push @$active_formatting_elements, $self->{open_elements}->[-1];
7028              !!!nack ('t375.1');
7029          } elsif ($token->{tag_name} eq 'input') {          } elsif ($token->{tag_name} eq 'input') {
7030            !!!cp ('t388');            !!!cp ('t388');
7031            ## TODO: associate with $self->{form_element} if defined            ## TODO: associate with $self->{form_element} if defined
7032            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
7033              !!!ack ('t388.2');
7034          } elsif ({          } elsif ({
7035                    area => 1, basefont => 1, bgsound => 1, br => 1,                    area => 1, basefont => 1, bgsound => 1, br => 1,
7036                    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 7038  sub _tree_construction_main ($) {
7038                   }->{$token->{tag_name}}) {                   }->{$token->{tag_name}}) {
7039            !!!cp ('t388.1');            !!!cp ('t388.1');
7040            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
7041              !!!ack ('t388.3');
7042          } elsif ($token->{tag_name} eq 'select') {          } elsif ($token->{tag_name} eq 'select') {
7043            ## TODO: associate with $self->{form_element} if defined            ## TODO: associate with $self->{form_element} if defined
7044                    
# Line 6074  sub _tree_construction_main ($) { Line 7051  sub _tree_construction_main ($) {
7051              !!!cp ('t400.2');              !!!cp ('t400.2');
7052              $self->{insertion_mode} = IN_SELECT_IM;              $self->{insertion_mode} = IN_SELECT_IM;
7053            }            }
7054              !!!nack ('t400.3');
7055          } else {          } else {
7056            !!!cp ('t402');            !!!nack ('t402');
7057          }          }
7058                    
7059          !!!next-token;          !!!next-token;
7060          redo B;          next B;
7061        }        }
7062      } elsif ($token->{type} == END_TAG_TOKEN) {      } elsif ($token->{type} == END_TAG_TOKEN) {
7063        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
# Line 6087  sub _tree_construction_main ($) { Line 7065  sub _tree_construction_main ($) {
7065          my $i;          my $i;
7066          INSCOPE: {          INSCOPE: {
7067            for (reverse @{$self->{open_elements}}) {            for (reverse @{$self->{open_elements}}) {
7068              if ($_->[1] eq 'body') {              if ($_->[1] & BODY_EL) {
7069                !!!cp ('t405');                !!!cp ('t405');
7070                $i = $_;                $i = $_;
7071                last INSCOPE;                last INSCOPE;
7072              } elsif ({              } elsif ($_->[1] & SCOPING_EL) {
                       applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                       button => 1, marquee => 1, object => 1, html => 1,  
                      }->{$_->[1]}) {  
7073                !!!cp ('t405.1');                !!!cp ('t405.1');
7074                last;                last;
7075              }              }
7076            }            }
7077    
7078            !!!parse-error (type => 'start tag not allowed',            !!!parse-error (type => 'start tag not allowed',
7079                            value => $token->{tag_name}, token => $token);                            text => $token->{tag_name}, token => $token);
7080            ## NOTE: Ignore the token.            ## NOTE: Ignore the token.
7081            !!!next-token;            !!!next-token;
7082            redo B;            next B;
7083          } # INSCOPE          } # INSCOPE
7084    
7085          for (@{$self->{open_elements}}) {          for (@{$self->{open_elements}}) {
7086            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]}) {  
7087              !!!cp ('t403');              !!!cp ('t403');
7088              !!!parse-error (type => 'not closed:'.$_->[1], token => $token);              !!!parse-error (type => 'not closed',
7089                                text => $_->[0]->manakai_local_name,
7090                                token => $token);
7091              last;              last;
7092            } else {            } else {
7093              !!!cp ('t404');              !!!cp ('t404');
# Line 6123  sub _tree_construction_main ($) { Line 7096  sub _tree_construction_main ($) {
7096    
7097          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
7098          !!!next-token;          !!!next-token;
7099          redo B;          next B;
7100        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
7101          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
7102            ## up-to-date, though it has same effect as speced.
7103            if (@{$self->{open_elements}} > 1 and
7104                $self->{open_elements}->[1]->[1] & BODY_EL) {
7105            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
7106            if ($self->{open_elements}->[-1]->[1] ne 'body') {            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
7107              !!!cp ('t406');              !!!cp ('t406');
7108              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1], token => $token);              !!!parse-error (type => 'not closed',
7109                                text => $self->{open_elements}->[1]->[0]
7110                                    ->manakai_local_name,
7111                                token => $token);
7112            } else {            } else {
7113              !!!cp ('t407');              !!!cp ('t407');
7114            }            }
7115            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
7116            ## reprocess            ## reprocess
7117            redo B;            next B;
7118          } else {          } else {
7119            !!!cp ('t408');            !!!cp ('t408');
7120            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
7121                              text => $token->{tag_name}, token => $token);
7122            ## Ignore the token            ## Ignore the token
7123            !!!next-token;            !!!next-token;
7124            redo B;            next B;
7125          }          }
7126        } elsif ({        } elsif ({
7127                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
# Line 6154  sub _tree_construction_main ($) { Line 7134  sub _tree_construction_main ($) {
7134          my $i;          my $i;
7135          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7136            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7137            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7138              !!!cp ('t410');              !!!cp ('t410');
7139              $i = $_;              $i = $_;
7140              last INSCOPE;              last INSCOPE;
7141            } 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]}) {  
7142              !!!cp ('t411');              !!!cp ('t411');
7143              last INSCOPE;              last INSCOPE;
7144            }            }
# Line 6169  sub _tree_construction_main ($) { Line 7146  sub _tree_construction_main ($) {
7146    
7147          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
7148            !!!cp ('t413');            !!!cp ('t413');
7149            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
7150                              text => $token->{tag_name}, token => $token);
7151              ## NOTE: Ignore the token.
7152          } else {          } else {
7153            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7154            while ({            while ({
7155                      ## END_TAG_OPTIONAL_EL
7156                    dd => ($token->{tag_name} ne 'dd'),                    dd => ($token->{tag_name} ne 'dd'),
7157                    dt => ($token->{tag_name} ne 'dt'),                    dt => ($token->{tag_name} ne 'dt'),
7158                    li => ($token->{tag_name} ne 'li'),                    li => ($token->{tag_name} ne 'li'),
7159                    p => 1,                    p => 1,
7160                   }->{$self->{open_elements}->[-1]->[1]}) {                    rt => 1,
7161                      rp => 1,
7162                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
7163              !!!cp ('t409');              !!!cp ('t409');
7164              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
7165            }            }
7166    
7167            ## Step 2.            ## Step 2.
7168            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7169                      ne $token->{tag_name}) {
7170              !!!cp ('t412');              !!!cp ('t412');
7171              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);              !!!parse-error (type => 'not closed',
7172                                text => $self->{open_elements}->[-1]->[0]
7173                                    ->manakai_local_name,
7174                                token => $token);
7175            } else {            } else {
7176              !!!cp ('t414');              !!!cp ('t414');
7177            }            }
# Line 6200  sub _tree_construction_main ($) { Line 7186  sub _tree_construction_main ($) {
7186                }->{$token->{tag_name}};                }->{$token->{tag_name}};
7187          }          }
7188          !!!next-token;          !!!next-token;
7189          redo B;          next B;
7190        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
7191          undef $self->{form_element};          undef $self->{form_element};
7192    
# Line 6208  sub _tree_construction_main ($) { Line 7194  sub _tree_construction_main ($) {
7194          my $i;          my $i;
7195          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7196            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7197            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & FORM_EL) {
7198              !!!cp ('t418');              !!!cp ('t418');
7199              $i = $_;              $i = $_;
7200              last INSCOPE;              last INSCOPE;
7201            } 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]}) {  
7202              !!!cp ('t419');              !!!cp ('t419');
7203              last INSCOPE;              last INSCOPE;
7204            }            }
# Line 6223  sub _tree_construction_main ($) { Line 7206  sub _tree_construction_main ($) {
7206    
7207          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
7208            !!!cp ('t421');            !!!cp ('t421');
7209            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
7210                              text => $token->{tag_name}, token => $token);
7211              ## NOTE: Ignore the token.
7212          } else {          } else {
7213            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7214            while ({            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                   dd => 1, dt => 1, li => 1, p => 1,  
                  }->{$self->{open_elements}->[-1]->[1]}) {  
7215              !!!cp ('t417');              !!!cp ('t417');
7216              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
7217            }            }
7218                        
7219            ## Step 2.            ## Step 2.
7220            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7221                      ne $token->{tag_name}) {
7222              !!!cp ('t417.1');              !!!cp ('t417.1');
7223              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);              !!!parse-error (type => 'not closed',
7224                                text => $self->{open_elements}->[-1]->[0]
7225                                    ->manakai_local_name,
7226                                token => $token);
7227            } else {            } else {
7228              !!!cp ('t420');              !!!cp ('t420');
7229            }              }  
# Line 6246  sub _tree_construction_main ($) { Line 7233  sub _tree_construction_main ($) {
7233          }          }
7234    
7235          !!!next-token;          !!!next-token;
7236          redo B;          next B;
7237        } elsif ({        } elsif ({
7238                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
7239                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 6254  sub _tree_construction_main ($) { Line 7241  sub _tree_construction_main ($) {
7241          my $i;          my $i;
7242          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7243            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7244            if ({            if ($node->[1] & HEADING_EL) {
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
7245              !!!cp ('t423');              !!!cp ('t423');
7246              $i = $_;              $i = $_;
7247              last INSCOPE;              last INSCOPE;
7248            } 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]}) {  
7249              !!!cp ('t424');              !!!cp ('t424');
7250              last INSCOPE;              last INSCOPE;
7251            }            }
# Line 6271  sub _tree_construction_main ($) { Line 7253  sub _tree_construction_main ($) {
7253    
7254          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
7255            !!!cp ('t425.1');            !!!cp ('t425.1');
7256            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
7257                              text => $token->{tag_name}, token => $token);
7258              ## NOTE: Ignore the token.
7259          } else {          } else {
7260            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7261            while ({            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                   dd => 1, dt => 1, li => 1, p => 1,  
                  }->{$self->{open_elements}->[-1]->[1]}) {  
7262              !!!cp ('t422');              !!!cp ('t422');
7263              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
7264            }            }
7265                        
7266            ## Step 2.            ## Step 2.
7267            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7268                      ne $token->{tag_name}) {
7269              !!!cp ('t425');              !!!cp ('t425');
7270              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag',
7271                                text => $token->{tag_name}, token => $token);
7272            } else {            } else {
7273              !!!cp ('t426');              !!!cp ('t426');
7274            }            }
# Line 6294  sub _tree_construction_main ($) { Line 7278  sub _tree_construction_main ($) {
7278          }          }
7279                    
7280          !!!next-token;          !!!next-token;
7281          redo B;          next B;
7282        } elsif ($token->{tag_name} eq 'p') {        } elsif ($token->{tag_name} eq 'p') {
7283          ## has an element in scope          ## has an element in scope
7284          my $i;          my $i;
7285          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7286            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7287            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & P_EL) {
7288              !!!cp ('t410.1');              !!!cp ('t410.1');
7289              $i = $_;              $i = $_;
7290              last INSCOPE;              last INSCOPE;
7291            } 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]}) {  
7292              !!!cp ('t411.1');              !!!cp ('t411.1');
7293              last INSCOPE;              last INSCOPE;
7294            }            }
7295          } # INSCOPE          } # INSCOPE
7296    
7297          if (defined $i) {          if (defined $i) {
7298            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7299                      ne $token->{tag_name}) {
7300              !!!cp ('t412.1');              !!!cp ('t412.1');
7301              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);              !!!parse-error (type => 'not closed',
7302                                text => $self->{open_elements}->[-1]->[0]
7303                                    ->manakai_local_name,
7304                                token => $token);
7305            } else {            } else {
7306              !!!cp ('t414.1');              !!!cp ('t414.1');
7307            }            }
# Line 6324  sub _tree_construction_main ($) { Line 7309  sub _tree_construction_main ($) {
7309            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
7310          } else {          } else {
7311            !!!cp ('t413.1');            !!!cp ('t413.1');
7312            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
7313                              text => $token->{tag_name}, token => $token);
7314    
7315            !!!cp ('t415.1');            !!!cp ('t415.1');
7316            ## As if <p>, then reprocess the current token            ## As if <p>, then reprocess the current token
7317            my $el;            my $el;
7318            !!!create-element ($el, 'p',, $token);            !!!create-element ($el, $HTML_NS, 'p',, $token);
7319            $insert->($el);            $insert->($el);
7320            ## NOTE: Not inserted into |$self->{open_elements}|.            ## NOTE: Not inserted into |$self->{open_elements}|.
7321          }          }
7322    
7323          !!!next-token;          !!!next-token;
7324          redo B;          next B;
7325        } elsif ({        } elsif ({
7326                  a => 1,                  a => 1,
7327                  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 7330  sub _tree_construction_main ($) {
7330                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7331          !!!cp ('t427');          !!!cp ('t427');
7332          $formatting_end_tag->($token);          $formatting_end_tag->($token);
7333          redo B;          next B;
7334        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
7335          !!!cp ('t428');          !!!cp ('t428');
7336          !!!parse-error (type => 'unmatched end tag:br', token => $token);          !!!parse-error (type => 'unmatched end tag',
7337                            text => 'br', token => $token);
7338    
7339          ## As if <br>          ## As if <br>
7340          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7341                    
7342          my $el;          my $el;
7343          !!!create-element ($el, 'br',, $token);          !!!create-element ($el, $HTML_NS, 'br',, $token);
7344          $insert->($el);          $insert->($el);
7345                    
7346          ## Ignore the token.          ## Ignore the token.
7347          !!!next-token;          !!!next-token;
7348          redo B;          next B;
7349        } elsif ({        } elsif ({
7350                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7351                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 6372  sub _tree_construction_main ($) { Line 7359  sub _tree_construction_main ($) {
7359                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
7360                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7361          !!!cp ('t429');          !!!cp ('t429');
7362          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'unmatched end tag',
7363                            text => $token->{tag_name}, token => $token);
7364          ## Ignore the token          ## Ignore the token
7365          !!!next-token;          !!!next-token;
7366          redo B;          next B;
7367                    
7368          ## ISSUE: Issue on HTML5 new elements in spec          ## ISSUE: Issue on HTML5 new elements in spec
7369                    
# Line 6386  sub _tree_construction_main ($) { Line 7374  sub _tree_construction_main ($) {
7374    
7375          ## Step 2          ## Step 2
7376          S2: {          S2: {
7377            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7378              ## Step 1              ## Step 1
7379              ## generate implied end tags              ## generate implied end tags
7380              while ({              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                     dd => 1, dt => 1, li => 1, p => 1,  
                    }->{$self->{open_elements}->[-1]->[1]}) {  
7381                !!!cp ('t430');                !!!cp ('t430');
7382                ## ISSUE: Can this case be reached?                ## NOTE: |<ruby><rt></ruby>|.
7383                  ## ISSUE: <ruby><rt></rt> will also take this code path,
7384                  ## which seems wrong.
7385                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
7386                  $node_i++;
7387              }              }
7388                    
7389              ## Step 2              ## Step 2
7390              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7391                        ne $token->{tag_name}) {
7392                !!!cp ('t431');                !!!cp ('t431');
7393                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
7394                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                !!!parse-error (type => 'not closed',
7395                                  text => $self->{open_elements}->[-1]->[0]
7396                                      ->manakai_local_name,
7397                                  token => $token);
7398              } else {              } else {
7399                !!!cp ('t432');                !!!cp ('t432');
7400              }              }
7401                            
7402              ## Step 3              ## Step 3
7403              splice @{$self->{open_elements}}, $node_i;              splice @{$self->{open_elements}}, $node_i if $node_i < 0;
7404    
7405              !!!next-token;              !!!next-token;
7406              last S2;              last S2;
7407            } else {            } else {
7408              ## Step 3              ## Step 3
7409              if (not $formatting_category->{$node->[1]} and              if (not ($node->[1] & FORMATTING_EL) and
7410                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
7411                  ($special_category->{$node->[1]} or                  ($node->[1] & SPECIAL_EL or
7412                   $scoping_category->{$node->[1]})) {                   $node->[1] & SCOPING_EL)) {
7413                !!!cp ('t433');                !!!cp ('t433');
7414                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag',
7415                                  text => $token->{tag_name}, token => $token);
7416                ## Ignore the token                ## Ignore the token
7417                !!!next-token;                !!!next-token;
7418                last S2;                last S2;
# Line 6434  sub _tree_construction_main ($) { Line 7428  sub _tree_construction_main ($) {
7428            ## Step 5;            ## Step 5;
7429            redo S2;            redo S2;
7430          } # S2          } # S2
7431          redo B;          next B;
7432        }        }
7433      }      }
7434      redo B;      next B;
7435      } continue { # B
7436        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7437          ## NOTE: The code below is executed in cases where it does not have
7438          ## to be, but it it is harmless even in those cases.
7439          ## has an element in scope
7440          INSCOPE: {
7441            for (reverse 0..$#{$self->{open_elements}}) {
7442              my $node = $self->{open_elements}->[$_];
7443              if ($node->[1] & FOREIGN_EL) {
7444                last INSCOPE;
7445              } elsif ($node->[1] & SCOPING_EL) {
7446                last;
7447              }
7448            }
7449            
7450            ## NOTE: No foreign element in scope.
7451            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7452          } # INSCOPE
7453        }
7454    } # B    } # B
7455    
7456    ## Stop parsing # MUST    ## Stop parsing # MUST
# Line 6483  sub set_inner_html ($$$) { Line 7496  sub set_inner_html ($$$) {
7496    
7497      ## Step 8 # MUST      ## Step 8 # MUST
7498      my $i = 0;      my $i = 0;
7499      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7500      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7501      $p->{set_next_char} = sub {      $p->{set_next_char} = sub {
7502        my $self = shift;        my $self = shift;
7503    
# Line 6493  sub set_inner_html ($$$) { Line 7506  sub set_inner_html ($$$) {
7506    
7507        $self->{next_char} = -1 and return if $i >= length $$s;        $self->{next_char} = -1 and return if $i >= length $$s;
7508        $self->{next_char} = ord substr $$s, $i++, 1;        $self->{next_char} = ord substr $$s, $i++, 1;
7509        $column++;  
7510          ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7511          $p->{column}++;
7512    
7513        if ($self->{next_char} == 0x000A) { # LF        if ($self->{next_char} == 0x000A) { # LF
7514          $line++;          $p->{line}++;
7515          $column = 0;          $p->{column} = 0;
7516          !!!cp ('i1');          !!!cp ('i1');
7517        } elsif ($self->{next_char} == 0x000D) { # CR        } elsif ($self->{next_char} == 0x000D) { # CR
7518          $i++ if substr ($$s, $i, 1) eq "\x0A";          $i++ if substr ($$s, $i, 1) eq "\x0A";
7519          $self->{next_char} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
7520          $line++;          $p->{line}++;
7521          $column = 0;          $p->{column} = 0;
7522          !!!cp ('i2');          !!!cp ('i2');
7523        } elsif ($self->{next_char} > 0x10FFFF) {        } elsif ($self->{next_char} > 0x10FFFF) {
7524          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
# Line 6512  sub set_inner_html ($$$) { Line 7527  sub set_inner_html ($$$) {
7527          !!!cp ('i4');          !!!cp ('i4');
7528          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
7529          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7530          } elsif ($self->{next_char} <= 0x0008 or
7531                   (0x000E <= $self->{next_char} and
7532                    $self->{next_char} <= 0x001F) or
7533                   (0x007F <= $self->{next_char} and
7534                    $self->{next_char} <= 0x009F) or
7535                   (0xD800 <= $self->{next_char} and
7536                    $self->{next_char} <= 0xDFFF) or
7537                   (0xFDD0 <= $self->{next_char} and
7538                    $self->{next_char} <= 0xFDDF) or
7539                   {
7540                    0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
7541                    0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
7542                    0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
7543                    0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
7544                    0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
7545                    0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
7546                    0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
7547                    0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
7548                    0x10FFFE => 1, 0x10FFFF => 1,
7549                   }->{$self->{next_char}}) {
7550            !!!cp ('i4.1');
7551            if ($self->{next_char} < 0x10000) {
7552              !!!parse-error (type => 'control char',
7553                              text => (sprintf 'U+%04X', $self->{next_char}));
7554            } else {
7555              !!!parse-error (type => 'control char',
7556                              text => (sprintf 'U-%08X', $self->{next_char}));
7557            }
7558        }        }
7559      };      };
7560      $p->{prev_char} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];
# Line 6519  sub set_inner_html ($$$) { Line 7562  sub set_inner_html ($$$) {
7562            
7563      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7564        my (%opt) = @_;        my (%opt) = @_;
7565        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7566          my $column = $opt{column};
7567          if (defined $opt{token} and defined $opt{token}->{line}) {
7568            $line = $opt{token}->{line};
7569            $column = $opt{token}->{column};
7570          }
7571          warn "Parse error ($opt{type}) at line $line column $column\n";
7572      };      };
7573      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7574        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7575      };      };
7576            
7577      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
# Line 6546  sub set_inner_html ($$$) { Line 7595  sub set_inner_html ($$$) {
7595          unless defined $p->{content_model};          unless defined $p->{content_model};
7596          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
7597    
7598      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7599          ## TODO: Foreign element OK?
7600    
7601      ## Step 3      ## Step 3
7602      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
# Line 6556  sub set_inner_html ($$$) { Line 7606  sub set_inner_html ($$$) {
7606      $doc->append_child ($root);      $doc->append_child ($root);
7607    
7608      ## Step 5 # MUST      ## Step 5 # MUST
7609      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7610    
7611      undef $p->{head_element};      undef $p->{head_element};
7612    
# Line 6602  sub set_inner_html ($$$) { Line 7652  sub set_inner_html ($$$) {
7652      ## ISSUE: mutation events?      ## ISSUE: mutation events?
7653    
7654      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
7655    
7656        delete $p->{parse_error}; # delete loop
7657    } else {    } else {
7658      die "$0: |set_inner_html| is not defined for node of type $nt";      die "$0: |set_inner_html| is not defined for node of type $nt";
7659    }    }

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24