/[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.104 by wakaba, Sun Mar 9 08:47:33 2008 UTC revision 1.143 by wakaba, Sat May 24 10:48:57 2008 UTC
# Line 8  use Error qw(:try); Line 8  use Error qw(:try);
8  ## doc.write ('');  ## doc.write ('');
9  ## alert (doc.compatMode);  ## alert (doc.compatMode);
10    
 ## TODO: Control charcters and noncharacters are not allowed (HTML5 revision 1263)  
11  ## TODO: 1252 parse error (revision 1264)  ## TODO: 1252 parse error (revision 1264)
12  ## TODO: 8859-11 = 874 (revision 1271)  ## TODO: 8859-11 = 874 (revision 1271)
13    
14  my $permitted_slash_tag_name = {  require IO::Handle;
15    base => 1,  
16    link => 1,  my $HTML_NS = q<http://www.w3.org/1999/xhtml>;
17    meta => 1,  my $MML_NS = q<http://www.w3.org/1998/Math/MathML>;
18    hr => 1,  my $SVG_NS = q<http://www.w3.org/2000/svg>;
19    br => 1,  my $XLINK_NS = q<http://www.w3.org/1999/xlink>;
20    img => 1,  my $XML_NS = q<http://www.w3.org/XML/1998/namespace>;
21    embed => 1,  my $XMLNS_NS = q<http://www.w3.org/2000/xmlns/>;
22    param => 1,  
23    area => 1,  sub A_EL () { 0b1 }
24    col => 1,  sub ADDRESS_EL () { 0b10 }
25    input => 1,  sub BODY_EL () { 0b100 }
26    sub BUTTON_EL () { 0b1000 }
27    sub CAPTION_EL () { 0b10000 }
28    sub DD_EL () { 0b100000 }
29    sub DIV_EL () { 0b1000000 }
30    sub DT_EL () { 0b10000000 }
31    sub FORM_EL () { 0b100000000 }
32    sub FORMATTING_EL () { 0b1000000000 }
33    sub FRAMESET_EL () { 0b10000000000 }
34    sub HEADING_EL () { 0b100000000000 }
35    sub HTML_EL () { 0b1000000000000 }
36    sub LI_EL () { 0b10000000000000 }
37    sub NOBR_EL () { 0b100000000000000 }
38    sub OPTION_EL () { 0b1000000000000000 }
39    sub OPTGROUP_EL () { 0b10000000000000000 }
40    sub P_EL () { 0b100000000000000000 }
41    sub SELECT_EL () { 0b1000000000000000000 }
42    sub TABLE_EL () { 0b10000000000000000000 }
43    sub TABLE_CELL_EL () { 0b100000000000000000000 }
44    sub TABLE_ROW_EL () { 0b1000000000000000000000 }
45    sub TABLE_ROW_GROUP_EL () { 0b10000000000000000000000 }
46    sub MISC_SCOPING_EL () { 0b100000000000000000000000 }
47    sub MISC_SPECIAL_EL () { 0b1000000000000000000000000 }
48    sub FOREIGN_EL () { 0b10000000000000000000000000 }
49    sub FOREIGN_FLOW_CONTENT_EL () { 0b100000000000000000000000000 }
50    sub MML_AXML_EL () { 0b1000000000000000000000000000 }
51    
52    sub TABLE_ROWS_EL () {
53      TABLE_EL |
54      TABLE_ROW_EL |
55      TABLE_ROW_GROUP_EL
56    }
57    
58    sub END_TAG_OPTIONAL_EL () {
59      DD_EL |
60      DT_EL |
61      LI_EL |
62      P_EL
63    }
64    
65    sub ALL_END_TAG_OPTIONAL_EL () {
66      END_TAG_OPTIONAL_EL |
67      BODY_EL |
68      HTML_EL |
69      TABLE_CELL_EL |
70      TABLE_ROW_EL |
71      TABLE_ROW_GROUP_EL
72    }
73    
74    sub SCOPING_EL () {
75      BUTTON_EL |
76      CAPTION_EL |
77      HTML_EL |
78      TABLE_EL |
79      TABLE_CELL_EL |
80      MISC_SCOPING_EL
81    }
82    
83    sub TABLE_SCOPING_EL () {
84      HTML_EL |
85      TABLE_EL
86    }
87    
88    sub TABLE_ROWS_SCOPING_EL () {
89      HTML_EL |
90      TABLE_ROW_GROUP_EL
91    }
92    
93    sub TABLE_ROW_SCOPING_EL () {
94      HTML_EL |
95      TABLE_ROW_EL
96    }
97    
98    sub SPECIAL_EL () {
99      ADDRESS_EL |
100      BODY_EL |
101      DIV_EL |
102      END_TAG_OPTIONAL_EL |
103      FORM_EL |
104      FRAMESET_EL |
105      HEADING_EL |
106      OPTION_EL |
107      OPTGROUP_EL |
108      SELECT_EL |
109      TABLE_ROW_EL |
110      TABLE_ROW_GROUP_EL |
111      MISC_SPECIAL_EL
112    }
113    
114    my $el_category = {
115      a => A_EL | FORMATTING_EL,
116      address => ADDRESS_EL,
117      applet => MISC_SCOPING_EL,
118      area => MISC_SPECIAL_EL,
119      b => FORMATTING_EL,
120      base => MISC_SPECIAL_EL,
121      basefont => MISC_SPECIAL_EL,
122      bgsound => MISC_SPECIAL_EL,
123      big => FORMATTING_EL,
124      blockquote => MISC_SPECIAL_EL,
125      body => BODY_EL,
126      br => MISC_SPECIAL_EL,
127      button => BUTTON_EL,
128      caption => CAPTION_EL,
129      center => MISC_SPECIAL_EL,
130      col => MISC_SPECIAL_EL,
131      colgroup => MISC_SPECIAL_EL,
132      dd => DD_EL,
133      dir => MISC_SPECIAL_EL,
134      div => DIV_EL,
135      dl => MISC_SPECIAL_EL,
136      dt => DT_EL,
137      em => FORMATTING_EL,
138      embed => MISC_SPECIAL_EL,
139      fieldset => MISC_SPECIAL_EL,
140      font => FORMATTING_EL,
141      form => FORM_EL,
142      frame => MISC_SPECIAL_EL,
143      frameset => FRAMESET_EL,
144      h1 => HEADING_EL,
145      h2 => HEADING_EL,
146      h3 => HEADING_EL,
147      h4 => HEADING_EL,
148      h5 => HEADING_EL,
149      h6 => HEADING_EL,
150      head => MISC_SPECIAL_EL,
151      hr => MISC_SPECIAL_EL,
152      html => HTML_EL,
153      i => FORMATTING_EL,
154      iframe => MISC_SPECIAL_EL,
155      img => MISC_SPECIAL_EL,
156      input => MISC_SPECIAL_EL,
157      isindex => MISC_SPECIAL_EL,
158      li => LI_EL,
159      link => MISC_SPECIAL_EL,
160      listing => MISC_SPECIAL_EL,
161      marquee => MISC_SCOPING_EL,
162      menu => MISC_SPECIAL_EL,
163      meta => MISC_SPECIAL_EL,
164      nobr => NOBR_EL | FORMATTING_EL,
165      noembed => MISC_SPECIAL_EL,
166      noframes => MISC_SPECIAL_EL,
167      noscript => MISC_SPECIAL_EL,
168      object => MISC_SCOPING_EL,
169      ol => MISC_SPECIAL_EL,
170      optgroup => OPTGROUP_EL,
171      option => OPTION_EL,
172      p => P_EL,
173      param => MISC_SPECIAL_EL,
174      plaintext => MISC_SPECIAL_EL,
175      pre => MISC_SPECIAL_EL,
176      s => FORMATTING_EL,
177      script => MISC_SPECIAL_EL,
178      select => SELECT_EL,
179      small => FORMATTING_EL,
180      spacer => MISC_SPECIAL_EL,
181      strike => FORMATTING_EL,
182      strong => FORMATTING_EL,
183      style => MISC_SPECIAL_EL,
184      table => TABLE_EL,
185      tbody => TABLE_ROW_GROUP_EL,
186      td => TABLE_CELL_EL,
187      textarea => MISC_SPECIAL_EL,
188      tfoot => TABLE_ROW_GROUP_EL,
189      th => TABLE_CELL_EL,
190      thead => TABLE_ROW_GROUP_EL,
191      title => MISC_SPECIAL_EL,
192      tr => TABLE_ROW_EL,
193      tt => FORMATTING_EL,
194      u => FORMATTING_EL,
195      ul => MISC_SPECIAL_EL,
196      wbr => MISC_SPECIAL_EL,
197    };
198    
199    my $el_category_f = {
200      $MML_NS => {
201        'annotation-xml' => MML_AXML_EL,
202        mi => FOREIGN_FLOW_CONTENT_EL,
203        mo => FOREIGN_FLOW_CONTENT_EL,
204        mn => FOREIGN_FLOW_CONTENT_EL,
205        ms => FOREIGN_FLOW_CONTENT_EL,
206        mtext => FOREIGN_FLOW_CONTENT_EL,
207      },
208      $SVG_NS => {
209        foreignObject => FOREIGN_FLOW_CONTENT_EL,
210        desc => FOREIGN_FLOW_CONTENT_EL,
211        title => FOREIGN_FLOW_CONTENT_EL,
212      },
213      ## NOTE: In addition, FOREIGN_EL is set to non-HTML elements.
214    };
215    
216    my $svg_attr_name = {
217      attributetype => 'attributeType',
218      basefrequency => 'baseFrequency',
219      baseprofile => 'baseProfile',
220      calcmode => 'calcMode',
221      clippathunits => 'clipPathUnits',
222      contentscripttype => 'contentScriptType',
223      contentstyletype => 'contentStyleType',
224      diffuseconstant => 'diffuseConstant',
225      edgemode => 'edgeMode',
226      externalresourcesrequired => 'externalResourcesRequired',
227      fecolormatrix => 'feColorMatrix',
228      fecomposite => 'feComposite',
229      fegaussianblur => 'feGaussianBlur',
230      femorphology => 'feMorphology',
231      fetile => 'feTile',
232      filterres => 'filterRes',
233      filterunits => 'filterUnits',
234      glyphref => 'glyphRef',
235      gradienttransform => 'gradientTransform',
236      gradientunits => 'gradientUnits',
237      kernelmatrix => 'kernelMatrix',
238      kernelunitlength => 'kernelUnitLength',
239      keypoints => 'keyPoints',
240      keysplines => 'keySplines',
241      keytimes => 'keyTimes',
242      lengthadjust => 'lengthAdjust',
243      limitingconeangle => 'limitingConeAngle',
244      markerheight => 'markerHeight',
245      markerunits => 'markerUnits',
246      markerwidth => 'markerWidth',
247      maskcontentunits => 'maskContentUnits',
248      maskunits => 'maskUnits',
249      numoctaves => 'numOctaves',
250      pathlength => 'pathLength',
251      patterncontentunits => 'patternContentUnits',
252      patterntransform => 'patternTransform',
253      patternunits => 'patternUnits',
254      pointsatx => 'pointsAtX',
255      pointsaty => 'pointsAtY',
256      pointsatz => 'pointsAtZ',
257      preservealpha => 'preserveAlpha',
258      preserveaspectratio => 'preserveAspectRatio',
259      primitiveunits => 'primitiveUnits',
260      refx => 'refX',
261      refy => 'refY',
262      repeatcount => 'repeatCount',
263      repeatdur => 'repeatDur',
264      requiredextensions => 'requiredExtensions',
265      specularconstant => 'specularConstant',
266      specularexponent => 'specularExponent',
267      spreadmethod => 'spreadMethod',
268      startoffset => 'startOffset',
269      stddeviation => 'stdDeviation',
270      stitchtiles => 'stitchTiles',
271      surfacescale => 'surfaceScale',
272      systemlanguage => 'systemLanguage',
273      tablevalues => 'tableValues',
274      targetx => 'targetX',
275      targety => 'targetY',
276      textlength => 'textLength',
277      viewbox => 'viewBox',
278      viewtarget => 'viewTarget',
279      xchannelselector => 'xChannelSelector',
280      ychannelselector => 'yChannelSelector',
281      zoomandpan => 'zoomAndPan',
282    };
283    
284    my $foreign_attr_xname = {
285      'xlink:actuate' => [$XLINK_NS, ['xlink', 'actuate']],
286      'xlink:arcrole' => [$XLINK_NS, ['xlink', 'arcrole']],
287      'xlink:href' => [$XLINK_NS, ['xlink', 'href']],
288      'xlink:role' => [$XLINK_NS, ['xlink', 'role']],
289      'xlink:show' => [$XLINK_NS, ['xlink', 'show']],
290      'xlink:title' => [$XLINK_NS, ['xlink', 'title']],
291      'xlink:type' => [$XLINK_NS, ['xlink', 'type']],
292      'xml:base' => [$XML_NS, ['xml', 'base']],
293      'xml:lang' => [$XML_NS, ['xml', 'lang']],
294      'xml:space' => [$XML_NS, ['xml', 'space']],
295      'xmlns' => [$XMLNS_NS, [undef, 'xmlns']],
296      'xmlns:xlink' => [$XMLNS_NS, ['xmlns', 'xlink']],
297  };  };
298    
299    ## ISSUE: xmlns:xlink="non-xlink-ns" is not an error.
300    
301  my $c1_entity_char = {  my $c1_entity_char = {
302    0x80 => 0x20AC,    0x80 => 0x20AC,
303    0x81 => 0xFFFD,    0x81 => 0xFFFD,
# Line 61  my $c1_entity_char = { Line 333  my $c1_entity_char = {
333    0x9F => 0x0178,    0x9F => 0x0178,
334  }; # $c1_entity_char  }; # $c1_entity_char
335    
 my $special_category = {  
   address => 1, area => 1, base => 1, basefont => 1, bgsound => 1,  
   blockquote => 1, body => 1, br => 1, center => 1, col => 1, colgroup => 1,  
   dd => 1, dir => 1, div => 1, dl => 1, dt => 1, embed => 1, fieldset => 1,  
   form => 1, frame => 1, frameset => 1, h1 => 1, h2 => 1, h3 => 1,  
   h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, iframe => 1, image => 1,  
   img => 1, input => 1, isindex => 1, li => 1, link => 1, listing => 1,  
   menu => 1, meta => 1, noembed => 1, noframes => 1, noscript => 1,  
   ol => 1, optgroup => 1, option => 1, p => 1, param => 1, plaintext => 1,  
   pre => 1, script => 1, select => 1, spacer => 1, style => 1, tbody => 1,  
   textarea => 1, tfoot => 1, thead => 1, title => 1, tr => 1, ul => 1, wbr => 1,  
 };  
 my $scoping_category = {  
   applet => 1, button => 1, caption => 1, html => 1, marquee => 1, object => 1,  
   table => 1, td => 1, th => 1,  
 };  
 my $formatting_category = {  
   a => 1, b => 1, big => 1, em => 1, font => 1, i => 1, nobr => 1,  
   s => 1, small => 1, strile => 1, strong => 1, tt => 1, u => 1,  
 };  
 # $phrasing_category: all other elements  
   
336  sub parse_byte_string ($$$$;$) {  sub parse_byte_string ($$$$;$) {
337      my $self = shift;
338      my $charset_name = shift;
339      open my $input, '<', ref $_[0] ? $_[0] : \($_[0]);
340      return $self->parse_byte_stream ($charset_name, $input, @_[1..$#_]);
341    } # parse_byte_string
342    
343    sub parse_byte_stream ($$$$;$) {
344    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
345    my $charset = shift;    my $charset_name = shift;
346    my $bytes_s = ref $_[0] ? $_[0] : \($_[0]);    my $byte_stream = $_[0];
   my $s;  
     
   if (defined $charset) {  
     require Encode; ## TODO: decode(utf8) don't delete BOM  
     $s = \ (Encode::decode ($charset, $$bytes_s));  
     $self->{input_encoding} = lc $charset; ## TODO: normalize name  
     $self->{confident} = 1;  
   } else {  
     ## TODO: Implement HTML5 detection algorithm  
     require Whatpm::Charset::UniversalCharDet;  
     $charset = Whatpm::Charset::UniversalCharDet->detect_byte_string  
         (substr ($$bytes_s, 0, 1024));  
     $charset ||= 'windows-1252';  
     $s = \ (Encode::decode ($charset, $$bytes_s));  
     $self->{input_encoding} = $charset;  
     $self->{confident} = 0;  
   }  
347    
348    $self->{change_encoding} = sub {    my $onerror = $_[2] || sub {
349      my $self = shift;      my (%opt) = @_;
350      my $charset = lc shift;      warn "Parse error ($opt{type})\n";
351      ## TODO: if $charset is supported    };
352      ## TODO: normalize charset name    $self->{parse_error} = $onerror; # updated later by parse_char_string
353    
354      ## "Change the encoding" algorithm:    ## HTML5 encoding sniffing algorithm
355      require Message::Charset::Info;
356      ## Step 1        my $charset;
357      if ($charset eq 'utf-16') { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?    my $buffer;
358        $charset = 'utf-8';    my ($char_stream, $e_status);
359    
360      SNIFFING: {
361    
362        ## Step 1
363        if (defined $charset_name) {
364          $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
365    
366          ## ISSUE: Unsupported encoding is not ignored according to the spec.
367          ($char_stream, $e_status) = $charset->get_decode_handle
368              ($byte_stream, allow_error_reporting => 1,
369               allow_fallback => 1);
370          if ($char_stream) {
371            $self->{confident} = 1;
372            last SNIFFING;
373          } else {
374            ## TODO: unsupported error
375          }
376      }      }
377    
378      ## Step 2      ## Step 2
379      if (defined $self->{input_encoding} and      my $byte_buffer = '';
380          $self->{input_encoding} eq $charset) {      for (1..1024) {
381          my $char = $byte_stream->getc;
382          last unless defined $char;
383          $byte_buffer .= $char;
384        } ## TODO: timeout
385    
386        ## Step 3
387        if ($byte_buffer =~ /^\xFE\xFF/) {
388          $charset = Message::Charset::Info->get_by_iana_name ('utf-16be');
389          ($char_stream, $e_status) = $charset->get_decode_handle
390              ($byte_stream, allow_error_reporting => 1,
391               allow_fallback => 1, byte_buffer => \$byte_buffer);
392        $self->{confident} = 1;        $self->{confident} = 1;
393        return;        last SNIFFING;
394        } elsif ($byte_buffer =~ /^\xFF\xFE/) {
395          $charset = Message::Charset::Info->get_by_iana_name ('utf-16le');
396          ($char_stream, $e_status) = $charset->get_decode_handle
397              ($byte_stream, allow_error_reporting => 1,
398               allow_fallback => 1, byte_buffer => \$byte_buffer);
399          $self->{confident} = 1;
400          last SNIFFING;
401        } elsif ($byte_buffer =~ /^\xEF\xBB\xBF/) {
402          $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
403          ($char_stream, $e_status) = $charset->get_decode_handle
404              ($byte_stream, allow_error_reporting => 1,
405               allow_fallback => 1, byte_buffer => \$byte_buffer);
406          $self->{confident} = 1;
407          last SNIFFING;
408      }      }
409    
410      !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.      ## Step 4
411          ':'.$charset, level => 'w');      ## TODO: <meta charset>
412    
413      ## Step 3      ## Step 5
414      # if (can) {      ## TODO: from history
       ## change the encoding on the fly.  
       #$self->{confident} = 1;  
       #return;  
     # }  
415    
416      ## Step 4      ## Step 6
417      throw Whatpm::HTML::RestartParser (charset => $charset);      require Whatpm::Charset::UniversalCharDet;
418        $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
419            ($byte_buffer);
420        if (defined $charset_name) {
421          $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
422    
423          ## ISSUE: Unsupported encoding is not ignored according to the spec.
424          require Whatpm::Charset::DecodeHandle;
425          $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
426              ($byte_stream);
427          ($char_stream, $e_status) = $charset->get_decode_handle
428              ($buffer, allow_error_reporting => 1,
429               allow_fallback => 1, byte_buffer => \$byte_buffer);
430          if ($char_stream) {
431            $buffer->{buffer} = $byte_buffer;
432            !!!parse-error (type => 'sniffing:chardet', ## TODO: type name
433                            value => $charset_name,
434                            level => $self->{info_level},
435                            line => 1, column => 1);
436            $self->{confident} = 0;
437            last SNIFFING;
438          }
439        }
440    
441        ## Step 7: default
442        ## TODO: Make this configurable.
443        $charset = Message::Charset::Info->get_by_iana_name ('windows-1252');
444            ## NOTE: We choose |windows-1252| here, since |utf-8| should be
445            ## detectable in the step 6.
446        require Whatpm::Charset::DecodeHandle;
447        $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
448            ($byte_stream);
449        ($char_stream, $e_status)
450            = $charset->get_decode_handle ($buffer,
451                                           allow_error_reporting => 1,
452                                           allow_fallback => 1,
453                                           byte_buffer => \$byte_buffer);
454        $buffer->{buffer} = $byte_buffer;
455        !!!parse-error (type => 'sniffing:default', ## TODO: type name
456                        value => 'windows-1252',
457                        level => $self->{info_level},
458                        line => 1, column => 1);
459        $self->{confident} = 0;
460      } # SNIFFING
461    
462      $self->{input_encoding} = $charset->get_iana_name;
463      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
464        !!!parse-error (type => 'chardecode:fallback', ## TODO: type name
465                        value => $self->{input_encoding},
466                        level => $self->{unsupported_level},
467                        line => 1, column => 1);
468      } elsif (not ($e_status &
469                    Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
470        !!!parse-error (type => 'chardecode:no error', ## TODO: type name
471                        value => $self->{input_encoding},
472                        level => $self->{unsupported_level},
473                        line => 1, column => 1);
474      }
475    
476      $self->{change_encoding} = sub {
477        my $self = shift;
478        $charset_name = shift;
479        my $token = shift;
480    
481        $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
482        ($char_stream, $e_status) = $charset->get_decode_handle
483            ($byte_stream, allow_error_reporting => 1, allow_fallback => 1,
484             byte_buffer => \ $buffer->{buffer});
485        
486        if ($char_stream) { # if supported
487          ## "Change the encoding" algorithm:
488    
489          ## Step 1    
490          if ($charset->{iana_names}->{'utf-16'}) { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?
491            $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
492            ($char_stream, $e_status) = $charset->get_decode_handle
493                ($byte_stream,
494                 byte_buffer => \ $buffer->{buffer});
495          }
496          $charset_name = $charset->get_iana_name;
497          
498          ## Step 2
499          if (defined $self->{input_encoding} and
500              $self->{input_encoding} eq $charset_name) {
501            !!!parse-error (type => 'charset label:matching', ## TODO: type
502                            value => $charset_name,
503                            level => $self->{info_level});
504            $self->{confident} = 1;
505            return;
506          }
507    
508          !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.
509              ':'.$charset_name, level => 'w', token => $token);
510          
511          ## Step 3
512          # if (can) {
513            ## change the encoding on the fly.
514            #$self->{confident} = 1;
515            #return;
516          # }
517          
518          ## Step 4
519          throw Whatpm::HTML::RestartParser ();
520        }
521    }; # $self->{change_encoding}    }; # $self->{change_encoding}
522    
523      my $char_onerror = sub {
524        my (undef, $type, %opt) = @_;
525        !!!parse-error (%opt, type => $type,
526                        line => $self->{line}, column => $self->{column} + 1);
527        if ($opt{octets}) {
528          ${$opt{octets}} = "\x{FFFD}"; # relacement character
529        }
530      };
531      $char_stream->onerror ($char_onerror);
532    
533    my @args = @_; shift @args; # $s    my @args = @_; shift @args; # $s
534    my $return;    my $return;
535    try {    try {
536      $return = $self->parse_char_string ($s, @args);        $return = $self->parse_char_stream ($char_stream, @args);  
537    } catch Whatpm::HTML::RestartParser with {    } catch Whatpm::HTML::RestartParser with {
538      my $charset = shift->{charset};      ## NOTE: Invoked after {change_encoding}.
539      $s = \ (Encode::decode ($charset, $$bytes_s));      
540      $self->{input_encoding} = $charset; ## TODO: normalize      $self->{input_encoding} = $charset->get_iana_name;
541        if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
542          !!!parse-error (type => 'chardecode:fallback', ## TODO: type name
543                          value => $self->{input_encoding},
544                          level => $self->{unsupported_level},
545                          line => 1, column => 1);
546        } elsif (not ($e_status &
547                      Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
548          !!!parse-error (type => 'chardecode:no error', ## TODO: type name
549                          value => $self->{input_encoding},
550                          level => $self->{unsupported_level},
551                          line => 1, column => 1);
552        }
553      $self->{confident} = 1;      $self->{confident} = 1;
554      $return = $self->parse_char_string ($s, @args);      $char_stream->onerror ($char_onerror);
555        $return = $self->parse_char_stream ($char_stream, @args);
556    };    };
557    return $return;    return $return;
558  } # parse_byte_string  } # parse_byte_stream
559    
560  ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM  ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
561  ## and the HTML layer MUST ignore it.  However, we does strip BOM in  ## and the HTML layer MUST ignore it.  However, we does strip BOM in
# Line 162  sub parse_byte_string ($$$$;$) { Line 566  sub parse_byte_string ($$$$;$) {
566  ## such as |parse_byte_string| in this module, must ensure that it does  ## such as |parse_byte_string| in this module, must ensure that it does
567  ## strip the BOM and never strip any ZWNBSP.  ## strip the BOM and never strip any ZWNBSP.
568    
569  *parse_char_string = \&parse_string;  sub parse_char_string ($$$;$) {
570      my $self = shift;
571      require utf8;
572      my $s = ref $_[0] ? $_[0] : \($_[0]);
573      open my $input, '<' . (utf8::is_utf8 ($$s) ? ':utf8' : ''), $s;
574      return $self->parse_char_stream ($input, @_[1..$#_]);
575    } # parse_char_string
576    *parse_string = \&parse_char_string;
577    
578  sub parse_string ($$$;$) {  sub parse_char_stream ($$$;$) {
579    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
580    my $s = ref $_[0] ? $_[0] : \($_[0]);    my $input = $_[0];
581    $self->{document} = $_[1];    $self->{document} = $_[1];
582    @{$self->{document}->child_nodes} = ();    @{$self->{document}->child_nodes} = ();
583    
# Line 177  sub parse_string ($$$;$) { Line 588  sub parse_string ($$$;$) {
588        if defined $self->{input_encoding};        if defined $self->{input_encoding};
589    
590    my $i = 0;    my $i = 0;
591    my $line = 1;    $self->{line_prev} = $self->{line} = 1;
592    my $column = 0;    $self->{column_prev} = $self->{column} = 0;
593    $self->{set_next_char} = sub {    $self->{set_next_char} = sub {
594      my $self = shift;      my $self = shift;
595    
596      pop @{$self->{prev_char}};      pop @{$self->{prev_char}};
597      unshift @{$self->{prev_char}}, $self->{next_char};      unshift @{$self->{prev_char}}, $self->{next_char};
598    
599      $self->{next_char} = -1 and return if $i >= length $$s;      my $char;
600      $self->{next_char} = ord substr $$s, $i++, 1;      if (defined $self->{next_next_char}) {
601      $column++;        $char = $self->{next_next_char};
602          delete $self->{next_next_char};
603        } else {
604          $char = $input->getc;
605        }
606        $self->{next_char} = -1 and return unless defined $char;
607        $self->{next_char} = ord $char;
608    
609        ($self->{line_prev}, $self->{column_prev})
610            = ($self->{line}, $self->{column});
611        $self->{column}++;
612            
613      if ($self->{next_char} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
614        $line++;        !!!cp ('j1');
615        $column = 0;        $self->{line}++;
616          $self->{column} = 0;
617      } elsif ($self->{next_char} == 0x000D) { # CR      } elsif ($self->{next_char} == 0x000D) { # CR
618        $i++ if substr ($$s, $i, 1) eq "\x0A";        !!!cp ('j2');
619          my $next = $input->getc;
620          if (defined $next and $next ne "\x0A") {
621            $self->{next_next_char} = $next;
622          }
623        $self->{next_char} = 0x000A; # LF # MUST        $self->{next_char} = 0x000A; # LF # MUST
624        $line++;        $self->{line}++;
625        $column = 0;        $self->{column} = 0;
626      } elsif ($self->{next_char} > 0x10FFFF) {      } elsif ($self->{next_char} > 0x10FFFF) {
627          !!!cp ('j3');
628        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
629      } elsif ($self->{next_char} == 0x0000) { # NULL      } elsif ($self->{next_char} == 0x0000) { # NULL
630          !!!cp ('j4');
631        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
632        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
633        } elsif ($self->{next_char} <= 0x0008 or
634                 (0x000E <= $self->{next_char} and $self->{next_char} <= 0x001F) or
635                 (0x007F <= $self->{next_char} and $self->{next_char} <= 0x009F) or
636                 (0xD800 <= $self->{next_char} and $self->{next_char} <= 0xDFFF) or
637                 (0xFDD0 <= $self->{next_char} and $self->{next_char} <= 0xFDDF) or
638                 {
639                  0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
640                  0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
641                  0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
642                  0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
643                  0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
644                  0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
645                  0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
646                  0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
647                  0x10FFFE => 1, 0x10FFFF => 1,
648                 }->{$self->{next_char}}) {
649          !!!cp ('j5');
650          !!!parse-error (type => 'control char', level => $self->{must_level});
651    ## TODO: error type documentation
652      }      }
653    };    };
654    $self->{prev_char} = [-1, -1, -1];    $self->{prev_char} = [-1, -1, -1];
# Line 209  sub parse_string ($$$;$) { Line 656  sub parse_string ($$$;$) {
656    
657    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
658      my (%opt) = @_;      my (%opt) = @_;
659      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
660        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
661        warn "Parse error ($opt{type}) at line $line column $column\n";
662    };    };
663    $self->{parse_error} = sub {    $self->{parse_error} = sub {
664      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
665    };    };
666    
667    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
# Line 220  sub parse_string ($$$;$) { Line 669  sub parse_string ($$$;$) {
669    $self->_construct_tree;    $self->_construct_tree;
670    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
671    
672      delete $self->{parse_error}; # remove loop
673    
674    return $self->{document};    return $self->{document};
675  } # parse_string  } # parse_char_stream
676    
677  sub new ($) {  sub new ($) {
678    my $class = shift;    my $class = shift;
679    my $self = bless {}, $class;    my $self = bless {
680        must_level => 'm',
681        should_level => 's',
682        good_level => 'w',
683        warn_level => 'w',
684        info_level => 'i',
685        unsupported_level => 'u',
686      }, $class;
687    $self->{set_next_char} = sub {    $self->{set_next_char} = sub {
688      $self->{next_char} = -1;      $self->{next_char} = -1;
689    };    };
# Line 287  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO Line 745  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO
745  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
746  sub BOGUS_DOCTYPE_STATE () { 32 }  sub BOGUS_DOCTYPE_STATE () { 32 }
747  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
748    sub SELF_CLOSING_START_TAG_STATE () { 34 }
749    sub CDATA_BLOCK_STATE () { 35 }
750    
751  sub DOCTYPE_TOKEN () { 1 }  sub DOCTYPE_TOKEN () { 1 }
752  sub COMMENT_TOKEN () { 2 }  sub COMMENT_TOKEN () { 2 }
# Line 304  sub ROW_IMS ()        { 0b10000000 } Line 764  sub ROW_IMS ()        { 0b10000000 }
764  sub BODY_AFTER_IMS () { 0b100000000 }  sub BODY_AFTER_IMS () { 0b100000000 }
765  sub FRAME_IMS ()      { 0b1000000000 }  sub FRAME_IMS ()      { 0b1000000000 }
766  sub SELECT_IMS ()     { 0b10000000000 }  sub SELECT_IMS ()     { 0b10000000000 }
767    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
768        ## NOTE: "in foreign content" insertion mode is special; it is combined
769        ## with the secondary insertion mode.  In this parser, they are stored
770        ## together in the bit-or'ed form.
771    
772  ## NOTE: "initial" and "before html" insertion modes have no constants.  ## NOTE: "initial" and "before html" insertion modes have no constants.
773    
# Line 340  sub _initialize_tokenizer ($) { Line 804  sub _initialize_tokenizer ($) {
804    undef $self->{current_attribute};    undef $self->{current_attribute};
805    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
806    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
807      delete $self->{self_closing};
808    $self->{char} = [];    $self->{char} = [];
809    # $self->{next_char}    # $self->{next_char}
810    !!!next-input-character;    !!!next-input-character;
# Line 360  sub _initialize_tokenizer ($) { Line 825  sub _initialize_tokenizer ($) {
825  ##        ->{value}  ##        ->{value}
826  ##        ->{has_reference} == 1 or 0  ##        ->{has_reference} == 1 or 0
827  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
828    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
829    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
830    ##     while the token is pushed back to the stack.
831    
832    ## ISSUE: "When a DOCTYPE token is created, its
833    ## <i>self-closing flag</i> must be unset (its other state is that it
834    ## be set), and its attributes list must be empty.": Wrong subject?
835    
836  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
837    
# Line 386  sub _initialize_tokenizer ($) { Line 858  sub _initialize_tokenizer ($) {
858    
859  sub _get_next_token ($) {  sub _get_next_token ($) {
860    my $self = shift;    my $self = shift;
861    
862      if ($self->{self_closing}) {
863        !!!parse-error (type => 'nestc', token => $self->{current_token});
864        ## NOTE: The |self_closing| flag is only set by start tag token.
865        ## In addition, when a start tag token is emitted, it is always set to
866        ## |current_token|.
867        delete $self->{self_closing};
868      }
869    
870    if (@{$self->{token}}) {    if (@{$self->{token}}) {
871        $self->{self_closing} = $self->{token}->[0]->{self_closing};
872      return shift @{$self->{token}};      return shift @{$self->{token}};
873    }    }
874    
# Line 449  sub _get_next_token ($) { Line 931  sub _get_next_token ($) {
931          #          #
932        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
933          !!!cp (11);          !!!cp (11);
934          !!!emit ({type => END_OF_FILE_TOKEN});          !!!emit ({type => END_OF_FILE_TOKEN,
935                      line => $self->{line}, column => $self->{column}});
936          last A; ## TODO: ok?          last A; ## TODO: ok?
937        } else {        } else {
938          !!!cp (12);          !!!cp (12);
939        }        }
940        # Anything else        # Anything else
941        my $token = {type => CHARACTER_TOKEN,        my $token = {type => CHARACTER_TOKEN,
942                     data => chr $self->{next_char}};                     data => chr $self->{next_char},
943                       line => $self->{line}, column => $self->{column},
944                      };
945        ## Stay in the data state        ## Stay in the data state
946        !!!next-input-character;        !!!next-input-character;
947    
# Line 465  sub _get_next_token ($) { Line 950  sub _get_next_token ($) {
950        redo A;        redo A;
951      } elsif ($self->{state} == ENTITY_DATA_STATE) {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
952        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
953    
954          my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
955                
956        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);
957    
# Line 473  sub _get_next_token ($) { Line 960  sub _get_next_token ($) {
960    
961        unless (defined $token) {        unless (defined $token) {
962          !!!cp (13);          !!!cp (13);
963          !!!emit ({type => CHARACTER_TOKEN, data => '&'});          !!!emit ({type => CHARACTER_TOKEN, data => '&',
964                      line => $l, column => $c,
965                     });
966        } else {        } else {
967          !!!cp (14);          !!!cp (14);
968          !!!emit ($token);          !!!emit ($token);
# Line 492  sub _get_next_token ($) { Line 981  sub _get_next_token ($) {
981            ## reconsume            ## reconsume
982            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
983    
984            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
985                        line => $self->{line_prev},
986                        column => $self->{column_prev},
987                       });
988    
989            redo A;            redo A;
990          }          }
# Line 512  sub _get_next_token ($) { Line 1004  sub _get_next_token ($) {
1004            !!!cp (19);            !!!cp (19);
1005            $self->{current_token}            $self->{current_token}
1006              = {type => START_TAG_TOKEN,              = {type => START_TAG_TOKEN,
1007                 tag_name => chr ($self->{next_char} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020),
1008                   line => $self->{line_prev},
1009                   column => $self->{column_prev}};
1010            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
1011            !!!next-input-character;            !!!next-input-character;
1012            redo A;            redo A;
# Line 520  sub _get_next_token ($) { Line 1014  sub _get_next_token ($) {
1014                   $self->{next_char} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
1015            !!!cp (20);            !!!cp (20);
1016            $self->{current_token} = {type => START_TAG_TOKEN,            $self->{current_token} = {type => START_TAG_TOKEN,
1017                              tag_name => chr ($self->{next_char})};                                      tag_name => chr ($self->{next_char}),
1018                                        line => $self->{line_prev},
1019                                        column => $self->{column_prev}};
1020            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
1021            !!!next-input-character;            !!!next-input-character;
1022            redo A;            redo A;
1023          } elsif ($self->{next_char} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
1024            !!!cp (21);            !!!cp (21);
1025            !!!parse-error (type => 'empty start tag');            !!!parse-error (type => 'empty start tag',
1026                              line => $self->{line_prev},
1027                              column => $self->{column_prev});
1028            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1029            !!!next-input-character;            !!!next-input-character;
1030    
1031            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
1032                        line => $self->{line_prev},
1033                        column => $self->{column_prev},
1034                       });
1035    
1036            redo A;            redo A;
1037          } elsif ($self->{next_char} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
1038            !!!cp (22);            !!!cp (22);
1039            !!!parse-error (type => 'pio');            !!!parse-error (type => 'pio',
1040                              line => $self->{line_prev},
1041                              column => $self->{column_prev});
1042            $self->{state} = BOGUS_COMMENT_STATE;            $self->{state} = BOGUS_COMMENT_STATE;
1043              $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1044                                        line => $self->{line_prev},
1045                                        column => $self->{column_prev},
1046                                       };
1047            ## $self->{next_char} is intentionally left as is            ## $self->{next_char} is intentionally left as is
1048            redo A;            redo A;
1049          } else {          } else {
1050            !!!cp (23);            !!!cp (23);
1051            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago',
1052                              line => $self->{line_prev},
1053                              column => $self->{column_prev});
1054            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1055            ## reconsume            ## reconsume
1056    
1057            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1058                        line => $self->{line_prev},
1059                        column => $self->{column_prev},
1060                       });
1061    
1062            redo A;            redo A;
1063          }          }
# Line 553  sub _get_next_token ($) { Line 1065  sub _get_next_token ($) {
1065          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
1066        }        }
1067      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
1068          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
1069        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1070          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
1071    
1072            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
1073            my @next_char;            my @next_char;
1074            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {
# Line 571  sub _get_next_token ($) { Line 1085  sub _get_next_token ($) {
1085                !!!back-next-input-character (@next_char);                !!!back-next-input-character (@next_char);
1086                $self->{state} = DATA_STATE;                $self->{state} = DATA_STATE;
1087    
1088                !!!emit ({type => CHARACTER_TOKEN, data => '</'});                !!!emit ({type => CHARACTER_TOKEN, data => '</',
1089                            line => $l, column => $c,
1090                           });
1091        
1092                redo A;                redo A;
1093              }              }
# Line 590  sub _get_next_token ($) { Line 1106  sub _get_next_token ($) {
1106              $self->{next_char} = shift @next_char; # reconsume              $self->{next_char} = shift @next_char; # reconsume
1107              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
1108              $self->{state} = DATA_STATE;              $self->{state} = DATA_STATE;
1109              !!!emit ({type => CHARACTER_TOKEN, data => '</'});              !!!emit ({type => CHARACTER_TOKEN, data => '</',
1110                          line => $l, column => $c,
1111                         });
1112              redo A;              redo A;
1113            } else {            } else {
1114              !!!cp (27);              !!!cp (27);
# Line 603  sub _get_next_token ($) { Line 1121  sub _get_next_token ($) {
1121            !!!cp (28);            !!!cp (28);
1122            # next-input-character is already done            # next-input-character is already done
1123            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1124            !!!emit ({type => CHARACTER_TOKEN, data => '</'});            !!!emit ({type => CHARACTER_TOKEN, data => '</',
1125                        line => $l, column => $c,
1126                       });
1127            redo A;            redo A;
1128          }          }
1129        }        }
# Line 611  sub _get_next_token ($) { Line 1131  sub _get_next_token ($) {
1131        if (0x0041 <= $self->{next_char} and        if (0x0041 <= $self->{next_char} and
1132            $self->{next_char} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
1133          !!!cp (29);          !!!cp (29);
1134          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token}
1135                            tag_name => chr ($self->{next_char} + 0x0020)};              = {type => END_TAG_TOKEN,
1136                   tag_name => chr ($self->{next_char} + 0x0020),
1137                   line => $l, column => $c};
1138          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1139          !!!next-input-character;          !!!next-input-character;
1140          redo A;          redo A;
# Line 620  sub _get_next_token ($) { Line 1142  sub _get_next_token ($) {
1142                 $self->{next_char} <= 0x007A) { # a..z                 $self->{next_char} <= 0x007A) { # a..z
1143          !!!cp (30);          !!!cp (30);
1144          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token} = {type => END_TAG_TOKEN,
1145                            tag_name => chr ($self->{next_char})};                                    tag_name => chr ($self->{next_char}),
1146                                      line => $l, column => $c};
1147          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1148          !!!next-input-character;          !!!next-input-character;
1149          redo A;          redo A;
1150        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1151          !!!cp (31);          !!!cp (31);
1152          !!!parse-error (type => 'empty end tag');          !!!parse-error (type => 'empty end tag',
1153                            line => $self->{line_prev}, ## "<" in "</>"
1154                            column => $self->{column_prev} - 1);
1155          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1156          !!!next-input-character;          !!!next-input-character;
1157          redo A;          redo A;
# Line 636  sub _get_next_token ($) { Line 1161  sub _get_next_token ($) {
1161          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1162          # reconsume          # reconsume
1163    
1164          !!!emit ({type => CHARACTER_TOKEN, data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
1165                      line => $l, column => $c,
1166                     });
1167    
1168          redo A;          redo A;
1169        } else {        } else {
1170          !!!cp (33);          !!!cp (33);
1171          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
1172          $self->{state} = BOGUS_COMMENT_STATE;          $self->{state} = BOGUS_COMMENT_STATE;
1173            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1174                                      line => $self->{line_prev}, # "<" of "</"
1175                                      column => $self->{column_prev} - 1,
1176                                     };
1177          ## $self->{next_char} is intentionally left as is          ## $self->{next_char} is intentionally left as is
1178          redo A;          redo A;
1179        }        }
# Line 659  sub _get_next_token ($) { Line 1190  sub _get_next_token ($) {
1190        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1191          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1192            !!!cp (35);            !!!cp (35);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1193            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1194          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1195            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 692  sub _get_next_token ($) { Line 1221  sub _get_next_token ($) {
1221          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1222          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1223            !!!cp (39);            !!!cp (39);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1224            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1225          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1226            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 714  sub _get_next_token ($) { Line 1241  sub _get_next_token ($) {
1241    
1242          redo A;          redo A;
1243        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1244            !!!cp (42);
1245            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1246          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_char} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           !!!cp (42);  
           #  
         } else {  
           !!!cp (43);  
           !!!parse-error (type => 'nestc');  
         }  
         $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;  
         # next-input-character is already done  
1247          redo A;          redo A;
1248        } else {        } else {
1249          !!!cp (44);          !!!cp (44);
# Line 749  sub _get_next_token ($) { Line 1266  sub _get_next_token ($) {
1266        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1267          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1268            !!!cp (46);            !!!cp (46);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1269            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1270          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1271            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 772  sub _get_next_token ($) { Line 1287  sub _get_next_token ($) {
1287        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{next_char} and
1288                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1289          !!!cp (49);          !!!cp (49);
1290          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{current_attribute}
1291                                value => ''};              = {name => chr ($self->{next_char} + 0x0020),
1292                   value => '',
1293                   line => $self->{line}, column => $self->{column}};
1294          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1295          !!!next-input-character;          !!!next-input-character;
1296          redo A;          redo A;
1297        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1298            !!!cp (50);
1299            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1300          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_char} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           !!!cp (50);  
           #  
         } else {  
           !!!cp (51);  
           !!!parse-error (type => 'nestc');  
         }  
         ## Stay in the state  
         # next-input-character is already done  
1301          redo A;          redo A;
1302        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1303          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1304          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1305            !!!cp (52);            !!!cp (52);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1306            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1307          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1308            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 827  sub _get_next_token ($) { Line 1332  sub _get_next_token ($) {
1332          } else {          } else {
1333            !!!cp (56);            !!!cp (56);
1334          }          }
1335          $self->{current_attribute} = {name => chr ($self->{next_char}),          $self->{current_attribute}
1336                                value => ''};              = {name => chr ($self->{next_char}),
1337                   value => '',
1338                   line => $self->{line}, column => $self->{column}};
1339          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1340          !!!next-input-character;          !!!next-input-character;
1341          redo A;          redo A;
# Line 838  sub _get_next_token ($) { Line 1345  sub _get_next_token ($) {
1345          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1346              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1347            !!!cp (57);            !!!cp (57);
1348            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name}, line => $self->{current_attribute}->{line}, column => $self->{current_attribute}->{column});
1349            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1350          } else {          } else {
1351            !!!cp (58);            !!!cp (58);
# Line 867  sub _get_next_token ($) { Line 1374  sub _get_next_token ($) {
1374          $before_leave->();          $before_leave->();
1375          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1376            !!!cp (61);            !!!cp (61);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1377            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1378          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1379            !!!cp (62);            !!!cp (62);
# Line 893  sub _get_next_token ($) { Line 1398  sub _get_next_token ($) {
1398          !!!next-input-character;          !!!next-input-character;
1399          redo A;          redo A;
1400        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1401            !!!cp (64);
1402          $before_leave->();          $before_leave->();
1403            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1404          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_char} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           !!!cp (64);  
           #  
         } else {  
           !!!cp (65);  
           !!!parse-error (type => 'nestc');  
         }  
         $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;  
         # next-input-character is already done  
