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

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

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

revision 1.116 by wakaba, Mon Mar 17 13:23:39 2008 UTC revision 1.162 by wakaba, Thu Sep 11 09:12:27 2008 UTC
# Line 8  use Error qw(:try); Line 8  use Error qw(:try);
8  ## doc.write ('');  ## doc.write ('');
9  ## alert (doc.compatMode);  ## alert (doc.compatMode);
10    
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, $charset_name, $byte_stream, $doc, $onerror, $get_wrapper) = @_;
359    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
360    my $charset = shift;    my $charset_name = shift;
361    my $bytes_s = ref $_[0] ? $_[0] : \($_[0]);    my $byte_stream = $_[0];
362    my $s;  
363        my $onerror = $_[2] || sub {
364    if (defined $charset) {      my (%opt) = @_;
365      require Encode; ## TODO: decode(utf8) don't delete BOM      warn "Parse error ($opt{type})\n";
366      $s = \ (Encode::decode ($charset, $$bytes_s));    };
367      $self->{input_encoding} = lc $charset; ## TODO: normalize name    $self->{parse_error} = $onerror; # updated later by parse_char_string
368      $self->{confident} = 1;  
369    } else {    my $get_wrapper = $_[3] || sub ($) {
370      ## TODO: Implement HTML5 detection algorithm      return $_[0]; # $_[0] = byte stream handle, returned = arg to char handle
371      };
372    
373      ## HTML5 encoding sniffing algorithm
374      require Message::Charset::Info;
375      my $charset;
376      my $buffer;
377      my ($char_stream, $e_status);
378    
379      SNIFFING: {
380        ## NOTE: By setting |allow_fallback| option true when the
381        ## |get_decode_handle| method is invoked, we ignore what the HTML5
382        ## spec requires, i.e. unsupported encoding should be ignored.
383          ## TODO: We should not do this unless the parser is invoked
384          ## in the conformance checking mode, in which this behavior
385          ## would be useful.
386    
387        ## Step 1
388        if (defined $charset_name) {
389          $charset = Message::Charset::Info->get_by_html_name ($charset_name);
390              ## TODO: Is this ok?  Transfer protocol's parameter should be
391              ## interpreted in its semantics?
392    
393          ## ISSUE: Unsupported encoding is not ignored according to the spec.
394          ($char_stream, $e_status) = $charset->get_decode_handle
395              ($byte_stream, allow_error_reporting => 1,
396               allow_fallback => 1);
397          if ($char_stream) {
398            $self->{confident} = 1;
399            last SNIFFING;
400          } else {
401            ## TODO: unsupported error
402          }
403        }
404    
405        ## Step 2
406        my $byte_buffer = '';
407        for (1..1024) {
408          my $char = $byte_stream->getc;
409          last unless defined $char;
410          $byte_buffer .= $char;
411        } ## TODO: timeout
412    
413        ## Step 3
414        if ($byte_buffer =~ /^\xFE\xFF/) {
415          $charset = Message::Charset::Info->get_by_html_name ('utf-16be');
416          ($char_stream, $e_status) = $charset->get_decode_handle
417              ($byte_stream, allow_error_reporting => 1,
418               allow_fallback => 1, byte_buffer => \$byte_buffer);
419          $self->{confident} = 1;
420          last SNIFFING;
421        } elsif ($byte_buffer =~ /^\xFF\xFE/) {
422          $charset = Message::Charset::Info->get_by_html_name ('utf-16le');
423          ($char_stream, $e_status) = $charset->get_decode_handle
424              ($byte_stream, allow_error_reporting => 1,
425               allow_fallback => 1, byte_buffer => \$byte_buffer);
426          $self->{confident} = 1;
427          last SNIFFING;
428        } elsif ($byte_buffer =~ /^\xEF\xBB\xBF/) {
429          $charset = Message::Charset::Info->get_by_html_name ('utf-8');
430          ($char_stream, $e_status) = $charset->get_decode_handle
431              ($byte_stream, allow_error_reporting => 1,
432               allow_fallback => 1, byte_buffer => \$byte_buffer);
433          $self->{confident} = 1;
434          last SNIFFING;
435        }
436    
437        ## Step 4
438        ## TODO: <meta charset>
439    
440        ## Step 5
441        ## TODO: from history
442    
443        ## Step 6
444      require Whatpm::Charset::UniversalCharDet;      require Whatpm::Charset::UniversalCharDet;
445      $charset = Whatpm::Charset::UniversalCharDet->detect_byte_string      $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
446          (substr ($$bytes_s, 0, 1024));          ($byte_buffer);
447      $charset ||= 'windows-1252';      if (defined $charset_name) {
448      $s = \ (Encode::decode ($charset, $$bytes_s));        $charset = Message::Charset::Info->get_by_html_name ($charset_name);
449      $self->{input_encoding} = $charset;  
450          ## ISSUE: Unsupported encoding is not ignored according to the spec.
451          require Whatpm::Charset::DecodeHandle;
452          $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
453              ($byte_stream);
454          ($char_stream, $e_status) = $charset->get_decode_handle
455              ($buffer, allow_error_reporting => 1,
456               allow_fallback => 1, byte_buffer => \$byte_buffer);
457          if ($char_stream) {
458            $buffer->{buffer} = $byte_buffer;
459            !!!parse-error (type => 'sniffing:chardet',
460                            text => $charset_name,
461                            level => $self->{level}->{info},
462                            layer => 'encode',
463                            line => 1, column => 1);
464            $self->{confident} = 0;
465            last SNIFFING;
466          }
467        }
468    
469        ## Step 7: default
470        ## TODO: Make this configurable.
471        $charset = Message::Charset::Info->get_by_html_name ('windows-1252');
472            ## NOTE: We choose |windows-1252| here, since |utf-8| should be
473            ## detectable in the step 6.
474        require Whatpm::Charset::DecodeHandle;
475        $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
476            ($byte_stream);
477        ($char_stream, $e_status)
478            = $charset->get_decode_handle ($buffer,
479                                           allow_error_reporting => 1,
480                                           allow_fallback => 1,
481                                           byte_buffer => \$byte_buffer);
482        $buffer->{buffer} = $byte_buffer;
483        !!!parse-error (type => 'sniffing:default',
484                        text => 'windows-1252',
485                        level => $self->{level}->{info},
486                        line => 1, column => 1,
487                        layer => 'encode');
488      $self->{confident} = 0;      $self->{confident} = 0;
489      } # SNIFFING
490    
491      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
492        $self->{input_encoding} = $charset->get_iana_name; ## TODO: Should we set actual charset decoder's encoding name?
493        !!!parse-error (type => 'chardecode:fallback',
494                        #text => $self->{input_encoding},
495                        level => $self->{level}->{uncertain},
496                        line => 1, column => 1,
497                        layer => 'encode');
498      } elsif (not ($e_status &
499                    Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
500        $self->{input_encoding} = $charset->get_iana_name;
501        !!!parse-error (type => 'chardecode:no error',
502                        text => $self->{input_encoding},
503                        level => $self->{level}->{uncertain},
504                        line => 1, column => 1,
505                        layer => 'encode');
506      } else {
507        $self->{input_encoding} = $charset->get_iana_name;
508    }    }
509    
510    $self->{change_encoding} = sub {    $self->{change_encoding} = sub {
511      my $self = shift;      my $self = shift;
512      my $charset = lc shift;      $charset_name = shift;
513      my $token = shift;      my $token = shift;
     ## TODO: if $charset is supported  
     ## TODO: normalize charset name  
514    
515      ## "Change the encoding" algorithm:      $charset = Message::Charset::Info->get_by_html_name ($charset_name);
516        ($char_stream, $e_status) = $charset->get_decode_handle
517            ($byte_stream, allow_error_reporting => 1, allow_fallback => 1,
518             byte_buffer => \ $buffer->{buffer});
519        
520        if ($char_stream) { # if supported
521          ## "Change the encoding" algorithm:
522    
523      ## Step 1            ## Step 1    
524      if ($charset eq 'utf-16') { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?        if ($charset->{category} &
525        $charset = 'utf-8';            Message::Charset::Info::CHARSET_CATEGORY_UTF16 ()) {
526      }          $charset = Message::Charset::Info->get_by_html_name ('utf-8');
527            ($char_stream, $e_status) = $charset->get_decode_handle
528                ($byte_stream,
529                 byte_buffer => \ $buffer->{buffer});
530          }
531          $charset_name = $charset->get_iana_name;
532          
533          ## Step 2
534          if (defined $self->{input_encoding} and
535              $self->{input_encoding} eq $charset_name) {
536            !!!parse-error (type => 'charset label:matching',
537                            text => $charset_name,
538                            level => $self->{level}->{info});
539            $self->{confident} = 1;
540            return;
541          }
542    
543      ## Step 2        !!!parse-error (type => 'charset label detected',
544      if (defined $self->{input_encoding} and                        text => $self->{input_encoding},
545          $self->{input_encoding} eq $charset) {                        value => $charset_name,
546        $self->{confident} = 1;                        level => $self->{level}->{warn},
547        return;                        token => $token);
548          
549          ## Step 3
550          # if (can) {
551            ## change the encoding on the fly.
552            #$self->{confident} = 1;
553            #return;
554          # }
555          
556          ## Step 4
557          throw Whatpm::HTML::RestartParser ();
558      }      }
559      }; # $self->{change_encoding}
560    
561      !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.    my $char_onerror = sub {
562          ':'.$charset, level => 'w', token => $token);      my (undef, $type, %opt) = @_;
563        !!!parse-error (layer => 'encode',
564      ## Step 3                      %opt, type => $type,
565      # if (can) {                      line => $self->{line}, column => $self->{column} + 1);
566        ## change the encoding on the fly.      if ($opt{octets}) {
567        #$self->{confident} = 1;        ${$opt{octets}} = "\x{FFFD}"; # relacement character
568        #return;      }
569      # }    };
570    
571      ## Step 4    my $wrapped_char_stream = $get_wrapper->($char_stream);
572      throw Whatpm::HTML::RestartParser (charset => $charset);    $wrapped_char_stream->onerror ($char_onerror);
   }; # $self->{change_encoding}  
