/[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.113 by wakaba, Sun Mar 16 07:07:59 2008 UTC revision 1.163 by wakaba, Sat Sep 13 04:19:56 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];
   my $s;  
     
   if (defined $charset) {  
     require Encode; ## TODO: decode(utf8) don't delete BOM  
     $s = \ (Encode::decode ($charset, $$bytes_s));  
     $self->{input_encoding} = lc $charset; ## TODO: normalize name  
     $self->{confident} = 1;  
   } else {  
     ## TODO: Implement HTML5 detection algorithm  
     require Whatpm::Charset::UniversalCharDet;  
     $charset = Whatpm::Charset::UniversalCharDet->detect_byte_string  
         (substr ($$bytes_s, 0, 1024));  
     $charset ||= 'windows-1252';  
     $s = \ (Encode::decode ($charset, $$bytes_s));  
     $self->{input_encoding} = $charset;  
     $self->{confident} = 0;  
   }  
362    
363    $self->{change_encoding} = sub {    my $onerror = $_[2] || sub {
364      my $self = shift;      my (%opt) = @_;
365      my $charset = lc shift;      warn "Parse error ($opt{type})\n";
366      ## TODO: if $charset is supported    };
367      ## TODO: normalize charset name    $self->{parse_error} = $onerror; # updated later by parse_char_string
368    
369      ## "Change the encoding" algorithm:    my $get_wrapper = $_[3] || sub ($) {
370        return $_[0]; # $_[0] = byte stream handle, returned = arg to char handle
371      ## Step 1        };
372      if ($charset eq 'utf-16') { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?  
373        $charset = 'utf-8';    ## 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      ## Step 2
406      if (defined $self->{input_encoding} and      my $byte_buffer = '';
407          $self->{input_encoding} eq $charset) {      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;        $self->{confident} = 1;
420        return;        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      !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.      ## Step 4
438          ':'.$charset, level => 'w');      ## TODO: <meta charset>
439    
440      ## Step 3      ## Step 5
441      # if (can) {      ## TODO: from history
       ## change the encoding on the fly.  
       #$self->{confident} = 1;  
       #return;  
     # }  
442    
443      ## Step 4      ## Step 6
444      throw Whatpm::HTML::RestartParser (charset => $charset);      require Whatpm::Charset::UniversalCharDet;
445        $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
446            ($byte_buffer);
447        if (defined $charset_name) {
448          $charset = Message::Charset::Info->get_by_html_name ($charset_name);
449    
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;
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 {
511        my $self = shift;
512        $charset_name = shift;
513        my $token = shift;
514    
515        $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    
524          if ($charset->{category} &
525              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          !!!parse-error (type => 'charset label detected',
544                          text => $self->{input_encoding},
545                          value => $charset_name,
546                          level => $self->{level}->{warn},
547                          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}    }; # $self->{change_encoding}
560    
561      my $char_onerror = sub {
562        my (undef, $type, %opt) = @_;
563        !!!parse-error (layer => 'encode',
564                        %opt, type => $type,
565                        line => $self->{line}, column => $self->{column} + 1);
566        if ($opt{octets}) {
567          ${$opt{octets}} = "\x{FFFD}"; # relacement character
568        }
569      };
570    
571      my $wrapped_char_stream = $get_wrapper->($char_stream);
572      $wrapped_char_stream->onerror ($char_onerror);
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 162  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 185  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 228  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 294  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    sub MD_HYPHEN_STATE () { 36 }
808    sub MD_DOCTYPE_STATE () { 37 }
809    sub MD_CDATA_STATE () { 38 }
810    
811  sub DOCTYPE_TOKEN () { 1 }  sub DOCTYPE_TOKEN () { 1 }
812  sub COMMENT_TOKEN () { 2 }  sub COMMENT_TOKEN () { 2 }
# Line 311  sub ROW_IMS ()        { 0b10000000 } Line 824  sub ROW_IMS ()        { 0b10000000 }
824  sub BODY_AFTER_IMS () { 0b100000000 }  sub BODY_AFTER_IMS () { 0b100000000 }
825  sub FRAME_IMS ()      { 0b1000000000 }  sub FRAME_IMS ()      { 0b1000000000 }
826  sub SELECT_IMS ()     { 0b10000000000 }  sub SELECT_IMS ()     { 0b10000000000 }
827    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
828        ## NOTE: "in foreign content" insertion mode is special; it is combined
829        ## with the secondary insertion mode.  In this parser, they are stored
830        ## together in the bit-or'ed form.
831    
832  ## NOTE: "initial" and "before html" insertion modes have no constants.  ## NOTE: "initial" and "before html" insertion modes have no constants.
833    
# Line 342  sub IN_COLUMN_GROUP_IM () { 0b10 } Line 859  sub IN_COLUMN_GROUP_IM () { 0b10 }
859  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
860    my $self = shift;    my $self = shift;
861    $self->{state} = DATA_STATE; # MUST    $self->{state} = DATA_STATE; # MUST
862      #$self->{state_keyword}; # initialized when used
863    $self->{content_model} = PCDATA_CONTENT_MODEL; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
864    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE
865    undef $self->{current_attribute};    undef $self->{current_attribute};
866    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
867    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
868      delete $self->{self_closing};
869    $self->{char} = [];    $self->{char} = [];
870    # $self->{next_char}    # $self->{next_char}
871    !!!next-input-character;    !!!next-input-character;
# Line 367  sub _initialize_tokenizer ($) { Line 886  sub _initialize_tokenizer ($) {
886  ##        ->{value}  ##        ->{value}
887  ##        ->{has_reference} == 1 or 0  ##        ->{has_reference} == 1 or 0
888  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
889    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
890    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
891    ##     while the token is pushed back to the stack.
892    
893  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
894    
# Line 393  sub _initialize_tokenizer ($) { Line 915  sub _initialize_tokenizer ($) {
915    
916  sub _get_next_token ($) {  sub _get_next_token ($) {
917    my $self = shift;    my $self = shift;
918    
919      if ($self->{self_closing}) {
920        !!!parse-error (type => 'nestc', token => $self->{current_token});
921        ## NOTE: The |self_closing| flag is only set by start tag token.
922        ## In addition, when a start tag token is emitted, it is always set to
923        ## |current_token|.
924        delete $self->{self_closing};
925      }
926    
927    if (@{$self->{token}}) {    if (@{$self->{token}}) {
928        $self->{self_closing} = $self->{token}->[0]->{self_closing};
929      return shift @{$self->{token}};      return shift @{$self->{token}};
930    }    }
931    
# Line 465  sub _get_next_token ($) { Line 997  sub _get_next_token ($) {
997        # Anything else        # Anything else
998        my $token = {type => CHARACTER_TOKEN,        my $token = {type => CHARACTER_TOKEN,
999                     data => chr $self->{next_char},                     data => chr $self->{next_char},
1000                     line => $self->{line}, column => $self->{column}};                     line => $self->{line}, column => $self->{column},
1001                      };
1002        ## Stay in the data state        ## Stay in the data state
1003        !!!next-input-character;        !!!next-input-character;
1004    
# Line 485  sub _get_next_token ($) { Line 1018  sub _get_next_token ($) {
1018        unless (defined $token) {        unless (defined $token) {
1019          !!!cp (13);          !!!cp (13);
1020          !!!emit ({type => CHARACTER_TOKEN, data => '&',          !!!emit ({type => CHARACTER_TOKEN, data => '&',
1021                    line => $l, column => $c});                    line => $l, column => $c,
1022                     });
1023        } else {        } else {
1024          !!!cp (14);          !!!cp (14);
1025          !!!emit ($token);          !!!emit ($token);
# Line 506  sub _get_next_token ($) { Line 1040  sub _get_next_token ($) {
1040    
1041            !!!emit ({type => CHARACTER_TOKEN, data => '<',            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1042                      line => $self->{line_prev},                      line => $self->{line_prev},
1043                      column => $self->{column_prev}});                      column => $self->{column_prev},
1044                       });
1045    
1046            redo A;            redo A;
1047          }          }
# Line 544  sub _get_next_token ($) { Line 1079  sub _get_next_token ($) {
1079            redo A;            redo A;
1080          } elsif ($self->{next_char} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
1081            !!!cp (21);            !!!cp (21);
1082            !!!parse-error (type => 'empty start tag');            !!!parse-error (type => 'empty start tag',
1083                              line => $self->{line_prev},
1084                              column => $self->{column_prev});
1085            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1086            !!!next-input-character;            !!!next-input-character;
1087    
1088            !!!emit ({type => CHARACTER_TOKEN, data => '<>',            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
1089                      line => $self->{line_prev},                      line => $self->{line_prev},
1090                      column => $self->{column_prev}});                      column => $self->{column_prev},
1091                       });
1092    
1093            redo A;            redo A;
1094          } elsif ($self->{next_char} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
1095            !!!cp (22);            !!!cp (22);
1096            !!!parse-error (type => 'pio');            !!!parse-error (type => 'pio',
1097                              line => $self->{line_prev},
1098                              column => $self->{column_prev});
1099            $self->{state} = BOGUS_COMMENT_STATE;            $self->{state} = BOGUS_COMMENT_STATE;
1100            $self->{current_token} = {type => COMMENT_TOKEN, data => '',            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1101                                      line => $self->{line_prev},                                      line => $self->{line_prev},
1102                                      column => $self->{column_prev}};                                      column => $self->{column_prev},
1103                                       };
1104            ## $self->{next_char} is intentionally left as is            ## $self->{next_char} is intentionally left as is
1105            redo A;            redo A;
1106          } else {          } else {
1107            !!!cp (23);            !!!cp (23);
1108            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago',
1109                              line => $self->{line_prev},
1110                              column => $self->{column_prev});
1111            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1112            ## reconsume            ## reconsume
1113    
1114            !!!emit ({type => CHARACTER_TOKEN, data => '<',            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1115                      line => $self->{line_prev},                      line => $self->{line_prev},
1116                      column => $self->{column_prev}});                      column => $self->{column_prev},
1117                       });
1118    
1119            redo A;            redo A;
1120          }          }
# Line 599  sub _get_next_token ($) { Line 1143  sub _get_next_token ($) {
1143                $self->{state} = DATA_STATE;                $self->{state} = DATA_STATE;
1144    
1145                !!!emit ({type => CHARACTER_TOKEN, data => '</',                !!!emit ({type => CHARACTER_TOKEN, data => '</',
1146                          line => $l, column => $c});                          line => $l, column => $c,
1147                           });
1148        
1149                redo A;                redo A;
1150              }              }
# Line 619  sub _get_next_token ($) { Line 1164  sub _get_next_token ($) {
1164              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
1165              $self->{state} = DATA_STATE;              $self->{state} = DATA_STATE;
1166              !!!emit ({type => CHARACTER_TOKEN, data => '</',              !!!emit ({type => CHARACTER_TOKEN, data => '</',
1167                        line => $l, column => $c});                        line => $l, column => $c,
1168                         });
1169              redo A;              redo A;
1170            } else {            } else {
1171              !!!cp (27);              !!!cp (27);
# Line 633  sub _get_next_token ($) { Line 1179  sub _get_next_token ($) {
1179            # next-input-character is already done            # next-input-character is already done
1180            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1181            !!!emit ({type => CHARACTER_TOKEN, data => '</',            !!!emit ({type => CHARACTER_TOKEN, data => '</',
1182                      line => $l, column => $c});                      line => $l, column => $c,
1183                       });
1184            redo A;            redo A;
1185          }          }
1186        }        }
# Line 659  sub _get_next_token ($) { Line 1206  sub _get_next_token ($) {
1206          redo A;          redo A;
1207        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1208          !!!cp (31);          !!!cp (31);
1209          !!!parse-error (type => 'empty end tag');          !!!parse-error (type => 'empty end tag',
1210                            line => $self->{line_prev}, ## "<" in "</>"
1211                            column => $self->{column_prev} - 1);
1212          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1213          !!!next-input-character;          !!!next-input-character;
1214          redo A;          redo A;
# Line 670  sub _get_next_token ($) { Line 1219  sub _get_next_token ($) {
1219          # reconsume          # reconsume
1220    
1221          !!!emit ({type => CHARACTER_TOKEN, data => '</',          !!!emit ({type => CHARACTER_TOKEN, data => '</',
1222                    line => $l, column => $c});                    line => $l, column => $c,
1223                     });
1224    
1225          redo A;          redo A;
1226        } else {        } else {
# Line 679  sub _get_next_token ($) { Line 1229  sub _get_next_token ($) {
1229          $self->{state} = BOGUS_COMMENT_STATE;          $self->{state} = BOGUS_COMMENT_STATE;
1230          $self->{current_token} = {type => COMMENT_TOKEN, data => '',          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1231                                    line => $self->{line_prev}, # "<" of "</"                                    line => $self->{line_prev}, # "<" of "</"
1232                                    column => $self->{column_prev} - 1};                                    column => $self->{column_prev} - 1,
1233                                     };
1234          ## $self->{next_char} is intentionally left as is          ## $self->{next_char} is intentionally left as is
1235          redo A;          redo A;
1236        }        }
# Line 696  sub _get_next_token ($) { Line 1247  sub _get_next_token ($) {
1247        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1248          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1249            !!!cp (35);            !!!cp (35);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1250            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1251          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1252            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 729  sub _get_next_token ($) { Line 1278  sub _get_next_token ($) {
1278          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1279          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1280            !!!cp (39);            !!!cp (39);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1281            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1282          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1283            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 751  sub _get_next_token ($) { Line 1298  sub _get_next_token ($) {
1298    
1299          redo A;          redo A;
1300        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1301            !!!cp (42);
1302            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1303          !!!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  
1304          redo A;          redo A;
1305        } else {        } else {
1306          !!!cp (44);          !!!cp (44);
# Line 786  sub _get_next_token ($) { Line 1323  sub _get_next_token ($) {
1323        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1324          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1325            !!!cp (46);            !!!cp (46);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1326            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1327          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1328            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 809  sub _get_next_token ($) { Line 1344  sub _get_next_token ($) {
1344        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{next_char} and
1345                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1346          !!!cp (49);          !!!cp (49);
1347          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{current_attribute}
1348                                value => ''};              = {name => chr ($self->{next_char} + 0x0020),
1349                   value => '',
1350                   line => $self->{line}, column => $self->{column}};
1351          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1352          !!!next-input-character;          !!!next-input-character;
1353          redo A;          redo A;
1354        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1355            !!!cp (50);
1356            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1357          !!!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  
1358          redo A;          redo A;
1359        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1360          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1361          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1362            !!!cp (52);            !!!cp (52);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1363            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1364          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1365            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 864  sub _get_next_token ($) { Line 1389  sub _get_next_token ($) {
1389          } else {          } else {
1390            !!!cp (56);            !!!cp (56);
1391          }          }
1392          $self->{current_attribute} = {name => chr ($self->{next_char}),          $self->{current_attribute}
1393                                value => ''};              = {name => chr ($self->{next_char}),
1394                   value => '',
1395                   line => $self->{line}, column => $self->{column}};
1396          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1397          !!!next-input-character;          !!!next-input-character;
1398          redo A;          redo A;
# Line 875  sub _get_next_token ($) { Line 1402  sub _get_next_token ($) {
1402          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1403              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1404            !!!cp (57);            !!!cp (57);
1405            !!!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});
1406            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1407          } else {          } else {
1408            !!!cp (58);            !!!cp (58);
# Line 904  sub _get_next_token ($) { Line 1431  sub _get_next_token ($) {
1431          $before_leave->();          $before_leave->();
1432          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1433            !!!cp (61);            !!!cp (61);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1434            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1435          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1436            !!!cp (62);            !!!cp (62);
# Line 930  sub _get_next_token ($) { Line 1455  sub _get_next_token ($) {
1455          !!!next-input-character;          !!!next-input-character;
1456          redo A;          redo A;
1457        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1458            !!!cp (64);
1459          $before_leave->();          $before_leave->();
1460            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1461          !!!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  
1462          redo A;          redo A;
1463        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1464          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1465          $before_leave->();          $before_leave->();
1466          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1467            !!!cp (66);            !!!cp (66);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1468            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1469          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1470            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1002  sub _get_next_token ($) { Line 1515  sub _get_next_token ($) {
1515        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1516          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1517            !!!cp (73);            !!!cp (73);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1518            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1519          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1520            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1026  sub _get_next_token ($) { Line 1537  sub _get_next_token ($) {
1537        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{next_char} and
1538                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1539          !!!cp (76);          !!!cp (76);
1540          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{current_attribute}
1541                                value => ''};              = {name => chr ($self->{next_char} + 0x0020),
1542                   value => '',
1543                   line => $self->{line}, column => $self->{column}};
1544          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1545          !!!next-input-character;          !!!next-input-character;
1546          redo A;          redo A;
1547        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1548            !!!cp (77);
1549            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1550          !!!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  
1551          redo A;          redo A;
1552        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1553          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1554          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1555            !!!cp (79);            !!!cp (79);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1556            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1557          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1558            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1073  sub _get_next_token ($) { Line 1573  sub _get_next_token ($) {
1573    
1574          redo A;          redo A;
1575        } else {        } else {
1576          !!!cp (82);          if ($self->{next_char} == 0x0022 or # "
1577          $self->{current_attribute} = {name => chr ($self->{next_char}),              $self->{next_char} == 0x0027) { # '
1578                                value => ''};            !!!cp (78);
1579              !!!parse-error (type => 'bad attribute name');
1580            } else {
1581              !!!cp (82);
1582            }
1583            $self->{current_attribute}
1584                = {name => chr ($self->{next_char}),
1585                   value => '',
1586                   line => $self->{line}, column => $self->{column}};
1587          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1588          !!!next-input-character;          !!!next-input-character;
1589          redo A;                  redo A;        
# Line 1106  sub _get_next_token ($) { Line 1614  sub _get_next_token ($) {
1614          !!!next-input-character;          !!!next-input-character;
1615          redo A;          redo A;
1616        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1617            !!!parse-error (type => 'empty unquoted attribute value');
1618          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1619            !!!cp (87);            !!!cp (87);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1620            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1621          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1622            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1133  sub _get_next_token ($) { Line 1640  sub _get_next_token ($) {
1640          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1641          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1642            !!!cp (90);            !!!cp (90);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1643            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1644          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1645            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1182  sub _get_next_token ($) { Line 1687  sub _get_next_token ($) {
1687          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1688          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1689            !!!cp (97);            !!!cp (97);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1690            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1691          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1692            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1226  sub _get_next_token ($) { Line 1729  sub _get_next_token ($) {
1729          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1730          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1731            !!!cp (103);            !!!cp (103);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1732            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1733          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1734            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1273  sub _get_next_token ($) { Line 1774  sub _get_next_token ($) {
1774        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1775          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1776            !!!cp (109);            !!!cp (109);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1777            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1778          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1779            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1298  sub _get_next_token ($) { Line 1797  sub _get_next_token ($) {
1797          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1798          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1799            !!!cp (112);            !!!cp (112);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1800            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1801          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1802            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1370  sub _get_next_token ($) { Line 1867  sub _get_next_token ($) {
1867        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1868          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1869            !!!cp (119);            !!!cp (119);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1870            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1871          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1872            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1392  sub _get_next_token ($) { Line 1887  sub _get_next_token ($) {
1887    
1888          redo A;          redo A;
1889        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1890            !!!cp (122);
1891            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1892          !!!next-input-character;          !!!next-input-character;
1893          if ($self->{next_char} == 0x003E and # >          redo A;
1894              $self->{current_token}->{type} == START_TAG_TOKEN and        } elsif ($self->{next_char} == -1) {
1895              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {          !!!parse-error (type => 'unclosed tag');
1896            # permitted slash          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1897            !!!cp (122);            !!!cp (122.3);
1898            #            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1899            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1900              if ($self->{current_token}->{attributes}) {
1901                !!!cp (122.1);
1902                !!!parse-error (type => 'end tag attribute');
1903              } else {
1904                ## NOTE: This state should never be reached.
1905                !!!cp (122.2);
1906              }
1907          } else {          } else {
1908            !!!cp (123);            die "$0: $self->{current_token}->{type}: Unknown token type";
           !!!parse-error (type => 'nestc');  
1909          }          }
1910          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = DATA_STATE;
1911          # next-input-character is already done          ## Reconsume.
1912            !!!emit ($self->{current_token}); # start tag or end tag
1913          redo A;          redo A;
1914        } else {        } else {
1915          !!!cp (124);          !!!cp ('124.1');
1916          !!!parse-error (type => 'no space between attributes');          !!!parse-error (type => 'no space between attributes');
1917          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1918          ## reconsume          ## reconsume
1919          redo A;          redo A;
1920        }        }
1921        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1922          if ($self->{next_char} == 0x003E) { # >
1923            if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1924              !!!cp ('124.2');
1925              !!!parse-error (type => 'nestc', token => $self->{current_token});
1926              ## TODO: Different type than slash in start tag
1927              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1928              if ($self->{current_token}->{attributes}) {
1929                !!!cp ('124.4');
1930                !!!parse-error (type => 'end tag attribute');
1931              } else {
1932                !!!cp ('124.5');
1933              }
1934              ## TODO: Test |<title></title/>|
1935            } else {
1936              !!!cp ('124.3');
1937              $self->{self_closing} = 1;
1938            }
1939    
1940            $self->{state} = DATA_STATE;
1941            !!!next-input-character;
1942    
1943            !!!emit ($self->{current_token}); # start tag or end tag
1944    
1945            redo A;
1946          } elsif ($self->{next_char} == -1) {
1947            !!!parse-error (type => 'unclosed tag');
1948            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1949              !!!cp (124.7);
1950              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1951            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1952              if ($self->{current_token}->{attributes}) {
1953                !!!cp (124.5);
1954                !!!parse-error (type => 'end tag attribute');
1955              } else {
1956                ## NOTE: This state should never be reached.
1957                !!!cp (124.6);
1958              }
1959            } else {
1960              die "$0: $self->{current_token}->{type}: Unknown token type";
1961            }
1962            $self->{state} = DATA_STATE;
1963            ## Reconsume.
1964            !!!emit ($self->{current_token}); # start tag or end tag
1965            redo A;
1966          } else {
1967            !!!cp ('124.4');
1968            !!!parse-error (type => 'nestc');
1969            ## TODO: This error type is wrong.
1970            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1971            ## Reconsume.
1972            redo A;
1973          }
1974      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1975        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1976                
# Line 1447  sub _get_next_token ($) { Line 2005  sub _get_next_token ($) {
2005        die "$0: _get_next_token: unexpected case [BC]";        die "$0: _get_next_token: unexpected case [BC]";
2006      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
2007        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
   
       my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1);  
   
       my @next_char;  
       push @next_char, $self->{next_char};  
2008                
2009        if ($self->{next_char} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2010            !!!cp (133);
2011            $self->{state} = MD_HYPHEN_STATE;
2012          !!!next-input-character;          !!!next-input-character;
2013          push @next_char, $self->{next_char};          redo A;
         if ($self->{next_char} == 0x002D) { # -  
           !!!cp (127);  
           $self->{current_token} = {type => COMMENT_TOKEN, data => '',  
                                     line => $l, column => $c};  
           $self->{state} = COMMENT_START_STATE;  
           !!!next-input-character;  
           redo A;  
         } else {  
           !!!cp (128);  
         }  
2014        } elsif ($self->{next_char} == 0x0044 or # D        } elsif ($self->{next_char} == 0x0044 or # D
2015                 $self->{next_char} == 0x0064) { # d                 $self->{next_char} == 0x0064) { # d
2016            ## ASCII case-insensitive.
2017            !!!cp (130);
2018            $self->{state} = MD_DOCTYPE_STATE;
2019            $self->{state_keyword} = chr $self->{next_char};
2020          !!!next-input-character;          !!!next-input-character;
2021          push @next_char, $self->{next_char};          redo A;
2022          if ($self->{next_char} == 0x004F or # O        } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
2023              $self->{next_char} == 0x006F) { # o                 $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
2024            !!!next-input-character;                 $self->{next_char} == 0x005B) { # [
2025            push @next_char, $self->{next_char};          !!!cp (135.4);                
2026            if ($self->{next_char} == 0x0043 or # C          $self->{state} = MD_CDATA_STATE;
2027                $self->{next_char} == 0x0063) { # c          $self->{state_keyword} = '[';
2028              !!!next-input-character;          !!!next-input-character;
2029              push @next_char, $self->{next_char};          redo A;
             if ($self->{next_char} == 0x0054 or # T  
                 $self->{next_char} == 0x0074) { # t  
               !!!next-input-character;  
               push @next_char, $self->{next_char};  
               if ($self->{next_char} == 0x0059 or # Y  
                   $self->{next_char} == 0x0079) { # y  
                 !!!next-input-character;  
                 push @next_char, $self->{next_char};  
                 if ($self->{next_char} == 0x0050 or # P  
                     $self->{next_char} == 0x0070) { # p  
                   !!!next-input-character;  
                   push @next_char, $self->{next_char};  
                   if ($self->{next_char} == 0x0045 or # E  
                       $self->{next_char} == 0x0065) { # e  
                     !!!cp (129);  
                     ## TODO: What a stupid code this is!  
                     $self->{state} = DOCTYPE_STATE;  
                     $self->{current_token} = {type => DOCTYPE_TOKEN,  
                                               quirks => 1,  
                                               line => $l, column => $c};  
                     !!!next-input-character;  
                     redo A;  
                   } else {  
                     !!!cp (130);  
                   }  
                 } else {  
                   !!!cp (131);  
                 }  
               } else {  
                 !!!cp (132);  
               }  
             } else {  
               !!!cp (133);  
             }  
           } else {  
             !!!cp (134);  
           }  
         } else {  
           !!!cp (135);  
         }  
2030        } else {        } else {
2031          !!!cp (136);          !!!cp (136);
2032        }        }
2033    
2034        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'bogus comment',
2035        $self->{next_char} = shift @next_char;                        line => $self->{line_prev},
2036        !!!back-next-input-character (@next_char);                        column => $self->{column_prev} - 1);
2037          ## Reconsume.
2038        $self->{state} = BOGUS_COMMENT_STATE;        $self->{state} = BOGUS_COMMENT_STATE;
2039        $self->{current_token} = {type => COMMENT_TOKEN, data => '',        $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2040                                  line => $l, column => $c};                                  line => $self->{line_prev},
2041                                    column => $self->{column_prev} - 1,
2042                                   };
2043        redo A;        redo A;
2044              } elsif ($self->{state} == MD_HYPHEN_STATE) {
2045        ## ISSUE: typos in spec: chacacters, is is a parse error        if ($self->{next_char} == 0x002D) { # -
2046        ## ISSUE: spec is somewhat unclear on "is the first character that will be in the comment"; what is "that will be in the comment" is what the algorithm defines, isn't it?          !!!cp (127);
2047            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2048                                      line => $self->{line_prev},
2049                                      column => $self->{column_prev} - 2,
2050                                     };
2051            $self->{state} = COMMENT_START_STATE;
2052            !!!next-input-character;
2053            redo A;
2054          } else {
2055            !!!cp (128);
2056            !!!parse-error (type => 'bogus comment',
2057                            line => $self->{line_prev},
2058                            column => $self->{column_prev} - 2);
2059            $self->{state} = BOGUS_COMMENT_STATE;
2060            ## Reconsume.
2061            $self->{current_token} = {type => COMMENT_TOKEN,
2062                                      data => '-',
2063                                      line => $self->{line_prev},
2064                                      column => $self->{column_prev} - 2,
2065                                     };
2066            redo A;
2067          }
2068        } elsif ($self->{state} == MD_DOCTYPE_STATE) {
2069          ## ASCII case-insensitive.
2070          if ($self->{next_char} == [
2071                undef,
2072                0x004F, # O
2073                0x0043, # C
2074                0x0054, # T
2075                0x0059, # Y
2076                0x0050, # P
2077              ]->[length $self->{state_keyword}] or
2078              $self->{next_char} == [
2079                undef,
2080                0x006F, # o
2081                0x0063, # c
2082                0x0074, # t
2083                0x0079, # y
2084                0x0070, # p
2085              ]->[length $self->{state_keyword}]) {
2086            !!!cp (131);
2087            ## Stay in the state.
2088            $self->{state_keyword} .= chr $self->{next_char};
2089            !!!next-input-character;
2090            redo A;
2091          } elsif ((length $self->{state_keyword}) == 6 and
2092                   ($self->{next_char} == 0x0045 or # E
2093                    $self->{next_char} == 0x0065)) { # e
2094            !!!cp (129);
2095            $self->{state} = DOCTYPE_STATE;
2096            $self->{current_token} = {type => DOCTYPE_TOKEN,
2097                                      quirks => 1,
2098                                      line => $self->{line_prev},
2099                                      column => $self->{column_prev} - 7,
2100                                     };
2101            !!!next-input-character;
2102            redo A;
2103          } else {
2104            !!!cp (132);        
2105            !!!parse-error (type => 'bogus comment',
2106                            line => $self->{line_prev},
2107                            column => $self->{column_prev} - 1 - length $self->{state_keyword});
2108            $self->{state} = BOGUS_COMMENT_STATE;
2109            ## Reconsume.
2110            $self->{current_token} = {type => COMMENT_TOKEN,
2111                                      data => $self->{state_keyword},
2112                                      line => $self->{line_prev},
2113                                      column => $self->{column_prev} - 1 - length $self->{state_keyword},
2114                                     };
2115            redo A;
2116          }
2117        } elsif ($self->{state} == MD_CDATA_STATE) {
2118          if ($self->{next_char} == {
2119                '[' => 0x0043, # C
2120                '[C' => 0x0044, # D
2121                '[CD' => 0x0041, # A
2122                '[CDA' => 0x0054, # T
2123                '[CDAT' => 0x0041, # A
2124              }->{$self->{state_keyword}}) {
2125            !!!cp (135.1);
2126            ## Stay in the state.
2127            $self->{state_keyword} .= chr $self->{next_char};
2128            !!!next-input-character;
2129            redo A;
2130          } elsif ($self->{state_keyword} eq '[CDATA' and
2131                   $self->{next_char} == 0x005B) { # [
2132            !!!cp (135.2);
2133            $self->{state} = CDATA_BLOCK_STATE;
2134            !!!next-input-character;
2135            redo A;
2136          } else {
2137            !!!cp (135.3);
2138            !!!parse-error (type => 'bogus comment',
2139                            line => $self->{line_prev},
2140                            column => $self->{column_prev} - 1 - length $self->{state_keyword});
2141            $self->{state} = BOGUS_COMMENT_STATE;
2142            ## Reconsume.
2143            $self->{current_token} = {type => COMMENT_TOKEN,
2144                                      data => $self->{state_keyword},
2145                                      line => $self->{line_prev},
2146                                      column => $self->{column_prev} - 1 - length $self->{state_keyword},
2147                                     };
2148            redo A;
2149          }
2150      } elsif ($self->{state} == COMMENT_START_STATE) {      } elsif ($self->{state} == COMMENT_START_STATE) {
2151        if ($self->{next_char} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2152          !!!cp (137);          !!!cp (137);
# Line 1651  sub _get_next_token ($) { Line 2266  sub _get_next_token ($) {
2266          redo A;          redo A;
2267        } elsif ($self->{next_char} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
2268          !!!cp (152);          !!!cp (152);
2269          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment',
2270                            line => $self->{line_prev},
2271                            column => $self->{column_prev});
2272          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
2273          ## Stay in the state          ## Stay in the state
2274          !!!next-input-character;          !!!next-input-character;
# Line 1667  sub _get_next_token ($) { Line 2284  sub _get_next_token ($) {
2284          redo A;          redo A;
2285        } else {        } else {
2286          !!!cp (154);          !!!cp (154);
2287          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment',
2288                            line => $self->{line_prev},
2289                            column => $self->{column_prev});
2290          $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment          $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
2291          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2292          !!!next-input-character;          !!!next-input-character;
# Line 2112  sub _get_next_token ($) { Line 2731  sub _get_next_token ($) {
2731          redo A;          redo A;
2732        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2733          !!!cp (208);          !!!cp (208);
2734          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2735    
2736          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2737          !!!next-input-character;          !!!next-input-character;
# Line 2148  sub _get_next_token ($) { Line 2767  sub _get_next_token ($) {
2767          redo A;          redo A;
2768        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2769          !!!cp (212);          !!!cp (212);
2770          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2771    
2772          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2773          !!!next-input-character;          !!!next-input-character;
# Line 2196  sub _get_next_token ($) { Line 2815  sub _get_next_token ($) {
2815        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
2816          !!!cp (217);          !!!cp (217);
2817          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
   
2818          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2819          ## reconsume          ## reconsume
2820    
# Line 2237  sub _get_next_token ($) { Line 2855  sub _get_next_token ($) {
2855          !!!next-input-character;          !!!next-input-character;
2856          redo A;          redo A;
2857        }        }
2858        } elsif ($self->{state} == CDATA_BLOCK_STATE) {
2859          my $s = '';
2860          
2861          my ($l, $c) = ($self->{line}, $self->{column});
2862    
2863          CS: while ($self->{next_char} != -1) {
2864            if ($self->{next_char} == 0x005D) { # ]
2865              !!!next-input-character;
2866              if ($self->{next_char} == 0x005D) { # ]
2867                !!!next-input-character;
2868                MDC: {
2869                  if ($self->{next_char} == 0x003E) { # >
2870                    !!!cp (221.1);
2871                    !!!next-input-character;
2872                    last CS;
2873                  } elsif ($self->{next_char} == 0x005D) { # ]
2874                    !!!cp (221.2);
2875                    $s .= ']';
2876                    !!!next-input-character;
2877                    redo MDC;
2878                  } else {
2879                    !!!cp (221.3);
2880                    $s .= ']]';
2881                    #
2882                  }
2883                } # MDC
2884              } else {
2885                !!!cp (221.4);
2886                $s .= ']';
2887                #
2888              }
2889            } else {
2890              !!!cp (221.5);
2891              #
2892            }
2893            $s .= chr $self->{next_char};
2894            !!!next-input-character;
2895          } # CS
2896    
2897          $self->{state} = DATA_STATE;
2898          ## next-input-character done or EOF, which is reconsumed.
2899    
2900          if (length $s) {
2901            !!!cp (221.6);
2902            !!!emit ({type => CHARACTER_TOKEN, data => $s,
2903                      line => $l, column => $c});
2904          } else {
2905            !!!cp (221.7);
2906          }
2907    
2908          redo A;
2909    
2910          ## ISSUE: "text tokens" in spec.
2911          ## TODO: Streaming support
2912      } else {      } else {
2913        die "$0: $self->{state}: Unknown state";        die "$0: $self->{state}: Unknown state";
2914      }      }
# Line 2304  sub _tokenize_attempt_to_consume_an_enti Line 2976  sub _tokenize_attempt_to_consume_an_enti
2976    
2977          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2978            !!!cp (1008);            !!!cp (1008);
2979            !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);            !!!parse-error (type => 'invalid character reference',
2980                              text => (sprintf 'U+%04X', $code),
2981                              line => $l, column => $c);
2982            $code = 0xFFFD;            $code = 0xFFFD;
2983          } elsif ($code > 0x10FFFF) {          } elsif ($code > 0x10FFFF) {
2984            !!!cp (1009);            !!!cp (1009);
2985            !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);            !!!parse-error (type => 'invalid character reference',
2986                              text => (sprintf 'U-%08X', $code),
2987                              line => $l, column => $c);
2988            $code = 0xFFFD;            $code = 0xFFFD;
2989          } elsif ($code == 0x000D) {          } elsif ($code == 0x000D) {
2990            !!!cp (1010);            !!!cp (1010);
# Line 2316  sub _tokenize_attempt_to_consume_an_enti Line 2992  sub _tokenize_attempt_to_consume_an_enti
2992            $code = 0x000A;            $code = 0x000A;
2993          } elsif (0x80 <= $code and $code <= 0x9F) {          } elsif (0x80 <= $code and $code <= 0x9F) {
2994            !!!cp (1011);            !!!cp (1011);
2995            !!!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);
2996            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
2997          }          }
2998    
2999          return {type => CHARACTER_TOKEN, data => chr $code,          return {type => CHARACTER_TOKEN, data => chr $code,
3000                  has_reference => 1, line => $l, column => $c};                  has_reference => 1,
3001                    line => $l, column => $c,
3002                   };
3003        } # X        } # X
3004      } elsif (0x0030 <= $self->{next_char} and      } elsif (0x0030 <= $self->{next_char} and
3005               $self->{next_char} <= 0x0039) { # 0..9               $self->{next_char} <= 0x0039) { # 0..9
# Line 2347  sub _tokenize_attempt_to_consume_an_enti Line 3025  sub _tokenize_attempt_to_consume_an_enti
3025    
3026        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3027          !!!cp (1015);          !!!cp (1015);
3028          !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);          !!!parse-error (type => 'invalid character reference',
3029                            text => (sprintf 'U+%04X', $code),
3030                            line => $l, column => $c);
3031          $code = 0xFFFD;          $code = 0xFFFD;
3032        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
3033          !!!cp (1016);          !!!cp (1016);
3034          !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);          !!!parse-error (type => 'invalid character reference',
3035                            text => (sprintf 'U-%08X', $code),
3036                            line => $l, column => $c);
3037          $code = 0xFFFD;          $code = 0xFFFD;
3038        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
3039          !!!cp (1017);          !!!cp (1017);
3040          !!!parse-error (type => 'CR character reference', line => $l, column => $c);          !!!parse-error (type => 'CR character reference',
3041                            line => $l, column => $c);
3042          $code = 0x000A;          $code = 0x000A;
3043        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
3044          !!!cp (1018);          !!!cp (1018);
3045          !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);          !!!parse-error (type => 'C1 character reference',
3046                            text => (sprintf 'U+%04X', $code),
3047                            line => $l, column => $c);
3048          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
3049        }        }
3050                
3051        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,
3052                line => $l, column => $c};                line => $l, column => $c,
3053                 };
3054      } else {      } else {
3055        !!!cp (1019);        !!!cp (1019);
3056        !!!parse-error (type => 'bare nero', line => $l, column => $c);        !!!parse-error (type => 'bare nero', line => $l, column => $c);
# Line 2384  sub _tokenize_attempt_to_consume_an_enti Line 3070  sub _tokenize_attempt_to_consume_an_enti
3070      require Whatpm::_NamedEntityList;      require Whatpm::_NamedEntityList;
3071      our $EntityChar;      our $EntityChar;
3072    
3073      while (length $entity_name < 10 and      while (length $entity_name < 30 and
3074             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
3075             ((0x0041 <= $self->{next_char} and # a             ((0x0041 <= $self->{next_char} and # a
3076               $self->{next_char} <= 0x005A) or # x               $self->{next_char} <= 0x005A) or # x
# Line 2418  sub _tokenize_attempt_to_consume_an_enti Line 3104  sub _tokenize_attempt_to_consume_an_enti
3104      if ($match > 0) {      if ($match > 0) {
3105        !!!cp (1023);        !!!cp (1023);
3106        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
3107                line => $l, column => $c};                line => $l, column => $c,
3108                 };
3109      } elsif ($match < 0) {      } elsif ($match < 0) {
3110        !!!parse-error (type => 'no refc', line => $l, column => $c);        !!!parse-error (type => 'no refc', line => $l, column => $c);
3111        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
3112          !!!cp (1024);          !!!cp (1024);
3113          return {type => CHARACTER_TOKEN, data => '&'.$entity_name,          return {type => CHARACTER_TOKEN, data => '&'.$entity_name,
3114                  line => $l, column => $c};                  line => $l, column => $c,
3115                   };
3116        } else {        } else {
3117          !!!cp (1025);          !!!cp (1025);
3118          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
3119                  line => $l, column => $c};                  line => $l, column => $c,
3120                   };
3121        }        }
3122      } else {      } else {
3123        !!!cp (1026);        !!!cp (1026);
3124        !!!parse-error (type => 'bare ero', line => $l, column => $c);        !!!parse-error (type => 'bare ero', line => $l, column => $c);
3125        ## NOTE: "No characters are consumed" in the spec.        ## NOTE: "No characters are consumed" in the spec.
3126        return {type => CHARACTER_TOKEN, data => '&'.$value,        return {type => CHARACTER_TOKEN, data => '&'.$value,
3127                line => $l, column => $c};                line => $l, column => $c,
3128                 };
3129      }      }
3130    } else {    } else {
3131      !!!cp (1027);      !!!cp (1027);
# Line 2452  sub _initialize_tree_constructor ($) { Line 3142  sub _initialize_tree_constructor ($) {
3142    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
3143    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
3144    $self->{document}->manakai_is_html (1); # MUST    $self->{document}->manakai_is_html (1); # MUST
3145      $self->{document}->set_user_data (manakai_source_line => 1);
3146      $self->{document}->set_user_data (manakai_source_column => 1);
3147  } # _initialize_tree_constructor  } # _initialize_tree_constructor
3148    
3149  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 2506  sub _tree_construction_initial ($) { Line 3198  sub _tree_construction_initial ($) {
3198        ## language.        ## language.
3199        my $doctype_name = $token->{name};        my $doctype_name = $token->{name};
3200        $doctype_name = '' unless defined $doctype_name;        $doctype_name = '' unless defined $doctype_name;
3201        $doctype_name =~ tr/a-z/A-Z/;        $doctype_name =~ tr/a-z/A-Z/; # ASCII case-insensitive
3202        if (not defined $token->{name} or # <!DOCTYPE>        if (not defined $token->{name} or # <!DOCTYPE>
           defined $token->{public_identifier} or  
3203            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
3204          !!!cp ('t1');          !!!cp ('t1');
3205          !!!parse-error (type => 'not HTML5', token => $token);          !!!parse-error (type => 'not HTML5', token => $token);
3206        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
3207          !!!cp ('t2');          !!!cp ('t2');
         ## ISSUE: ASCII case-insensitive? (in fact it does not matter)  
3208          !!!parse-error (type => 'not HTML5', token => $token);          !!!parse-error (type => 'not HTML5', token => $token);
3209          } elsif (defined $token->{public_identifier}) {
3210            if ($token->{public_identifier} eq 'XSLT-compat') {
3211              !!!cp ('t1.2');
3212              !!!parse-error (type => 'XSLT-compat', token => $token,
3213                              level => $self->{level}->{should});
3214            } else {
3215              !!!parse-error (type => 'not HTML5', token => $token);
3216            }
3217        } else {        } else {
3218          !!!cp ('t3');          !!!cp ('t3');
3219            #
3220        }        }
3221                
3222        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
3223          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3224          ## NOTE: Default value for both |public_id| and |system_id| attributes
3225          ## are empty strings, so that we don't set any value in missing cases.
3226        $doctype->public_id ($token->{public_identifier})        $doctype->public_id ($token->{public_identifier})
3227            if defined $token->{public_identifier};            if defined $token->{public_identifier};
3228        $doctype->system_id ($token->{system_identifier})        $doctype->system_id ($token->{system_identifier})
# Line 2536  sub _tree_construction_initial ($) { Line 3237  sub _tree_construction_initial ($) {
3237        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{public_identifier}) {
3238          my $pubid = $token->{public_identifier};          my $pubid = $token->{public_identifier};
3239          $pubid =~ tr/a-z/A-z/;          $pubid =~ tr/a-z/A-z/;
3240          if ({          my $prefix = [
3241            "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,            "+//SILMARIL//DTD HTML PRO V0R11 19970101//",
3242            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3243            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3244            "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 1//",
3245            "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 2//",
3246            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//",
3247            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//",
3248            "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT//",
3249            "-//IETF//DTD HTML 2.0//EN" => 1,            "-//IETF//DTD HTML 2.0//",
3250            "-//IETF//DTD HTML 2.1E//EN" => 1,            "-//IETF//DTD HTML 2.1E//",
3251            "-//IETF//DTD HTML 3.0//EN" => 1,            "-//IETF//DTD HTML 3.0//",
3252            "-//IETF//DTD HTML 3.0//EN//" => 1,            "-//IETF//DTD HTML 3.2 FINAL//",
3253            "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,            "-//IETF//DTD HTML 3.2//",
3254            "-//IETF//DTD HTML 3.2//EN" => 1,            "-//IETF//DTD HTML 3//",
3255            "-//IETF//DTD HTML 3//EN" => 1,            "-//IETF//DTD HTML LEVEL 0//",
3256            "-//IETF//DTD HTML LEVEL 0//EN" => 1,            "-//IETF//DTD HTML LEVEL 1//",
3257            "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,            "-//IETF//DTD HTML LEVEL 2//",
3258            "-//IETF//DTD HTML LEVEL 1//EN" => 1,            "-//IETF//DTD HTML LEVEL 3//",
3259            "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 0//",
3260            "-//IETF//DTD HTML LEVEL 2//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 1//",
3261            "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 2//",
3262            "-//IETF//DTD HTML LEVEL 3//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 3//",
3263            "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,            "-//IETF//DTD HTML STRICT//",
3264            "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,            "-//IETF//DTD HTML//",
3265            "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,            "-//METRIUS//DTD METRIUS PRESENTATIONAL//",
3266            "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//",
3267            "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//",
3268            "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//",
3269            "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//",
3270            "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//",
3271            "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//",
3272            "-//IETF//DTD HTML STRICT//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD HTML//",
3273            "-//IETF//DTD HTML STRICT//EN//2.0" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//",
3274            "-//IETF//DTD HTML STRICT//EN//3.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//",
3275            "-//IETF//DTD HTML//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//",
3276            "-//IETF//DTD HTML//EN//2.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//",
3277            "-//IETF//DTD HTML//EN//3.0" => 1,            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//",
3278            "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//",
3279            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//",
3280            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//",
3281            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//",
3282            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//",
3283            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,            "-//W3C//DTD HTML 3 1995-03-24//",
3284            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,            "-//W3C//DTD HTML 3.2 DRAFT//",
3285            "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,            "-//W3C//DTD HTML 3.2 FINAL//",
3286            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//W3C//DTD HTML 3.2//",
3287            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//W3C//DTD HTML 3.2S DRAFT//",
3288            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 FRAMESET//",
3289            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 TRANSITIONAL//",
3290            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMETNAL 19960712//",
3291            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMENTAL 970421//",
3292            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//W3C//DTD W3 HTML//",
3293            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//W3O//DTD W3 HTML 3.0//",
3294            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML 2.0//",
3295            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML//",
3296            "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,          ]; # $prefix
3297            "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,          my $match;
3298            "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,          for (@$prefix) {
3299            "-//W3C//DTD HTML 3.2//EN" => 1,            if (substr ($prefix, 0, length $_) eq $_) {
3300            "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,              $match = 1;
3301            "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,              last;
3302            "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,            }
3303            "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,          }
3304            "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,          if ($match or
3305            "-//W3C//DTD W3 HTML//EN" => 1,              $pubid eq "-//W3O//DTD W3 HTML STRICT 3.0//EN//" or
3306            "-//W3O//DTD W3 HTML 3.0//EN" => 1,              $pubid eq "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" or
3307            "-//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}) {  
3308            !!!cp ('t5');            !!!cp ('t5');
3309            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3310          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD HTML 4.01 FRAMESET//] or
3311                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD HTML 4.01 TRANSITIONAL//]) {
3312            if (defined $token->{system_identifier}) {            if (defined $token->{system_identifier}) {
3313              !!!cp ('t6');              !!!cp ('t6');
3314              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
# Line 2621  sub _tree_construction_initial ($) { Line 3316  sub _tree_construction_initial ($) {
3316              !!!cp ('t7');              !!!cp ('t7');
3317              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
3318            }            }
3319          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD XHTML 1.0 FRAMESET//] or
3320                   $pubid eq "-//W3C//DTD XHTML 1.0 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD XHTML 1.0 TRANSITIONAL//]) {
3321            !!!cp ('t8');            !!!cp ('t8');
3322            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
3323          } else {          } else {
# Line 2635  sub _tree_construction_initial ($) { Line 3330  sub _tree_construction_initial ($) {
3330          my $sysid = $token->{system_identifier};          my $sysid = $token->{system_identifier};
3331          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
3332          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") {
3333            ## TODO: Check the spec: PUBLIC "(limited quirks)" "(quirks)"            ## NOTE: Ensure that |PUBLIC "(limited quirks)" "(quirks)"| is
3334              ## marked as quirks.
3335            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3336            !!!cp ('t11');            !!!cp ('t11');
3337          } else {          } else {
# Line 2658  sub _tree_construction_initial ($) { Line 3354  sub _tree_construction_initial ($) {
3354        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3355        ## Go to the "before html" insertion mode.        ## Go to the "before html" insertion mode.
3356        ## reprocess        ## reprocess
3357          !!!ack-later;
3358        return;        return;
3359      } elsif ($token->{type} == CHARACTER_TOKEN) {      } elsif ($token->{type} == CHARACTER_TOKEN) {
3360        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
# Line 2738  sub _tree_construction_root_element ($) Line 3435  sub _tree_construction_root_element ($)
3435        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
3436          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
3437            my $root_element;            my $root_element;
3438            !!!create-element ($root_element, $token->{tag_name}, $token->{attributes});            !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3439            $self->{document}->append_child ($root_element);            $self->{document}->append_child ($root_element);
3440            push @{$self->{open_elements}}, [$root_element, 'html'];            push @{$self->{open_elements}},
3441                  [$root_element, $el_category->{html}];
3442    
3443            if ($token->{attributes}->{manifest}) {            if ($token->{attributes}->{manifest}) {
3444              !!!cp ('t24');              !!!cp ('t24');
3445              $self->{application_cache_selection}              $self->{application_cache_selection}
3446                  ->($token->{attributes}->{manifest}->{value});                  ->($token->{attributes}->{manifest}->{value});
3447              ## ISSUE: No relative reference resolution?              ## ISSUE: Spec is unclear on relative references.
3448                ## According to Hixie (#whatwg 2008-03-19), it should be
3449                ## resolved against the base URI of the document in HTML
3450                ## or xml:base of the element in XHTML.
3451            } else {            } else {
3452              !!!cp ('t25');              !!!cp ('t25');
3453              $self->{application_cache_selection}->(undef);              $self->{application_cache_selection}->(undef);
3454            }            }
3455    
3456              !!!nack ('t25c');
3457    
3458            !!!next-token;            !!!next-token;
3459            return; ## Go to the "before head" insertion mode.            return; ## Go to the "before head" insertion mode.
3460          } else {          } else {
# Line 2768  sub _tree_construction_root_element ($) Line 3471  sub _tree_construction_root_element ($)
3471          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
3472        }        }
3473    
3474      my $root_element; !!!create-element ($root_element, 'html');      my $root_element;
3475        !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3476      $self->{document}->append_child ($root_element);      $self->{document}->append_child ($root_element);
3477      push @{$self->{open_elements}}, [$root_element, 'html'];      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3478    
3479      $self->{application_cache_selection}->(undef);      $self->{application_cache_selection}->(undef);
3480    
3481      ## NOTE: Reprocess the token.      ## NOTE: Reprocess the token.
3482        !!!ack-later;
3483      return; ## Go to the "before head" insertion mode.      return; ## Go to the "before head" insertion mode.
3484    
3485      ## ISSUE: There is an issue in the spec      ## ISSUE: There is an issue in the spec
# Line 2798  sub _reset_insertion_mode ($) { Line 3503  sub _reset_insertion_mode ($) {
3503        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3504          $last = 1;          $last = 1;
3505          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
3506            if ($self->{inner_html_node}->[1] eq 'td' or            !!!cp ('t28');
3507                $self->{inner_html_node}->[1] eq 'th') {            $node = $self->{inner_html_node};
3508              !!!cp ('t27');          } else {
3509              #            die "_reset_insertion_mode: t27";
           } else {  
             !!!cp ('t28');  
             $node = $self->{inner_html_node};  
           }  
3510          }          }
3511        }        }
3512              
3513        ## Step 4..13        ## Step 4..14
3514        my $new_mode = {        my $new_mode;
3515          if ($node->[1] & FOREIGN_EL) {
3516            !!!cp ('t28.1');
3517            ## NOTE: Strictly spaking, the line below only applies to MathML and
3518            ## SVG elements.  Currently the HTML syntax supports only MathML and
3519            ## SVG elements as foreigners.
3520            $new_mode = IN_BODY_IM | IN_FOREIGN_CONTENT_IM;
3521          } elsif ($node->[1] & TABLE_CELL_EL) {
3522            if ($last) {
3523              !!!cp ('t28.2');
3524              #
3525            } else {
3526              !!!cp ('t28.3');
3527              $new_mode = IN_CELL_IM;
3528            }
3529          } else {
3530            !!!cp ('t28.4');
3531            $new_mode = {
3532                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
3533                        ## NOTE: |option| and |optgroup| do not set                        ## NOTE: |option| and |optgroup| do not set
3534                        ## insertion mode to "in select" by themselves.                        ## insertion mode to "in select" by themselves.
                       td => IN_CELL_IM,  
                       th => IN_CELL_IM,  
3535                        tr => IN_ROW_IM,                        tr => IN_ROW_IM,
3536                        tbody => IN_TABLE_BODY_IM,                        tbody => IN_TABLE_BODY_IM,
3537                        thead => IN_TABLE_BODY_IM,                        thead => IN_TABLE_BODY_IM,
# Line 2826  sub _reset_insertion_mode ($) { Line 3542  sub _reset_insertion_mode ($) {
3542                        head => IN_BODY_IM, # not in head!                        head => IN_BODY_IM, # not in head!
3543                        body => IN_BODY_IM,                        body => IN_BODY_IM,
3544                        frameset => IN_FRAMESET_IM,                        frameset => IN_FRAMESET_IM,
3545                       }->{$node->[1]};                       }->{$node->[0]->manakai_local_name};
3546          }
3547        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3548                
3549        ## Step 14        ## Step 15
3550        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3551          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3552            !!!cp ('t29');            !!!cp ('t29');
3553            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
# Line 2844  sub _reset_insertion_mode ($) { Line 3561  sub _reset_insertion_mode ($) {
3561          !!!cp ('t31');          !!!cp ('t31');
3562        }        }
3563                
3564        ## Step 15        ## Step 16
3565        $self->{insertion_mode} = IN_BODY_IM and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
3566                
3567        ## Step 16        ## Step 17
3568        $i--;        $i--;
3569        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3570                
3571        ## Step 17        ## Step 18
3572        redo S3;        redo S3;
3573      } # S3      } # S3
3574    
# Line 2963  sub _tree_construction_main ($) { Line 3680  sub _tree_construction_main ($) {
3680      ## Step 1      ## Step 1
3681      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3682      my $el;      my $el;
3683      !!!create-element ($el, $start_tag_name, $token->{attributes});      !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3684    
3685      ## Step 2      ## Step 2
3686      $insert->($el);      $insert->($el);
# Line 2974  sub _tree_construction_main ($) { Line 3691  sub _tree_construction_main ($) {
3691    
3692      ## Step 4      ## Step 4
3693      my $text = '';      my $text = '';
3694        !!!nack ('t40.1');
3695      !!!next-token;      !!!next-token;
3696      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3697        !!!cp ('t40');        !!!cp ('t40');
# Line 3000  sub _tree_construction_main ($) { Line 3718  sub _tree_construction_main ($) {
3718        ## NOTE: An end-of-file token.        ## NOTE: An end-of-file token.
3719        if ($content_model_flag == CDATA_CONTENT_MODEL) {        if ($content_model_flag == CDATA_CONTENT_MODEL) {
3720          !!!cp ('t43');          !!!cp ('t43');
3721          !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);          !!!parse-error (type => 'in CDATA:#eof', token => $token);
3722        } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {        } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3723          !!!cp ('t44');          !!!cp ('t44');
3724          !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);          !!!parse-error (type => 'in RCDATA:#eof', token => $token);
3725        } else {        } else {
3726          die "$0: $content_model_flag in parse_rcdata";          die "$0: $content_model_flag in parse_rcdata";
3727        }        }
# Line 3013  sub _tree_construction_main ($) { Line 3731  sub _tree_construction_main ($) {
3731    
3732    my $script_start_tag = sub () {    my $script_start_tag = sub () {
3733      my $script_el;      my $script_el;
3734      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
3735      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3736    
3737      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
3738      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
3739            
3740      my $text = '';      my $text = '';
3741        !!!nack ('t45.1');
3742      !!!next-token;      !!!next-token;
3743      while ($token->{type} == CHARACTER_TOKEN) {      while ($token->{type} == CHARACTER_TOKEN) {
3744        !!!cp ('t45');        !!!cp ('t45');
# Line 3039  sub _tree_construction_main ($) { Line 3758  sub _tree_construction_main ($) {
3758        ## Ignore the token        ## Ignore the token
3759      } else {      } else {
3760        !!!cp ('t48');        !!!cp ('t48');
3761        !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);        !!!parse-error (type => 'in CDATA:#eof', token => $token);
3762        ## ISSUE: And ignore?        ## ISSUE: And ignore?
3763        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3764      }      }
# Line 3077  sub _tree_construction_main ($) { Line 3796  sub _tree_construction_main ($) {
3796        my $formatting_element;        my $formatting_element;
3797        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3798        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3799          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3800              !!!cp ('t52');
3801              last AFE;
3802            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
3803                         eq $tag_name) {
3804            !!!cp ('t51');            !!!cp ('t51');
3805            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3806            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3807            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           !!!cp ('t52');  
           last AFE;  
3808          }          }
3809        } # AFE        } # AFE
3810        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3811          !!!cp ('t53');          !!!cp ('t53');
3812          !!!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);
3813          ## Ignore the token          ## Ignore the token
3814          !!!next-token;          !!!next-token;
3815          return;          return;
# Line 3106  sub _tree_construction_main ($) { Line 3826  sub _tree_construction_main ($) {
3826              last INSCOPE;              last INSCOPE;
3827            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3828              !!!cp ('t55');              !!!cp ('t55');
3829              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},              !!!parse-error (type => 'unmatched end tag',
3830                                text => $token->{tag_name},
3831                              token => $end_tag_token);                              token => $end_tag_token);
3832              ## Ignore the token              ## Ignore the token
3833              !!!next-token;              !!!next-token;
3834              return;              return;
3835            }            }
3836          } 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]}) {  
3837            !!!cp ('t56');            !!!cp ('t56');
3838            $in_scope = 0;            $in_scope = 0;
3839          }          }
3840        } # INSCOPE        } # INSCOPE
3841        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3842          !!!cp ('t57');          !!!cp ('t57');
3843          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},          !!!parse-error (type => 'unmatched end tag',
3844                            text => $token->{tag_name},
3845                          token => $end_tag_token);                          token => $end_tag_token);
3846          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3847          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
# Line 3130  sub _tree_construction_main ($) { Line 3849  sub _tree_construction_main ($) {
3849        }        }
3850        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3851          !!!cp ('t58');          !!!cp ('t58');
3852          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1],          !!!parse-error (type => 'not closed',
3853                            text => $self->{open_elements}->[-1]->[0]
3854                                ->manakai_local_name,
3855                          token => $end_tag_token);                          token => $end_tag_token);
3856        }        }
3857                
# Line 3139  sub _tree_construction_main ($) { Line 3860  sub _tree_construction_main ($) {
3860        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
3861        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3862          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3863          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
3864              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3865              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
3866               $scoping_category->{$node->[1]})) { ## Scoping is redundant, maybe               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
3867            !!!cp ('t59');            !!!cp ('t59');
3868            $furthest_block = $node;            $furthest_block = $node;
3869            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
# Line 3228  sub _tree_construction_main ($) { Line 3949  sub _tree_construction_main ($) {
3949        } # S7          } # S7  
3950                
3951        ## Step 8        ## Step 8
3952        if ({        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
            table => 1, tbody => 1, tfoot => 1, thead => 1, tr => 1,  
           }->{$common_ancestor_node->[1]}) {  
3953          my $foster_parent_element;          my $foster_parent_element;
3954          my $next_sibling;          my $next_sibling;
3955                           OE: for (reverse 0..$#{$self->{open_elements}}) {          OE: for (reverse 0..$#{$self->{open_elements}}) {
3956                             if ($self->{open_elements}->[$_]->[1] eq 'table') {            if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3957                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3958                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3959                                 !!!cp ('t65.1');                                 !!!cp ('t65.1');
# Line 3307  sub _tree_construction_main ($) { Line 4026  sub _tree_construction_main ($) {
4026    
4027    my $insert_to_foster = sub {    my $insert_to_foster = sub {
4028      my $child = shift;      my $child = shift;
4029      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]}) {  
4030        # MUST        # MUST
4031        my $foster_parent_element;        my $foster_parent_element;
4032        my $next_sibling;        my $next_sibling;
4033                           OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
4034                             if ($self->{open_elements}->[$_]->[1] eq 'table') {          if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
4035                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4036                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
4037                                 !!!cp ('t70');                                 !!!cp ('t70');
# Line 3339  sub _tree_construction_main ($) { Line 4056  sub _tree_construction_main ($) {
4056      }      }
4057    }; # $insert_to_foster    }; # $insert_to_foster
4058    
4059    B: {    B: while (1) {
4060      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
4061        !!!cp ('t73');        !!!cp ('t73');
4062        !!!parse-error (type => 'DOCTYPE in the middle', token => $token);        !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
4063        ## Ignore the token        ## Ignore the token
4064        ## Stay in the phase        ## Stay in the phase
4065        !!!next-token;        !!!next-token;
4066        redo B;        next B;
4067      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
4068               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
4069        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4070          !!!cp ('t79');          !!!cp ('t79');
4071          !!!parse-error (type => 'after html:html', token => $token);          !!!parse-error (type => 'after html', text => 'html', token => $token);
4072          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
4073        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4074          !!!cp ('t80');          !!!cp ('t80');
4075          !!!parse-error (type => 'after html:html', token => $token);          !!!parse-error (type => 'after html', text => 'html', token => $token);
4076          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
4077        } else {        } else {
4078          !!!cp ('t81');          !!!cp ('t81');
# Line 3372  sub _tree_construction_main ($) { Line 4089  sub _tree_construction_main ($) {
4089               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
4090          }          }
4091        }        }
4092          !!!nack ('t84.1');
4093        !!!next-token;        !!!next-token;
4094        redo B;        next B;
4095      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
4096        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
4097        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
# Line 3387  sub _tree_construction_main ($) { Line 4105  sub _tree_construction_main ($) {
4105          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
4106        }        }
4107        !!!next-token;        !!!next-token;
4108        redo B;        next B;
4109      } elsif ($self->{insertion_mode} & HEAD_IMS) {      } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
4110          if ($token->{type} == CHARACTER_TOKEN) {
4111            !!!cp ('t87.1');
4112            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4113            !!!next-token;
4114            next B;
4115          } elsif ($token->{type} == START_TAG_TOKEN) {
4116            if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
4117                 $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
4118                not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
4119                ($token->{tag_name} eq 'svg' and
4120                 $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
4121              ## NOTE: "using the rules for secondary insertion mode"then"continue"
4122              !!!cp ('t87.2');
4123              #
4124            } elsif ({
4125                      b => 1, big => 1, blockquote => 1, body => 1, br => 1,
4126                      center => 1, code => 1, dd => 1, div => 1, dl => 1, dt => 1,
4127                      em => 1, embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1,
4128                      h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, i => 1,
4129                      img => 1, li => 1, listing => 1, menu => 1, meta => 1,
4130                      nobr => 1, ol => 1, p => 1, pre => 1, ruby => 1, s => 1,
4131                      small => 1, span => 1, strong => 1, strike => 1, sub => 1,
4132                      sup => 1, table => 1, tt => 1, u => 1, ul => 1, var => 1,
4133                     }->{$token->{tag_name}}) {
4134              !!!cp ('t87.2');
4135              !!!parse-error (type => 'not closed',
4136                              text => $self->{open_elements}->[-1]->[0]
4137                                  ->manakai_local_name,
4138                              token => $token);
4139    
4140              pop @{$self->{open_elements}}
4141                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4142    
4143              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4144              ## Reprocess.
4145              next B;
4146            } else {
4147              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
4148              my $tag_name = $token->{tag_name};
4149              if ($nsuri eq $SVG_NS) {
4150                $tag_name = {
4151                   altglyph => 'altGlyph',
4152                   altglyphdef => 'altGlyphDef',
4153                   altglyphitem => 'altGlyphItem',
4154                   animatecolor => 'animateColor',
4155                   animatemotion => 'animateMotion',
4156                   animatetransform => 'animateTransform',
4157                   clippath => 'clipPath',
4158                   feblend => 'feBlend',
4159                   fecolormatrix => 'feColorMatrix',
4160                   fecomponenttransfer => 'feComponentTransfer',
4161                   fecomposite => 'feComposite',
4162                   feconvolvematrix => 'feConvolveMatrix',
4163                   fediffuselighting => 'feDiffuseLighting',
4164                   fedisplacementmap => 'feDisplacementMap',
4165                   fedistantlight => 'feDistantLight',
4166                   feflood => 'feFlood',
4167                   fefunca => 'feFuncA',
4168                   fefuncb => 'feFuncB',
4169                   fefuncg => 'feFuncG',
4170                   fefuncr => 'feFuncR',
4171                   fegaussianblur => 'feGaussianBlur',
4172                   feimage => 'feImage',
4173                   femerge => 'feMerge',
4174                   femergenode => 'feMergeNode',
4175                   femorphology => 'feMorphology',
4176                   feoffset => 'feOffset',
4177                   fepointlight => 'fePointLight',
4178                   fespecularlighting => 'feSpecularLighting',
4179                   fespotlight => 'feSpotLight',
4180                   fetile => 'feTile',
4181                   feturbulence => 'feTurbulence',
4182                   foreignobject => 'foreignObject',
4183                   glyphref => 'glyphRef',
4184                   lineargradient => 'linearGradient',
4185                   radialgradient => 'radialGradient',
4186                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4187                   textpath => 'textPath',  
4188                }->{$tag_name} || $tag_name;
4189              }
4190    
4191              ## "adjust SVG attributes" (SVG only) - done in insert-element-f
4192    
4193              ## "adjust foreign attributes" - done in insert-element-f
4194    
4195              !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
4196    
4197              if ($self->{self_closing}) {
4198                pop @{$self->{open_elements}};
4199                !!!ack ('t87.3');
4200              } else {
4201                !!!cp ('t87.4');
4202              }
4203    
4204              !!!next-token;
4205              next B;
4206            }
4207          } elsif ($token->{type} == END_TAG_TOKEN) {
4208            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4209            !!!cp ('t87.5');
4210            #
4211          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4212            !!!cp ('t87.6');
4213            !!!parse-error (type => 'not closed',
4214                            text => $self->{open_elements}->[-1]->[0]
4215                                ->manakai_local_name,
4216                            token => $token);
4217    
4218            pop @{$self->{open_elements}}
4219                while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4220    
4221            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4222            ## Reprocess.
4223            next B;
4224          } else {
4225            die "$0: $token->{type}: Unknown token type";        
4226          }
4227        }
4228    
4229        if ($self->{insertion_mode} & HEAD_IMS) {
4230        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4231          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4232            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
# Line 3398  sub _tree_construction_main ($) { Line 4236  sub _tree_construction_main ($) {
4236              !!!cp ('t88.1');              !!!cp ('t88.1');
4237              ## Ignore the token.              ## Ignore the token.
4238              !!!next-token;              !!!next-token;
4239              redo B;              next B;
4240            }            }
4241            unless (length $token->{data}) {            unless (length $token->{data}) {
4242              !!!cp ('t88');              !!!cp ('t88');
4243              !!!next-token;              !!!next-token;
4244              redo B;              next B;
4245            }            }
4246          }          }
4247    
4248          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4249            !!!cp ('t89');            !!!cp ('t89');
4250            ## As if <head>            ## As if <head>
4251            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4252            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4253            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            push @{$self->{open_elements}},
4254                  [$self->{head_element}, $el_category->{head}];
4255    
4256            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4257            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
# Line 3422  sub _tree_construction_main ($) { Line 4261  sub _tree_construction_main ($) {
4261            !!!cp ('t90');            !!!cp ('t90');
4262            ## As if </noscript>            ## As if </noscript>
4263            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4264            !!!parse-error (type => 'in noscript:#character', token => $token);            !!!parse-error (type => 'in noscript:#text', token => $token);
4265                        
4266            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4267            ## As if </head>            ## As if </head>
# Line 3438  sub _tree_construction_main ($) { Line 4277  sub _tree_construction_main ($) {
4277            !!!cp ('t92');            !!!cp ('t92');
4278          }          }
4279    
4280              ## "after head" insertion mode          ## "after head" insertion mode
4281              ## As if <body>          ## As if <body>
4282              !!!insert-element ('body');          !!!insert-element ('body',, $token);
4283              $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4284              ## reprocess          ## reprocess
4285              redo B;          next B;
4286            } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4287              if ($token->{tag_name} eq 'head') {          if ($token->{tag_name} eq 'head') {
4288                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4289                  !!!cp ('t93');              !!!cp ('t93');
4290                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4291                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              $self->{open_elements}->[-1]->[0]->append_child
4292                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];                  ($self->{head_element});
4293                  $self->{insertion_mode} = IN_HEAD_IM;              push @{$self->{open_elements}},
4294                  !!!next-token;                  [$self->{head_element}, $el_category->{head}];
4295                  redo B;              $self->{insertion_mode} = IN_HEAD_IM;
4296                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {              !!!nack ('t93.1');
4297                  !!!cp ('t94');              !!!next-token;
4298                  #              next B;
4299                } else {            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4300                  !!!cp ('t95');              !!!cp ('t93.2');
4301                  !!!parse-error (type => 'in head:head', token => $token); # or in head noscript              !!!parse-error (type => 'after head', text => 'head',
4302                  ## Ignore the token                              token => $token);
4303                  !!!next-token;              ## Ignore the token
4304                  redo B;              !!!nack ('t93.3');
4305                }              !!!next-token;
4306              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              next B;
4307                !!!cp ('t96');            } else {
4308                ## As if <head>              !!!cp ('t95');
4309                !!!create-element ($self->{head_element}, 'head');              !!!parse-error (type => 'in head:head',
4310                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                              token => $token); # or in head noscript
4311                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];              ## Ignore the token
4312                !!!nack ('t95.1');
4313                !!!next-token;
4314                next B;
4315              }
4316            } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4317              !!!cp ('t96');
4318              ## As if <head>
4319              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4320              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4321              push @{$self->{open_elements}},
4322                  [$self->{head_element}, $el_category->{head}];
4323    
4324                $self->{insertion_mode} = IN_HEAD_IM;            $self->{insertion_mode} = IN_HEAD_IM;
4325                ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4326              } else {          } else {
4327                !!!cp ('t97');            !!!cp ('t97');
4328              }          }
4329    
4330              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
4331                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4332                  !!!cp ('t98');                  !!!cp ('t98');
4333                  ## As if </noscript>                  ## As if </noscript>
4334                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4335                  !!!parse-error (type => 'in noscript:base', token => $token);                  !!!parse-error (type => 'in noscript', text => 'base',
4336                                    token => $token);
4337                                
4338                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4339                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
# Line 3493  sub _tree_construction_main ($) { Line 4344  sub _tree_construction_main ($) {
4344                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4345                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4346                  !!!cp ('t100');                  !!!cp ('t100');
4347                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4348                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4349                    push @{$self->{open_elements}},
4350                        [$self->{head_element}, $el_category->{head}];
4351                } else {                } else {
4352                  !!!cp ('t101');                  !!!cp ('t101');
4353                }                }
4354                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4355                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4356                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4357                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4358                  !!!nack ('t101.1');
4359                !!!next-token;                !!!next-token;
4360                redo B;                next B;
4361              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
4362                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4363                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4364                  !!!cp ('t102');                  !!!cp ('t102');
4365                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4366                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4367                    push @{$self->{open_elements}},
4368                        [$self->{head_element}, $el_category->{head}];
4369                } else {                } else {
4370                  !!!cp ('t103');                  !!!cp ('t103');
4371                }                }
4372                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4373                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4374                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4375                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4376                  !!!ack ('t103.1');
4377                !!!next-token;                !!!next-token;
4378                redo B;                next B;
4379              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
4380                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4381                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4382                  !!!cp ('t104');                  !!!cp ('t104');
4383                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4384                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4385                    push @{$self->{open_elements}},
4386                        [$self->{head_element}, $el_category->{head}];
4387                } else {                } else {
4388                  !!!cp ('t105');                  !!!cp ('t105');
4389                }                }
4390                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4391                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.
4392    
4393                unless ($self->{confident}) {                unless ($self->{confident}) {
4394                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) {
4395                    !!!cp ('t106');                    !!!cp ('t106');
4396                      ## NOTE: Whether the encoding is supported or not is handled
4397                      ## in the {change_encoding} callback.
4398                    $self->{change_encoding}                    $self->{change_encoding}
4399                        ->($self, $token->{attributes}->{charset}->{value});                        ->($self, $token->{attributes}->{charset}->{value},
4400                             $token);
4401                                        
4402                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4403                        ->set_user_data (manakai_has_reference =>                        ->set_user_data (manakai_has_reference =>
4404                                             $token->{attributes}->{charset}                                             $token->{attributes}->{charset}
4405                                                 ->{has_reference});                                                 ->{has_reference});
4406                  } elsif ($token->{attributes}->{content}) {                  } elsif ($token->{attributes}->{content}) {
                   ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
4407                    if ($token->{attributes}->{content}->{value}                    if ($token->{attributes}->{content}->{value}
4408                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                        =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4409                            [\x09-\x0D\x20]*=                            [\x09-\x0D\x20]*=
4410                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4411                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
4412                      !!!cp ('t107');                      !!!cp ('t107');
4413                        ## NOTE: Whether the encoding is supported or not is handled
4414                        ## in the {change_encoding} callback.
4415                      $self->{change_encoding}                      $self->{change_encoding}
4416                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4417                               $token);
4418                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4419                          ->set_user_data (manakai_has_reference =>                          ->set_user_data (manakai_has_reference =>
4420                                               $token->{attributes}->{content}                                               $token->{attributes}->{content}
# Line 3578  sub _tree_construction_main ($) { Line 4442  sub _tree_construction_main ($) {
4442    
4443                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4444                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4445                  !!!ack ('t110.1');
4446                !!!next-token;                !!!next-token;
4447                redo B;                next B;
4448              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
4449                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4450                  !!!cp ('t111');                  !!!cp ('t111');
4451                  ## As if </noscript>                  ## As if </noscript>
4452                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4453                  !!!parse-error (type => 'in noscript:title', token => $token);                  !!!parse-error (type => 'in noscript', text => 'title',
4454                                    token => $token);
4455                                
4456                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4457                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4458                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4459                  !!!cp ('t112');                  !!!cp ('t112');
4460                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4461                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4462                    push @{$self->{open_elements}},
4463                        [$self->{head_element}, $el_category->{head}];
4464                } else {                } else {
4465                  !!!cp ('t113');                  !!!cp ('t113');
4466                }                }
# Line 3603  sub _tree_construction_main ($) { Line 4471  sub _tree_construction_main ($) {
4471                $parse_rcdata->(RCDATA_CONTENT_MODEL);                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4472                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4473                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4474                redo B;                next B;
4475              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style' or
4476                         $token->{tag_name} eq 'noframes') {
4477                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4478                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
4479                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4480                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4481                  !!!cp ('t114');                  !!!cp ('t114');
4482                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4483                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4484                    push @{$self->{open_elements}},
4485                        [$self->{head_element}, $el_category->{head}];
4486                } else {                } else {
4487                  !!!cp ('t115');                  !!!cp ('t115');
4488                }                }
4489                $parse_rcdata->(CDATA_CONTENT_MODEL);                $parse_rcdata->(CDATA_CONTENT_MODEL);
4490                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4491                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4492                redo B;                next B;
4493              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4494                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
4495                  !!!cp ('t116');                  !!!cp ('t116');
4496                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
4497                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4498                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4499                    !!!nack ('t116.1');
4500                  !!!next-token;                  !!!next-token;
4501                  redo B;                  next B;
4502                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4503                  !!!cp ('t117');                  !!!cp ('t117');
4504                  !!!parse-error (type => 'in noscript:noscript', token => $token);                  !!!parse-error (type => 'in noscript', text => 'noscript',
4505                                    token => $token);
4506                  ## Ignore the token                  ## Ignore the token
4507                    !!!nack ('t117.1');
4508                  !!!next-token;                  !!!next-token;
4509                  redo B;                  next B;
4510                } else {                } else {
4511                  !!!cp ('t118');                  !!!cp ('t118');
4512                  #                  #
# Line 3642  sub _tree_construction_main ($) { Line 4516  sub _tree_construction_main ($) {
4516                  !!!cp ('t119');                  !!!cp ('t119');
4517                  ## As if </noscript>                  ## As if </noscript>
4518                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4519                  !!!parse-error (type => 'in noscript:script', token => $token);                  !!!parse-error (type => 'in noscript', text => 'script',
4520                                    token => $token);
4521                                
4522                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4523                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4524                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4525                  !!!cp ('t120');                  !!!cp ('t120');
4526                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4527                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4528                    push @{$self->{open_elements}},
4529                        [$self->{head_element}, $el_category->{head}];
4530                } else {                } else {
4531                  !!!cp ('t121');                  !!!cp ('t121');
4532                }                }
# Line 3658  sub _tree_construction_main ($) { Line 4535  sub _tree_construction_main ($) {
4535                $script_start_tag->();                $script_start_tag->();
4536                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4537                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4538                redo B;                next B;
4539              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
4540                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
4541                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4542                  !!!cp ('t122');                  !!!cp ('t122');
4543                  ## As if </noscript>                  ## As if </noscript>
4544                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4545                  !!!parse-error (type => 'in noscript:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'in noscript',
4546                                    text => $token->{tag_name}, token => $token);
4547                                    
4548                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4549                  ## As if </head>                  ## As if </head>
# Line 3682  sub _tree_construction_main ($) { Line 4560  sub _tree_construction_main ($) {
4560                }                }
4561    
4562                ## "after head" insertion mode                ## "after head" insertion mode
4563                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4564                if ($token->{tag_name} eq 'body') {                if ($token->{tag_name} eq 'body') {
4565                  !!!cp ('t126');                  !!!cp ('t126');
4566                  $self->{insertion_mode} = IN_BODY_IM;                  $self->{insertion_mode} = IN_BODY_IM;
# Line 3692  sub _tree_construction_main ($) { Line 4570  sub _tree_construction_main ($) {
4570                } else {                } else {
4571                  die "$0: tag name: $self->{tag_name}";                  die "$0: tag name: $self->{tag_name}";
4572                }                }
4573                  !!!nack ('t127.1');
4574                !!!next-token;                !!!next-token;
4575                redo B;                next B;
4576              } else {              } else {
4577                !!!cp ('t128');                !!!cp ('t128');
4578                #                #
# Line 3703  sub _tree_construction_main ($) { Line 4582  sub _tree_construction_main ($) {
4582                !!!cp ('t129');                !!!cp ('t129');
4583                ## As if </noscript>                ## As if </noscript>
4584                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4585                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'in noscript:/',
4586                                  text => $token->{tag_name}, token => $token);
4587                                
4588                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4589                ## As if </head>                ## As if </head>
# Line 3722  sub _tree_construction_main ($) { Line 4602  sub _tree_construction_main ($) {
4602    
4603              ## "after head" insertion mode              ## "after head" insertion mode
4604              ## As if <body>              ## As if <body>
4605              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4606              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4607              ## reprocess              ## reprocess
4608              redo B;              !!!ack-later;
4609                next B;
4610            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
4611              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
4612                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4613                  !!!cp ('t132');                  !!!cp ('t132');
4614                  ## As if <head>                  ## As if <head>
4615                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4616                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4617                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4618                        [$self->{head_element}, $el_category->{head}];
4619    
4620                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4621                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4622                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4623                  !!!next-token;                  !!!next-token;
4624                  redo B;                  next B;
4625                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4626                  !!!cp ('t133');                  !!!cp ('t133');
4627                  ## As if </noscript>                  ## As if </noscript>
4628                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4629                  !!!parse-error (type => 'in noscript:/head', token => $token);                  !!!parse-error (type => 'in noscript:/',
4630                                    text => 'head', token => $token);
4631                                    
4632                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4633                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4634                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4635                  !!!next-token;                  !!!next-token;
4636                  redo B;                  next B;
4637                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4638                  !!!cp ('t134');                  !!!cp ('t134');
4639                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4640                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4641                  !!!next-token;                  !!!next-token;
4642                  redo B;                  next B;
4643                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4644                    !!!cp ('t134.1');
4645                    !!!parse-error (type => 'unmatched end tag', text => 'head',
4646                                    token => $token);
4647                    ## Ignore the token
4648                    !!!next-token;
4649                    next B;
4650                } else {                } else {
4651                  !!!cp ('t135');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
                 #  
4652                }                }
4653              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4654                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
# Line 3767  sub _tree_construction_main ($) { Line 4656  sub _tree_construction_main ($) {
4656                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4657                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4658                  !!!next-token;                  !!!next-token;
4659                  redo B;                  next B;
4660                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
4661                           $self->{insertion_mode} == AFTER_HEAD_IM) {
4662                  !!!cp ('t137');                  !!!cp ('t137');
4663                  !!!parse-error (type => 'unmatched end tag:noscript', token => $token);                  !!!parse-error (type => 'unmatched end tag',
4664                                    text => 'noscript', token => $token);
4665                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
4666                  !!!next-token;                  !!!next-token;
4667                  redo B;                  next B;
4668                } else {                } else {
4669                  !!!cp ('t138');                  !!!cp ('t138');
4670                  #                  #
# Line 3781  sub _tree_construction_main ($) { Line 4672  sub _tree_construction_main ($) {
4672              } elsif ({              } elsif ({
4673                        body => 1, html => 1,                        body => 1, html => 1,
4674                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4675                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM or
4676                  !!!cp ('t139');                    $self->{insertion_mode} == IN_HEAD_IM or
4677                  ## As if <head>                    $self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
                 !!!create-element ($self->{head_element}, 'head');  
                 $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) {  
4678                  !!!cp ('t140');                  !!!cp ('t140');
4679                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
4680                                    text => $token->{tag_name}, token => $token);
4681                  ## Ignore the token                  ## Ignore the token
4682                  !!!next-token;                  !!!next-token;
4683                  redo B;                  next B;
4684                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4685                    !!!cp ('t140.1');
4686                    !!!parse-error (type => 'unmatched end tag',
4687                                    text => $token->{tag_name}, token => $token);
4688                    ## Ignore the token
4689                    !!!next-token;
4690                    next B;
4691                } else {                } else {
4692                  !!!cp ('t141');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
4693                }                }
4694                              } elsif ($token->{tag_name} eq 'p') {
4695                #                !!!cp ('t142');
4696              } elsif ({                !!!parse-error (type => 'unmatched end tag',
4697                        p => 1, br => 1,                                text => $token->{tag_name}, token => $token);
4698                       }->{$token->{tag_name}}) {                ## Ignore the token
4699                  !!!next-token;
4700                  next B;
4701                } elsif ($token->{tag_name} eq 'br') {
4702                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4703                  !!!cp ('t142');                  !!!cp ('t142.2');
4704                  ## As if <head>                  ## (before head) as if <head>, (in head) as if </head>
4705                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4706                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4707                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  $self->{insertion_mode} = AFTER_HEAD_IM;
4708      
4709                    ## Reprocess in the "after head" insertion mode...
4710                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4711                    !!!cp ('t143.2');
4712                    ## As if </head>
4713                    pop @{$self->{open_elements}};
4714                    $self->{insertion_mode} = AFTER_HEAD_IM;
4715      
4716                    ## Reprocess in the "after head" insertion mode...
4717                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4718                    !!!cp ('t143.3');
4719                    ## ISSUE: Two parse errors for <head><noscript></br>
4720                    !!!parse-error (type => 'unmatched end tag',
4721                                    text => 'br', token => $token);
4722                    ## As if </noscript>
4723                    pop @{$self->{open_elements}};
4724                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4725    
4726                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4727                } else {                  ## As if </head>
4728                  !!!cp ('t143');                  pop @{$self->{open_elements}};
4729                }                  $self->{insertion_mode} = AFTER_HEAD_IM;
4730    
4731                #                  ## Reprocess in the "after head" insertion mode...
4732              } else {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4733                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                  !!!cp ('t143.4');
                 !!!cp ('t144');  
4734                  #                  #
4735                } else {                } else {
4736                  !!!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;  
4737                }                }
4738    
4739                  ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
4740                  !!!parse-error (type => 'unmatched end tag',
4741                                  text => 'br', token => $token);
4742                  ## Ignore the token
4743                  !!!next-token;
4744                  next B;
4745                } else {
4746                  !!!cp ('t145');
4747                  !!!parse-error (type => 'unmatched end tag',
4748                                  text => $token->{tag_name}, token => $token);
4749                  ## Ignore the token
4750                  !!!next-token;
4751                  next B;
4752              }              }
4753    
4754              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4755                !!!cp ('t146');                !!!cp ('t146');
4756                ## As if </noscript>                ## As if </noscript>
4757                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4758                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'in noscript:/',
4759                                  text => $token->{tag_name}, token => $token);
4760                                
4761                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4762                ## As if </head>                ## As if </head>
# Line 3851  sub _tree_construction_main ($) { Line 4772  sub _tree_construction_main ($) {
4772              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4773  ## ISSUE: This case cannot be reached?  ## ISSUE: This case cannot be reached?
4774                !!!cp ('t148');                !!!cp ('t148');
4775                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag',
4776                                  text => $token->{tag_name}, token => $token);
4777                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
4778                !!!next-token;                !!!next-token;
4779                redo B;                next B;
4780              } else {              } else {
4781                !!!cp ('t149');                !!!cp ('t149');
4782              }              }
4783    
4784              ## "after head" insertion mode              ## "after head" insertion mode
4785              ## As if <body>              ## As if <body>
4786              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4787              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4788              ## reprocess              ## reprocess
4789              redo B;              next B;
4790        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4791          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4792            !!!cp ('t149.1');            !!!cp ('t149.1');
4793    
4794            ## NOTE: As if <head>            ## NOTE: As if <head>
4795            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4796            $self->{open_elements}->[-1]->[0]->append_child            $self->{open_elements}->[-1]->[0]->append_child
4797                ($self->{head_element});                ($self->{head_element});
4798            #push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            #push @{$self->{open_elements}},
4799              #    [$self->{head_element}, $el_category->{head}];
4800            #$self->{insertion_mode} = IN_HEAD_IM;            #$self->{insertion_mode} = IN_HEAD_IM;
4801            ## NOTE: Reprocess.            ## NOTE: Reprocess.
4802    
# Line 3914  sub _tree_construction_main ($) { Line 4837  sub _tree_construction_main ($) {
4837          }          }
4838    
4839          ## NOTE: As if <body>          ## NOTE: As if <body>
4840          !!!insert-element ('body');          !!!insert-element ('body',, $token);
4841          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4842          ## NOTE: Reprocess.          ## NOTE: Reprocess.
4843          redo B;          next B;
4844        } else {        } else {
4845          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
4846        }        }
# Line 3932  sub _tree_construction_main ($) { Line 4855  sub _tree_construction_main ($) {
4855              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4856    
4857              !!!next-token;              !!!next-token;
4858              redo B;              next B;
4859            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
4860              if ({              if ({
4861                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
# Line 3942  sub _tree_construction_main ($) { Line 4865  sub _tree_construction_main ($) {
4865                  ## have an element in table scope                  ## have an element in table scope
4866                  for (reverse 0..$#{$self->{open_elements}}) {                  for (reverse 0..$#{$self->{open_elements}}) {
4867                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4868                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] & TABLE_CELL_EL) {
4869                      !!!cp ('t151');                      !!!cp ('t151');
4870    
4871                      ## Close the cell                      ## Close the cell
4872                      !!!back-token; # <?>                      !!!back-token; # <x>
4873                      $token = {type => END_TAG_TOKEN, tag_name => $node->[1]};                      $token = {type => END_TAG_TOKEN,
4874                      redo B;                                tag_name => $node->[0]->manakai_local_name,
4875                    } elsif ({                                line => $token->{line},
4876                              table => 1, html => 1,                                column => $token->{column}};
4877                             }->{$node->[1]}) {                      next B;
4878                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4879                      !!!cp ('t152');                      !!!cp ('t152');
4880                      ## ISSUE: This case can never be reached, maybe.                      ## ISSUE: This case can never be reached, maybe.
4881                      last;                      last;
# Line 3960  sub _tree_construction_main ($) { Line 4884  sub _tree_construction_main ($) {
4884    
4885                  !!!cp ('t153');                  !!!cp ('t153');
4886                  !!!parse-error (type => 'start tag not allowed',                  !!!parse-error (type => 'start tag not allowed',
4887                      value => $token->{tag_name}, token => $token);                      text => $token->{tag_name}, token => $token);
4888                  ## Ignore the token                  ## Ignore the token
4889                    !!!nack ('t153.1');
4890                  !!!next-token;                  !!!next-token;
4891                  redo B;                  next B;
4892                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4893                  !!!parse-error (type => 'not closed:caption', token => $token);                  !!!parse-error (type => 'not closed', text => 'caption',
4894                                    token => $token);
4895                                    
4896                  ## NOTE: As if </caption>.                  ## NOTE: As if </caption>.
4897                  ## have a table element in table scope                  ## have a table element in table scope
# Line 3973  sub _tree_construction_main ($) { Line 4899  sub _tree_construction_main ($) {
4899                  INSCOPE: {                  INSCOPE: {
4900                    for (reverse 0..$#{$self->{open_elements}}) {                    for (reverse 0..$#{$self->{open_elements}}) {
4901                      my $node = $self->{open_elements}->[$_];                      my $node = $self->{open_elements}->[$_];
4902                      if ($node->[1] eq 'caption') {                      if ($node->[1] & CAPTION_EL) {
4903                        !!!cp ('t155');                        !!!cp ('t155');
4904                        $i = $_;                        $i = $_;
4905                        last INSCOPE;                        last INSCOPE;
4906                      } elsif ({                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
                               table => 1, html => 1,  
                              }->{$node->[1]}) {  
4907                        !!!cp ('t156');                        !!!cp ('t156');
4908                        last;                        last;
4909                      }                      }
# Line 3987  sub _tree_construction_main ($) { Line 4911  sub _tree_construction_main ($) {
4911    
4912                    !!!cp ('t157');                    !!!cp ('t157');
4913                    !!!parse-error (type => 'start tag not allowed',                    !!!parse-error (type => 'start tag not allowed',
4914                                    value => $token->{tag_name}, token => $token);                                    text => $token->{tag_name}, token => $token);
4915                    ## Ignore the token                    ## Ignore the token
4916                      !!!nack ('t157.1');
4917                    !!!next-token;                    !!!next-token;
4918                    redo B;                    next B;
4919                  } # INSCOPE                  } # INSCOPE
4920                                    
4921                  ## generate implied end tags                  ## generate implied end tags
4922                  while ({                  while ($self->{open_elements}->[-1]->[1]
4923                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4924                    !!!cp ('t158');                    !!!cp ('t158');
4925                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4926                  }                  }
4927    
4928                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4929                    !!!cp ('t159');                    !!!cp ('t159');
4930                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                    !!!parse-error (type => 'not closed',
4931                                      text => $self->{open_elements}->[-1]->[0]
4932                                          ->manakai_local_name,
4933                                      token => $token);
4934                  } else {                  } else {
4935                    !!!cp ('t160');                    !!!cp ('t160');
4936                  }                  }
# Line 4015  sub _tree_construction_main ($) { Line 4942  sub _tree_construction_main ($) {
4942                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4943                                    
4944                  ## reprocess                  ## reprocess
4945                  redo B;                  !!!ack-later;
4946                    next B;
4947                } else {                } else {
4948                  !!!cp ('t161');                  !!!cp ('t161');
4949                  #                  #
# Line 4031  sub _tree_construction_main ($) { Line 4959  sub _tree_construction_main ($) {
4959                  my $i;                  my $i;
4960                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4961                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4962                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4963                      !!!cp ('t163');                      !!!cp ('t163');
4964                      $i = $_;                      $i = $_;
4965                      last INSCOPE;                      last INSCOPE;
4966                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
4967                      !!!cp ('t164');                      !!!cp ('t164');
4968                      last INSCOPE;                      last INSCOPE;
4969                    }                    }
4970                  } # INSCOPE                  } # INSCOPE
4971                    unless (defined $i) {                    unless (defined $i) {
4972                      !!!cp ('t165');                      !!!cp ('t165');
4973                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                      !!!parse-error (type => 'unmatched end tag',
4974                                        text => $token->{tag_name},
4975                                        token => $token);
4976                      ## Ignore the token                      ## Ignore the token
4977                      !!!next-token;                      !!!next-token;
4978                      redo B;                      next B;
4979                    }                    }
4980                                    
4981                  ## generate implied end tags                  ## generate implied end tags
4982                  while ({                  while ($self->{open_elements}->[-1]->[1]
4983                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4984                    !!!cp ('t166');                    !!!cp ('t166');
4985                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4986                  }                  }
4987    
4988                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
4989                            ne $token->{tag_name}) {
4990                    !!!cp ('t167');                    !!!cp ('t167');
4991                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                    !!!parse-error (type => 'not closed',
4992                                      text => $self->{open_elements}->[-1]->[0]
4993                                          ->manakai_local_name,
4994                                      token => $token);
4995                  } else {                  } else {
4996                    !!!cp ('t168');                    !!!cp ('t168');
4997                  }                  }
# Line 4072  sub _tree_construction_main ($) { Line 5003  sub _tree_construction_main ($) {
5003                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
5004                                    
5005                  !!!next-token;                  !!!next-token;
5006                  redo B;                  next B;
5007                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
5008                  !!!cp ('t169');                  !!!cp ('t169');
5009                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
5010                                    text => $token->{tag_name}, token => $token);
5011                  ## Ignore the token                  ## Ignore the token
5012                  !!!next-token;                  !!!next-token;
5013                  redo B;                  next B;
5014                } else {                } else {
5015                  !!!cp ('t170');                  !!!cp ('t170');
5016                  #                  #
# Line 4090  sub _tree_construction_main ($) { Line 5022  sub _tree_construction_main ($) {
5022                  INSCOPE: {                  INSCOPE: {
5023                    for (reverse 0..$#{$self->{open_elements}}) {                    for (reverse 0..$#{$self->{open_elements}}) {
5024                      my $node = $self->{open_elements}->[$_];                      my $node = $self->{open_elements}->[$_];
5025                      if ($node->[1] eq $token->{tag_name}) {                      if ($node->[1] & CAPTION_EL) {
5026                        !!!cp ('t171');                        !!!cp ('t171');
5027                        $i = $_;                        $i = $_;
5028                        last INSCOPE;                        last INSCOPE;
5029                      } elsif ({                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
                               table => 1, html => 1,  
                              }->{$node->[1]}) {  
5030                        !!!cp ('t172');                        !!!cp ('t172');
5031                        last;                        last;
5032                      }                      }
# Line 4104  sub _tree_construction_main ($) { Line 5034  sub _tree_construction_main ($) {
5034    
5035                    !!!cp ('t173');                    !!!cp ('t173');
5036                    !!!parse-error (type => 'unmatched end tag',                    !!!parse-error (type => 'unmatched end tag',
5037                                    value => $token->{tag_name}, token => $token);                                    text => $token->{tag_name}, token => $token);
5038                    ## Ignore the token                    ## Ignore the token
5039                    !!!next-token;                    !!!next-token;
5040                    redo B;                    next B;
5041                  } # INSCOPE                  } # INSCOPE
5042                                    
5043                  ## generate implied end tags                  ## generate implied end tags
5044                  while ({                  while ($self->{open_elements}->[-1]->[1]
5045                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
5046                    !!!cp ('t174');                    !!!cp ('t174');
5047                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5048                  }                  }
5049                                    
5050                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5051                    !!!cp ('t175');                    !!!cp ('t175');
5052                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                    !!!parse-error (type => 'not closed',
5053                                      text => $self->{open_elements}->[-1]->[0]
5054                                          ->manakai_local_name,
5055                                      token => $token);
5056                  } else {                  } else {
5057                    !!!cp ('t176');                    !!!cp ('t176');
5058                  }                  }
# Line 4132  sub _tree_construction_main ($) { Line 5064  sub _tree_construction_main ($) {
5064                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5065                                    
5066                  !!!next-token;                  !!!next-token;
5067                  redo B;                  next B;
5068                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
5069                  !!!cp ('t177');                  !!!cp ('t177');
5070                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
5071                                    text => $token->{tag_name}, token => $token);
5072                  ## Ignore the token                  ## Ignore the token
5073                  !!!next-token;                  !!!next-token;
5074                  redo B;                  next B;
5075                } else {                } else {
5076                  !!!cp ('t178');                  !!!cp ('t178');
5077                  #                  #
# Line 4154  sub _tree_construction_main ($) { Line 5087  sub _tree_construction_main ($) {
5087                INSCOPE: {                INSCOPE: {
5088                  for (reverse 0..$#{$self->{open_elements}}) {                  for (reverse 0..$#{$self->{open_elements}}) {
5089                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5090                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5091                      !!!cp ('t179');                      !!!cp ('t179');
5092                      $i = $_;                      $i = $_;
5093    
5094                      ## Close the cell                      ## Close the cell
5095                      !!!back-token; # </?>                      !!!back-token; # </x>
5096                      $token = {type => END_TAG_TOKEN, tag_name => $tn};                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
5097                      redo B;                                line => $token->{line},
5098                    } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {                                column => $token->{column}};
5099                        next B;
5100                      } elsif ($node->[1] & TABLE_CELL_EL) {
5101                      !!!cp ('t180');                      !!!cp ('t180');
5102                      $tn = $node->[1];                      $tn = $node->[0]->manakai_local_name;
5103                      ## NOTE: There is exactly one |td| or |th| element                      ## NOTE: There is exactly one |td| or |th| element
5104                      ## in scope in the stack of open elements by definition.                      ## in scope in the stack of open elements by definition.
5105                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5106                      ## ISSUE: Can this be reached?                      ## ISSUE: Can this be reached?
5107                      !!!cp ('t181');                      !!!cp ('t181');
5108                      last;                      last;
# Line 4178  sub _tree_construction_main ($) { Line 5111  sub _tree_construction_main ($) {
5111    
5112                  !!!cp ('t182');                  !!!cp ('t182');
5113                  !!!parse-error (type => 'unmatched end tag',                  !!!parse-error (type => 'unmatched end tag',
5114                      value => $token->{tag_name}, token => $token);                      text => $token->{tag_name}, token => $token);
5115                  ## Ignore the token                  ## Ignore the token
5116                  !!!next-token;                  !!!next-token;
5117                  redo B;                  next B;
5118                } # INSCOPE                } # INSCOPE
5119              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
5120                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5121                !!!parse-error (type => 'not closed:caption', token => $token);                !!!parse-error (type => 'not closed', text => 'caption',
5122                                  token => $token);
5123    
5124                ## As if </caption>                ## As if </caption>
5125                ## have a table element in table scope                ## have a table element in table scope
5126                my $i;                my $i;
5127                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5128                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5129                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
5130                    !!!cp ('t184');                    !!!cp ('t184');
5131                    $i = $_;                    $i = $_;
5132                    last INSCOPE;                    last INSCOPE;
5133                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5134                    !!!cp ('t185');                    !!!cp ('t185');
5135                    last INSCOPE;                    last INSCOPE;
5136                  }                  }
5137                } # INSCOPE                } # INSCOPE
5138                unless (defined $i) {                unless (defined $i) {
5139                  !!!cp ('t186');                  !!!cp ('t186');
5140                  !!!parse-error (type => 'unmatched end tag:caption', token => $token);                  !!!parse-error (type => 'unmatched end tag',
5141                                    text => 'caption', token => $token);
5142                  ## Ignore the token                  ## Ignore the token
5143                  !!!next-token;                  !!!next-token;
5144                  redo B;                  next B;
5145                }                }
5146                                
5147                ## generate implied end tags                ## generate implied end tags
5148                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
5149                  !!!cp ('t187');                  !!!cp ('t187');
5150                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5151                }                }
5152    
5153                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5154                  !!!cp ('t188');                  !!!cp ('t188');
5155                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                  !!!parse-error (type => 'not closed',
5156                                    text => $self->{open_elements}->[-1]->[0]
5157                                        ->manakai_local_name,
5158                                    token => $token);
5159                } else {                } else {
5160                  !!!cp ('t189');                  !!!cp ('t189');
5161                }                }
# Line 4233  sub _tree_construction_main ($) { Line 5167  sub _tree_construction_main ($) {
5167                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5168    
5169                ## reprocess                ## reprocess
5170                redo B;                next B;
5171              } elsif ({              } elsif ({
5172                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
5173                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5174                if ($self->{insertion_mode} & BODY_TABLE_IMS) {                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
5175                  !!!cp ('t190');                  !!!cp ('t190');
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 ('t191');                  !!!cp ('t191');
5183                  #                  #
# Line 4253  sub _tree_construction_main ($) { Line 5188  sub _tree_construction_main ($) {
5188                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
5189                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5190                !!!cp ('t192');                !!!cp ('t192');
5191                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag',
5192                                  text => $token->{tag_name}, token => $token);
5193                ## Ignore the token                ## Ignore the token
5194                !!!next-token;                !!!next-token;
5195                redo B;                next B;
5196              } else {              } else {
5197                !!!cp ('t193');                !!!cp ('t193');
5198                #                #
5199              }              }
5200        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5201          for my $entry (@{$self->{open_elements}}) {          for my $entry (@{$self->{open_elements}}) {
5202            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]}) {  
5203              !!!cp ('t75');              !!!cp ('t75');
5204              !!!parse-error (type => 'in body:#eof', token => $token);              !!!parse-error (type => 'in body:#eof', token => $token);
5205              last;              last;
# Line 4290  sub _tree_construction_main ($) { Line 5223  sub _tree_construction_main ($) {
5223            unless (length $token->{data}) {            unless (length $token->{data}) {
5224              !!!cp ('t194');              !!!cp ('t194');
5225              !!!next-token;              !!!next-token;
5226              redo B;              next B;
5227            } else {            } else {
5228              !!!cp ('t195');              !!!cp ('t195');
5229            }            }
5230          }          }
5231    
5232              !!!parse-error (type => 'in table:#character', token => $token);          !!!parse-error (type => 'in table:#text', token => $token);
5233    
5234              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
5235              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
# Line 4304  sub _tree_construction_main ($) { Line 5237  sub _tree_construction_main ($) {
5237              ## result in a new Text node.              ## result in a new Text node.
5238              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
5239                            
5240              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]}) {  
5241                # MUST                # MUST
5242                my $foster_parent_element;                my $foster_parent_element;
5243                my $next_sibling;                my $next_sibling;
5244                my $prev_sibling;                my $prev_sibling;
5245                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
5246                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
5247                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
5248                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
5249                      !!!cp ('t196');                      !!!cp ('t196');
# Line 4348  sub _tree_construction_main ($) { Line 5278  sub _tree_construction_main ($) {
5278          }          }
5279                            
5280          !!!next-token;          !!!next-token;
5281          redo B;          next B;
5282        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5283              if ({          if ({
5284                   tr => ($self->{insertion_mode} != IN_ROW_IM),               tr => ($self->{insertion_mode} != IN_ROW_IM),
5285                   th => 1, td => 1,               th => 1, td => 1,
5286                  }->{$token->{tag_name}}) {              }->{$token->{tag_name}}) {
5287                if ($self->{insertion_mode} == IN_TABLE_IM) {            if ($self->{insertion_mode} == IN_TABLE_IM) {
5288                  ## Clear back to table context              ## Clear back to table context
5289                  while ($self->{open_elements}->[-1]->[1] ne 'table' and              while (not ($self->{open_elements}->[-1]->[1]
5290                         $self->{open_elements}->[-1]->[1] ne 'html') {                              & TABLE_SCOPING_EL)) {
5291                    !!!cp ('t201');                !!!cp ('t201');
5292                    pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5293                  }              }
5294                                
5295                  !!!insert-element ('tbody');              !!!insert-element ('tbody',, $token);
5296                  $self->{insertion_mode} = IN_TABLE_BODY_IM;              $self->{insertion_mode} = IN_TABLE_BODY_IM;
5297                  ## reprocess in the "in table body" insertion mode...              ## reprocess in the "in table body" insertion mode...
5298                }            }
5299              
5300                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {            if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5301                  unless ($token->{tag_name} eq 'tr') {              unless ($token->{tag_name} eq 'tr') {
5302                    !!!cp ('t202');                !!!cp ('t202');
5303                    !!!parse-error (type => 'missing start tag:tr', token => $token);                !!!parse-error (type => 'missing start tag:tr', token => $token);
5304                  }              }
5305                                    
5306                  ## Clear back to table body context              ## Clear back to table body context
5307                  while (not {              while (not ($self->{open_elements}->[-1]->[1]
5308                    tbody => 1, tfoot => 1, thead => 1, html => 1,                              & TABLE_ROWS_SCOPING_EL)) {
5309                  }->{$self->{open_elements}->[-1]->[1]}) {                !!!cp ('t203');
5310                    !!!cp ('t203');                ## ISSUE: Can this case be reached?
5311                    ## ISSUE: Can this case be reached?                pop @{$self->{open_elements}};
5312                    pop @{$self->{open_elements}};              }
                 }  
5313                                    
5314                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
5315                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5316                    !!!cp ('t204');                    !!!cp ('t204');
5317                    !!!insert-element ($token->{tag_name}, $token->{attributes});                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5318                      !!!nack ('t204');
5319                    !!!next-token;                    !!!next-token;
5320                    redo B;                    next B;
5321                  } else {                  } else {
5322                    !!!cp ('t205');                    !!!cp ('t205');
5323                    !!!insert-element ('tr');                    !!!insert-element ('tr',, $token);
5324                    ## reprocess in the "in row" insertion mode                    ## reprocess in the "in row" insertion mode
5325                  }                  }
5326                } else {                } else {
# Line 4398  sub _tree_construction_main ($) { Line 5328  sub _tree_construction_main ($) {
5328                }                }
5329    
5330                ## Clear back to table row context                ## Clear back to table row context
5331                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5332                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5333                  !!!cp ('t207');                  !!!cp ('t207');
5334                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5335                }                }
5336                                
5337                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5338                $self->{insertion_mode} = IN_CELL_IM;                $self->{insertion_mode} = IN_CELL_IM;
5339    
5340                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
5341                                
5342                  !!!nack ('t207.1');
5343                !!!next-token;                !!!next-token;
5344                redo B;                next B;
5345              } elsif ({              } elsif ({
5346                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5347                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
# Line 4423  sub _tree_construction_main ($) { Line 5353  sub _tree_construction_main ($) {
5353                  my $i;                  my $i;
5354                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5355                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5356                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5357                      !!!cp ('t208');                      !!!cp ('t208');
5358                      $i = $_;                      $i = $_;
5359                      last INSCOPE;                      last INSCOPE;
5360                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             html => 1,  
   
                             ## NOTE: This element does not appear here, maybe.  
                             table => 1,  
                            }->{$node->[1]}) {  
5361                      !!!cp ('t209');                      !!!cp ('t209');
5362                      last INSCOPE;                      last INSCOPE;
5363                    }                    }
5364                  } # INSCOPE                  } # INSCOPE
5365                  unless (defined $i) {                  unless (defined $i) {
5366                   !!!cp ('t210');                    !!!cp ('t210');
5367  ## TODO: This type is wrong.  ## TODO: This type is wrong.
5368                   !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);                    !!!parse-error (type => 'unmacthed end tag',
5369                                      text => $token->{tag_name}, token => $token);
5370                    ## Ignore the token                    ## Ignore the token
5371                      !!!nack ('t210.1');
5372                    !!!next-token;                    !!!next-token;
5373                    redo B;                    next B;
5374                  }                  }
5375                                    
5376                  ## Clear back to table row context                  ## Clear back to table row context
5377                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5378                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5379                    !!!cp ('t211');                    !!!cp ('t211');
5380                    ## ISSUE: Can this case be reached?                    ## ISSUE: Can this case be reached?
5381                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4460  sub _tree_construction_main ($) { Line 5386  sub _tree_construction_main ($) {
5386                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5387                    !!!cp ('t212');                    !!!cp ('t212');
5388                    ## reprocess                    ## reprocess
5389                    redo B;                    !!!ack-later;
5390                      next B;
5391                  } else {                  } else {
5392                    !!!cp ('t213');                    !!!cp ('t213');
5393                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
# Line 4472  sub _tree_construction_main ($) { Line 5399  sub _tree_construction_main ($) {
5399                  my $i;                  my $i;
5400                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5401                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5402                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5403                      !!!cp ('t214');                      !!!cp ('t214');
5404                      $i = $_;                      $i = $_;
5405                      last INSCOPE;                      last INSCOPE;
5406                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5407                      !!!cp ('t215');                      !!!cp ('t215');
5408                      last INSCOPE;                      last INSCOPE;
5409                    }                    }
5410                  } # INSCOPE                  } # INSCOPE
5411                  unless (defined $i) {                  unless (defined $i) {
5412                    !!!cp ('t216');                    !!!cp ('t216');
5413  ## TODO: This erorr type ios wrong.  ## TODO: This erorr type is wrong.
5414                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                    !!!parse-error (type => 'unmatched end tag',
5415                                      text => $token->{tag_name}, token => $token);
5416                    ## Ignore the token                    ## Ignore the token
5417                      !!!nack ('t216.1');
5418                    !!!next-token;                    !!!next-token;
5419                    redo B;                    next B;
5420                  }                  }
5421    
5422                  ## Clear back to table body context                  ## Clear back to table body context
5423                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5424                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5425                    !!!cp ('t217');                    !!!cp ('t217');
5426                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5427                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4519  sub _tree_construction_main ($) { Line 5443  sub _tree_construction_main ($) {
5443    
5444                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
5445                  ## Clear back to table context                  ## Clear back to table context
5446                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5447                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5448                    !!!cp ('t219');                    !!!cp ('t219');
5449                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5450                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5451                  }                  }
5452                                    
5453                  !!!insert-element ('colgroup');                  !!!insert-element ('colgroup',, $token);
5454                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5455                  ## reprocess                  ## reprocess
5456                  redo B;                  !!!ack-later;
5457                    next B;
5458                } elsif ({                } elsif ({
5459                          caption => 1,                          caption => 1,
5460                          colgroup => 1,                          colgroup => 1,
5461                          tbody => 1, tfoot => 1, thead => 1,                          tbody => 1, tfoot => 1, thead => 1,
5462                         }->{$token->{tag_name}}) {                         }->{$token->{tag_name}}) {
5463                  ## Clear back to table context                  ## Clear back to table context
5464                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5465                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5466                    !!!cp ('t220');                    !!!cp ('t220');
5467                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5468                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4546  sub _tree_construction_main ($) { Line 5471  sub _tree_construction_main ($) {
5471                  push @$active_formatting_elements, ['#marker', '']                  push @$active_formatting_elements, ['#marker', '']
5472                      if $token->{tag_name} eq 'caption';                      if $token->{tag_name} eq 'caption';
5473                                    
5474                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5475                  $self->{insertion_mode} = {                  $self->{insertion_mode} = {
5476                                             caption => IN_CAPTION_IM,                                             caption => IN_CAPTION_IM,
5477                                             colgroup => IN_COLUMN_GROUP_IM,                                             colgroup => IN_COLUMN_GROUP_IM,
# Line 4555  sub _tree_construction_main ($) { Line 5480  sub _tree_construction_main ($) {
5480                                             thead => IN_TABLE_BODY_IM,                                             thead => IN_TABLE_BODY_IM,
5481                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
5482                  !!!next-token;                  !!!next-token;
5483                  redo B;                  !!!nack ('t220.1');
5484                    next B;
5485                } else {                } else {
5486                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
5487                }                }
5488              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5489                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                !!!parse-error (type => 'not closed',
5490                                  text => $self->{open_elements}->[-1]->[0]
5491                                      ->manakai_local_name,
5492                                  token => $token);
5493    
5494                ## As if </table>                ## As if </table>
5495                ## have a table element in table scope                ## have a table element in table scope
5496                my $i;                my $i;
5497                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5498                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5499                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5500                    !!!cp ('t221');                    !!!cp ('t221');
5501                    $i = $_;                    $i = $_;
5502                    last INSCOPE;                    last INSCOPE;
5503                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           #table => 1,  
                           html => 1,  
                          }->{$node->[1]}) {  
5504                    !!!cp ('t222');                    !!!cp ('t222');
5505                    last INSCOPE;                    last INSCOPE;
5506                  }                  }
# Line 4582  sub _tree_construction_main ($) { Line 5508  sub _tree_construction_main ($) {
5508                unless (defined $i) {                unless (defined $i) {
5509                  !!!cp ('t223');                  !!!cp ('t223');
5510  ## TODO: The following is wrong, maybe.  ## TODO: The following is wrong, maybe.
5511                  !!!parse-error (type => 'unmatched end tag:table', token => $token);                  !!!parse-error (type => 'unmatched end tag', text => 'table',
5512                                    token => $token);
5513                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5514                    !!!nack ('t223.1');
5515                  !!!next-token;                  !!!next-token;
5516                  redo B;                  next B;
5517                }                }
5518                                
5519  ## TODO: Followings are removed from the latest spec.  ## TODO: Followings are removed from the latest spec.
5520                ## generate implied end tags                ## generate implied end tags
5521                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
5522                  !!!cp ('t224');                  !!!cp ('t224');
5523                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5524                }                }
5525    
5526                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5527                  !!!cp ('t225');                  !!!cp ('t225');
5528  ## ISSUE: Can this case be reached?                  ## NOTE: |<table><tr><table>|
5529                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                  !!!parse-error (type => 'not closed',
5530                                    text => $self->{open_elements}->[-1]->[0]
5531                                        ->manakai_local_name,
5532                                    token => $token);
5533                } else {                } else {
5534                  !!!cp ('t226');                  !!!cp ('t226');
5535                }                }
# Line 4610  sub _tree_construction_main ($) { Line 5539  sub _tree_construction_main ($) {
5539    
5540                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5541    
5542                ## reprocess            ## reprocess
5543                redo B;            !!!ack-later;
5544              next B;
5545          } elsif ($token->{tag_name} eq 'style') {          } elsif ($token->{tag_name} eq 'style') {
5546            if (not $open_tables->[-1]->[1]) { # tainted            if (not $open_tables->[-1]->[1]) { # tainted
5547              !!!cp ('t227.8');              !!!cp ('t227.8');
5548              ## NOTE: This is a "as if in head" code clone.              ## NOTE: This is a "as if in head" code clone.
5549              $parse_rcdata->(CDATA_CONTENT_MODEL);              $parse_rcdata->(CDATA_CONTENT_MODEL);
5550              redo B;              next B;
5551            } else {            } else {
5552              !!!cp ('t227.7');              !!!cp ('t227.7');
5553              #              #
# Line 4627  sub _tree_construction_main ($) { Line 5557  sub _tree_construction_main ($) {
5557              !!!cp ('t227.6');              !!!cp ('t227.6');
5558              ## NOTE: This is a "as if in head" code clone.              ## NOTE: This is a "as if in head" code clone.
5559              $script_start_tag->();              $script_start_tag->();
5560              redo B;              next B;
5561            } else {            } else {
5562              !!!cp ('t227.5');              !!!cp ('t227.5');
5563              #              #
# Line 4638  sub _tree_construction_main ($) { Line 5568  sub _tree_construction_main ($) {
5568                my $type = lc $token->{attributes}->{type}->{value};                my $type = lc $token->{attributes}->{type}->{value};
5569                if ($type eq 'hidden') {                if ($type eq 'hidden') {
5570                  !!!cp ('t227.3');                  !!!cp ('t227.3');
5571                  !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'in table',
5572                                    text => $token->{tag_name}, token => $token);
5573    
5574                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5575    
5576                  ## TODO: form element pointer                  ## TODO: form element pointer
5577    
5578                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5579    
5580                  !!!next-token;                  !!!next-token;
5581                  redo B;                  !!!ack ('t227.2.1');
5582                    next B;
5583                } else {                } else {
5584                  !!!cp ('t227.2');                  !!!cp ('t227.2');
5585                  #                  #
# Line 4665  sub _tree_construction_main ($) { Line 5597  sub _tree_construction_main ($) {
5597            #            #
5598          }          }
5599    
5600          !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'in table', text => $token->{tag_name},
5601                            token => $token);
5602    
5603          $insert = $insert_to_foster;          $insert = $insert_to_foster;
5604          #          #
# Line 4676  sub _tree_construction_main ($) { Line 5609  sub _tree_construction_main ($) {
5609                my $i;                my $i;
5610                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5611                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5612                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_ROW_EL) {
5613                    !!!cp ('t228');                    !!!cp ('t228');
5614                    $i = $_;                    $i = $_;
5615                    last INSCOPE;                    last INSCOPE;
5616                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5617                    !!!cp ('t229');                    !!!cp ('t229');
5618                    last INSCOPE;                    last INSCOPE;
5619                  }                  }
5620                } # INSCOPE                } # INSCOPE
5621                unless (defined $i) {                unless (defined $i) {
5622                  !!!cp ('t230');                  !!!cp ('t230');
5623                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
5624                                    text => $token->{tag_name}, token => $token);
5625                  ## Ignore the token                  ## Ignore the token
5626                    !!!nack ('t230.1');
5627                  !!!next-token;                  !!!next-token;
5628                  redo B;                  next B;
5629                } else {                } else {
5630                  !!!cp ('t232');                  !!!cp ('t232');
5631                }                }
5632    
5633                ## Clear back to table row context                ## Clear back to table row context
5634                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5635                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5636                  !!!cp ('t231');                  !!!cp ('t231');
5637  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
5638                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 4709  sub _tree_construction_main ($) { Line 5641  sub _tree_construction_main ($) {
5641                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5642                $self->{insertion_mode} = IN_TABLE_BODY_IM;                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5643                !!!next-token;                !!!next-token;
5644                redo B;                !!!nack ('t231.1');
5645                  next B;
5646              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5647                if ($self->{insertion_mode} == IN_ROW_IM) {                if ($self->{insertion_mode} == IN_ROW_IM) {
5648                  ## As if </tr>                  ## As if </tr>
# Line 4717  sub _tree_construction_main ($) { Line 5650  sub _tree_construction_main ($) {
5650                  my $i;                  my $i;
5651                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5652                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5653                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5654                      !!!cp ('t233');                      !!!cp ('t233');
5655                      $i = $_;                      $i = $_;
5656                      last INSCOPE;                      last INSCOPE;
5657                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5658                      !!!cp ('t234');                      !!!cp ('t234');
5659                      last INSCOPE;                      last INSCOPE;
5660                    }                    }
# Line 4731  sub _tree_construction_main ($) { Line 5662  sub _tree_construction_main ($) {
5662                  unless (defined $i) {                  unless (defined $i) {
5663                    !!!cp ('t235');                    !!!cp ('t235');
5664  ## TODO: The following is wrong.  ## TODO: The following is wrong.
5665                    !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);                    !!!parse-error (type => 'unmatched end tag',
5666                                      text => $token->{type}, token => $token);
5667                    ## Ignore the token                    ## Ignore the token
5668                      !!!nack ('t236.1');
5669                    !!!next-token;                    !!!next-token;
5670                    redo B;                    next B;
5671                  }                  }
5672                                    
5673                  ## Clear back to table row context                  ## Clear back to table row context
5674                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5675                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5676                    !!!cp ('t236');                    !!!cp ('t236');
5677  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
5678                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4756  sub _tree_construction_main ($) { Line 5688  sub _tree_construction_main ($) {
5688                  my $i;                  my $i;
5689                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5690                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5691                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5692                      !!!cp ('t237');                      !!!cp ('t237');
5693                      $i = $_;                      $i = $_;
5694                      last INSCOPE;                      last INSCOPE;
5695                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5696                      !!!cp ('t238');                      !!!cp ('t238');
5697                      last INSCOPE;                      last INSCOPE;
5698                    }                    }
5699                  } # INSCOPE                  } # INSCOPE
5700                  unless (defined $i) {                  unless (defined $i) {
5701                    !!!cp ('t239');                    !!!cp ('t239');
5702                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                    !!!parse-error (type => 'unmatched end tag',
5703                                      text => $token->{tag_name}, token => $token);
5704                    ## Ignore the token                    ## Ignore the token
5705                      !!!nack ('t239.1');
5706                    !!!next-token;                    !!!next-token;
5707                    redo B;                    next B;
5708                  }                  }
5709                                    
5710                  ## Clear back to table body context                  ## Clear back to table body context
5711                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5712                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5713                    !!!cp ('t240');                    !!!cp ('t240');
5714                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5715                  }                  }
# Line 4806  sub _tree_construction_main ($) { Line 5735  sub _tree_construction_main ($) {
5735                my $i;                my $i;
5736                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5737                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5738                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_EL) {
5739                    !!!cp ('t241');                    !!!cp ('t241');
5740                    $i = $_;                    $i = $_;
5741                    last INSCOPE;                    last INSCOPE;
5742                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5743                    !!!cp ('t242');                    !!!cp ('t242');
5744                    last INSCOPE;                    last INSCOPE;
5745                  }                  }
5746                } # INSCOPE                } # INSCOPE
5747                unless (defined $i) {                unless (defined $i) {
5748                  !!!cp ('t243');                  !!!cp ('t243');
5749                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
5750                                    text => $token->{tag_name}, token => $token);
5751                  ## Ignore the token                  ## Ignore the token
5752                    !!!nack ('t243.1');
5753                  !!!next-token;                  !!!next-token;
5754                  redo B;                  next B;
5755                }                }
5756                                    
5757                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
# Line 4831  sub _tree_construction_main ($) { Line 5760  sub _tree_construction_main ($) {
5760                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5761                                
5762                !!!next-token;                !!!next-token;
5763                redo B;                next B;
5764              } elsif ({              } elsif ({
5765                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5766                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
# Line 4841  sub _tree_construction_main ($) { Line 5770  sub _tree_construction_main ($) {
5770                  my $i;                  my $i;
5771                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5772                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5773                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5774                      !!!cp ('t247');                      !!!cp ('t247');
5775                      $i = $_;                      $i = $_;
5776                      last INSCOPE;                      last INSCOPE;
5777                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5778                      !!!cp ('t248');                      !!!cp ('t248');
5779                      last INSCOPE;                      last INSCOPE;
5780                    }                    }
5781                  } # INSCOPE                  } # INSCOPE
5782                    unless (defined $i) {                    unless (defined $i) {
5783                      !!!cp ('t249');                      !!!cp ('t249');
5784                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                      !!!parse-error (type => 'unmatched end tag',
5785                                        text => $token->{tag_name}, token => $token);
5786                      ## Ignore the token                      ## Ignore the token
5787                        !!!nack ('t249.1');
5788                      !!!next-token;                      !!!next-token;
5789                      redo B;                      next B;
5790                    }                    }
5791                                    
5792                  ## As if </tr>                  ## As if </tr>
# Line 4865  sub _tree_construction_main ($) { Line 5794  sub _tree_construction_main ($) {
5794                  my $i;                  my $i;
5795                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5796                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5797                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5798                      !!!cp ('t250');                      !!!cp ('t250');
5799                      $i = $_;                      $i = $_;
5800                      last INSCOPE;                      last INSCOPE;
5801                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5802                      !!!cp ('t251');                      !!!cp ('t251');
5803                      last INSCOPE;                      last INSCOPE;
5804                    }                    }
5805                  } # INSCOPE                  } # INSCOPE
5806                    unless (defined $i) {                    unless (defined $i) {
5807                      !!!cp ('t252');                      !!!cp ('t252');
5808                      !!!parse-error (type => 'unmatched end tag:tr', token => $token);                      !!!parse-error (type => 'unmatched end tag',
5809                                        text => 'tr', token => $token);
5810                      ## Ignore the token                      ## Ignore the token
5811                        !!!nack ('t252.1');
5812                      !!!next-token;                      !!!next-token;
5813                      redo B;                      next B;
5814                    }                    }
5815                                    
5816                  ## Clear back to table row context                  ## Clear back to table row context
5817                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5818                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5819                    !!!cp ('t253');                    !!!cp ('t253');
5820  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
5821                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4902  sub _tree_construction_main ($) { Line 5830  sub _tree_construction_main ($) {
5830                my $i;                my $i;
5831                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5832                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5833                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5834                    !!!cp ('t254');                    !!!cp ('t254');
5835                    $i = $_;                    $i = $_;
5836                    last INSCOPE;                    last INSCOPE;
5837                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5838                    !!!cp ('t255');                    !!!cp ('t255');
5839                    last INSCOPE;                    last INSCOPE;
5840                  }                  }
5841                } # INSCOPE                } # INSCOPE
5842                unless (defined $i) {                unless (defined $i) {
5843                  !!!cp ('t256');                  !!!cp ('t256');
5844                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
5845                                    text => $token->{tag_name}, token => $token);
5846                  ## Ignore the token                  ## Ignore the token
5847                    !!!nack ('t256.1');
5848                  !!!next-token;                  !!!next-token;
5849                  redo B;                  next B;
5850                }                }
5851    
5852                ## Clear back to table body context                ## Clear back to table body context
5853                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5854                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5855                  !!!cp ('t257');                  !!!cp ('t257');
5856  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
5857                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 4932  sub _tree_construction_main ($) { Line 5859  sub _tree_construction_main ($) {
5859    
5860                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5861                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5862                  !!!nack ('t257.1');
5863                !!!next-token;                !!!next-token;
5864                redo B;                next B;
5865              } elsif ({              } elsif ({
5866                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
5867                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
5868                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5869                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
5870                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5871                !!!cp ('t258');            !!!cp ('t258');
5872                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
5873                ## Ignore the token                            text => $token->{tag_name}, token => $token);
5874                !!!next-token;            ## Ignore the token
5875                redo B;            !!!nack ('t258.1');
5876               !!!next-token;
5877              next B;
5878          } else {          } else {
5879            !!!cp ('t259');            !!!cp ('t259');
5880            !!!parse-error (type => 'in table:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'in table:/',
5881                              text => $token->{tag_name}, token => $token);
5882    
5883            $insert = $insert_to_foster;            $insert = $insert_to_foster;
5884            #            #
5885          }          }
5886        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5887          unless ($self->{open_elements}->[-1]->[1] eq 'html' and          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5888                  @{$self->{open_elements}} == 1) { # redundant, maybe                  @{$self->{open_elements}} == 1) { # redundant, maybe
5889            !!!parse-error (type => 'in body:#eof', token => $token);            !!!parse-error (type => 'in body:#eof', token => $token);
5890            !!!cp ('t259.1');            !!!cp ('t259.1');
# Line 4975  sub _tree_construction_main ($) { Line 5906  sub _tree_construction_main ($) {
5906                unless (length $token->{data}) {                unless (length $token->{data}) {
5907                  !!!cp ('t260');                  !!!cp ('t260');
5908                  !!!next-token;                  !!!next-token;
5909                  redo B;                  next B;
5910                }                }
5911              }              }
5912                            
# Line 4984  sub _tree_construction_main ($) { Line 5915  sub _tree_construction_main ($) {
5915            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
5916              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
5917                !!!cp ('t262');                !!!cp ('t262');
5918                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5919                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5920                  !!!ack ('t262.1');
5921                !!!next-token;                !!!next-token;
5922                redo B;                next B;
5923              } else {              } else {
5924                !!!cp ('t263');                !!!cp ('t263');
5925                #                #
5926              }              }
5927            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
5928              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
5929                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5930                  !!!cp ('t264');                  !!!cp ('t264');
5931                  !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);                  !!!parse-error (type => 'unmatched end tag',
5932                                    text => 'colgroup', token => $token);
5933                  ## Ignore the token                  ## Ignore the token
5934                  !!!next-token;                  !!!next-token;
5935                  redo B;                  next B;
5936                } else {                } else {
5937                  !!!cp ('t265');                  !!!cp ('t265');
5938                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
5939                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5940                  !!!next-token;                  !!!next-token;
5941                  redo B;                              next B;            
5942                }                }
5943              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
5944                !!!cp ('t266');                !!!cp ('t266');
5945                !!!parse-error (type => 'unmatched end tag:col', token => $token);                !!!parse-error (type => 'unmatched end tag',
5946                                  text => 'col', token => $token);
5947                ## Ignore the token                ## Ignore the token
5948                !!!next-token;                !!!next-token;
5949                redo B;                next B;
5950              } else {              } else {
5951                !!!cp ('t267');                !!!cp ('t267');
5952                #                #
5953              }              }
5954        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5955          if ($self->{open_elements}->[-1]->[1] eq 'html' or          if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5956              @{$self->{open_elements}} == 1) { # redundant, maybe              @{$self->{open_elements}} == 1) { # redundant, maybe
5957            !!!cp ('t270.2');            !!!cp ('t270.2');
5958            ## Stop parsing.            ## Stop parsing.
# Line 5029  sub _tree_construction_main ($) { Line 5963  sub _tree_construction_main ($) {
5963            pop @{$self->{open_elements}}; # colgroup            pop @{$self->{open_elements}}; # colgroup
5964            $self->{insertion_mode} = IN_TABLE_IM;            $self->{insertion_mode} = IN_TABLE_IM;
5965            ## Reprocess.            ## Reprocess.
5966            redo B;            next B;
5967          }          }
5968        } else {        } else {
5969          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5970        }        }
5971    
5972            ## As if </colgroup>            ## As if </colgroup>
5973            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5974              !!!cp ('t269');              !!!cp ('t269');
5975  ## TODO: Wrong error type?  ## TODO: Wrong error type?
5976              !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);              !!!parse-error (type => 'unmatched end tag',
5977                                text => 'colgroup', token => $token);
5978              ## Ignore the token              ## Ignore the token
5979                !!!nack ('t269.1');
5980              !!!next-token;              !!!next-token;
5981              redo B;              next B;
5982            } else {            } else {
5983              !!!cp ('t270');              !!!cp ('t270');
5984              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
5985              $self->{insertion_mode} = IN_TABLE_IM;              $self->{insertion_mode} = IN_TABLE_IM;
5986                !!!ack-later;
5987              ## reprocess              ## reprocess
5988              redo B;              next B;
5989            }            }
5990      } elsif ($self->{insertion_mode} & SELECT_IMS) {      } elsif ($self->{insertion_mode} & SELECT_IMS) {
5991        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5992          !!!cp ('t271');          !!!cp ('t271');
5993          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5994          !!!next-token;          !!!next-token;
5995          redo B;          next B;
5996        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5997              if ($token->{tag_name} eq 'option') {          if ($token->{tag_name} eq 'option') {
5998                if ($self->{open_elements}->[-1]->[1] eq 'option') {            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5999                  !!!cp ('t272');              !!!cp ('t272');
6000                  ## As if </option>              ## As if </option>
6001                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6002                } else {            } else {
6003                  !!!cp ('t273');              !!!cp ('t273');
6004                }            }
6005    
6006                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6007                !!!next-token;            !!!nack ('t273.1');
6008                redo B;            !!!next-token;
6009              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
6010                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
6011                  !!!cp ('t274');            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6012                  ## As if </option>              !!!cp ('t274');
6013                  pop @{$self->{open_elements}};              ## As if </option>
6014                } else {              pop @{$self->{open_elements}};
6015                  !!!cp ('t275');            } else {
6016                }              !!!cp ('t275');
6017              }
6018    
6019                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6020                  !!!cp ('t276');              !!!cp ('t276');
6021                  ## As if </optgroup>              ## As if </optgroup>
6022                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6023                } else {            } else {
6024                  !!!cp ('t277');              !!!cp ('t277');
6025                }            }
6026    
6027                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6028                !!!next-token;            !!!nack ('t277.1');
6029                redo B;            !!!next-token;
6030          } elsif ($token->{tag_name} eq 'select' or            next B;
6031                   $token->{tag_name} eq 'input' or          } elsif ({
6032                       select => 1, input => 1, textarea => 1,
6033                     }->{$token->{tag_name}} or
6034                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6035                    {                    {
6036                     caption => 1, table => 1,                     caption => 1, table => 1,
# Line 5098  sub _tree_construction_main ($) { Line 6038  sub _tree_construction_main ($) {
6038                     tr => 1, td => 1, th => 1,                     tr => 1, td => 1, th => 1,
6039                    }->{$token->{tag_name}})) {                    }->{$token->{tag_name}})) {
6040            ## TODO: The type below is not good - <select> is replaced by </select>            ## TODO: The type below is not good - <select> is replaced by </select>
6041            !!!parse-error (type => 'not closed:select', token => $token);            !!!parse-error (type => 'not closed', text => 'select',
6042                              token => $token);
6043            ## NOTE: As if the token were </select> (<select> case) or            ## NOTE: As if the token were </select> (<select> case) or
6044            ## as if there were </select> (otherwise).            ## as if there were </select> (otherwise).
6045                ## have an element in table scope            ## have an element in table scope
6046                my $i;            my $i;
6047                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6048                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6049                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
6050                    !!!cp ('t278');                !!!cp ('t278');
6051                    $i = $_;                $i = $_;
6052                    last INSCOPE;                last INSCOPE;
6053                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6054                            table => 1, html => 1,                !!!cp ('t279');
6055                           }->{$node->[1]}) {                last INSCOPE;
6056                    !!!cp ('t279');              }
6057                    last INSCOPE;            } # INSCOPE
6058                  }            unless (defined $i) {
6059                } # INSCOPE              !!!cp ('t280');
6060                unless (defined $i) {              !!!parse-error (type => 'unmatched end tag',
6061                  !!!cp ('t280');                              text => 'select', token => $token);
6062                  !!!parse-error (type => 'unmatched end tag:select', token => $token);              ## Ignore the token
6063                  ## Ignore the token              !!!nack ('t280.1');
6064                  !!!next-token;              !!!next-token;
6065                  redo B;              next B;
6066                }            }
6067                                
6068                !!!cp ('t281');            !!!cp ('t281');
6069                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6070    
6071                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6072    
6073            if ($token->{tag_name} eq 'select') {            if ($token->{tag_name} eq 'select') {
6074              !!!cp ('t281.2');              !!!nack ('t281.2');
6075              !!!next-token;              !!!next-token;
6076              redo B;              next B;
6077            } else {            } else {
6078              !!!cp ('t281.1');              !!!cp ('t281.1');
6079                !!!ack-later;
6080              ## Reprocess the token.              ## Reprocess the token.
6081              redo B;              next B;
6082            }            }
6083          } else {          } else {
6084            !!!cp ('t282');            !!!cp ('t282');
6085            !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'in select',
6086                              text => $token->{tag_name}, token => $token);
6087            ## Ignore the token            ## Ignore the token
6088              !!!nack ('t282.1');
6089            !!!next-token;            !!!next-token;
6090            redo B;            next B;
6091          }          }
6092        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6093              if ($token->{tag_name} eq 'optgroup') {          if ($token->{tag_name} eq 'optgroup') {
6094                if ($self->{open_elements}->[-1]->[1] eq 'option' and            if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
6095                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
6096                  !!!cp ('t283');              !!!cp ('t283');
6097                  ## As if </option>              ## As if </option>
6098                  splice @{$self->{open_elements}}, -2;              splice @{$self->{open_elements}}, -2;
6099                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6100                  !!!cp ('t284');              !!!cp ('t284');
6101                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6102                } else {            } else {
6103                  !!!cp ('t285');              !!!cp ('t285');
6104                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag',
6105                  ## Ignore the token                              text => $token->{tag_name}, token => $token);
6106                }              ## Ignore the token
6107                !!!next-token;            }
6108                redo B;            !!!nack ('t285.1');
6109              } elsif ($token->{tag_name} eq 'option') {            !!!next-token;
6110                if ($self->{open_elements}->[-1]->[1] eq 'option') {            next B;
6111                  !!!cp ('t286');          } elsif ($token->{tag_name} eq 'option') {
6112                  pop @{$self->{open_elements}};            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6113                } else {              !!!cp ('t286');
6114                  !!!cp ('t287');              pop @{$self->{open_elements}};
6115                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            } else {
6116                  ## Ignore the token              !!!cp ('t287');
6117                }              !!!parse-error (type => 'unmatched end tag',
6118                !!!next-token;                              text => $token->{tag_name}, token => $token);
6119                redo B;              ## Ignore the token
6120              } elsif ($token->{tag_name} eq 'select') {            }
6121                ## have an element in table scope            !!!nack ('t287.1');
6122                my $i;            !!!next-token;
6123                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            next B;
6124                  my $node = $self->{open_elements}->[$_];          } elsif ($token->{tag_name} eq 'select') {
6125                  if ($node->[1] eq $token->{tag_name}) {            ## have an element in table scope
6126                    !!!cp ('t288');            my $i;
6127                    $i = $_;            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6128                    last INSCOPE;              my $node = $self->{open_elements}->[$_];
6129                  } elsif ({              if ($node->[1] & SELECT_EL) {
6130                            table => 1, html => 1,                !!!cp ('t288');
6131                           }->{$node->[1]}) {                $i = $_;
6132                    !!!cp ('t289');                last INSCOPE;
6133                    last INSCOPE;              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6134                  }                !!!cp ('t289');
6135                } # INSCOPE                last INSCOPE;
6136                unless (defined $i) {              }
6137                  !!!cp ('t290');            } # INSCOPE
6138                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            unless (defined $i) {
6139                  ## Ignore the token              !!!cp ('t290');
6140                  !!!next-token;              !!!parse-error (type => 'unmatched end tag',
6141                  redo B;                              text => $token->{tag_name}, token => $token);
6142                }              ## Ignore the token
6143                !!!nack ('t290.1');
6144                !!!next-token;
6145                next B;
6146              }
6147                                
6148                !!!cp ('t291');            !!!cp ('t291');
6149                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6150    
6151                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6152    
6153                !!!next-token;            !!!nack ('t291.1');
6154                redo B;            !!!next-token;
6155              next B;
6156          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6157                   {                   {
6158                    caption => 1, table => 1, tbody => 1,                    caption => 1, table => 1, tbody => 1,
6159                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
6160                   }->{$token->{tag_name}}) {                   }->{$token->{tag_name}}) {
6161  ## TODO: The following is wrong?  ## TODO: The following is wrong?
6162                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
6163                              text => $token->{tag_name}, token => $token);
6164                                
6165                ## have an element in table scope            ## have an element in table scope
6166                my $i;            my $i;
6167                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6168                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6169                  if ($node->[1] eq $token->{tag_name}) {              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6170                    !!!cp ('t292');                !!!cp ('t292');
6171                    $i = $_;                $i = $_;
6172                    last INSCOPE;                last INSCOPE;
6173                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6174                            table => 1, html => 1,                !!!cp ('t293');
6175                           }->{$node->[1]}) {                last INSCOPE;
6176                    !!!cp ('t293');              }
6177                    last INSCOPE;            } # INSCOPE
6178                  }            unless (defined $i) {
6179                } # INSCOPE              !!!cp ('t294');
6180                unless (defined $i) {              ## Ignore the token
6181                  !!!cp ('t294');              !!!nack ('t294.1');
6182                  ## Ignore the token              !!!next-token;
6183                  !!!next-token;              next B;
6184                  redo B;            }
               }  