1405          redo A;          redo A;
1406        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1407          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1408          $before_leave->();          $before_leave->();
1409          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1410            !!!cp (66);            !!!cp (66);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1411            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1412          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1413            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 965  sub _get_next_token ($) { Line 1458  sub _get_next_token ($) {
1458        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1459          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1460            !!!cp (73);            !!!cp (73);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1461            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1462          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1463            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 989  sub _get_next_token ($) { Line 1480  sub _get_next_token ($) {
1480        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{next_char} and
1481                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1482          !!!cp (76);          !!!cp (76);
1483          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{current_attribute}
1484                                value => ''};              = {name => chr ($self->{next_char} + 0x0020),
1485                   value => '',
1486                   line => $self->{line}, column => $self->{column}};
1487          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1488          !!!next-input-character;          !!!next-input-character;
1489          redo A;          redo A;
1490        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1491            !!!cp (77);
1492            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1493          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_char} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           !!!cp (77);  
           #  
         } else {  
           !!!cp (78);  
           !!!parse-error (type => 'nestc');  
           ## TODO: Different error type for <aa / bb> than <aa/>  
         }  
         $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;  
         # next-input-character is already done  
1494          redo A;          redo A;
1495        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1496          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1497          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1498            !!!cp (79);            !!!cp (79);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1499            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1500          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1501            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1037  sub _get_next_token ($) { Line 1517  sub _get_next_token ($) {
1517          redo A;          redo A;
1518        } else {        } else {
1519          !!!cp (82);          !!!cp (82);
1520          $self->{current_attribute} = {name => chr ($self->{next_char}),          $self->{current_attribute}
1521                                value => ''};              = {name => chr ($self->{next_char}),
1522                   value => '',
1523                   line => $self->{line}, column => $self->{column}};
1524          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1525          !!!next-input-character;          !!!next-input-character;
1526          redo A;                  redo A;        
# Line 1071  sub _get_next_token ($) { Line 1553  sub _get_next_token ($) {
1553        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1554          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1555            !!!cp (87);            !!!cp (87);
           $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 1096  sub _get_next_token ($) { Line 1576  sub _get_next_token ($) {
1576          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1577          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1578            !!!cp (90);            !!!cp (90);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1579            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1580          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1581            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1145  sub _get_next_token ($) { Line 1623  sub _get_next_token ($) {
1623          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1624          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1625            !!!cp (97);            !!!cp (97);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1626            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1627          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1628            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1189  sub _get_next_token ($) { Line 1665  sub _get_next_token ($) {
1665          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1666          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1667            !!!cp (103);            !!!cp (103);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1668            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1669          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1670            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1236  sub _get_next_token ($) { Line 1710  sub _get_next_token ($) {
1710        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1711          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1712            !!!cp (109);            !!!cp (109);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1713            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1714          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1715            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1261  sub _get_next_token ($) { Line 1733  sub _get_next_token ($) {
1733          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1734          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1735            !!!cp (112);            !!!cp (112);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1736            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1737          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1738            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1333  sub _get_next_token ($) { Line 1803  sub _get_next_token ($) {
1803        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1804          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1805            !!!cp (119);            !!!cp (119);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1806            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1807          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1808            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1355  sub _get_next_token ($) { Line 1823  sub _get_next_token ($) {
1823    
1824          redo A;          redo A;
1825        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1826            !!!cp (122);
1827            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1828          !!!next-input-character;          !!!next-input-character;
1829          if ($self->{next_char} == 0x003E and # >          redo A;
1830              $self->{current_token}->{type} == START_TAG_TOKEN and        } elsif ($self->{next_char} == -1) {
1831              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {          !!!parse-error (type => 'unclosed tag');
1832            # permitted slash          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1833            !!!cp (122);            !!!cp (122.3);
1834            #            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1835            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1836              if ($self->{current_token}->{attributes}) {
1837                !!!cp (122.1);
1838                !!!parse-error (type => 'end tag attribute');
1839              } else {
1840                ## NOTE: This state should never be reached.
1841                !!!cp (122.2);
1842              }
1843          } else {          } else {
1844            !!!cp (123);            die "$0: $self->{current_token}->{type}: Unknown token type";
           !!!parse-error (type => 'nestc');  
1845          }          }
1846          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = DATA_STATE;
1847          # next-input-character is already done          ## Reconsume.
1848            !!!emit ($self->{current_token}); # start tag or end tag
1849          redo A;          redo A;
1850        } else {        } else {
1851          !!!cp (124);          !!!cp ('124.1');
1852          !!!parse-error (type => 'no space between attributes');          !!!parse-error (type => 'no space between attributes');
1853          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1854          ## reconsume          ## reconsume
1855          redo A;          redo A;
1856        }        }
1857        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1858          if ($self->{next_char} == 0x003E) { # >
1859            if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1860              !!!cp ('124.2');
1861              !!!parse-error (type => 'nestc', token => $self->{current_token});
1862              ## TODO: Different type than slash in start tag
1863              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1864              if ($self->{current_token}->{attributes}) {
1865                !!!cp ('124.4');
1866                !!!parse-error (type => 'end tag attribute');
1867              } else {
1868                !!!cp ('124.5');
1869              }
1870              ## TODO: Test |<title></title/>|
1871            } else {
1872              !!!cp ('124.3');
1873              $self->{self_closing} = 1;
1874            }
1875    
1876            $self->{state} = DATA_STATE;
1877            !!!next-input-character;
1878    
1879            !!!emit ($self->{current_token}); # start tag or end tag
1880    
1881            redo A;
1882          } elsif ($self->{next_char} == -1) {
1883            !!!parse-error (type => 'unclosed tag');
1884            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1885              !!!cp (124.7);
1886              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1887            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1888              if ($self->{current_token}->{attributes}) {
1889                !!!cp (124.5);
1890                !!!parse-error (type => 'end tag attribute');
1891              } else {
1892                ## NOTE: This state should never be reached.
1893                !!!cp (124.6);
1894              }
1895            } else {
1896              die "$0: $self->{current_token}->{type}: Unknown token type";
1897            }
1898            $self->{state} = DATA_STATE;
1899            ## Reconsume.
1900            !!!emit ($self->{current_token}); # start tag or end tag
1901            redo A;
1902          } else {
1903            !!!cp ('124.4');
1904            !!!parse-error (type => 'nestc');
1905            ## TODO: This error type is wrong.
1906            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1907            ## Reconsume.
1908            redo A;
1909          }
1910      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1911        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1912                
1913        my $token = {type => COMMENT_TOKEN, data => ''};        ## NOTE: Set by the previous state
1914          #my $token = {type => COMMENT_TOKEN, data => ''};
1915    
1916        BC: {        BC: {
1917          if ($self->{next_char} == 0x003E) { # >          if ($self->{next_char} == 0x003E) { # >
# Line 1387  sub _get_next_token ($) { Line 1919  sub _get_next_token ($) {
1919            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1920            !!!next-input-character;            !!!next-input-character;
1921    
1922            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1923    
1924            redo A;            redo A;
1925          } elsif ($self->{next_char} == -1) {          } elsif ($self->{next_char} == -1) {
# Line 1395  sub _get_next_token ($) { Line 1927  sub _get_next_token ($) {
1927            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1928            ## reconsume            ## reconsume
1929    
1930            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1931    
1932            redo A;            redo A;
1933          } else {          } else {
1934            !!!cp (126);            !!!cp (126);
1935            $token->{data} .= chr ($self->{next_char});            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1936            !!!next-input-character;            !!!next-input-character;
1937            redo BC;            redo BC;
1938          }          }
# Line 1410  sub _get_next_token ($) { Line 1942  sub _get_next_token ($) {
1942      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1943        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1944    
1945          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1);
1946    
1947        my @next_char;        my @next_char;
1948        push @next_char, $self->{next_char};        push @next_char, $self->{next_char};
1949                
# Line 1418  sub _get_next_token ($) { Line 1952  sub _get_next_token ($) {
1952          push @next_char, $self->{next_char};          push @next_char, $self->{next_char};
1953          if ($self->{next_char} == 0x002D) { # -          if ($self->{next_char} == 0x002D) { # -
1954            !!!cp (127);            !!!cp (127);
1955            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1956                                        line => $l, column => $c,
1957                                       };
1958            $self->{state} = COMMENT_START_STATE;            $self->{state} = COMMENT_START_STATE;
1959            !!!next-input-character;            !!!next-input-character;
1960            redo A;            redo A;
# Line 1454  sub _get_next_token ($) { Line 1990  sub _get_next_token ($) {
1990                      !!!cp (129);                      !!!cp (129);
1991                      ## TODO: What a stupid code this is!                      ## TODO: What a stupid code this is!
1992                      $self->{state} = DOCTYPE_STATE;                      $self->{state} = DOCTYPE_STATE;
1993                        $self->{current_token} = {type => DOCTYPE_TOKEN,
1994                                                  quirks => 1,
1995                                                  line => $l, column => $c,
1996                                                 };
1997                      !!!next-input-character;                      !!!next-input-character;
1998                      redo A;                      redo A;
1999                    } else {                    } else {
# Line 1474  sub _get_next_token ($) { Line 2014  sub _get_next_token ($) {
2014          } else {          } else {
2015            !!!cp (135);            !!!cp (135);
2016          }          }
2017          } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
2018                   $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
2019                   $self->{next_char} == 0x005B) { # [
2020            !!!next-input-character;
2021            push @next_char, $self->{next_char};
2022            if ($self->{next_char} == 0x0043) { # C
2023              !!!next-input-character;
2024              push @next_char, $self->{next_char};
2025              if ($self->{next_char} == 0x0044) { # D
2026                !!!next-input-character;
2027                push @next_char, $self->{next_char};
2028                if ($self->{next_char} == 0x0041) { # A
2029                  !!!next-input-character;
2030                  push @next_char, $self->{next_char};
2031                  if ($self->{next_char} == 0x0054) { # T
2032                    !!!next-input-character;
2033                    push @next_char, $self->{next_char};
2034                    if ($self->{next_char} == 0x0041) { # A
2035                      !!!next-input-character;
2036                      push @next_char, $self->{next_char};
2037                      if ($self->{next_char} == 0x005B) { # [
2038                        !!!cp (135.1);
2039                        $self->{state} = CDATA_BLOCK_STATE;
2040                        !!!next-input-character;
2041                        redo A;
2042                      } else {
2043                        !!!cp (135.2);
2044                      }
2045                    } else {
2046                      !!!cp (135.3);
2047                    }
2048                  } else {
2049                    !!!cp (135.4);                
2050                  }
2051                } else {
2052                  !!!cp (135.5);
2053                }
2054              } else {
2055                !!!cp (135.6);
2056              }
2057            } else {
2058              !!!cp (135.7);
2059            }
2060        } else {        } else {
2061          !!!cp (136);          !!!cp (136);
2062        }        }
# Line 1482  sub _get_next_token ($) { Line 2065  sub _get_next_token ($) {
2065        $self->{next_char} = shift @next_char;        $self->{next_char} = shift @next_char;
2066        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
2067        $self->{state} = BOGUS_COMMENT_STATE;        $self->{state} = BOGUS_COMMENT_STATE;
2068          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2069                                    line => $l, column => $c,
2070                                   };
2071        redo A;        redo A;
2072                
2073        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
# Line 1605  sub _get_next_token ($) { Line 2191  sub _get_next_token ($) {
2191          redo A;          redo A;
2192        } elsif ($self->{next_char} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
2193          !!!cp (152);          !!!cp (152);
2194          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment',
2195                            line => $self->{line_prev},
2196                            column => $self->{column_prev});
2197          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
2198          ## Stay in the state          ## Stay in the state
2199          !!!next-input-character;          !!!next-input-character;
# Line 1621  sub _get_next_token ($) { Line 2209  sub _get_next_token ($) {
2209          redo A;          redo A;
2210        } else {        } else {
2211          !!!cp (154);          !!!cp (154);
2212          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment',
2213                            line => $self->{line_prev},
2214                            column => $self->{column_prev});
2215          $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment          $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
2216          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2217          !!!next-input-character;          !!!next-input-character;
# Line 1660  sub _get_next_token ($) { Line 2250  sub _get_next_token ($) {
2250          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2251          !!!next-input-character;          !!!next-input-character;
2252    
2253          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2254    
2255          redo A;          redo A;
2256        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
# Line 1669  sub _get_next_token ($) { Line 2259  sub _get_next_token ($) {
2259          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2260          ## reconsume          ## reconsume
2261    
2262          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2263    
2264          redo A;          redo A;
2265        } else {        } else {
2266          !!!cp (160);          !!!cp (160);
2267          $self->{current_token}          $self->{current_token}->{name} = chr $self->{next_char};
2268              = {type => DOCTYPE_TOKEN,          delete $self->{current_token}->{quirks};
                name => chr ($self->{next_char}),  
                #quirks => 0,  
               };  
2269  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
2270          $self->{state} = DOCTYPE_NAME_STATE;          $self->{state} = DOCTYPE_NAME_STATE;
2271          !!!next-input-character;          !!!next-input-character;
# Line 2152  sub _get_next_token ($) { Line 2739  sub _get_next_token ($) {
2739          redo A;          redo A;
2740        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
2741          !!!cp (217);          !!!cp (217);
         !!!parse-error (type => 'unclosed DOCTYPE');  
2742    
2743          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2744          ## reconsume          ## reconsume
# Line 2194  sub _get_next_token ($) { Line 2780  sub _get_next_token ($) {
2780          !!!next-input-character;          !!!next-input-character;
2781          redo A;          redo A;
2782        }        }
2783        } elsif ($self->{state} == CDATA_BLOCK_STATE) {
2784          my $s = '';
2785          
2786          my ($l, $c) = ($self->{line}, $self->{column});
2787    
2788          CS: while ($self->{next_char} != -1) {
2789            if ($self->{next_char} == 0x005D) { # ]
2790              !!!next-input-character;
2791              if ($self->{next_char} == 0x005D) { # ]
2792                !!!next-input-character;
2793                MDC: {
2794                  if ($self->{next_char} == 0x003E) { # >
2795                    !!!cp (221.1);
2796                    !!!next-input-character;
2797                    last CS;
2798                  } elsif ($self->{next_char} == 0x005D) { # ]
2799                    !!!cp (221.2);
2800                    $s .= ']';
2801                    !!!next-input-character;
2802                    redo MDC;
2803                  } else {
2804                    !!!cp (221.3);
2805                    $s .= ']]';
2806                    #
2807                  }
2808                } # MDC
2809              } else {
2810                !!!cp (221.4);
2811                $s .= ']';
2812                #
2813              }
2814            } else {
2815              !!!cp (221.5);
2816              #
2817            }
2818            $s .= chr $self->{next_char};
2819            !!!next-input-character;
2820          } # CS
2821    
2822          $self->{state} = DATA_STATE;
2823          ## next-input-character done or EOF, which is reconsumed.
2824    
2825          if (length $s) {
2826            !!!cp (221.6);
2827            !!!emit ({type => CHARACTER_TOKEN, data => $s,
2828                      line => $l, column => $c});
2829          } else {
2830            !!!cp (221.7);
2831          }
2832    
2833          redo A;
2834    
2835          ## ISSUE: "text tokens" in spec.
2836          ## TODO: Streaming support
2837      } else {      } else {
2838        die "$0: $self->{state}: Unknown state";        die "$0: $self->{state}: Unknown state";
2839      }      }
# Line 2205  sub _get_next_token ($) { Line 2845  sub _get_next_token ($) {
2845  sub _tokenize_attempt_to_consume_an_entity ($$$) {  sub _tokenize_attempt_to_consume_an_entity ($$$) {
2846    my ($self, $in_attr, $additional) = @_;    my ($self, $in_attr, $additional) = @_;
2847    
2848      my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
2849    
2850    if ({    if ({
2851         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
2852         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
# Line 2245  sub _tokenize_attempt_to_consume_an_enti Line 2887  sub _tokenize_attempt_to_consume_an_enti
2887            redo X;            redo X;
2888          } elsif (not defined $code) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
2889            !!!cp (1005);            !!!cp (1005);
2890            !!!parse-error (type => 'bare hcro');            !!!parse-error (type => 'bare hcro', line => $l, column => $c);
2891            !!!back-next-input-character ($x_char, $self->{next_char});            !!!back-next-input-character ($x_char, $self->{next_char});
2892            $self->{next_char} = 0x0023; # #            $self->{next_char} = 0x0023; # #
2893            return undef;            return undef;
# Line 2254  sub _tokenize_attempt_to_consume_an_enti Line 2896  sub _tokenize_attempt_to_consume_an_enti
2896            !!!next-input-character;            !!!next-input-character;
2897          } else {          } else {
2898            !!!cp (1007);            !!!cp (1007);
2899            !!!parse-error (type => 'no refc');            !!!parse-error (type => 'no refc', line => $l, column => $c);
2900          }          }
2901    
2902          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2903            !!!cp (1008);            !!!cp (1008);
2904            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);            !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2905            $code = 0xFFFD;            $code = 0xFFFD;
2906          } elsif ($code > 0x10FFFF) {          } elsif ($code > 0x10FFFF) {
2907            !!!cp (1009);            !!!cp (1009);
2908            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);            !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2909            $code = 0xFFFD;            $code = 0xFFFD;
2910          } elsif ($code == 0x000D) {          } elsif ($code == 0x000D) {
2911            !!!cp (1010);            !!!cp (1010);
2912            !!!parse-error (type => 'CR character reference');            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2913            $code = 0x000A;            $code = 0x000A;
2914          } elsif (0x80 <= $code and $code <= 0x9F) {          } elsif (0x80 <= $code and $code <= 0x9F) {
2915            !!!cp (1011);            !!!cp (1011);
2916            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);            !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2917            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
2918          }          }
2919    
2920          return {type => CHARACTER_TOKEN, data => chr $code,          return {type => CHARACTER_TOKEN, data => chr $code,
2921                  has_reference => 1};                  has_reference => 1,
2922                    line => $l, column => $c,
2923                   };
2924        } # X        } # X
2925      } elsif (0x0030 <= $self->{next_char} and      } elsif (0x0030 <= $self->{next_char} and
2926               $self->{next_char} <= 0x0039) { # 0..9               $self->{next_char} <= 0x0039) { # 0..9
# Line 2297  sub _tokenize_attempt_to_consume_an_enti Line 2941  sub _tokenize_attempt_to_consume_an_enti
2941          !!!next-input-character;          !!!next-input-character;
2942        } else {        } else {
2943          !!!cp (1014);          !!!cp (1014);
2944          !!!parse-error (type => 'no refc');          !!!parse-error (type => 'no refc', line => $l, column => $c);
2945        }        }
2946    
2947        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2948          !!!cp (1015);          !!!cp (1015);
2949          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);          !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2950          $code = 0xFFFD;          $code = 0xFFFD;
2951        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
2952          !!!cp (1016);          !!!cp (1016);
2953          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);          !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2954          $code = 0xFFFD;          $code = 0xFFFD;
2955        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
2956          !!!cp (1017);          !!!cp (1017);
2957          !!!parse-error (type => 'CR character reference');          !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2958          $code = 0x000A;          $code = 0x000A;
2959        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
2960          !!!cp (1018);          !!!cp (1018);
2961          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2962          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
2963        }        }
2964                
2965        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,
2966                  line => $l, column => $c,
2967                 };
2968      } else {      } else {
2969        !!!cp (1019);        !!!cp (1019);
2970        !!!parse-error (type => 'bare nero');        !!!parse-error (type => 'bare nero', line => $l, column => $c);
2971        !!!back-next-input-character ($self->{next_char});        !!!back-next-input-character ($self->{next_char});
2972        $self->{next_char} = 0x0023; # #        $self->{next_char} = 0x0023; # #
2973        return undef;        return undef;
# Line 2338  sub _tokenize_attempt_to_consume_an_enti Line 2984  sub _tokenize_attempt_to_consume_an_enti
2984      require Whatpm::_NamedEntityList;      require Whatpm::_NamedEntityList;
2985      our $EntityChar;      our $EntityChar;
2986    
2987      while (length $entity_name < 10 and      while (length $entity_name < 30 and
2988             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
2989             ((0x0041 <= $self->{next_char} and # a             ((0x0041 <= $self->{next_char} and # a
2990               $self->{next_char} <= 0x005A) or # x               $self->{next_char} <= 0x005A) or # x
# Line 2371  sub _tokenize_attempt_to_consume_an_enti Line 3017  sub _tokenize_attempt_to_consume_an_enti
3017            
3018      if ($match > 0) {      if ($match > 0) {
3019        !!!cp (1023);        !!!cp (1023);
3020        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
3021                  line => $l, column => $c,
3022                 };
3023      } elsif ($match < 0) {      } elsif ($match < 0) {
3024        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc', line => $l, column => $c);
3025        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
3026          !!!cp (1024);          !!!cp (1024);
3027          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};          return {type => CHARACTER_TOKEN, data => '&'.$entity_name,
3028                    line => $l, column => $c,
3029                   };
3030        } else {        } else {
3031          !!!cp (1025);          !!!cp (1025);
3032          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
3033                    line => $l, column => $c,
3034                   };
3035        }        }
3036      } else {      } else {
3037        !!!cp (1026);        !!!cp (1026);
3038        !!!parse-error (type => 'bare ero');        !!!parse-error (type => 'bare ero', line => $l, column => $c);
3039        ## NOTE: "No characters are consumed" in the spec.        ## NOTE: "No characters are consumed" in the spec.
3040        return {type => CHARACTER_TOKEN, data => '&'.$value};        return {type => CHARACTER_TOKEN, data => '&'.$value,
3041                  line => $l, column => $c,
3042                 };
3043      }      }
3044    } else {    } else {
3045      !!!cp (1027);      !!!cp (1027);
3046      ## no characters are consumed      ## no characters are consumed
3047      !!!parse-error (type => 'bare ero');      !!!parse-error (type => 'bare ero', line => $l, column => $c);
3048      return undef;      return undef;
3049    }    }
3050  } # _tokenize_attempt_to_consume_an_entity  } # _tokenize_attempt_to_consume_an_entity
# Line 2461  sub _tree_construction_initial ($) { Line 3115  sub _tree_construction_initial ($) {
3115            defined $token->{public_identifier} or            defined $token->{public_identifier} or
3116            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
3117          !!!cp ('t1');          !!!cp ('t1');
3118          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
3119        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
3120          !!!cp ('t2');          !!!cp ('t2');
3121          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)
3122          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
3123        } else {        } else {
3124          !!!cp ('t3');          !!!cp ('t3');
3125        }        }
3126                
3127        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
3128          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3129          ## NOTE: Default value for both |public_id| and |system_id| attributes
3130          ## are empty strings, so that we don't set any value in missing cases.
3131        $doctype->public_id ($token->{public_identifier})        $doctype->public_id ($token->{public_identifier})
3132            if defined $token->{public_identifier};            if defined $token->{public_identifier};
3133        $doctype->system_id ($token->{system_identifier})        $doctype->system_id ($token->{system_identifier})
# Line 2486  sub _tree_construction_initial ($) { Line 3142  sub _tree_construction_initial ($) {
3142        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{public_identifier}) {
3143          my $pubid = $token->{public_identifier};          my $pubid = $token->{public_identifier};
3144          $pubid =~ tr/a-z/A-z/;          $pubid =~ tr/a-z/A-z/;
3145          if ({          my $prefix = [
3146            "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,            "+//SILMARIL//DTD HTML PRO V0R11 19970101//",
3147            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3148            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3149            "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 1//",
3150            "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 2//",
3151            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//",
3152            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//",
3153            "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT//",
3154            "-//IETF//DTD HTML 2.0//EN" => 1,            "-//IETF//DTD HTML 2.0//",
3155            "-//IETF//DTD HTML 2.1E//EN" => 1,            "-//IETF//DTD HTML 2.1E//",
3156            "-//IETF//DTD HTML 3.0//EN" => 1,            "-//IETF//DTD HTML 3.0//",
3157            "-//IETF//DTD HTML 3.0//EN//" => 1,            "-//IETF//DTD HTML 3.2 FINAL//",
3158            "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,            "-//IETF//DTD HTML 3.2//",
3159            "-//IETF//DTD HTML 3.2//EN" => 1,            "-//IETF//DTD HTML 3//",
3160            "-//IETF//DTD HTML 3//EN" => 1,            "-//IETF//DTD HTML LEVEL 0//",
3161            "-//IETF//DTD HTML LEVEL 0//EN" => 1,            "-//IETF//DTD HTML LEVEL 1//",
3162            "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,            "-//IETF//DTD HTML LEVEL 2//",
3163            "-//IETF//DTD HTML LEVEL 1//EN" => 1,            "-//IETF//DTD HTML LEVEL 3//",
3164            "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 0//",
3165            "-//IETF//DTD HTML LEVEL 2//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 1//",
3166            "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 2//",
3167            "-//IETF//DTD HTML LEVEL 3//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 3//",
3168            "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,            "-//IETF//DTD HTML STRICT//",
3169            "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,            "-//IETF//DTD HTML//",
3170            "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,            "-//METRIUS//DTD METRIUS PRESENTATIONAL//",
3171            "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//",
3172            "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//",
3173            "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//",
3174            "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//",
3175            "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//",
3176            "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//",
3177            "-//IETF//DTD HTML STRICT//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD HTML//",
3178            "-//IETF//DTD HTML STRICT//EN//2.0" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//",
3179            "-//IETF//DTD HTML STRICT//EN//3.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//",
3180            "-//IETF//DTD HTML//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//",
3181            "-//IETF//DTD HTML//EN//2.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//",
3182            "-//IETF//DTD HTML//EN//3.0" => 1,            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//",
3183            "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//",
3184            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//",
3185            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//",
3186            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//",
3187            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//",
3188            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,            "-//W3C//DTD HTML 3 1995-03-24//",
3189            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,            "-//W3C//DTD HTML 3.2 DRAFT//",
3190            "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,            "-//W3C//DTD HTML 3.2 FINAL//",
3191            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//W3C//DTD HTML 3.2//",
3192            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//W3C//DTD HTML 3.2S DRAFT//",
3193            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 FRAMESET//",
3194            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 TRANSITIONAL//",
3195            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMETNAL 19960712//",
3196            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMENTAL 970421//",
3197            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//W3C//DTD W3 HTML//",
3198            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//W3O//DTD W3 HTML 3.0//",
3199            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML 2.0//",
3200            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML//",
3201            "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,          ]; # $prefix
3202            "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,          my $match;
3203            "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,          for (@$prefix) {
3204            "-//W3C//DTD HTML 3.2//EN" => 1,            if (substr ($prefix, 0, length $_) eq $_) {
3205            "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,              $match = 1;
3206            "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,              last;
3207            "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,            }
3208            "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,          }
3209            "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,          if ($match or
3210            "-//W3C//DTD W3 HTML//EN" => 1,              $pubid eq "-//W3O//DTD W3 HTML STRICT 3.0//EN//" or
3211            "-//W3O//DTD W3 HTML 3.0//EN" => 1,              $pubid eq "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" or
3212            "-//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}) {  
3213            !!!cp ('t5');            !!!cp ('t5');
3214            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3215          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD HTML 4.01 FRAMESET//] or
3216                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD HTML 4.01 TRANSITIONAL//]) {
3217            if (defined $token->{system_identifier}) {            if (defined $token->{system_identifier}) {
3218              !!!cp ('t6');              !!!cp ('t6');
3219              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
# Line 2571  sub _tree_construction_initial ($) { Line 3221  sub _tree_construction_initial ($) {
3221              !!!cp ('t7');              !!!cp ('t7');
3222              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
3223            }            }
3224          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD XHTML 1.0 FRAMESET//] or
3225                   $pubid eq "-//W3C//DTD XHTML 1.0 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD XHTML 1.0 TRANSITIONAL//]) {
3226            !!!cp ('t8');            !!!cp ('t8');
3227            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
3228          } else {          } else {
# Line 2585  sub _tree_construction_initial ($) { Line 3235  sub _tree_construction_initial ($) {
3235          my $sysid = $token->{system_identifier};          my $sysid = $token->{system_identifier};
3236          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
3237          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") {
3238            ## TODO: Check the spec: PUBLIC "(limited quirks)" "(quirks)"            ## NOTE: Ensure that |PUBLIC "(limited quirks)" "(quirks)"| is
3239              ## marked as quirks.
3240            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3241            !!!cp ('t11');            !!!cp ('t11');
3242          } else {          } else {
# Line 2604  sub _tree_construction_initial ($) { Line 3255  sub _tree_construction_initial ($) {
3255                END_OF_FILE_TOKEN, 1,                END_OF_FILE_TOKEN, 1,
3256               }->{$token->{type}}) {               }->{$token->{type}}) {
3257        !!!cp ('t14');        !!!cp ('t14');
3258        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3259        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3260        ## Go to the "before html" insertion mode.        ## Go to the "before html" insertion mode.
3261        ## reprocess        ## reprocess
3262          !!!ack-later;
3263        return;        return;
3264      } elsif ($token->{type} == CHARACTER_TOKEN) {      } elsif ($token->{type} == CHARACTER_TOKEN) {
3265        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
# Line 2625  sub _tree_construction_initial ($) { Line 3277  sub _tree_construction_initial ($) {
3277          !!!cp ('t17');          !!!cp ('t17');
3278        }        }
3279    
3280        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3281        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3282        ## Go to the "before html" insertion mode.        ## Go to the "before html" insertion mode.
3283        ## reprocess        ## reprocess
# Line 2654  sub _tree_construction_root_element ($) Line 3306  sub _tree_construction_root_element ($)
3306    B: {    B: {
3307        if ($token->{type} == DOCTYPE_TOKEN) {        if ($token->{type} == DOCTYPE_TOKEN) {
3308          !!!cp ('t19');          !!!cp ('t19');
3309          !!!parse-error (type => 'in html:#DOCTYPE');          !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
3310          ## Ignore the token          ## Ignore the token
3311          ## Stay in the insertion mode.          ## Stay in the insertion mode.
3312          !!!next-token;          !!!next-token;
# Line 2688  sub _tree_construction_root_element ($) Line 3340  sub _tree_construction_root_element ($)
3340        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
3341          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
3342            my $root_element;            my $root_element;
3343            !!!create-element ($root_element, $token->{tag_name}, $token->{attributes});            !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3344            $self->{document}->append_child ($root_element);            $self->{document}->append_child ($root_element);
3345            push @{$self->{open_elements}}, [$root_element, 'html'];            push @{$self->{open_elements}},
3346                  [$root_element, $el_category->{html}];
3347    
3348            if ($token->{attributes}->{manifest}) {            if ($token->{attributes}->{manifest}) {
3349              !!!cp ('t24');              !!!cp ('t24');
3350              $self->{application_cache_selection}              $self->{application_cache_selection}
3351                  ->($token->{attributes}->{manifest}->{value});                  ->($token->{attributes}->{manifest}->{value});
3352              ## ISSUE: No relative reference resolution?              ## ISSUE: Spec is unclear on relative references.
3353                ## According to Hixie (#whatwg 2008-03-19), it should be
3354                ## resolved against the base URI of the document in HTML
3355                ## or xml:base of the element in XHTML.
3356            } else {            } else {
3357              !!!cp ('t25');              !!!cp ('t25');
3358              $self->{application_cache_selection}->(undef);              $self->{application_cache_selection}->(undef);
3359            }            }
3360    
3361              !!!nack ('t25c');
3362    
3363            !!!next-token;            !!!next-token;
3364            return; ## Go to the "before head" insertion mode.            return; ## Go to the "before head" insertion mode.
3365          } else {          } else {
# Line 2718  sub _tree_construction_root_element ($) Line 3376  sub _tree_construction_root_element ($)
3376          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
3377        }        }
3378    
3379      my $root_element; !!!create-element ($root_element, 'html');      my $root_element;
3380        !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3381      $self->{document}->append_child ($root_element);      $self->{document}->append_child ($root_element);
3382      push @{$self->{open_elements}}, [$root_element, 'html'];      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3383    
3384      $self->{application_cache_selection}->(undef);      $self->{application_cache_selection}->(undef);
3385    
3386      ## NOTE: Reprocess the token.      ## NOTE: Reprocess the token.
3387        !!!ack-later;
3388      return; ## Go to the "before head" insertion mode.      return; ## Go to the "before head" insertion mode.
3389    
3390      ## ISSUE: There is an issue in the spec      ## ISSUE: There is an issue in the spec
# Line 2748  sub _reset_insertion_mode ($) { Line 3408  sub _reset_insertion_mode ($) {
3408        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3409          $last = 1;          $last = 1;
3410          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
3411            if ($self->{inner_html_node}->[1] eq 'td' or            !!!cp ('t28');
3412                $self->{inner_html_node}->[1] eq 'th') {            $node = $self->{inner_html_node};
3413              !!!cp ('t27');          } else {
3414              #            die "_reset_insertion_mode: t27";
           } else {  
             !!!cp ('t28');  
             $node = $self->{inner_html_node};  
           }  
3415          }          }
3416        }        }
3417              
3418        ## Step 4..13        ## Step 4..14
3419        my $new_mode = {        my $new_mode;
3420          if ($node->[1] & FOREIGN_EL) {
3421            !!!cp ('t28.1');
3422            ## NOTE: Strictly spaking, the line below only applies to MathML and
3423            ## SVG elements.  Currently the HTML syntax supports only MathML and
3424            ## SVG elements as foreigners.
3425            $new_mode = $self->{insertion_mode} | IN_FOREIGN_CONTENT_IM;
3426            ## ISSUE: What is set as the secondary insertion mode?
3427          } elsif ($node->[1] & TABLE_CELL_EL) {
3428            if ($last) {
3429              !!!cp ('t28.2');
3430              #
3431            } else {
3432              !!!cp ('t28.3');
3433              $new_mode = IN_CELL_IM;
3434            }
3435          } else {
3436            !!!cp ('t28.4');
3437            $new_mode = {
3438                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
3439                        ## NOTE: |option| and |optgroup| do not set                        ## NOTE: |option| and |optgroup| do not set
3440                        ## insertion mode to "in select" by themselves.                        ## insertion mode to "in select" by themselves.
                       td => IN_CELL_IM,  
                       th => IN_CELL_IM,  
3441                        tr => IN_ROW_IM,                        tr => IN_ROW_IM,
3442                        tbody => IN_TABLE_BODY_IM,                        tbody => IN_TABLE_BODY_IM,
3443                        thead => IN_TABLE_BODY_IM,                        thead => IN_TABLE_BODY_IM,
# Line 2776  sub _reset_insertion_mode ($) { Line 3448  sub _reset_insertion_mode ($) {
3448                        head => IN_BODY_IM, # not in head!                        head => IN_BODY_IM, # not in head!
3449                        body => IN_BODY_IM,                        body => IN_BODY_IM,
3450                        frameset => IN_FRAMESET_IM,                        frameset => IN_FRAMESET_IM,
3451                       }->{$node->[1]};                       }->{$node->[0]->manakai_local_name};
3452          }
3453        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3454                
3455        ## Step 14        ## Step 15
3456        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3457          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3458            !!!cp ('t29');            !!!cp ('t29');
3459            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
# Line 2794  sub _reset_insertion_mode ($) { Line 3467  sub _reset_insertion_mode ($) {
3467          !!!cp ('t31');          !!!cp ('t31');
3468        }        }
3469                
3470        ## Step 15        ## Step 16
3471        $self->{insertion_mode} = IN_BODY_IM and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
3472                
3473        ## Step 16        ## Step 17
3474        $i--;        $i--;
3475        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3476                
3477        ## Step 17        ## Step 18
3478        redo S3;        redo S3;
3479      } # S3      } # S3
3480    
# Line 2913  sub _tree_construction_main ($) { Line 3586  sub _tree_construction_main ($) {
3586      ## Step 1      ## Step 1
3587      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3588      my $el;      my $el;
3589      !!!create-element ($el, $start_tag_name, $token->{attributes});      !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3590    
3591      ## Step 2      ## Step 2
3592      $insert->($el);      $insert->($el);
# Line 2924  sub _tree_construction_main ($) { Line 3597  sub _tree_construction_main ($) {
3597    
3598      ## Step 4      ## Step 4
3599      my $text = '';      my $text = '';
3600        !!!nack ('t40.1');
3601      !!!next-token;      !!!next-token;
3602      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3603        !!!cp ('t40');        !!!cp ('t40');
# Line 2950  sub _tree_construction_main ($) { Line 3624  sub _tree_construction_main ($) {
3624        ## NOTE: An end-of-file token.        ## NOTE: An end-of-file token.
3625        if ($content_model_flag == CDATA_CONTENT_MODEL) {        if ($content_model_flag == CDATA_CONTENT_MODEL) {
3626          !!!cp ('t43');          !!!cp ('t43');
3627          !!!parse-error (type => 'in CDATA:#'.$token->{type});          !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3628        } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {        } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3629          !!!cp ('t44');          !!!cp ('t44');
3630          !!!parse-error (type => 'in RCDATA:#'.$token->{type});          !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
3631        } else {        } else {
3632          die "$0: $content_model_flag in parse_rcdata";          die "$0: $content_model_flag in parse_rcdata";
3633        }        }
# Line 2963  sub _tree_construction_main ($) { Line 3637  sub _tree_construction_main ($) {
3637    
3638    my $script_start_tag = sub () {    my $script_start_tag = sub () {
3639      my $script_el;      my $script_el;
3640      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
3641      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3642    
3643      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
3644      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
3645            
3646      my $text = '';      my $text = '';
3647        !!!nack ('t45.1');
3648      !!!next-token;      !!!next-token;
3649      while ($token->{type} == CHARACTER_TOKEN) {      while ($token->{type} == CHARACTER_TOKEN) {
3650        !!!cp ('t45');        !!!cp ('t45');
# Line 2989  sub _tree_construction_main ($) { Line 3664  sub _tree_construction_main ($) {
3664        ## Ignore the token        ## Ignore the token
3665      } else {      } else {
3666        !!!cp ('t48');        !!!cp ('t48');
3667        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3668        ## ISSUE: And ignore?        ## ISSUE: And ignore?
3669        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3670      }      }
# Line 3017  sub _tree_construction_main ($) { Line 3692  sub _tree_construction_main ($) {
3692    my $open_tables = [[$self->{open_elements}->[0]->[0]]];    my $open_tables = [[$self->{open_elements}->[0]->[0]]];
3693    
3694    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
3695      my $tag_name = shift;      my $end_tag_token = shift;
3696        my $tag_name = $end_tag_token->{tag_name};
3697    
3698      ## NOTE: The adoption agency algorithm (AAA).      ## NOTE: The adoption agency algorithm (AAA).
3699    
# Line 3026  sub _tree_construction_main ($) { Line 3702  sub _tree_construction_main ($) {
3702        my $formatting_element;        my $formatting_element;
3703        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3704        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3705          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3706              !!!cp ('t52');
3707              last AFE;
3708            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
3709                         eq $tag_name) {
3710            !!!cp ('t51');            !!!cp ('t51');
3711            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3712            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3713            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           !!!cp ('t52');  
           last AFE;  
3714          }          }
3715        } # AFE        } # AFE
3716        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3717          !!!cp ('t53');          !!!cp ('t53');
3718          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!parse-error (type => 'unmatched end tag:'.$tag_name, token => $end_tag_token);
3719          ## Ignore the token          ## Ignore the token
3720          !!!next-token;          !!!next-token;
3721          return;          return;
# Line 3055  sub _tree_construction_main ($) { Line 3732  sub _tree_construction_main ($) {
3732              last INSCOPE;              last INSCOPE;
3733            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3734              !!!cp ('t55');              !!!cp ('t55');
3735              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3736                                token => $end_tag_token);
3737              ## Ignore the token              ## Ignore the token
3738              !!!next-token;              !!!next-token;
3739              return;              return;
3740            }            }
3741          } 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]}) {  
3742            !!!cp ('t56');            !!!cp ('t56');
3743            $in_scope = 0;            $in_scope = 0;
3744          }          }
3745        } # INSCOPE        } # INSCOPE
3746        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3747          !!!cp ('t57');          !!!cp ('t57');
3748          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3749                            token => $end_tag_token);
3750          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3751          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
3752          return;          return;
3753        }        }
3754        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3755          !!!cp ('t58');          !!!cp ('t58');
3756          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!parse-error (type => 'not closed',
3757                            value => $self->{open_elements}->[-1]->[0]
3758                                ->manakai_local_name,
3759                            token => $end_tag_token);
3760        }        }
3761                
3762        ## Step 2        ## Step 2
# Line 3085  sub _tree_construction_main ($) { Line 3764  sub _tree_construction_main ($) {
3764        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
3765        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3766          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3767          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
3768              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3769              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
3770               $scoping_category->{$node->[1]})) { ## Scoping is redundant, maybe               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
3771            !!!cp ('t59');            !!!cp ('t59');
3772            $furthest_block = $node;            $furthest_block = $node;
3773            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
# Line 3174  sub _tree_construction_main ($) { Line 3853  sub _tree_construction_main ($) {
3853        } # S7          } # S7  
3854                
3855        ## Step 8        ## Step 8
3856        if ({        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
            table => 1, tbody => 1, tfoot => 1, thead => 1, tr => 1,  
           }->{$common_ancestor_node->[1]}) {  
3857          my $foster_parent_element;          my $foster_parent_element;
3858          my $next_sibling;          my $next_sibling;
3859                           OE: for (reverse 0..$#{$self->{open_elements}}) {          OE: for (reverse 0..$#{$self->{open_elements}}) {
3860                             if ($self->{open_elements}->[$_]->[1] eq 'table') {            if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3861                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3862                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3863                                 !!!cp ('t65.1');                                 !!!cp ('t65.1');
# Line 3253  sub _tree_construction_main ($) { Line 3930  sub _tree_construction_main ($) {
3930    
3931    my $insert_to_foster = sub {    my $insert_to_foster = sub {
3932      my $child = shift;      my $child = shift;
3933      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]}) {  
3934        # MUST        # MUST
3935        my $foster_parent_element;        my $foster_parent_element;
3936        my $next_sibling;        my $next_sibling;
3937                           OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3938                             if ($self->{open_elements}->[$_]->[1] eq 'table') {          if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3939                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3940                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3941                                 !!!cp ('t70');                                 !!!cp ('t70');
# Line 3285  sub _tree_construction_main ($) { Line 3960  sub _tree_construction_main ($) {
3960      }      }
3961    }; # $insert_to_foster    }; # $insert_to_foster
3962    
3963    B: {    B: while (1) {
3964      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
3965        !!!cp ('t73');        !!!cp ('t73');
3966        !!!parse-error (type => 'DOCTYPE in the middle');        !!!parse-error (type => 'DOCTYPE in the middle', token => $token);
3967        ## Ignore the token        ## Ignore the token
3968        ## Stay in the phase        ## Stay in the phase
3969        !!!next-token;        !!!next-token;
3970        redo B;        next B;
3971      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
3972               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
3973        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
3974          !!!cp ('t79');          !!!cp ('t79');
3975          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html', token => $token);
3976          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
3977        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
3978          !!!cp ('t80');          !!!cp ('t80');
3979          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html', token => $token);
3980          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
3981        } else {        } else {
3982          !!!cp ('t81');          !!!cp ('t81');
3983        }        }
3984    
3985        !!!cp ('t82');        !!!cp ('t82');
3986        !!!parse-error (type => 'not first start tag');        !!!parse-error (type => 'not first start tag', token => $token);
3987        my $top_el = $self->{open_elements}->[0]->[0];        my $top_el = $self->{open_elements}->[0]->[0];
3988        for my $attr_name (keys %{$token->{attributes}}) {        for my $attr_name (keys %{$token->{attributes}}) {
3989          unless ($top_el->has_attribute_ns (undef, $attr_name)) {          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
# Line 3318  sub _tree_construction_main ($) { Line 3993  sub _tree_construction_main ($) {
3993               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
3994          }          }
3995        }        }
3996          !!!nack ('t84.1');
3997        !!!next-token;        !!!next-token;
3998        redo B;        next B;
3999      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
4000        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
4001        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
# Line 3333  sub _tree_construction_main ($) { Line 4009  sub _tree_construction_main ($) {
4009          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
4010        }        }
4011        !!!next-token;        !!!next-token;
4012        redo B;        next B;
4013      } elsif ($self->{insertion_mode} & HEAD_IMS) {      } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
4014          if ($token->{type} == CHARACTER_TOKEN) {
4015            !!!cp ('t87.1');
4016            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4017            !!!next-token;
4018            next B;
4019          } elsif ($token->{type} == START_TAG_TOKEN) {
4020            if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
4021                 $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
4022                not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
4023                ($token->{tag_name} eq 'svg' and
4024                 $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
4025              ## NOTE: "using the rules for secondary insertion mode"then"continue"
4026              !!!cp ('t87.2');
4027              #
4028            } elsif ({
4029                      b => 1, big => 1, blockquote => 1, body => 1, br => 1,
4030                      center => 1, code => 1, dd => 1, div => 1, dl => 1, em => 1,
4031                      embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1, ## No h4!
4032                      h5 => 1, h6 => 1, head => 1, hr => 1, i => 1, img => 1,
4033                      li => 1, menu => 1, meta => 1, nobr => 1, p => 1, pre => 1,
4034                      ruby => 1, s => 1, small => 1, span => 1, strong => 1,
4035                      sub => 1, sup => 1, table => 1, tt => 1, u => 1, ul => 1,
4036                      var => 1,
4037                     }->{$token->{tag_name}}) {
4038              !!!cp ('t87.2');
4039              !!!parse-error (type => 'not closed',
4040                              value => $self->{open_elements}->[-1]->[0]
4041                                  ->manakai_local_name,
4042                              token => $token);
4043    
4044              pop @{$self->{open_elements}}
4045                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4046    
4047              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4048              ## Reprocess.
4049              next B;
4050            } else {
4051              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
4052              my $tag_name = $token->{tag_name};
4053              if ($nsuri eq $SVG_NS) {
4054                $tag_name = {
4055                   altglyph => 'altGlyph',
4056                   altglyphdef => 'altGlyphDef',
4057                   altglyphitem => 'altGlyphItem',
4058                   animatecolor => 'animateColor',
4059                   animatemotion => 'animateMotion',
4060                   animatetransform => 'animateTransform',
4061                   clippath => 'clipPath',
4062                   feblend => 'feBlend',
4063                   fecolormatrix => 'feColorMatrix',
4064                   fecomponenttransfer => 'feComponentTransfer',
4065                   fecomposite => 'feComposite',
4066                   feconvolvematrix => 'feConvolveMatrix',
4067                   fediffuselighting => 'feDiffuseLighting',
4068                   fedisplacementmap => 'feDisplacementMap',
4069                   fedistantlight => 'feDistantLight',
4070                   feflood => 'feFlood',
4071                   fefunca => 'feFuncA',
4072                   fefuncb => 'feFuncB',
4073                   fefuncg => 'feFuncG',
4074                   fefuncr => 'feFuncR',
4075                   fegaussianblur => 'feGaussianBlur',
4076                   feimage => 'feImage',
4077                   femerge => 'feMerge',
4078                   femergenode => 'feMergeNode',
4079                   femorphology => 'feMorphology',
4080                   feoffset => 'feOffset',
4081                   fepointlight => 'fePointLight',
4082                   fespecularlighting => 'feSpecularLighting',
4083                   fespotlight => 'feSpotLight',
4084                   fetile => 'feTile',
4085                   feturbulence => 'feTurbulence',
4086                   foreignobject => 'foreignObject',
4087                   glyphref => 'glyphRef',
4088                   lineargradient => 'linearGradient',
4089                   radialgradient => 'radialGradient',
4090                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4091                   textpath => 'textPath',  
4092                }->{$tag_name} || $tag_name;
4093              }
4094    
4095              ## "adjust SVG attributes" (SVG only) - done in insert-element-f
4096    
4097              ## "adjust foreign attributes" - done in insert-element-f
4098    
4099              !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
4100    
4101              if ($self->{self_closing}) {
4102                pop @{$self->{open_elements}};
4103                !!!ack ('t87.3');
4104              } else {
4105                !!!cp ('t87.4');
4106              }
4107    
4108              !!!next-token;
4109              next B;
4110            }
4111          } elsif ($token->{type} == END_TAG_TOKEN) {
4112            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4113            !!!cp ('t87.5');
4114            #
4115          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4116            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4117            !!!cp ('t87.6');
4118            #
4119            ## TODO: ...
4120          } else {
4121            die "$0: $token->{type}: Unknown token type";        
4122          }
4123        }
4124    
4125        if ($self->{insertion_mode} & HEAD_IMS) {
4126        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4127          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4128            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
# Line 3344  sub _tree_construction_main ($) { Line 4132  sub _tree_construction_main ($) {
4132              !!!cp ('t88.1');              !!!cp ('t88.1');
4133              ## Ignore the token.              ## Ignore the token.
4134              !!!next-token;              !!!next-token;
4135              redo B;              next B;
4136            }            }
4137            unless (length $token->{data}) {            unless (length $token->{data}) {
4138              !!!cp ('t88');              !!!cp ('t88');
4139              !!!next-token;              !!!next-token;
4140              redo B;              next B;
4141            }            }
4142          }          }
4143    
4144          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4145            !!!cp ('t89');            !!!cp ('t89');
4146            ## As if <head>            ## As if <head>
4147            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4148            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4149            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            push @{$self->{open_elements}},
4150                  [$self->{head_element}, $el_category->{head}];
4151    
4152            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4153            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
# Line 3368  sub _tree_construction_main ($) { Line 4157  sub _tree_construction_main ($) {
4157            !!!cp ('t90');            !!!cp ('t90');
4158            ## As if </noscript>            ## As if </noscript>
4159            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4160            !!!parse-error (type => 'in noscript:#character');            !!!parse-error (type => 'in noscript:#character', token => $token);
4161                        
4162            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4163            ## As if </head>            ## As if </head>
# Line 3384  sub _tree_construction_main ($) { Line 4173  sub _tree_construction_main ($) {
4173            !!!cp ('t92');            !!!cp ('t92');
4174          }          }
4175    
4176              ## "after head" insertion mode          ## "after head" insertion mode
4177              ## As if <body>          ## As if <body>
4178              !!!insert-element ('body');          !!!insert-element ('body',, $token);
4179              $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4180              ## reprocess          ## reprocess
4181              redo B;          next B;
4182            } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4183              if ($token->{tag_name} eq 'head') {          if ($token->{tag_name} eq 'head') {
4184                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4185                  !!!cp ('t93');              !!!cp ('t93');
4186                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4187                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              $self->{open_elements}->[-1]->[0]->append_child
4188                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];                  ($self->{head_element});
4189                  $self->{insertion_mode} = IN_HEAD_IM;              push @{$self->{open_elements}},
4190                  !!!next-token;                  [$self->{head_element}, $el_category->{head}];
4191                  redo B;              $self->{insertion_mode} = IN_HEAD_IM;
4192                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {              !!!nack ('t93.1');
4193                  !!!cp ('t94');              !!!next-token;
4194                  #              next B;
4195                } else {            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4196                  !!!cp ('t95');              !!!cp ('t93.2');
4197                  !!!parse-error (type => 'in head:head'); # or in head noscript              !!!parse-error (type => 'after head:head', token => $token); ## TODO: error type
4198                  ## Ignore the token              ## Ignore the token
4199                  !!!next-token;              !!!nack ('t93.3');
4200                  redo B;              !!!next-token;
4201                }              next B;
4202              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {            } else {
4203                !!!cp ('t96');              !!!cp ('t95');
4204                ## As if <head>              !!!parse-error (type => 'in head:head', token => $token); # or in head noscript
4205                !!!create-element ($self->{head_element}, 'head');              ## Ignore the token
4206                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              !!!nack ('t95.1');
4207                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];              !!!next-token;
4208                next B;
4209              }
4210            } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4211              !!!cp ('t96');
4212              ## As if <head>
4213              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4214              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4215              push @{$self->{open_elements}},
4216                  [$self->{head_element}, $el_category->{head}];
4217    
4218                $self->{insertion_mode} = IN_HEAD_IM;            $self->{insertion_mode} = IN_HEAD_IM;
4219                ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4220              } else {          } else {
4221                !!!cp ('t97');            !!!cp ('t97');
4222              }          }
4223    
4224              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
4225                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4226                  !!!cp ('t98');                  !!!cp ('t98');
4227                  ## As if </noscript>                  ## As if </noscript>
4228                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4229                  !!!parse-error (type => 'in noscript:base');                  !!!parse-error (type => 'in noscript:base', token => $token);
4230                                
4231                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4232                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
# Line 3439  sub _tree_construction_main ($) { Line 4237  sub _tree_construction_main ($) {
4237                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4238                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4239                  !!!cp ('t100');                  !!!cp ('t100');
4240                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4241                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4242                        [$self->{head_element}, $el_category->{head}];
4243                } else {                } else {
4244                  !!!cp ('t101');                  !!!cp ('t101');
4245                }                }
4246                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4247                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4248                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4249                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4250                  !!!nack ('t101.1');
4251                !!!next-token;                !!!next-token;
4252                redo B;                next B;
4253              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
4254                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4255                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4256                  !!!cp ('t102');                  !!!cp ('t102');
4257                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4258                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4259                        [$self->{head_element}, $el_category->{head}];
4260                } else {                } else {
4261                  !!!cp ('t103');                  !!!cp ('t103');
4262                }                }
4263                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4264                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4265                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4266                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4267                  !!!ack ('t103.1');
4268                !!!next-token;                !!!next-token;
4269                redo B;                next B;
4270              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
4271                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4272                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4273                  !!!cp ('t104');                  !!!cp ('t104');
4274                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4275                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4276                        [$self->{head_element}, $el_category->{head}];
4277                } else {                } else {
4278                  !!!cp ('t105');                  !!!cp ('t105');
4279                }                }
4280                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4281                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.
4282    
4283                unless ($self->{confident}) {                unless ($self->{confident}) {
4284                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) {
4285                    !!!cp ('t106');                    !!!cp ('t106');
4286                      ## NOTE: Whether the encoding is supported or not is handled
4287                      ## in the {change_encoding} callback.
4288                    $self->{change_encoding}                    $self->{change_encoding}
4289                        ->($self, $token->{attributes}->{charset}->{value});                        ->($self, $token->{attributes}->{charset}->{value},
4290                             $token);
4291                                        
4292                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4293                        ->set_user_data (manakai_has_reference =>                        ->set_user_data (manakai_has_reference =>
4294                                             $token->{attributes}->{charset}                                             $token->{attributes}->{charset}
4295                                                 ->{has_reference});                                                 ->{has_reference});
4296                  } elsif ($token->{attributes}->{content}) {                  } elsif ($token->{attributes}->{content}) {
                   ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
4297                    if ($token->{attributes}->{content}->{value}                    if ($token->{attributes}->{content}->{value}
4298                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4299                            [\x09-\x0D\x20]*=                            [\x09-\x0D\x20]*=
4300                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4301                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
4302                      !!!cp ('t107');                      !!!cp ('t107');
4303                        ## NOTE: Whether the encoding is supported or not is handled
4304                        ## in the {change_encoding} callback.
4305                      $self->{change_encoding}                      $self->{change_encoding}
4306                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4307                               $token);
4308                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4309                          ->set_user_data (manakai_has_reference =>                          ->set_user_data (manakai_has_reference =>
4310                                               $token->{attributes}->{content}                                               $token->{attributes}->{content}
# Line 3524  sub _tree_construction_main ($) { Line 4332  sub _tree_construction_main ($) {
4332    
4333                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4334                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4335                  !!!ack ('t110.1');
4336                !!!next-token;                !!!next-token;
4337                redo B;                next B;
4338              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
4339                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4340                  !!!cp ('t111');                  !!!cp ('t111');
4341                  ## As if </noscript>                  ## As if </noscript>
4342                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4343                  !!!parse-error (type => 'in noscript:title');                  !!!parse-error (type => 'in noscript:title', token => $token);
4344                                
4345                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4346                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4347                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4348                  !!!cp ('t112');                  !!!cp ('t112');
4349                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4350                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4351                        [$self->{head_element}, $el_category->{head}];
4352                } else {                } else {
4353                  !!!cp ('t113');                  !!!cp ('t113');
4354                }                }
# Line 3549  sub _tree_construction_main ($) { Line 4359  sub _tree_construction_main ($) {
4359                $parse_rcdata->(RCDATA_CONTENT_MODEL);                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4360                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4361                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4362                redo B;                next B;
4363              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style') {
4364                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4365                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
4366                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4367                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4368                  !!!cp ('t114');                  !!!cp ('t114');
4369                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4370                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4371                        [$self->{head_element}, $el_category->{head}];
4372                } else {                } else {
4373                  !!!cp ('t115');                  !!!cp ('t115');
4374                }                }
4375                $parse_rcdata->(CDATA_CONTENT_MODEL);                $parse_rcdata->(CDATA_CONTENT_MODEL);
4376                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4377                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4378                redo B;                next B;
4379              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4380                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
4381                  !!!cp ('t116');                  !!!cp ('t116');
4382                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
4383                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4384                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4385                    !!!nack ('t116.1');
4386                  !!!next-token;                  !!!next-token;
4387                  redo B;                  next B;
4388                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4389                  !!!cp ('t117');                  !!!cp ('t117');
4390                  !!!parse-error (type => 'in noscript:noscript');                  !!!parse-error (type => 'in noscript:noscript', token => $token);
4391                  ## Ignore the token                  ## Ignore the token
4392                    !!!nack ('t117.1');
4393                  !!!next-token;                  !!!next-token;
4394                  redo B;                  next B;
4395                } else {                } else {
4396                  !!!cp ('t118');                  !!!cp ('t118');
4397                  #                  #
# Line 3588  sub _tree_construction_main ($) { Line 4401  sub _tree_construction_main ($) {
4401                  !!!cp ('t119');                  !!!cp ('t119');
4402                  ## As if </noscript>                  ## As if </noscript>
4403                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4404                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:script', token => $token);
4405                                
4406                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4407                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4408                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4409                  !!!cp ('t120');                  !!!cp ('t120');
4410                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4411                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4412                        [$self->{head_element}, $el_category->{head}];
4413                } else {                } else {
4414                  !!!cp ('t121');                  !!!cp ('t121');
4415                }                }
# Line 3604  sub _tree_construction_main ($) { Line 4418  sub _tree_construction_main ($) {
4418                $script_start_tag->();                $script_start_tag->();
4419                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4420                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4421                redo B;                next B;
4422              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
4423                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
4424                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4425                  !!!cp ('t122');                  !!!cp ('t122');
4426                  ## As if </noscript>                  ## As if </noscript>
4427                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4428                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});                  !!!parse-error (type => 'in noscript:'.$token->{tag_name}, token => $token);
4429                                    
4430                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4431                  ## As if </head>                  ## As if </head>
# Line 3628  sub _tree_construction_main ($) { Line 4442  sub _tree_construction_main ($) {
4442                }                }
4443    
4444                ## "after head" insertion mode                ## "after head" insertion mode
4445                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4446                if ($token->{tag_name} eq 'body') {                if ($token->{tag_name} eq 'body') {
4447                  !!!cp ('t126');                  !!!cp ('t126');
4448                  $self->{insertion_mode} = IN_BODY_IM;                  $self->{insertion_mode} = IN_BODY_IM;
# Line 3638  sub _tree_construction_main ($) { Line 4452  sub _tree_construction_main ($) {
4452                } else {                } else {
4453                  die "$0: tag name: $self->{tag_name}";                  die "$0: tag name: $self->{tag_name}";
4454                }                }
4455                  !!!nack ('t127.1');
4456                !!!next-token;                !!!next-token;
4457                redo B;                next B;
4458              } else {              } else {
4459                !!!cp ('t128');                !!!cp ('t128');
4460                #                #
# Line 3649  sub _tree_construction_main ($) { Line 4464  sub _tree_construction_main ($) {
4464                !!!cp ('t129');                !!!cp ('t129');
4465                ## As if </noscript>                ## As if </noscript>
4466                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4467                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
4468                                
4469                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4470                ## As if </head>                ## As if </head>
# Line 3668  sub _tree_construction_main ($) { Line 4483  sub _tree_construction_main ($) {
4483    
4484              ## "after head" insertion mode              ## "after head" insertion mode
4485              ## As if <body>              ## As if <body>
4486              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4487              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4488              ## reprocess              ## reprocess
4489              redo B;              !!!ack-later;
4490                next B;
4491            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
4492              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
4493                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4494                  !!!cp ('t132');                  !!!cp ('t132');
4495                  ## As if <head>                  ## As if <head>
4496                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4497                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4498                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4499                        [$self->{head_element}, $el_category->{head}];
4500    
4501                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4502                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4503                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4504                  !!!next-token;                  !!!next-token;
4505                  redo B;                  next B;
4506                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4507                  !!!cp ('t133');                  !!!cp ('t133');
4508                  ## As if </noscript>                  ## As if </noscript>
4509                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4510                  !!!parse-error (type => 'in noscript:/head');                  !!!parse-error (type => 'in noscript:/head', token => $token);
4511                                    
4512                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4513                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4514                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4515                  !!!next-token;                  !!!next-token;
4516                  redo B;                  next B;
4517                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4518                  !!!cp ('t134');                  !!!cp ('t134');
4519                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4520                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4521                  !!!next-token;                  !!!next-token;
4522                  redo B;                  next B;
4523                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4524                    !!!cp ('t134.1');
4525                    !!!parse-error (type => 'unmatched end tag:head', token => $token);
4526                    ## Ignore the token
4527                    !!!next-token;
4528                    next B;
4529                } else {                } else {
4530                  !!!cp ('t135');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
                 #  
4531                }                }
4532              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4533                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
# Line 3713  sub _tree_construction_main ($) { Line 4535  sub _tree_construction_main ($) {
4535                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4536                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4537                  !!!next-token;                  !!!next-token;
4538                  redo B;                  next B;
4539                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
4540                           $self->{insertion_mode} == AFTER_HEAD_IM) {
4541                  !!!cp ('t137');                  !!!cp ('t137');
4542                  !!!parse-error (type => 'unmatched end tag:noscript');                  !!!parse-error (type => 'unmatched end tag:noscript', token => $token);
4543                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
4544                  !!!next-token;                  !!!next-token;
4545                  redo B;                  next B;
4546                } else {                } else {
4547                  !!!cp ('t138');                  !!!cp ('t138');
4548                  #                  #
# Line 3727  sub _tree_construction_main ($) { Line 4550  sub _tree_construction_main ($) {
4550              } elsif ({              } elsif ({
4551                        body => 1, html => 1,                        body => 1, html => 1,
4552                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4553                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM or
4554                  !!!cp ('t139');                    $self->{insertion_mode} == IN_HEAD_IM or
4555                  ## 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) {  
4556                  !!!cp ('t140');                  !!!cp ('t140');
4557                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4558                    ## Ignore the token
4559                    !!!next-token;
4560                    next B;
4561                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4562                    !!!cp ('t140.1');
4563                    !!!parse-error (type => 'unmatched end tag:' . $token->{tag_name}, token => $token);
4564                  ## Ignore the token                  ## Ignore the token
4565                  !!!next-token;                  !!!next-token;
4566                  redo B;                  next B;
4567                } else {                } else {
4568                  !!!cp ('t141');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
4569                }                }
4570                              } elsif ($token->{tag_name} eq 'p') {
4571                #                !!!cp ('t142');
4572              } elsif ({                !!!parse-error (type => 'unmatched end tag:p', token => $token);
4573                        p => 1, br => 1,                ## Ignore the token
4574                       }->{$token->{tag_name}}) {                !!!next-token;
4575                  next B;
4576                } elsif ($token->{tag_name} eq 'br') {
4577                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4578                  !!!cp ('t142');                  !!!cp ('t142.2');
4579                  ## As if <head>                  ## (before head) as if <head>, (in head) as if </head>
4580                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4581                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4582                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  $self->{insertion_mode} = AFTER_HEAD_IM;
4583      
4584                    ## Reprocess in the "after head" insertion mode...
4585                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4586                    !!!cp ('t143.2');
4587                    ## As if </head>
4588                    pop @{$self->{open_elements}};
4589                    $self->{insertion_mode} = AFTER_HEAD_IM;
4590      
4591                    ## Reprocess in the "after head" insertion mode...
4592                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4593                    !!!cp ('t143.3');
4594                    ## ISSUE: Two parse errors for <head><noscript></br>
4595                    !!!parse-error (type => 'unmatched end tag:br', token => $token);
4596                    ## As if </noscript>
4597                    pop @{$self->{open_elements}};
4598                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4599    
4600                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4601                } else {                  ## As if </head>
4602                  !!!cp ('t143');                  pop @{$self->{open_elements}};
4603                }                  $self->{insertion_mode} = AFTER_HEAD_IM;
4604    
4605                #                  ## Reprocess in the "after head" insertion mode...
4606              } else {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4607                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                  !!!cp ('t143.4');
                 !!!cp ('t144');  
4608                  #                  #
4609                } else {                } else {
4610                  !!!cp ('t145');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
4611                }                }
4612    
4613                  ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
4614                  !!!parse-error (type => 'unmatched end tag:br', token => $token);
4615                  ## Ignore the token
4616                  !!!next-token;
4617                  next B;
4618                } else {
4619                  !!!cp ('t145');
4620                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4621                  ## Ignore the token
4622                  !!!next-token;
4623                  next B;
4624              }              }
4625    
4626              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4627                !!!cp ('t146');                !!!cp ('t146');
4628                ## As if </noscript>                ## As if </noscript>
4629                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4630                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
4631                                
4632                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4633                ## As if </head>                ## As if </head>
# Line 3797  sub _tree_construction_main ($) { Line 4643  sub _tree_construction_main ($) {
4643              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4644  ## ISSUE: This case cannot be reached?  ## ISSUE: This case cannot be reached?
4645                !!!cp ('t148');                !!!cp ('t148');
4646                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4647                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
4648                !!!next-token;                !!!next-token;
4649                redo B;                next B;
4650              } else {              } else {
4651                !!!cp ('t149');                !!!cp ('t149');
4652              }              }
4653    
4654              ## "after head" insertion mode              ## "after head" insertion mode
4655              ## As if <body>              ## As if <body>
4656              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4657              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4658              ## reprocess              ## reprocess
4659              redo B;              next B;
4660        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4661          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4662            !!!cp ('t149.1');            !!!cp ('t149.1');
4663    
4664            ## NOTE: As if <head>            ## NOTE: As if <head>
4665            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4666            $self->{open_elements}->[-1]->[0]->append_child            $self->{open_elements}->[-1]->[0]->append_child
4667                ($self->{head_element});                ($self->{head_element});
4668            #push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            #push @{$self->{open_elements}},
4669              #    [$self->{head_element}, $el_category->{head}];
4670            #$self->{insertion_mode} = IN_HEAD_IM;            #$self->{insertion_mode} = IN_HEAD_IM;
4671            ## NOTE: Reprocess.            ## NOTE: Reprocess.
4672    
# Line 3841  sub _tree_construction_main ($) { Line 4688  sub _tree_construction_main ($) {
4688          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4689            !!!cp ('t149.3');            !!!cp ('t149.3');
4690    
4691            !!!parse-error (type => 'in noscript:#eof');            !!!parse-error (type => 'in noscript:#eof', token => $token);
4692    
4693            ## As if </noscript>            ## As if </noscript>
4694            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
# Line 3860  sub _tree_construction_main ($) { Line 4707  sub _tree_construction_main ($) {
4707          }          }
4708    
4709          ## NOTE: As if <body>          ## NOTE: As if <body>
4710          !!!insert-element ('body');          !!!insert-element ('body',, $token);
4711          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4712          ## NOTE: Reprocess.          ## NOTE: Reprocess.
4713          redo B;          next B;
4714        } else {        } else {
4715          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
4716        }        }
# Line 3878  sub _tree_construction_main ($) { Line 4725  sub _tree_construction_main ($) {
4725              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4726    
4727              !!!next-token;              !!!next-token;
4728              redo B;              next B;
4729            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
4730              if ({              if ({
4731                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
# Line 3886  sub _tree_construction_main ($) { Line 4733  sub _tree_construction_main ($) {
4733                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4734                if ($self->{insertion_mode} == IN_CELL_IM) {                if ($self->{insertion_mode} == IN_CELL_IM) {
4735                  ## have an element in table scope                  ## have an element in table scope
4736                  my $tn;                  for (reverse 0..$#{$self->{open_elements}}) {
                 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
4737                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4738                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] & TABLE_CELL_EL) {
4739                      !!!cp ('t151');                      !!!cp ('t151');
4740                      $tn = $node->[1];  
4741                      last INSCOPE;                      ## Close the cell
4742                    } elsif ({                      !!!back-token; # <x>
4743                              table => 1, html => 1,                      $token = {type => END_TAG_TOKEN,
4744                             }->{$node->[1]}) {                                tag_name => $node->[0]->manakai_local_name,
4745                                  line => $token->{line},
4746                                  column => $token->{column}};
4747                        next B;
4748                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4749                      !!!cp ('t152');                      !!!cp ('t152');
4750                      last INSCOPE;                      ## ISSUE: This case can never be reached, maybe.
4751                    }                      last;
                 } # INSCOPE  
                   unless (defined $tn) {  
                     !!!cp ('t153');  
 ## TODO: This error type is wrong.  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
4752                    }                    }
4753                                    }
4754                  !!!cp ('t154');  
4755                  ## Close the cell                  !!!cp ('t153');
4756                  !!!back-token; # <?>                  !!!parse-error (type => 'start tag not allowed',
4757                  $token = {type => END_TAG_TOKEN, tag_name => $tn};                      value => $token->{tag_name}, token => $token);
4758                  redo B;                  ## Ignore the token
4759                    !!!nack ('t153.1');
4760                    !!!next-token;
4761                    next B;
4762                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4763                  !!!parse-error (type => 'not closed:caption');                  !!!parse-error (type => 'not closed:caption', token => $token);
4764                                    
4765                  ## As if </caption>                  ## NOTE: As if </caption>.
4766                  ## have a table element in table scope                  ## have a table element in table scope
4767                  my $i;                  my $i;
4768                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4769                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4770                    if ($node->[1] eq 'caption') {                      my $node = $self->{open_elements}->[$_];
4771                      !!!cp ('t155');                      if ($node->[1] & CAPTION_EL) {
4772                      $i = $_;                        !!!cp ('t155');
4773                      last INSCOPE;                        $i = $_;
4774                    } elsif ({                        last INSCOPE;
4775                              table => 1, html => 1,                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4776                             }->{$node->[1]}) {                        !!!cp ('t156');
4777                      !!!cp ('t156');                        last;
4778                      last INSCOPE;                      }
4779                    }                    }
4780    
4781                      !!!cp ('t157');
4782                      !!!parse-error (type => 'start tag not allowed',
4783                                      value => $token->{tag_name}, token => $token);
4784                      ## Ignore the token
4785                      !!!nack ('t157.1');
4786                      !!!next-token;
4787                      next B;
4788                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!cp ('t157');  
 ## TODO: this type is wrong.  
                     !!!parse-error (type => 'unmatched end tag:caption');  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