573    
574    my @args = @_; shift @args; # $s    my @args = @_; shift @args; # $s
575    my $return;    my $return;
576    try {    try {
577      $return = $self->parse_char_string ($s, @args);        $return = $self->parse_char_stream ($wrapped_char_stream, @args);  
578    } catch Whatpm::HTML::RestartParser with {    } catch Whatpm::HTML::RestartParser with {
579      my $charset = shift->{charset};      ## NOTE: Invoked after {change_encoding}.
580      $s = \ (Encode::decode ($charset, $$bytes_s));      
581      $self->{input_encoding} = $charset; ## TODO: normalize      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
582          $self->{input_encoding} = $charset->get_iana_name; ## TODO: Should we set actual charset decoder's encoding name?
583          !!!parse-error (type => 'chardecode:fallback',
584                          level => $self->{level}->{uncertain},
585                          #text => $self->{input_encoding},
586                          line => 1, column => 1,
587                          layer => 'encode');
588        } elsif (not ($e_status &
589                      Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
590          $self->{input_encoding} = $charset->get_iana_name;
591          !!!parse-error (type => 'chardecode:no error',
592                          text => $self->{input_encoding},
593                          level => $self->{level}->{uncertain},
594                          line => 1, column => 1,
595                          layer => 'encode');
596        } else {
597          $self->{input_encoding} = $charset->get_iana_name;
598        }
599      $self->{confident} = 1;      $self->{confident} = 1;
600      $return = $self->parse_char_string ($s, @args);  
601        $wrapped_char_stream = $get_wrapper->($char_stream);
602        $wrapped_char_stream->onerror ($char_onerror);
603    
604        $return = $self->parse_char_stream ($wrapped_char_stream, @args);
605    };    };
606    return $return;    return $return;
607  } # parse_byte_string  } # parse_byte_stream
608    
609  ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM  ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
610  ## 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 615  sub parse_byte_string ($$$$;$) {
615  ## 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
616  ## strip the BOM and never strip any ZWNBSP.  ## strip the BOM and never strip any ZWNBSP.
617    
618  *parse_char_string = \&parse_string;  sub parse_char_string ($$$;$$) {
619      #my ($self, $s, $doc, $onerror, $get_wrapper) = @_;
620      my $self = shift;
621      require utf8;
622      my $s = ref $_[0] ? $_[0] : \($_[0]);
623      open my $input, '<' . (utf8::is_utf8 ($$s) ? ':utf8' : ''), $s;
624      if ($_[3]) {
625        $input = $_[3]->($input);
626      }
627      return $self->parse_char_stream ($input, @_[1..$#_]);
628    } # parse_char_string
629    *parse_string = \&parse_char_string; ## NOTE: Alias for backward compatibility.
630    
631  sub parse_string ($$$;$) {  sub parse_char_stream ($$$;$) {
632    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
633    my $s = ref $_[0] ? $_[0] : \($_[0]);    my $input = $_[0];
634    $self->{document} = $_[1];    $self->{document} = $_[1];
635    @{$self->{document}->child_nodes} = ();    @{$self->{document}->child_nodes} = ();
636    
# Line 186  sub parse_string ($$$;$) { Line 649  sub parse_string ($$$;$) {
649      pop @{$self->{prev_char}};      pop @{$self->{prev_char}};
650      unshift @{$self->{prev_char}}, $self->{next_char};      unshift @{$self->{prev_char}}, $self->{next_char};
651    
652      $self->{next_char} = -1 and return if $i >= length $$s;      my $char;
653      $self->{next_char} = ord substr $$s, $i++, 1;      if (defined $self->{next_next_char}) {
654          $char = $self->{next_next_char};
655          delete $self->{next_next_char};
656        } else {
657          $char = $input->getc;
658        }
659        $self->{next_char} = -1 and return unless defined $char;
660        $self->{next_char} = ord $char;
661    
662      ($self->{line_prev}, $self->{column_prev})      ($self->{line_prev}, $self->{column_prev})
663          = ($self->{line}, $self->{column});          = ($self->{line}, $self->{column});
664      $self->{column}++;      $self->{column}++;
665            
666      if ($self->{next_char} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
667          !!!cp ('j1');
668        $self->{line}++;        $self->{line}++;
669        $self->{column} = 0;        $self->{column} = 0;
670      } elsif ($self->{next_char} == 0x000D) { # CR      } elsif ($self->{next_char} == 0x000D) { # CR
671        $i++ if substr ($$s, $i, 1) eq "\x0A";        !!!cp ('j2');
672          my $next = $input->getc;
673          if (defined $next and $next ne "\x0A") {
674            $self->{next_next_char} = $next;
675          }
676        $self->{next_char} = 0x000A; # LF # MUST        $self->{next_char} = 0x000A; # LF # MUST
677        $self->{line}++;        $self->{line}++;
678        $self->{column} = 0;        $self->{column} = 0;
679      } elsif ($self->{next_char} > 0x10FFFF) {      } elsif ($self->{next_char} > 0x10FFFF) {
680          !!!cp ('j3');
681        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
682      } elsif ($self->{next_char} == 0x0000) { # NULL      } elsif ($self->{next_char} == 0x0000) { # NULL
683          !!!cp ('j4');
684        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
685        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
686        } elsif ($self->{next_char} <= 0x0008 or
687                 (0x000E <= $self->{next_char} and $self->{next_char} <= 0x001F) or
688                 (0x007F <= $self->{next_char} and $self->{next_char} <= 0x009F) or
689                 (0xD800 <= $self->{next_char} and $self->{next_char} <= 0xDFFF) or
690                 (0xFDD0 <= $self->{next_char} and $self->{next_char} <= 0xFDDF) or
691                 {
692                  0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
693                  0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
694                  0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
695                  0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
696                  0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
697                  0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
698                  0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
699                  0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
700                  0x10FFFE => 1, 0x10FFFF => 1,
701                 }->{$self->{next_char}}) {
702          !!!cp ('j5');
703          if ($self->{next_char} < 0x10000) {
704            !!!parse-error (type => 'control char',
705                            text => (sprintf 'U+%04X', $self->{next_char}));
706          } else {
707            !!!parse-error (type => 'control char',
708                            text => (sprintf 'U-%08X', $self->{next_char}));
709          }
710      }      }
711    };    };
712    $self->{prev_char} = [-1, -1, -1];    $self->{prev_char} = [-1, -1, -1];
# Line 229  sub parse_string ($$$;$) { Line 730  sub parse_string ($$$;$) {
730    delete $self->{parse_error}; # remove loop    delete $self->{parse_error}; # remove loop
731    
732    return $self->{document};    return $self->{document};
733  } # parse_string  } # parse_char_stream
734    
735  sub new ($) {  sub new ($) {
736    my $class = shift;    my $class = shift;
737    my $self = bless {}, $class;    my $self = bless {
738        level => {must => 'm',
739                  should => 's',
740                  warn => 'w',
741                  info => 'i',
742                  uncertain => 'u'},
743      }, $class;
744    $self->{set_next_char} = sub {    $self->{set_next_char} = sub {
745      $self->{next_char} = -1;      $self->{next_char} = -1;
746    };    };
# Line 295  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO Line 802  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO
802  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
803  sub BOGUS_DOCTYPE_STATE () { 32 }  sub BOGUS_DOCTYPE_STATE () { 32 }
804  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
805    sub SELF_CLOSING_START_TAG_STATE () { 34 }
806    sub CDATA_BLOCK_STATE () { 35 }
807    
808  sub DOCTYPE_TOKEN () { 1 }  sub DOCTYPE_TOKEN () { 1 }
809  sub COMMENT_TOKEN () { 2 }  sub COMMENT_TOKEN () { 2 }
# Line 312  sub ROW_IMS ()        { 0b10000000 } Line 821  sub ROW_IMS ()        { 0b10000000 }
821  sub BODY_AFTER_IMS () { 0b100000000 }  sub BODY_AFTER_IMS () { 0b100000000 }
822  sub FRAME_IMS ()      { 0b1000000000 }  sub FRAME_IMS ()      { 0b1000000000 }
823  sub SELECT_IMS ()     { 0b10000000000 }  sub SELECT_IMS ()     { 0b10000000000 }
824    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
825        ## NOTE: "in foreign content" insertion mode is special; it is combined
826        ## with the secondary insertion mode.  In this parser, they are stored
827        ## together in the bit-or'ed form.
828    
829  ## NOTE: "initial" and "before html" insertion modes have no constants.  ## NOTE: "initial" and "before html" insertion modes have no constants.
830    
# Line 348  sub _initialize_tokenizer ($) { Line 861  sub _initialize_tokenizer ($) {
861    undef $self->{current_attribute};    undef $self->{current_attribute};
862    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
863    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
864      delete $self->{self_closing};
865    $self->{char} = [];    $self->{char} = [];
866    # $self->{next_char}    # $self->{next_char}
867    !!!next-input-character;    !!!next-input-character;
# Line 368  sub _initialize_tokenizer ($) { Line 882  sub _initialize_tokenizer ($) {
882  ##        ->{value}  ##        ->{value}
883  ##        ->{has_reference} == 1 or 0  ##        ->{has_reference} == 1 or 0
884  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
885    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
886    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
887    ##     while the token is pushed back to the stack.
888    
889  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
890    
# Line 394  sub _initialize_tokenizer ($) { Line 911  sub _initialize_tokenizer ($) {
911    
912  sub _get_next_token ($) {  sub _get_next_token ($) {
913    my $self = shift;    my $self = shift;
914    
915      if ($self->{self_closing}) {
916        !!!parse-error (type => 'nestc', token => $self->{current_token});
917        ## NOTE: The |self_closing| flag is only set by start tag token.
918        ## In addition, when a start tag token is emitted, it is always set to
919        ## |current_token|.
920        delete $self->{self_closing};
921      }
922    
923    if (@{$self->{token}}) {    if (@{$self->{token}}) {
924        $self->{self_closing} = $self->{token}->[0]->{self_closing};
925      return shift @{$self->{token}};      return shift @{$self->{token}};
926    }    }
927    
# Line 466  sub _get_next_token ($) { Line 993  sub _get_next_token ($) {
993        # Anything else        # Anything else
994        my $token = {type => CHARACTER_TOKEN,        my $token = {type => CHARACTER_TOKEN,
995                     data => chr $self->{next_char},                     data => chr $self->{next_char},
996                     line => $self->{line}, column => $self->{column}};                     line => $self->{line}, column => $self->{column},
997                      };
998        ## Stay in the data state        ## Stay in the data state
999        !!!next-input-character;        !!!next-input-character;
1000    
# Line 486  sub _get_next_token ($) { Line 1014  sub _get_next_token ($) {
1014        unless (defined $token) {        unless (defined $token) {
1015          !!!cp (13);          !!!cp (13);
1016          !!!emit ({type => CHARACTER_TOKEN, data => '&',          !!!emit ({type => CHARACTER_TOKEN, data => '&',
1017                    line => $l, column => $c});                    line => $l, column => $c,
1018                     });
1019        } else {        } else {
1020          !!!cp (14);          !!!cp (14);
1021          !!!emit ($token);          !!!emit ($token);
# Line 507  sub _get_next_token ($) { Line 1036  sub _get_next_token ($) {
1036    
1037            !!!emit ({type => CHARACTER_TOKEN, data => '<',            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1038                      line => $self->{line_prev},                      line => $self->{line_prev},
1039                      column => $self->{column_prev}});                      column => $self->{column_prev},
1040                       });
1041    
1042            redo A;            redo A;
1043          }          }
# Line 553  sub _get_next_token ($) { Line 1083  sub _get_next_token ($) {
1083    
1084            !!!emit ({type => CHARACTER_TOKEN, data => '<>',            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
1085                      line => $self->{line_prev},                      line => $self->{line_prev},
1086                      column => $self->{column_prev}});                      column => $self->{column_prev},
1087                       });
1088    
1089            redo A;            redo A;
1090          } elsif ($self->{next_char} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
# Line 564  sub _get_next_token ($) { Line 1095  sub _get_next_token ($) {
1095            $self->{state} = BOGUS_COMMENT_STATE;            $self->{state} = BOGUS_COMMENT_STATE;
1096            $self->{current_token} = {type => COMMENT_TOKEN, data => '',            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1097                                      line => $self->{line_prev},                                      line => $self->{line_prev},
1098                                      column => $self->{column_prev}};                                      column => $self->{column_prev},
1099                                       };
1100            ## $self->{next_char} is intentionally left as is            ## $self->{next_char} is intentionally left as is
1101            redo A;            redo A;
1102          } else {          } else {
1103            !!!cp (23);            !!!cp (23);
1104            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago',
1105                              line => $self->{line_prev},
1106                              column => $self->{column_prev});
1107            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1108            ## reconsume            ## reconsume
1109    
1110            !!!emit ({type => CHARACTER_TOKEN, data => '<',            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1111                      line => $self->{line_prev},                      line => $self->{line_prev},
1112                      column => $self->{column_prev}});                      column => $self->{column_prev},
1113                       });
1114    
1115            redo A;            redo A;
1116          }          }
# Line 604  sub _get_next_token ($) { Line 1139  sub _get_next_token ($) {
1139                $self->{state} = DATA_STATE;                $self->{state} = DATA_STATE;
1140    
1141                !!!emit ({type => CHARACTER_TOKEN, data => '</',                !!!emit ({type => CHARACTER_TOKEN, data => '</',
1142                          line => $l, column => $c});                          line => $l, column => $c,
1143                           });
1144        
1145                redo A;                redo A;
1146              }              }
# Line 624  sub _get_next_token ($) { Line 1160  sub _get_next_token ($) {
1160              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
1161              $self->{state} = DATA_STATE;              $self->{state} = DATA_STATE;
1162              !!!emit ({type => CHARACTER_TOKEN, data => '</',              !!!emit ({type => CHARACTER_TOKEN, data => '</',
1163                        line => $l, column => $c});                        line => $l, column => $c,
1164                         });
1165              redo A;              redo A;
1166            } else {            } else {
1167              !!!cp (27);              !!!cp (27);
# Line 638  sub _get_next_token ($) { Line 1175  sub _get_next_token ($) {
1175            # next-input-character is already done            # next-input-character is already done
1176            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1177            !!!emit ({type => CHARACTER_TOKEN, data => '</',            !!!emit ({type => CHARACTER_TOKEN, data => '</',
1178                      line => $l, column => $c});                      line => $l, column => $c,
1179                       });
1180            redo A;            redo A;
1181          }          }
1182        }        }
# Line 677  sub _get_next_token ($) { Line 1215  sub _get_next_token ($) {
1215          # reconsume          # reconsume
1216    
1217          !!!emit ({type => CHARACTER_TOKEN, data => '</',          !!!emit ({type => CHARACTER_TOKEN, data => '</',
1218                    line => $l, column => $c});                    line => $l, column => $c,
1219                     });
1220    
1221          redo A;          redo A;
1222        } else {        } else {
# Line 686  sub _get_next_token ($) { Line 1225  sub _get_next_token ($) {
1225          $self->{state} = BOGUS_COMMENT_STATE;          $self->{state} = BOGUS_COMMENT_STATE;
1226          $self->{current_token} = {type => COMMENT_TOKEN, data => '',          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1227                                    line => $self->{line_prev}, # "<" of "</"                                    line => $self->{line_prev}, # "<" of "</"
1228                                    column => $self->{column_prev} - 1};                                    column => $self->{column_prev} - 1,
1229                                     };
1230          ## $self->{next_char} is intentionally left as is          ## $self->{next_char} is intentionally left as is
1231          redo A;          redo A;
1232        }        }
# Line 703  sub _get_next_token ($) { Line 1243  sub _get_next_token ($) {
1243        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1244          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1245            !!!cp (35);            !!!cp (35);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1246            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1247          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1248            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 736  sub _get_next_token ($) { Line 1274  sub _get_next_token ($) {
1274          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1275          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1276            !!!cp (39);            !!!cp (39);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1277            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1278          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1279            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 758  sub _get_next_token ($) { Line 1294  sub _get_next_token ($) {
1294    
1295          redo A;          redo A;
1296        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1297            !!!cp (42);
1298            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1299          !!!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  
1300          redo A;          redo A;
1301        } else {        } else {
1302          !!!cp (44);          !!!cp (44);
# Line 793  sub _get_next_token ($) { Line 1319  sub _get_next_token ($) {
1319        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1320          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1321            !!!cp (46);            !!!cp (46);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1322            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1323          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1324            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 816  sub _get_next_token ($) { Line 1340  sub _get_next_token ($) {
1340        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{next_char} and
1341                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1342          !!!cp (49);          !!!cp (49);
1343          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{current_attribute}
1344                                value => ''};              = {name => chr ($self->{next_char} + 0x0020),
1345                   value => '',
1346                   line => $self->{line}, column => $self->{column}};
1347          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1348          !!!next-input-character;          !!!next-input-character;
1349          redo A;          redo A;
1350        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1351            !!!cp (50);
1352            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1353          !!!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  
1354          redo A;          redo A;
1355        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1356          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1357          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1358            !!!cp (52);            !!!cp (52);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1359            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1360          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1361            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 871  sub _get_next_token ($) { Line 1385  sub _get_next_token ($) {
1385          } else {          } else {
1386            !!!cp (56);            !!!cp (56);
1387          }          }
1388          $self->{current_attribute} = {name => chr ($self->{next_char}),          $self->{current_attribute}
1389                                value => ''};              = {name => chr ($self->{next_char}),
1390                   value => '',
1391                   line => $self->{line}, column => $self->{column}};
1392          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1393          !!!next-input-character;          !!!next-input-character;
1394          redo A;          redo A;
# Line 882  sub _get_next_token ($) { Line 1398  sub _get_next_token ($) {
1398          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1399              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1400            !!!cp (57);            !!!cp (57);
1401            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});            !!!parse-error (type => 'duplicate attribute', text => $self->{current_attribute}->{name}, line => $self->{current_attribute}->{line}, column => $self->{current_attribute}->{column});
1402            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1403          } else {          } else {
1404            !!!cp (58);            !!!cp (58);
# Line 911  sub _get_next_token ($) { Line 1427  sub _get_next_token ($) {
1427          $before_leave->();          $before_leave->();
1428          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1429            !!!cp (61);            !!!cp (61);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1430            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1431          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1432            !!!cp (62);            !!!cp (62);
# Line 937  sub _get_next_token ($) { Line 1451  sub _get_next_token ($) {
1451          !!!next-input-character;          !!!next-input-character;
1452          redo A;          redo A;
1453        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1454            !!!cp (64);
1455          $before_leave->();          $before_leave->();
1456            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1457          !!!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  
1458          redo A;          redo A;
1459        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1460          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1461          $before_leave->();          $before_leave->();
1462          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1463            !!!cp (66);            !!!cp (66);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1464            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1465          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1466            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1009  sub _get_next_token ($) { Line 1511  sub _get_next_token ($) {
1511        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1512          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1513            !!!cp (73);            !!!cp (73);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1514            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1515          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1516            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1033  sub _get_next_token ($) { Line 1533  sub _get_next_token ($) {
1533        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{next_char} and
1534                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1535          !!!cp (76);          !!!cp (76);
1536          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{current_attribute}
1537                                value => ''};              = {name => chr ($self->{next_char} + 0x0020),
1538                   value => '',
1539                   line => $self->{line}, column => $self->{column}};
1540          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1541          !!!next-input-character;          !!!next-input-character;
1542          redo A;          redo A;
1543        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1544            !!!cp (77);
1545            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1546          !!!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  
1547          redo A;          redo A;
1548        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1549          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1550          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1551            !!!cp (79);            !!!cp (79);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1552            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1553          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1554            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1080  sub _get_next_token ($) { Line 1569  sub _get_next_token ($) {
1569    
1570          redo A;          redo A;
1571        } else {        } else {
1572          !!!cp (82);          if ($self->{next_char} == 0x0022 or # "
1573          $self->{current_attribute} = {name => chr ($self->{next_char}),              $self->{next_char} == 0x0027) { # '
1574                                value => ''};            !!!cp (78);
1575              !!!parse-error (type => 'bad attribute name');
1576            } else {
1577              !!!cp (82);
1578            }
1579            $self->{current_attribute}
1580                = {name => chr ($self->{next_char}),
1581                   value => '',
1582                   line => $self->{line}, column => $self->{column}};
1583          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1584          !!!next-input-character;          !!!next-input-character;
1585          redo A;                  redo A;        
# Line 1113  sub _get_next_token ($) { Line 1610  sub _get_next_token ($) {
1610          !!!next-input-character;          !!!next-input-character;
1611          redo A;          redo A;
1612        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1613            !!!parse-error (type => 'empty unquoted attribute value');
1614          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1615            !!!cp (87);            !!!cp (87);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1616            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1617          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1618            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1140  sub _get_next_token ($) { Line 1636  sub _get_next_token ($) {
1636          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1637          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1638            !!!cp (90);            !!!cp (90);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1639            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1640          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1641            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1189  sub _get_next_token ($) { Line 1683  sub _get_next_token ($) {
1683          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1684          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1685            !!!cp (97);            !!!cp (97);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1686            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1687          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1688            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1233  sub _get_next_token ($) { Line 1725  sub _get_next_token ($) {
1725          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1726          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1727            !!!cp (103);            !!!cp (103);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1728            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1729          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1730            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1280  sub _get_next_token ($) { Line 1770  sub _get_next_token ($) {
1770        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1771          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1772            !!!cp (109);            !!!cp (109);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1773            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1774          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1775            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1305  sub _get_next_token ($) { Line 1793  sub _get_next_token ($) {
1793          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1794          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1795            !!!cp (112);            !!!cp (112);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1796            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1797          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1798            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1377  sub _get_next_token ($) { Line 1863  sub _get_next_token ($) {
1863        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1864          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1865            !!!cp (119);            !!!cp (119);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1866            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1867          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1868            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1399  sub _get_next_token ($) { Line 1883  sub _get_next_token ($) {
1883    
1884          redo A;          redo A;
1885        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1886            !!!cp (122);
1887            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1888          !!!next-input-character;          !!!next-input-character;
1889          if ($self->{next_char} == 0x003E and # >          redo A;
1890              $self->{current_token}->{type} == START_TAG_TOKEN and        } elsif ($self->{next_char} == -1) {
1891              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {          !!!parse-error (type => 'unclosed tag');
1892            # permitted slash          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1893            !!!cp (122);            !!!cp (122.3);
1894            #            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1895            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1896              if ($self->{current_token}->{attributes}) {
1897                !!!cp (122.1);
1898                !!!parse-error (type => 'end tag attribute');
1899              } else {
1900                ## NOTE: This state should never be reached.
1901                !!!cp (122.2);
1902              }
1903          } else {          } else {
1904            !!!cp (123);            die "$0: $self->{current_token}->{type}: Unknown token type";
           !!!parse-error (type => 'nestc');  
1905          }          }
1906          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = DATA_STATE;
1907          # next-input-character is already done          ## Reconsume.
1908            !!!emit ($self->{current_token}); # start tag or end tag
1909          redo A;          redo A;
1910        } else {        } else {
1911          !!!cp (124);          !!!cp ('124.1');
1912          !!!parse-error (type => 'no space between attributes');          !!!parse-error (type => 'no space between attributes');
1913          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1914          ## reconsume          ## reconsume
1915          redo A;          redo A;
1916        }        }
1917        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1918          if ($self->{next_char} == 0x003E) { # >
1919            if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1920              !!!cp ('124.2');
1921              !!!parse-error (type => 'nestc', token => $self->{current_token});
1922              ## TODO: Different type than slash in start tag
1923              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1924              if ($self->{current_token}->{attributes}) {
1925                !!!cp ('124.4');
1926                !!!parse-error (type => 'end tag attribute');
1927              } else {
1928                !!!cp ('124.5');
1929              }
1930              ## TODO: Test |<title></title/>|
1931            } else {
1932              !!!cp ('124.3');
1933              $self->{self_closing} = 1;
1934            }
1935    
1936            $self->{state} = DATA_STATE;
1937            !!!next-input-character;
1938    
1939            !!!emit ($self->{current_token}); # start tag or end tag
1940    
1941            redo A;
1942          } elsif ($self->{next_char} == -1) {
1943            !!!parse-error (type => 'unclosed tag');
1944            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1945              !!!cp (124.7);
1946              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1947            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1948              if ($self->{current_token}->{attributes}) {
1949                !!!cp (124.5);
1950                !!!parse-error (type => 'end tag attribute');
1951              } else {
1952                ## NOTE: This state should never be reached.
1953                !!!cp (124.6);
1954              }
1955            } else {
1956              die "$0: $self->{current_token}->{type}: Unknown token type";
1957            }
1958            $self->{state} = DATA_STATE;
1959            ## Reconsume.
1960            !!!emit ($self->{current_token}); # start tag or end tag
1961            redo A;
1962          } else {
1963            !!!cp ('124.4');
1964            !!!parse-error (type => 'nestc');
1965            ## TODO: This error type is wrong.
1966            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1967            ## Reconsume.
1968            redo A;
1969          }
1970      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1971        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1972                
# Line 1466  sub _get_next_token ($) { Line 2013  sub _get_next_token ($) {
2013          if ($self->{next_char} == 0x002D) { # -          if ($self->{next_char} == 0x002D) { # -
2014            !!!cp (127);            !!!cp (127);
2015            $self->{current_token} = {type => COMMENT_TOKEN, data => '',            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2016                                      line => $l, column => $c};                                      line => $l, column => $c,
2017                                       };
2018            $self->{state} = COMMENT_START_STATE;            $self->{state} = COMMENT_START_STATE;
2019            !!!next-input-character;            !!!next-input-character;
2020            redo A;            redo A;
# Line 1504  sub _get_next_token ($) { Line 2052  sub _get_next_token ($) {
2052                      $self->{state} = DOCTYPE_STATE;                      $self->{state} = DOCTYPE_STATE;
2053                      $self->{current_token} = {type => DOCTYPE_TOKEN,                      $self->{current_token} = {type => DOCTYPE_TOKEN,
2054                                                quirks => 1,                                                quirks => 1,
2055                                                line => $l, column => $c};                                                line => $l, column => $c,
2056                                                 };
2057                      !!!next-input-character;                      !!!next-input-character;
2058                      redo A;                      redo A;
2059                    } else {                    } else {
# Line 1525  sub _get_next_token ($) { Line 2074  sub _get_next_token ($) {
2074          } else {          } else {
2075            !!!cp (135);            !!!cp (135);
2076          }          }
2077          } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
2078                   $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
2079                   $self->{next_char} == 0x005B) { # [
2080            !!!next-input-character;
2081            push @next_char, $self->{next_char};
2082            if ($self->{next_char} == 0x0043) { # C
2083              !!!next-input-character;
2084              push @next_char, $self->{next_char};
2085              if ($self->{next_char} == 0x0044) { # D
2086                !!!next-input-character;
2087                push @next_char, $self->{next_char};
2088                if ($self->{next_char} == 0x0041) { # A
2089                  !!!next-input-character;
2090                  push @next_char, $self->{next_char};
2091                  if ($self->{next_char} == 0x0054) { # T
2092                    !!!next-input-character;
2093                    push @next_char, $self->{next_char};
2094                    if ($self->{next_char} == 0x0041) { # A
2095                      !!!next-input-character;
2096                      push @next_char, $self->{next_char};
2097                      if ($self->{next_char} == 0x005B) { # [
2098                        !!!cp (135.1);
2099                        $self->{state} = CDATA_BLOCK_STATE;
2100                        !!!next-input-character;
2101                        redo A;
2102                      } else {
2103                        !!!cp (135.2);
2104                      }
2105                    } else {
2106                      !!!cp (135.3);
2107                    }
2108                  } else {
2109                    !!!cp (135.4);                
2110                  }
2111                } else {
2112                  !!!cp (135.5);
2113                }
2114              } else {
2115                !!!cp (135.6);
2116              }
2117            } else {
2118              !!!cp (135.7);
2119            }
2120        } else {        } else {
2121          !!!cp (136);          !!!cp (136);
2122        }        }
# Line 1534  sub _get_next_token ($) { Line 2126  sub _get_next_token ($) {
2126        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
2127        $self->{state} = BOGUS_COMMENT_STATE;        $self->{state} = BOGUS_COMMENT_STATE;
2128        $self->{current_token} = {type => COMMENT_TOKEN, data => '',        $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2129                                  line => $l, column => $c};                                  line => $l, column => $c,
2130                                   };
2131        redo A;        redo A;
2132                
2133        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
# Line 2123  sub _get_next_token ($) { Line 2716  sub _get_next_token ($) {
2716          redo A;          redo A;
2717        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2718          !!!cp (208);          !!!cp (208);
2719          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2720    
2721          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2722          !!!next-input-character;          !!!next-input-character;
# Line 2159  sub _get_next_token ($) { Line 2752  sub _get_next_token ($) {
2752          redo A;          redo A;
2753        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2754          !!!cp (212);          !!!cp (212);
2755          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2756    
2757          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2758          !!!next-input-character;          !!!next-input-character;
# Line 2207  sub _get_next_token ($) { Line 2800  sub _get_next_token ($) {
2800        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
2801          !!!cp (217);          !!!cp (217);
2802          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
   
2803          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2804          ## reconsume          ## reconsume
2805    
# Line 2248  sub _get_next_token ($) { Line 2840  sub _get_next_token ($) {
2840          !!!next-input-character;          !!!next-input-character;
2841          redo A;          redo A;
2842        }        }
2843        } elsif ($self->{state} == CDATA_BLOCK_STATE) {
2844          my $s = '';
2845          
2846          my ($l, $c) = ($self->{line}, $self->{column});
2847    
2848          CS: while ($self->{next_char} != -1) {
2849            if ($self->{next_char} == 0x005D) { # ]
2850              !!!next-input-character;
2851              if ($self->{next_char} == 0x005D) { # ]
2852                !!!next-input-character;
2853                MDC: {
2854                  if ($self->{next_char} == 0x003E) { # >
2855                    !!!cp (221.1);
2856                    !!!next-input-character;
2857                    last CS;
2858                  } elsif ($self->{next_char} == 0x005D) { # ]
2859                    !!!cp (221.2);
2860                    $s .= ']';
2861                    !!!next-input-character;
2862                    redo MDC;
2863                  } else {
2864                    !!!cp (221.3);
2865                    $s .= ']]';
2866                    #
2867                  }
2868                } # MDC
2869              } else {
2870                !!!cp (221.4);
2871                $s .= ']';
2872                #
2873              }
2874            } else {
2875              !!!cp (221.5);
2876              #
2877            }
2878            $s .= chr $self->{next_char};
2879            !!!next-input-character;
2880          } # CS
2881    
2882          $self->{state} = DATA_STATE;
2883          ## next-input-character done or EOF, which is reconsumed.
2884    
2885          if (length $s) {
2886            !!!cp (221.6);
2887            !!!emit ({type => CHARACTER_TOKEN, data => $s,
2888                      line => $l, column => $c});
2889          } else {
2890            !!!cp (221.7);
2891          }
2892    
2893          redo A;
2894    
2895          ## ISSUE: "text tokens" in spec.
2896          ## TODO: Streaming support
2897      } else {      } else {
2898        die "$0: $self->{state}: Unknown state";        die "$0: $self->{state}: Unknown state";
2899      }      }
# Line 2315  sub _tokenize_attempt_to_consume_an_enti Line 2961  sub _tokenize_attempt_to_consume_an_enti
2961    
2962          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2963            !!!cp (1008);            !!!cp (1008);
2964            !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);            !!!parse-error (type => 'invalid character reference',
2965                              text => (sprintf 'U+%04X', $code),
2966                              line => $l, column => $c);
2967            $code = 0xFFFD;            $code = 0xFFFD;
2968          } elsif ($code > 0x10FFFF) {          } elsif ($code > 0x10FFFF) {
2969            !!!cp (1009);            !!!cp (1009);
2970            !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);            !!!parse-error (type => 'invalid character reference',
2971                              text => (sprintf 'U-%08X', $code),
2972                              line => $l, column => $c);
2973            $code = 0xFFFD;            $code = 0xFFFD;
2974          } elsif ($code == 0x000D) {          } elsif ($code == 0x000D) {
2975            !!!cp (1010);            !!!cp (1010);
# Line 2327  sub _tokenize_attempt_to_consume_an_enti Line 2977  sub _tokenize_attempt_to_consume_an_enti
2977            $code = 0x000A;            $code = 0x000A;
2978          } elsif (0x80 <= $code and $code <= 0x9F) {          } elsif (0x80 <= $code and $code <= 0x9F) {
2979            !!!cp (1011);            !!!cp (1011);
2980            !!!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);
2981            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
2982          }          }
2983    
2984          return {type => CHARACTER_TOKEN, data => chr $code,          return {type => CHARACTER_TOKEN, data => chr $code,
2985                  has_reference => 1, line => $l, column => $c};                  has_reference => 1,
2986                    line => $l, column => $c,
2987                   };
2988        } # X        } # X
2989      } elsif (0x0030 <= $self->{next_char} and      } elsif (0x0030 <= $self->{next_char} and
2990               $self->{next_char} <= 0x0039) { # 0..9               $self->{next_char} <= 0x0039) { # 0..9
# Line 2358  sub _tokenize_attempt_to_consume_an_enti Line 3010  sub _tokenize_attempt_to_consume_an_enti
3010    
3011        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3012          !!!cp (1015);          !!!cp (1015);
3013          !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);          !!!parse-error (type => 'invalid character reference',
3014                            text => (sprintf 'U+%04X', $code),
3015                            line => $l, column => $c);
3016          $code = 0xFFFD;          $code = 0xFFFD;
3017        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
3018          !!!cp (1016);          !!!cp (1016);
3019          !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);          !!!parse-error (type => 'invalid character reference',
3020                            text => (sprintf 'U-%08X', $code),
3021                            line => $l, column => $c);
3022          $code = 0xFFFD;          $code = 0xFFFD;
3023        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
3024          !!!cp (1017);          !!!cp (1017);
3025          !!!parse-error (type => 'CR character reference', line => $l, column => $c);          !!!parse-error (type => 'CR character reference',
3026                            line => $l, column => $c);
3027          $code = 0x000A;          $code = 0x000A;
3028        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
3029          !!!cp (1018);          !!!cp (1018);
3030          !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);          !!!parse-error (type => 'C1 character reference',
3031                            text => (sprintf 'U+%04X', $code),
3032                            line => $l, column => $c);
3033          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
3034        }        }
3035                
3036        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,
3037                line => $l, column => $c};                line => $l, column => $c,
3038                 };
3039      } else {      } else {
3040        !!!cp (1019);        !!!cp (1019);
3041        !!!parse-error (type => 'bare nero', line => $l, column => $c);        !!!parse-error (type => 'bare nero', line => $l, column => $c);
# Line 2395  sub _tokenize_attempt_to_consume_an_enti Line 3055  sub _tokenize_attempt_to_consume_an_enti
3055      require Whatpm::_NamedEntityList;      require Whatpm::_NamedEntityList;
3056      our $EntityChar;      our $EntityChar;
3057    
3058      while (length $entity_name < 10 and      while (length $entity_name < 30 and
3059             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
3060             ((0x0041 <= $self->{next_char} and # a             ((0x0041 <= $self->{next_char} and # a
3061               $self->{next_char} <= 0x005A) or # x               $self->{next_char} <= 0x005A) or # x
# Line 2429  sub _tokenize_attempt_to_consume_an_enti Line 3089  sub _tokenize_attempt_to_consume_an_enti
3089      if ($match > 0) {      if ($match > 0) {
3090        !!!cp (1023);        !!!cp (1023);
3091        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
3092                line => $l, column => $c};                line => $l, column => $c,
3093                 };
3094      } elsif ($match < 0) {      } elsif ($match < 0) {
3095        !!!parse-error (type => 'no refc', line => $l, column => $c);        !!!parse-error (type => 'no refc', line => $l, column => $c);
3096        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
3097          !!!cp (1024);          !!!cp (1024);
3098          return {type => CHARACTER_TOKEN, data => '&'.$entity_name,          return {type => CHARACTER_TOKEN, data => '&'.$entity_name,
3099                  line => $l, column => $c};                  line => $l, column => $c,
3100                   };
3101        } else {        } else {
3102          !!!cp (1025);          !!!cp (1025);
3103          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
3104                  line => $l, column => $c};                  line => $l, column => $c,
3105                   };
3106        }        }
3107      } else {      } else {
3108        !!!cp (1026);        !!!cp (1026);
3109        !!!parse-error (type => 'bare ero', line => $l, column => $c);        !!!parse-error (type => 'bare ero', line => $l, column => $c);
3110        ## NOTE: "No characters are consumed" in the spec.        ## NOTE: "No characters are consumed" in the spec.
3111        return {type => CHARACTER_TOKEN, data => '&'.$value,        return {type => CHARACTER_TOKEN, data => '&'.$value,
3112                line => $l, column => $c};                line => $l, column => $c,
3113                 };
3114      }      }
3115    } else {    } else {
3116      !!!cp (1027);      !!!cp (1027);
# Line 2463  sub _initialize_tree_constructor ($) { Line 3127  sub _initialize_tree_constructor ($) {
3127    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
3128    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
3129    $self->{document}->manakai_is_html (1); # MUST    $self->{document}->manakai_is_html (1); # MUST
3130      $self->{document}->set_user_data (manakai_source_line => 1);
3131      $self->{document}->set_user_data (manakai_source_column => 1);
3132  } # _initialize_tree_constructor  } # _initialize_tree_constructor
3133    
3134  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 2517  sub _tree_construction_initial ($) { Line 3183  sub _tree_construction_initial ($) {
3183        ## language.        ## language.
3184        my $doctype_name = $token->{name};        my $doctype_name = $token->{name};
3185        $doctype_name = '' unless defined $doctype_name;        $doctype_name = '' unless defined $doctype_name;
3186        $doctype_name =~ tr/a-z/A-Z/;        $doctype_name =~ tr/a-z/A-Z/; # ASCII case-insensitive
3187        if (not defined $token->{name} or # <!DOCTYPE>        if (not defined $token->{name} or # <!DOCTYPE>
           defined $token->{public_identifier} or  
3188            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
3189          !!!cp ('t1');          !!!cp ('t1');
3190          !!!parse-error (type => 'not HTML5', token => $token);          !!!parse-error (type => 'not HTML5', token => $token);
3191        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
3192          !!!cp ('t2');          !!!cp ('t2');
         ## ISSUE: ASCII case-insensitive? (in fact it does not matter)  
3193          !!!parse-error (type => 'not HTML5', token => $token);          !!!parse-error (type => 'not HTML5', token => $token);
3194          } elsif (defined $token->{public_identifier}) {
3195            if ($token->{public_identifier} eq 'XSLT-compat') {
3196              !!!cp ('t1.2');
3197              !!!parse-error (type => 'XSLT-compat', token => $token,
3198                              level => $self->{level}->{should});
3199            } else {
3200              !!!parse-error (type => 'not HTML5', token => $token);
3201            }
3202        } else {        } else {
3203          !!!cp ('t3');          !!!cp ('t3');
3204            #
3205        }        }
3206                
3207        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
3208          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3209          ## NOTE: Default value for both |public_id| and |system_id| attributes
3210          ## are empty strings, so that we don't set any value in missing cases.
3211        $doctype->public_id ($token->{public_identifier})        $doctype->public_id ($token->{public_identifier})
3212            if defined $token->{public_identifier};            if defined $token->{public_identifier};
3213        $doctype->system_id ($token->{system_identifier})        $doctype->system_id ($token->{system_identifier})
# Line 2547  sub _tree_construction_initial ($) { Line 3222  sub _tree_construction_initial ($) {
3222        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{public_identifier}) {
3223          my $pubid = $token->{public_identifier};          my $pubid = $token->{public_identifier};
3224          $pubid =~ tr/a-z/A-z/;          $pubid =~ tr/a-z/A-z/;
3225          if ({          my $prefix = [
3226            "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,            "+//SILMARIL//DTD HTML PRO V0R11 19970101//",
3227            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3228            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3229            "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 1//",
3230            "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 2//",
3231            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//",
3232            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//",
3233            "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT//",
3234            "-//IETF//DTD HTML 2.0//EN" => 1,            "-//IETF//DTD HTML 2.0//",
3235            "-//IETF//DTD HTML 2.1E//EN" => 1,            "-//IETF//DTD HTML 2.1E//",
3236            "-//IETF//DTD HTML 3.0//EN" => 1,            "-//IETF//DTD HTML 3.0//",
3237            "-//IETF//DTD HTML 3.0//EN//" => 1,            "-//IETF//DTD HTML 3.2 FINAL//",
3238            "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,            "-//IETF//DTD HTML 3.2//",
3239            "-//IETF//DTD HTML 3.2//EN" => 1,            "-//IETF//DTD HTML 3//",
3240            "-//IETF//DTD HTML 3//EN" => 1,            "-//IETF//DTD HTML LEVEL 0//",
3241            "-//IETF//DTD HTML LEVEL 0//EN" => 1,            "-//IETF//DTD HTML LEVEL 1//",
3242            "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,            "-//IETF//DTD HTML LEVEL 2//",
3243            "-//IETF//DTD HTML LEVEL 1//EN" => 1,            "-//IETF//DTD HTML LEVEL 3//",
3244            "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 0//",
3245            "-//IETF//DTD HTML LEVEL 2//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 1//",
3246            "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 2//",
3247            "-//IETF//DTD HTML LEVEL 3//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 3//",
3248            "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,            "-//IETF//DTD HTML STRICT//",
3249            "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,            "-//IETF//DTD HTML//",
3250            "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,            "-//METRIUS//DTD METRIUS PRESENTATIONAL//",
3251            "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//",
3252            "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//",
3253            "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//",
3254            "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//",
3255            "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//",
3256            "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//",
3257            "-//IETF//DTD HTML STRICT//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD HTML//",
3258            "-//IETF//DTD HTML STRICT//EN//2.0" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//",
3259            "-//IETF//DTD HTML STRICT//EN//3.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//",
3260            "-//IETF//DTD HTML//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//",
3261            "-//IETF//DTD HTML//EN//2.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//",
3262            "-//IETF//DTD HTML//EN//3.0" => 1,            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//",
3263            "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//",
3264            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//",
3265            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//",
3266            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//",
3267            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//",
3268            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,            "-//W3C//DTD HTML 3 1995-03-24//",
3269            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,            "-//W3C//DTD HTML 3.2 DRAFT//",
3270            "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,            "-//W3C//DTD HTML 3.2 FINAL//",
3271            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//W3C//DTD HTML 3.2//",
3272            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//W3C//DTD HTML 3.2S DRAFT//",
3273            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 FRAMESET//",
3274            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 TRANSITIONAL//",
3275            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMETNAL 19960712//",
3276            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMENTAL 970421//",
3277            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//W3C//DTD W3 HTML//",
3278            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//W3O//DTD W3 HTML 3.0//",
3279            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML 2.0//",
3280            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML//",
3281            "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,          ]; # $prefix
3282            "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,          my $match;
3283            "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,          for (@$prefix) {
3284            "-//W3C//DTD HTML 3.2//EN" => 1,            if (substr ($prefix, 0, length $_) eq $_) {
3285            "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,              $match = 1;
3286            "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,              last;
3287            "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,            }
3288            "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,          }
3289            "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,          if ($match or
3290            "-//W3C//DTD W3 HTML//EN" => 1,              $pubid eq "-//W3O//DTD W3 HTML STRICT 3.0//EN//" or
3291            "-//W3O//DTD W3 HTML 3.0//EN" => 1,              $pubid eq "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" or
3292            "-//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}) {  
3293            !!!cp ('t5');            !!!cp ('t5');
3294            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3295          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD HTML 4.01 FRAMESET//] or
3296                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD HTML 4.01 TRANSITIONAL//]) {
3297            if (defined $token->{system_identifier}) {            if (defined $token->{system_identifier}) {
3298              !!!cp ('t6');              !!!cp ('t6');
3299              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
# Line 2632  sub _tree_construction_initial ($) { Line 3301  sub _tree_construction_initial ($) {
3301              !!!cp ('t7');              !!!cp ('t7');
3302              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
3303            }            }
3304          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD XHTML 1.0 FRAMESET//] or
3305                   $pubid eq "-//W3C//DTD XHTML 1.0 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD XHTML 1.0 TRANSITIONAL//]) {
3306            !!!cp ('t8');            !!!cp ('t8');
3307            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
3308          } else {          } else {
# Line 2646  sub _tree_construction_initial ($) { Line 3315  sub _tree_construction_initial ($) {
3315          my $sysid = $token->{system_identifier};          my $sysid = $token->{system_identifier};
3316          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
3317          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") {
3318            ## TODO: Check the spec: PUBLIC "(limited quirks)" "(quirks)"            ## NOTE: Ensure that |PUBLIC "(limited quirks)" "(quirks)"| is
3319              ## marked as quirks.
3320            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3321            !!!cp ('t11');            !!!cp ('t11');
3322          } else {          } else {
# Line 2669  sub _tree_construction_initial ($) { Line 3339  sub _tree_construction_initial ($) {
3339        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3340        ## Go to the "before html" insertion mode.        ## Go to the "before html" insertion mode.
3341        ## reprocess        ## reprocess
3342          !!!ack-later;
3343        return;        return;
3344      } elsif ($token->{type} == CHARACTER_TOKEN) {      } elsif ($token->{type} == CHARACTER_TOKEN) {
3345        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
# Line 2749  sub _tree_construction_root_element ($) Line 3420  sub _tree_construction_root_element ($)
3420        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
3421          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
3422            my $root_element;            my $root_element;
3423            !!!create-element ($root_element, $token->{tag_name}, $token->{attributes}, $token);            !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3424            $self->{document}->append_child ($root_element);            $self->{document}->append_child ($root_element);
3425            push @{$self->{open_elements}}, [$root_element, 'html'];            push @{$self->{open_elements}},
3426                  [$root_element, $el_category->{html}];
3427    
3428            if ($token->{attributes}->{manifest}) {            if ($token->{attributes}->{manifest}) {
3429              !!!cp ('t24');              !!!cp ('t24');
3430              $self->{application_cache_selection}              $self->{application_cache_selection}
3431                  ->($token->{attributes}->{manifest}->{value});                  ->($token->{attributes}->{manifest}->{value});
3432              ## ISSUE: No relative reference resolution?              ## ISSUE: Spec is unclear on relative references.
3433                ## According to Hixie (#whatwg 2008-03-19), it should be
3434                ## resolved against the base URI of the document in HTML
3435                ## or xml:base of the element in XHTML.
3436            } else {            } else {
3437              !!!cp ('t25');              !!!cp ('t25');
3438              $self->{application_cache_selection}->(undef);              $self->{application_cache_selection}->(undef);
3439            }            }
3440    
3441              !!!nack ('t25c');
3442    
3443            !!!next-token;            !!!next-token;
3444            return; ## Go to the "before head" insertion mode.            return; ## Go to the "before head" insertion mode.
3445          } else {          } else {
# Line 2779  sub _tree_construction_root_element ($) Line 3456  sub _tree_construction_root_element ($)
3456          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
3457        }        }
3458    
3459      my $root_element; !!!create-element ($root_element, 'html',, $token);      my $root_element;
3460        !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3461      $self->{document}->append_child ($root_element);      $self->{document}->append_child ($root_element);
3462      push @{$self->{open_elements}}, [$root_element, 'html'];      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3463    
3464      $self->{application_cache_selection}->(undef);      $self->{application_cache_selection}->(undef);
3465    
3466      ## NOTE: Reprocess the token.      ## NOTE: Reprocess the token.
3467        !!!ack-later;
3468      return; ## Go to the "before head" insertion mode.      return; ## Go to the "before head" insertion mode.
3469    
3470      ## ISSUE: There is an issue in the spec      ## ISSUE: There is an issue in the spec
# Line 2809  sub _reset_insertion_mode ($) { Line 3488  sub _reset_insertion_mode ($) {
3488        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3489          $last = 1;          $last = 1;
3490          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
3491            if ($self->{inner_html_node}->[1] eq 'td' or            !!!cp ('t28');
3492                $self->{inner_html_node}->[1] eq 'th') {            $node = $self->{inner_html_node};
3493              !!!cp ('t27');          } else {
3494              #            die "_reset_insertion_mode: t27";
           } else {  
             !!!cp ('t28');  
             $node = $self->{inner_html_node};  
           }  
3495          }          }
3496        }        }
3497              
3498        ## Step 4..13        ## Step 4..14
3499        my $new_mode = {        my $new_mode;
3500          if ($node->[1] & FOREIGN_EL) {
3501            !!!cp ('t28.1');
3502            ## NOTE: Strictly spaking, the line below only applies to MathML and
3503            ## SVG elements.  Currently the HTML syntax supports only MathML and
3504            ## SVG elements as foreigners.
3505            $new_mode = IN_BODY_IM | IN_FOREIGN_CONTENT_IM;
3506          } elsif ($node->[1] & TABLE_CELL_EL) {
3507            if ($last) {
3508              !!!cp ('t28.2');
3509              #
3510            } else {
3511              !!!cp ('t28.3');
3512              $new_mode = IN_CELL_IM;
3513            }
3514          } else {
3515            !!!cp ('t28.4');
3516            $new_mode = {
3517                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
3518                        ## NOTE: |option| and |optgroup| do not set                        ## NOTE: |option| and |optgroup| do not set
3519                        ## insertion mode to "in select" by themselves.                        ## insertion mode to "in select" by themselves.
                       td => IN_CELL_IM,  
                       th => IN_CELL_IM,  
3520                        tr => IN_ROW_IM,                        tr => IN_ROW_IM,
3521                        tbody => IN_TABLE_BODY_IM,                        tbody => IN_TABLE_BODY_IM,
3522                        thead => IN_TABLE_BODY_IM,                        thead => IN_TABLE_BODY_IM,
# Line 2837  sub _reset_insertion_mode ($) { Line 3527  sub _reset_insertion_mode ($) {
3527                        head => IN_BODY_IM, # not in head!                        head => IN_BODY_IM, # not in head!
3528                        body => IN_BODY_IM,                        body => IN_BODY_IM,
3529                        frameset => IN_FRAMESET_IM,                        frameset => IN_FRAMESET_IM,
3530                       }->{$node->[1]};                       }->{$node->[0]->manakai_local_name};
3531          }
3532        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3533                
3534        ## Step 14        ## Step 15
3535        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3536          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3537            !!!cp ('t29');            !!!cp ('t29');
3538            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
# Line 2855  sub _reset_insertion_mode ($) { Line 3546  sub _reset_insertion_mode ($) {
3546          !!!cp ('t31');          !!!cp ('t31');
3547        }        }
3548                
3549        ## Step 15        ## Step 16
3550        $self->{insertion_mode} = IN_BODY_IM and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
3551                
3552        ## Step 16        ## Step 17
3553        $i--;        $i--;
3554        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3555                
3556        ## Step 17        ## Step 18
3557        redo S3;        redo S3;
3558      } # S3      } # S3
3559    
# Line 2974  sub _tree_construction_main ($) { Line 3665  sub _tree_construction_main ($) {
3665      ## Step 1      ## Step 1
3666      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3667      my $el;      my $el;
3668      !!!create-element ($el, $start_tag_name, $token->{attributes}, $token);      !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3669    
3670      ## Step 2      ## Step 2
3671      $insert->($el);      $insert->($el);
# Line 2985  sub _tree_construction_main ($) { Line 3676  sub _tree_construction_main ($) {
3676    
3677      ## Step 4      ## Step 4
3678      my $text = '';      my $text = '';
3679        !!!nack ('t40.1');
3680      !!!next-token;      !!!next-token;
3681      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3682        !!!cp ('t40');        !!!cp ('t40');
# Line 3011  sub _tree_construction_main ($) { Line 3703  sub _tree_construction_main ($) {
3703        ## NOTE: An end-of-file token.        ## NOTE: An end-of-file token.
3704        if ($content_model_flag == CDATA_CONTENT_MODEL) {        if ($content_model_flag == CDATA_CONTENT_MODEL) {
3705          !!!cp ('t43');          !!!cp ('t43');
3706          !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);          !!!parse-error (type => 'in CDATA:#eof', token => $token);
3707        } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {        } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3708          !!!cp ('t44');          !!!cp ('t44');
3709          !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);          !!!parse-error (type => 'in RCDATA:#eof', token => $token);
3710        } else {        } else {
3711          die "$0: $content_model_flag in parse_rcdata";          die "$0: $content_model_flag in parse_rcdata";
3712        }        }
# Line 3024  sub _tree_construction_main ($) { Line 3716  sub _tree_construction_main ($) {
3716    
3717    my $script_start_tag = sub () {    my $script_start_tag = sub () {
3718      my $script_el;      my $script_el;
3719      !!!create-element ($script_el, 'script', $token->{attributes}, $token);      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
3720      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3721    
3722      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
3723      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
3724            
3725      my $text = '';      my $text = '';
3726        !!!nack ('t45.1');
3727      !!!next-token;      !!!next-token;
3728      while ($token->{type} == CHARACTER_TOKEN) {      while ($token->{type} == CHARACTER_TOKEN) {
3729        !!!cp ('t45');        !!!cp ('t45');
# Line 3050  sub _tree_construction_main ($) { Line 3743  sub _tree_construction_main ($) {
3743        ## Ignore the token        ## Ignore the token
3744      } else {      } else {
3745        !!!cp ('t48');        !!!cp ('t48');
3746        !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);        !!!parse-error (type => 'in CDATA:#eof', token => $token);
3747        ## ISSUE: And ignore?        ## ISSUE: And ignore?
3748        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3749      }      }
# Line 3088  sub _tree_construction_main ($) { Line 3781  sub _tree_construction_main ($) {
3781        my $formatting_element;        my $formatting_element;
3782        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3783        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3784          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3785              !!!cp ('t52');
3786              last AFE;
3787            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
3788                         eq $tag_name) {
3789            !!!cp ('t51');            !!!cp ('t51');
3790            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3791            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3792            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           !!!cp ('t52');  
           last AFE;  
3793          }          }
3794        } # AFE        } # AFE
3795        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3796          !!!cp ('t53');          !!!cp ('t53');
3797          !!!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);
3798          ## Ignore the token          ## Ignore the token
3799          !!!next-token;          !!!next-token;
3800          return;          return;
# Line 3117  sub _tree_construction_main ($) { Line 3811  sub _tree_construction_main ($) {
3811              last INSCOPE;              last INSCOPE;
3812            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3813              !!!cp ('t55');              !!!cp ('t55');
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              ## Ignore the token              ## Ignore the token
3818              !!!next-token;              !!!next-token;
3819              return;              return;
3820            }            }
3821          } 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]}) {  
3822            !!!cp ('t56');            !!!cp ('t56');
3823            $in_scope = 0;            $in_scope = 0;
3824          }          }
3825        } # INSCOPE        } # INSCOPE
3826        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3827          !!!cp ('t57');          !!!cp ('t57');
3828          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},          !!!parse-error (type => 'unmatched end tag',
3829                            text => $token->{tag_name},
3830                          token => $end_tag_token);                          token => $end_tag_token);
3831          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3832          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
# Line 3141  sub _tree_construction_main ($) { Line 3834  sub _tree_construction_main ($) {
3834        }        }
3835        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3836          !!!cp ('t58');          !!!cp ('t58');
3837          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1],          !!!parse-error (type => 'not closed',
3838                            text => $self->{open_elements}->[-1]->[0]
3839                                ->manakai_local_name,
3840                          token => $end_tag_token);                          token => $end_tag_token);
3841        }        }
3842                
# Line 3150  sub _tree_construction_main ($) { Line 3845  sub _tree_construction_main ($) {
3845        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
3846        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3847          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3848          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
3849              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3850              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
3851               $scoping_category->{$node->[1]})) { ## Scoping is redundant, maybe               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
3852            !!!cp ('t59');            !!!cp ('t59');
3853            $furthest_block = $node;            $furthest_block = $node;
3854            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
# Line 3239  sub _tree_construction_main ($) { Line 3934  sub _tree_construction_main ($) {
3934        } # S7          } # S7  
3935                
3936        ## Step 8        ## Step 8
3937        if ({        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
            table => 1, tbody => 1, tfoot => 1, thead => 1, tr => 1,  
           }->{$common_ancestor_node->[1]}) {  
3938          my $foster_parent_element;          my $foster_parent_element;
3939          my $next_sibling;          my $next_sibling;
3940                           OE: for (reverse 0..$#{$self->{open_elements}}) {          OE: for (reverse 0..$#{$self->{open_elements}}) {
3941                             if ($self->{open_elements}->[$_]->[1] eq 'table') {            if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3942                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3943                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3944                                 !!!cp ('t65.1');                                 !!!cp ('t65.1');
# Line 3318  sub _tree_construction_main ($) { Line 4011  sub _tree_construction_main ($) {
4011    
4012    my $insert_to_foster = sub {    my $insert_to_foster = sub {
4013      my $child = shift;      my $child = shift;
4014      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]}) {  
4015        # MUST        # MUST
4016        my $foster_parent_element;        my $foster_parent_element;
4017        my $next_sibling;        my $next_sibling;
4018                           OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
4019                             if ($self->{open_elements}->[$_]->[1] eq 'table') {          if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
4020                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4021                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
4022                                 !!!cp ('t70');                                 !!!cp ('t70');
# Line 3350  sub _tree_construction_main ($) { Line 4041  sub _tree_construction_main ($) {
4041      }      }
4042    }; # $insert_to_foster    }; # $insert_to_foster
4043    
4044    B: {    B: while (1) {
4045      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
4046        !!!cp ('t73');        !!!cp ('t73');
4047        !!!parse-error (type => 'DOCTYPE in the middle', token => $token);        !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
4048        ## Ignore the token        ## Ignore the token
4049        ## Stay in the phase        ## Stay in the phase
4050        !!!next-token;        !!!next-token;
4051        redo B;        next B;
4052      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
4053               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
4054        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4055          !!!cp ('t79');          !!!cp ('t79');
4056          !!!parse-error (type => 'after html:html', token => $token);          !!!parse-error (type => 'after html', text => 'html', token => $token);
4057          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
4058        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4059          !!!cp ('t80');          !!!cp ('t80');
4060          !!!parse-error (type => 'after html:html', token => $token);          !!!parse-error (type => 'after html', text => 'html', token => $token);
4061          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
4062        } else {        } else {
4063          !!!cp ('t81');          !!!cp ('t81');
# Line 3383  sub _tree_construction_main ($) { Line 4074  sub _tree_construction_main ($) {
4074               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
4075          }          }
4076        }        }
4077          !!!nack ('t84.1');
4078        !!!next-token;        !!!next-token;
4079        redo B;        next B;
4080      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
4081        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
4082        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
# Line 3398  sub _tree_construction_main ($) { Line 4090  sub _tree_construction_main ($) {
4090          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
4091        }        }
4092        !!!next-token;        !!!next-token;
4093        redo B;        next B;
4094      } elsif ($self->{insertion_mode} & HEAD_IMS) {      } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
4095          if ($token->{type} == CHARACTER_TOKEN) {
4096            !!!cp ('t87.1');
4097            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4098            !!!next-token;
4099            next B;
4100          } elsif ($token->{type} == START_TAG_TOKEN) {
4101            if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
4102                 $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
4103                not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
4104                ($token->{tag_name} eq 'svg' and
4105                 $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
4106              ## NOTE: "using the rules for secondary insertion mode"then"continue"
4107              !!!cp ('t87.2');
4108              #
4109            } elsif ({
4110                      b => 1, big => 1, blockquote => 1, body => 1, br => 1,
4111                      center => 1, code => 1, dd => 1, div => 1, dl => 1, dt => 1,
4112                      em => 1, embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1,
4113                      h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, i => 1,
4114                      img => 1, li => 1, listing => 1, menu => 1, meta => 1,
4115                      nobr => 1, ol => 1, p => 1, pre => 1, ruby => 1, s => 1,
4116                      small => 1, span => 1, strong => 1, strike => 1, sub => 1,
4117                      sup => 1, table => 1, tt => 1, u => 1, ul => 1, var => 1,
4118                     }->{$token->{tag_name}}) {
4119              !!!cp ('t87.2');
4120              !!!parse-error (type => 'not closed',
4121                              text => $self->{open_elements}->[-1]->[0]
4122                                  ->manakai_local_name,
4123                              token => $token);
4124    
4125              pop @{$self->{open_elements}}
4126                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4127    
4128              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4129              ## Reprocess.
4130              next B;
4131            } else {
4132              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
4133              my $tag_name = $token->{tag_name};
4134              if ($nsuri eq $SVG_NS) {
4135                $tag_name = {
4136                   altglyph => 'altGlyph',
4137                   altglyphdef => 'altGlyphDef',
4138                   altglyphitem => 'altGlyphItem',
4139                   animatecolor => 'animateColor',
4140                   animatemotion => 'animateMotion',
4141                   animatetransform => 'animateTransform',
4142                   clippath => 'clipPath',
4143                   feblend => 'feBlend',
4144                   fecolormatrix => 'feColorMatrix',
4145                   fecomponenttransfer => 'feComponentTransfer',
4146                   fecomposite => 'feComposite',
4147                   feconvolvematrix => 'feConvolveMatrix',
4148                   fediffuselighting => 'feDiffuseLighting',
4149                   fedisplacementmap => 'feDisplacementMap',
4150                   fedistantlight => 'feDistantLight',
4151                   feflood => 'feFlood',
4152                   fefunca => 'feFuncA',
4153                   fefuncb => 'feFuncB',
4154                   fefuncg => 'feFuncG',
4155                   fefuncr => 'feFuncR',
4156                   fegaussianblur => 'feGaussianBlur',
4157                   feimage => 'feImage',
4158                   femerge => 'feMerge',
4159                   femergenode => 'feMergeNode',
4160                   femorphology => 'feMorphology',
4161                   feoffset => 'feOffset',
4162                   fepointlight => 'fePointLight',
4163                   fespecularlighting => 'feSpecularLighting',
4164                   fespotlight => 'feSpotLight',
4165                   fetile => 'feTile',
4166                   feturbulence => 'feTurbulence',
4167                   foreignobject => 'foreignObject',
4168                   glyphref => 'glyphRef',
4169                   lineargradient => 'linearGradient',
4170                   radialgradient => 'radialGradient',
4171                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4172                   textpath => 'textPath',  
4173                }->{$tag_name} || $tag_name;
4174              }
4175    
4176              ## "adjust SVG attributes" (SVG only) - done in insert-element-f
4177    
4178              ## "adjust foreign attributes" - done in insert-element-f
4179    
4180              !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
4181    
4182              if ($self->{self_closing}) {
4183                pop @{$self->{open_elements}};
4184                !!!ack ('t87.3');
4185              } else {
4186                !!!cp ('t87.4');
4187              }
4188    
4189              !!!next-token;
4190              next B;
4191            }
4192          } elsif ($token->{type} == END_TAG_TOKEN) {
4193            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4194            !!!cp ('t87.5');
4195            #
4196          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4197            !!!cp ('t87.6');
4198            !!!parse-error (type => 'not closed',
4199                            text => $self->{open_elements}->[-1]->[0]
4200                                ->manakai_local_name,
4201                            token => $token);
4202    
4203            pop @{$self->{open_elements}}
4204                while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4205    
4206            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4207            ## Reprocess.
4208            next B;
4209          } else {
4210            die "$0: $token->{type}: Unknown token type";        
4211          }
4212        }
4213    
4214        if ($self->{insertion_mode} & HEAD_IMS) {
4215        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4216          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4217            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
# Line 3409  sub _tree_construction_main ($) { Line 4221  sub _tree_construction_main ($) {
4221              !!!cp ('t88.1');              !!!cp ('t88.1');
4222              ## Ignore the token.              ## Ignore the token.
4223              !!!next-token;              !!!next-token;
4224              redo B;              next B;
4225            }            }
4226            unless (length $token->{data}) {            unless (length $token->{data}) {
4227              !!!cp ('t88');              !!!cp ('t88');
4228              !!!next-token;              !!!next-token;
4229              redo B;              next B;
4230            }            }
4231          }          }
4232    
4233          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4234            !!!cp ('t89');            !!!cp ('t89');
4235            ## As if <head>            ## As if <head>
4236            !!!create-element ($self->{head_element}, 'head',, $token);            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4237            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4238            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            push @{$self->{open_elements}},
4239                  [$self->{head_element}, $el_category->{head}];
4240    
4241            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4242            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
# Line 3433  sub _tree_construction_main ($) { Line 4246  sub _tree_construction_main ($) {
4246            !!!cp ('t90');            !!!cp ('t90');
4247            ## As if </noscript>            ## As if </noscript>
4248            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4249            !!!parse-error (type => 'in noscript:#character', token => $token);            !!!parse-error (type => 'in noscript:#text', token => $token);
4250                        
4251            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4252            ## As if </head>            ## As if </head>
# Line 3449  sub _tree_construction_main ($) { Line 4262  sub _tree_construction_main ($) {
4262            !!!cp ('t92');            !!!cp ('t92');
4263          }          }
4264    
4265              ## "after head" insertion mode          ## "after head" insertion mode
4266              ## As if <body>          ## As if <body>
4267              !!!insert-element ('body',, $token);          !!!insert-element ('body',, $token);
4268              $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4269              ## reprocess          ## reprocess
4270              redo B;          next B;
4271            } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4272              if ($token->{tag_name} eq 'head') {          if ($token->{tag_name} eq 'head') {
4273                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4274                  !!!cp ('t93');              !!!cp ('t93');
4275                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes}, $token);              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4276                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              $self->{open_elements}->[-1]->[0]->append_child
4277                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];                  ($self->{head_element});
4278                  $self->{insertion_mode} = IN_HEAD_IM;              push @{$self->{open_elements}},
4279                  !!!next-token;                  [$self->{head_element}, $el_category->{head}];
4280                  redo B;              $self->{insertion_mode} = IN_HEAD_IM;
4281                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {              !!!nack ('t93.1');
4282                  !!!cp ('t94');              !!!next-token;
4283                  #              next B;
4284                } else {            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4285                  !!!cp ('t95');              !!!cp ('t93.2');
4286                  !!!parse-error (type => 'in head:head', token => $token); # or in head noscript              !!!parse-error (type => 'after head', text => 'head',
4287                  ## Ignore the token                              token => $token);
4288                  !!!next-token;              ## Ignore the token
4289                  redo B;              !!!nack ('t93.3');
4290                }              !!!next-token;
4291              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              next B;
4292                !!!cp ('t96');            } else {
4293                ## As if <head>              !!!cp ('t95');
4294                !!!create-element ($self->{head_element}, 'head',, $token);              !!!parse-error (type => 'in head:head',
4295                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                              token => $token); # or in head noscript
4296                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];              ## Ignore the token
4297                !!!nack ('t95.1');
4298                !!!next-token;
4299                next B;
4300              }
4301            } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4302              !!!cp ('t96');
4303              ## As if <head>
4304              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4305              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4306              push @{$self->{open_elements}},
4307                  [$self->{head_element}, $el_category->{head}];
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...
4311              } else {          } else {
4312                !!!cp ('t97');            !!!cp ('t97');
4313              }          }
4314    
4315              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
4316                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4317                  !!!cp ('t98');                  !!!cp ('t98');
4318                  ## As if </noscript>                  ## As if </noscript>
4319                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4320                  !!!parse-error (type => 'in noscript:base', token => $token);                  !!!parse-error (type => 'in noscript', text => 'base',
4321                                    token => $token);
4322                                
4323                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4324                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
# Line 3504  sub _tree_construction_main ($) { Line 4329  sub _tree_construction_main ($) {
4329                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4330                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4331                  !!!cp ('t100');                  !!!cp ('t100');
4332                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4333                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4334                    push @{$self->{open_elements}},
4335                        [$self->{head_element}, $el_category->{head}];
4336                } else {                } else {
4337                  !!!cp ('t101');                  !!!cp ('t101');
4338                }                }
# Line 3513  sub _tree_construction_main ($) { Line 4340  sub _tree_construction_main ($) {
4340                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4341                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4342                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4343                  !!!nack ('t101.1');
4344                !!!next-token;                !!!next-token;
4345                redo B;                next B;
4346              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
4347                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4348                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4349                  !!!cp ('t102');                  !!!cp ('t102');
4350                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4351                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4352                    push @{$self->{open_elements}},
4353                        [$self->{head_element}, $el_category->{head}];
4354                } else {                } else {
4355                  !!!cp ('t103');                  !!!cp ('t103');
4356                }                }
# Line 3528  sub _tree_construction_main ($) { Line 4358  sub _tree_construction_main ($) {
4358                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4359                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4360                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4361                  !!!ack ('t103.1');
4362                !!!next-token;                !!!next-token;
4363                redo B;                next B;
4364              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
4365                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4366                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4367                  !!!cp ('t104');                  !!!cp ('t104');
4368                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4369                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4370                    push @{$self->{open_elements}},
4371                        [$self->{head_element}, $el_category->{head}];
4372                } else {                } else {
4373                  !!!cp ('t105');                  !!!cp ('t105');
4374                }                }
# Line 3543  sub _tree_construction_main ($) { Line 4376  sub _tree_construction_main ($) {
4376                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.
4377    
4378                unless ($self->{confident}) {                unless ($self->{confident}) {
4379                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) {
4380                    !!!cp ('t106');                    !!!cp ('t106');
4381                      ## NOTE: Whether the encoding is supported or not is handled
4382                      ## in the {change_encoding} callback.
4383                    $self->{change_encoding}                    $self->{change_encoding}
4384                        ->($self, $token->{attributes}->{charset}->{value},                        ->($self, $token->{attributes}->{charset}->{value},
4385                           $token);                           $token);
# Line 3554  sub _tree_construction_main ($) { Line 4389  sub _tree_construction_main ($) {
4389                                             $token->{attributes}->{charset}                                             $token->{attributes}->{charset}
4390                                                 ->{has_reference});                                                 ->{has_reference});
4391                  } elsif ($token->{attributes}->{content}) {                  } elsif ($token->{attributes}->{content}) {
                   ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
4392                    if ($token->{attributes}->{content}->{value}                    if ($token->{attributes}->{content}->{value}
4393                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                        =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4394                            [\x09-\x0D\x20]*=                            [\x09-\x0D\x20]*=
4395                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4396                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
4397                      !!!cp ('t107');                      !!!cp ('t107');
4398                        ## NOTE: Whether the encoding is supported or not is handled
4399                        ## in the {change_encoding} callback.
4400                      $self->{change_encoding}                      $self->{change_encoding}
4401                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4402                             $token);                             $token);
# Line 3591  sub _tree_construction_main ($) { Line 4427  sub _tree_construction_main ($) {
4427    
4428                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4429                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4430                  !!!ack ('t110.1');
4431                !!!next-token;                !!!next-token;
4432                redo B;                next B;
4433              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
4434                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4435                  !!!cp ('t111');                  !!!cp ('t111');
4436                  ## As if </noscript>                  ## As if </noscript>
4437                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4438                  !!!parse-error (type => 'in noscript:title', token => $token);                  !!!parse-error (type => 'in noscript', text => 'title',
4439                                    token => $token);
4440                                
4441                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4442                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4443                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4444                  !!!cp ('t112');                  !!!cp ('t112');
4445                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4446                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4447                    push @{$self->{open_elements}},
4448                        [$self->{head_element}, $el_category->{head}];
4449                } else {                } else {
4450                  !!!cp ('t113');                  !!!cp ('t113');
4451                }                }
# Line 3616  sub _tree_construction_main ($) { Line 4456  sub _tree_construction_main ($) {
4456                $parse_rcdata->(RCDATA_CONTENT_MODEL);                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4457                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4458                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4459                redo B;                next B;
4460              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style' or
4461                         $token->{tag_name} eq 'noframes') {
4462                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4463                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
4464                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4465                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4466                  !!!cp ('t114');                  !!!cp ('t114');
4467                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4468                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4469                    push @{$self->{open_elements}},
4470                        [$self->{head_element}, $el_category->{head}];
4471                } else {                } else {
4472                  !!!cp ('t115');                  !!!cp ('t115');
4473                }                }
4474                $parse_rcdata->(CDATA_CONTENT_MODEL);                $parse_rcdata->(CDATA_CONTENT_MODEL);
4475                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4476                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4477                redo B;                next B;
4478              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4479                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
4480                  !!!cp ('t116');                  !!!cp ('t116');
4481                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
4482                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4483                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4484                    !!!nack ('t116.1');
4485                  !!!next-token;                  !!!next-token;
4486                  redo B;                  next B;
4487                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4488                  !!!cp ('t117');                  !!!cp ('t117');
4489                  !!!parse-error (type => 'in noscript:noscript', token => $token);                  !!!parse-error (type => 'in noscript', text => 'noscript',
4490                                    token => $token);
4491                  ## Ignore the token                  ## Ignore the token
4492                    !!!nack ('t117.1');
4493                  !!!next-token;                  !!!next-token;
4494                  redo B;                  next B;
4495                } else {                } else {
4496                  !!!cp ('t118');                  !!!cp ('t118');
4497                  #                  #
# Line 3655  sub _tree_construction_main ($) { Line 4501  sub _tree_construction_main ($) {
4501                  !!!cp ('t119');                  !!!cp ('t119');
4502                  ## As if </noscript>                  ## As if </noscript>
4503                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4504                  !!!parse-error (type => 'in noscript:script', token => $token);                  !!!parse-error (type => 'in noscript', text => 'script',
4505                                    token => $token);
4506                                
4507                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4508                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4509                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4510                  !!!cp ('t120');                  !!!cp ('t120');
4511                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4512                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4513                    push @{$self->{open_elements}},
4514                        [$self->{head_element}, $el_category->{head}];
4515                } else {                } else {
4516                  !!!cp ('t121');                  !!!cp ('t121');
4517                }                }
# Line 3671  sub _tree_construction_main ($) { Line 4520  sub _tree_construction_main ($) {
4520                $script_start_tag->();                $script_start_tag->();
4521                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4522                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4523                redo B;                next B;
4524              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
4525                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
4526                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4527                  !!!cp ('t122');                  !!!cp ('t122');
4528                  ## As if </noscript>                  ## As if </noscript>
4529                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4530                  !!!parse-error (type => 'in noscript:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'in noscript',
4531                                    text => $token->{tag_name}, token => $token);
4532                                    
4533                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4534                  ## As if </head>                  ## As if </head>
# Line 3705  sub _tree_construction_main ($) { Line 4555  sub _tree_construction_main ($) {
4555                } else {                } else {
4556                  die "$0: tag name: $self->{tag_name}";                  die "$0: tag name: $self->{tag_name}";
4557                }                }
4558                  !!!nack ('t127.1');
4559                !!!next-token;                !!!next-token;
4560                redo B;                next B;
4561              } else {              } else {
4562                !!!cp ('t128');                !!!cp ('t128');
4563                #                #
# Line 3716  sub _tree_construction_main ($) { Line 4567  sub _tree_construction_main ($) {
4567                !!!cp ('t129');                !!!cp ('t129');
4568                ## As if </noscript>                ## As if </noscript>
4569                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4570                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'in noscript:/',
4571                                  text => $token->{tag_name}, token => $token);
4572                                
4573                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4574                ## As if </head>                ## As if </head>
# Line 3738  sub _tree_construction_main ($) { Line 4590  sub _tree_construction_main ($) {
4590              !!!insert-element ('body',, $token);              !!!insert-element ('body',, $token);
4591              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4592              ## reprocess              ## reprocess
4593              redo B;              !!!ack-later;
4594                next B;
4595            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
4596              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
4597                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4598                  !!!cp ('t132');                  !!!cp ('t132');
4599                  ## As if <head>                  ## As if <head>
4600                  !!!create-element ($self->{head_element}, 'head',, $token);                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4601                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4602                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4603                        [$self->{head_element}, $el_category->{head}];
4604    
4605                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4606                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4607                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4608                  !!!next-token;                  !!!next-token;
4609                  redo B;                  next B;
4610                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4611                  !!!cp ('t133');                  !!!cp ('t133');
4612                  ## As if </noscript>                  ## As if </noscript>
4613                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4614                  !!!parse-error (type => 'in noscript:/head', token => $token);                  !!!parse-error (type => 'in noscript:/',
4615                                    text => 'head', token => $token);
4616                                    
4617                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4618                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4619                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4620                  !!!next-token;                  !!!next-token;
4621                  redo B;                  next B;
4622                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4623                  !!!cp ('t134');                  !!!cp ('t134');
4624                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4625                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4626                  !!!next-token;                  !!!next-token;
4627                  redo B;                  next B;
4628                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4629                    !!!cp ('t134.1');
4630                    !!!parse-error (type => 'unmatched end tag', text => 'head',
4631                                    token => $token);
4632                    ## Ignore the token
4633                    !!!next-token;
4634                    next B;
4635                } else {                } else {
4636                  !!!cp ('t135');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
                 #  
4637                }                }
4638              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4639                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
# Line 3780  sub _tree_construction_main ($) { Line 4641  sub _tree_construction_main ($) {
4641                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4642                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4643                  !!!next-token;                  !!!next-token;
4644                  redo B;                  next B;
4645                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
4646                           $self->{insertion_mode} == AFTER_HEAD_IM) {
4647                  !!!cp ('t137');                  !!!cp ('t137');
4648                  !!!parse-error (type => 'unmatched end tag:noscript', token => $token);                  !!!parse-error (type => 'unmatched end tag',
4649                                    text => 'noscript', token => $token);
4650                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
4651                  !!!next-token;                  !!!next-token;
4652                  redo B;                  next B;
4653                } else {                } else {
4654                  !!!cp ('t138');                  !!!cp ('t138');
4655                  #                  #
# Line 3794  sub _tree_construction_main ($) { Line 4657  sub _tree_construction_main ($) {
4657              } elsif ({              } elsif ({
4658                        body => 1, html => 1,                        body => 1, html => 1,
4659                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4660                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM or
4661                  !!!cp ('t139');                    $self->{insertion_mode} == IN_HEAD_IM or
4662                  ## 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) {  
4663                  !!!cp ('t140');                  !!!cp ('t140');
4664                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
4665                                    text => $token->{tag_name}, token => $token);
4666                  ## Ignore the token                  ## Ignore the token
4667                  !!!next-token;                  !!!next-token;
4668                  redo B;                  next B;
4669                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4670                    !!!cp ('t140.1');
4671                    !!!parse-error (type => 'unmatched end tag',
4672                                    text => $token->{tag_name}, token => $token);
4673                    ## Ignore the token
4674                    !!!next-token;
4675                    next B;
4676                } else {                } else {
4677                  !!!cp ('t141');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
4678                }                }
4679                              } elsif ($token->{tag_name} eq 'p') {
4680                #                !!!cp ('t142');
4681              } elsif ({                !!!parse-error (type => 'unmatched end tag',
4682                        p => 1, br => 1,                                text => $token->{tag_name}, token => $token);
4683                       }->{$token->{tag_name}}) {                ## Ignore the token
4684                  !!!next-token;
4685                  next B;
4686                } elsif ($token->{tag_name} eq 'br') {
4687                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4688                  !!!cp ('t142');                  !!!cp ('t142.2');
4689                  ## As if <head>                  ## (before head) as if <head>, (in head) as if </head>
4690                  !!!create-element ($self->{head_element}, 'head',, $token);                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4691                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4692                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  $self->{insertion_mode} = AFTER_HEAD_IM;
4693      
4694                    ## Reprocess in the "after head" insertion mode...
4695                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4696                    !!!cp ('t143.2');
4697                    ## As if </head>
4698                    pop @{$self->{open_elements}};
4699                    $self->{insertion_mode} = AFTER_HEAD_IM;
4700      
4701                    ## Reprocess in the "after head" insertion mode...
4702                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4703                    !!!cp ('t143.3');
4704                    ## ISSUE: Two parse errors for <head><noscript></br>
4705                    !!!parse-error (type => 'unmatched end tag',
4706                                    text => 'br', token => $token);
4707                    ## As if </noscript>
4708                    pop @{$self->{open_elements}};
4709                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4710    
4711                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4712                } else {                  ## As if </head>
4713                  !!!cp ('t143');                  pop @{$self->{open_elements}};
4714                }                  $self->{insertion_mode} = AFTER_HEAD_IM;
4715    
4716                #                  ## Reprocess in the "after head" insertion mode...
4717              } else {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4718                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                  !!!cp ('t143.4');
                 !!!cp ('t144');  
4719                  #                  #
4720                } else {                } else {
4721                  !!!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;  
4722                }                }
4723    
4724                  ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
4725                  !!!parse-error (type => 'unmatched end tag',
4726                                  text => 'br', token => $token);
4727                  ## Ignore the token
4728                  !!!next-token;
4729                  next B;
4730                } else {
4731                  !!!cp ('t145');
4732                  !!!parse-error (type => 'unmatched end tag',
4733                                  text => $token->{tag_name}, token => $token);
4734                  ## Ignore the token
4735                  !!!next-token;
4736                  next B;
4737              }              }
4738    
4739              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4740                !!!cp ('t146');                !!!cp ('t146');
4741                ## As if </noscript>                ## As if </noscript>
4742                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4743                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'in noscript:/',
4744                                  text => $token->{tag_name}, token => $token);
4745                                
4746                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4747                ## As if </head>                ## As if </head>
# Line 3864  sub _tree_construction_main ($) { Line 4757  sub _tree_construction_main ($) {
4757              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4758  ## ISSUE: This case cannot be reached?  ## ISSUE: This case cannot be reached?
4759                !!!cp ('t148');                !!!cp ('t148');
4760                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag',
4761                                  text => $token->{tag_name}, token => $token);
4762                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
4763                !!!next-token;                !!!next-token;
4764                redo B;                next B;
4765              } else {              } else {
4766                !!!cp ('t149');                !!!cp ('t149');
4767              }              }
# Line 3877  sub _tree_construction_main ($) { Line 4771  sub _tree_construction_main ($) {
4771              !!!insert-element ('body',, $token);              !!!insert-element ('body',, $token);
4772              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4773              ## reprocess              ## reprocess
4774              redo B;              next B;
4775        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4776          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4777            !!!cp ('t149.1');            !!!cp ('t149.1');
4778    
4779            ## NOTE: As if <head>            ## NOTE: As if <head>
4780            !!!create-element ($self->{head_element}, 'head',, $token);            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4781            $self->{open_elements}->[-1]->[0]->append_child            $self->{open_elements}->[-1]->[0]->append_child
4782                ($self->{head_element});                ($self->{head_element});
4783            #push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            #push @{$self->{open_elements}},
4784              #    [$self->{head_element}, $el_category->{head}];
4785            #$self->{insertion_mode} = IN_HEAD_IM;            #$self->{insertion_mode} = IN_HEAD_IM;
4786            ## NOTE: Reprocess.            ## NOTE: Reprocess.
4787    
# Line 3930  sub _tree_construction_main ($) { Line 4825  sub _tree_construction_main ($) {
4825          !!!insert-element ('body',, $token);          !!!insert-element ('body',, $token);
4826          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4827          ## NOTE: Reprocess.          ## NOTE: Reprocess.
4828          redo B;          next B;
4829        } else {        } else {
4830          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
4831        }        }
# Line 3945  sub _tree_construction_main ($) { Line 4840  sub _tree_construction_main ($) {
4840              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4841    
4842              !!!next-token;              !!!next-token;
4843              redo B;              next B;
4844            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
4845              if ({              if ({
4846                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
# Line 3955  sub _tree_construction_main ($) { Line 4850  sub _tree_construction_main ($) {
4850                  ## have an element in table scope                  ## have an element in table scope
4851                  for (reverse 0..$#{$self->{open_elements}}) {                  for (reverse 0..$#{$self->{open_elements}}) {
4852                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4853                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] & TABLE_CELL_EL) {
4854                      !!!cp ('t151');                      !!!cp ('t151');
4855    
4856                      ## Close the cell                      ## Close the cell
4857                      !!!back-token; # <?>                      !!!back-token; # <x>
4858                      $token = {type => END_TAG_TOKEN, tag_name => $node->[1],                      $token = {type => END_TAG_TOKEN,
4859                                  tag_name => $node->[0]->manakai_local_name,
4860                                line => $token->{line},                                line => $token->{line},
4861                                column => $token->{column}};                                column => $token->{column}};
4862                      redo B;                      next B;
4863                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
4864                      !!!cp ('t152');                      !!!cp ('t152');
4865                      ## ISSUE: This case can never be reached, maybe.                      ## ISSUE: This case can never be reached, maybe.
4866                      last;                      last;
# Line 3975  sub _tree_construction_main ($) { Line 4869  sub _tree_construction_main ($) {
4869    
4870                  !!!cp ('t153');                  !!!cp ('t153');
4871                  !!!parse-error (type => 'start tag not allowed',                  !!!parse-error (type => 'start tag not allowed',
4872                      value => $token->{tag_name}, token => $token);                      text => $token->{tag_name}, token => $token);
4873                  ## Ignore the token                  ## Ignore the token
4874                    !!!nack ('t153.1');
4875                  !!!next-token;                  !!!next-token;
4876                  redo B;                  next B;
4877                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4878                  !!!parse-error (type => 'not closed:caption', token => $token);                  !!!parse-error (type => 'not closed', text => 'caption',
4879                                    token => $token);
4880                                    
4881                  ## NOTE: As if </caption>.                  ## NOTE: As if </caption>.
4882                  ## have a table element in table scope                  ## have a table element in table scope
# Line 3988  sub _tree_construction_main ($) { Line 4884  sub _tree_construction_main ($) {
4884                  INSCOPE: {                  INSCOPE: {
4885                    for (reverse 0..$#{$self->{open_elements}}) {                    for (reverse 0..$#{$self->{open_elements}}) {
4886                      my $node = $self->{open_elements}->[$_];                      my $node = $self->{open_elements}->[$_];
4887                      if ($node->[1] eq 'caption') {                      if ($node->[1] & CAPTION_EL) {
4888                        !!!cp ('t155');                        !!!cp ('t155');
4889                        $i = $_;                        $i = $_;
4890                        last INSCOPE;                        last INSCOPE;
4891                      } elsif ({                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
                               table => 1, html => 1,  
                              }->{$node->[1]}) {  
4892                        !!!cp ('t156');                        !!!cp ('t156');
4893                        last;                        last;
4894                      }                      }
# Line 4002  sub _tree_construction_main ($) { Line 4896  sub _tree_construction_main ($) {
4896    
4897                    !!!cp ('t157');                    !!!cp ('t157');
4898                    !!!parse-error (type => 'start tag not allowed',                    !!!parse-error (type => 'start tag not allowed',
4899                                    value => $token->{tag_name}, token => $token);                                    text => $token->{tag_name}, token => $token);
4900                    ## Ignore the token                    ## Ignore the token
4901                      !!!nack ('t157.1');
4902                    !!!next-token;                    !!!next-token;
4903                    redo B;                    next B;
4904                  } # INSCOPE                  } # INSCOPE
4905                                    
4906                  ## generate implied end tags                  ## generate implied end tags
4907                  while ({                  while ($self->{open_elements}->[-1]->[1]
4908                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4909                    !!!cp ('t158');                    !!!cp ('t158');
4910                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4911                  }                  }
4912    
4913                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4914                    !!!cp ('t159');                    !!!cp ('t159');
4915                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                    !!!parse-error (type => 'not closed',
4916                                      text => $self->{open_elements}->[-1]->[0]
4917                                          ->manakai_local_name,
4918                                      token => $token);
4919                  } else {                  } else {
4920                    !!!cp ('t160');                    !!!cp ('t160');
4921                  }                  }
# Line 4030  sub _tree_construction_main ($) { Line 4927  sub _tree_construction_main ($) {
4927                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4928                                    
4929                  ## reprocess                  ## reprocess
4930                  redo B;                  !!!ack-later;
4931                    next B;
4932                } else {                } else {
4933                  !!!cp ('t161');                  !!!cp ('t161');
4934                  #                  #
# Line 4046  sub _tree_construction_main ($) { Line 4944  sub _tree_construction_main ($) {
4944                  my $i;                  my $i;
4945                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4946                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4947                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4948                      !!!cp ('t163');                      !!!cp ('t163');
4949                      $i = $_;                      $i = $_;
4950                      last INSCOPE;                      last INSCOPE;
4951                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
4952                      !!!cp ('t164');                      !!!cp ('t164');
4953                      last INSCOPE;                      last INSCOPE;
4954                    }                    }
4955                  } # INSCOPE                  } # INSCOPE
4956                    unless (defined $i) {                    unless (defined $i) {
4957                      !!!cp ('t165');                      !!!cp ('t165');
4958                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                      !!!parse-error (type => 'unmatched end tag',
4959                                        text => $token->{tag_name},
4960                                        token => $token);
4961                      ## Ignore the token                      ## Ignore the token
4962                      !!!next-token;                      !!!next-token;
4963                      redo B;                      next B;
4964                    }                    }
4965                                    
4966                  ## generate implied end tags                  ## generate implied end tags
4967                  while ({                  while ($self->{open_elements}->[-1]->[1]
4968                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4969                    !!!cp ('t166');                    !!!cp ('t166');
4970                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4971                  }                  }
4972    
4973                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
4974                            ne $token->{tag_name}) {
4975                    !!!cp ('t167');                    !!!cp ('t167');
4976                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                    !!!parse-error (type => 'not closed',
4977                                      text => $self->{open_elements}->[-1]->[0]
4978                                          ->manakai_local_name,
4979                                      token => $token);
4980                  } else {                  } else {
4981                    !!!cp ('t168');                    !!!cp ('t168');
4982                  }                  }
# Line 4087  sub _tree_construction_main ($) { Line 4988  sub _tree_construction_main ($) {
4988                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
4989                                    
4990                  !!!next-token;                  !!!next-token;
4991                  redo B;                  next B;
4992                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4993                  !!!cp ('t169');                  !!!cp ('t169');
4994                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
4995                                    text => $token->{tag_name}, token => $token);
4996                  ## Ignore the token                  ## Ignore the token
4997                  !!!next-token;                  !!!next-token;
4998                  redo B;                  next B;
4999                } else {                } else {
5000                  !!!cp ('t170');                  !!!cp ('t170');
5001                  #                  #
# Line 4105  sub _tree_construction_main ($) { Line 5007  sub _tree_construction_main ($) {
5007                  INSCOPE: {                  INSCOPE: {
5008                    for (reverse 0..$#{$self->{open_elements}}) {                    for (reverse 0..$#{$self->{open_elements}}) {
5009                      my $node = $self->{open_elements}->[$_];                      my $node = $self->{open_elements}->[$_];
5010                      if ($node->[1] eq $token->{tag_name}) {                      if ($node->[1] & CAPTION_EL) {
5011                        !!!cp ('t171');                        !!!cp ('t171');
5012                        $i = $_;                        $i = $_;
5013                        last INSCOPE;                        last INSCOPE;
5014                      } elsif ({                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
                               table => 1, html => 1,  
                              }->{$node->[1]}) {  
5015                        !!!cp ('t172');                        !!!cp ('t172');
5016                        last;                        last;
5017                      }                      }
# Line 4119  sub _tree_construction_main ($) { Line 5019  sub _tree_construction_main ($) {
5019    
5020                    !!!cp ('t173');                    !!!cp ('t173');
5021                    !!!parse-error (type => 'unmatched end tag',                    !!!parse-error (type => 'unmatched end tag',
5022                                    value => $token->{tag_name}, token => $token);                                    text => $token->{tag_name}, token => $token);
5023                    ## Ignore the token                    ## Ignore the token
5024                    !!!next-token;                    !!!next-token;
5025                    redo B;                    next B;
5026                  } # INSCOPE                  } # INSCOPE
5027                                    
5028                  ## generate implied end tags                  ## generate implied end tags
5029                  while ({                  while ($self->{open_elements}->[-1]->[1]
5030                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
5031                    !!!cp ('t174');                    !!!cp ('t174');
5032                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5033                  }                  }
5034                                    
5035                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5036                    !!!cp ('t175');                    !!!cp ('t175');
5037                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                    !!!parse-error (type => 'not closed',
5038                                      text => $self->{open_elements}->[-1]->[0]
5039                                          ->manakai_local_name,
5040                                      token => $token);
5041                  } else {                  } else {
5042                    !!!cp ('t176');                    !!!cp ('t176');
5043                  }                  }
# Line 4147  sub _tree_construction_main ($) { Line 5049  sub _tree_construction_main ($) {
5049                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5050                                    
5051                  !!!next-token;                  !!!next-token;
5052                  redo B;                  next B;
5053                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
5054                  !!!cp ('t177');                  !!!cp ('t177');
5055                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
5056                                    text => $token->{tag_name}, token => $token);
5057                  ## Ignore the token                  ## Ignore the token
5058                  !!!next-token;                  !!!next-token;
5059                  redo B;                  next B;
5060                } else {                } else {
5061                  !!!cp ('t178');                  !!!cp ('t178');
5062                  #                  #
# Line 4169  sub _tree_construction_main ($) { Line 5072  sub _tree_construction_main ($) {
5072                INSCOPE: {                INSCOPE: {
5073                  for (reverse 0..$#{$self->{open_elements}}) {                  for (reverse 0..$#{$self->{open_elements}}) {
5074                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5075                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5076                      !!!cp ('t179');                      !!!cp ('t179');
5077                      $i = $_;                      $i = $_;
5078    
5079                      ## Close the cell                      ## Close the cell
5080                      !!!back-token; # </?>                      !!!back-token; # </x>
5081                      $token = {type => END_TAG_TOKEN, tag_name => $tn,                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
5082                                line => $token->{line},                                line => $token->{line},
5083                                column => $token->{column}};                                column => $token->{column}};
5084                      redo B;                      next B;
5085                    } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {                    } elsif ($node->[1] & TABLE_CELL_EL) {
5086                      !!!cp ('t180');                      !!!cp ('t180');
5087                      $tn = $node->[1];                      $tn = $node->[0]->manakai_local_name;
5088                      ## NOTE: There is exactly one |td| or |th| element                      ## NOTE: There is exactly one |td| or |th| element
5089                      ## in scope in the stack of open elements by definition.                      ## in scope in the stack of open elements by definition.
5090                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5091                      ## ISSUE: Can this be reached?                      ## ISSUE: Can this be reached?
5092                      !!!cp ('t181');                      !!!cp ('t181');
5093                      last;                      last;
# Line 4195  sub _tree_construction_main ($) { Line 5096  sub _tree_construction_main ($) {
5096    
5097                  !!!cp ('t182');                  !!!cp ('t182');
5098                  !!!parse-error (type => 'unmatched end tag',                  !!!parse-error (type => 'unmatched end tag',
5099                      value => $token->{tag_name}, token => $token);                      text => $token->{tag_name}, token => $token);
5100                  ## Ignore the token                  ## Ignore the token
5101                  !!!next-token;                  !!!next-token;
5102                  redo B;                  next B;
5103                } # INSCOPE                } # INSCOPE
5104              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
5105                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5106                !!!parse-error (type => 'not closed:caption', token => $token);                !!!parse-error (type => 'not closed', text => 'caption',
5107                                  token => $token);
5108    
5109                ## As if </caption>                ## As if </caption>
5110                ## have a table element in table scope                ## have a table element in table scope
5111                my $i;                my $i;
5112                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5113                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5114                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
5115                    !!!cp ('t184');                    !!!cp ('t184');
5116                    $i = $_;                    $i = $_;
5117                    last INSCOPE;                    last INSCOPE;
5118                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5119                    !!!cp ('t185');                    !!!cp ('t185');
5120                    last INSCOPE;                    last INSCOPE;
5121                  }                  }
5122                } # INSCOPE                } # INSCOPE
5123                unless (defined $i) {                unless (defined $i) {
5124                  !!!cp ('t186');                  !!!cp ('t186');
5125                  !!!parse-error (type => 'unmatched end tag:caption', token => $token);                  !!!parse-error (type => 'unmatched end tag',
5126                                    text => 'caption', token => $token);
5127                  ## Ignore the token                  ## Ignore the token
5128                  !!!next-token;                  !!!next-token;
5129                  redo B;                  next B;
5130                }                }
5131                                
5132                ## generate implied end tags                ## generate implied end tags
5133                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
5134                  !!!cp ('t187');                  !!!cp ('t187');
5135                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5136                }                }
5137    
5138                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5139                  !!!cp ('t188');                  !!!cp ('t188');
5140                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                  !!!parse-error (type => 'not closed',
5141                                    text => $self->{open_elements}->[-1]->[0]
5142                                        ->manakai_local_name,
5143                                    token => $token);
5144                } else {                } else {
5145                  !!!cp ('t189');                  !!!cp ('t189');
5146                }                }
# Line 4250  sub _tree_construction_main ($) { Line 5152  sub _tree_construction_main ($) {
5152                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5153    
5154                ## reprocess                ## reprocess
5155                redo B;                next B;
5156              } elsif ({              } elsif ({
5157                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
5158                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5159                if ($self->{insertion_mode} & BODY_TABLE_IMS) {                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
5160                  !!!cp ('t190');                  !!!cp ('t190');
5161                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
5162                                    text => $token->{tag_name}, token => $token);
5163                  ## Ignore the token                  ## Ignore the token
5164                  !!!next-token;                  !!!next-token;
5165                  redo B;                  next B;
5166                } else {                } else {
5167                  !!!cp ('t191');                  !!!cp ('t191');
5168                  #                  #
# Line 4270  sub _tree_construction_main ($) { Line 5173  sub _tree_construction_main ($) {
5173                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
5174                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5175                !!!cp ('t192');                !!!cp ('t192');
5176                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag',
5177                                  text => $token->{tag_name}, token => $token);
5178                ## Ignore the token                ## Ignore the token
5179                !!!next-token;                !!!next-token;
5180                redo B;                next B;
5181              } else {              } else {
5182                !!!cp ('t193');                !!!cp ('t193');
5183                #                #
5184              }              }
5185        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5186          for my $entry (@{$self->{open_elements}}) {          for my $entry (@{$self->{open_elements}}) {
5187            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]}) {  
5188              !!!cp ('t75');              !!!cp ('t75');
5189              !!!parse-error (type => 'in body:#eof', token => $token);              !!!parse-error (type => 'in body:#eof', token => $token);
5190              last;              last;
# Line 4307  sub _tree_construction_main ($) { Line 5208  sub _tree_construction_main ($) {
5208            unless (length $token->{data}) {            unless (length $token->{data}) {
5209              !!!cp ('t194');              !!!cp ('t194');
5210              !!!next-token;              !!!next-token;
5211              redo B;              next B;
5212            } else {            } else {
5213              !!!cp ('t195');              !!!cp ('t195');
5214            }            }
5215          }          }
5216    
5217              !!!parse-error (type => 'in table:#character', token => $token);          !!!parse-error (type => 'in table:#text', token => $token);
5218    
5219              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
5220              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
# Line 4321  sub _tree_construction_main ($) { Line 5222  sub _tree_construction_main ($) {
5222              ## result in a new Text node.              ## result in a new Text node.
5223              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
5224                            
5225              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]}) {  
5226                # MUST                # MUST
5227                my $foster_parent_element;                my $foster_parent_element;
5228                my $next_sibling;                my $next_sibling;
5229                my $prev_sibling;                my $prev_sibling;
5230                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
5231                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
5232                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
5233                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
5234                      !!!cp ('t196');                      !!!cp ('t196');
# Line 4365  sub _tree_construction_main ($) { Line 5263  sub _tree_construction_main ($) {
5263          }          }
5264                            
5265          !!!next-token;          !!!next-token;
5266          redo B;          next B;
5267        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5268              if ({          if ({
5269                   tr => ($self->{insertion_mode} != IN_ROW_IM),               tr => ($self->{insertion_mode} != IN_ROW_IM),
5270                   th => 1, td => 1,               th => 1, td => 1,
5271                  }->{$token->{tag_name}}) {              }->{$token->{tag_name}}) {
5272                if ($self->{insertion_mode} == IN_TABLE_IM) {            if ($self->{insertion_mode} == IN_TABLE_IM) {
5273                  ## Clear back to table context              ## Clear back to table context
5274                  while ($self->{open_elements}->[-1]->[1] ne 'table' and              while (not ($self->{open_elements}->[-1]->[1]
5275                         $self->{open_elements}->[-1]->[1] ne 'html') {                              & TABLE_SCOPING_EL)) {
5276                    !!!cp ('t201');                !!!cp ('t201');
5277                    pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5278                  }              }
5279                                
5280                  !!!insert-element ('tbody',, $token);              !!!insert-element ('tbody',, $token);
5281                  $self->{insertion_mode} = IN_TABLE_BODY_IM;              $self->{insertion_mode} = IN_TABLE_BODY_IM;
5282                  ## reprocess in the "in table body" insertion mode...              ## reprocess in the "in table body" insertion mode...
5283                }            }
5284              
5285                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {            if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5286                  unless ($token->{tag_name} eq 'tr') {              unless ($token->{tag_name} eq 'tr') {
5287                    !!!cp ('t202');                !!!cp ('t202');
5288                    !!!parse-error (type => 'missing start tag:tr', token => $token);                !!!parse-error (type => 'missing start tag:tr', token => $token);
5289                  }              }
5290                                    
5291                  ## Clear back to table body context              ## Clear back to table body context
5292                  while (not {              while (not ($self->{open_elements}->[-1]->[1]
5293                    tbody => 1, tfoot => 1, thead => 1, html => 1,                              & TABLE_ROWS_SCOPING_EL)) {
5294                  }->{$self->{open_elements}->[-1]->[1]}) {                !!!cp ('t203');
5295                    !!!cp ('t203');                ## ISSUE: Can this case be reached?
5296                    ## ISSUE: Can this case be reached?                pop @{$self->{open_elements}};
5297                    pop @{$self->{open_elements}};              }
                 }  
5298                                    
5299                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
5300                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5301                    !!!cp ('t204');                    !!!cp ('t204');
5302                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5303                      !!!nack ('t204');
5304                    !!!next-token;                    !!!next-token;
5305                    redo B;                    next B;
5306                  } else {                  } else {
5307                    !!!cp ('t205');                    !!!cp ('t205');
5308                    !!!insert-element ('tr',, $token);                    !!!insert-element ('tr',, $token);
# Line 4415  sub _tree_construction_main ($) { Line 5313  sub _tree_construction_main ($) {
5313                }                }
5314    
5315                ## Clear back to table row context                ## Clear back to table row context
5316                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5317                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5318                  !!!cp ('t207');                  !!!cp ('t207');
5319                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5320                }                }
# Line 4427  sub _tree_construction_main ($) { Line 5324  sub _tree_construction_main ($) {
5324    
5325                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
5326                                
5327                  !!!nack ('t207.1');
5328                !!!next-token;                !!!next-token;
5329                redo B;                next B;
5330              } elsif ({              } elsif ({
5331                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5332                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
# Line 4440  sub _tree_construction_main ($) { Line 5338  sub _tree_construction_main ($) {
5338                  my $i;                  my $i;
5339                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5340                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5341                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5342                      !!!cp ('t208');                      !!!cp ('t208');
5343                      $i = $_;                      $i = $_;
5344                      last INSCOPE;                      last INSCOPE;
5345                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             html => 1,  
   
                             ## NOTE: This element does not appear here, maybe.  
                             table => 1,  
                            }->{$node->[1]}) {  
5346                      !!!cp ('t209');                      !!!cp ('t209');
5347                      last INSCOPE;                      last INSCOPE;
5348                    }                    }
5349                  } # INSCOPE                  } # INSCOPE
5350                  unless (defined $i) {                  unless (defined $i) {
5351                   !!!cp ('t210');                    !!!cp ('t210');
5352  ## TODO: This type is wrong.  ## TODO: This type is wrong.
5353                   !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);                    !!!parse-error (type => 'unmacthed end tag',
5354                                      text => $token->{tag_name}, token => $token);
5355                    ## Ignore the token                    ## Ignore the token
5356                      !!!nack ('t210.1');
5357                    !!!next-token;                    !!!next-token;
5358                    redo B;                    next B;
5359                  }                  }
5360                                    
5361                  ## Clear back to table row context                  ## Clear back to table row context
5362                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5363                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5364                    !!!cp ('t211');                    !!!cp ('t211');
5365                    ## ISSUE: Can this case be reached?                    ## ISSUE: Can this case be reached?
5366                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4477  sub _tree_construction_main ($) { Line 5371  sub _tree_construction_main ($) {
5371                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5372                    !!!cp ('t212');                    !!!cp ('t212');
5373                    ## reprocess                    ## reprocess
5374                    redo B;                    !!!ack-later;
5375                      next B;
5376                  } else {                  } else {
5377                    !!!cp ('t213');                    !!!cp ('t213');
5378                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
# Line 4489  sub _tree_construction_main ($) { Line 5384  sub _tree_construction_main ($) {
5384                  my $i;                  my $i;
5385                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5386                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5387                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5388                      !!!cp ('t214');                      !!!cp ('t214');
5389                      $i = $_;                      $i = $_;
5390                      last INSCOPE;                      last INSCOPE;
5391                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5392                      !!!cp ('t215');                      !!!cp ('t215');
5393                      last INSCOPE;                      last INSCOPE;
5394                    }                    }
5395                  } # INSCOPE                  } # INSCOPE
5396                  unless (defined $i) {                  unless (defined $i) {
5397                    !!!cp ('t216');                    !!!cp ('t216');
5398  ## TODO: This erorr type ios wrong.  ## TODO: This erorr type is wrong.
5399                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                    !!!parse-error (type => 'unmatched end tag',
5400                                      text => $token->{tag_name}, token => $token);
5401                    ## Ignore the token                    ## Ignore the token
5402                      !!!nack ('t216.1');
5403                    !!!next-token;                    !!!next-token;
5404                    redo B;                    next B;
5405                  }                  }
5406    
5407                  ## Clear back to table body context                  ## Clear back to table body context
5408                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5409                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5410                    !!!cp ('t217');                    !!!cp ('t217');
5411                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5412                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4536  sub _tree_construction_main ($) { Line 5428  sub _tree_construction_main ($) {
5428    
5429                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
5430                  ## Clear back to table context                  ## Clear back to table context
5431                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5432                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5433                    !!!cp ('t219');                    !!!cp ('t219');
5434                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5435                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4546  sub _tree_construction_main ($) { Line 5438  sub _tree_construction_main ($) {
5438                  !!!insert-element ('colgroup',, $token);                  !!!insert-element ('colgroup',, $token);
5439                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5440                  ## reprocess                  ## reprocess
5441                  redo B;                  !!!ack-later;
5442                    next B;
5443                } elsif ({                } elsif ({
5444                          caption => 1,                          caption => 1,
5445                          colgroup => 1,                          colgroup => 1,
5446                          tbody => 1, tfoot => 1, thead => 1,                          tbody => 1, tfoot => 1, thead => 1,
5447                         }->{$token->{tag_name}}) {                         }->{$token->{tag_name}}) {
5448                  ## Clear back to table context                  ## Clear back to table context
5449                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5450                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5451                    !!!cp ('t220');                    !!!cp ('t220');
5452                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5453                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4572  sub _tree_construction_main ($) { Line 5465  sub _tree_construction_main ($) {
5465                                             thead => IN_TABLE_BODY_IM,                                             thead => IN_TABLE_BODY_IM,
5466                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
5467                  !!!next-token;                  !!!next-token;
5468                  redo B;                  !!!nack ('t220.1');
5469                    next B;
5470                } else {                } else {
5471                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
5472                }                }
5473              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5474                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                !!!parse-error (type => 'not closed',
5475                                  text => $self->{open_elements}->[-1]->[0]
5476                                      ->manakai_local_name,
5477                                  token => $token);
5478    
5479                ## As if </table>                ## As if </table>
5480                ## have a table element in table scope                ## have a table element in table scope
5481                my $i;                my $i;
5482                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5483                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5484                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5485                    !!!cp ('t221');                    !!!cp ('t221');
5486                    $i = $_;                    $i = $_;
5487                    last INSCOPE;                    last INSCOPE;
5488                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           #table => 1,  
                           html => 1,  
                          }->{$node->[1]}) {  
5489                    !!!cp ('t222');                    !!!cp ('t222');
5490                    last INSCOPE;                    last INSCOPE;
5491                  }                  }
# Line 4599  sub _tree_construction_main ($) { Line 5493  sub _tree_construction_main ($) {
5493                unless (defined $i) {                unless (defined $i) {
5494                  !!!cp ('t223');                  !!!cp ('t223');
5495  ## TODO: The following is wrong, maybe.  ## TODO: The following is wrong, maybe.
5496                  !!!parse-error (type => 'unmatched end tag:table', token => $token);                  !!!parse-error (type => 'unmatched end tag', text => 'table',
5497                                    token => $token);
5498                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5499                    !!!nack ('t223.1');
5500                  !!!next-token;                  !!!next-token;
5501                  redo B;                  next B;
5502                }                }
5503                                
5504  ## TODO: Followings are removed from the latest spec.  ## TODO: Followings are removed from the latest spec.
5505                ## generate implied end tags                ## generate implied end tags
5506                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
5507                  !!!cp ('t224');                  !!!cp ('t224');
5508                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5509                }                }
5510    
5511                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5512                  !!!cp ('t225');                  !!!cp ('t225');
5513  ## ISSUE: Can this case be reached?                  ## NOTE: |<table><tr><table>|
5514                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                  !!!parse-error (type => 'not closed',
5515                                    text => $self->{open_elements}->[-1]->[0]
5516                                        ->manakai_local_name,
5517                                    token => $token);
5518                } else {                } else {
5519                  !!!cp ('t226');                  !!!cp ('t226');
5520                }                }
# Line 4627  sub _tree_construction_main ($) { Line 5524  sub _tree_construction_main ($) {
5524    
5525                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5526    
5527                ## reprocess            ## reprocess
5528                redo B;            !!!ack-later;
5529              next B;
5530          } elsif ($token->{tag_name} eq 'style') {          } elsif ($token->{tag_name} eq 'style') {
5531            if (not $open_tables->[-1]->[1]) { # tainted            if (not $open_tables->[-1]->[1]) { # tainted
5532              !!!cp ('t227.8');              !!!cp ('t227.8');
5533              ## NOTE: This is a "as if in head" code clone.              ## NOTE: This is a "as if in head" code clone.
5534              $parse_rcdata->(CDATA_CONTENT_MODEL);              $parse_rcdata->(CDATA_CONTENT_MODEL);
5535              redo B;              next B;
5536            } else {            } else {
5537              !!!cp ('t227.7');              !!!cp ('t227.7');
5538              #              #
# Line 4644  sub _tree_construction_main ($) { Line 5542  sub _tree_construction_main ($) {
5542              !!!cp ('t227.6');              !!!cp ('t227.6');
5543              ## NOTE: This is a "as if in head" code clone.              ## NOTE: This is a "as if in head" code clone.
5544              $script_start_tag->();              $script_start_tag->();
5545              redo B;              next B;
5546            } else {            } else {
5547              !!!cp ('t227.5');              !!!cp ('t227.5');
5548              #              #
# Line 4655  sub _tree_construction_main ($) { Line 5553  sub _tree_construction_main ($) {
5553                my $type = lc $token->{attributes}->{type}->{value};                my $type = lc $token->{attributes}->{type}->{value};
5554                if ($type eq 'hidden') {                if ($type eq 'hidden') {
5555                  !!!cp ('t227.3');                  !!!cp ('t227.3');
5556                  !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'in table',
5557                                    text => $token->{tag_name}, token => $token);
5558    
5559                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5560    
# Line 4664  sub _tree_construction_main ($) { Line 5563  sub _tree_construction_main ($) {
5563                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5564    
5565                  !!!next-token;                  !!!next-token;
5566                  redo B;                  !!!ack ('t227.2.1');
5567                    next B;
5568                } else {                } else {
5569                  !!!cp ('t227.2');                  !!!cp ('t227.2');
5570                  #                  #
# Line 4682  sub _tree_construction_main ($) { Line 5582  sub _tree_construction_main ($) {
5582            #            #
5583          }          }
5584    
5585          !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'in table', text => $token->{tag_name},
5586                            token => $token);
5587    
5588          $insert = $insert_to_foster;          $insert = $insert_to_foster;
5589          #          #
# Line 4693  sub _tree_construction_main ($) { Line 5594  sub _tree_construction_main ($) {
5594                my $i;                my $i;
5595                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5596                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5597                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_ROW_EL) {
5598                    !!!cp ('t228');                    !!!cp ('t228');
5599                    $i = $_;                    $i = $_;
5600                    last INSCOPE;                    last INSCOPE;
5601                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5602                    !!!cp ('t229');                    !!!cp ('t229');
5603                    last INSCOPE;                    last INSCOPE;
5604                  }                  }
5605                } # INSCOPE                } # INSCOPE
5606                unless (defined $i) {                unless (defined $i) {
5607                  !!!cp ('t230');                  !!!cp ('t230');
5608                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
5609                                    text => $token->{tag_name}, token => $token);
5610                  ## Ignore the token                  ## Ignore the token
5611                    !!!nack ('t230.1');
5612                  !!!next-token;                  !!!next-token;
5613                  redo B;                  next B;
5614                } else {                } else {
5615                  !!!cp ('t232');                  !!!cp ('t232');
5616                }                }
5617    
5618                ## Clear back to table row context                ## Clear back to table row context
5619                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5620                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5621                  !!!cp ('t231');                  !!!cp ('t231');
5622  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
5623                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 4726  sub _tree_construction_main ($) { Line 5626  sub _tree_construction_main ($) {
5626                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5627                $self->{insertion_mode} = IN_TABLE_BODY_IM;                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5628                !!!next-token;                !!!next-token;
5629                redo B;                !!!nack ('t231.1');
5630                  next B;
5631              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5632                if ($self->{insertion_mode} == IN_ROW_IM) {                if ($self->{insertion_mode} == IN_ROW_IM) {
5633                  ## As if </tr>                  ## As if </tr>
# Line 4734  sub _tree_construction_main ($) { Line 5635  sub _tree_construction_main ($) {
5635                  my $i;                  my $i;
5636                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5637                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5638                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5639                      !!!cp ('t233');                      !!!cp ('t233');
5640                      $i = $_;                      $i = $_;
5641                      last INSCOPE;                      last INSCOPE;
5642                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5643                      !!!cp ('t234');                      !!!cp ('t234');
5644                      last INSCOPE;                      last INSCOPE;
5645                    }                    }
# Line 4748  sub _tree_construction_main ($) { Line 5647  sub _tree_construction_main ($) {
5647                  unless (defined $i) {                  unless (defined $i) {
5648                    !!!cp ('t235');                    !!!cp ('t235');
5649  ## TODO: The following is wrong.  ## TODO: The following is wrong.
5650                    !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);                    !!!parse-error (type => 'unmatched end tag',
5651                                      text => $token->{type}, token => $token);
5652                    ## Ignore the token                    ## Ignore the token
5653                      !!!nack ('t236.1');
5654                    !!!next-token;                    !!!next-token;
5655                    redo B;                    next B;
5656                  }                  }
5657                                    
5658                  ## Clear back to table row context                  ## Clear back to table row context
5659                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5660                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5661                    !!!cp ('t236');                    !!!cp ('t236');
5662  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
5663                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4773  sub _tree_construction_main ($) { Line 5673  sub _tree_construction_main ($) {
5673                  my $i;                  my $i;
5674                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5675                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5676                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5677                      !!!cp ('t237');                      !!!cp ('t237');
5678                      $i = $_;                      $i = $_;
5679                      last INSCOPE;                      last INSCOPE;
5680                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5681                      !!!cp ('t238');                      !!!cp ('t238');
5682                      last INSCOPE;                      last INSCOPE;
5683                    }                    }
5684                  } # INSCOPE                  } # INSCOPE
5685                  unless (defined $i) {                  unless (defined $i) {
5686                    !!!cp ('t239');                    !!!cp ('t239');
5687                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                    !!!parse-error (type => 'unmatched end tag',
5688                                      text => $token->{tag_name}, token => $token);
5689                    ## Ignore the token                    ## Ignore the token
5690                      !!!nack ('t239.1');
5691                    !!!next-token;                    !!!next-token;
5692                    redo B;                    next B;
5693                  }                  }
5694                                    
5695                  ## Clear back to table body context                  ## Clear back to table body context
5696                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5697                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5698                    !!!cp ('t240');                    !!!cp ('t240');
5699                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5700                  }                  }
# Line 4823  sub _tree_construction_main ($) { Line 5720  sub _tree_construction_main ($) {
5720                my $i;                my $i;
5721                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5722                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5723                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_EL) {
5724                    !!!cp ('t241');                    !!!cp ('t241');
5725                    $i = $_;                    $i = $_;
5726                    last INSCOPE;                    last INSCOPE;
5727                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5728                    !!!cp ('t242');                    !!!cp ('t242');
5729                    last INSCOPE;                    last INSCOPE;
5730                  }                  }
5731                } # INSCOPE                } # INSCOPE
5732                unless (defined $i) {                unless (defined $i) {
5733                  !!!cp ('t243');                  !!!cp ('t243');
5734                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
5735                                    text => $token->{tag_name}, token => $token);
5736                  ## Ignore the token                  ## Ignore the token
5737                    !!!nack ('t243.1');
5738                  !!!next-token;                  !!!next-token;
5739                  redo B;                  next B;
5740                }                }
5741                                    
5742                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
# Line 4848  sub _tree_construction_main ($) { Line 5745  sub _tree_construction_main ($) {
5745                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5746                                
5747                !!!next-token;                !!!next-token;
5748                redo B;                next B;
5749              } elsif ({              } elsif ({
5750                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5751                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
# Line 4858  sub _tree_construction_main ($) { Line 5755  sub _tree_construction_main ($) {
5755                  my $i;                  my $i;
5756                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5757                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5758                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5759                      !!!cp ('t247');                      !!!cp ('t247');
5760                      $i = $_;                      $i = $_;
5761                      last INSCOPE;                      last INSCOPE;
5762                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5763                      !!!cp ('t248');                      !!!cp ('t248');
5764                      last INSCOPE;                      last INSCOPE;
5765                    }                    }
5766                  } # INSCOPE                  } # INSCOPE
5767                    unless (defined $i) {                    unless (defined $i) {
5768                      !!!cp ('t249');                      !!!cp ('t249');
5769                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                      !!!parse-error (type => 'unmatched end tag',
5770                                        text => $token->{tag_name}, token => $token);
5771                      ## Ignore the token                      ## Ignore the token
5772                        !!!nack ('t249.1');
5773                      !!!next-token;                      !!!next-token;
5774                      redo B;                      next B;
5775                    }                    }
5776                                    
5777                  ## As if </tr>                  ## As if </tr>
# Line 4882  sub _tree_construction_main ($) { Line 5779  sub _tree_construction_main ($) {
5779                  my $i;                  my $i;
5780                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5781                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5782                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5783                      !!!cp ('t250');                      !!!cp ('t250');
5784                      $i = $_;                      $i = $_;
5785                      last INSCOPE;                      last INSCOPE;
5786                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5787                      !!!cp ('t251');                      !!!cp ('t251');
5788                      last INSCOPE;                      last INSCOPE;
5789                    }                    }
5790                  } # INSCOPE                  } # INSCOPE
5791                    unless (defined $i) {                    unless (defined $i) {
5792                      !!!cp ('t252');                      !!!cp ('t252');
5793                      !!!parse-error (type => 'unmatched end tag:tr', token => $token);                      !!!parse-error (type => 'unmatched end tag',
5794                                        text => 'tr', token => $token);
5795                      ## Ignore the token                      ## Ignore the token
5796                        !!!nack ('t252.1');
5797                      !!!next-token;                      !!!next-token;
5798                      redo B;                      next B;
5799                    }                    }
5800                                    
5801                  ## Clear back to table row context                  ## Clear back to table row context
5802                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5803                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5804                    !!!cp ('t253');                    !!!cp ('t253');
5805  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
5806                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4919  sub _tree_construction_main ($) { Line 5815  sub _tree_construction_main ($) {
5815                my $i;                my $i;
5816                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5817                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5818                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5819                    !!!cp ('t254');                    !!!cp ('t254');
5820                    $i = $_;                    $i = $_;
5821                    last INSCOPE;                    last INSCOPE;
5822                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5823                    !!!cp ('t255');                    !!!cp ('t255');
5824                    last INSCOPE;                    last INSCOPE;
5825                  }                  }
5826                } # INSCOPE                } # INSCOPE
5827                unless (defined $i) {                unless (defined $i) {
5828                  !!!cp ('t256');                  !!!cp ('t256');
5829                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
5830                                    text => $token->{tag_name}, token => $token);
5831                  ## Ignore the token                  ## Ignore the token
5832                    !!!nack ('t256.1');
5833                  !!!next-token;                  !!!next-token;
5834                  redo B;                  next B;
5835                }                }
5836    
5837                ## Clear back to table body context                ## Clear back to table body context
5838                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5839                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5840                  !!!cp ('t257');                  !!!cp ('t257');
5841  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
5842                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 4949  sub _tree_construction_main ($) { Line 5844  sub _tree_construction_main ($) {
5844    
5845                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5846                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5847                  !!!nack ('t257.1');
5848                !!!next-token;                !!!next-token;
5849                redo B;                next B;
5850              } elsif ({              } elsif ({
5851                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
5852                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
5853                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5854                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
5855                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5856                !!!cp ('t258');            !!!cp ('t258');
5857                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
5858                ## Ignore the token                            text => $token->{tag_name}, token => $token);
5859                !!!next-token;            ## Ignore the token
5860                redo B;            !!!nack ('t258.1');
5861               !!!next-token;
5862              next B;
5863          } else {          } else {
5864            !!!cp ('t259');            !!!cp ('t259');
5865            !!!parse-error (type => 'in table:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'in table:/',
5866                              text => $token->{tag_name}, token => $token);
5867    
5868            $insert = $insert_to_foster;            $insert = $insert_to_foster;
5869            #            #
5870          }          }
5871        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5872          unless ($self->{open_elements}->[-1]->[1] eq 'html' and          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5873                  @{$self->{open_elements}} == 1) { # redundant, maybe                  @{$self->{open_elements}} == 1) { # redundant, maybe
5874            !!!parse-error (type => 'in body:#eof', token => $token);            !!!parse-error (type => 'in body:#eof', token => $token);
5875            !!!cp ('t259.1');            !!!cp ('t259.1');
# Line 4992  sub _tree_construction_main ($) { Line 5891  sub _tree_construction_main ($) {
5891                unless (length $token->{data}) {                unless (length $token->{data}) {
5892                  !!!cp ('t260');                  !!!cp ('t260');
5893                  !!!next-token;                  !!!next-token;
5894                  redo B;                  next B;
5895                }                }
5896              }              }
5897                            
# Line 5003  sub _tree_construction_main ($) { Line 5902  sub _tree_construction_main ($) {
5902                !!!cp ('t262');                !!!cp ('t262');
5903                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5904                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5905                  !!!ack ('t262.1');
5906                !!!next-token;                !!!next-token;
5907                redo B;                next B;
5908              } else {              } else {
5909                !!!cp ('t263');                !!!cp ('t263');
5910                #                #
5911              }              }
5912            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
5913              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
5914                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5915                  !!!cp ('t264');                  !!!cp ('t264');
5916                  !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);                  !!!parse-error (type => 'unmatched end tag',
5917                                    text => 'colgroup', token => $token);
5918                  ## Ignore the token                  ## Ignore the token
5919                  !!!next-token;                  !!!next-token;
5920                  redo B;                  next B;
5921                } else {                } else {
5922                  !!!cp ('t265');                  !!!cp ('t265');
5923                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
5924                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5925                  !!!next-token;                  !!!next-token;
5926                  redo B;                              next B;            
5927                }                }
5928              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
5929                !!!cp ('t266');                !!!cp ('t266');
5930                !!!parse-error (type => 'unmatched end tag:col', token => $token);                !!!parse-error (type => 'unmatched end tag',
5931                                  text => 'col', token => $token);
5932                ## Ignore the token                ## Ignore the token
5933                !!!next-token;                !!!next-token;
5934                redo B;                next B;
5935              } else {              } else {
5936                !!!cp ('t267');                !!!cp ('t267');
5937                #                #
5938              }              }
5939        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5940          if ($self->{open_elements}->[-1]->[1] eq 'html' or          if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5941              @{$self->{open_elements}} == 1) { # redundant, maybe              @{$self->{open_elements}} == 1) { # redundant, maybe
5942            !!!cp ('t270.2');            !!!cp ('t270.2');
5943            ## Stop parsing.            ## Stop parsing.
# Line 5046  sub _tree_construction_main ($) { Line 5948  sub _tree_construction_main ($) {
5948            pop @{$self->{open_elements}}; # colgroup            pop @{$self->{open_elements}}; # colgroup
5949            $self->{insertion_mode} = IN_TABLE_IM;            $self->{insertion_mode} = IN_TABLE_IM;
5950            ## Reprocess.            ## Reprocess.
5951            redo B;            next B;
5952          }          }
5953        } else {        } else {
5954          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5955        }        }
5956    
5957            ## As if </colgroup>            ## As if </colgroup>
5958            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5959              !!!cp ('t269');              !!!cp ('t269');
5960  ## TODO: Wrong error type?  ## TODO: Wrong error type?
5961              !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);              !!!parse-error (type => 'unmatched end tag',
5962                                text => 'colgroup', token => $token);
5963              ## Ignore the token              ## Ignore the token
5964                !!!nack ('t269.1');
5965              !!!next-token;              !!!next-token;
5966              redo B;              next B;
5967            } else {            } else {
5968              !!!cp ('t270');              !!!cp ('t270');
5969              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
5970              $self->{insertion_mode} = IN_TABLE_IM;              $self->{insertion_mode} = IN_TABLE_IM;
5971                !!!ack-later;
5972              ## reprocess              ## reprocess
5973              redo B;              next B;
5974            }            }
5975      } elsif ($self->{insertion_mode} & SELECT_IMS) {      } elsif ($self->{insertion_mode} & SELECT_IMS) {
5976        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5977          !!!cp ('t271');          !!!cp ('t271');
5978          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5979          !!!next-token;          !!!next-token;
5980          redo B;          next B;
5981        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5982              if ($token->{tag_name} eq 'option') {          if ($token->{tag_name} eq 'option') {
5983                if ($self->{open_elements}->[-1]->[1] eq 'option') {            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5984                  !!!cp ('t272');              !!!cp ('t272');
5985                  ## As if </option>              ## As if </option>
5986                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5987                } else {            } else {
5988                  !!!cp ('t273');              !!!cp ('t273');
5989                }            }
5990    
5991                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5992                !!!next-token;            !!!nack ('t273.1');
5993                redo B;            !!!next-token;
5994              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
5995                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
5996                  !!!cp ('t274');            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5997                  ## As if </option>              !!!cp ('t274');
5998                  pop @{$self->{open_elements}};              ## As if </option>
5999                } else {              pop @{$self->{open_elements}};
6000                  !!!cp ('t275');            } else {
6001                }              !!!cp ('t275');
6002              }
6003    
6004                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6005                  !!!cp ('t276');              !!!cp ('t276');
6006                  ## As if </optgroup>              ## As if </optgroup>
6007                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6008                } else {            } else {
6009                  !!!cp ('t277');              !!!cp ('t277');
6010                }            }
6011    
6012                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6013                !!!next-token;            !!!nack ('t277.1');
6014                redo B;            !!!next-token;
6015          } elsif ($token->{tag_name} eq 'select' or            next B;
6016                   $token->{tag_name} eq 'input' or          } elsif ({
6017                       select => 1, input => 1, textarea => 1,
6018                     }->{$token->{tag_name}} or
6019                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6020                    {                    {
6021                     caption => 1, table => 1,                     caption => 1, table => 1,
# Line 5115  sub _tree_construction_main ($) { Line 6023  sub _tree_construction_main ($) {
6023                     tr => 1, td => 1, th => 1,                     tr => 1, td => 1, th => 1,
6024                    }->{$token->{tag_name}})) {                    }->{$token->{tag_name}})) {
6025            ## TODO: The type below is not good - <select> is replaced by </select>            ## TODO: The type below is not good - <select> is replaced by </select>
6026            !!!parse-error (type => 'not closed:select', token => $token);            !!!parse-error (type => 'not closed', text => 'select',
6027                              token => $token);
6028            ## NOTE: As if the token were </select> (<select> case) or            ## NOTE: As if the token were </select> (<select> case) or
6029            ## as if there were </select> (otherwise).            ## as if there were </select> (otherwise).
6030                ## have an element in table scope            ## have an element in table scope
6031                my $i;            my $i;
6032                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6033                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6034                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
6035                    !!!cp ('t278');                !!!cp ('t278');
6036                    $i = $_;                $i = $_;
6037                    last INSCOPE;                last INSCOPE;
6038                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6039                            table => 1, html => 1,                !!!cp ('t279');
6040                           }->{$node->[1]}) {                last INSCOPE;
6041                    !!!cp ('t279');              }
6042                    last INSCOPE;            } # INSCOPE
6043                  }            unless (defined $i) {
6044                } # INSCOPE              !!!cp ('t280');
6045                unless (defined $i) {              !!!parse-error (type => 'unmatched end tag',
6046                  !!!cp ('t280');                              text => 'select', token => $token);
6047                  !!!parse-error (type => 'unmatched end tag:select', token => $token);              ## Ignore the token
6048                  ## Ignore the token              !!!nack ('t280.1');
6049                  !!!next-token;              !!!next-token;
6050                  redo B;              next B;
6051                }            }
6052                                
6053                !!!cp ('t281');            !!!cp ('t281');
6054                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6055    
6056                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6057    
6058            if ($token->{tag_name} eq 'select') {            if ($token->{tag_name} eq 'select') {
6059              !!!cp ('t281.2');              !!!nack ('t281.2');
6060              !!!next-token;              !!!next-token;
6061              redo B;              next B;
6062            } else {            } else {
6063              !!!cp ('t281.1');              !!!cp ('t281.1');
6064                !!!ack-later;
6065              ## Reprocess the token.              ## Reprocess the token.
6066              redo B;              next B;
6067            }            }
6068          } else {          } else {
6069            !!!cp ('t282');            !!!cp ('t282');
6070            !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'in select',
6071                              text => $token->{tag_name}, token => $token);
6072            ## Ignore the token            ## Ignore the token
6073              !!!nack ('t282.1');
6074            !!!next-token;            !!!next-token;
6075            redo B;            next B;
6076          }          }
6077        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6078              if ($token->{tag_name} eq 'optgroup') {          if ($token->{tag_name} eq 'optgroup') {
6079                if ($self->{open_elements}->[-1]->[1] eq 'option' and            if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
6080                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
6081                  !!!cp ('t283');              !!!cp ('t283');
6082                  ## As if </option>              ## As if </option>
6083                  splice @{$self->{open_elements}}, -2;              splice @{$self->{open_elements}}, -2;
6084                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6085                  !!!cp ('t284');              !!!cp ('t284');
6086                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6087                } else {            } else {
6088                  !!!cp ('t285');              !!!cp ('t285');
6089                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag',
6090                  ## Ignore the token                              text => $token->{tag_name}, token => $token);
6091                }              ## Ignore the token
6092                !!!next-token;            }
6093                redo B;            !!!nack ('t285.1');
6094              } elsif ($token->{tag_name} eq 'option') {            !!!next-token;
6095                if ($self->{open_elements}->[-1]->[1] eq 'option') {            next B;
6096                  !!!cp ('t286');          } elsif ($token->{tag_name} eq 'option') {
6097                  pop @{$self->{open_elements}};            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6098                } else {              !!!cp ('t286');
6099                  !!!cp ('t287');              pop @{$self->{open_elements}};
6100                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            } else {
6101                  ## Ignore the token              !!!cp ('t287');
6102                }              !!!parse-error (type => 'unmatched end tag',
6103                !!!next-token;                              text => $token->{tag_name}, token => $token);
6104                redo B;              ## Ignore the token
6105              } elsif ($token->{tag_name} eq 'select') {            }
6106                ## have an element in table scope            !!!nack ('t287.1');
6107                my $i;            !!!next-token;
6108                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            next B;
6109                  my $node = $self->{open_elements}->[$_];          } elsif ($token->{tag_name} eq 'select') {
6110                  if ($node->[1] eq $token->{tag_name}) {            ## have an element in table scope
6111                    !!!cp ('t288');            my $i;
6112                    $i = $_;            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6113                    last INSCOPE;              my $node = $self->{open_elements}->[$_];
6114                  } elsif ({              if ($node->[1] & SELECT_EL) {
6115                            table => 1, html => 1,                !!!cp ('t288');
6116                           }->{$node->[1]}) {                $i = $_;
6117                    !!!cp ('t289');                last INSCOPE;
6118                    last INSCOPE;              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6119                  }                !!!cp ('t289');
6120                } # INSCOPE                last INSCOPE;
6121                unless (defined $i) {              }
6122                  !!!cp ('t290');            } # INSCOPE
6123                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            unless (defined $i) {
6124                  ## Ignore the token              !!!cp ('t290');
6125                  !!!next-token;              !!!parse-error (type => 'unmatched end tag',
6126                  redo B;                              text => $token->{tag_name}, token => $token);
6127                }              ## Ignore the token
6128                !!!nack ('t290.1');
6129                !!!next-token;
6130                next B;
6131              }
6132                                
6133                !!!cp ('t291');            !!!cp ('t291');
6134                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6135    
6136                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6137    
6138                !!!next-token;            !!!nack ('t291.1');
6139                redo B;            !!!next-token;
6140              next B;
6141          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6142                   {                   {
6143                    caption => 1, table => 1, tbody => 1,                    caption => 1, table => 1, tbody => 1,
6144                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
6145                   }->{$token->{tag_name}}) {                   }->{$token->{tag_name}}) {
6146  ## TODO: The following is wrong?  ## TODO: The following is wrong?
6147                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
6148                              text => $token->{tag_name}, token => $token);
6149                                
6150                ## have an element in table scope            ## have an element in table scope
6151                my $i;            my $i;
6152                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6153                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6154                  if ($node->[1] eq $token->{tag_name}) {              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6155                    !!!cp ('t292');                !!!cp ('t292');
6156                    $i = $_;                $i = $_;
6157                    last INSCOPE;                last INSCOPE;
6158                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6159                            table => 1, html => 1,                !!!cp ('t293');
6160                           }->{$node->[1]}) {                last INSCOPE;
6161                    !!!cp ('t293');              }
6162                    last INSCOPE;            } # INSCOPE
6163                  }            unless (defined $i) {
6164                } # INSCOPE              !!!cp ('t294');
6165                unless (defined $i) {              ## Ignore the token
6166                  !!!cp ('t294');              !!!nack ('t294.1');
6167                  ## Ignore the token              !!!next-token;
6168                  !!!next-token;              next B;
6169                  redo B;            }
               }  