6185                                
6186                ## As if </select>            ## As if </select>
6187                ## have an element in table scope            ## have an element in table scope
6188                undef $i;            undef $i;
6189                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6190                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6191                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
6192                    !!!cp ('t295');                !!!cp ('t295');
6193                    $i = $_;                $i = $_;
6194                    last INSCOPE;                last INSCOPE;
6195                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
6196  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
6197                    !!!cp ('t296');                !!!cp ('t296');
6198                    last INSCOPE;                last INSCOPE;
6199                  }              }
6200                } # INSCOPE            } # INSCOPE
6201                unless (defined $i) {            unless (defined $i) {
6202                  !!!cp ('t297');              !!!cp ('t297');
6203  ## TODO: The following error type is correct?  ## TODO: The following error type is correct?
6204                  !!!parse-error (type => 'unmatched end tag:select', token => $token);              !!!parse-error (type => 'unmatched end tag',
6205                  ## Ignore the </select> token                              text => 'select', token => $token);
6206                  !!!next-token; ## TODO: ok?              ## Ignore the </select> token
6207                  redo B;              !!!nack ('t297.1');
6208                }              !!!next-token; ## TODO: ok?
6209                next B;
6210              }
6211                                
6212                !!!cp ('t298');            !!!cp ('t298');
6213                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6214    
6215                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6216    
6217                ## reprocess            !!!ack-later;
6218                redo B;            ## reprocess
6219              next B;
6220          } else {          } else {
6221            !!!cp ('t299');            !!!cp ('t299');
6222            !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'in select:/',
6223                              text => $token->{tag_name}, token => $token);
6224            ## Ignore the token            ## Ignore the token
6225              !!!nack ('t299.3');
6226            !!!next-token;            !!!next-token;
6227            redo B;            next B;
6228          }          }
6229        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6230          unless ($self->{open_elements}->[-1]->[1] eq 'html' and          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6231                  @{$self->{open_elements}} == 1) { # redundant, maybe                  @{$self->{open_elements}} == 1) { # redundant, maybe
6232            !!!cp ('t299.1');            !!!cp ('t299.1');
6233            !!!parse-error (type => 'in body:#eof', token => $token);            !!!parse-error (type => 'in body:#eof', token => $token);
# Line 5300  sub _tree_construction_main ($) { Line 6252  sub _tree_construction_main ($) {
6252            unless (length $token->{data}) {            unless (length $token->{data}) {
6253              !!!cp ('t300');              !!!cp ('t300');
6254              !!!next-token;              !!!next-token;
6255              redo B;              next B;
6256            }            }
6257          }          }
6258                    
6259          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6260            !!!cp ('t301');            !!!cp ('t301');
6261            !!!parse-error (type => 'after html:#character', token => $token);            !!!parse-error (type => 'after html:#text', token => $token);
6262    
6263            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
6264          } else {          } else {
# Line 5314  sub _tree_construction_main ($) { Line 6266  sub _tree_construction_main ($) {
6266          }          }
6267                    
6268          ## "after body" insertion mode          ## "after body" insertion mode
6269          !!!parse-error (type => 'after body:#character', token => $token);          !!!parse-error (type => 'after body:#text', token => $token);
6270    
6271          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6272          ## reprocess          ## reprocess
6273          redo B;          next B;
6274        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6275          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6276            !!!cp ('t303');            !!!cp ('t303');
6277            !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'after html',
6278                              text => $token->{tag_name}, token => $token);
6279                        
6280            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
6281          } else {          } else {
# Line 5330  sub _tree_construction_main ($) { Line 6283  sub _tree_construction_main ($) {
6283          }          }
6284    
6285          ## "after body" insertion mode          ## "after body" insertion mode
6286          !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'after body',
6287                            text => $token->{tag_name}, token => $token);
6288    
6289          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6290            !!!ack-later;
6291          ## reprocess          ## reprocess
6292          redo B;          next B;
6293        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6294          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6295            !!!cp ('t305');            !!!cp ('t305');
6296            !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'after html:/',
6297                              text => $token->{tag_name}, token => $token);
6298                        
6299            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6300            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
# Line 5350  sub _tree_construction_main ($) { Line 6306  sub _tree_construction_main ($) {
6306          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
6307            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
6308              !!!cp ('t307');              !!!cp ('t307');
6309              !!!parse-error (type => 'unmatched end tag:html', token => $token);              !!!parse-error (type => 'unmatched end tag',
6310                                text => 'html', token => $token);
6311              ## Ignore the token              ## Ignore the token
6312              !!!next-token;              !!!next-token;
6313              redo B;              next B;
6314            } else {            } else {
6315              !!!cp ('t308');              !!!cp ('t308');
6316              $self->{insertion_mode} = AFTER_HTML_BODY_IM;              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6317              !!!next-token;              !!!next-token;
6318              redo B;              next B;
6319            }            }
6320          } else {          } else {
6321            !!!cp ('t309');            !!!cp ('t309');
6322            !!!parse-error (type => 'after body:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'after body:/',
6323                              text => $token->{tag_name}, token => $token);
6324    
6325            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
6326            ## reprocess            ## reprocess
6327            redo B;            next B;
6328          }          }
6329        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6330          !!!cp ('t309.2');          !!!cp ('t309.2');
# Line 5383  sub _tree_construction_main ($) { Line 6341  sub _tree_construction_main ($) {
6341            unless (length $token->{data}) {            unless (length $token->{data}) {
6342              !!!cp ('t310');              !!!cp ('t310');
6343              !!!next-token;              !!!next-token;
6344              redo B;              next B;
6345            }            }
6346          }          }
6347                    
6348          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
6349            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6350              !!!cp ('t311');              !!!cp ('t311');
6351              !!!parse-error (type => 'in frameset:#character', token => $token);              !!!parse-error (type => 'in frameset:#text', token => $token);
6352            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6353              !!!cp ('t312');              !!!cp ('t312');
6354              !!!parse-error (type => 'after frameset:#character', token => $token);              !!!parse-error (type => 'after frameset:#text', token => $token);
6355            } else { # "after html frameset"            } else { # "after after frameset"
6356              !!!cp ('t313');              !!!cp ('t313');
6357              !!!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);  
6358            }            }
6359                        
6360            ## Ignore the token.            ## Ignore the token.
# Line 5411  sub _tree_construction_main ($) { Line 6365  sub _tree_construction_main ($) {
6365              !!!cp ('t315');              !!!cp ('t315');
6366              !!!next-token;              !!!next-token;
6367            }            }
6368            redo B;            next B;
6369          }          }
6370                    
6371          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
6372        } 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');  
         }  
   