4789                                    
4790                  ## generate implied end tags                  ## generate implied end tags
4791                  while ({                  while ($self->{open_elements}->[-1]->[1]
4792                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4793                    !!!cp ('t158');                    !!!cp ('t158');
4794                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4795                  }                  }
4796    
4797                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4798                    !!!cp ('t159');                    !!!cp ('t159');
4799                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
4800                                      value => $self->{open_elements}->[-1]->[0]
4801                                          ->manakai_local_name,
4802                                      token => $token);
4803                  } else {                  } else {
4804                    !!!cp ('t160');                    !!!cp ('t160');
4805                  }                  }
# Line 3964  sub _tree_construction_main ($) { Line 4811  sub _tree_construction_main ($) {
4811                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4812                                    
4813                  ## reprocess                  ## reprocess
4814                  redo B;                  !!!ack-later;
4815                    next B;
4816                } else {                } else {
4817                  !!!cp ('t161');                  !!!cp ('t161');
4818                  #                  #
# Line 3980  sub _tree_construction_main ($) { Line 4828  sub _tree_construction_main ($) {
4828                  my $i;                  my $i;
4829                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4830                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4831                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4832                      !!!cp ('t163');                      !!!cp ('t163');
4833                      $i = $_;                      $i = $_;
4834                      last INSCOPE;                      last INSCOPE;
4835                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
4836                      !!!cp ('t164');                      !!!cp ('t164');
4837                      last INSCOPE;                      last INSCOPE;
4838                    }                    }
4839                  } # INSCOPE                  } # INSCOPE
4840                    unless (defined $i) {                    unless (defined $i) {
4841                      !!!cp ('t165');                      !!!cp ('t165');
4842                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4843                      ## Ignore the token                      ## Ignore the token
4844                      !!!next-token;                      !!!next-token;
4845                      redo B;                      next B;
4846                    }                    }
4847                                    
4848                  ## generate implied end tags                  ## generate implied end tags
4849                  while ({                  while ($self->{open_elements}->[-1]->[1]
4850                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4851                    !!!cp ('t166');                    !!!cp ('t166');
4852                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4853                  }                  }
4854    
4855                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
4856                            ne $token->{tag_name}) {
4857                    !!!cp ('t167');                    !!!cp ('t167');
4858                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
4859                                      value => $self->{open_elements}->[-1]->[0]
4860                                          ->manakai_local_name,
4861                                      token => $token);
4862                  } else {                  } else {
4863                    !!!cp ('t168');                    !!!cp ('t168');
4864                  }                  }
# Line 4021  sub _tree_construction_main ($) { Line 4870  sub _tree_construction_main ($) {
4870                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
4871                                    
4872                  !!!next-token;                  !!!next-token;
4873                  redo B;                  next B;
4874                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4875                  !!!cp ('t169');                  !!!cp ('t169');
4876                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4877                  ## Ignore the token                  ## Ignore the token
4878                  !!!next-token;                  !!!next-token;
4879                  redo B;                  next B;
4880                } else {                } else {
4881                  !!!cp ('t170');                  !!!cp ('t170');
4882                  #                  #
# Line 4036  sub _tree_construction_main ($) { Line 4885  sub _tree_construction_main ($) {
4885                if ($self->{insertion_mode} == IN_CAPTION_IM) {                if ($self->{insertion_mode} == IN_CAPTION_IM) {
4886                  ## have a table element in table scope                  ## have a table element in table scope
4887                  my $i;                  my $i;
4888                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4889                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4890                    if ($node->[1] eq $token->{tag_name}) {                      my $node = $self->{open_elements}->[$_];
4891                      !!!cp ('t171');                      if ($node->[1] & CAPTION_EL) {
4892                      $i = $_;                        !!!cp ('t171');
4893                      last INSCOPE;                        $i = $_;
4894                    } elsif ({                        last INSCOPE;
4895                              table => 1, html => 1,                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4896                             }->{$node->[1]}) {                        !!!cp ('t172');
4897                      !!!cp ('t172');                        last;
4898                      last INSCOPE;                      }
4899                    }                    }
4900    
4901                      !!!cp ('t173');
4902                      !!!parse-error (type => 'unmatched end tag',
4903                                      value => $token->{tag_name}, token => $token);
4904                      ## Ignore the token
4905                      !!!next-token;
4906                      next B;
4907                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!cp ('t173');  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
4908                                    
4909                  ## generate implied end tags                  ## generate implied end tags
4910                  while ({                  while ($self->{open_elements}->[-1]->[1]
4911                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4912                    !!!cp ('t174');                    !!!cp ('t174');
4913                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4914                  }                  }
4915                                    
4916                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4917                    !!!cp ('t175');                    !!!cp ('t175');
4918                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
4919                                      value => $self->{open_elements}->[-1]->[0]
4920                                          ->manakai_local_name,
4921                                      token => $token);
4922                  } else {                  } else {
4923                    !!!cp ('t176');                    !!!cp ('t176');
4924                  }                  }
# Line 4079  sub _tree_construction_main ($) { Line 4930  sub _tree_construction_main ($) {
4930                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4931                                    
4932                  !!!next-token;                  !!!next-token;
4933                  redo B;                  next B;
4934                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
4935                  !!!cp ('t177');                  !!!cp ('t177');
4936                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4937                  ## Ignore the token                  ## Ignore the token
4938                  !!!next-token;                  !!!next-token;
4939                  redo B;                  next B;
4940                } else {                } else {
4941                  !!!cp ('t178');                  !!!cp ('t178');
4942                  #                  #
# Line 4098  sub _tree_construction_main ($) { Line 4949  sub _tree_construction_main ($) {
4949                ## have an element in table scope                ## have an element in table scope
4950                my $i;                my $i;
4951                my $tn;                my $tn;
4952                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: {
4953                  my $node = $self->{open_elements}->[$_];                  for (reverse 0..$#{$self->{open_elements}}) {
4954                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
4955                    !!!cp ('t179');                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4956                    $i = $_;                      !!!cp ('t179');
4957                    last INSCOPE;                      $i = $_;
4958                  } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {  
4959                    !!!cp ('t180');                      ## Close the cell
4960                    $tn = $node->[1];                      !!!back-token; # </x>
4961                    ## NOTE: There is exactly one |td| or |th| element                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
4962                    ## in scope in the stack of open elements by definition.                                line => $token->{line},
4963                  } elsif ({                                column => $token->{column}};
4964                            table => 1, html => 1,                      next B;
4965                           }->{$node->[1]}) {                    } elsif ($node->[1] & TABLE_CELL_EL) {
4966                    !!!cp ('t181');                      !!!cp ('t180');
4967                    last INSCOPE;                      $tn = $node->[0]->manakai_local_name;
4968                        ## NOTE: There is exactly one |td| or |th| element
4969                        ## in scope in the stack of open elements by definition.
4970                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4971                        ## ISSUE: Can this be reached?
4972                        !!!cp ('t181');
4973                        last;
4974                      }
4975                  }                  }
4976                } # INSCOPE  
               unless (defined $i) {  
4977                  !!!cp ('t182');                  !!!cp ('t182');
4978                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
4979                        value => $token->{tag_name}, token => $token);
4980                  ## Ignore the token                  ## Ignore the token
4981                  !!!next-token;                  !!!next-token;
4982                  redo B;                  next B;
4983                } else {                } # INSCOPE
                 !!!cp ('t183');  
               }  
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => END_TAG_TOKEN, tag_name => $tn};  
               redo B;  