6170                                
6171                ## As if </select>            ## As if </select>
6172                ## have an element in table scope            ## have an element in table scope
6173                undef $i;            undef $i;
6174                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6175                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6176                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
6177                    !!!cp ('t295');                !!!cp ('t295');
6178                    $i = $_;                $i = $_;
6179                    last INSCOPE;                last INSCOPE;
6180                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
6181  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
6182                    !!!cp ('t296');                !!!cp ('t296');
6183                    last INSCOPE;                last INSCOPE;
6184                  }              }
6185                } # INSCOPE            } # INSCOPE
6186                unless (defined $i) {            unless (defined $i) {
6187                  !!!cp ('t297');              !!!cp ('t297');
6188  ## TODO: The following error type is correct?  ## TODO: The following error type is correct?
6189                  !!!parse-error (type => 'unmatched end tag:select', token => $token);              !!!parse-error (type => 'unmatched end tag',
6190                  ## Ignore the </select> token                              text => 'select', token => $token);
6191                  !!!next-token; ## TODO: ok?              ## Ignore the </select> token
6192                  redo B;              !!!nack ('t297.1');
6193                }              !!!next-token; ## TODO: ok?
6194                next B;
6195              }
6196                                
6197                !!!cp ('t298');            !!!cp ('t298');
6198                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6199    
6200                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6201    
6202                ## reprocess            !!!ack-later;
6203                redo B;            ## reprocess
6204              next B;
6205          } else {          } else {
6206            !!!cp ('t299');            !!!cp ('t299');
6207            !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'in select:/',
6208                              text => $token->{tag_name}, token => $token);
6209            ## Ignore the token            ## Ignore the token
6210              !!!nack ('t299.3');
6211            !!!next-token;            !!!next-token;
6212            redo B;            next B;
6213          }          }
6214        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6215          unless ($self->{open_elements}->[-1]->[1] eq 'html' and          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6216                  @{$self->{open_elements}} == 1) { # redundant, maybe                  @{$self->{open_elements}} == 1) { # redundant, maybe
6217            !!!cp ('t299.1');            !!!cp ('t299.1');
6218            !!!parse-error (type => 'in body:#eof', token => $token);            !!!parse-error (type => 'in body:#eof', token => $token);
# Line 5317  sub _tree_construction_main ($) { Line 6237  sub _tree_construction_main ($) {
6237            unless (length $token->{data}) {            unless (length $token->{data}) {
6238              !!!cp ('t300');              !!!cp ('t300');
6239              !!!next-token;              !!!next-token;
6240              redo B;              next B;
6241            }            }
6242          }          }
6243                    
6244          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6245            !!!cp ('t301');            !!!cp ('t301');
6246            !!!parse-error (type => 'after html:#character', token => $token);            !!!parse-error (type => 'after html:#text', token => $token);
6247    
6248            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
6249          } else {          } else {
# Line 5331  sub _tree_construction_main ($) { Line 6251  sub _tree_construction_main ($) {
6251          }          }
6252                    
6253          ## "after body" insertion mode          ## "after body" insertion mode
6254          !!!parse-error (type => 'after body:#character', token => $token);          !!!parse-error (type => 'after body:#text', token => $token);
6255    
6256          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6257          ## reprocess          ## reprocess
6258          redo B;          next B;
6259        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6260          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6261            !!!cp ('t303');            !!!cp ('t303');
6262            !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'after html',
6263                              text => $token->{tag_name}, token => $token);
6264                        
6265            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
6266          } else {          } else {
# Line 5347  sub _tree_construction_main ($) { Line 6268  sub _tree_construction_main ($) {
6268          }          }
6269    
6270          ## "after body" insertion mode          ## "after body" insertion mode
6271          !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'after body',
6272                            text => $token->{tag_name}, token => $token);
6273    
6274          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6275            !!!ack-later;
6276          ## reprocess          ## reprocess
6277          redo B;          next B;
6278        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6279          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6280            !!!cp ('t305');            !!!cp ('t305');
6281            !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'after html:/',
6282                              text => $token->{tag_name}, token => $token);
6283                        
6284            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6285            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
# Line 5367  sub _tree_construction_main ($) { Line 6291  sub _tree_construction_main ($) {
6291          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
6292            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
6293              !!!cp ('t307');              !!!cp ('t307');
6294              !!!parse-error (type => 'unmatched end tag:html', token => $token);              !!!parse-error (type => 'unmatched end tag',
6295                                text => 'html', token => $token);
6296              ## Ignore the token              ## Ignore the token
6297              !!!next-token;              !!!next-token;
6298              redo B;              next B;
6299            } else {            } else {
6300              !!!cp ('t308');              !!!cp ('t308');
6301              $self->{insertion_mode} = AFTER_HTML_BODY_IM;              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6302              !!!next-token;              !!!next-token;
6303              redo B;              next B;
6304            }            }
6305          } else {          } else {
6306            !!!cp ('t309');            !!!cp ('t309');
6307            !!!parse-error (type => 'after body:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'after body:/',
6308                              text => $token->{tag_name}, token => $token);
6309    
6310            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
6311            ## reprocess            ## reprocess
6312            redo B;            next B;
6313          }          }
6314        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6315          !!!cp ('t309.2');          !!!cp ('t309.2');
# Line 5400  sub _tree_construction_main ($) { Line 6326  sub _tree_construction_main ($) {
6326            unless (length $token->{data}) {            unless (length $token->{data}) {
6327              !!!cp ('t310');              !!!cp ('t310');
6328              !!!next-token;              !!!next-token;
6329              redo B;              next B;
6330            }            }
6331          }          }
6332                    
6333          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
6334            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6335              !!!cp ('t311');              !!!cp ('t311');
6336              !!!parse-error (type => 'in frameset:#character', token => $token);              !!!parse-error (type => 'in frameset:#text', token => $token);
6337            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6338              !!!cp ('t312');              !!!cp ('t312');
6339              !!!parse-error (type => 'after frameset:#character', token => $token);              !!!parse-error (type => 'after frameset:#text', token => $token);
6340            } else { # "after html frameset"            } else { # "after after frameset"
6341              !!!cp ('t313');              !!!cp ('t313');
6342              !!!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);  
6343            }            }
6344                        
6345            ## Ignore the token.            ## Ignore the token.
# Line 5428  sub _tree_construction_main ($) { Line 6350  sub _tree_construction_main ($) {
6350              !!!cp ('t315');              !!!cp ('t315');
6351              !!!next-token;              !!!next-token;
6352            }            }
6353            redo B;            next B;
6354          }          }
6355                    
6356          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
6357        } 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');  
         }  
   