6373          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6374              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6375            !!!cp ('t318');            !!!cp ('t318');
6376            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6377              !!!nack ('t318.1');
6378            !!!next-token;            !!!next-token;
6379            redo B;            next B;
6380          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
6381                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
6382            !!!cp ('t319');            !!!cp ('t319');
6383            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6384            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6385              !!!ack ('t319.1');
6386            !!!next-token;            !!!next-token;
6387            redo B;            next B;
6388          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
6389            !!!cp ('t320');            !!!cp ('t320');
6390            ## NOTE: As if in body.            ## NOTE: As if in head.
6391            $parse_rcdata->(CDATA_CONTENT_MODEL);            $parse_rcdata->(CDATA_CONTENT_MODEL);
6392            redo B;            next B;
6393    
6394              ## NOTE: |<!DOCTYPE HTML><frameset></frameset></html><noframes></noframes>|
6395              ## has no parse error.
6396          } else {          } else {
6397            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6398              !!!cp ('t321');              !!!cp ('t321');
6399              !!!parse-error (type => 'in frameset:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'in frameset',
6400            } else {                              text => $token->{tag_name}, token => $token);
6401              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6402              !!!cp ('t322');              !!!cp ('t322');
6403              !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'after frameset',
6404                                text => $token->{tag_name}, token => $token);
6405              } else { # "after after frameset"
6406                !!!cp ('t322.2');
6407                !!!parse-error (type => 'after after frameset',
6408                                text => $token->{tag_name}, token => $token);
6409            }            }
6410            ## Ignore the token            ## Ignore the token
6411              !!!nack ('t322.1');
6412            !!!next-token;            !!!next-token;
6413            redo B;            next B;
6414          }          }
6415        } 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');  
         }  
   