4984              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
4985                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4986                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption', token => $token);
4987    
4988                ## As if </caption>                ## As if </caption>
4989                ## have a table element in table scope                ## have a table element in table scope
4990                my $i;                my $i;
4991                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4992                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4993                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
4994                    !!!cp ('t184');                    !!!cp ('t184');
4995                    $i = $_;                    $i = $_;
4996                    last INSCOPE;                    last INSCOPE;
4997                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
4998                    !!!cp ('t185');                    !!!cp ('t185');
4999                    last INSCOPE;                    last INSCOPE;
5000                  }                  }
5001                } # INSCOPE                } # INSCOPE
5002                unless (defined $i) {                unless (defined $i) {
5003                  !!!cp ('t186');                  !!!cp ('t186');
5004                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!parse-error (type => 'unmatched end tag:caption', token => $token);
5005                  ## Ignore the token                  ## Ignore the token
5006                  !!!next-token;                  !!!next-token;
5007                  redo B;                  next B;
5008                }                }
5009                                
5010                ## generate implied end tags                ## generate implied end tags
5011                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
5012                  !!!cp ('t187');                  !!!cp ('t187');
5013                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5014                }                }
5015    
5016                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5017                  !!!cp ('t188');                  !!!cp ('t188');
5018                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed',
5019                                    value => $self->{open_elements}->[-1]->[0]
5020                                        ->manakai_local_name,
5021                                    token => $token);
5022                } else {                } else {
5023                  !!!cp ('t189');                  !!!cp ('t189');
5024                }                }
# Line 4180  sub _tree_construction_main ($) { Line 5030  sub _tree_construction_main ($) {
5030                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5031    
5032                ## reprocess                ## reprocess
5033                redo B;                next B;
5034              } elsif ({              } elsif ({
5035                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
5036                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5037                if ($self->{insertion_mode} & BODY_TABLE_IMS) {                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
5038                  !!!cp ('t190');                  !!!cp ('t190');
5039                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5040                  ## Ignore the token                  ## Ignore the token
5041                  !!!next-token;                  !!!next-token;
5042                  redo B;                  next B;
5043                } else {                } else {
5044                  !!!cp ('t191');                  !!!cp ('t191');
5045                  #                  #
# Line 4200  sub _tree_construction_main ($) { Line 5050  sub _tree_construction_main ($) {
5050                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
5051                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5052                !!!cp ('t192');                !!!cp ('t192');
5053                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5054                ## Ignore the token                ## Ignore the token
5055                !!!next-token;                !!!next-token;
5056                redo B;                next B;
5057              } else {              } else {
5058                !!!cp ('t193');                !!!cp ('t193');
5059                #                #
5060              }              }
5061        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5062          for my $entry (@{$self->{open_elements}}) {          for my $entry (@{$self->{open_elements}}) {
5063            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]}) {  
5064              !!!cp ('t75');              !!!cp ('t75');
5065              !!!parse-error (type => 'in body:#eof');              !!!parse-error (type => 'in body:#eof', token => $token);
5066              last;              last;
5067            }            }
5068          }          }
# Line 4237  sub _tree_construction_main ($) { Line 5084  sub _tree_construction_main ($) {
5084            unless (length $token->{data}) {            unless (length $token->{data}) {
5085              !!!cp ('t194');              !!!cp ('t194');
5086              !!!next-token;              !!!next-token;
5087              redo B;              next B;
5088            } else {            } else {
5089              !!!cp ('t195');              !!!cp ('t195');
5090            }            }
5091          }          }
5092    
5093              !!!parse-error (type => 'in table:#character');              !!!parse-error (type => 'in table:#character', token => $token);
5094    
5095              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
5096              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
# Line 4251  sub _tree_construction_main ($) { Line 5098  sub _tree_construction_main ($) {
5098              ## result in a new Text node.              ## result in a new Text node.
5099              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
5100                            
5101              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]}) {  
5102                # MUST                # MUST
5103                my $foster_parent_element;                my $foster_parent_element;
5104                my $next_sibling;                my $next_sibling;
5105                my $prev_sibling;                my $prev_sibling;
5106                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
5107                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
5108                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
5109                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
5110                      !!!cp ('t196');                      !!!cp ('t196');
# Line 4295  sub _tree_construction_main ($) { Line 5139  sub _tree_construction_main ($) {
5139          }          }
5140                            
5141          !!!next-token;          !!!next-token;
5142          redo B;          next B;
5143        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5144              if ({              if ({
5145                   tr => ($self->{insertion_mode} != IN_ROW_IM),                   tr => ($self->{insertion_mode} != IN_ROW_IM),
# Line 4303  sub _tree_construction_main ($) { Line 5147  sub _tree_construction_main ($) {
5147                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
5148                if ($self->{insertion_mode} == IN_TABLE_IM) {                if ($self->{insertion_mode} == IN_TABLE_IM) {
5149                  ## Clear back to table context                  ## Clear back to table context
5150                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5151                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5152                    !!!cp ('t201');                    !!!cp ('t201');
5153                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5154                  }                  }
5155                                    
5156                  !!!insert-element ('tbody');                  !!!insert-element ('tbody',, $token);
5157                  $self->{insertion_mode} = IN_TABLE_BODY_IM;                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5158                  ## reprocess in the "in table body" insertion mode...                  ## reprocess in the "in table body" insertion mode...
5159                }                }
# Line 4317  sub _tree_construction_main ($) { Line 5161  sub _tree_construction_main ($) {
5161                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5162                  unless ($token->{tag_name} eq 'tr') {                  unless ($token->{tag_name} eq 'tr') {
5163                    !!!cp ('t202');                    !!!cp ('t202');
5164                    !!!parse-error (type => 'missing start tag:tr');                    !!!parse-error (type => 'missing start tag:tr', token => $token);
5165                  }                  }
5166                                    
5167                  ## Clear back to table body context                  ## Clear back to table body context
5168                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5169                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5170                    !!!cp ('t203');                    !!!cp ('t203');
5171                    ## ISSUE: Can this case be reached?                    ## ISSUE: Can this case be reached?
5172                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4332  sub _tree_construction_main ($) { Line 5175  sub _tree_construction_main ($) {
5175                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
5176                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5177                    !!!cp ('t204');                    !!!cp ('t204');
5178                    !!!insert-element ($token->{tag_name}, $token->{attributes});                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5179                      !!!nack ('t204');
5180                    !!!next-token;                    !!!next-token;
5181                    redo B;                    next B;
5182                  } else {                  } else {
5183                    !!!cp ('t205');                    !!!cp ('t205');
5184                    !!!insert-element ('tr');                    !!!insert-element ('tr',, $token);
5185                    ## reprocess in the "in row" insertion mode                    ## reprocess in the "in row" insertion mode
5186                  }                  }
5187                } else {                } else {
# Line 4345  sub _tree_construction_main ($) { Line 5189  sub _tree_construction_main ($) {
5189                }                }
5190    
5191                ## Clear back to table row context                ## Clear back to table row context
5192                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5193                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5194                  !!!cp ('t207');                  !!!cp ('t207');
5195                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5196                }                }
5197                                
5198                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5199                $self->{insertion_mode} = IN_CELL_IM;                $self->{insertion_mode} = IN_CELL_IM;
5200    
5201                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
5202                                
5203                  !!!nack ('t207.1');
5204                !!!next-token;                !!!next-token;
5205                redo B;                next B;
5206              } elsif ({              } elsif ({
5207                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5208                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
# Line 4370  sub _tree_construction_main ($) { Line 5214  sub _tree_construction_main ($) {
5214                  my $i;                  my $i;
5215                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5216                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5217                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5218                      !!!cp ('t208');                      !!!cp ('t208');
5219                      $i = $_;                      $i = $_;
5220                      last INSCOPE;                      last INSCOPE;
5221                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             html => 1,  
   
                             ## NOTE: This element does not appear here, maybe.  
                             table => 1,  
                            }->{$node->[1]}) {  
5222                      !!!cp ('t209');                      !!!cp ('t209');
5223                      last INSCOPE;                      last INSCOPE;
5224                    }                    }
5225                  } # INSCOPE                  } # INSCOPE
5226                  unless (defined $i) {                  unless (defined $i) {
5227                   !!!cp ('t210');                    !!!cp ('t210');
5228  ## TODO: This type is wrong.  ## TODO: This type is wrong.
5229                   !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);
5230                    ## Ignore the token                    ## Ignore the token
5231                      !!!nack ('t210.1');
5232                    !!!next-token;                    !!!next-token;
5233                    redo B;                    next B;
5234                  }                  }
5235                                    
5236                  ## Clear back to table row context                  ## Clear back to table row context
5237                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5238                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5239                    !!!cp ('t211');                    !!!cp ('t211');
5240                    ## ISSUE: Can this case be reached?                    ## ISSUE: Can this case be reached?
5241                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4407  sub _tree_construction_main ($) { Line 5246  sub _tree_construction_main ($) {
5246                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5247                    !!!cp ('t212');                    !!!cp ('t212');
5248                    ## reprocess                    ## reprocess
5249                    redo B;                    !!!ack-later;
5250                      next B;
5251                  } else {                  } else {
5252                    !!!cp ('t213');                    !!!cp ('t213');
5253                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
# Line 4419  sub _tree_construction_main ($) { Line 5259  sub _tree_construction_main ($) {
5259                  my $i;                  my $i;
5260                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5261                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5262                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5263                      !!!cp ('t214');                      !!!cp ('t214');
5264                      $i = $_;                      $i = $_;
5265                      last INSCOPE;                      last INSCOPE;
5266                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5267                      !!!cp ('t215');                      !!!cp ('t215');
5268                      last INSCOPE;                      last INSCOPE;
5269                    }                    }
# Line 4435  sub _tree_construction_main ($) { Line 5271  sub _tree_construction_main ($) {
5271                  unless (defined $i) {                  unless (defined $i) {
5272                    !!!cp ('t216');                    !!!cp ('t216');
5273  ## TODO: This erorr type ios wrong.  ## TODO: This erorr type ios wrong.
5274                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5275                    ## Ignore the token                    ## Ignore the token
5276                      !!!nack ('t216.1');
5277                    !!!next-token;                    !!!next-token;
5278                    redo B;                    next B;
5279                  }                  }
5280    
5281                  ## Clear back to table body context                  ## Clear back to table body context
5282                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5283                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5284                    !!!cp ('t217');                    !!!cp ('t217');
5285                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5286                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4466  sub _tree_construction_main ($) { Line 5302  sub _tree_construction_main ($) {
5302    
5303                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
5304                  ## Clear back to table context                  ## Clear back to table context
5305                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5306                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5307                    !!!cp ('t219');                    !!!cp ('t219');
5308                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5309                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5310                  }                  }
5311                                    
5312                  !!!insert-element ('colgroup');                  !!!insert-element ('colgroup',, $token);
5313                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5314                  ## reprocess                  ## reprocess
5315                  redo B;                  !!!ack-later;
5316                    next B;
5317                } elsif ({                } elsif ({
5318                          caption => 1,                          caption => 1,
5319                          colgroup => 1,                          colgroup => 1,
5320                          tbody => 1, tfoot => 1, thead => 1,                          tbody => 1, tfoot => 1, thead => 1,
5321                         }->{$token->{tag_name}}) {                         }->{$token->{tag_name}}) {
5322                  ## Clear back to table context                  ## Clear back to table context
5323                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5324                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5325                    !!!cp ('t220');                    !!!cp ('t220');
5326                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5327                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4493  sub _tree_construction_main ($) { Line 5330  sub _tree_construction_main ($) {
5330                  push @$active_formatting_elements, ['#marker', '']                  push @$active_formatting_elements, ['#marker', '']
5331                      if $token->{tag_name} eq 'caption';                      if $token->{tag_name} eq 'caption';
5332                                    
5333                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5334                  $self->{insertion_mode} = {                  $self->{insertion_mode} = {
5335                                             caption => IN_CAPTION_IM,                                             caption => IN_CAPTION_IM,
5336                                             colgroup => IN_COLUMN_GROUP_IM,                                             colgroup => IN_COLUMN_GROUP_IM,
# Line 4502  sub _tree_construction_main ($) { Line 5339  sub _tree_construction_main ($) {
5339                                             thead => IN_TABLE_BODY_IM,                                             thead => IN_TABLE_BODY_IM,
5340                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
5341                  !!!next-token;                  !!!next-token;
5342                  redo B;                  !!!nack ('t220.1');
5343                    next B;
5344                } else {                } else {
5345                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
5346                }                }
5347              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5348                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
5349                                  value => $self->{open_elements}->[-1]->[0]
5350                                      ->manakai_local_name,
5351                                  token => $token);
5352    
5353                ## As if </table>                ## As if </table>
5354                ## have a table element in table scope                ## have a table element in table scope
5355                my $i;                my $i;
5356                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5357                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5358                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5359                    !!!cp ('t221');                    !!!cp ('t221');
5360                    $i = $_;                    $i = $_;
5361                    last INSCOPE;                    last INSCOPE;
5362                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           #table => 1,  
                           html => 1,  
                          }->{$node->[1]}) {  
5363                    !!!cp ('t222');                    !!!cp ('t222');
5364                    last INSCOPE;                    last INSCOPE;
5365                  }                  }
# Line 4529  sub _tree_construction_main ($) { Line 5367  sub _tree_construction_main ($) {
5367                unless (defined $i) {                unless (defined $i) {
5368                  !!!cp ('t223');                  !!!cp ('t223');
5369  ## TODO: The following is wrong, maybe.  ## TODO: The following is wrong, maybe.
5370                  !!!parse-error (type => 'unmatched end tag:table');                  !!!parse-error (type => 'unmatched end tag:table', token => $token);
5371                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5372                    !!!nack ('t223.1');
5373                  !!!next-token;                  !!!next-token;
5374                  redo B;                  next B;
5375                }                }
5376                                
5377    ## TODO: Followings are removed from the latest spec.
5378                ## generate implied end tags                ## generate implied end tags
5379                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
5380                  !!!cp ('t224');                  !!!cp ('t224');
5381                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5382                }                }
5383    
5384                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5385                  !!!cp ('t225');                  !!!cp ('t225');
5386  ## ISSUE: Can this case be reached?                  ## NOTE: |<table><tr><table>|
5387                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed',
5388                                    value => $self->{open_elements}->[-1]->[0]
5389                                        ->manakai_local_name,
5390                                    token => $token);
5391                } else {                } else {
5392                  !!!cp ('t226');                  !!!cp ('t226');
5393                }                }
# Line 4556  sub _tree_construction_main ($) { Line 5397  sub _tree_construction_main ($) {
5397    
5398                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5399    
5400                ## reprocess            ## reprocess
5401                redo B;            !!!ack-later;
5402              next B;
5403          } elsif ($token->{tag_name} eq 'style') {          } elsif ($token->{tag_name} eq 'style') {
5404            if (not $open_tables->[-1]->[1]) { # tainted            if (not $open_tables->[-1]->[1]) { # tainted
5405              !!!cp ('t227.8');              !!!cp ('t227.8');
5406              ## NOTE: This is a "as if in head" code clone.              ## NOTE: This is a "as if in head" code clone.
5407              $parse_rcdata->(CDATA_CONTENT_MODEL);              $parse_rcdata->(CDATA_CONTENT_MODEL);
5408              redo B;              next B;
5409            } else {            } else {
5410              !!!cp ('t227.7');              !!!cp ('t227.7');
5411              #              #
# Line 4573  sub _tree_construction_main ($) { Line 5415  sub _tree_construction_main ($) {
5415              !!!cp ('t227.6');              !!!cp ('t227.6');
5416              ## NOTE: This is a "as if in head" code clone.              ## NOTE: This is a "as if in head" code clone.
5417              $script_start_tag->();              $script_start_tag->();
5418              redo B;              next B;
5419            } else {            } else {
5420              !!!cp ('t227.5');              !!!cp ('t227.5');
5421              #              #
# Line 4584  sub _tree_construction_main ($) { Line 5426  sub _tree_construction_main ($) {
5426                my $type = lc $token->{attributes}->{type}->{value};                my $type = lc $token->{attributes}->{type}->{value};
5427                if ($type eq 'hidden') {                if ($type eq 'hidden') {
5428                  !!!cp ('t227.3');                  !!!cp ('t227.3');
5429                  !!!parse-error (type => 'in table:'.$token->{tag_name});                  !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
5430    
5431                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5432    
5433                  ## TODO: form element pointer                  ## TODO: form element pointer
5434    
5435                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5436    
5437                  !!!next-token;                  !!!next-token;
5438                  redo B;                  !!!ack ('t227.2.1');
5439                    next B;
5440                } else {                } else {
5441                  !!!cp ('t227.2');                  !!!cp ('t227.2');
5442                  #                  #
# Line 4611  sub _tree_construction_main ($) { Line 5454  sub _tree_construction_main ($) {
5454            #            #
5455          }          }
5456    
5457          !!!parse-error (type => 'in table:'.$token->{tag_name});          !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
5458    
5459          $insert = $insert_to_foster;          $insert = $insert_to_foster;
5460          #          #
# Line 4622  sub _tree_construction_main ($) { Line 5465  sub _tree_construction_main ($) {
5465                my $i;                my $i;
5466                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5467                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5468                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_ROW_EL) {
5469                    !!!cp ('t228');                    !!!cp ('t228');
5470                    $i = $_;                    $i = $_;
5471                    last INSCOPE;                    last INSCOPE;
5472                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5473                    !!!cp ('t229');                    !!!cp ('t229');
5474                    last INSCOPE;                    last INSCOPE;
5475                  }                  }
5476                } # INSCOPE                } # INSCOPE
5477                unless (defined $i) {                unless (defined $i) {
5478                  !!!cp ('t230');                  !!!cp ('t230');
5479                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5480                  ## Ignore the token                  ## Ignore the token
5481                    !!!nack ('t230.1');
5482                  !!!next-token;                  !!!next-token;
5483                  redo B;                  next B;
5484                } else {                } else {
5485                  !!!cp ('t232');                  !!!cp ('t232');
5486                }                }
5487    
5488                ## Clear back to table row context                ## Clear back to table row context
5489                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5490                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5491                  !!!cp ('t231');                  !!!cp ('t231');
5492  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
5493                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 4655  sub _tree_construction_main ($) { Line 5496  sub _tree_construction_main ($) {
5496                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5497                $self->{insertion_mode} = IN_TABLE_BODY_IM;                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5498                !!!next-token;                !!!next-token;
5499                redo B;                !!!nack ('t231.1');
5500                  next B;
5501              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5502                if ($self->{insertion_mode} == IN_ROW_IM) {                if ($self->{insertion_mode} == IN_ROW_IM) {
5503                  ## As if </tr>                  ## As if </tr>
# Line 4663  sub _tree_construction_main ($) { Line 5505  sub _tree_construction_main ($) {
5505                  my $i;                  my $i;
5506                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5507                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5508                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5509                      !!!cp ('t233');                      !!!cp ('t233');
5510                      $i = $_;                      $i = $_;
5511                      last INSCOPE;                      last INSCOPE;
5512                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5513                      !!!cp ('t234');                      !!!cp ('t234');
5514                      last INSCOPE;                      last INSCOPE;
5515                    }                    }
# Line 4677  sub _tree_construction_main ($) { Line 5517  sub _tree_construction_main ($) {
5517                  unless (defined $i) {                  unless (defined $i) {
5518                    !!!cp ('t235');                    !!!cp ('t235');
5519  ## TODO: The following is wrong.  ## TODO: The following is wrong.
5520                    !!!parse-error (type => 'unmatched end tag:'.$token->{type});                    !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);
5521                    ## Ignore the token                    ## Ignore the token
5522                      !!!nack ('t236.1');
5523                    !!!next-token;                    !!!next-token;
5524                    redo B;                    next B;
5525                  }                  }
5526                                    
5527                  ## Clear back to table row context                  ## Clear back to table row context
5528                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5529                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5530                    !!!cp ('t236');                    !!!cp ('t236');
5531  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
5532                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4702  sub _tree_construction_main ($) { Line 5542  sub _tree_construction_main ($) {
5542                  my $i;                  my $i;
5543                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5544                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5545                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5546                      !!!cp ('t237');                      !!!cp ('t237');
5547                      $i = $_;                      $i = $_;
5548                      last INSCOPE;                      last INSCOPE;
5549                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5550                      !!!cp ('t238');                      !!!cp ('t238');
5551                      last INSCOPE;                      last INSCOPE;
5552                    }                    }
5553                  } # INSCOPE                  } # INSCOPE
5554                  unless (defined $i) {                  unless (defined $i) {
5555                    !!!cp ('t239');                    !!!cp ('t239');
5556                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5557                    ## Ignore the token                    ## Ignore the token
5558                      !!!nack ('t239.1');
5559                    !!!next-token;                    !!!next-token;
5560                    redo B;                    next B;
5561                  }                  }
5562                                    
5563                  ## Clear back to table body context                  ## Clear back to table body context
5564                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5565                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5566                    !!!cp ('t240');                    !!!cp ('t240');
5567                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5568                  }                  }
# Line 4752  sub _tree_construction_main ($) { Line 5588  sub _tree_construction_main ($) {
5588                my $i;                my $i;
5589                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5590                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5591                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_EL) {
5592                    !!!cp ('t241');                    !!!cp ('t241');
5593                    $i = $_;                    $i = $_;
5594                    last INSCOPE;                    last INSCOPE;
5595                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5596                    !!!cp ('t242');                    !!!cp ('t242');
5597                    last INSCOPE;                    last INSCOPE;
5598                  }                  }
5599                } # INSCOPE                } # INSCOPE
5600                unless (defined $i) {                unless (defined $i) {
5601                  !!!cp ('t243');                  !!!cp ('t243');
5602                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5603                  ## Ignore the token                  ## Ignore the token
5604                    !!!nack ('t243.1');
5605                  !!!next-token;                  !!!next-token;
5606                  redo B;                  next B;
5607                }                }
5608                                    
5609                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
# Line 4777  sub _tree_construction_main ($) { Line 5612  sub _tree_construction_main ($) {
5612                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5613                                
5614                !!!next-token;                !!!next-token;
5615                redo B;                next B;
5616              } elsif ({              } elsif ({
5617                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5618                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
# Line 4787  sub _tree_construction_main ($) { Line 5622  sub _tree_construction_main ($) {
5622                  my $i;                  my $i;
5623                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5624                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5625                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5626                      !!!cp ('t247');                      !!!cp ('t247');
5627                      $i = $_;                      $i = $_;
5628                      last INSCOPE;                      last INSCOPE;
5629                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5630                      !!!cp ('t248');                      !!!cp ('t248');
5631                      last INSCOPE;                      last INSCOPE;
5632                    }                    }
5633                  } # INSCOPE                  } # INSCOPE
5634                    unless (defined $i) {                    unless (defined $i) {
5635                      !!!cp ('t249');                      !!!cp ('t249');
5636                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5637                      ## Ignore the token                      ## Ignore the token
5638                        !!!nack ('t249.1');
5639                      !!!next-token;                      !!!next-token;
5640                      redo B;                      next B;
5641                    }                    }
5642                                    
5643                  ## As if </tr>                  ## As if </tr>
# Line 4811  sub _tree_construction_main ($) { Line 5645  sub _tree_construction_main ($) {
5645                  my $i;                  my $i;
5646                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5647                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5648                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5649                      !!!cp ('t250');                      !!!cp ('t250');
5650                      $i = $_;                      $i = $_;
5651                      last INSCOPE;                      last INSCOPE;
5652                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5653                      !!!cp ('t251');                      !!!cp ('t251');
5654                      last INSCOPE;                      last INSCOPE;
5655                    }                    }
5656                  } # INSCOPE                  } # INSCOPE
5657                    unless (defined $i) {                    unless (defined $i) {
5658                      !!!cp ('t252');                      !!!cp ('t252');
5659                      !!!parse-error (type => 'unmatched end tag:tr');                      !!!parse-error (type => 'unmatched end tag:tr', token => $token);
5660                      ## Ignore the token                      ## Ignore the token
5661                        !!!nack ('t252.1');
5662                      !!!next-token;                      !!!next-token;
5663                      redo B;                      next B;
5664                    }                    }
5665                                    
5666                  ## Clear back to table row context                  ## Clear back to table row context
5667                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5668                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5669                    !!!cp ('t253');                    !!!cp ('t253');
5670  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
5671                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4848  sub _tree_construction_main ($) { Line 5680  sub _tree_construction_main ($) {
5680                my $i;                my $i;
5681                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5682                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5683                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5684                    !!!cp ('t254');                    !!!cp ('t254');
5685                    $i = $_;                    $i = $_;
5686                    last INSCOPE;                    last INSCOPE;
5687                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5688                    !!!cp ('t255');                    !!!cp ('t255');
5689                    last INSCOPE;                    last INSCOPE;
5690                  }                  }
5691                } # INSCOPE                } # INSCOPE
5692                unless (defined $i) {                unless (defined $i) {
5693                  !!!cp ('t256');                  !!!cp ('t256');
5694                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5695                  ## Ignore the token                  ## Ignore the token
5696                    !!!nack ('t256.1');
5697                  !!!next-token;                  !!!next-token;
5698                  redo B;                  next B;
5699                }                }
5700    
5701                ## Clear back to table body context                ## Clear back to table body context
5702                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5703                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5704                  !!!cp ('t257');                  !!!cp ('t257');
5705  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
5706                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 4878  sub _tree_construction_main ($) { Line 5708  sub _tree_construction_main ($) {
5708    
5709                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5710                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5711                  !!!nack ('t257.1');
5712                !!!next-token;                !!!next-token;
5713                redo B;                next B;
5714              } elsif ({              } elsif ({
5715                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
5716                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
5717                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5718                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
5719                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5720                !!!cp ('t258');            !!!cp ('t258');
5721                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5722                ## Ignore the token            ## Ignore the token
5723                !!!next-token;            !!!nack ('t258.1');
5724                redo B;             !!!next-token;
5725              next B;
5726          } else {          } else {
5727            !!!cp ('t259');            !!!cp ('t259');
5728            !!!parse-error (type => 'in table:/'.$token->{tag_name});            !!!parse-error (type => 'in table:/'.$token->{tag_name}, token => $token);
5729    
5730            $insert = $insert_to_foster;            $insert = $insert_to_foster;
5731            #            #
5732          }          }
5733        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5734          unless ($self->{open_elements}->[-1]->[1] eq 'html' and          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5735                  @{$self->{open_elements}} == 1) { # redundant, maybe                  @{$self->{open_elements}} == 1) { # redundant, maybe
5736              !!!parse-error (type => 'in body:#eof', token => $token);
5737            !!!cp ('t259.1');            !!!cp ('t259.1');
5738            !!!parse-error (type => 'in body:#eof');            #
5739          } else {          } else {
5740            !!!cp ('t259.2');            !!!cp ('t259.2');
5741              #
5742          }          }
5743    
5744          ## Stop parsing          ## Stop parsing
# Line 4919  sub _tree_construction_main ($) { Line 5753  sub _tree_construction_main ($) {
5753                unless (length $token->{data}) {                unless (length $token->{data}) {
5754                  !!!cp ('t260');                  !!!cp ('t260');
5755                  !!!next-token;                  !!!next-token;
5756                  redo B;                  next B;
5757                }                }
5758              }              }
5759                            
# Line 4928  sub _tree_construction_main ($) { Line 5762  sub _tree_construction_main ($) {
5762            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
5763              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
5764                !!!cp ('t262');                !!!cp ('t262');
5765                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5766                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5767                  !!!ack ('t262.1');
5768                !!!next-token;                !!!next-token;
5769                redo B;                next B;
5770              } else {              } else {
5771                !!!cp ('t263');                !!!cp ('t263');
5772                #                #
5773              }              }
5774            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
5775              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
5776                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5777                  !!!cp ('t264');                  !!!cp ('t264');
5778                  !!!parse-error (type => 'unmatched end tag:colgroup');                  !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5779                  ## Ignore the token                  ## Ignore the token
5780                  !!!next-token;                  !!!next-token;
5781                  redo B;                  next B;
5782                } else {                } else {
5783                  !!!cp ('t265');                  !!!cp ('t265');
5784                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
5785                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5786                  !!!next-token;                  !!!next-token;
5787                  redo B;                              next B;            
5788                }                }
5789              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
5790                !!!cp ('t266');                !!!cp ('t266');
5791                !!!parse-error (type => 'unmatched end tag:col');                !!!parse-error (type => 'unmatched end tag:col', token => $token);
5792                ## Ignore the token                ## Ignore the token
5793                !!!next-token;                !!!next-token;
5794                redo B;                next B;
5795              } else {              } else {
5796                !!!cp ('t267');                !!!cp ('t267');
5797                #                #
5798              }              }
5799        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5800          if ($self->{open_elements}->[-1]->[1] eq 'html' or          if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5801              @{$self->{open_elements}} == 1) { # redundant, maybe              @{$self->{open_elements}} == 1) { # redundant, maybe
5802            !!!cp ('t270.2');            !!!cp ('t270.2');
5803            ## Stop parsing.            ## Stop parsing.
# Line 4973  sub _tree_construction_main ($) { Line 5808  sub _tree_construction_main ($) {
5808            pop @{$self->{open_elements}}; # colgroup            pop @{$self->{open_elements}}; # colgroup
5809            $self->{insertion_mode} = IN_TABLE_IM;            $self->{insertion_mode} = IN_TABLE_IM;
5810            ## Reprocess.            ## Reprocess.
5811            redo B;            next B;
5812          }          }
5813        } else {        } else {
5814          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5815        }        }
5816    
5817            ## As if </colgroup>            ## As if </colgroup>
5818            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5819              !!!cp ('t269');              !!!cp ('t269');
5820  ## TODO: Wrong error type?  ## TODO: Wrong error type?
5821              !!!parse-error (type => 'unmatched end tag:colgroup');              !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5822              ## Ignore the token              ## Ignore the token
5823                !!!nack ('t269.1');
5824              !!!next-token;              !!!next-token;
5825              redo B;              next B;
5826            } else {            } else {
5827              !!!cp ('t270');              !!!cp ('t270');
5828              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
5829              $self->{insertion_mode} = IN_TABLE_IM;              $self->{insertion_mode} = IN_TABLE_IM;
5830                !!!ack-later;
5831              ## reprocess              ## reprocess
5832              redo B;              next B;
5833            }            }
5834      } elsif ($self->{insertion_mode} & SELECT_IMS) {      } elsif ($self->{insertion_mode} & SELECT_IMS) {
5835        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5836          !!!cp ('t271');          !!!cp ('t271');
5837          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5838          !!!next-token;          !!!next-token;
5839          redo B;          next B;
5840        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5841              if ($token->{tag_name} eq 'option') {          if ($token->{tag_name} eq 'option') {
5842                if ($self->{open_elements}->[-1]->[1] eq 'option') {            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5843                  !!!cp ('t272');              !!!cp ('t272');
5844                  ## As if </option>              ## As if </option>
5845                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5846                } else {            } else {
5847                  !!!cp ('t273');              !!!cp ('t273');
5848                }            }
5849    
5850                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5851                !!!next-token;            !!!nack ('t273.1');
5852                redo B;            !!!next-token;
5853              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
5854                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
5855                  !!!cp ('t274');            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5856                  ## As if </option>              !!!cp ('t274');
5857                  pop @{$self->{open_elements}};              ## As if </option>
5858                } else {              pop @{$self->{open_elements}};
5859                  !!!cp ('t275');            } else {
5860                }              !!!cp ('t275');
5861              }
5862    
5863                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5864                  !!!cp ('t276');              !!!cp ('t276');
5865                  ## As if </optgroup>              ## As if </optgroup>
5866                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5867                } else {            } else {
5868                  !!!cp ('t277');              !!!cp ('t277');
5869                }            }
5870    
5871                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5872                !!!next-token;            !!!nack ('t277.1');
5873                redo B;            !!!next-token;
5874              next B;
5875          } elsif ($token->{tag_name} eq 'select' or          } elsif ($token->{tag_name} eq 'select' or
5876                   $token->{tag_name} eq 'input' or                   $token->{tag_name} eq 'input' or
5877                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
# Line 5042  sub _tree_construction_main ($) { Line 5881  sub _tree_construction_main ($) {
5881                     tr => 1, td => 1, th => 1,                     tr => 1, td => 1, th => 1,
5882                    }->{$token->{tag_name}})) {                    }->{$token->{tag_name}})) {
5883            ## TODO: The type below is not good - <select> is replaced by </select>            ## TODO: The type below is not good - <select> is replaced by </select>
5884            !!!parse-error (type => 'not closed:select');            !!!parse-error (type => 'not closed:select', token => $token);
5885            ## NOTE: As if the token were </select> (<select> case) or            ## NOTE: As if the token were </select> (<select> case) or
5886            ## as if there were </select> (otherwise).            ## as if there were </select> (otherwise).
5887                ## have an element in table scope            ## have an element in table scope
5888                my $i;            my $i;
5889                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5890                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
5891                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
5892                    !!!cp ('t278');                !!!cp ('t278');
5893                    $i = $_;                $i = $_;
5894                    last INSCOPE;                last INSCOPE;
5895                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5896                            table => 1, html => 1,                !!!cp ('t279');
5897                           }->{$node->[1]}) {                last INSCOPE;
5898                    !!!cp ('t279');              }
5899                    last INSCOPE;            } # INSCOPE
5900                  }            unless (defined $i) {
5901                } # INSCOPE              !!!cp ('t280');
5902                unless (defined $i) {              !!!parse-error (type => 'unmatched end tag:select', token => $token);
5903                  !!!cp ('t280');              ## Ignore the token
5904                  !!!parse-error (type => 'unmatched end tag:select');              !!!nack ('t280.1');
5905                  ## Ignore the token              !!!next-token;
5906                  !!!next-token;              next B;
5907                  redo B;            }
               }  