6358          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6359              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6360            !!!cp ('t318');            !!!cp ('t318');
6361            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6362              !!!nack ('t318.1');
6363            !!!next-token;            !!!next-token;
6364            redo B;            next B;
6365          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
6366                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
6367            !!!cp ('t319');            !!!cp ('t319');
6368            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6369            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6370              !!!ack ('t319.1');
6371            !!!next-token;            !!!next-token;
6372            redo B;            next B;
6373          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
6374            !!!cp ('t320');            !!!cp ('t320');
6375            ## NOTE: As if in body.            ## NOTE: As if in head.
6376            $parse_rcdata->(CDATA_CONTENT_MODEL);            $parse_rcdata->(CDATA_CONTENT_MODEL);
6377            redo B;            next B;
6378    
6379              ## NOTE: |<!DOCTYPE HTML><frameset></frameset></html><noframes></noframes>|
6380              ## has no parse error.
6381          } else {          } else {
6382            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6383              !!!cp ('t321');              !!!cp ('t321');
6384              !!!parse-error (type => 'in frameset:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'in frameset',
6385            } else {                              text => $token->{tag_name}, token => $token);
6386              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6387              !!!cp ('t322');              !!!cp ('t322');
6388              !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'after frameset',
6389                                text => $token->{tag_name}, token => $token);
6390              } else { # "after after frameset"
6391                !!!cp ('t322.2');
6392                !!!parse-error (type => 'after after frameset',
6393                                text => $token->{tag_name}, token => $token);
6394            }            }
6395            ## Ignore the token            ## Ignore the token
6396              !!!nack ('t322.1');
6397            !!!next-token;            !!!next-token;
6398            redo B;            next B;
6399          }          }
6400        } 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');  
         }  
   