6416          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6417              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6418            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6419                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
6420              !!!cp ('t325');              !!!cp ('t325');
6421              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag',
6422                                text => $token->{tag_name}, token => $token);
6423              ## Ignore the token              ## Ignore the token
6424              !!!next-token;              !!!next-token;
6425            } else {            } else {
# Line 5482  sub _tree_construction_main ($) { Line 6429  sub _tree_construction_main ($) {
6429            }            }
6430    
6431            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
6432                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6433              !!!cp ('t327');              !!!cp ('t327');
6434              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6435            } else {            } else {
6436              !!!cp ('t328');              !!!cp ('t328');
6437            }            }
6438            redo B;            next B;
6439          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
6440                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6441            !!!cp ('t329');            !!!cp ('t329');
6442            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6443            !!!next-token;            !!!next-token;
6444            redo B;            next B;
6445          } else {          } else {
6446            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6447              !!!cp ('t330');              !!!cp ('t330');
6448              !!!parse-error (type => 'in frameset:/'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'in frameset:/',
6449            } else {                              text => $token->{tag_name}, token => $token);
6450              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6451                !!!cp ('t330.1');
6452                !!!parse-error (type => 'after frameset:/',
6453                                text => $token->{tag_name}, token => $token);
6454              } else { # "after after html"
6455              !!!cp ('t331');              !!!cp ('t331');
6456              !!!parse-error (type => 'after frameset:/'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'after after frameset:/',
6457                                text => $token->{tag_name}, token => $token);
6458            }            }
6459            ## Ignore the token            ## Ignore the token
6460            !!!next-token;            !!!next-token;
6461            redo B;            next B;
6462          }          }
6463        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6464          unless ($self->{open_elements}->[-1]->[1] eq 'html' and          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6465                  @{$self->{open_elements}} == 1) { # redundant, maybe                  @{$self->{open_elements}} == 1) { # redundant, maybe
6466            !!!cp ('t331.1');            !!!cp ('t331.1');
6467            !!!parse-error (type => 'in body:#eof', token => $token);            !!!parse-error (type => 'in body:#eof', token => $token);
# Line 5533  sub _tree_construction_main ($) { Line 6486  sub _tree_construction_main ($) {
6486          !!!cp ('t332');          !!!cp ('t332');
6487          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6488          $script_start_tag->();          $script_start_tag->();
6489          redo B;          next B;
6490        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
6491          !!!cp ('t333');          !!!cp ('t333');
6492          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6493          $parse_rcdata->(CDATA_CONTENT_MODEL);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6494          redo B;          next B;
6495        } elsif ({        } elsif ({
6496                  base => 1, link => 1,                  base => 1, link => 1,
6497                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6498          !!!cp ('t334');          !!!cp ('t334');
6499          ## 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
6500          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6501          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6502            !!!ack ('t334.1');
6503          !!!next-token;          !!!next-token;
6504          redo B;          next B;
6505        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
6506          ## 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
6507          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6508          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.
6509    
6510          unless ($self->{confident}) {          unless ($self->{confident}) {
6511            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) {
6512              !!!cp ('t335');              !!!cp ('t335');
6513                ## NOTE: Whether the encoding is supported or not is handled
6514                ## in the {change_encoding} callback.
6515              $self->{change_encoding}              $self->{change_encoding}
6516                  ->($self, $token->{attributes}->{charset}->{value});                  ->($self, $token->{attributes}->{charset}->{value}, $token);
6517                            
6518              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6519                  ->set_user_data (manakai_has_reference =>                  ->set_user_data (manakai_has_reference =>
6520                                       $token->{attributes}->{charset}                                       $token->{attributes}->{charset}
6521                                           ->{has_reference});                                           ->{has_reference});
6522            } elsif ($token->{attributes}->{content}) {            } elsif ($token->{attributes}->{content}) {
             ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
6523              if ($token->{attributes}->{content}->{value}              if ($token->{attributes}->{content}->{value}
6524                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                  =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6525                      [\x09-\x0D\x20]*=                      [\x09-\x0D\x20]*=
6526                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6527                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
6528                !!!cp ('t336');                !!!cp ('t336');
6529                  ## NOTE: Whether the encoding is supported or not is handled
6530                  ## in the {change_encoding} callback.
6531                $self->{change_encoding}                $self->{change_encoding}
6532                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6533                $meta_el->[0]->get_attribute_node_ns (undef, 'content')                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6534                    ->set_user_data (manakai_has_reference =>                    ->set_user_data (manakai_has_reference =>
6535                                         $token->{attributes}->{content}                                         $token->{attributes}->{content}
# Line 5596  sub _tree_construction_main ($) { Line 6553  sub _tree_construction_main ($) {
6553            }            }
6554          }          }
6555    
6556            !!!ack ('t338.1');
6557          !!!next-token;          !!!next-token;
6558          redo B;          next B;
6559        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
6560          !!!cp ('t341');          !!!cp ('t341');
6561          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6562          $parse_rcdata->(RCDATA_CONTENT_MODEL);          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6563          redo B;          next B;
6564        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
6565          !!!parse-error (type => 'in body:body', token => $token);          !!!parse-error (type => 'in body', text => 'body', token => $token);
6566                                
6567          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
6568              $self->{open_elements}->[1]->[1] ne 'body') {              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6569            !!!cp ('t342');            !!!cp ('t342');
6570            ## Ignore the token            ## Ignore the token
6571          } else {          } else {
# Line 5621  sub _tree_construction_main ($) { Line 6579  sub _tree_construction_main ($) {
6579              }              }
6580            }            }
6581          }          }
6582            !!!nack ('t343.1');
6583          !!!next-token;          !!!next-token;
6584          redo B;          next B;
6585        } elsif ({        } elsif ({
6586                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6587                  div => 1, dl => 1, fieldset => 1,                  div => 1, dl => 1, fieldset => 1,
# Line 5637  sub _tree_construction_main ($) { Line 6596  sub _tree_construction_main ($) {
6596            !!!cp ('t350');            !!!cp ('t350');
6597            !!!parse-error (type => 'in form:form', token => $token);            !!!parse-error (type => 'in form:form', token => $token);
6598            ## Ignore the token            ## Ignore the token
6599              !!!nack ('t350.1');
6600            !!!next-token;            !!!next-token;
6601            redo B;            next B;
6602          }          }
6603    
6604          ## has a p element in scope          ## has a p element in scope
6605          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6606            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6607              !!!cp ('t344');              !!!cp ('t344');
6608              !!!back-token;              !!!back-token; # <form>
6609              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6610              redo B;                        line => $token->{line}, column => $token->{column}};
6611            } elsif ({              next B;
6612                      applet => 1, table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6613              !!!cp ('t345');              !!!cp ('t345');
6614              last INSCOPE;              last INSCOPE;
6615            }            }
6616          } # INSCOPE          } # INSCOPE
6617                        
6618          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6619          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6620              !!!nack ('t346.1');
6621            !!!next-token;            !!!next-token;
6622            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
6623              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
# Line 5675  sub _tree_construction_main ($) { Line 6634  sub _tree_construction_main ($) {
6634            !!!cp ('t347.1');            !!!cp ('t347.1');
6635            $self->{form_element} = $self->{open_elements}->[-1]->[0];            $self->{form_element} = $self->{open_elements}->[-1]->[0];
6636    
6637              !!!nack ('t347.2');
6638            !!!next-token;            !!!next-token;
6639          } elsif ($token->{tag_name} eq 'table') {          } elsif ($token->{tag_name} eq 'table') {
6640            !!!cp ('t382');            !!!cp ('t382');
# Line 5682  sub _tree_construction_main ($) { Line 6642  sub _tree_construction_main ($) {
6642                        
6643            $self->{insertion_mode} = IN_TABLE_IM;            $self->{insertion_mode} = IN_TABLE_IM;
6644    
6645              !!!nack ('t382.1');
6646            !!!next-token;            !!!next-token;
6647          } elsif ($token->{tag_name} eq 'hr') {          } elsif ($token->{tag_name} eq 'hr') {
6648            !!!cp ('t386');            !!!cp ('t386');
6649            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6650                    
6651              !!!nack ('t386.1');
6652            !!!next-token;            !!!next-token;
6653          } else {          } else {
6654            !!!cp ('t347');            !!!nack ('t347.1');
6655            !!!next-token;            !!!next-token;
6656          }          }
6657          redo B;          next B;
6658        } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {        } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
6659          ## has a p element in scope          ## has a p element in scope
6660          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6661            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6662              !!!cp ('t353');              !!!cp ('t353');
6663              !!!back-token;              !!!back-token; # <x>
6664              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6665              redo B;                        line => $token->{line}, column => $token->{column}};
6666            } elsif ({              next B;
6667                      applet => 1, table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6668              !!!cp ('t354');              !!!cp ('t354');
6669              last INSCOPE;              last INSCOPE;
6670            }            }
# Line 5718  sub _tree_construction_main ($) { Line 6678  sub _tree_construction_main ($) {
6678                            dd => {dt => 1, dd => 1}}->{$token->{tag_name}};                            dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6679          LI: {          LI: {
6680            ## Step 2            ## Step 2
6681            if ($li_or_dtdd->{$node->[1]}) {            if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6682              if ($i != -1) {              if ($i != -1) {
6683                !!!cp ('t355');                !!!cp ('t355');
6684                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'not closed',
6685                                $self->{open_elements}->[-1]->[1], token => $token);                                text => $self->{open_elements}->[-1]->[0]
6686                                      ->manakai_local_name,
6687                                  token => $token);
6688              } else {              } else {
6689                !!!cp ('t356');                !!!cp ('t356');
6690              }              }
# Line 5733  sub _tree_construction_main ($) { Line 6695  sub _tree_construction_main ($) {
6695            }            }
6696                        
6697            ## Step 3            ## Step 3
6698            if (not $formatting_category->{$node->[1]} and            if (not ($node->[1] & FORMATTING_EL) and
6699                #not $phrasing_category->{$node->[1]} and                #not $phrasing_category->{$node->[1]} and
6700                ($special_category->{$node->[1]} or                ($node->[1] & SPECIAL_EL or
6701                 $scoping_category->{$node->[1]}) and                 $node->[1] & SCOPING_EL) and
6702                $node->[1] ne 'address' and $node->[1] ne 'div') {                not ($node->[1] & ADDRESS_EL) and
6703                  not ($node->[1] & DIV_EL)) {
6704              !!!cp ('t358');              !!!cp ('t358');
6705              last LI;              last LI;
6706            }            }
# Line 5749  sub _tree_construction_main ($) { Line 6712  sub _tree_construction_main ($) {
6712            redo LI;            redo LI;
6713          } # LI          } # LI
6714                        
6715          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6716            !!!nack ('t359.1');
6717          !!!next-token;          !!!next-token;
6718          redo B;          next B;
6719        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
6720          ## has a p element in scope          ## has a p element in scope
6721          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6722            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6723              !!!cp ('t367');              !!!cp ('t367');
6724              !!!back-token;              !!!back-token; # <plaintext>
6725              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6726              redo B;                        line => $token->{line}, column => $token->{column}};
6727            } elsif ({              next B;
6728                      applet => 1, table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6729              !!!cp ('t368');              !!!cp ('t368');
6730              last INSCOPE;              last INSCOPE;
6731            }            }
6732          } # INSCOPE          } # INSCOPE
6733                        
6734          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6735                        
6736          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
6737                        
6738            !!!nack ('t368.1');
6739          !!!next-token;          !!!next-token;
6740          redo B;          next B;
6741        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
6742          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
6743            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
6744            if ($node->[1] eq 'a') {            if ($node->[1] & A_EL) {
6745              !!!cp ('t371');              !!!cp ('t371');
6746              !!!parse-error (type => 'in a:a', token => $token);              !!!parse-error (type => 'in a:a', token => $token);
6747                            
6748              !!!back-token;              !!!back-token; # <a>
6749              $token = {type => END_TAG_TOKEN, tag_name => 'a'};              $token = {type => END_TAG_TOKEN, tag_name => 'a',
6750                          line => $token->{line}, column => $token->{column}};
6751              $formatting_end_tag->($token);              $formatting_end_tag->($token);
6752                            
6753              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
# Line 5809  sub _tree_construction_main ($) { Line 6773  sub _tree_construction_main ($) {
6773                        
6774          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6775    
6776          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6777          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6778    
6779            !!!nack ('t374.1');
6780          !!!next-token;          !!!next-token;
6781          redo B;          next B;
6782        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
6783          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6784    
6785          ## has a |nobr| element in scope          ## has a |nobr| element in scope
6786          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6787            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6788            if ($node->[1] eq 'nobr') {            if ($node->[1] & NOBR_EL) {
6789              !!!cp ('t376');              !!!cp ('t376');
6790              !!!parse-error (type => 'in nobr:nobr', token => $token);              !!!parse-error (type => 'in nobr:nobr', token => $token);
6791              !!!back-token;              !!!back-token; # <nobr>
6792              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
6793              redo B;                        line => $token->{line}, column => $token->{column}};
6794            } elsif ({              next B;
6795                      applet => 1, table => 1, caption => 1, td => 1, th => 1,            } elsif ($node->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6796              !!!cp ('t377');              !!!cp ('t377');
6797              last INSCOPE;              last INSCOPE;
6798            }            }
6799          } # INSCOPE          } # INSCOPE
6800                    
6801          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6802          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6803                    
6804            !!!nack ('t377.1');
6805          !!!next-token;          !!!next-token;
6806          redo B;          next B;
6807        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
6808          ## has a button element in scope          ## has a button element in scope
6809          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6810            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6811            if ($node->[1] eq 'button') {            if ($node->[1] & BUTTON_EL) {
6812              !!!cp ('t378');              !!!cp ('t378');
6813              !!!parse-error (type => 'in button:button', token => $token);              !!!parse-error (type => 'in button:button', token => $token);
6814              !!!back-token;              !!!back-token; # <button>
6815              $token = {type => END_TAG_TOKEN, tag_name => 'button'};              $token = {type => END_TAG_TOKEN, tag_name => 'button',
6816              redo B;                        line => $token->{line}, column => $token->{column}};
6817            } elsif ({              next B;
6818                      applet => 1, table => 1, caption => 1, td => 1, th => 1,            } elsif ($node->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6819              !!!cp ('t379');              !!!cp ('t379');
6820              last INSCOPE;              last INSCOPE;
6821            }            }
# Line 5861  sub _tree_construction_main ($) { Line 6823  sub _tree_construction_main ($) {
6823                        
6824          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6825                        
6826          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6827    
6828          ## TODO: associate with $self->{form_element} if defined          ## TODO: associate with $self->{form_element} if defined
6829    
6830          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
6831    
6832            !!!nack ('t379.1');
6833          !!!next-token;          !!!next-token;
6834          redo B;          next B;
6835        } elsif ({        } elsif ({
6836                  xmp => 1,                  xmp => 1,
6837                  iframe => 1,                  iframe => 1,
6838                  noembed => 1,                  noembed => 1,
6839                  noframes => 1,                  noframes => 1, ## NOTE: This is an "as if in head" code clone.
6840                  noscript => 0, ## TODO: 1 if scripting is enabled                  noscript => 0, ## TODO: 1 if scripting is enabled
6841                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6842          if ($token->{tag_name} eq 'xmp') {          if ($token->{tag_name} eq 'xmp') {
# Line 5884  sub _tree_construction_main ($) { Line 6847  sub _tree_construction_main ($) {
6847          }          }
6848          ## NOTE: There is an "as if in body" code clone.          ## NOTE: There is an "as if in body" code clone.
6849          $parse_rcdata->(CDATA_CONTENT_MODEL);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6850          redo B;          next B;
6851        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
6852          !!!parse-error (type => 'isindex', token => $token);          !!!parse-error (type => 'isindex', token => $token);
6853                    
6854          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
6855            !!!cp ('t389');            !!!cp ('t389');
6856            ## Ignore the token            ## Ignore the token
6857              !!!nack ('t389'); ## NOTE: Not acknowledged.
6858            !!!next-token;            !!!next-token;
6859            redo B;            next B;
6860          } else {          } else {
6861              !!!ack ('t391.1');
6862    
6863            my $at = $token->{attributes};            my $at = $token->{attributes};
6864            my $form_attrs;            my $form_attrs;
6865            $form_attrs->{action} = $at->{action} if $at->{action};            $form_attrs->{action} = $at->{action} if $at->{action};
# Line 5903  sub _tree_construction_main ($) { Line 6869  sub _tree_construction_main ($) {
6869            delete $at->{prompt};            delete $at->{prompt};
6870            my @tokens = (            my @tokens = (
6871                          {type => START_TAG_TOKEN, tag_name => 'form',                          {type => START_TAG_TOKEN, tag_name => 'form',
6872                           attributes => $form_attrs},                           attributes => $form_attrs,
6873                          {type => START_TAG_TOKEN, tag_name => 'hr'},                           line => $token->{line}, column => $token->{column}},
6874                          {type => START_TAG_TOKEN, tag_name => 'p'},                          {type => START_TAG_TOKEN, tag_name => 'hr',
6875                          {type => START_TAG_TOKEN, tag_name => 'label'},                           line => $token->{line}, column => $token->{column}},
6876                            {type => START_TAG_TOKEN, tag_name => 'p',
6877                             line => $token->{line}, column => $token->{column}},
6878                            {type => START_TAG_TOKEN, tag_name => 'label',
6879                             line => $token->{line}, column => $token->{column}},
6880                         );                         );
6881            if ($prompt_attr) {            if ($prompt_attr) {
6882              !!!cp ('t390');              !!!cp ('t390');
6883              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
6884                               #line => $token->{line}, column => $token->{column},
6885                              };
6886            } else {            } else {
6887              !!!cp ('t391');              !!!cp ('t391');
6888              push @tokens, {type => CHARACTER_TOKEN,              push @tokens, {type => CHARACTER_TOKEN,
6889                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD                             data => 'This is a searchable index. Insert your search keywords here: ',
6890                               #line => $token->{line}, column => $token->{column},
6891                              }; # SHOULD
6892              ## TODO: make this configurable              ## TODO: make this configurable
6893            }            }
6894            push @tokens,            push @tokens,
6895                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
6896                             line => $token->{line}, column => $token->{column}},
6897                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
6898                          {type => END_TAG_TOKEN, tag_name => 'label'},                          {type => END_TAG_TOKEN, tag_name => 'label',
6899                          {type => END_TAG_TOKEN, tag_name => 'p'},                           line => $token->{line}, column => $token->{column}},
6900                          {type => START_TAG_TOKEN, tag_name => 'hr'},                          {type => END_TAG_TOKEN, tag_name => 'p',
6901                          {type => END_TAG_TOKEN, tag_name => 'form'};                           line => $token->{line}, column => $token->{column}},
6902            $token = shift @tokens;                          {type => START_TAG_TOKEN, tag_name => 'hr',
6903                             line => $token->{line}, column => $token->{column}},
6904                            {type => END_TAG_TOKEN, tag_name => 'form',
6905                             line => $token->{line}, column => $token->{column}};
6906            !!!back-token (@tokens);            !!!back-token (@tokens);
6907            redo B;            !!!next-token;
6908              next B;
6909          }          }
6910        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
6911          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
6912          my $el;          my $el;
6913          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
6914                    
6915          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
6916          $self->{content_model} = RCDATA_CONTENT_MODEL;          $self->{content_model} = RCDATA_CONTENT_MODEL;
# Line 5940  sub _tree_construction_main ($) { Line 6919  sub _tree_construction_main ($) {
6919          $insert->($el);          $insert->($el);
6920                    
6921          my $text = '';          my $text = '';
6922            !!!nack ('t392.1');
6923          !!!next-token;          !!!next-token;
6924          if ($token->{type} == CHARACTER_TOKEN) {          if ($token->{type} == CHARACTER_TOKEN) {
6925            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
# Line 5970  sub _tree_construction_main ($) { Line 6950  sub _tree_construction_main ($) {
6950            ## Ignore the token            ## Ignore the token
6951          } else {          } else {
6952            !!!cp ('t398');            !!!cp ('t398');
6953            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);            !!!parse-error (type => 'in RCDATA:#eof', token => $token);
6954          }          }
6955          !!!next-token;          !!!next-token;
6956            next B;
6957          } elsif ($token->{tag_name} eq 'rt' or
6958                   $token->{tag_name} eq 'rp') {
6959            ## has a |ruby| element in scope
6960            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6961              my $node = $self->{open_elements}->[$_];
6962              if ($node->[1] & RUBY_EL) {
6963                !!!cp ('t398.1');
6964                ## generate implied end tags
6965                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
6966                  !!!cp ('t398.2');
6967                  pop @{$self->{open_elements}};
6968                }
6969                unless ($self->{open_elements}->[-1]->[1] & RUBY_EL) {
6970                  !!!cp ('t398.3');
6971                  !!!parse-error (type => 'not closed',
6972                                  text => $self->{open_elements}->[-1]->[0]
6973                                      ->manakai_local_name,
6974                                  token => $token);
6975                  pop @{$self->{open_elements}}
6976                      while not $self->{open_elements}->[-1]->[1] & RUBY_EL;
6977                }
6978                last INSCOPE;
6979              } elsif ($node->[1] & SCOPING_EL) {
6980                !!!cp ('t398.4');
6981                last INSCOPE;
6982              }
6983            } # INSCOPE
6984    
6985            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6986    
6987            !!!nack ('t398.5');
6988            !!!next-token;
6989          redo B;          redo B;
6990          } elsif ($token->{tag_name} eq 'math' or
6991                   $token->{tag_name} eq 'svg') {
6992            $reconstruct_active_formatting_elements->($insert_to_current);
6993    
6994            ## "Adjust MathML attributes" ('math' only) - done in insert-element-f
6995    
6996            ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
6997    
6998            ## "adjust foreign attributes" - done in insert-element-f
6999            
7000            !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
7001            
7002            if ($self->{self_closing}) {
7003              pop @{$self->{open_elements}};
7004              !!!ack ('t398.1');
7005            } else {
7006              !!!cp ('t398.2');
7007              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
7008              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
7009              ## mode, "in body" (not "in foreign content") secondary insertion
7010              ## mode, maybe.
7011            }
7012    
7013            !!!next-token;
7014            next B;
7015        } elsif ({        } elsif ({
7016                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7017                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 5981  sub _tree_construction_main ($) { Line 7019  sub _tree_construction_main ($) {
7019                  thead => 1, tr => 1,                  thead => 1, tr => 1,
7020                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7021          !!!cp ('t401');          !!!cp ('t401');
7022          !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'in body',
7023                            text => $token->{tag_name}, token => $token);
7024          ## Ignore the token          ## Ignore the token
7025            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
7026          !!!next-token;          !!!next-token;
7027          redo B;          next B;
7028                    
7029          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
7030        } else {        } else {
# Line 5999  sub _tree_construction_main ($) { Line 7039  sub _tree_construction_main ($) {
7039          ## NOTE: There is an "as if <br>" code clone.          ## NOTE: There is an "as if <br>" code clone.
7040          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7041                    
7042          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7043    
7044          if ({          if ({
7045               applet => 1, marquee => 1, object => 1,               applet => 1, marquee => 1, object => 1,
7046              }->{$token->{tag_name}}) {              }->{$token->{tag_name}}) {
7047            !!!cp ('t380');            !!!cp ('t380');
7048            push @$active_formatting_elements, ['#marker', ''];            push @$active_formatting_elements, ['#marker', ''];
7049              !!!nack ('t380.1');
7050          } elsif ({          } elsif ({
7051                    b => 1, big => 1, em => 1, font => 1, i => 1,                    b => 1, big => 1, em => 1, font => 1, i => 1,
7052                    s => 1, small => 1, strile => 1,                    s => 1, small => 1, strile => 1,
# Line 6013  sub _tree_construction_main ($) { Line 7054  sub _tree_construction_main ($) {
7054                   }->{$token->{tag_name}}) {                   }->{$token->{tag_name}}) {
7055            !!!cp ('t375');            !!!cp ('t375');
7056            push @$active_formatting_elements, $self->{open_elements}->[-1];            push @$active_formatting_elements, $self->{open_elements}->[-1];
7057              !!!nack ('t375.1');
7058          } elsif ($token->{tag_name} eq 'input') {          } elsif ($token->{tag_name} eq 'input') {
7059            !!!cp ('t388');            !!!cp ('t388');
7060            ## TODO: associate with $self->{form_element} if defined            ## TODO: associate with $self->{form_element} if defined
7061            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
7062              !!!ack ('t388.2');
7063          } elsif ({          } elsif ({
7064                    area => 1, basefont => 1, bgsound => 1, br => 1,                    area => 1, basefont => 1, bgsound => 1, br => 1,
7065                    embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                    embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
# Line 6024  sub _tree_construction_main ($) { Line 7067  sub _tree_construction_main ($) {
7067                   }->{$token->{tag_name}}) {                   }->{$token->{tag_name}}) {
7068            !!!cp ('t388.1');            !!!cp ('t388.1');
7069            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
7070              !!!ack ('t388.3');
7071          } elsif ($token->{tag_name} eq 'select') {          } elsif ($token->{tag_name} eq 'select') {
7072            ## TODO: associate with $self->{form_element} if defined            ## TODO: associate with $self->{form_element} if defined
7073                    
# Line 6036  sub _tree_construction_main ($) { Line 7080  sub _tree_construction_main ($) {
7080              !!!cp ('t400.2');              !!!cp ('t400.2');
7081              $self->{insertion_mode} = IN_SELECT_IM;              $self->{insertion_mode} = IN_SELECT_IM;
7082            }            }
7083              !!!nack ('t400.3');
7084          } else {          } else {
7085            !!!cp ('t402');            !!!nack ('t402');
7086          }          }
7087                    
7088          !!!next-token;          !!!next-token;
7089          redo B;          next B;
7090        }        }
7091      } elsif ($token->{type} == END_TAG_TOKEN) {      } elsif ($token->{type} == END_TAG_TOKEN) {
7092        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
# Line 6049  sub _tree_construction_main ($) { Line 7094  sub _tree_construction_main ($) {
7094          my $i;          my $i;
7095          INSCOPE: {          INSCOPE: {
7096            for (reverse @{$self->{open_elements}}) {            for (reverse @{$self->{open_elements}}) {
7097              if ($_->[1] eq 'body') {              if ($_->[1] & BODY_EL) {
7098                !!!cp ('t405');                !!!cp ('t405');
7099                $i = $_;                $i = $_;
7100                last INSCOPE;                last INSCOPE;
7101              } elsif ({              } elsif ($_->[1] & SCOPING_EL) {
                       applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                       button => 1, marquee => 1, object => 1, html => 1,  
                      }->{$_->[1]}) {  
7102                !!!cp ('t405.1');                !!!cp ('t405.1');
7103                last;                last;
7104              }              }
7105            }            }
7106    
7107            !!!parse-error (type => 'start tag not allowed',            !!!parse-error (type => 'start tag not allowed',
7108                            value => $token->{tag_name}, token => $token);                            text => $token->{tag_name}, token => $token);
7109            ## NOTE: Ignore the token.            ## NOTE: Ignore the token.
7110            !!!next-token;            !!!next-token;
7111            redo B;            next B;
7112          } # INSCOPE          } # INSCOPE
7113    
7114          for (@{$self->{open_elements}}) {          for (@{$self->{open_elements}}) {
7115            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]}) {  
7116              !!!cp ('t403');              !!!cp ('t403');
7117              !!!parse-error (type => 'not closed:'.$_->[1], token => $token);              !!!parse-error (type => 'not closed',
7118                                text => $_->[0]->manakai_local_name,
7119                                token => $token);
7120              last;              last;
7121            } else {            } else {
7122              !!!cp ('t404');              !!!cp ('t404');
# Line 6085  sub _tree_construction_main ($) { Line 7125  sub _tree_construction_main ($) {
7125    
7126          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
7127          !!!next-token;          !!!next-token;
7128          redo B;          next B;
7129        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
7130          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
7131            ## up-to-date, though it has same effect as speced.
7132            if (@{$self->{open_elements}} > 1 and
7133                $self->{open_elements}->[1]->[1] & BODY_EL) {
7134            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
7135            if ($self->{open_elements}->[-1]->[1] ne 'body') {            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
7136              !!!cp ('t406');              !!!cp ('t406');
7137              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1], token => $token);              !!!parse-error (type => 'not closed',
7138                                text => $self->{open_elements}->[1]->[0]
7139                                    ->manakai_local_name,
7140                                token => $token);
7141            } else {            } else {
7142              !!!cp ('t407');              !!!cp ('t407');
7143            }            }
7144            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
7145            ## reprocess            ## reprocess
7146            redo B;            next B;
7147          } else {          } else {
7148            !!!cp ('t408');            !!!cp ('t408');
7149            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
7150                              text => $token->{tag_name}, token => $token);
7151            ## Ignore the token            ## Ignore the token
7152            !!!next-token;            !!!next-token;
7153            redo B;            next B;
7154          }          }
7155        } elsif ({        } elsif ({
7156                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
# Line 6116  sub _tree_construction_main ($) { Line 7163  sub _tree_construction_main ($) {
7163          my $i;          my $i;
7164          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7165            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7166            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7167              !!!cp ('t410');              !!!cp ('t410');
7168              $i = $_;              $i = $_;
7169              last INSCOPE;              last INSCOPE;
7170            } 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]}) {  
7171              !!!cp ('t411');              !!!cp ('t411');
7172              last INSCOPE;              last INSCOPE;
7173            }            }
# Line 6131  sub _tree_construction_main ($) { Line 7175  sub _tree_construction_main ($) {
7175    
7176          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
7177            !!!cp ('t413');            !!!cp ('t413');
7178            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
7179                              text => $token->{tag_name}, token => $token);
7180              ## NOTE: Ignore the token.
7181          } else {          } else {
7182            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7183            while ({            while ({
7184                      ## END_TAG_OPTIONAL_EL
7185                    dd => ($token->{tag_name} ne 'dd'),                    dd => ($token->{tag_name} ne 'dd'),
7186                    dt => ($token->{tag_name} ne 'dt'),                    dt => ($token->{tag_name} ne 'dt'),
7187                    li => ($token->{tag_name} ne 'li'),                    li => ($token->{tag_name} ne 'li'),
7188                    p => 1,                    p => 1,
7189                   }->{$self->{open_elements}->[-1]->[1]}) {                    rt => 1,
7190                      rp => 1,
7191                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
7192              !!!cp ('t409');              !!!cp ('t409');
7193              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
7194            }            }
7195    
7196            ## Step 2.            ## Step 2.
7197            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7198                      ne $token->{tag_name}) {
7199              !!!cp ('t412');              !!!cp ('t412');
7200              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);              !!!parse-error (type => 'not closed',
7201                                text => $self->{open_elements}->[-1]->[0]
7202                                    ->manakai_local_name,
7203                                token => $token);
7204            } else {            } else {
7205              !!!cp ('t414');              !!!cp ('t414');
7206            }            }
# Line 6162  sub _tree_construction_main ($) { Line 7215  sub _tree_construction_main ($) {
7215                }->{$token->{tag_name}};                }->{$token->{tag_name}};
7216          }          }
7217          !!!next-token;          !!!next-token;
7218          redo B;          next B;
7219        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
7220          undef $self->{form_element};          undef $self->{form_element};
7221    
# Line 6170  sub _tree_construction_main ($) { Line 7223  sub _tree_construction_main ($) {
7223          my $i;          my $i;
7224          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7225            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7226            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & FORM_EL) {
7227              !!!cp ('t418');              !!!cp ('t418');
7228              $i = $_;              $i = $_;
7229              last INSCOPE;              last INSCOPE;
7230            } 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]}) {  
7231              !!!cp ('t419');              !!!cp ('t419');
7232              last INSCOPE;              last INSCOPE;
7233            }            }
# Line 6185  sub _tree_construction_main ($) { Line 7235  sub _tree_construction_main ($) {
7235    
7236          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
7237            !!!cp ('t421');            !!!cp ('t421');
7238            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
7239                              text => $token->{tag_name}, token => $token);
7240              ## NOTE: Ignore the token.
7241          } else {          } else {
7242            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7243            while ({            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                   dd => 1, dt => 1, li => 1, p => 1,  
                  }->{$self->{open_elements}->[-1]->[1]}) {  
7244              !!!cp ('t417');              !!!cp ('t417');
7245              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
7246            }            }
7247                        
7248            ## Step 2.            ## Step 2.
7249            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7250                      ne $token->{tag_name}) {
7251              !!!cp ('t417.1');              !!!cp ('t417.1');
7252              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);              !!!parse-error (type => 'not closed',
7253                                text => $self->{open_elements}->[-1]->[0]
7254                                    ->manakai_local_name,
7255                                token => $token);
7256            } else {            } else {
7257              !!!cp ('t420');              !!!cp ('t420');
7258            }              }  
# Line 6208  sub _tree_construction_main ($) { Line 7262  sub _tree_construction_main ($) {
7262          }          }
7263    
7264          !!!next-token;          !!!next-token;
7265          redo B;          next B;
7266        } elsif ({        } elsif ({
7267                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
7268                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 6216  sub _tree_construction_main ($) { Line 7270  sub _tree_construction_main ($) {
7270          my $i;          my $i;
7271          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7272            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7273            if ({            if ($node->[1] & HEADING_EL) {
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
7274              !!!cp ('t423');              !!!cp ('t423');
7275              $i = $_;              $i = $_;
7276              last INSCOPE;              last INSCOPE;
7277            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7278              !!!cp ('t424');              !!!cp ('t424');
7279              last INSCOPE;              last INSCOPE;
7280            }            }
# Line 6233  sub _tree_construction_main ($) { Line 7282  sub _tree_construction_main ($) {
7282    
7283          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
7284            !!!cp ('t425.1');            !!!cp ('t425.1');
7285            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
7286                              text => $token->{tag_name}, token => $token);
7287              ## NOTE: Ignore the token.
7288          } else {          } else {
7289            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7290            while ({            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                   dd => 1, dt => 1, li => 1, p => 1,  
                  }->{$self->{open_elements}->[-1]->[1]}) {  
7291              !!!cp ('t422');              !!!cp ('t422');
7292              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
7293            }            }
7294                        
7295            ## Step 2.            ## Step 2.
7296            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7297                      ne $token->{tag_name}) {
7298              !!!cp ('t425');              !!!cp ('t425');
7299              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag',
7300                                text => $token->{tag_name}, token => $token);
7301            } else {            } else {
7302              !!!cp ('t426');              !!!cp ('t426');
7303            }            }
# Line 6256  sub _tree_construction_main ($) { Line 7307  sub _tree_construction_main ($) {
7307          }          }
7308                    
7309          !!!next-token;          !!!next-token;
7310          redo B;          next B;
7311        } elsif ($token->{tag_name} eq 'p') {        } elsif ($token->{tag_name} eq 'p') {
7312          ## has an element in scope          ## has an element in scope
7313          my $i;          my $i;
7314          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7315            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7316            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & P_EL) {
7317              !!!cp ('t410.1');              !!!cp ('t410.1');
7318              $i = $_;              $i = $_;
7319              last INSCOPE;              last INSCOPE;
7320            } 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]}) {  
7321              !!!cp ('t411.1');              !!!cp ('t411.1');
7322              last INSCOPE;              last INSCOPE;
7323            }            }
7324          } # INSCOPE          } # INSCOPE
7325    
7326          if (defined $i) {          if (defined $i) {
7327            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7328                      ne $token->{tag_name}) {
7329              !!!cp ('t412.1');              !!!cp ('t412.1');
7330              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);              !!!parse-error (type => 'not closed',
7331                                text => $self->{open_elements}->[-1]->[0]
7332                                    ->manakai_local_name,
7333                                token => $token);
7334            } else {            } else {
7335              !!!cp ('t414.1');              !!!cp ('t414.1');
7336            }            }
# Line 6286  sub _tree_construction_main ($) { Line 7338  sub _tree_construction_main ($) {
7338            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
7339          } else {          } else {
7340            !!!cp ('t413.1');            !!!cp ('t413.1');
7341            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
7342                              text => $token->{tag_name}, token => $token);
7343    
7344            !!!cp ('t415.1');            !!!cp ('t415.1');
7345            ## As if <p>, then reprocess the current token            ## As if <p>, then reprocess the current token
7346            my $el;            my $el;
7347            !!!create-element ($el, 'p');            !!!create-element ($el, $HTML_NS, 'p',, $token);
7348            $insert->($el);            $insert->($el);
7349            ## NOTE: Not inserted into |$self->{open_elements}|.            ## NOTE: Not inserted into |$self->{open_elements}|.
7350          }          }
7351    
7352          !!!next-token;          !!!next-token;
7353          redo B;          next B;
7354        } elsif ({        } elsif ({
7355                  a => 1,                  a => 1,
7356                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
# Line 6306  sub _tree_construction_main ($) { Line 7359  sub _tree_construction_main ($) {
7359                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7360          !!!cp ('t427');          !!!cp ('t427');
7361          $formatting_end_tag->($token);          $formatting_end_tag->($token);
7362          redo B;          next B;
7363        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
7364          !!!cp ('t428');          !!!cp ('t428');
7365          !!!parse-error (type => 'unmatched end tag:br', token => $token);          !!!parse-error (type => 'unmatched end tag',
7366                            text => 'br', token => $token);
7367    
7368          ## As if <br>          ## As if <br>
7369          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7370                    
7371          my $el;          my $el;
7372          !!!create-element ($el, 'br');          !!!create-element ($el, $HTML_NS, 'br',, $token);
7373          $insert->($el);          $insert->($el);
7374                    
7375          ## Ignore the token.          ## Ignore the token.
7376          !!!next-token;          !!!next-token;
7377          redo B;          next B;
7378        } elsif ({        } elsif ({
7379                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7380                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 6334  sub _tree_construction_main ($) { Line 7388  sub _tree_construction_main ($) {
7388                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
7389                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7390          !!!cp ('t429');          !!!cp ('t429');
7391          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'unmatched end tag',
7392                            text => $token->{tag_name}, token => $token);
7393          ## Ignore the token          ## Ignore the token
7394          !!!next-token;          !!!next-token;
7395          redo B;          next B;
7396                    
7397          ## ISSUE: Issue on HTML5 new elements in spec          ## ISSUE: Issue on HTML5 new elements in spec
7398                    
# Line 6348  sub _tree_construction_main ($) { Line 7403  sub _tree_construction_main ($) {
7403    
7404          ## Step 2          ## Step 2
7405          S2: {          S2: {
7406            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7407              ## Step 1              ## Step 1
7408              ## generate implied end tags              ## generate implied end tags
7409              while ({              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                     dd => 1, dt => 1, li => 1, p => 1,  
                    }->{$self->{open_elements}->[-1]->[1]}) {  
7410                !!!cp ('t430');                !!!cp ('t430');
7411                ## ISSUE: Can this case be reached?                ## NOTE: |<ruby><rt></ruby>|.
7412                  ## ISSUE: <ruby><rt></rt> will also take this code path,
7413                  ## which seems wrong.
7414                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
7415                  $node_i++;
7416              }              }
7417                    
7418              ## Step 2              ## Step 2
7419              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7420                        ne $token->{tag_name}) {
7421                !!!cp ('t431');                !!!cp ('t431');
7422                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
7423                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                !!!parse-error (type => 'not closed',
7424                                  text => $self->{open_elements}->[-1]->[0]
7425                                      ->manakai_local_name,
7426                                  token => $token);
7427              } else {              } else {
7428                !!!cp ('t432');                !!!cp ('t432');
7429              }              }
7430                            
7431              ## Step 3              ## Step 3
7432              splice @{$self->{open_elements}}, $node_i;              splice @{$self->{open_elements}}, $node_i if $node_i < 0;
7433    
7434              !!!next-token;              !!!next-token;
7435              last S2;              last S2;
7436            } else {            } else {
7437              ## Step 3              ## Step 3
7438              if (not $formatting_category->{$node->[1]} and              if (not ($node->[1] & FORMATTING_EL) and
7439                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
7440                  ($special_category->{$node->[1]} or                  ($node->[1] & SPECIAL_EL or
7441                   $scoping_category->{$node->[1]})) {                   $node->[1] & SCOPING_EL)) {
7442                !!!cp ('t433');                !!!cp ('t433');
7443                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag',
7444                                  text => $token->{tag_name}, token => $token);
7445                ## Ignore the token                ## Ignore the token
7446                !!!next-token;                !!!next-token;
7447                last S2;                last S2;
# Line 6396  sub _tree_construction_main ($) { Line 7457  sub _tree_construction_main ($) {
7457            ## Step 5;            ## Step 5;
7458            redo S2;            redo S2;
7459          } # S2          } # S2
7460          redo B;          next B;
7461        }        }
7462      }      }
7463      redo B;      next B;
7464      } continue { # B
7465        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7466          ## NOTE: The code below is executed in cases where it does not have
7467          ## to be, but it it is harmless even in those cases.
7468          ## has an element in scope
7469          INSCOPE: {
7470            for (reverse 0..$#{$self->{open_elements}}) {
7471              my $node = $self->{open_elements}->[$_];
7472              if ($node->[1] & FOREIGN_EL) {
7473                last INSCOPE;
7474              } elsif ($node->[1] & SCOPING_EL) {
7475                last;
7476              }
7477            }
7478            
7479            ## NOTE: No foreign element in scope.
7480            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7481          } # INSCOPE
7482        }
7483    } # B    } # B
7484    
7485    ## Stop parsing # MUST    ## Stop parsing # MUST
# Line 6407  sub _tree_construction_main ($) { Line 7487  sub _tree_construction_main ($) {
7487    ## TODO: script stuffs    ## TODO: script stuffs
7488  } # _tree_construct_main  } # _tree_construct_main
7489    
7490  sub set_inner_html ($$$) {  sub set_inner_html ($$$;$) {
7491    my $class = shift;    my $class = shift;
7492    my $node = shift;    my $node = shift;
7493    my $s = \$_[0];    my $s = \$_[0];
7494    my $onerror = $_[1];    my $onerror = $_[1];
7495      my $get_wrapper = $_[2] || sub ($) { return $_[0] };
7496    
7497    ## ISSUE: Should {confident} be true?    ## ISSUE: Should {confident} be true?
7498    
# Line 6430  sub set_inner_html ($$$) { Line 7511  sub set_inner_html ($$$) {
7511      }      }
7512    
7513      ## Step 3, 4, 5 # MUST      ## Step 3, 4, 5 # MUST
7514      $class->parse_string ($$s => $node, $onerror);      $class->parse_char_string ($$s => $node, $onerror, $get_wrapper);
7515    } elsif ($nt == 1) {    } elsif ($nt == 1) {
7516      ## TODO: If non-html element      ## TODO: If non-html element
7517    
7518      ## NOTE: Most of this code is copied from |parse_string|      ## NOTE: Most of this code is copied from |parse_string|
7519    
7520    ## TODO: Support for $get_wrapper
7521    
7522      ## Step 1 # MUST      ## Step 1 # MUST
7523      my $this_doc = $node->owner_document;      my $this_doc = $node->owner_document;
7524      my $doc = $this_doc->implementation->create_document;      my $doc = $this_doc->implementation->create_document;
# Line 6445  sub set_inner_html ($$$) { Line 7528  sub set_inner_html ($$$) {
7528    
7529      ## Step 8 # MUST      ## Step 8 # MUST
7530      my $i = 0;      my $i = 0;
7531      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7532      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7533      $p->{set_next_char} = sub {      $p->{set_next_char} = sub {
7534        my $self = shift;        my $self = shift;
7535    
# Line 6455  sub set_inner_html ($$$) { Line 7538  sub set_inner_html ($$$) {
7538    
7539        $self->{next_char} = -1 and return if $i >= length $$s;        $self->{next_char} = -1 and return if $i >= length $$s;
7540        $self->{next_char} = ord substr $$s, $i++, 1;        $self->{next_char} = ord substr $$s, $i++, 1;
7541        $column++;  
7542          ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7543          $p->{column}++;
7544    
7545        if ($self->{next_char} == 0x000A) { # LF        if ($self->{next_char} == 0x000A) { # LF
7546          $line++;          $p->{line}++;
7547          $column = 0;          $p->{column} = 0;
7548          !!!cp ('i1');          !!!cp ('i1');
7549        } elsif ($self->{next_char} == 0x000D) { # CR        } elsif ($self->{next_char} == 0x000D) { # CR
7550          $i++ if substr ($$s, $i, 1) eq "\x0A";          $i++ if substr ($$s, $i, 1) eq "\x0A";
7551          $self->{next_char} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
7552          $line++;          $p->{line}++;
7553          $column = 0;          $p->{column} = 0;
7554          !!!cp ('i2');          !!!cp ('i2');
7555        } elsif ($self->{next_char} > 0x10FFFF) {        } elsif ($self->{next_char} > 0x10FFFF) {
7556          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
# Line 6474  sub set_inner_html ($$$) { Line 7559  sub set_inner_html ($$$) {
7559          !!!cp ('i4');          !!!cp ('i4');
7560          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
7561          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7562          } elsif ($self->{next_char} <= 0x0008 or
7563                   (0x000E <= $self->{next_char} and
7564                    $self->{next_char} <= 0x001F) or
7565                   (0x007F <= $self->{next_char} and
7566                    $self->{next_char} <= 0x009F) or
7567                   (0xD800 <= $self->{next_char} and
7568                    $self->{next_char} <= 0xDFFF) or
7569                   (0xFDD0 <= $self->{next_char} and
7570                    $self->{next_char} <= 0xFDDF) or
7571                   {
7572                    0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
7573                    0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
7574                    0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
7575                    0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
7576                    0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
7577                    0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
7578                    0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
7579                    0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
7580                    0x10FFFE => 1, 0x10FFFF => 1,
7581                   }->{$self->{next_char}}) {
7582            !!!cp ('i4.1');
7583            if ($self->{next_char} < 0x10000) {
7584              !!!parse-error (type => 'control char',
7585                              text => (sprintf 'U+%04X', $self->{next_char}));
7586            } else {
7587              !!!parse-error (type => 'control char',
7588                              text => (sprintf 'U-%08X', $self->{next_char}));
7589            }
7590        }        }
7591      };      };
7592      $p->{prev_char} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];
# Line 6481  sub set_inner_html ($$$) { Line 7594  sub set_inner_html ($$$) {
7594            
7595      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7596        my (%opt) = @_;        my (%opt) = @_;
7597        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7598          my $column = $opt{column};
7599          if (defined $opt{token} and defined $opt{token}->{line}) {
7600            $line = $opt{token}->{line};
7601            $column = $opt{token}->{column};
7602          }
7603          warn "Parse error ($opt{type}) at line $line column $column\n";
7604      };      };
7605      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7606        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7607      };      };
7608            
7609      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
# Line 6508  sub set_inner_html ($$$) { Line 7627  sub set_inner_html ($$$) {
7627          unless defined $p->{content_model};          unless defined $p->{content_model};
7628          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
7629    
7630      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7631          ## TODO: Foreign element OK?
7632    
7633      ## Step 3      ## Step 3
7634      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
# Line 6518  sub set_inner_html ($$$) { Line 7638  sub set_inner_html ($$$) {
7638      $doc->append_child ($root);      $doc->append_child ($root);
7639    
7640      ## Step 5 # MUST      ## Step 5 # MUST
7641      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7642    
7643      undef $p->{head_element};      undef $p->{head_element};
7644    
# Line 6564  sub set_inner_html ($$$) { Line 7684  sub set_inner_html ($$$) {
7684      ## ISSUE: mutation events?      ## ISSUE: mutation events?
7685    
7686      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
7687    
7688        delete $p->{parse_error}; # delete loop
7689    } else {    } else {
7690      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";
7691    }    }

Legend:
Removed from v.1.113  
changed lines
  Added in v.1.163

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24