5908                                
5909                !!!cp ('t281');            !!!cp ('t281');
5910                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
5911    
5912                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5913    
5914            if ($token->{tag_name} eq 'select') {            if ($token->{tag_name} eq 'select') {
5915              !!!cp ('t281.2');              !!!nack ('t281.2');
5916              !!!next-token;              !!!next-token;
5917              redo B;              next B;
5918            } else {            } else {
5919              !!!cp ('t281.1');              !!!cp ('t281.1');
5920                !!!ack-later;
5921              ## Reprocess the token.              ## Reprocess the token.
5922              redo B;              next B;
5923            }            }
5924          } else {          } else {
5925            !!!cp ('t282');            !!!cp ('t282');
5926            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);
5927            ## Ignore the token            ## Ignore the token
5928              !!!nack ('t282.1');
5929            !!!next-token;            !!!next-token;
5930            redo B;            next B;
5931          }          }
5932        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5933              if ($token->{tag_name} eq 'optgroup') {          if ($token->{tag_name} eq 'optgroup') {
5934                if ($self->{open_elements}->[-1]->[1] eq 'option' and            if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
5935                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
5936                  !!!cp ('t283');              !!!cp ('t283');
5937                  ## As if </option>              ## As if </option>
5938                  splice @{$self->{open_elements}}, -2;              splice @{$self->{open_elements}}, -2;
5939                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5940                  !!!cp ('t284');              !!!cp ('t284');
5941                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5942                } else {            } else {
5943                  !!!cp ('t285');              !!!cp ('t285');
5944                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5945                  ## Ignore the token              ## Ignore the token
5946                }            }
5947                !!!next-token;            !!!nack ('t285.1');
5948                redo B;            !!!next-token;
5949              } elsif ($token->{tag_name} eq 'option') {            next B;
5950                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'option') {
5951                  !!!cp ('t286');            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5952                  pop @{$self->{open_elements}};              !!!cp ('t286');
5953                } else {              pop @{$self->{open_elements}};
5954                  !!!cp ('t287');            } else {
5955                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t287');
5956                  ## Ignore the token              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5957                }              ## Ignore the token
5958                !!!next-token;            }
5959                redo B;            !!!nack ('t287.1');
5960              } elsif ($token->{tag_name} eq 'select') {            !!!next-token;
5961                ## have an element in table scope            next B;
5962                my $i;          } elsif ($token->{tag_name} eq 'select') {
5963                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            ## have an element in table scope
5964                  my $node = $self->{open_elements}->[$_];            my $i;
5965                  if ($node->[1] eq $token->{tag_name}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5966                    !!!cp ('t288');              my $node = $self->{open_elements}->[$_];
5967                    $i = $_;              if ($node->[1] & SELECT_EL) {
5968                    last INSCOPE;                !!!cp ('t288');
5969                  } elsif ({                $i = $_;
5970                            table => 1, html => 1,                last INSCOPE;
5971                           }->{$node->[1]}) {              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5972                    !!!cp ('t289');                !!!cp ('t289');
5973                    last INSCOPE;                last INSCOPE;
5974                  }              }
5975                } # INSCOPE            } # INSCOPE
5976                unless (defined $i) {            unless (defined $i) {
5977                  !!!cp ('t290');              !!!cp ('t290');
5978                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5979                  ## Ignore the token              ## Ignore the token
5980                  !!!next-token;              !!!nack ('t290.1');
5981                  redo B;              !!!next-token;
5982                }              next B;
5983              }
5984                                
5985                !!!cp ('t291');            !!!cp ('t291');
5986                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
5987    
5988                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5989    
5990                !!!next-token;            !!!nack ('t291.1');
5991                redo B;            !!!next-token;
5992              next B;
5993          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5994                   {                   {
5995                    caption => 1, table => 1, tbody => 1,                    caption => 1, table => 1, tbody => 1,
5996                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
5997                   }->{$token->{tag_name}}) {                   }->{$token->{tag_name}}) {
5998  ## TODO: The following is wrong?  ## TODO: The following is wrong?
5999                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6000                                
6001                ## have an element in table scope            ## have an element in table scope
6002                my $i;            my $i;
6003                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6004                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6005                  if ($node->[1] eq $token->{tag_name}) {              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6006                    !!!cp ('t292');                !!!cp ('t292');
6007                    $i = $_;                $i = $_;
6008                    last INSCOPE;                last INSCOPE;
6009                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6010                            table => 1, html => 1,                !!!cp ('t293');
6011                           }->{$node->[1]}) {                last INSCOPE;
6012                    !!!cp ('t293');              }
6013                    last INSCOPE;            } # INSCOPE
6014                  }            unless (defined $i) {
6015                } # INSCOPE              !!!cp ('t294');
6016                unless (defined $i) {              ## Ignore the token
6017                  !!!cp ('t294');              !!!nack ('t294.1');
6018                  ## Ignore the token              !!!next-token;
6019                  !!!next-token;              next B;
6020                  redo B;            }
               }  