6401          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6402              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6403            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6404                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
6405              !!!cp ('t325');              !!!cp ('t325');
6406              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag',
6407                                text => $token->{tag_name}, token => $token);
6408              ## Ignore the token              ## Ignore the token
6409              !!!next-token;              !!!next-token;
6410            } else {            } else {
# Line 5499  sub _tree_construction_main ($) { Line 6414  sub _tree_construction_main ($) {
6414            }            }
6415    
6416            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
6417                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6418              !!!cp ('t327');              !!!cp ('t327');
6419              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6420            } else {            } else {
6421              !!!cp ('t328');              !!!cp ('t328');
6422            }            }
6423            redo B;            next B;
6424          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
6425                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6426            !!!cp ('t329');            !!!cp ('t329');
6427            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6428            !!!next-token;            !!!next-token;
6429            redo B;            next B;
6430          } else {          } else {
6431            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6432              !!!cp ('t330');              !!!cp ('t330');
6433              !!!parse-error (type => 'in frameset:/'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'in frameset:/',
6434            } else {                              text => $token->{tag_name}, token => $token);
6435              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6436                !!!cp ('t330.1');
6437                !!!parse-error (type => 'after frameset:/',
6438                                text => $token->{tag_name}, token => $token);
6439              } else { # "after after html"
6440              !!!cp ('t331');              !!!cp ('t331');
6441              !!!parse-error (type => 'after frameset:/'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'after after frameset:/',
6442                                text => $token->{tag_name}, token => $token);
6443            }            }
6444            ## Ignore the token            ## Ignore the token
6445            !!!next-token;            !!!next-token;
6446            redo B;            next B;
6447          }          }
6448        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6449          unless ($self->{open_elements}->[-1]->[1] eq 'html' and          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6450                  @{$self->{open_elements}} == 1) { # redundant, maybe                  @{$self->{open_elements}} == 1) { # redundant, maybe
6451            !!!cp ('t331.1');            !!!cp ('t331.1');
6452            !!!parse-error (type => 'in body:#eof', token => $token);            !!!parse-error (type => 'in body:#eof', token => $token);
# Line 5550  sub _tree_construction_main ($) { Line 6471  sub _tree_construction_main ($) {
6471          !!!cp ('t332');          !!!cp ('t332');
6472          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6473          $script_start_tag->();          $script_start_tag->();
6474          redo B;          next B;
6475        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
6476          !!!cp ('t333');          !!!cp ('t333');
6477          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6478          $parse_rcdata->(CDATA_CONTENT_MODEL);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6479          redo B;          next B;
6480        } elsif ({        } elsif ({
6481                  base => 1, link => 1,                  base => 1, link => 1,
6482                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 5563  sub _tree_construction_main ($) { Line 6484  sub _tree_construction_main ($) {
6484          ## 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
6485          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6486          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6487            !!!ack ('t334.1');
6488          !!!next-token;          !!!next-token;
6489          redo B;          next B;
6490        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
6491          ## 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
6492          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6493          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.
6494    
6495          unless ($self->{confident}) {          unless ($self->{confident}) {
6496            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) {
6497              !!!cp ('t335');              !!!cp ('t335');
6498                ## NOTE: Whether the encoding is supported or not is handled
6499                ## in the {change_encoding} callback.
6500              $self->{change_encoding}              $self->{change_encoding}
6501                  ->($self, $token->{attributes}->{charset}->{value}, $token);                  ->($self, $token->{attributes}->{charset}->{value}, $token);
6502                            
# Line 5581  sub _tree_construction_main ($) { Line 6505  sub _tree_construction_main ($) {
6505                                       $token->{attributes}->{charset}                                       $token->{attributes}->{charset}
6506                                           ->{has_reference});                                           ->{has_reference});
6507            } elsif ($token->{attributes}->{content}) {            } elsif ($token->{attributes}->{content}) {
             ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
6508              if ($token->{attributes}->{content}->{value}              if ($token->{attributes}->{content}->{value}
6509                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                  =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6510                      [\x09-\x0D\x20]*=                      [\x09-\x0D\x20]*=
6511                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6512                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
6513                !!!cp ('t336');                !!!cp ('t336');
6514                  ## NOTE: Whether the encoding is supported or not is handled
6515                  ## in the {change_encoding} callback.
6516                $self->{change_encoding}                $self->{change_encoding}
6517                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6518                $meta_el->[0]->get_attribute_node_ns (undef, 'content')                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
# Line 5613  sub _tree_construction_main ($) { Line 6538  sub _tree_construction_main ($) {
6538            }            }
6539          }          }
6540    
6541            !!!ack ('t338.1');
6542          !!!next-token;          !!!next-token;
6543          redo B;          next B;
6544        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
6545          !!!cp ('t341');          !!!cp ('t341');
6546          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6547          $parse_rcdata->(RCDATA_CONTENT_MODEL);          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6548          redo B;          next B;
6549        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
6550          !!!parse-error (type => 'in body:body', token => $token);          !!!parse-error (type => 'in body', text => 'body', token => $token);
6551                                
6552          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
6553              $self->{open_elements}->[1]->[1] ne 'body') {              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6554            !!!cp ('t342');            !!!cp ('t342');
6555            ## Ignore the token            ## Ignore the token
6556          } else {          } else {
# Line 5638  sub _tree_construction_main ($) { Line 6564  sub _tree_construction_main ($) {
6564              }              }
6565            }            }
6566          }          }
6567            !!!nack ('t343.1');
6568          !!!next-token;          !!!next-token;
6569          redo B;          next B;
6570        } elsif ({        } elsif ({
6571                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6572                  div => 1, dl => 1, fieldset => 1,                  div => 1, dl => 1, fieldset => 1,
# Line 5654  sub _tree_construction_main ($) { Line 6581  sub _tree_construction_main ($) {
6581            !!!cp ('t350');            !!!cp ('t350');
6582            !!!parse-error (type => 'in form:form', token => $token);            !!!parse-error (type => 'in form:form', token => $token);
6583            ## Ignore the token            ## Ignore the token
6584              !!!nack ('t350.1');
6585            !!!next-token;            !!!next-token;
6586            redo B;            next B;
6587          }          }
6588    
6589          ## has a p element in scope          ## has a p element in scope
6590          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6591            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6592              !!!cp ('t344');              !!!cp ('t344');
6593              !!!back-token;              !!!back-token; # <form>
6594              $token = {type => END_TAG_TOKEN, tag_name => 'p',              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6595                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6596              redo B;              next B;
6597            } elsif ({            } elsif ($_->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6598              !!!cp ('t345');              !!!cp ('t345');
6599              last INSCOPE;              last INSCOPE;
6600            }            }
# Line 5677  sub _tree_construction_main ($) { Line 6602  sub _tree_construction_main ($) {
6602                        
6603          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6604          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6605              !!!nack ('t346.1');
6606            !!!next-token;            !!!next-token;
6607            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
6608              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
# Line 5693  sub _tree_construction_main ($) { Line 6619  sub _tree_construction_main ($) {
6619            !!!cp ('t347.1');            !!!cp ('t347.1');
6620            $self->{form_element} = $self->{open_elements}->[-1]->[0];            $self->{form_element} = $self->{open_elements}->[-1]->[0];
6621    
6622              !!!nack ('t347.2');
6623            !!!next-token;            !!!next-token;
6624          } elsif ($token->{tag_name} eq 'table') {          } elsif ($token->{tag_name} eq 'table') {
6625            !!!cp ('t382');            !!!cp ('t382');
# Line 5700  sub _tree_construction_main ($) { Line 6627  sub _tree_construction_main ($) {
6627                        
6628            $self->{insertion_mode} = IN_TABLE_IM;            $self->{insertion_mode} = IN_TABLE_IM;
6629    
6630              !!!nack ('t382.1');
6631            !!!next-token;            !!!next-token;
6632          } elsif ($token->{tag_name} eq 'hr') {          } elsif ($token->{tag_name} eq 'hr') {
6633            !!!cp ('t386');            !!!cp ('t386');
6634            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6635                    
6636              !!!nack ('t386.1');
6637            !!!next-token;            !!!next-token;
6638          } else {          } else {
6639            !!!cp ('t347');            !!!nack ('t347.1');
6640            !!!next-token;            !!!next-token;
6641          }          }
6642          redo B;          next B;
6643        } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {        } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
6644          ## has a p element in scope          ## has a p element in scope
6645          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6646            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6647              !!!cp ('t353');              !!!cp ('t353');
6648              !!!back-token;              !!!back-token; # <x>
6649              $token = {type => END_TAG_TOKEN, tag_name => 'p',              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6650                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6651              redo B;              next B;
6652            } elsif ({            } elsif ($_->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6653              !!!cp ('t354');              !!!cp ('t354');
6654              last INSCOPE;              last INSCOPE;
6655            }            }
# Line 5737  sub _tree_construction_main ($) { Line 6663  sub _tree_construction_main ($) {
6663                            dd => {dt => 1, dd => 1}}->{$token->{tag_name}};                            dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6664          LI: {          LI: {
6665            ## Step 2            ## Step 2
6666            if ($li_or_dtdd->{$node->[1]}) {            if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6667              if ($i != -1) {              if ($i != -1) {
6668                !!!cp ('t355');                !!!cp ('t355');
6669                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'not closed',
6670                                $self->{open_elements}->[-1]->[1], token => $token);                                text => $self->{open_elements}->[-1]->[0]
6671                                      ->manakai_local_name,
6672                                  token => $token);
6673              } else {              } else {
6674                !!!cp ('t356');                !!!cp ('t356');
6675              }              }
# Line 5752  sub _tree_construction_main ($) { Line 6680  sub _tree_construction_main ($) {
6680            }            }
6681                        
6682            ## Step 3            ## Step 3
6683            if (not $formatting_category->{$node->[1]} and            if (not ($node->[1] & FORMATTING_EL) and
6684                #not $phrasing_category->{$node->[1]} and                #not $phrasing_category->{$node->[1]} and
6685                ($special_category->{$node->[1]} or                ($node->[1] & SPECIAL_EL or
6686                 $scoping_category->{$node->[1]}) and                 $node->[1] & SCOPING_EL) and
6687                $node->[1] ne 'address' and $node->[1] ne 'div') {                not ($node->[1] & ADDRESS_EL) and
6688                  not ($node->[1] & DIV_EL)) {
6689              !!!cp ('t358');              !!!cp ('t358');
6690              last LI;              last LI;
6691            }            }
# Line 5769  sub _tree_construction_main ($) { Line 6698  sub _tree_construction_main ($) {
6698          } # LI          } # LI
6699                        
6700          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6701            !!!nack ('t359.1');
6702          !!!next-token;          !!!next-token;
6703          redo B;          next B;
6704        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
6705          ## has a p element in scope          ## has a p element in scope
6706          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6707            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6708              !!!cp ('t367');              !!!cp ('t367');
6709              !!!back-token;              !!!back-token; # <plaintext>
6710              $token = {type => END_TAG_TOKEN, tag_name => 'p',              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6711                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6712              redo B;              next B;
6713            } elsif ({            } elsif ($_->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6714              !!!cp ('t368');              !!!cp ('t368');
6715              last INSCOPE;              last INSCOPE;
6716            }            }
# Line 5793  sub _tree_construction_main ($) { Line 6720  sub _tree_construction_main ($) {
6720                        
6721          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
6722                        
6723            !!!nack ('t368.1');
6724          !!!next-token;          !!!next-token;
6725          redo B;          next B;
6726        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
6727          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
6728            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
6729            if ($node->[1] eq 'a') {            if ($node->[1] & A_EL) {
6730              !!!cp ('t371');              !!!cp ('t371');
6731              !!!parse-error (type => 'in a:a', token => $token);              !!!parse-error (type => 'in a:a', token => $token);
6732                            
6733              !!!back-token;              !!!back-token; # <a>
6734              $token = {type => END_TAG_TOKEN, tag_name => 'a',              $token = {type => END_TAG_TOKEN, tag_name => 'a',
6735                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6736              $formatting_end_tag->($token);              $formatting_end_tag->($token);
# Line 5833  sub _tree_construction_main ($) { Line 6761  sub _tree_construction_main ($) {
6761          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6762          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6763    
6764            !!!nack ('t374.1');
6765          !!!next-token;          !!!next-token;
6766          redo B;          next B;
6767        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
6768          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6769    
6770          ## has a |nobr| element in scope          ## has a |nobr| element in scope
6771          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6772            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6773            if ($node->[1] eq 'nobr') {            if ($node->[1] & NOBR_EL) {
6774              !!!cp ('t376');              !!!cp ('t376');
6775              !!!parse-error (type => 'in nobr:nobr', token => $token);              !!!parse-error (type => 'in nobr:nobr', token => $token);
6776              !!!back-token;              !!!back-token; # <nobr>
6777              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
6778                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6779              redo B;              next B;
6780            } 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]}) {  
6781              !!!cp ('t377');              !!!cp ('t377');
6782              last INSCOPE;              last INSCOPE;
6783            }            }
# Line 5860  sub _tree_construction_main ($) { Line 6786  sub _tree_construction_main ($) {
6786          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6787          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6788                    
6789            !!!nack ('t377.1');
6790          !!!next-token;          !!!next-token;
6791          redo B;          next B;
6792        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
6793          ## has a button element in scope          ## has a button element in scope
6794          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6795            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6796            if ($node->[1] eq 'button') {            if ($node->[1] & BUTTON_EL) {
6797              !!!cp ('t378');              !!!cp ('t378');
6798              !!!parse-error (type => 'in button:button', token => $token);              !!!parse-error (type => 'in button:button', token => $token);
6799              !!!back-token;              !!!back-token; # <button>
6800              $token = {type => END_TAG_TOKEN, tag_name => 'button',              $token = {type => END_TAG_TOKEN, tag_name => 'button',
6801                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6802              redo B;              next B;
6803            } 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]}) {  
6804              !!!cp ('t379');              !!!cp ('t379');
6805              last INSCOPE;              last INSCOPE;
6806            }            }
# Line 5890  sub _tree_construction_main ($) { Line 6814  sub _tree_construction_main ($) {
6814    
6815          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
6816    
6817            !!!nack ('t379.1');
6818          !!!next-token;          !!!next-token;
6819          redo B;          next B;
6820        } elsif ({        } elsif ({
6821                  xmp => 1,                  xmp => 1,
6822                  iframe => 1,                  iframe => 1,
6823                  noembed => 1,                  noembed => 1,
6824                  noframes => 1,                  noframes => 1, ## NOTE: This is an "as if in head" code clone.
6825                  noscript => 0, ## TODO: 1 if scripting is enabled                  noscript => 0, ## TODO: 1 if scripting is enabled
6826                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6827          if ($token->{tag_name} eq 'xmp') {          if ($token->{tag_name} eq 'xmp') {
# Line 5907  sub _tree_construction_main ($) { Line 6832  sub _tree_construction_main ($) {
6832          }          }
6833          ## NOTE: There is an "as if in body" code clone.          ## NOTE: There is an "as if in body" code clone.
6834          $parse_rcdata->(CDATA_CONTENT_MODEL);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6835          redo B;          next B;
6836        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
6837          !!!parse-error (type => 'isindex', token => $token);          !!!parse-error (type => 'isindex', token => $token);
6838                    
6839          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
6840            !!!cp ('t389');            !!!cp ('t389');
6841            ## Ignore the token            ## Ignore the token
6842              !!!nack ('t389'); ## NOTE: Not acknowledged.
6843            !!!next-token;            !!!next-token;
6844            redo B;            next B;
6845          } else {          } else {
6846              !!!ack ('t391.1');
6847    
6848            my $at = $token->{attributes};            my $at = $token->{attributes};
6849            my $form_attrs;            my $form_attrs;
6850            $form_attrs->{action} = $at->{action} if $at->{action};            $form_attrs->{action} = $at->{action} if $at->{action};
# Line 5938  sub _tree_construction_main ($) { Line 6866  sub _tree_construction_main ($) {
6866            if ($prompt_attr) {            if ($prompt_attr) {
6867              !!!cp ('t390');              !!!cp ('t390');
6868              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
6869                             line => $token->{line}, column => $token->{column}};                             #line => $token->{line}, column => $token->{column},
6870                              };
6871            } else {            } else {
6872              !!!cp ('t391');              !!!cp ('t391');
6873              push @tokens, {type => CHARACTER_TOKEN,              push @tokens, {type => CHARACTER_TOKEN,
6874                             data => 'This is a searchable index. Insert your search keywords here: ',                             data => 'This is a searchable index. Insert your search keywords here: ',
6875                             line => $token->{line}, column => $token->{column}}; # SHOULD                             #line => $token->{line}, column => $token->{column},
6876                              }; # SHOULD
6877              ## TODO: make this configurable              ## TODO: make this configurable
6878            }            }
6879            push @tokens,            push @tokens,
# Line 5958  sub _tree_construction_main ($) { Line 6888  sub _tree_construction_main ($) {
6888                           line => $token->{line}, column => $token->{column}},                           line => $token->{line}, column => $token->{column}},
6889                          {type => END_TAG_TOKEN, tag_name => 'form',                          {type => END_TAG_TOKEN, tag_name => 'form',
6890                           line => $token->{line}, column => $token->{column}};                           line => $token->{line}, column => $token->{column}};
           $token = shift @tokens;  