6021                                
6022                ## As if </select>            ## As if </select>
6023                ## have an element in table scope            ## have an element in table scope
6024                undef $i;            undef $i;
6025                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6026                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6027                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
6028                    !!!cp ('t295');                !!!cp ('t295');
6029                    $i = $_;                $i = $_;
6030                    last INSCOPE;                last INSCOPE;
6031                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
6032  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
6033                    !!!cp ('t296');                !!!cp ('t296');
6034                    last INSCOPE;                last INSCOPE;
6035                  }              }
6036                } # INSCOPE            } # INSCOPE
6037                unless (defined $i) {            unless (defined $i) {
6038                  !!!cp ('t297');              !!!cp ('t297');
6039  ## TODO: The following error type is correct?  ## TODO: The following error type is correct?
6040                  !!!parse-error (type => 'unmatched end tag:select');              !!!parse-error (type => 'unmatched end tag:select', token => $token);
6041                  ## Ignore the </select> token              ## Ignore the </select> token
6042                  !!!next-token; ## TODO: ok?              !!!nack ('t297.1');
6043                  redo B;              !!!next-token; ## TODO: ok?
6044                }              next B;
6045              }
6046                                
6047                !!!cp ('t298');            !!!cp ('t298');
6048                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6049    
6050                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6051    
6052                ## reprocess            !!!ack-later;
6053                redo B;            ## reprocess
6054              next B;
6055          } else {          } else {
6056            !!!cp ('t299');            !!!cp ('t299');
6057            !!!parse-error (type => 'in select:/'.$token->{tag_name});            !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);
6058            ## Ignore the token            ## Ignore the token
6059              !!!nack ('t299.3');
6060            !!!next-token;            !!!next-token;
6061            redo B;            next B;
6062          }          }
6063        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6064          unless ($self->{open_elements}->[-1]->[1] eq 'html' and          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6065                  @{$self->{open_elements}} == 1) { # redundant, maybe                  @{$self->{open_elements}} == 1) { # redundant, maybe
6066            !!!cp ('t299.1');            !!!cp ('t299.1');
6067            !!!parse-error (type => 'in body:#eof');            !!!parse-error (type => 'in body:#eof', token => $token);
6068          } else {          } else {
6069            !!!cp ('t299.2');            !!!cp ('t299.2');
6070          }          }
# Line 5244  sub _tree_construction_main ($) { Line 6086  sub _tree_construction_main ($) {
6086            unless (length $token->{data}) {            unless (length $token->{data}) {
6087              !!!cp ('t300');              !!!cp ('t300');
6088              !!!next-token;              !!!next-token;
6089              redo B;              next B;
6090            }            }
6091          }          }
6092                    
6093          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6094            !!!cp ('t301');            !!!cp ('t301');
6095            !!!parse-error (type => 'after html:#character');            !!!parse-error (type => 'after html:#character', token => $token);
6096    
6097            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
6098          } else {          } else {
# Line 5258  sub _tree_construction_main ($) { Line 6100  sub _tree_construction_main ($) {
6100          }          }
6101                    
6102          ## "after body" insertion mode          ## "after body" insertion mode
6103          !!!parse-error (type => 'after body:#character');          !!!parse-error (type => 'after body:#character', token => $token);
6104    
6105          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6106          ## reprocess          ## reprocess
6107          redo B;          next B;
6108        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6109          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6110            !!!cp ('t303');            !!!cp ('t303');
6111            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
6112                        
6113            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
6114          } else {          } else {
# Line 5274  sub _tree_construction_main ($) { Line 6116  sub _tree_construction_main ($) {
6116          }          }
6117    
6118          ## "after body" insertion mode          ## "after body" insertion mode
6119          !!!parse-error (type => 'after body:'.$token->{tag_name});          !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);
6120    
6121          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6122            !!!ack-later;
6123          ## reprocess          ## reprocess
6124          redo B;          next B;
6125        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6126          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6127            !!!cp ('t305');            !!!cp ('t305');
6128            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
6129                        
6130            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6131            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
# Line 5294  sub _tree_construction_main ($) { Line 6137  sub _tree_construction_main ($) {
6137          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
6138            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
6139              !!!cp ('t307');              !!!cp ('t307');
6140              !!!parse-error (type => 'unmatched end tag:html');              !!!parse-error (type => 'unmatched end tag:html', token => $token);
6141              ## Ignore the token              ## Ignore the token
6142              !!!next-token;              !!!next-token;
6143              redo B;              next B;
6144            } else {            } else {
6145              !!!cp ('t308');              !!!cp ('t308');
6146              $self->{insertion_mode} = AFTER_HTML_BODY_IM;              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6147              !!!next-token;              !!!next-token;
6148              redo B;              next B;
6149            }            }
6150          } else {          } else {
6151            !!!cp ('t309');            !!!cp ('t309');
6152            !!!parse-error (type => 'after body:/'.$token->{tag_name});            !!!parse-error (type => 'after body:/'.$token->{tag_name}, token => $token);
6153    
6154            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
6155            ## reprocess            ## reprocess
6156            redo B;            next B;
6157          }          }
6158        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6159          !!!cp ('t309.2');          !!!cp ('t309.2');
# Line 5327  sub _tree_construction_main ($) { Line 6170  sub _tree_construction_main ($) {
6170            unless (length $token->{data}) {            unless (length $token->{data}) {
6171              !!!cp ('t310');              !!!cp ('t310');
6172              !!!next-token;              !!!next-token;
6173              redo B;              next B;
6174            }            }
6175          }          }
6176                    
6177          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
6178            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6179              !!!cp ('t311');              !!!cp ('t311');
6180              !!!parse-error (type => 'in frameset:#character');              !!!parse-error (type => 'in frameset:#character', token => $token);
6181            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6182              !!!cp ('t312');              !!!cp ('t312');
6183              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#character', token => $token);
6184            } else { # "after html frameset"            } else { # "after html frameset"
6185              !!!cp ('t313');              !!!cp ('t313');
6186              !!!parse-error (type => 'after html:#character');              !!!parse-error (type => 'after html:#character', token => $token);
6187    
6188              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6189              ## Reprocess in the "after frameset" insertion mode.              ## Reprocess in the "after frameset" insertion mode.
6190              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#character', token => $token);
6191            }            }
6192                        
6193            ## Ignore the token.            ## Ignore the token.
# Line 5355  sub _tree_construction_main ($) { Line 6198  sub _tree_construction_main ($) {
6198              !!!cp ('t315');              !!!cp ('t315');
6199              !!!next-token;              !!!next-token;
6200            }            }
6201            redo B;            next B;
6202          }          }
6203                    
6204          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
6205        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6206          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
6207            !!!cp ('t316');            !!!cp ('t316');
6208            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
6209    
6210            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
6211            ## Process in the "after frameset" insertion mode.            ## Process in the "after frameset" insertion mode.
# Line 5373  sub _tree_construction_main ($) { Line 6216  sub _tree_construction_main ($) {
6216          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6217              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6218            !!!cp ('t318');            !!!cp ('t318');
6219            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6220              !!!nack ('t318.1');
6221            !!!next-token;            !!!next-token;
6222            redo B;            next B;
6223          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
6224                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
6225            !!!cp ('t319');            !!!cp ('t319');
6226            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6227            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6228              !!!ack ('t319.1');
6229            !!!next-token;            !!!next-token;
6230            redo B;            next B;
6231          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
6232            !!!cp ('t320');            !!!cp ('t320');
6233            ## NOTE: As if in body.            ## NOTE: As if in body.
6234            $parse_rcdata->(CDATA_CONTENT_MODEL);            $parse_rcdata->(CDATA_CONTENT_MODEL);
6235            redo B;            next B;
6236          } else {          } else {
6237            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6238              !!!cp ('t321');              !!!cp ('t321');
6239              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!parse-error (type => 'in frameset:'.$token->{tag_name}, token => $token);
6240            } else {            } else {
6241              !!!cp ('t322');              !!!cp ('t322');
6242              !!!parse-error (type => 'after frameset:'.$token->{tag_name});              !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);
6243            }            }
6244            ## Ignore the token            ## Ignore the token
6245              !!!nack ('t322.1');
6246            !!!next-token;            !!!next-token;
6247            redo B;            next B;
6248          }          }
6249        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6250          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
6251            !!!cp ('t323');            !!!cp ('t323');
6252            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
6253    
6254            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
6255            ## Process in the "after frameset" insertion mode.            ## Process in the "after frameset" insertion mode.
# Line 5413  sub _tree_construction_main ($) { Line 6259  sub _tree_construction_main ($) {
6259    
6260          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6261              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6262            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6263                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
6264              !!!cp ('t325');              !!!cp ('t325');
6265              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6266              ## Ignore the token              ## Ignore the token
6267              !!!next-token;              !!!next-token;
6268            } else {            } else {
# Line 5426  sub _tree_construction_main ($) { Line 6272  sub _tree_construction_main ($) {
6272            }            }
6273    
6274            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
6275                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6276              !!!cp ('t327');              !!!cp ('t327');
6277              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6278            } else {            } else {
6279              !!!cp ('t328');              !!!cp ('t328');
6280            }            }
6281            redo B;            next B;
6282          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
6283                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6284            !!!cp ('t329');            !!!cp ('t329');
6285            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6286            !!!next-token;            !!!next-token;
6287            redo B;            next B;
6288          } else {          } else {
6289            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6290              !!!cp ('t330');              !!!cp ('t330');
6291              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});              !!!parse-error (type => 'in frameset:/'.$token->{tag_name}, token => $token);
6292            } else {            } else {
6293              !!!cp ('t331');              !!!cp ('t331');
6294              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});              !!!parse-error (type => 'after frameset:/'.$token->{tag_name}, token => $token);
6295            }            }
6296            ## Ignore the token            ## Ignore the token
6297            !!!next-token;            !!!next-token;
6298            redo B;            next B;
6299          }          }
6300        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6301          unless ($self->{open_elements}->[-1]->[1] eq 'html' and          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6302                  @{$self->{open_elements}} == 1) { # redundant, maybe                  @{$self->{open_elements}} == 1) { # redundant, maybe
6303            !!!cp ('t331.1');            !!!cp ('t331.1');
6304            !!!parse-error (type => 'in body:#eof');            !!!parse-error (type => 'in body:#eof', token => $token);
6305          } else {          } else {
6306            !!!cp ('t331.2');            !!!cp ('t331.2');
6307          }          }
# Line 5477  sub _tree_construction_main ($) { Line 6323  sub _tree_construction_main ($) {
6323          !!!cp ('t332');          !!!cp ('t332');
6324          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6325          $script_start_tag->();          $script_start_tag->();
6326          redo B;          next B;
6327        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
6328          !!!cp ('t333');          !!!cp ('t333');
6329          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6330          $parse_rcdata->(CDATA_CONTENT_MODEL);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6331          redo B;          next B;
6332        } elsif ({        } elsif ({
6333                  base => 1, link => 1,                  base => 1, link => 1,
6334                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6335          !!!cp ('t334');          !!!cp ('t334');
6336          ## 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
6337          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6338          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6339            !!!ack ('t334.1');
6340          !!!next-token;          !!!next-token;
6341          redo B;          next B;
6342        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
6343          ## 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
6344          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6345          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.
6346    
6347          unless ($self->{confident}) {          unless ($self->{confident}) {
6348            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) {
6349              !!!cp ('t335');              !!!cp ('t335');
6350                ## NOTE: Whether the encoding is supported or not is handled
6351                ## in the {change_encoding} callback.
6352              $self->{change_encoding}              $self->{change_encoding}
6353                  ->($self, $token->{attributes}->{charset}->{value});                  ->($self, $token->{attributes}->{charset}->{value}, $token);
6354                            
6355              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6356                  ->set_user_data (manakai_has_reference =>                  ->set_user_data (manakai_has_reference =>
6357                                       $token->{attributes}->{charset}                                       $token->{attributes}->{charset}
6358                                           ->{has_reference});                                           ->{has_reference});
6359            } elsif ($token->{attributes}->{content}) {            } elsif ($token->{attributes}->{content}) {
             ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
6360              if ($token->{attributes}->{content}->{value}              if ($token->{attributes}->{content}->{value}
6361                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6362                      [\x09-\x0D\x20]*=                      [\x09-\x0D\x20]*=
6363                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6364                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
6365                !!!cp ('t336');                !!!cp ('t336');
6366                  ## NOTE: Whether the encoding is supported or not is handled
6367                  ## in the {change_encoding} callback.
6368                $self->{change_encoding}                $self->{change_encoding}
6369                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6370                $meta_el->[0]->get_attribute_node_ns (undef, 'content')                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6371                    ->set_user_data (manakai_has_reference =>                    ->set_user_data (manakai_has_reference =>
6372                                         $token->{attributes}->{content}                                         $token->{attributes}->{content}
# Line 5540  sub _tree_construction_main ($) { Line 6390  sub _tree_construction_main ($) {
6390            }            }
6391          }          }
6392    
6393            !!!ack ('t338.1');
6394          !!!next-token;          !!!next-token;
6395          redo B;          next B;
6396        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
6397          !!!cp ('t341');          !!!cp ('t341');
6398          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6399          $parse_rcdata->(RCDATA_CONTENT_MODEL);          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6400          redo B;          next B;
6401        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
6402          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body:body', token => $token);
6403                                
6404          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
6405              $self->{open_elements}->[1]->[1] ne 'body') {              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6406            !!!cp ('t342');            !!!cp ('t342');
6407            ## Ignore the token            ## Ignore the token
6408          } else {          } else {
# Line 5565  sub _tree_construction_main ($) { Line 6416  sub _tree_construction_main ($) {
6416              }              }
6417            }            }
6418          }          }
6419            !!!nack ('t343.1');
6420          !!!next-token;          !!!next-token;
6421          redo B;          next B;
6422        } elsif ({        } elsif ({
6423                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6424                  div => 1, dl => 1, fieldset => 1,                  div => 1, dl => 1, fieldset => 1,
6425                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6426                  menu => 1, ol => 1, p => 1, ul => 1,                  menu => 1, ol => 1, p => 1, ul => 1,
6427                  pre => 1, listing => 1,                  pre => 1, listing => 1,
6428                    form => 1,
6429                    table => 1,
6430                    hr => 1,
6431                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6432            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
6433              !!!cp ('t350');
6434              !!!parse-error (type => 'in form:form', token => $token);
6435              ## Ignore the token
6436              !!!nack ('t350.1');
6437              !!!next-token;
6438              next B;
6439            }
6440    
6441          ## has a p element in scope          ## has a p element in scope
6442          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6443            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6444              !!!cp ('t344');              !!!cp ('t344');
6445              !!!back-token;              !!!back-token; # <form>
6446              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6447              redo B;                        line => $token->{line}, column => $token->{column}};
6448            } elsif ({              next B;
6449                      applet => 1, table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6450              !!!cp ('t345');              !!!cp ('t345');
6451              last INSCOPE;              last INSCOPE;
6452            }            }
6453          } # INSCOPE          } # INSCOPE
6454                        
6455          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6456          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6457              !!!nack ('t346.1');
6458            !!!next-token;            !!!next-token;
6459            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
6460              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
# Line 5604  sub _tree_construction_main ($) { Line 6467  sub _tree_construction_main ($) {
6467            } else {            } else {
6468              !!!cp ('t348');              !!!cp ('t348');
6469            }            }
6470          } else {          } elsif ($token->{tag_name} eq 'form') {
6471            !!!cp ('t347');            !!!cp ('t347.1');
6472              $self->{form_element} = $self->{open_elements}->[-1]->[0];
6473    
6474              !!!nack ('t347.2');
6475            !!!next-token;            !!!next-token;
6476          }          } elsif ($token->{tag_name} eq 'table') {
6477          redo B;            !!!cp ('t382');
6478        } elsif ($token->{tag_name} eq 'form') {            push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
6479          if (defined $self->{form_element}) {            
6480            !!!cp ('t350');            $self->{insertion_mode} = IN_TABLE_IM;
6481            !!!parse-error (type => 'in form:form');  
6482            ## Ignore the token            !!!nack ('t382.1');
6483              !!!next-token;
6484            } elsif ($token->{tag_name} eq 'hr') {
6485              !!!cp ('t386');
6486              pop @{$self->{open_elements}};
6487            
6488              !!!nack ('t386.1');
6489            !!!next-token;            !!!next-token;
           redo B;  
6490          } else {          } else {
6491            ## has a p element in scope            !!!nack ('t347.1');
           INSCOPE: for (reverse @{$self->{open_elements}}) {  
             if ($_->[1] eq 'p') {  
               !!!cp ('t351');  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
               redo B;  
             } elsif ({  
                       applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                       button => 1, marquee => 1, object => 1, html => 1,  
                      }->{$_->[1]}) {  
               !!!cp ('t352');  
               last INSCOPE;  
             }  
           } # INSCOPE  
               
           !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           $self->{form_element} = $self->{open_elements}->[-1]->[0];  
6492            !!!next-token;            !!!next-token;
           redo B;  
6493          }          }
6494        } elsif ($token->{tag_name} eq 'li') {          next B;
6495          } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
6496          ## has a p element in scope          ## has a p element in scope
6497          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6498            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6499              !!!cp ('t353');              !!!cp ('t353');
6500              !!!back-token;              !!!back-token; # <x>
6501              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6502              redo B;                        line => $token->{line}, column => $token->{column}};
6503            } elsif ({              next B;
6504                      applet => 1, table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6505              !!!cp ('t354');              !!!cp ('t354');
6506              last INSCOPE;              last INSCOPE;
6507            }            }
# Line 5658  sub _tree_construction_main ($) { Line 6510  sub _tree_construction_main ($) {
6510          ## Step 1          ## Step 1
6511          my $i = -1;          my $i = -1;
6512          my $node = $self->{open_elements}->[$i];          my $node = $self->{open_elements}->[$i];
6513            my $li_or_dtdd = {li => {li => 1},
6514                              dt => {dt => 1, dd => 1},
6515                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6516          LI: {          LI: {
6517            ## Step 2            ## Step 2
6518            if ($node->[1] eq 'li') {            if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6519              if ($i != -1) {              if ($i != -1) {
6520                !!!cp ('t355');                !!!cp ('t355');
6521                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'not closed',
6522                                $self->{open_elements}->[-1]->[1]);                                value => $self->{open_elements}->[-1]->[0]
6523                                      ->manakai_local_name,
6524                                  token => $token);
6525              } else {              } else {
6526                !!!cp ('t356');                !!!cp ('t356');
6527              }              }
# Line 5675  sub _tree_construction_main ($) { Line 6532  sub _tree_construction_main ($) {
6532            }            }
6533                        
6534            ## Step 3            ## Step 3
6535            if (not $formatting_category->{$node->[1]} and            if (not ($node->[1] & FORMATTING_EL) and
6536                #not $phrasing_category->{$node->[1]} and                #not $phrasing_category->{$node->[1]} and
6537                ($special_category->{$node->[1]} or                ($node->[1] & SPECIAL_EL or
6538                 $scoping_category->{$node->[1]}) and                 $node->[1] & SCOPING_EL) and
6539                $node->[1] ne 'address' and $node->[1] ne 'div') {                not ($node->[1] & ADDRESS_EL) and
6540                  not ($node->[1] & DIV_EL)) {
6541              !!!cp ('t358');              !!!cp ('t358');
6542              last LI;              last LI;
6543            }            }
# Line 5691  sub _tree_construction_main ($) { Line 6549  sub _tree_construction_main ($) {
6549            redo LI;            redo LI;
6550          } # LI          } # LI
6551                        
6552          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6553          !!!next-token;          !!!nack ('t359.1');
         redo B;  
       } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!cp ('t360');  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             !!!cp ('t361');  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {  
             if ($i != -1) {  
               !!!cp ('t362');  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
             } else {  
               !!!cp ('t363');  
             }  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           } else {  
             !!!cp ('t364');  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             !!!cp ('t365');  
             last LI;  
           }  
             
           !!!cp ('t366');  
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
6554          !!!next-token;          !!!next-token;
6555          redo B;          next B;
6556        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
6557          ## has a p element in scope          ## has a p element in scope
6558          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6559            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6560              !!!cp ('t367');              !!!cp ('t367');
6561              !!!back-token;              !!!back-token; # <plaintext>
6562              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6563              redo B;                        line => $token->{line}, column => $token->{column}};
6564            } elsif ({              next B;
6565                      applet => 1, table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6566              !!!cp ('t368');              !!!cp ('t368');
6567              last INSCOPE;              last INSCOPE;
6568            }            }
6569          } # INSCOPE          } # INSCOPE
6570                        
6571          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6572                        
6573          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
6574                        
6575            !!!nack ('t368.1');
6576          !!!next-token;          !!!next-token;
6577          redo B;          next B;
6578        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
6579          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
6580            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
6581            if ($node->[1] eq 'a') {            if ($node->[1] & A_EL) {
6582              !!!cp ('t371');              !!!cp ('t371');
6583              !!!parse-error (type => 'in a:a');              !!!parse-error (type => 'in a:a', token => $token);
6584                            
6585              !!!back-token;              !!!back-token; # <a>
6586              $token = {type => END_TAG_TOKEN, tag_name => 'a'};              $token = {type => END_TAG_TOKEN, tag_name => 'a',
6587              $formatting_end_tag->($token->{tag_name});                        line => $token->{line}, column => $token->{column}};
6588                $formatting_end_tag->($token);
6589                            
6590              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
6591                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
# Line 5807  sub _tree_construction_main ($) { Line 6610  sub _tree_construction_main ($) {
6610                        
6611          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6612    
6613          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6614          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6615    
6616            !!!nack ('t374.1');
6617          !!!next-token;          !!!next-token;
6618          redo B;          next B;
       } elsif ({  
                 b => 1, big => 1, em => 1, font => 1, i => 1,  
                 s => 1, small => 1, strile => 1,  
                 strong => 1, tt => 1, u => 1,  
                }->{$token->{tag_name}}) {  
         !!!cp ('t375');  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, $self->{open_elements}->[-1];  
           
         !!!next-token;  
         redo B;  
6619        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
6620          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6621    
6622          ## has a |nobr| element in scope          ## has a |nobr| element in scope
6623          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6624            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6625            if ($node->[1] eq 'nobr') {            if ($node->[1] & NOBR_EL) {
6626              !!!cp ('t376');              !!!cp ('t376');
6627              !!!parse-error (type => 'in nobr:nobr');              !!!parse-error (type => 'in nobr:nobr', token => $token);
6628              !!!back-token;              !!!back-token; # <nobr>
6629              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
6630              redo B;                        line => $token->{line}, column => $token->{column}};
6631            } elsif ({              next B;
6632                      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]}) {  
6633              !!!cp ('t377');              !!!cp ('t377');
6634              last INSCOPE;              last INSCOPE;
6635            }            }
6636          } # INSCOPE          } # INSCOPE
6637                    
6638          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6639          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6640                    
6641            !!!nack ('t377.1');
6642          !!!next-token;          !!!next-token;
6643          redo B;          next B;
6644        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
6645          ## has a button element in scope          ## has a button element in scope
6646          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6647            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6648            if ($node->[1] eq 'button') {            if ($node->[1] & BUTTON_EL) {
6649              !!!cp ('t378');              !!!cp ('t378');
6650              !!!parse-error (type => 'in button:button');              !!!parse-error (type => 'in button:button', token => $token);
6651              !!!back-token;              !!!back-token; # <button>
6652              $token = {type => END_TAG_TOKEN, tag_name => 'button'};              $token = {type => END_TAG_TOKEN, tag_name => 'button',
6653              redo B;                        line => $token->{line}, column => $token->{column}};
6654            } elsif ({              next B;
6655                      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]}) {  
6656              !!!cp ('t379');              !!!cp ('t379');
6657              last INSCOPE;              last INSCOPE;
6658            }            }
# Line 5872  sub _tree_construction_main ($) { Line 6660  sub _tree_construction_main ($) {
6660                        
6661          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6662                        
6663          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6664    
6665          ## TODO: associate with $self->{form_element} if defined          ## TODO: associate with $self->{form_element} if defined
6666    
6667          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
6668    
6669            !!!nack ('t379.1');
6670          !!!next-token;          !!!next-token;
6671          redo B;          next B;
6672        } elsif ({        } elsif ({
6673                  applet => 1, marquee => 1, object => 1,                  xmp => 1,
6674                 }->{$token->{tag_name}}) {                  iframe => 1,
6675          !!!cp ('t380');                  noembed => 1,
6676          $reconstruct_active_formatting_elements->($insert_to_current);                  noframes => 1,
6677                            noscript => 0, ## TODO: 1 if scripting is enabled
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, ['#marker', ''];  
           
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'xmp') {  
         !!!cp ('t381');  
         $reconstruct_active_formatting_elements->($insert_to_current);  
         $parse_rcdata->(CDATA_CONTENT_MODEL);  
         redo B;  
       } elsif ($token->{tag_name} eq 'table') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!cp ('t382');  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             !!!cp ('t383');  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];  
   
         $self->{insertion_mode} = IN_TABLE_IM;  
             
         !!!next-token;  
         redo B;  
       } elsif ({  
                 area => 1, basefont => 1, bgsound => 1, br => 1,  
                 embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,  
                 image => 1,  
6678                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6679          if ($token->{tag_name} eq 'image') {          if ($token->{tag_name} eq 'xmp') {
6680            !!!cp ('t384');            !!!cp ('t381');
6681            !!!parse-error (type => 'image');            $reconstruct_active_formatting_elements->($insert_to_current);
           $token->{tag_name} = 'img';  
6682          } else {          } else {
6683            !!!cp ('t385');            !!!cp ('t399');
6684          }          }
6685            ## NOTE: There is an "as if in body" code clone.
6686          ## NOTE: There is an "as if <br>" code clone.          $parse_rcdata->(CDATA_CONTENT_MODEL);
6687          $reconstruct_active_formatting_elements->($insert_to_current);          next B;
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'hr') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!cp ('t386');  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             !!!cp ('t387');  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
             
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'input') {  
         !!!cp ('t388');  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         ## TODO: associate with $self->{form_element} if defined  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         redo B;  
6688        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
6689          !!!parse-error (type => 'isindex');          !!!parse-error (type => 'isindex', token => $token);
6690                    
6691          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
6692            !!!cp ('t389');            !!!cp ('t389');
6693            ## Ignore the token            ## Ignore the token
6694              !!!nack ('t389'); ## NOTE: Not acknowledged.
6695            !!!next-token;            !!!next-token;
6696            redo B;            next B;
6697          } else {          } else {
6698            my $at = $token->{attributes};            my $at = $token->{attributes};
6699            my $form_attrs;            my $form_attrs;
# Line 5991  sub _tree_construction_main ($) { Line 6704  sub _tree_construction_main ($) {
6704            delete $at->{prompt};            delete $at->{prompt};
6705            my @tokens = (            my @tokens = (
6706                          {type => START_TAG_TOKEN, tag_name => 'form',                          {type => START_TAG_TOKEN, tag_name => 'form',
6707                           attributes => $form_attrs},                           attributes => $form_attrs,
6708                          {type => START_TAG_TOKEN, tag_name => 'hr'},                           line => $token->{line}, column => $token->{column}},
6709                          {type => START_TAG_TOKEN, tag_name => 'p'},                          {type => START_TAG_TOKEN, tag_name => 'hr',
6710                          {type => START_TAG_TOKEN, tag_name => 'label'},                           line => $token->{line}, column => $token->{column}},
6711                            {type => START_TAG_TOKEN, tag_name => 'p',
6712                             line => $token->{line}, column => $token->{column}},
6713                            {type => START_TAG_TOKEN, tag_name => 'label',
6714                             line => $token->{line}, column => $token->{column}},
6715                         );                         );
6716            if ($prompt_attr) {            if ($prompt_attr) {
6717              !!!cp ('t390');              !!!cp ('t390');
6718              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
6719                               #line => $token->{line}, column => $token->{column},
6720                              };
6721            } else {            } else {
6722              !!!cp ('t391');              !!!cp ('t391');
6723              push @tokens, {type => CHARACTER_TOKEN,              push @tokens, {type => CHARACTER_TOKEN,
6724                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD                             data => 'This is a searchable index. Insert your search keywords here: ',
6725                               #line => $token->{line}, column => $token->{column},
6726                              }; # SHOULD
6727              ## TODO: make this configurable              ## TODO: make this configurable
6728            }            }
6729            push @tokens,            push @tokens,
6730                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
6731                             line => $token->{line}, column => $token->{column}},
6732                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
6733                          {type => END_TAG_TOKEN, tag_name => 'label'},                          {type => END_TAG_TOKEN, tag_name => 'label',
6734                          {type => END_TAG_TOKEN, tag_name => 'p'},                           line => $token->{line}, column => $token->{column}},
6735                          {type => START_TAG_TOKEN, tag_name => 'hr'},                          {type => END_TAG_TOKEN, tag_name => 'p',
6736                          {type => END_TAG_TOKEN, tag_name => 'form'};                           line => $token->{line}, column => $token->{column}},
6737            $token = shift @tokens;                          {type => START_TAG_TOKEN, tag_name => 'hr',
6738                             line => $token->{line}, column => $token->{column}},
6739                            {type => END_TAG_TOKEN, tag_name => 'form',
6740                             line => $token->{line}, column => $token->{column}};
6741              !!!nack ('t391.1'); ## NOTE: Not acknowledged.
6742            !!!back-token (@tokens);            !!!back-token (@tokens);
6743            redo B;            !!!next-token;
6744              next B;
6745          }          }
6746        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
6747          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
6748          my $el;          my $el;
6749          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
6750                    
6751          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
6752          $self->{content_model} = RCDATA_CONTENT_MODEL;          $self->{content_model} = RCDATA_CONTENT_MODEL;
# Line 6028  sub _tree_construction_main ($) { Line 6755  sub _tree_construction_main ($) {
6755          $insert->($el);          $insert->($el);
6756                    
6757          my $text = '';          my $text = '';
6758            !!!nack ('t392.1');
6759          !!!next-token;          !!!next-token;
6760          if ($token->{type} == CHARACTER_TOKEN) {          if ($token->{type} == CHARACTER_TOKEN) {
6761            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
# Line 6058  sub _tree_construction_main ($) { Line 6786  sub _tree_construction_main ($) {
6786            ## Ignore the token            ## Ignore the token
6787          } else {          } else {
6788            !!!cp ('t398');            !!!cp ('t398');
6789            !!!parse-error (type => 'in RCDATA:#'.$token->{type});            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
6790          }          }
6791          !!!next-token;          !!!next-token;
6792          redo B;          next B;
6793        } elsif ({        } elsif ($token->{tag_name} eq 'math' or
6794                  iframe => 1,                 $token->{tag_name} eq 'svg') {
                 noembed => 1,  
                 noframes => 1,  
                 noscript => 0, ## TODO: 1 if scripting is enabled  
                }->{$token->{tag_name}}) {  
         !!!cp ('t399');  
         ## NOTE: There is an "as if in body" code clone.  
         $parse_rcdata->(CDATA_CONTENT_MODEL);  
         redo B;  
       } elsif ($token->{tag_name} eq 'select') {  
         !!!cp ('t400');  
6795          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
6796    
6797          ## TODO: associate with $self->{form_element} if defined          ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
6798    
6799            ## "adjust foreign attributes" - done in insert-element-f
6800            
6801            !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
6802                    
6803          if ($self->{insertion_mode} & TABLE_IMS or          if ($self->{self_closing}) {
6804              $self->{insertion_mode} & BODY_TABLE_IMS or            pop @{$self->{open_elements}};
6805              $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {            !!!ack ('t398.1');
           !!!cp ('t400.1');  
           $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;  
6806          } else {          } else {
6807            !!!cp ('t400.2');            !!!cp ('t398.2');
6808            $self->{insertion_mode} = IN_SELECT_IM;            $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
6809              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
6810              ## mode, "in body" (not "in foreign content") secondary insertion
6811              ## mode, maybe.
6812          }          }
6813    
6814          !!!next-token;          !!!next-token;
6815          redo B;          next B;
6816        } elsif ({        } elsif ({
6817                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
6818                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 6098  sub _tree_construction_main ($) { Line 6820  sub _tree_construction_main ($) {
6820                  thead => 1, tr => 1,                  thead => 1, tr => 1,
6821                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6822          !!!cp ('t401');          !!!cp ('t401');
6823          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);
6824          ## Ignore the token          ## Ignore the token
6825            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
6826          !!!next-token;          !!!next-token;
6827          redo B;          next B;
6828                    
6829          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
6830        } else {        } else {
6831          !!!cp ('t402');          if ($token->{tag_name} eq 'image') {
6832              !!!cp ('t384');
6833              !!!parse-error (type => 'image', token => $token);
6834              $token->{tag_name} = 'img';
6835            } else {
6836              !!!cp ('t385');
6837            }
6838    
6839            ## NOTE: There is an "as if <br>" code clone.
6840          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6841                    
6842          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6843    
6844            if ({
6845                 applet => 1, marquee => 1, object => 1,
6846                }->{$token->{tag_name}}) {
6847              !!!cp ('t380');
6848              push @$active_formatting_elements, ['#marker', ''];
6849              !!!nack ('t380.1');
6850            } elsif ({
6851                      b => 1, big => 1, em => 1, font => 1, i => 1,
6852                      s => 1, small => 1, strile => 1,
6853                      strong => 1, tt => 1, u => 1,
6854                     }->{$token->{tag_name}}) {
6855              !!!cp ('t375');
6856              push @$active_formatting_elements, $self->{open_elements}->[-1];
6857              !!!nack ('t375.1');
6858            } elsif ($token->{tag_name} eq 'input') {
6859              !!!cp ('t388');
6860              ## TODO: associate with $self->{form_element} if defined
6861              pop @{$self->{open_elements}};
6862              !!!ack ('t388.2');
6863            } elsif ({
6864                      area => 1, basefont => 1, bgsound => 1, br => 1,
6865                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
6866                      #image => 1,
6867                     }->{$token->{tag_name}}) {
6868              !!!cp ('t388.1');
6869              pop @{$self->{open_elements}};
6870              !!!ack ('t388.3');
6871            } elsif ($token->{tag_name} eq 'select') {
6872              ## TODO: associate with $self->{form_element} if defined
6873            
6874              if ($self->{insertion_mode} & TABLE_IMS or
6875                  $self->{insertion_mode} & BODY_TABLE_IMS or
6876                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
6877                !!!cp ('t400.1');
6878                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
6879              } else {
6880                !!!cp ('t400.2');
6881                $self->{insertion_mode} = IN_SELECT_IM;
6882              }
6883              !!!nack ('t400.3');
6884            } else {
6885              !!!nack ('t402');
6886            }
6887                    
6888          !!!next-token;          !!!next-token;
6889          redo B;          next B;
6890        }        }
6891      } elsif ($token->{type} == END_TAG_TOKEN) {      } elsif ($token->{type} == END_TAG_TOKEN) {
6892        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
6893          if (@{$self->{open_elements}} > 1 and          ## has a |body| element in scope
6894              $self->{open_elements}->[1]->[1] eq 'body') {          my $i;
6895            for (@{$self->{open_elements}}) {          INSCOPE: {
6896              unless ({            for (reverse @{$self->{open_elements}}) {
6897                         dd => 1, dt => 1, li => 1, p => 1, td => 1,              if ($_->[1] & BODY_EL) {
6898                         th => 1, tr => 1, body => 1, html => 1,                !!!cp ('t405');
6899                       tbody => 1, tfoot => 1, thead => 1,                $i = $_;
6900                      }->{$_->[1]}) {                last INSCOPE;
6901                !!!cp ('t403');              } elsif ($_->[1] & SCOPING_EL) {
6902                !!!parse-error (type => 'not closed:'.$_->[1]);                !!!cp ('t405.1');
6903              } else {                last;
               !!!cp ('t404');  
6904              }              }
6905            }            }
6906    
6907            $self->{insertion_mode} = AFTER_BODY_IM;            !!!parse-error (type => 'start tag not allowed',
6908            !!!next-token;                            value => $token->{tag_name}, token => $token);
6909            redo B;            ## NOTE: Ignore the token.
         } else {  
           !!!cp ('t405');  
           !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           ## Ignore the token  
6910            !!!next-token;            !!!next-token;
6911            redo B;            next B;
6912            } # INSCOPE
6913    
6914            for (@{$self->{open_elements}}) {
6915              unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
6916                !!!cp ('t403');
6917                !!!parse-error (type => 'not closed',
6918                                value => $_->[0]->manakai_local_name,
6919                                token => $token);
6920                last;
6921              } else {
6922                !!!cp ('t404');
6923              }
6924          }          }
6925    
6926            $self->{insertion_mode} = AFTER_BODY_IM;
6927            !!!next-token;
6928            next B;
6929        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
6930          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
6931            ## up-to-date, though it has same effect as speced.
6932            if (@{$self->{open_elements}} > 1 and
6933                $self->{open_elements}->[1]->[1] & BODY_EL) {
6934            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
6935            if ($self->{open_elements}->[-1]->[1] ne 'body') {            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
6936              !!!cp ('t406');              !!!cp ('t406');
6937              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!parse-error (type => 'not closed',
6938                                value => $self->{open_elements}->[1]->[0]
6939                                    ->manakai_local_name,
6940                                token => $token);
6941            } else {            } else {
6942              !!!cp ('t407');              !!!cp ('t407');
6943            }            }
6944            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6945            ## reprocess            ## reprocess
6946            redo B;            next B;
6947          } else {          } else {
6948            !!!cp ('t408');            !!!cp ('t408');
6949            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6950            ## Ignore the token            ## Ignore the token
6951            !!!next-token;            !!!next-token;
6952            redo B;            next B;
6953          }          }
6954        } elsif ({        } elsif ({
6955                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
# Line 6170  sub _tree_construction_main ($) { Line 6962  sub _tree_construction_main ($) {
6962          my $i;          my $i;
6963          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6964            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6965            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6966              !!!cp ('t410');              !!!cp ('t410');
6967              $i = $_;              $i = $_;
6968              last INSCOPE;              last INSCOPE;
6969            } 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]}) {  
6970              !!!cp ('t411');              !!!cp ('t411');
6971              last INSCOPE;              last INSCOPE;
6972            }            }
# Line 6185  sub _tree_construction_main ($) { Line 6974  sub _tree_construction_main ($) {
6974    
6975          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
6976            !!!cp ('t413');            !!!cp ('t413');
6977            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6978          } else {          } else {
6979            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
6980            while ({            while ({
# Line 6193  sub _tree_construction_main ($) { Line 6982  sub _tree_construction_main ($) {
6982                    dt => ($token->{tag_name} ne 'dt'),                    dt => ($token->{tag_name} ne 'dt'),
6983                    li => ($token->{tag_name} ne 'li'),                    li => ($token->{tag_name} ne 'li'),
6984                    p => 1,                    p => 1,
6985                   }->{$self->{open_elements}->[-1]->[1]}) {                   }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
6986              !!!cp ('t409');              !!!cp ('t409');
6987              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6988            }            }
6989    
6990            ## Step 2.            ## Step 2.
6991            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6992                      ne $token->{tag_name}) {
6993              !!!cp ('t412');              !!!cp ('t412');
6994              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              !!!parse-error (type => 'not closed',
6995                                value => $self->{open_elements}->[-1]->[0]
6996                                    ->manakai_local_name,
6997                                token => $token);
6998            } else {            } else {
6999              !!!cp ('t414');              !!!cp ('t414');
7000            }            }
# Line 6216  sub _tree_construction_main ($) { Line 7009  sub _tree_construction_main ($) {
7009                }->{$token->{tag_name}};                }->{$token->{tag_name}};
7010          }          }
7011          !!!next-token;          !!!next-token;
7012          redo B;          next B;
7013        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
7014          undef $self->{form_element};          undef $self->{form_element};
7015    
# Line 6224  sub _tree_construction_main ($) { Line 7017  sub _tree_construction_main ($) {
7017          my $i;          my $i;
7018          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7019            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7020            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & FORM_EL) {
7021              !!!cp ('t418');              !!!cp ('t418');
7022              $i = $_;              $i = $_;
7023              last INSCOPE;              last INSCOPE;
7024            } 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]}) {  
7025              !!!cp ('t419');              !!!cp ('t419');
7026              last INSCOPE;              last INSCOPE;
7027            }            }
# Line 6239  sub _tree_construction_main ($) { Line 7029  sub _tree_construction_main ($) {
7029    
7030          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
7031            !!!cp ('t421');            !!!cp ('t421');
7032            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7033          } else {          } else {
7034            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7035            while ({            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                   dd => 1, dt => 1, li => 1, p => 1,  
                  }->{$self->{open_elements}->[-1]->[1]}) {  
7036              !!!cp ('t417');              !!!cp ('t417');
7037              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
7038            }            }
7039                        
7040            ## Step 2.            ## Step 2.
7041            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7042                      ne $token->{tag_name}) {
7043              !!!cp ('t417.1');              !!!cp ('t417.1');
7044              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              !!!parse-error (type => 'not closed',
7045                                value => $self->{open_elements}->[-1]->[0]
7046                                    ->manakai_local_name,
7047                                token => $token);
7048            } else {            } else {
7049              !!!cp ('t420');              !!!cp ('t420');
7050            }              }  
# Line 6262  sub _tree_construction_main ($) { Line 7054  sub _tree_construction_main ($) {
7054          }          }
7055    
7056          !!!next-token;          !!!next-token;
7057          redo B;          next B;
7058        } elsif ({        } elsif ({
7059                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
7060                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 6270  sub _tree_construction_main ($) { Line 7062  sub _tree_construction_main ($) {
7062          my $i;          my $i;
7063          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7064            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7065            if ({            if ($node->[1] & HEADING_EL) {
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
7066              !!!cp ('t423');              !!!cp ('t423');
7067              $i = $_;              $i = $_;
7068              last INSCOPE;              last INSCOPE;
7069            } 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]}) {  
7070              !!!cp ('t424');              !!!cp ('t424');
7071              last INSCOPE;              last INSCOPE;
7072            }            }
# Line 6287  sub _tree_construction_main ($) { Line 7074  sub _tree_construction_main ($) {
7074    
7075          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
7076            !!!cp ('t425.1');            !!!cp ('t425.1');
7077            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7078          } else {          } else {
7079            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7080            while ({            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                   dd => 1, dt => 1, li => 1, p => 1,  
                  }->{$self->{open_elements}->[-1]->[1]}) {  
7081              !!!cp ('t422');              !!!cp ('t422');
7082              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
7083            }            }
7084                        
7085            ## Step 2.            ## Step 2.
7086            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7087                      ne $token->{tag_name}) {
7088              !!!cp ('t425');              !!!cp ('t425');
7089              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7090            } else {            } else {
7091              !!!cp ('t426');              !!!cp ('t426');
7092            }            }
# Line 6310  sub _tree_construction_main ($) { Line 7096  sub _tree_construction_main ($) {
7096          }          }
7097                    
7098          !!!next-token;          !!!next-token;
7099          redo B;          next B;
7100        } elsif ($token->{tag_name} eq 'p') {        } elsif ($token->{tag_name} eq 'p') {
7101          ## has an element in scope          ## has an element in scope
7102          my $i;          my $i;
7103          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7104            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7105            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & P_EL) {
7106              !!!cp ('t410.1');              !!!cp ('t410.1');
7107              $i = $_;              $i = $_;
7108              last INSCOPE;              last INSCOPE;
7109            } 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]}) {  
7110              !!!cp ('t411.1');              !!!cp ('t411.1');
7111              last INSCOPE;              last INSCOPE;
7112            }            }
7113          } # INSCOPE          } # INSCOPE
7114    
7115          if (defined $i) {          if (defined $i) {
7116            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7117                      ne $token->{tag_name}) {
7118              !!!cp ('t412.1');              !!!cp ('t412.1');
7119              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              !!!parse-error (type => 'not closed',
7120                                value => $self->{open_elements}->[-1]->[0]
7121                                    ->manakai_local_name,
7122                                token => $token);
7123            } else {            } else {
7124              !!!cp ('t414.1');              !!!cp ('t414.1');
7125            }            }
# Line 6340  sub _tree_construction_main ($) { Line 7127  sub _tree_construction_main ($) {
7127            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
7128          } else {          } else {
7129            !!!cp ('t413.1');            !!!cp ('t413.1');
7130            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7131    
7132            !!!cp ('t415.1');            !!!cp ('t415.1');
7133            ## As if <p>, then reprocess the current token            ## As if <p>, then reprocess the current token
7134            my $el;            my $el;
7135            !!!create-element ($el, 'p');            !!!create-element ($el, $HTML_NS, 'p',, $token);
7136            $insert->($el);            $insert->($el);
7137            ## NOTE: Not inserted into |$self->{open_elements}|.            ## NOTE: Not inserted into |$self->{open_elements}|.
7138          }          }
7139    
7140          !!!next-token;          !!!next-token;
7141          redo B;          next B;
7142        } elsif ({        } elsif ({
7143                  a => 1,                  a => 1,
7144                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
# Line 6359  sub _tree_construction_main ($) { Line 7146  sub _tree_construction_main ($) {
7146                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
7147                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7148          !!!cp ('t427');          !!!cp ('t427');
7149          $formatting_end_tag->($token->{tag_name});          $formatting_end_tag->($token);
7150          redo B;          next B;
7151        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
7152          !!!cp ('t428');          !!!cp ('t428');
7153          !!!parse-error (type => 'unmatched end tag:br');          !!!parse-error (type => 'unmatched end tag:br', token => $token);
7154    
7155          ## As if <br>          ## As if <br>
7156          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7157                    
7158          my $el;          my $el;
7159          !!!create-element ($el, 'br');          !!!create-element ($el, $HTML_NS, 'br',, $token);
7160          $insert->($el);          $insert->($el);
7161                    
7162          ## Ignore the token.          ## Ignore the token.
7163          !!!next-token;          !!!next-token;
7164          redo B;          next B;
7165        } elsif ({        } elsif ({
7166                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7167                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 6388  sub _tree_construction_main ($) { Line 7175  sub _tree_construction_main ($) {
7175                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
7176                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7177          !!!cp ('t429');          !!!cp ('t429');
7178          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7179          ## Ignore the token          ## Ignore the token
7180          !!!next-token;          !!!next-token;
7181          redo B;          next B;
7182                    
7183          ## ISSUE: Issue on HTML5 new elements in spec          ## ISSUE: Issue on HTML5 new elements in spec
7184                    
# Line 6402  sub _tree_construction_main ($) { Line 7189  sub _tree_construction_main ($) {
7189    
7190          ## Step 2          ## Step 2
7191          S2: {          S2: {
7192            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7193              ## Step 1              ## Step 1
7194              ## generate implied end tags              ## generate implied end tags
7195              while ({              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                     dd => 1, dt => 1, li => 1, p => 1,  
                    }->{$self->{open_elements}->[-1]->[1]}) {  
7196                !!!cp ('t430');                !!!cp ('t430');
7197                ## ISSUE: Can this case be reached?                ## ISSUE: Can this case be reached?
7198                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
7199              }              }
7200                    
7201              ## Step 2              ## Step 2
7202              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7203                        ne $token->{tag_name}) {
7204                !!!cp ('t431');                !!!cp ('t431');
7205                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
7206                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
7207                                  value => $self->{open_elements}->[-1]->[0]
7208                                      ->manakai_local_name,
7209                                  token => $token);
7210              } else {              } else {
7211                !!!cp ('t432');                !!!cp ('t432');
7212              }              }
# Line 6429  sub _tree_construction_main ($) { Line 7218  sub _tree_construction_main ($) {
7218              last S2;              last S2;
7219            } else {            } else {
7220              ## Step 3              ## Step 3
7221              if (not $formatting_category->{$node->[1]} and              if (not ($node->[1] & FORMATTING_EL) and
7222                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
7223                  ($special_category->{$node->[1]} or                  ($node->[1] & SPECIAL_EL or
7224                   $scoping_category->{$node->[1]})) {                   $node->[1] & SCOPING_EL)) {
7225                !!!cp ('t433');                !!!cp ('t433');
7226                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7227                ## Ignore the token                ## Ignore the token
7228                !!!next-token;                !!!next-token;
7229                last S2;                last S2;
# Line 6450  sub _tree_construction_main ($) { Line 7239  sub _tree_construction_main ($) {
7239            ## Step 5;            ## Step 5;
7240            redo S2;            redo S2;
7241          } # S2          } # S2
7242          redo B;          next B;
7243        }        }
7244      }      }
7245      redo B;      next B;
7246      } continue { # B
7247        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7248          ## NOTE: The code below is executed in cases where it does not have
7249          ## to be, but it it is harmless even in those cases.
7250          ## has an element in scope
7251          INSCOPE: {
7252            for (reverse 0..$#{$self->{open_elements}}) {
7253              my $node = $self->{open_elements}->[$_];
7254              if ($node->[1] & FOREIGN_EL) {
7255                last INSCOPE;
7256              } elsif ($node->[1] & SCOPING_EL) {
7257                last;
7258              }
7259            }
7260            
7261            ## NOTE: No foreign element in scope.
7262            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7263          } # INSCOPE
7264        }
7265    } # B    } # B
7266    
7267    ## Stop parsing # MUST    ## Stop parsing # MUST
# Line 6499  sub set_inner_html ($$$) { Line 7307  sub set_inner_html ($$$) {
7307    
7308      ## Step 8 # MUST      ## Step 8 # MUST
7309      my $i = 0;      my $i = 0;
7310      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7311      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7312      $p->{set_next_char} = sub {      $p->{set_next_char} = sub {
7313        my $self = shift;        my $self = shift;
7314    
# Line 6509  sub set_inner_html ($$$) { Line 7317  sub set_inner_html ($$$) {
7317    
7318        $self->{next_char} = -1 and return if $i >= length $$s;        $self->{next_char} = -1 and return if $i >= length $$s;
7319        $self->{next_char} = ord substr $$s, $i++, 1;        $self->{next_char} = ord substr $$s, $i++, 1;
7320        $column++;  
7321          ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7322          $p->{column}++;
7323    
7324        if ($self->{next_char} == 0x000A) { # LF        if ($self->{next_char} == 0x000A) { # LF
7325          $line++;          $p->{line}++;
7326          $column = 0;          $p->{column} = 0;
7327          !!!cp ('i1');          !!!cp ('i1');
7328        } elsif ($self->{next_char} == 0x000D) { # CR        } elsif ($self->{next_char} == 0x000D) { # CR
7329          $i++ if substr ($$s, $i, 1) eq "\x0A";          $i++ if substr ($$s, $i, 1) eq "\x0A";
7330          $self->{next_char} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
7331          $line++;          $p->{line}++;
7332          $column = 0;          $p->{column} = 0;
7333          !!!cp ('i2');          !!!cp ('i2');
7334        } elsif ($self->{next_char} > 0x10FFFF) {        } elsif ($self->{next_char} > 0x10FFFF) {
7335          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
# Line 6528  sub set_inner_html ($$$) { Line 7338  sub set_inner_html ($$$) {
7338          !!!cp ('i4');          !!!cp ('i4');
7339          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
7340          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7341          } elsif ($self->{next_char} <= 0x0008 or
7342                   (0x000E <= $self->{next_char} and
7343                    $self->{next_char} <= 0x001F) or
7344                   (0x007F <= $self->{next_char} and
7345                    $self->{next_char} <= 0x009F) or
7346                   (0xD800 <= $self->{next_char} and
7347                    $self->{next_char} <= 0xDFFF) or
7348                   (0xFDD0 <= $self->{next_char} and
7349                    $self->{next_char} <= 0xFDDF) or
7350                   {
7351                    0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
7352                    0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
7353                    0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
7354                    0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
7355                    0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
7356                    0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
7357                    0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
7358                    0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
7359                    0x10FFFE => 1, 0x10FFFF => 1,
7360                   }->{$self->{next_char}}) {
7361            !!!cp ('i4.1');
7362            !!!parse-error (type => 'control char', level => $self->{must_level});
7363    ## TODO: error type documentation
7364        }        }
7365      };      };
7366      $p->{prev_char} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];
# Line 6535  sub set_inner_html ($$$) { Line 7368  sub set_inner_html ($$$) {
7368            
7369      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7370        my (%opt) = @_;        my (%opt) = @_;
7371        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7372          my $column = $opt{column};
7373          if (defined $opt{token} and defined $opt{token}->{line}) {
7374            $line = $opt{token}->{line};
7375            $column = $opt{token}->{column};
7376          }
7377          warn "Parse error ($opt{type}) at line $line column $column\n";
7378      };      };
7379      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7380        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7381      };      };
7382            
7383      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
# Line 6562  sub set_inner_html ($$$) { Line 7401  sub set_inner_html ($$$) {
7401          unless defined $p->{content_model};          unless defined $p->{content_model};
7402          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
7403    
7404      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7405          ## TODO: Foreign element OK?
7406    
7407      ## Step 3      ## Step 3
7408      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
# Line 6572  sub set_inner_html ($$$) { Line 7412  sub set_inner_html ($$$) {
7412      $doc->append_child ($root);      $doc->append_child ($root);
7413    
7414      ## Step 5 # MUST      ## Step 5 # MUST
7415      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7416    
7417      undef $p->{head_element};      undef $p->{head_element};
7418    
# Line 6618  sub set_inner_html ($$$) { Line 7458  sub set_inner_html ($$$) {
7458      ## ISSUE: mutation events?      ## ISSUE: mutation events?
7459    
7460      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
7461    
7462        delete $p->{parse_error}; # delete loop
7463    } else {    } else {
7464      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";
7465    }    }

Legend:
Removed from v.1.104  
changed lines
  Added in v.1.143

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24