6891            !!!back-token (@tokens);            !!!back-token (@tokens);
6892            redo B;            !!!next-token;
6893              next B;
6894          }          }
6895        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
6896          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
6897          my $el;          my $el;
6898          !!!create-element ($el, $token->{tag_name}, $token->{attributes}, $token);          !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
6899                    
6900          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
6901          $self->{content_model} = RCDATA_CONTENT_MODEL;          $self->{content_model} = RCDATA_CONTENT_MODEL;
# Line 5974  sub _tree_construction_main ($) { Line 6904  sub _tree_construction_main ($) {
6904          $insert->($el);          $insert->($el);
6905                    
6906          my $text = '';          my $text = '';
6907            !!!nack ('t392.1');
6908          !!!next-token;          !!!next-token;
6909          if ($token->{type} == CHARACTER_TOKEN) {          if ($token->{type} == CHARACTER_TOKEN) {
6910            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
# Line 6004  sub _tree_construction_main ($) { Line 6935  sub _tree_construction_main ($) {
6935            ## Ignore the token            ## Ignore the token
6936          } else {          } else {
6937            !!!cp ('t398');            !!!cp ('t398');
6938            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);            !!!parse-error (type => 'in RCDATA:#eof', token => $token);
6939          }          }
6940          !!!next-token;          !!!next-token;
6941            next B;
6942          } elsif ($token->{tag_name} eq 'rt' or
6943                   $token->{tag_name} eq 'rp') {
6944            ## has a |ruby| element in scope
6945            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6946              my $node = $self->{open_elements}->[$_];
6947              if ($node->[1] & RUBY_EL) {
6948                !!!cp ('t398.1');
6949                ## generate implied end tags
6950                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
6951                  !!!cp ('t398.2');
6952                  pop @{$self->{open_elements}};
6953                }
6954                unless ($self->{open_elements}->[-1]->[1] & RUBY_EL) {
6955                  !!!cp ('t398.3');
6956                  !!!parse-error (type => 'not closed',
6957                                  text => $self->{open_elements}->[-1]->[0]
6958                                      ->manakai_local_name,
6959                                  token => $token);
6960                  pop @{$self->{open_elements}}
6961                      while not $self->{open_elements}->[-1]->[1] & RUBY_EL;
6962                }
6963                last INSCOPE;
6964              } elsif ($node->[1] & SCOPING_EL) {
6965                !!!cp ('t398.4');
6966                last INSCOPE;
6967              }
6968            } # INSCOPE
6969    
6970            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6971    
6972            !!!nack ('t398.5');
6973            !!!next-token;
6974          redo B;          redo B;
6975          } elsif ($token->{tag_name} eq 'math' or
6976                   $token->{tag_name} eq 'svg') {
6977            $reconstruct_active_formatting_elements->($insert_to_current);
6978    
6979            ## "Adjust MathML attributes" ('math' only) - done in insert-element-f
6980    
6981            ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
6982    
6983            ## "adjust foreign attributes" - done in insert-element-f
6984            
6985            !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
6986            
6987            if ($self->{self_closing}) {
6988              pop @{$self->{open_elements}};
6989              !!!ack ('t398.1');
6990            } else {
6991              !!!cp ('t398.2');
6992              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
6993              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
6994              ## mode, "in body" (not "in foreign content") secondary insertion
6995              ## mode, maybe.
6996            }
6997    
6998            !!!next-token;
6999            next B;
7000        } elsif ({        } elsif ({
7001                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7002                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 6015  sub _tree_construction_main ($) { Line 7004  sub _tree_construction_main ($) {
7004                  thead => 1, tr => 1,                  thead => 1, tr => 1,
7005                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7006          !!!cp ('t401');          !!!cp ('t401');
7007          !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'in body',
7008                            text => $token->{tag_name}, token => $token);
7009          ## Ignore the token          ## Ignore the token
7010            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
7011          !!!next-token;          !!!next-token;
7012          redo B;          next B;
7013                    
7014          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
7015        } else {        } else {
# Line 6040  sub _tree_construction_main ($) { Line 7031  sub _tree_construction_main ($) {
7031              }->{$token->{tag_name}}) {              }->{$token->{tag_name}}) {
7032            !!!cp ('t380');            !!!cp ('t380');
7033            push @$active_formatting_elements, ['#marker', ''];            push @$active_formatting_elements, ['#marker', ''];
7034              !!!nack ('t380.1');
7035          } elsif ({          } elsif ({
7036                    b => 1, big => 1, em => 1, font => 1, i => 1,                    b => 1, big => 1, em => 1, font => 1, i => 1,
7037                    s => 1, small => 1, strile => 1,                    s => 1, small => 1, strile => 1,
# Line 6047  sub _tree_construction_main ($) { Line 7039  sub _tree_construction_main ($) {
7039                   }->{$token->{tag_name}}) {                   }->{$token->{tag_name}}) {
7040            !!!cp ('t375');            !!!cp ('t375');
7041            push @$active_formatting_elements, $self->{open_elements}->[-1];            push @$active_formatting_elements, $self->{open_elements}->[-1];
7042              !!!nack ('t375.1');
7043          } elsif ($token->{tag_name} eq 'input') {          } elsif ($token->{tag_name} eq 'input') {
7044            !!!cp ('t388');            !!!cp ('t388');
7045            ## TODO: associate with $self->{form_element} if defined            ## TODO: associate with $self->{form_element} if defined
7046            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
7047              !!!ack ('t388.2');
7048          } elsif ({          } elsif ({
7049                    area => 1, basefont => 1, bgsound => 1, br => 1,                    area => 1, basefont => 1, bgsound => 1, br => 1,
7050                    embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                    embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
# Line 6058  sub _tree_construction_main ($) { Line 7052  sub _tree_construction_main ($) {
7052                   }->{$token->{tag_name}}) {                   }->{$token->{tag_name}}) {
7053            !!!cp ('t388.1');            !!!cp ('t388.1');
7054            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
7055              !!!ack ('t388.3');
7056          } elsif ($token->{tag_name} eq 'select') {          } elsif ($token->{tag_name} eq 'select') {
7057            ## TODO: associate with $self->{form_element} if defined            ## TODO: associate with $self->{form_element} if defined
7058                    
# Line 6070  sub _tree_construction_main ($) { Line 7065  sub _tree_construction_main ($) {
7065              !!!cp ('t400.2');              !!!cp ('t400.2');
7066              $self->{insertion_mode} = IN_SELECT_IM;              $self->{insertion_mode} = IN_SELECT_IM;
7067            }            }
7068              !!!nack ('t400.3');
7069          } else {          } else {
7070            !!!cp ('t402');            !!!nack ('t402');
7071          }          }
7072                    
7073          !!!next-token;          !!!next-token;
7074          redo B;          next B;
7075        }        }
7076      } elsif ($token->{type} == END_TAG_TOKEN) {      } elsif ($token->{type} == END_TAG_TOKEN) {
7077        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
# Line 6083  sub _tree_construction_main ($) { Line 7079  sub _tree_construction_main ($) {
7079          my $i;          my $i;
7080          INSCOPE: {          INSCOPE: {
7081            for (reverse @{$self->{open_elements}}) {            for (reverse @{$self->{open_elements}}) {
7082              if ($_->[1] eq 'body') {              if ($_->[1] & BODY_EL) {
7083                !!!cp ('t405');                !!!cp ('t405');
7084                $i = $_;                $i = $_;
7085                last INSCOPE;                last INSCOPE;
7086              } elsif ({              } elsif ($_->[1] & SCOPING_EL) {
                       applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                       button => 1, marquee => 1, object => 1, html => 1,  
                      }->{$_->[1]}) {  
7087                !!!cp ('t405.1');                !!!cp ('t405.1');
7088                last;                last;
7089              }              }
7090            }            }
7091    
7092            !!!parse-error (type => 'start tag not allowed',            !!!parse-error (type => 'start tag not allowed',
7093                            value => $token->{tag_name}, token => $token);                            text => $token->{tag_name}, token => $token);
7094            ## NOTE: Ignore the token.            ## NOTE: Ignore the token.
7095            !!!next-token;            !!!next-token;
7096            redo B;            next B;
7097          } # INSCOPE          } # INSCOPE
7098    
7099          for (@{$self->{open_elements}}) {          for (@{$self->{open_elements}}) {
7100            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]}) {  
7101              !!!cp ('t403');              !!!cp ('t403');
7102              !!!parse-error (type => 'not closed:'.$_->[1], token => $token);              !!!parse-error (type => 'not closed',
7103                                text => $_->[0]->manakai_local_name,
7104                                token => $token);
7105              last;              last;
7106            } else {            } else {
7107              !!!cp ('t404');              !!!cp ('t404');
# Line 6119  sub _tree_construction_main ($) { Line 7110  sub _tree_construction_main ($) {
7110    
7111          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
7112          !!!next-token;          !!!next-token;
7113          redo B;          next B;
7114        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
7115          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
7116            ## up-to-date, though it has same effect as speced.
7117            if (@{$self->{open_elements}} > 1 and
7118                $self->{open_elements}->[1]->[1] & BODY_EL) {
7119            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
7120            if ($self->{open_elements}->[-1]->[1] ne 'body') {            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
7121              !!!cp ('t406');              !!!cp ('t406');
7122              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1], token => $token);              !!!parse-error (type => 'not closed',
7123                                text => $self->{open_elements}->[1]->[0]
7124                                    ->manakai_local_name,
7125                                token => $token);
7126            } else {            } else {
7127              !!!cp ('t407');              !!!cp ('t407');
7128            }            }
7129            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
7130            ## reprocess            ## reprocess
7131            redo B;            next B;
7132          } else {          } else {
7133            !!!cp ('t408');            !!!cp ('t408');
7134            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
7135                              text => $token->{tag_name}, token => $token);
7136            ## Ignore the token            ## Ignore the token
7137            !!!next-token;            !!!next-token;
7138            redo B;            next B;
7139          }          }
7140        } elsif ({        } elsif ({
7141                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
# Line 6150  sub _tree_construction_main ($) { Line 7148  sub _tree_construction_main ($) {
7148          my $i;          my $i;
7149          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7150            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7151            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7152              !!!cp ('t410');              !!!cp ('t410');
7153              $i = $_;              $i = $_;
7154              last INSCOPE;              last INSCOPE;
7155            } 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]}) {  
7156              !!!cp ('t411');              !!!cp ('t411');
7157              last INSCOPE;              last INSCOPE;
7158            }            }
# Line 6165  sub _tree_construction_main ($) { Line 7160  sub _tree_construction_main ($) {
7160    
7161          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
7162            !!!cp ('t413');            !!!cp ('t413');
7163            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
7164                              text => $token->{tag_name}, token => $token);
7165              ## NOTE: Ignore the token.
7166          } else {          } else {
7167            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7168            while ({            while ({
7169                      ## END_TAG_OPTIONAL_EL
7170                    dd => ($token->{tag_name} ne 'dd'),                    dd => ($token->{tag_name} ne 'dd'),
7171                    dt => ($token->{tag_name} ne 'dt'),                    dt => ($token->{tag_name} ne 'dt'),
7172                    li => ($token->{tag_name} ne 'li'),                    li => ($token->{tag_name} ne 'li'),
7173                    p => 1,                    p => 1,
7174                   }->{$self->{open_elements}->[-1]->[1]}) {                    rt => 1,
7175                      rp => 1,
7176                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
7177              !!!cp ('t409');              !!!cp ('t409');
7178              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
7179            }            }
7180    
7181            ## Step 2.            ## Step 2.
7182            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7183                      ne $token->{tag_name}) {
7184              !!!cp ('t412');              !!!cp ('t412');
7185              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);              !!!parse-error (type => 'not closed',
7186                                text => $self->{open_elements}->[-1]->[0]
7187                                    ->manakai_local_name,
7188                                token => $token);
7189            } else {            } else {
7190              !!!cp ('t414');              !!!cp ('t414');
7191            }            }
# Line 6196  sub _tree_construction_main ($) { Line 7200  sub _tree_construction_main ($) {
7200                }->{$token->{tag_name}};                }->{$token->{tag_name}};
7201          }          }
7202          !!!next-token;          !!!next-token;
7203          redo B;          next B;
7204        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
7205          undef $self->{form_element};          undef $self->{form_element};
7206    
# Line 6204  sub _tree_construction_main ($) { Line 7208  sub _tree_construction_main ($) {
7208          my $i;          my $i;
7209          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7210            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7211            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & FORM_EL) {
7212              !!!cp ('t418');              !!!cp ('t418');
7213              $i = $_;              $i = $_;
7214              last INSCOPE;              last INSCOPE;
7215            } 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]}) {  
7216              !!!cp ('t419');              !!!cp ('t419');
7217              last INSCOPE;              last INSCOPE;
7218            }            }
# Line 6219  sub _tree_construction_main ($) { Line 7220  sub _tree_construction_main ($) {
7220    
7221          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
7222            !!!cp ('t421');            !!!cp ('t421');
7223            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
7224                              text => $token->{tag_name}, token => $token);
7225              ## NOTE: Ignore the token.
7226          } else {          } else {
7227            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7228            while ({            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                   dd => 1, dt => 1, li => 1, p => 1,  
                  }->{$self->{open_elements}->[-1]->[1]}) {  
7229              !!!cp ('t417');              !!!cp ('t417');
7230              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
7231            }            }
7232                        
7233            ## Step 2.            ## Step 2.
7234            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7235                      ne $token->{tag_name}) {
7236              !!!cp ('t417.1');              !!!cp ('t417.1');
7237              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);              !!!parse-error (type => 'not closed',
7238                                text => $self->{open_elements}->[-1]->[0]
7239                                    ->manakai_local_name,
7240                                token => $token);
7241            } else {            } else {
7242              !!!cp ('t420');              !!!cp ('t420');
7243            }              }  
# Line 6242  sub _tree_construction_main ($) { Line 7247  sub _tree_construction_main ($) {
7247          }          }
7248    
7249          !!!next-token;          !!!next-token;
7250          redo B;          next B;
7251        } elsif ({        } elsif ({
7252                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
7253                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 6250  sub _tree_construction_main ($) { Line 7255  sub _tree_construction_main ($) {
7255          my $i;          my $i;
7256          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7257            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7258            if ({            if ($node->[1] & HEADING_EL) {
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
7259              !!!cp ('t423');              !!!cp ('t423');
7260              $i = $_;              $i = $_;
7261              last INSCOPE;              last INSCOPE;
7262            } 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]}) {  
7263              !!!cp ('t424');              !!!cp ('t424');
7264              last INSCOPE;              last INSCOPE;
7265            }            }
# Line 6267  sub _tree_construction_main ($) { Line 7267  sub _tree_construction_main ($) {
7267    
7268          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
7269            !!!cp ('t425.1');            !!!cp ('t425.1');
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              ## NOTE: Ignore the token.
7273          } else {          } else {
7274            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7275            while ({            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                   dd => 1, dt => 1, li => 1, p => 1,  
                  }->{$self->{open_elements}->[-1]->[1]}) {  
7276              !!!cp ('t422');              !!!cp ('t422');
7277              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
7278            }            }
7279                        
7280            ## Step 2.            ## Step 2.
7281            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7282                      ne $token->{tag_name}) {
7283              !!!cp ('t425');              !!!cp ('t425');
7284              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag',
7285                                text => $token->{tag_name}, token => $token);
7286            } else {            } else {
7287              !!!cp ('t426');              !!!cp ('t426');
7288            }            }
# Line 6290  sub _tree_construction_main ($) { Line 7292  sub _tree_construction_main ($) {
7292          }          }
7293                    
7294          !!!next-token;          !!!next-token;
7295          redo B;          next B;
7296        } elsif ($token->{tag_name} eq 'p') {        } elsif ($token->{tag_name} eq 'p') {
7297          ## has an element in scope          ## has an element in scope
7298          my $i;          my $i;
7299          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7300            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7301            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & P_EL) {
7302              !!!cp ('t410.1');              !!!cp ('t410.1');
7303              $i = $_;              $i = $_;
7304              last INSCOPE;              last INSCOPE;
7305            } 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]}) {  
7306              !!!cp ('t411.1');              !!!cp ('t411.1');
7307              last INSCOPE;              last INSCOPE;
7308            }            }
7309          } # INSCOPE          } # INSCOPE
7310    
7311          if (defined $i) {          if (defined $i) {
7312            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7313                      ne $token->{tag_name}) {
7314              !!!cp ('t412.1');              !!!cp ('t412.1');
7315              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);              !!!parse-error (type => 'not closed',
7316                                text => $self->{open_elements}->[-1]->[0]
7317                                    ->manakai_local_name,
7318                                token => $token);
7319            } else {            } else {
7320              !!!cp ('t414.1');              !!!cp ('t414.1');
7321            }            }
# Line 6320  sub _tree_construction_main ($) { Line 7323  sub _tree_construction_main ($) {
7323            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
7324          } else {          } else {
7325            !!!cp ('t413.1');            !!!cp ('t413.1');
7326            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
7327                              text => $token->{tag_name}, token => $token);
7328    
7329            !!!cp ('t415.1');            !!!cp ('t415.1');
7330            ## As if <p>, then reprocess the current token            ## As if <p>, then reprocess the current token
7331            my $el;            my $el;
7332            !!!create-element ($el, 'p',, $token);            !!!create-element ($el, $HTML_NS, 'p',, $token);
7333            $insert->($el);            $insert->($el);
7334            ## NOTE: Not inserted into |$self->{open_elements}|.            ## NOTE: Not inserted into |$self->{open_elements}|.
7335          }          }
7336    
7337          !!!next-token;          !!!next-token;
7338          redo B;          next B;
7339        } elsif ({        } elsif ({
7340                  a => 1,                  a => 1,
7341                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
# Line 6340  sub _tree_construction_main ($) { Line 7344  sub _tree_construction_main ($) {
7344                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7345          !!!cp ('t427');          !!!cp ('t427');
7346          $formatting_end_tag->($token);          $formatting_end_tag->($token);
7347          redo B;          next B;
7348        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
7349          !!!cp ('t428');          !!!cp ('t428');
7350          !!!parse-error (type => 'unmatched end tag:br', token => $token);          !!!parse-error (type => 'unmatched end tag',
7351                            text => 'br', token => $token);
7352    
7353          ## As if <br>          ## As if <br>
7354          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7355                    
7356          my $el;          my $el;
7357          !!!create-element ($el, 'br',, $token);          !!!create-element ($el, $HTML_NS, 'br',, $token);
7358          $insert->($el);          $insert->($el);
7359                    
7360          ## Ignore the token.          ## Ignore the token.
7361          !!!next-token;          !!!next-token;
7362          redo B;          next B;
7363        } elsif ({        } elsif ({
7364                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7365                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 6368  sub _tree_construction_main ($) { Line 7373  sub _tree_construction_main ($) {
7373                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
7374                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7375          !!!cp ('t429');          !!!cp ('t429');
7376          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'unmatched end tag',
7377                            text => $token->{tag_name}, token => $token);
7378          ## Ignore the token          ## Ignore the token
7379          !!!next-token;          !!!next-token;
7380          redo B;          next B;
7381                    
7382          ## ISSUE: Issue on HTML5 new elements in spec          ## ISSUE: Issue on HTML5 new elements in spec
7383                    
# Line 6382  sub _tree_construction_main ($) { Line 7388  sub _tree_construction_main ($) {
7388    
7389          ## Step 2          ## Step 2
7390          S2: {          S2: {
7391            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7392              ## Step 1              ## Step 1
7393              ## generate implied end tags              ## generate implied end tags
7394              while ({              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                     dd => 1, dt => 1, li => 1, p => 1,  
                    }->{$self->{open_elements}->[-1]->[1]}) {  
7395                !!!cp ('t430');                !!!cp ('t430');
7396                ## ISSUE: Can this case be reached?                ## NOTE: |<ruby><rt></ruby>|.
7397                  ## ISSUE: <ruby><rt></rt> will also take this code path,
7398                  ## which seems wrong.
7399                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
7400                  $node_i++;
7401              }              }
7402                    
7403              ## Step 2              ## Step 2
7404              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7405                        ne $token->{tag_name}) {
7406                !!!cp ('t431');                !!!cp ('t431');
7407                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
7408                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                !!!parse-error (type => 'not closed',
7409                                  text => $self->{open_elements}->[-1]->[0]
7410                                      ->manakai_local_name,
7411                                  token => $token);
7412              } else {              } else {
7413                !!!cp ('t432');                !!!cp ('t432');
7414              }              }
7415                            
7416              ## Step 3              ## Step 3
7417              splice @{$self->{open_elements}}, $node_i;              splice @{$self->{open_elements}}, $node_i if $node_i < 0;
7418    
7419              !!!next-token;              !!!next-token;
7420              last S2;              last S2;
7421            } else {            } else {
7422              ## Step 3              ## Step 3
7423              if (not $formatting_category->{$node->[1]} and              if (not ($node->[1] & FORMATTING_EL) and
7424                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
7425                  ($special_category->{$node->[1]} or                  ($node->[1] & SPECIAL_EL or
7426                   $scoping_category->{$node->[1]})) {                   $node->[1] & SCOPING_EL)) {
7427                !!!cp ('t433');                !!!cp ('t433');
7428                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag',
7429                                  text => $token->{tag_name}, token => $token);
7430                ## Ignore the token                ## Ignore the token
7431                !!!next-token;                !!!next-token;
7432                last S2;                last S2;
# Line 6430  sub _tree_construction_main ($) { Line 7442  sub _tree_construction_main ($) {
7442            ## Step 5;            ## Step 5;
7443            redo S2;            redo S2;
7444          } # S2          } # S2
7445          redo B;          next B;
7446        }        }
7447      }      }
7448      redo B;      next B;
7449      } continue { # B
7450        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7451          ## NOTE: The code below is executed in cases where it does not have
7452          ## to be, but it it is harmless even in those cases.
7453          ## has an element in scope
7454          INSCOPE: {
7455            for (reverse 0..$#{$self->{open_elements}}) {
7456              my $node = $self->{open_elements}->[$_];
7457              if ($node->[1] & FOREIGN_EL) {
7458                last INSCOPE;
7459              } elsif ($node->[1] & SCOPING_EL) {
7460                last;
7461              }
7462            }
7463            
7464            ## NOTE: No foreign element in scope.
7465            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7466          } # INSCOPE
7467        }
7468    } # B    } # B
7469    
7470    ## Stop parsing # MUST    ## Stop parsing # MUST
# Line 6441  sub _tree_construction_main ($) { Line 7472  sub _tree_construction_main ($) {
7472    ## TODO: script stuffs    ## TODO: script stuffs
7473  } # _tree_construct_main  } # _tree_construct_main
7474    
7475  sub set_inner_html ($$$) {  sub set_inner_html ($$$;$) {
7476    my $class = shift;    my $class = shift;
7477    my $node = shift;    my $node = shift;
7478    my $s = \$_[0];    my $s = \$_[0];
7479    my $onerror = $_[1];    my $onerror = $_[1];
7480      my $get_wrapper = $_[2] || sub ($) { return $_[0] };
7481    
7482    ## ISSUE: Should {confident} be true?    ## ISSUE: Should {confident} be true?
7483    
# Line 6464  sub set_inner_html ($$$) { Line 7496  sub set_inner_html ($$$) {
7496      }      }
7497    
7498      ## Step 3, 4, 5 # MUST      ## Step 3, 4, 5 # MUST
7499      $class->parse_string ($$s => $node, $onerror);      $class->parse_char_string ($$s => $node, $onerror, $get_wrapper);
7500    } elsif ($nt == 1) {    } elsif ($nt == 1) {
7501      ## TODO: If non-html element      ## TODO: If non-html element
7502    
7503      ## NOTE: Most of this code is copied from |parse_string|      ## NOTE: Most of this code is copied from |parse_string|
7504    
7505    ## TODO: Support for $get_wrapper
7506    
7507      ## Step 1 # MUST      ## Step 1 # MUST
7508      my $this_doc = $node->owner_document;      my $this_doc = $node->owner_document;
7509      my $doc = $this_doc->implementation->create_document;      my $doc = $this_doc->implementation->create_document;
# Line 6479  sub set_inner_html ($$$) { Line 7513  sub set_inner_html ($$$) {
7513    
7514      ## Step 8 # MUST      ## Step 8 # MUST
7515      my $i = 0;      my $i = 0;
7516      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7517      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7518      $p->{set_next_char} = sub {      $p->{set_next_char} = sub {
7519        my $self = shift;        my $self = shift;
7520    
# Line 6489  sub set_inner_html ($$$) { Line 7523  sub set_inner_html ($$$) {
7523    
7524        $self->{next_char} = -1 and return if $i >= length $$s;        $self->{next_char} = -1 and return if $i >= length $$s;
7525        $self->{next_char} = ord substr $$s, $i++, 1;        $self->{next_char} = ord substr $$s, $i++, 1;
7526        $column++;  
7527          ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7528          $p->{column}++;
7529    
7530        if ($self->{next_char} == 0x000A) { # LF        if ($self->{next_char} == 0x000A) { # LF
7531          $line++;          $p->{line}++;
7532          $column = 0;          $p->{column} = 0;
7533          !!!cp ('i1');          !!!cp ('i1');
7534        } elsif ($self->{next_char} == 0x000D) { # CR        } elsif ($self->{next_char} == 0x000D) { # CR
7535          $i++ if substr ($$s, $i, 1) eq "\x0A";          $i++ if substr ($$s, $i, 1) eq "\x0A";
7536          $self->{next_char} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
7537          $line++;          $p->{line}++;
7538          $column = 0;          $p->{column} = 0;
7539          !!!cp ('i2');          !!!cp ('i2');
7540        } elsif ($self->{next_char} > 0x10FFFF) {        } elsif ($self->{next_char} > 0x10FFFF) {
7541          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
# Line 6508  sub set_inner_html ($$$) { Line 7544  sub set_inner_html ($$$) {
7544          !!!cp ('i4');          !!!cp ('i4');
7545          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
7546          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7547          } elsif ($self->{next_char} <= 0x0008 or
7548                   (0x000E <= $self->{next_char} and
7549                    $self->{next_char} <= 0x001F) or
7550                   (0x007F <= $self->{next_char} and
7551                    $self->{next_char} <= 0x009F) or
7552                   (0xD800 <= $self->{next_char} and
7553                    $self->{next_char} <= 0xDFFF) or
7554                   (0xFDD0 <= $self->{next_char} and
7555                    $self->{next_char} <= 0xFDDF) or
7556                   {
7557                    0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
7558                    0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
7559                    0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
7560                    0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
7561                    0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
7562                    0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
7563                    0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
7564                    0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
7565                    0x10FFFE => 1, 0x10FFFF => 1,
7566                   }->{$self->{next_char}}) {
7567            !!!cp ('i4.1');
7568            if ($self->{next_char} < 0x10000) {
7569              !!!parse-error (type => 'control char',
7570                              text => (sprintf 'U+%04X', $self->{next_char}));
7571            } else {
7572              !!!parse-error (type => 'control char',
7573                              text => (sprintf 'U-%08X', $self->{next_char}));
7574            }
7575        }        }
7576      };      };
7577      $p->{prev_char} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];
# Line 6515  sub set_inner_html ($$$) { Line 7579  sub set_inner_html ($$$) {
7579            
7580      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7581        my (%opt) = @_;        my (%opt) = @_;
7582        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7583          my $column = $opt{column};
7584          if (defined $opt{token} and defined $opt{token}->{line}) {
7585            $line = $opt{token}->{line};
7586            $column = $opt{token}->{column};
7587          }
7588          warn "Parse error ($opt{type}) at line $line column $column\n";
7589      };      };
7590      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7591        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7592      };      };
7593            
7594      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
# Line 6542  sub set_inner_html ($$$) { Line 7612  sub set_inner_html ($$$) {
7612          unless defined $p->{content_model};          unless defined $p->{content_model};
7613          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
7614    
7615      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7616          ## TODO: Foreign element OK?
7617    
7618      ## Step 3      ## Step 3
7619      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
# Line 6552  sub set_inner_html ($$$) { Line 7623  sub set_inner_html ($$$) {
7623      $doc->append_child ($root);      $doc->append_child ($root);
7624    
7625      ## Step 5 # MUST      ## Step 5 # MUST
7626      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7627    
7628      undef $p->{head_element};      undef $p->{head_element};
7629    
# Line 6598  sub set_inner_html ($$$) { Line 7669  sub set_inner_html ($$$) {
7669      ## ISSUE: mutation events?      ## ISSUE: mutation events?
7670    
7671      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
7672    
7673        delete $p->{parse_error}; # delete loop
7674    } else {    } else {
7675      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";
7676    }    }

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24