/[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.100 by wakaba, Sun Mar 9 04:08:41 2008 UTC revision 1.151 by wakaba, Sun Jun 8 05:08:42 2008 UTC
# Line 8  use Error qw(:try); Line 8  use Error qw(:try);
8  ## doc.write ('');  ## doc.write ('');
9  ## alert (doc.compatMode);  ## alert (doc.compatMode);
10    
11  ## TODO: Control charcters and noncharacters are not allowed (HTML5 revision 1263)  require IO::Handle;
12  ## TODO: 1252 parse error (revision 1264)  
13  ## TODO: 8859-11 = 874 (revision 1271)  my $HTML_NS = q<http://www.w3.org/1999/xhtml>;
14    my $MML_NS = q<http://www.w3.org/1998/Math/MathML>;
15  my $permitted_slash_tag_name = {  my $SVG_NS = q<http://www.w3.org/2000/svg>;
16    base => 1,  my $XLINK_NS = q<http://www.w3.org/1999/xlink>;
17    link => 1,  my $XML_NS = q<http://www.w3.org/XML/1998/namespace>;
18    meta => 1,  my $XMLNS_NS = q<http://www.w3.org/2000/xmlns/>;
19    hr => 1,  
20    br => 1,  sub A_EL () { 0b1 }
21    img => 1,  sub ADDRESS_EL () { 0b10 }
22    embed => 1,  sub BODY_EL () { 0b100 }
23    param => 1,  sub BUTTON_EL () { 0b1000 }
24    area => 1,  sub CAPTION_EL () { 0b10000 }
25    col => 1,  sub DD_EL () { 0b100000 }
26    input => 1,  sub DIV_EL () { 0b1000000 }
27    sub DT_EL () { 0b10000000 }
28    sub FORM_EL () { 0b100000000 }
29    sub FORMATTING_EL () { 0b1000000000 }
30    sub FRAMESET_EL () { 0b10000000000 }
31    sub HEADING_EL () { 0b100000000000 }
32    sub HTML_EL () { 0b1000000000000 }
33    sub LI_EL () { 0b10000000000000 }
34    sub NOBR_EL () { 0b100000000000000 }
35    sub OPTION_EL () { 0b1000000000000000 }
36    sub OPTGROUP_EL () { 0b10000000000000000 }
37    sub P_EL () { 0b100000000000000000 }
38    sub SELECT_EL () { 0b1000000000000000000 }
39    sub TABLE_EL () { 0b10000000000000000000 }
40    sub TABLE_CELL_EL () { 0b100000000000000000000 }
41    sub TABLE_ROW_EL () { 0b1000000000000000000000 }
42    sub TABLE_ROW_GROUP_EL () { 0b10000000000000000000000 }
43    sub MISC_SCOPING_EL () { 0b100000000000000000000000 }
44    sub MISC_SPECIAL_EL () { 0b1000000000000000000000000 }
45    sub FOREIGN_EL () { 0b10000000000000000000000000 }
46    sub FOREIGN_FLOW_CONTENT_EL () { 0b100000000000000000000000000 }
47    sub MML_AXML_EL () { 0b1000000000000000000000000000 }
48    sub RUBY_EL () { 0b10000000000000000000000000000 }
49    sub RUBY_COMPONENT_EL () { 0b100000000000000000000000000000 }
50    
51    sub TABLE_ROWS_EL () {
52      TABLE_EL |
53      TABLE_ROW_EL |
54      TABLE_ROW_GROUP_EL
55    }
56    
57    ## NOTE: Used in "generate implied end tags" algorithm.
58    ## NOTE: There is a code where a modified version of END_TAG_OPTIONAL_EL
59    ## is used in "generate implied end tags" implementation (search for the
60    ## function mae).
61    sub END_TAG_OPTIONAL_EL () {
62      DD_EL |
63      DT_EL |
64      LI_EL |
65      P_EL |
66      RUBY_COMPONENT_EL
67    }
68    
69    ## NOTE: Used in </body> and EOF algorithms.
70    sub ALL_END_TAG_OPTIONAL_EL () {
71      DD_EL |
72      DT_EL |
73      LI_EL |
74      P_EL |
75    
76      BODY_EL |
77      HTML_EL |
78      TABLE_CELL_EL |
79      TABLE_ROW_EL |
80      TABLE_ROW_GROUP_EL
81    }
82    
83    sub SCOPING_EL () {
84      BUTTON_EL |
85      CAPTION_EL |
86      HTML_EL |
87      TABLE_EL |
88      TABLE_CELL_EL |
89      MISC_SCOPING_EL
90    }
91    
92    sub TABLE_SCOPING_EL () {
93      HTML_EL |
94      TABLE_EL
95    }
96    
97    sub TABLE_ROWS_SCOPING_EL () {
98      HTML_EL |
99      TABLE_ROW_GROUP_EL
100    }
101    
102    sub TABLE_ROW_SCOPING_EL () {
103      HTML_EL |
104      TABLE_ROW_EL
105    }
106    
107    sub SPECIAL_EL () {
108      ADDRESS_EL |
109      BODY_EL |
110      DIV_EL |
111    
112      DD_EL |
113      DT_EL |
114      LI_EL |
115      P_EL |
116    
117      FORM_EL |
118      FRAMESET_EL |
119      HEADING_EL |
120      OPTION_EL |
121      OPTGROUP_EL |
122      SELECT_EL |
123      TABLE_ROW_EL |
124      TABLE_ROW_GROUP_EL |
125      MISC_SPECIAL_EL
126    }
127    
128    my $el_category = {
129      a => A_EL | FORMATTING_EL,
130      address => ADDRESS_EL,
131      applet => MISC_SCOPING_EL,
132      area => MISC_SPECIAL_EL,
133      b => FORMATTING_EL,
134      base => MISC_SPECIAL_EL,
135      basefont => MISC_SPECIAL_EL,
136      bgsound => MISC_SPECIAL_EL,
137      big => FORMATTING_EL,
138      blockquote => MISC_SPECIAL_EL,
139      body => BODY_EL,
140      br => MISC_SPECIAL_EL,
141      button => BUTTON_EL,
142      caption => CAPTION_EL,
143      center => MISC_SPECIAL_EL,
144      col => MISC_SPECIAL_EL,
145      colgroup => MISC_SPECIAL_EL,
146      dd => DD_EL,
147      dir => MISC_SPECIAL_EL,
148      div => DIV_EL,
149      dl => MISC_SPECIAL_EL,
150      dt => DT_EL,
151      em => FORMATTING_EL,
152      embed => MISC_SPECIAL_EL,
153      fieldset => MISC_SPECIAL_EL,
154      font => FORMATTING_EL,
155      form => FORM_EL,
156      frame => MISC_SPECIAL_EL,
157      frameset => FRAMESET_EL,
158      h1 => HEADING_EL,
159      h2 => HEADING_EL,
160      h3 => HEADING_EL,
161      h4 => HEADING_EL,
162      h5 => HEADING_EL,
163      h6 => HEADING_EL,
164      head => MISC_SPECIAL_EL,
165      hr => MISC_SPECIAL_EL,
166      html => HTML_EL,
167      i => FORMATTING_EL,
168      iframe => MISC_SPECIAL_EL,
169      img => MISC_SPECIAL_EL,
170      input => MISC_SPECIAL_EL,
171      isindex => MISC_SPECIAL_EL,
172      li => LI_EL,
173      link => MISC_SPECIAL_EL,
174      listing => MISC_SPECIAL_EL,
175      marquee => MISC_SCOPING_EL,
176      menu => MISC_SPECIAL_EL,
177      meta => MISC_SPECIAL_EL,
178      nobr => NOBR_EL | FORMATTING_EL,
179      noembed => MISC_SPECIAL_EL,
180      noframes => MISC_SPECIAL_EL,
181      noscript => MISC_SPECIAL_EL,
182      object => MISC_SCOPING_EL,
183      ol => MISC_SPECIAL_EL,
184      optgroup => OPTGROUP_EL,
185      option => OPTION_EL,
186      p => P_EL,
187      param => MISC_SPECIAL_EL,
188      plaintext => MISC_SPECIAL_EL,
189      pre => MISC_SPECIAL_EL,
190      rp => RUBY_COMPONENT_EL,
191      rt => RUBY_COMPONENT_EL,
192      ruby => RUBY_EL,
193      s => FORMATTING_EL,
194      script => MISC_SPECIAL_EL,
195      select => SELECT_EL,
196      small => FORMATTING_EL,
197      spacer => MISC_SPECIAL_EL,
198      strike => FORMATTING_EL,
199      strong => FORMATTING_EL,
200      style => MISC_SPECIAL_EL,
201      table => TABLE_EL,
202      tbody => TABLE_ROW_GROUP_EL,
203      td => TABLE_CELL_EL,
204      textarea => MISC_SPECIAL_EL,
205      tfoot => TABLE_ROW_GROUP_EL,
206      th => TABLE_CELL_EL,
207      thead => TABLE_ROW_GROUP_EL,
208      title => MISC_SPECIAL_EL,
209      tr => TABLE_ROW_EL,
210      tt => FORMATTING_EL,
211      u => FORMATTING_EL,
212      ul => MISC_SPECIAL_EL,
213      wbr => MISC_SPECIAL_EL,
214    };
215    
216    my $el_category_f = {
217      $MML_NS => {
218        'annotation-xml' => MML_AXML_EL,
219        mi => FOREIGN_FLOW_CONTENT_EL,
220        mo => FOREIGN_FLOW_CONTENT_EL,
221        mn => FOREIGN_FLOW_CONTENT_EL,
222        ms => FOREIGN_FLOW_CONTENT_EL,
223        mtext => FOREIGN_FLOW_CONTENT_EL,
224      },
225      $SVG_NS => {
226        foreignObject => FOREIGN_FLOW_CONTENT_EL,
227        desc => FOREIGN_FLOW_CONTENT_EL,
228        title => FOREIGN_FLOW_CONTENT_EL,
229      },
230      ## NOTE: In addition, FOREIGN_EL is set to non-HTML elements.
231    };
232    
233    my $svg_attr_name = {
234      attributename => 'attributeName',
235      attributetype => 'attributeType',
236      basefrequency => 'baseFrequency',
237      baseprofile => 'baseProfile',
238      calcmode => 'calcMode',
239      clippathunits => 'clipPathUnits',
240      contentscripttype => 'contentScriptType',
241      contentstyletype => 'contentStyleType',
242      diffuseconstant => 'diffuseConstant',
243      edgemode => 'edgeMode',
244      externalresourcesrequired => 'externalResourcesRequired',
245      filterres => 'filterRes',
246      filterunits => 'filterUnits',
247      glyphref => 'glyphRef',
248      gradienttransform => 'gradientTransform',
249      gradientunits => 'gradientUnits',
250      kernelmatrix => 'kernelMatrix',
251      kernelunitlength => 'kernelUnitLength',
252      keypoints => 'keyPoints',
253      keysplines => 'keySplines',
254      keytimes => 'keyTimes',
255      lengthadjust => 'lengthAdjust',
256      limitingconeangle => 'limitingConeAngle',
257      markerheight => 'markerHeight',
258      markerunits => 'markerUnits',
259      markerwidth => 'markerWidth',
260      maskcontentunits => 'maskContentUnits',
261      maskunits => 'maskUnits',
262      numoctaves => 'numOctaves',
263      pathlength => 'pathLength',
264      patterncontentunits => 'patternContentUnits',
265      patterntransform => 'patternTransform',
266      patternunits => 'patternUnits',
267      pointsatx => 'pointsAtX',
268      pointsaty => 'pointsAtY',
269      pointsatz => 'pointsAtZ',
270      preservealpha => 'preserveAlpha',
271      preserveaspectratio => 'preserveAspectRatio',
272      primitiveunits => 'primitiveUnits',
273      refx => 'refX',
274      refy => 'refY',
275      repeatcount => 'repeatCount',
276      repeatdur => 'repeatDur',
277      requiredextensions => 'requiredExtensions',
278      requiredfeatures => 'requiredFeatures',
279      specularconstant => 'specularConstant',
280      specularexponent => 'specularExponent',
281      spreadmethod => 'spreadMethod',
282      startoffset => 'startOffset',
283      stddeviation => 'stdDeviation',
284      stitchtiles => 'stitchTiles',
285      surfacescale => 'surfaceScale',
286      systemlanguage => 'systemLanguage',
287      tablevalues => 'tableValues',
288      targetx => 'targetX',
289      targety => 'targetY',
290      textlength => 'textLength',
291      viewbox => 'viewBox',
292      viewtarget => 'viewTarget',
293      xchannelselector => 'xChannelSelector',
294      ychannelselector => 'yChannelSelector',
295      zoomandpan => 'zoomAndPan',
296    };
297    
298    my $foreign_attr_xname = {
299      'xlink:actuate' => [$XLINK_NS, ['xlink', 'actuate']],
300      'xlink:arcrole' => [$XLINK_NS, ['xlink', 'arcrole']],
301      'xlink:href' => [$XLINK_NS, ['xlink', 'href']],
302      'xlink:role' => [$XLINK_NS, ['xlink', 'role']],
303      'xlink:show' => [$XLINK_NS, ['xlink', 'show']],
304      'xlink:title' => [$XLINK_NS, ['xlink', 'title']],
305      'xlink:type' => [$XLINK_NS, ['xlink', 'type']],
306      'xml:base' => [$XML_NS, ['xml', 'base']],
307      'xml:lang' => [$XML_NS, ['xml', 'lang']],
308      'xml:space' => [$XML_NS, ['xml', 'space']],
309      'xmlns' => [$XMLNS_NS, [undef, 'xmlns']],
310      'xmlns:xlink' => [$XMLNS_NS, ['xmlns', 'xlink']],
311  };  };
312    
313    ## ISSUE: xmlns:xlink="non-xlink-ns" is not an error.
314    
315  my $c1_entity_char = {  my $c1_entity_char = {
316    0x80 => 0x20AC,    0x80 => 0x20AC,
317    0x81 => 0xFFFD,    0x81 => 0xFFFD,
# Line 61  my $c1_entity_char = { Line 347  my $c1_entity_char = {
347    0x9F => 0x0178,    0x9F => 0x0178,
348  }; # $c1_entity_char  }; # $c1_entity_char
349    
 my $special_category = {  
   address => 1, area => 1, base => 1, basefont => 1, bgsound => 1,  
   blockquote => 1, body => 1, br => 1, center => 1, col => 1, colgroup => 1,  
   dd => 1, dir => 1, div => 1, dl => 1, dt => 1, embed => 1, fieldset => 1,  
   form => 1, frame => 1, frameset => 1, h1 => 1, h2 => 1, h3 => 1,  
   h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, iframe => 1, image => 1,  
   img => 1, input => 1, isindex => 1, li => 1, link => 1, listing => 1,  
   menu => 1, meta => 1, noembed => 1, noframes => 1, noscript => 1,  
   ol => 1, optgroup => 1, option => 1, p => 1, param => 1, plaintext => 1,  
   pre => 1, script => 1, select => 1, spacer => 1, style => 1, tbody => 1,  
   textarea => 1, tfoot => 1, thead => 1, title => 1, tr => 1, ul => 1, wbr => 1,  
 };  
 my $scoping_category = {  
   button => 1, caption => 1, html => 1, marquee => 1, object => 1,  
   table => 1, td => 1, th => 1,  
 };  
 my $formatting_category = {  
   a => 1, b => 1, big => 1, em => 1, font => 1, i => 1, nobr => 1,  
   s => 1, small => 1, strile => 1, strong => 1, tt => 1, u => 1,  
 };  
 # $phrasing_category: all other elements  
   
350  sub parse_byte_string ($$$$;$) {  sub parse_byte_string ($$$$;$) {
351      my $self = shift;
352      my $charset_name = shift;
353      open my $input, '<', ref $_[0] ? $_[0] : \($_[0]);
354      return $self->parse_byte_stream ($charset_name, $input, @_[1..$#_]);
355    } # parse_byte_string
356    
357    sub parse_byte_stream ($$$$;$) {
358    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
359    my $charset = shift;    my $charset_name = shift;
360    my $bytes_s = ref $_[0] ? $_[0] : \($_[0]);    my $byte_stream = $_[0];
   my $s;  
     
   if (defined $charset) {  
     require Encode; ## TODO: decode(utf8) don't delete BOM  
     $s = \ (Encode::decode ($charset, $$bytes_s));  
     $self->{input_encoding} = lc $charset; ## TODO: normalize name  
     $self->{confident} = 1;  
   } else {  
     ## TODO: Implement HTML5 detection algorithm  
     require Whatpm::Charset::UniversalCharDet;  
     $charset = Whatpm::Charset::UniversalCharDet->detect_byte_string  
         (substr ($$bytes_s, 0, 1024));  
     $charset ||= 'windows-1252';  
     $s = \ (Encode::decode ($charset, $$bytes_s));  
     $self->{input_encoding} = $charset;  
     $self->{confident} = 0;  
   }  
361    
362    $self->{change_encoding} = sub {    my $onerror = $_[2] || sub {
363      my $self = shift;      my (%opt) = @_;
364      my $charset = lc shift;      warn "Parse error ($opt{type})\n";
365      ## TODO: if $charset is supported    };
366      ## TODO: normalize charset name    $self->{parse_error} = $onerror; # updated later by parse_char_string
367    
368      ## "Change the encoding" algorithm:    ## HTML5 encoding sniffing algorithm
369      require Message::Charset::Info;
370      ## Step 1        my $charset;
371      if ($charset eq 'utf-16') { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?    my $buffer;
372        $charset = 'utf-8';    my ($char_stream, $e_status);
373    
374      SNIFFING: {
375    
376        ## Step 1
377        if (defined $charset_name) {
378          $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
379    
380          ## ISSUE: Unsupported encoding is not ignored according to the spec.
381          ($char_stream, $e_status) = $charset->get_decode_handle
382              ($byte_stream, allow_error_reporting => 1,
383               allow_fallback => 1);
384          if ($char_stream) {
385            $self->{confident} = 1;
386            last SNIFFING;
387          } else {
388            ## TODO: unsupported error
389          }
390      }      }
391    
392      ## Step 2      ## Step 2
393      if (defined $self->{input_encoding} and      my $byte_buffer = '';
394          $self->{input_encoding} eq $charset) {      for (1..1024) {
395          my $char = $byte_stream->getc;
396          last unless defined $char;
397          $byte_buffer .= $char;
398        } ## TODO: timeout
399    
400        ## Step 3
401        if ($byte_buffer =~ /^\xFE\xFF/) {
402          $charset = Message::Charset::Info->get_by_iana_name ('utf-16be');
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;        $self->{confident} = 1;
407        return;        last SNIFFING;
408        } elsif ($byte_buffer =~ /^\xFF\xFE/) {
409          $charset = Message::Charset::Info->get_by_iana_name ('utf-16le');
410          ($char_stream, $e_status) = $charset->get_decode_handle
411              ($byte_stream, allow_error_reporting => 1,
412               allow_fallback => 1, byte_buffer => \$byte_buffer);
413          $self->{confident} = 1;
414          last SNIFFING;
415        } elsif ($byte_buffer =~ /^\xEF\xBB\xBF/) {
416          $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
417          ($char_stream, $e_status) = $charset->get_decode_handle
418              ($byte_stream, allow_error_reporting => 1,
419               allow_fallback => 1, byte_buffer => \$byte_buffer);
420          $self->{confident} = 1;
421          last SNIFFING;
422      }      }
423    
424      !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.      ## Step 4
425          ':'.$charset, level => 'w');      ## TODO: <meta charset>
426    
427      ## Step 3      ## Step 5
428      # if (can) {      ## TODO: from history
       ## change the encoding on the fly.  
       #$self->{confident} = 1;  
       #return;  
     # }  
429    
430      ## Step 4      ## Step 6
431      throw Whatpm::HTML::RestartParser (charset => $charset);      require Whatpm::Charset::UniversalCharDet;
432        $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
433            ($byte_buffer);
434        if (defined $charset_name) {
435          $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
436    
437          ## ISSUE: Unsupported encoding is not ignored according to the spec.
438          require Whatpm::Charset::DecodeHandle;
439          $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
440              ($byte_stream);
441          ($char_stream, $e_status) = $charset->get_decode_handle
442              ($buffer, allow_error_reporting => 1,
443               allow_fallback => 1, byte_buffer => \$byte_buffer);
444          if ($char_stream) {
445            $buffer->{buffer} = $byte_buffer;
446            !!!parse-error (type => 'sniffing:chardet', ## TODO: type name
447                            value => $charset_name,
448                            level => $self->{info_level},
449                            line => 1, column => 1);
450            $self->{confident} = 0;
451            last SNIFFING;
452          }
453        }
454    
455        ## Step 7: default
456        ## TODO: Make this configurable.
457        $charset = Message::Charset::Info->get_by_iana_name ('windows-1252');
458            ## NOTE: We choose |windows-1252| here, since |utf-8| should be
459            ## detectable in the step 6.
460        require Whatpm::Charset::DecodeHandle;
461        $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
462            ($byte_stream);
463        ($char_stream, $e_status)
464            = $charset->get_decode_handle ($buffer,
465                                           allow_error_reporting => 1,
466                                           allow_fallback => 1,
467                                           byte_buffer => \$byte_buffer);
468        $buffer->{buffer} = $byte_buffer;
469        !!!parse-error (type => 'sniffing:default', ## TODO: type name
470                        value => 'windows-1252',
471                        level => $self->{info_level},
472                        line => 1, column => 1);
473        $self->{confident} = 0;
474      } # SNIFFING
475    
476      $self->{input_encoding} = $charset->get_iana_name;
477      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
478        !!!parse-error (type => 'chardecode:fallback', ## TODO: type name
479                        value => $self->{input_encoding},
480                        level => $self->{unsupported_level},
481                        line => 1, column => 1);
482      } elsif (not ($e_status &
483                    Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
484        !!!parse-error (type => 'chardecode:no error', ## TODO: type name
485                        value => $self->{input_encoding},
486                        level => $self->{unsupported_level},
487                        line => 1, column => 1);
488      }
489    
490      $self->{change_encoding} = sub {
491        my $self = shift;
492        $charset_name = shift;
493        my $token = shift;
494    
495        $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
496        ($char_stream, $e_status) = $charset->get_decode_handle
497            ($byte_stream, allow_error_reporting => 1, allow_fallback => 1,
498             byte_buffer => \ $buffer->{buffer});
499        
500        if ($char_stream) { # if supported
501          ## "Change the encoding" algorithm:
502    
503          ## Step 1    
504          if ($charset->{category} &
505              Message::Charset::Info::CHARSET_CATEGORY_UTF16 ()) {
506            $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
507            ($char_stream, $e_status) = $charset->get_decode_handle
508                ($byte_stream,
509                 byte_buffer => \ $buffer->{buffer});
510          }
511          $charset_name = $charset->get_iana_name;
512          
513          ## Step 2
514          if (defined $self->{input_encoding} and
515              $self->{input_encoding} eq $charset_name) {
516            !!!parse-error (type => 'charset label:matching', ## TODO: type
517                            value => $charset_name,
518                            level => $self->{info_level});
519            $self->{confident} = 1;
520            return;
521          }
522    
523          !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.
524              ':'.$charset_name, level => 'w', token => $token);
525          
526          ## Step 3
527          # if (can) {
528            ## change the encoding on the fly.
529            #$self->{confident} = 1;
530            #return;
531          # }
532          
533          ## Step 4
534          throw Whatpm::HTML::RestartParser ();
535        }
536    }; # $self->{change_encoding}    }; # $self->{change_encoding}
537    
538      my $char_onerror = sub {
539        my (undef, $type, %opt) = @_;
540        !!!parse-error (%opt, type => $type,
541                        line => $self->{line}, column => $self->{column} + 1);
542        if ($opt{octets}) {
543          ${$opt{octets}} = "\x{FFFD}"; # relacement character
544        }
545      };
546      $char_stream->onerror ($char_onerror);
547    
548    my @args = @_; shift @args; # $s    my @args = @_; shift @args; # $s
549    my $return;    my $return;
550    try {    try {
551      $return = $self->parse_char_string ($s, @args);        $return = $self->parse_char_stream ($char_stream, @args);  
552    } catch Whatpm::HTML::RestartParser with {    } catch Whatpm::HTML::RestartParser with {
553      my $charset = shift->{charset};      ## NOTE: Invoked after {change_encoding}.
554      $s = \ (Encode::decode ($charset, $$bytes_s));      
555      $self->{input_encoding} = $charset; ## TODO: normalize      $self->{input_encoding} = $charset->get_iana_name;
556        if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
557          !!!parse-error (type => 'chardecode:fallback', ## TODO: type name
558                          value => $self->{input_encoding},
559                          level => $self->{unsupported_level},
560                          line => 1, column => 1);
561        } elsif (not ($e_status &
562                      Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
563          !!!parse-error (type => 'chardecode:no error', ## TODO: type name
564                          value => $self->{input_encoding},
565                          level => $self->{unsupported_level},
566                          line => 1, column => 1);
567        }
568      $self->{confident} = 1;      $self->{confident} = 1;
569      $return = $self->parse_char_string ($s, @args);      $char_stream->onerror ($char_onerror);
570        $return = $self->parse_char_stream ($char_stream, @args);
571    };    };
572    return $return;    return $return;
573  } # parse_byte_string  } # parse_byte_stream
574    
575  ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM  ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
576  ## 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 581  sub parse_byte_string ($$$$;$) {
581  ## 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
582  ## strip the BOM and never strip any ZWNBSP.  ## strip the BOM and never strip any ZWNBSP.
583    
584  *parse_char_string = \&parse_string;  sub parse_char_string ($$$;$) {
585      my $self = shift;
586      require utf8;
587      my $s = ref $_[0] ? $_[0] : \($_[0]);
588      open my $input, '<' . (utf8::is_utf8 ($$s) ? ':utf8' : ''), $s;
589      return $self->parse_char_stream ($input, @_[1..$#_]);
590    } # parse_char_string
591    *parse_string = \&parse_char_string;
592    
593  sub parse_string ($$$;$) {  sub parse_char_stream ($$$;$) {
594    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
595    my $s = ref $_[0] ? $_[0] : \($_[0]);    my $input = $_[0];
596    $self->{document} = $_[1];    $self->{document} = $_[1];
597    @{$self->{document}->child_nodes} = ();    @{$self->{document}->child_nodes} = ();
598    
# Line 177  sub parse_string ($$$;$) { Line 603  sub parse_string ($$$;$) {
603        if defined $self->{input_encoding};        if defined $self->{input_encoding};
604    
605    my $i = 0;    my $i = 0;
606    my $line = 1;    $self->{line_prev} = $self->{line} = 1;
607    my $column = 0;    $self->{column_prev} = $self->{column} = 0;
608    $self->{set_next_char} = sub {    $self->{set_next_char} = sub {
609      my $self = shift;      my $self = shift;
610    
611      pop @{$self->{prev_char}};      pop @{$self->{prev_char}};
612      unshift @{$self->{prev_char}}, $self->{next_char};      unshift @{$self->{prev_char}}, $self->{next_char};
613    
614      $self->{next_char} = -1 and return if $i >= length $$s;      my $char;
615      $self->{next_char} = ord substr $$s, $i++, 1;      if (defined $self->{next_next_char}) {
616      $column++;        $char = $self->{next_next_char};
617          delete $self->{next_next_char};
618        } else {
619          $char = $input->getc;
620        }
621        $self->{next_char} = -1 and return unless defined $char;
622        $self->{next_char} = ord $char;
623    
624        ($self->{line_prev}, $self->{column_prev})
625            = ($self->{line}, $self->{column});
626        $self->{column}++;
627            
628      if ($self->{next_char} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
629        $line++;        !!!cp ('j1');
630        $column = 0;        $self->{line}++;
631          $self->{column} = 0;
632      } elsif ($self->{next_char} == 0x000D) { # CR      } elsif ($self->{next_char} == 0x000D) { # CR
633        $i++ if substr ($$s, $i, 1) eq "\x0A";        !!!cp ('j2');
634          my $next = $input->getc;
635          if (defined $next and $next ne "\x0A") {
636            $self->{next_next_char} = $next;
637          }
638        $self->{next_char} = 0x000A; # LF # MUST        $self->{next_char} = 0x000A; # LF # MUST
639        $line++;        $self->{line}++;
640        $column = 0;        $self->{column} = 0;
641      } elsif ($self->{next_char} > 0x10FFFF) {      } elsif ($self->{next_char} > 0x10FFFF) {
642          !!!cp ('j3');
643        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
644      } elsif ($self->{next_char} == 0x0000) { # NULL      } elsif ($self->{next_char} == 0x0000) { # NULL
645          !!!cp ('j4');
646        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
647        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
648        } elsif ($self->{next_char} <= 0x0008 or
649                 (0x000E <= $self->{next_char} and $self->{next_char} <= 0x001F) or
650                 (0x007F <= $self->{next_char} and $self->{next_char} <= 0x009F) or
651                 (0xD800 <= $self->{next_char} and $self->{next_char} <= 0xDFFF) or
652                 (0xFDD0 <= $self->{next_char} and $self->{next_char} <= 0xFDDF) or
653                 {
654                  0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
655                  0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
656                  0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
657                  0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
658                  0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
659                  0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
660                  0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
661                  0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
662                  0x10FFFE => 1, 0x10FFFF => 1,
663                 }->{$self->{next_char}}) {
664          !!!cp ('j5');
665          !!!parse-error (type => 'control char', level => $self->{must_level});
666    ## TODO: error type documentation
667      }      }
668    };    };
669    $self->{prev_char} = [-1, -1, -1];    $self->{prev_char} = [-1, -1, -1];
# Line 209  sub parse_string ($$$;$) { Line 671  sub parse_string ($$$;$) {
671    
672    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
673      my (%opt) = @_;      my (%opt) = @_;
674      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
675        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
676        warn "Parse error ($opt{type}) at line $line column $column\n";
677    };    };
678    $self->{parse_error} = sub {    $self->{parse_error} = sub {
679      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
680    };    };
681    
682    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
# Line 220  sub parse_string ($$$;$) { Line 684  sub parse_string ($$$;$) {
684    $self->_construct_tree;    $self->_construct_tree;
685    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
686    
687      delete $self->{parse_error}; # remove loop
688    
689    return $self->{document};    return $self->{document};
690  } # parse_string  } # parse_char_stream
691    
692  sub new ($) {  sub new ($) {
693    my $class = shift;    my $class = shift;
694    my $self = bless {}, $class;    my $self = bless {
695        must_level => 'm',
696        should_level => 's',
697        good_level => 'w',
698        warn_level => 'w',
699        info_level => 'i',
700        unsupported_level => 'u',
701      }, $class;
702    $self->{set_next_char} = sub {    $self->{set_next_char} = sub {
703      $self->{next_char} = -1;      $self->{next_char} = -1;
704    };    };
# Line 287  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO Line 760  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO
760  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
761  sub BOGUS_DOCTYPE_STATE () { 32 }  sub BOGUS_DOCTYPE_STATE () { 32 }
762  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
763    sub SELF_CLOSING_START_TAG_STATE () { 34 }
764    sub CDATA_BLOCK_STATE () { 35 }
765    
766  sub DOCTYPE_TOKEN () { 1 }  sub DOCTYPE_TOKEN () { 1 }
767  sub COMMENT_TOKEN () { 2 }  sub COMMENT_TOKEN () { 2 }
# Line 303  sub TABLE_IMS ()      { 0b1000000 } Line 778  sub TABLE_IMS ()      { 0b1000000 }
778  sub ROW_IMS ()        { 0b10000000 }  sub ROW_IMS ()        { 0b10000000 }
779  sub BODY_AFTER_IMS () { 0b100000000 }  sub BODY_AFTER_IMS () { 0b100000000 }
780  sub FRAME_IMS ()      { 0b1000000000 }  sub FRAME_IMS ()      { 0b1000000000 }
781    sub SELECT_IMS ()     { 0b10000000000 }
782    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
783        ## NOTE: "in foreign content" insertion mode is special; it is combined
784        ## with the secondary insertion mode.  In this parser, they are stored
785        ## together in the bit-or'ed form.
786    
787  ## NOTE: "initial" and "before html" insertion modes have no constants.  ## NOTE: "initial" and "before html" insertion modes have no constants.
788    
# Line 325  sub IN_TABLE_IM () { TABLE_IMS } Line 805  sub IN_TABLE_IM () { TABLE_IMS }
805  sub AFTER_BODY_IM () { BODY_AFTER_IMS }  sub AFTER_BODY_IM () { BODY_AFTER_IMS }
806  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
807  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
808  sub IN_SELECT_IM () { 0b01 }  sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
809    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
810  sub IN_COLUMN_GROUP_IM () { 0b10 }  sub IN_COLUMN_GROUP_IM () { 0b10 }
811    
812  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
# Line 338  sub _initialize_tokenizer ($) { Line 819  sub _initialize_tokenizer ($) {
819    undef $self->{current_attribute};    undef $self->{current_attribute};
820    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
821    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
822      delete $self->{self_closing};
823    $self->{char} = [];    $self->{char} = [];
824    # $self->{next_char}    # $self->{next_char}
825    !!!next-input-character;    !!!next-input-character;
# Line 358  sub _initialize_tokenizer ($) { Line 840  sub _initialize_tokenizer ($) {
840  ##        ->{value}  ##        ->{value}
841  ##        ->{has_reference} == 1 or 0  ##        ->{has_reference} == 1 or 0
842  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
843    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
844    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
845    ##     while the token is pushed back to the stack.
846    
847  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
848    
# Line 384  sub _initialize_tokenizer ($) { Line 869  sub _initialize_tokenizer ($) {
869    
870  sub _get_next_token ($) {  sub _get_next_token ($) {
871    my $self = shift;    my $self = shift;
872    
873      if ($self->{self_closing}) {
874        !!!parse-error (type => 'nestc', token => $self->{current_token});
875        ## NOTE: The |self_closing| flag is only set by start tag token.
876        ## In addition, when a start tag token is emitted, it is always set to
877        ## |current_token|.
878        delete $self->{self_closing};
879      }
880    
881    if (@{$self->{token}}) {    if (@{$self->{token}}) {
882        $self->{self_closing} = $self->{token}->[0]->{self_closing};
883      return shift @{$self->{token}};      return shift @{$self->{token}};
884    }    }
885    
# Line 447  sub _get_next_token ($) { Line 942  sub _get_next_token ($) {
942          #          #
943        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
944          !!!cp (11);          !!!cp (11);
945          !!!emit ({type => END_OF_FILE_TOKEN});          !!!emit ({type => END_OF_FILE_TOKEN,
946                      line => $self->{line}, column => $self->{column}});
947          last A; ## TODO: ok?          last A; ## TODO: ok?
948        } else {        } else {
949          !!!cp (12);          !!!cp (12);
950        }        }
951        # Anything else        # Anything else
952        my $token = {type => CHARACTER_TOKEN,        my $token = {type => CHARACTER_TOKEN,
953                     data => chr $self->{next_char}};                     data => chr $self->{next_char},
954                       line => $self->{line}, column => $self->{column},
955                      };
956        ## Stay in the data state        ## Stay in the data state
957        !!!next-input-character;        !!!next-input-character;
958    
# Line 463  sub _get_next_token ($) { Line 961  sub _get_next_token ($) {
961        redo A;        redo A;
962      } elsif ($self->{state} == ENTITY_DATA_STATE) {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
963        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
964    
965          my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
966                
967        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);
968    
# Line 471  sub _get_next_token ($) { Line 971  sub _get_next_token ($) {
971    
972        unless (defined $token) {        unless (defined $token) {
973          !!!cp (13);          !!!cp (13);
974          !!!emit ({type => CHARACTER_TOKEN, data => '&'});          !!!emit ({type => CHARACTER_TOKEN, data => '&',
975                      line => $l, column => $c,
976                     });
977        } else {        } else {
978          !!!cp (14);          !!!cp (14);
979          !!!emit ($token);          !!!emit ($token);
# Line 490  sub _get_next_token ($) { Line 992  sub _get_next_token ($) {
992            ## reconsume            ## reconsume
993            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
994    
995            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
996                        line => $self->{line_prev},
997                        column => $self->{column_prev},
998                       });
999    
1000            redo A;            redo A;
1001          }          }
# Line 510  sub _get_next_token ($) { Line 1015  sub _get_next_token ($) {
1015            !!!cp (19);            !!!cp (19);
1016            $self->{current_token}            $self->{current_token}
1017              = {type => START_TAG_TOKEN,              = {type => START_TAG_TOKEN,
1018                 tag_name => chr ($self->{next_char} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020),
1019                   line => $self->{line_prev},
1020                   column => $self->{column_prev}};
1021            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
1022            !!!next-input-character;            !!!next-input-character;
1023            redo A;            redo A;
# Line 518  sub _get_next_token ($) { Line 1025  sub _get_next_token ($) {
1025                   $self->{next_char} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
1026            !!!cp (20);            !!!cp (20);
1027            $self->{current_token} = {type => START_TAG_TOKEN,            $self->{current_token} = {type => START_TAG_TOKEN,
1028                              tag_name => chr ($self->{next_char})};                                      tag_name => chr ($self->{next_char}),
1029                                        line => $self->{line_prev},
1030                                        column => $self->{column_prev}};
1031            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
1032            !!!next-input-character;            !!!next-input-character;
1033            redo A;            redo A;
1034          } elsif ($self->{next_char} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
1035            !!!cp (21);            !!!cp (21);
1036            !!!parse-error (type => 'empty start tag');            !!!parse-error (type => 'empty start tag',
1037                              line => $self->{line_prev},
1038                              column => $self->{column_prev});
1039            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1040            !!!next-input-character;            !!!next-input-character;
1041    
1042            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
1043                        line => $self->{line_prev},
1044                        column => $self->{column_prev},
1045                       });
1046    
1047            redo A;            redo A;
1048          } elsif ($self->{next_char} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
1049            !!!cp (22);            !!!cp (22);
1050            !!!parse-error (type => 'pio');            !!!parse-error (type => 'pio',
1051                              line => $self->{line_prev},
1052                              column => $self->{column_prev});
1053            $self->{state} = BOGUS_COMMENT_STATE;            $self->{state} = BOGUS_COMMENT_STATE;
1054              $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1055                                        line => $self->{line_prev},
1056                                        column => $self->{column_prev},
1057                                       };
1058            ## $self->{next_char} is intentionally left as is            ## $self->{next_char} is intentionally left as is
1059            redo A;            redo A;
1060          } else {          } else {
1061            !!!cp (23);            !!!cp (23);
1062            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago',
1063                              line => $self->{line_prev},
1064                              column => $self->{column_prev});
1065            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1066            ## reconsume            ## reconsume
1067    
1068            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1069                        line => $self->{line_prev},
1070                        column => $self->{column_prev},
1071                       });
1072    
1073            redo A;            redo A;
1074          }          }
# Line 551  sub _get_next_token ($) { Line 1076  sub _get_next_token ($) {
1076          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
1077        }        }
1078      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
1079          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
1080        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1081          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
1082    
1083            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
1084            my @next_char;            my @next_char;
1085            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 569  sub _get_next_token ($) { Line 1096  sub _get_next_token ($) {
1096                !!!back-next-input-character (@next_char);                !!!back-next-input-character (@next_char);
1097                $self->{state} = DATA_STATE;                $self->{state} = DATA_STATE;
1098    
1099                !!!emit ({type => CHARACTER_TOKEN, data => '</'});                !!!emit ({type => CHARACTER_TOKEN, data => '</',
1100                            line => $l, column => $c,
1101                           });
1102        
1103                redo A;                redo A;
1104              }              }
# Line 588  sub _get_next_token ($) { Line 1117  sub _get_next_token ($) {
1117              $self->{next_char} = shift @next_char; # reconsume              $self->{next_char} = shift @next_char; # reconsume
1118              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
1119              $self->{state} = DATA_STATE;              $self->{state} = DATA_STATE;
1120              !!!emit ({type => CHARACTER_TOKEN, data => '</'});              !!!emit ({type => CHARACTER_TOKEN, data => '</',
1121                          line => $l, column => $c,
1122                         });
1123              redo A;              redo A;
1124            } else {            } else {
1125              !!!cp (27);              !!!cp (27);
# Line 601  sub _get_next_token ($) { Line 1132  sub _get_next_token ($) {
1132            !!!cp (28);            !!!cp (28);
1133            # next-input-character is already done            # next-input-character is already done
1134            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1135            !!!emit ({type => CHARACTER_TOKEN, data => '</'});            !!!emit ({type => CHARACTER_TOKEN, data => '</',
1136                        line => $l, column => $c,
1137                       });
1138            redo A;            redo A;
1139          }          }
1140        }        }
# Line 609  sub _get_next_token ($) { Line 1142  sub _get_next_token ($) {
1142        if (0x0041 <= $self->{next_char} and        if (0x0041 <= $self->{next_char} and
1143            $self->{next_char} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
1144          !!!cp (29);          !!!cp (29);
1145          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token}
1146                            tag_name => chr ($self->{next_char} + 0x0020)};              = {type => END_TAG_TOKEN,
1147                   tag_name => chr ($self->{next_char} + 0x0020),
1148                   line => $l, column => $c};
1149          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1150          !!!next-input-character;          !!!next-input-character;
1151          redo A;          redo A;
# Line 618  sub _get_next_token ($) { Line 1153  sub _get_next_token ($) {
1153                 $self->{next_char} <= 0x007A) { # a..z                 $self->{next_char} <= 0x007A) { # a..z
1154          !!!cp (30);          !!!cp (30);
1155          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token} = {type => END_TAG_TOKEN,
1156                            tag_name => chr ($self->{next_char})};                                    tag_name => chr ($self->{next_char}),
1157                                      line => $l, column => $c};
1158          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1159          !!!next-input-character;          !!!next-input-character;
1160          redo A;          redo A;
1161        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1162          !!!cp (31);          !!!cp (31);
1163          !!!parse-error (type => 'empty end tag');          !!!parse-error (type => 'empty end tag',
1164                            line => $self->{line_prev}, ## "<" in "</>"
1165                            column => $self->{column_prev} - 1);
1166          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1167          !!!next-input-character;          !!!next-input-character;
1168          redo A;          redo A;
# Line 634  sub _get_next_token ($) { Line 1172  sub _get_next_token ($) {
1172          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1173          # reconsume          # reconsume
1174    
1175          !!!emit ({type => CHARACTER_TOKEN, data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
1176                      line => $l, column => $c,
1177                     });
1178    
1179          redo A;          redo A;
1180        } else {        } else {
1181          !!!cp (33);          !!!cp (33);
1182          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
1183          $self->{state} = BOGUS_COMMENT_STATE;          $self->{state} = BOGUS_COMMENT_STATE;
1184            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1185                                      line => $self->{line_prev}, # "<" of "</"
1186                                      column => $self->{column_prev} - 1,
1187                                     };
1188          ## $self->{next_char} is intentionally left as is          ## $self->{next_char} is intentionally left as is
1189          redo A;          redo A;
1190        }        }
# Line 657  sub _get_next_token ($) { Line 1201  sub _get_next_token ($) {
1201        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1202          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1203            !!!cp (35);            !!!cp (35);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1204            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1205          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1206            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 690  sub _get_next_token ($) { Line 1232  sub _get_next_token ($) {
1232          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1233          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1234            !!!cp (39);            !!!cp (39);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1235            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1236          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1237            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 712  sub _get_next_token ($) { Line 1252  sub _get_next_token ($) {
1252    
1253          redo A;          redo A;
1254        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1255            !!!cp (42);
1256            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1257          !!!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  
1258          redo A;          redo A;
1259        } else {        } else {
1260          !!!cp (44);          !!!cp (44);
# Line 747  sub _get_next_token ($) { Line 1277  sub _get_next_token ($) {
1277        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1278          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1279            !!!cp (46);            !!!cp (46);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1280            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1281          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1282            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 770  sub _get_next_token ($) { Line 1298  sub _get_next_token ($) {
1298        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{next_char} and
1299                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1300          !!!cp (49);          !!!cp (49);
1301          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{current_attribute}
1302                                value => ''};              = {name => chr ($self->{next_char} + 0x0020),
1303                   value => '',
1304                   line => $self->{line}, column => $self->{column}};
1305          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1306          !!!next-input-character;          !!!next-input-character;
1307          redo A;          redo A;
1308        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1309            !!!cp (50);
1310            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1311          !!!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  
1312          redo A;          redo A;
1313        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1314          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1315          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1316            !!!cp (52);            !!!cp (52);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1317            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1318          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1319            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 825  sub _get_next_token ($) { Line 1343  sub _get_next_token ($) {
1343          } else {          } else {
1344            !!!cp (56);            !!!cp (56);
1345          }          }
1346          $self->{current_attribute} = {name => chr ($self->{next_char}),          $self->{current_attribute}
1347                                value => ''};              = {name => chr ($self->{next_char}),
1348                   value => '',
1349                   line => $self->{line}, column => $self->{column}};
1350          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1351          !!!next-input-character;          !!!next-input-character;
1352          redo A;          redo A;
# Line 836  sub _get_next_token ($) { Line 1356  sub _get_next_token ($) {
1356          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1357              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1358            !!!cp (57);            !!!cp (57);
1359            !!!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});
1360            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1361          } else {          } else {
1362            !!!cp (58);            !!!cp (58);
# Line 865  sub _get_next_token ($) { Line 1385  sub _get_next_token ($) {
1385          $before_leave->();          $before_leave->();
1386          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1387            !!!cp (61);            !!!cp (61);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1388            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1389          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1390            !!!cp (62);            !!!cp (62);
# Line 891  sub _get_next_token ($) { Line 1409  sub _get_next_token ($) {
1409          !!!next-input-character;          !!!next-input-character;
1410          redo A;          redo A;
1411        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1412            !!!cp (64);
1413          $before_leave->();          $before_leave->();
1414            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1415          !!!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  
1416          redo A;          redo A;
1417        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1418          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1419          $before_leave->();          $before_leave->();
1420          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1421            !!!cp (66);            !!!cp (66);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1422            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1423          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1424            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 963  sub _get_next_token ($) { Line 1469  sub _get_next_token ($) {
1469        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1470          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1471            !!!cp (73);            !!!cp (73);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1472            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1473          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1474            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 987  sub _get_next_token ($) { Line 1491  sub _get_next_token ($) {
1491        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{next_char} and
1492                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1493          !!!cp (76);          !!!cp (76);
1494          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{current_attribute}
1495                                value => ''};              = {name => chr ($self->{next_char} + 0x0020),
1496                   value => '',
1497                   line => $self->{line}, column => $self->{column}};
1498          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1499          !!!next-input-character;          !!!next-input-character;
1500          redo A;          redo A;
1501        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1502            !!!cp (77);
1503            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1504          !!!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  
1505          redo A;          redo A;
1506        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1507          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1508          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1509            !!!cp (79);            !!!cp (79);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1510            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1511          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1512            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1035  sub _get_next_token ($) { Line 1528  sub _get_next_token ($) {
1528          redo A;          redo A;
1529        } else {        } else {
1530          !!!cp (82);          !!!cp (82);
1531          $self->{current_attribute} = {name => chr ($self->{next_char}),          $self->{current_attribute}
1532                                value => ''};              = {name => chr ($self->{next_char}),
1533                   value => '',
1534                   line => $self->{line}, column => $self->{column}};
1535          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1536          !!!next-input-character;          !!!next-input-character;
1537          redo A;                  redo A;        
# Line 1069  sub _get_next_token ($) { Line 1564  sub _get_next_token ($) {
1564        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1565          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1566            !!!cp (87);            !!!cp (87);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1567            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1568          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1569            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1094  sub _get_next_token ($) { Line 1587  sub _get_next_token ($) {
1587          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1588          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1589            !!!cp (90);            !!!cp (90);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1590            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1591          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1592            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1143  sub _get_next_token ($) { Line 1634  sub _get_next_token ($) {
1634          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1635          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1636            !!!cp (97);            !!!cp (97);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1637            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1638          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1639            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1187  sub _get_next_token ($) { Line 1676  sub _get_next_token ($) {
1676          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1677          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1678            !!!cp (103);            !!!cp (103);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1679            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1680          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1681            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1234  sub _get_next_token ($) { Line 1721  sub _get_next_token ($) {
1721        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1722          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1723            !!!cp (109);            !!!cp (109);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1724            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1725          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1726            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1259  sub _get_next_token ($) { Line 1744  sub _get_next_token ($) {
1744          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1745          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1746            !!!cp (112);            !!!cp (112);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1747            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1748          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1749            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1331  sub _get_next_token ($) { Line 1814  sub _get_next_token ($) {
1814        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1815          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1816            !!!cp (119);            !!!cp (119);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1817            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1818          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1819            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1353  sub _get_next_token ($) { Line 1834  sub _get_next_token ($) {
1834    
1835          redo A;          redo A;
1836        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1837            !!!cp (122);
1838            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1839          !!!next-input-character;          !!!next-input-character;
1840          if ($self->{next_char} == 0x003E and # >          redo A;
1841              $self->{current_token}->{type} == START_TAG_TOKEN and        } elsif ($self->{next_char} == -1) {
1842              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {          !!!parse-error (type => 'unclosed tag');
1843            # permitted slash          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1844            !!!cp (122);            !!!cp (122.3);
1845            #            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1846            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1847              if ($self->{current_token}->{attributes}) {
1848                !!!cp (122.1);
1849                !!!parse-error (type => 'end tag attribute');
1850              } else {
1851                ## NOTE: This state should never be reached.
1852                !!!cp (122.2);
1853              }
1854          } else {          } else {
1855            !!!cp (123);            die "$0: $self->{current_token}->{type}: Unknown token type";
           !!!parse-error (type => 'nestc');  
1856          }          }
1857          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = DATA_STATE;
1858          # next-input-character is already done          ## Reconsume.
1859            !!!emit ($self->{current_token}); # start tag or end tag
1860          redo A;          redo A;
1861        } else {        } else {
1862          !!!cp (124);          !!!cp ('124.1');
1863          !!!parse-error (type => 'no space between attributes');          !!!parse-error (type => 'no space between attributes');
1864          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1865          ## reconsume          ## reconsume
1866          redo A;          redo A;
1867        }        }
1868        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1869          if ($self->{next_char} == 0x003E) { # >
1870            if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1871              !!!cp ('124.2');
1872              !!!parse-error (type => 'nestc', token => $self->{current_token});
1873              ## TODO: Different type than slash in start tag
1874              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1875              if ($self->{current_token}->{attributes}) {
1876                !!!cp ('124.4');
1877                !!!parse-error (type => 'end tag attribute');
1878              } else {
1879                !!!cp ('124.5');
1880              }
1881              ## TODO: Test |<title></title/>|
1882            } else {
1883              !!!cp ('124.3');
1884              $self->{self_closing} = 1;
1885            }
1886    
1887            $self->{state} = DATA_STATE;
1888            !!!next-input-character;
1889    
1890            !!!emit ($self->{current_token}); # start tag or end tag
1891    
1892            redo A;
1893          } elsif ($self->{next_char} == -1) {
1894            !!!parse-error (type => 'unclosed tag');
1895            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1896              !!!cp (124.7);
1897              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1898            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1899              if ($self->{current_token}->{attributes}) {
1900                !!!cp (124.5);
1901                !!!parse-error (type => 'end tag attribute');
1902              } else {
1903                ## NOTE: This state should never be reached.
1904                !!!cp (124.6);
1905              }
1906            } else {
1907              die "$0: $self->{current_token}->{type}: Unknown token type";
1908            }
1909            $self->{state} = DATA_STATE;
1910            ## Reconsume.
1911            !!!emit ($self->{current_token}); # start tag or end tag
1912            redo A;
1913          } else {
1914            !!!cp ('124.4');
1915            !!!parse-error (type => 'nestc');
1916            ## TODO: This error type is wrong.
1917            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1918            ## Reconsume.
1919            redo A;
1920          }
1921      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1922        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1923                
1924        my $token = {type => COMMENT_TOKEN, data => ''};        ## NOTE: Set by the previous state
1925          #my $token = {type => COMMENT_TOKEN, data => ''};
1926    
1927        BC: {        BC: {
1928          if ($self->{next_char} == 0x003E) { # >          if ($self->{next_char} == 0x003E) { # >
# Line 1385  sub _get_next_token ($) { Line 1930  sub _get_next_token ($) {
1930            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1931            !!!next-input-character;            !!!next-input-character;
1932    
1933            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1934    
1935            redo A;            redo A;
1936          } elsif ($self->{next_char} == -1) {          } elsif ($self->{next_char} == -1) {
# Line 1393  sub _get_next_token ($) { Line 1938  sub _get_next_token ($) {
1938            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1939            ## reconsume            ## reconsume
1940    
1941            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1942    
1943            redo A;            redo A;
1944          } else {          } else {
1945            !!!cp (126);            !!!cp (126);
1946            $token->{data} .= chr ($self->{next_char});            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1947            !!!next-input-character;            !!!next-input-character;
1948            redo BC;            redo BC;
1949          }          }
# Line 1408  sub _get_next_token ($) { Line 1953  sub _get_next_token ($) {
1953      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1954        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1955    
1956          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1);
1957    
1958        my @next_char;        my @next_char;
1959        push @next_char, $self->{next_char};        push @next_char, $self->{next_char};
1960                
# Line 1416  sub _get_next_token ($) { Line 1963  sub _get_next_token ($) {
1963          push @next_char, $self->{next_char};          push @next_char, $self->{next_char};
1964          if ($self->{next_char} == 0x002D) { # -          if ($self->{next_char} == 0x002D) { # -
1965            !!!cp (127);            !!!cp (127);
1966            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1967                                        line => $l, column => $c,
1968                                       };
1969            $self->{state} = COMMENT_START_STATE;            $self->{state} = COMMENT_START_STATE;
1970            !!!next-input-character;            !!!next-input-character;
1971            redo A;            redo A;
# Line 1452  sub _get_next_token ($) { Line 2001  sub _get_next_token ($) {
2001                      !!!cp (129);                      !!!cp (129);
2002                      ## TODO: What a stupid code this is!                      ## TODO: What a stupid code this is!
2003                      $self->{state} = DOCTYPE_STATE;                      $self->{state} = DOCTYPE_STATE;
2004                        $self->{current_token} = {type => DOCTYPE_TOKEN,
2005                                                  quirks => 1,
2006                                                  line => $l, column => $c,
2007                                                 };
2008                      !!!next-input-character;                      !!!next-input-character;
2009                      redo A;                      redo A;
2010                    } else {                    } else {
# Line 1472  sub _get_next_token ($) { Line 2025  sub _get_next_token ($) {
2025          } else {          } else {
2026            !!!cp (135);            !!!cp (135);
2027          }          }
2028          } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
2029                   $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
2030                   $self->{next_char} == 0x005B) { # [
2031            !!!next-input-character;
2032            push @next_char, $self->{next_char};
2033            if ($self->{next_char} == 0x0043) { # C
2034              !!!next-input-character;
2035              push @next_char, $self->{next_char};
2036              if ($self->{next_char} == 0x0044) { # D
2037                !!!next-input-character;
2038                push @next_char, $self->{next_char};
2039                if ($self->{next_char} == 0x0041) { # A
2040                  !!!next-input-character;
2041                  push @next_char, $self->{next_char};
2042                  if ($self->{next_char} == 0x0054) { # T
2043                    !!!next-input-character;
2044                    push @next_char, $self->{next_char};
2045                    if ($self->{next_char} == 0x0041) { # A
2046                      !!!next-input-character;
2047                      push @next_char, $self->{next_char};
2048                      if ($self->{next_char} == 0x005B) { # [
2049                        !!!cp (135.1);
2050                        $self->{state} = CDATA_BLOCK_STATE;
2051                        !!!next-input-character;
2052                        redo A;
2053                      } else {
2054                        !!!cp (135.2);
2055                      }
2056                    } else {
2057                      !!!cp (135.3);
2058                    }
2059                  } else {
2060                    !!!cp (135.4);                
2061                  }
2062                } else {
2063                  !!!cp (135.5);
2064                }
2065              } else {
2066                !!!cp (135.6);
2067              }
2068            } else {
2069              !!!cp (135.7);
2070            }
2071        } else {        } else {
2072          !!!cp (136);          !!!cp (136);
2073        }        }
# Line 1480  sub _get_next_token ($) { Line 2076  sub _get_next_token ($) {
2076        $self->{next_char} = shift @next_char;        $self->{next_char} = shift @next_char;
2077        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
2078        $self->{state} = BOGUS_COMMENT_STATE;        $self->{state} = BOGUS_COMMENT_STATE;
2079          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2080                                    line => $l, column => $c,
2081                                   };
2082        redo A;        redo A;
2083                
2084        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
# Line 1603  sub _get_next_token ($) { Line 2202  sub _get_next_token ($) {
2202          redo A;          redo A;
2203        } elsif ($self->{next_char} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
2204          !!!cp (152);          !!!cp (152);
2205          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment',
2206                            line => $self->{line_prev},
2207                            column => $self->{column_prev});
2208          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
2209          ## Stay in the state          ## Stay in the state
2210          !!!next-input-character;          !!!next-input-character;
# Line 1619  sub _get_next_token ($) { Line 2220  sub _get_next_token ($) {
2220          redo A;          redo A;
2221        } else {        } else {
2222          !!!cp (154);          !!!cp (154);
2223          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment',
2224                            line => $self->{line_prev},
2225                            column => $self->{column_prev});
2226          $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment          $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
2227          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2228          !!!next-input-character;          !!!next-input-character;
# Line 1658  sub _get_next_token ($) { Line 2261  sub _get_next_token ($) {
2261          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2262          !!!next-input-character;          !!!next-input-character;
2263    
2264          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2265    
2266          redo A;          redo A;
2267        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
# Line 1667  sub _get_next_token ($) { Line 2270  sub _get_next_token ($) {
2270          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2271          ## reconsume          ## reconsume
2272    
2273          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2274    
2275          redo A;          redo A;
2276        } else {        } else {
2277          !!!cp (160);          !!!cp (160);
2278          $self->{current_token}          $self->{current_token}->{name} = chr $self->{next_char};
2279              = {type => DOCTYPE_TOKEN,          delete $self->{current_token}->{quirks};
                name => chr ($self->{next_char}),  
                #quirks => 0,  
               };  
2280  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
2281          $self->{state} = DOCTYPE_NAME_STATE;          $self->{state} = DOCTYPE_NAME_STATE;
2282          !!!next-input-character;          !!!next-input-character;
# Line 2151  sub _get_next_token ($) { Line 2751  sub _get_next_token ($) {
2751        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
2752          !!!cp (217);          !!!cp (217);
2753          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
   
2754          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2755          ## reconsume          ## reconsume
2756    
# Line 2192  sub _get_next_token ($) { Line 2791  sub _get_next_token ($) {
2791          !!!next-input-character;          !!!next-input-character;
2792          redo A;          redo A;
2793        }        }
2794        } elsif ($self->{state} == CDATA_BLOCK_STATE) {
2795          my $s = '';
2796          
2797          my ($l, $c) = ($self->{line}, $self->{column});
2798    
2799          CS: while ($self->{next_char} != -1) {
2800            if ($self->{next_char} == 0x005D) { # ]
2801              !!!next-input-character;
2802              if ($self->{next_char} == 0x005D) { # ]
2803                !!!next-input-character;
2804                MDC: {
2805                  if ($self->{next_char} == 0x003E) { # >
2806                    !!!cp (221.1);
2807                    !!!next-input-character;
2808                    last CS;
2809                  } elsif ($self->{next_char} == 0x005D) { # ]
2810                    !!!cp (221.2);
2811                    $s .= ']';
2812                    !!!next-input-character;
2813                    redo MDC;
2814                  } else {
2815                    !!!cp (221.3);
2816                    $s .= ']]';
2817                    #
2818                  }
2819                } # MDC
2820              } else {
2821                !!!cp (221.4);
2822                $s .= ']';
2823                #
2824              }
2825            } else {
2826              !!!cp (221.5);
2827              #
2828            }
2829            $s .= chr $self->{next_char};
2830            !!!next-input-character;
2831          } # CS
2832    
2833          $self->{state} = DATA_STATE;
2834          ## next-input-character done or EOF, which is reconsumed.
2835    
2836          if (length $s) {
2837            !!!cp (221.6);
2838            !!!emit ({type => CHARACTER_TOKEN, data => $s,
2839                      line => $l, column => $c});
2840          } else {
2841            !!!cp (221.7);
2842          }
2843    
2844          redo A;
2845    
2846          ## ISSUE: "text tokens" in spec.
2847          ## TODO: Streaming support
2848      } else {      } else {
2849        die "$0: $self->{state}: Unknown state";        die "$0: $self->{state}: Unknown state";
2850      }      }
# Line 2203  sub _get_next_token ($) { Line 2856  sub _get_next_token ($) {
2856  sub _tokenize_attempt_to_consume_an_entity ($$$) {  sub _tokenize_attempt_to_consume_an_entity ($$$) {
2857    my ($self, $in_attr, $additional) = @_;    my ($self, $in_attr, $additional) = @_;
2858    
2859      my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
2860    
2861    if ({    if ({
2862         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
2863         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
# Line 2243  sub _tokenize_attempt_to_consume_an_enti Line 2898  sub _tokenize_attempt_to_consume_an_enti
2898            redo X;            redo X;
2899          } elsif (not defined $code) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
2900            !!!cp (1005);            !!!cp (1005);
2901            !!!parse-error (type => 'bare hcro');            !!!parse-error (type => 'bare hcro', line => $l, column => $c);
2902            !!!back-next-input-character ($x_char, $self->{next_char});            !!!back-next-input-character ($x_char, $self->{next_char});
2903            $self->{next_char} = 0x0023; # #            $self->{next_char} = 0x0023; # #
2904            return undef;            return undef;
# Line 2252  sub _tokenize_attempt_to_consume_an_enti Line 2907  sub _tokenize_attempt_to_consume_an_enti
2907            !!!next-input-character;            !!!next-input-character;
2908          } else {          } else {
2909            !!!cp (1007);            !!!cp (1007);
2910            !!!parse-error (type => 'no refc');            !!!parse-error (type => 'no refc', line => $l, column => $c);
2911          }          }
2912    
2913          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2914            !!!cp (1008);            !!!cp (1008);
2915            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);            !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2916            $code = 0xFFFD;            $code = 0xFFFD;
2917          } elsif ($code > 0x10FFFF) {          } elsif ($code > 0x10FFFF) {
2918            !!!cp (1009);            !!!cp (1009);
2919            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);            !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2920            $code = 0xFFFD;            $code = 0xFFFD;
2921          } elsif ($code == 0x000D) {          } elsif ($code == 0x000D) {
2922            !!!cp (1010);            !!!cp (1010);
2923            !!!parse-error (type => 'CR character reference');            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2924            $code = 0x000A;            $code = 0x000A;
2925          } elsif (0x80 <= $code and $code <= 0x9F) {          } elsif (0x80 <= $code and $code <= 0x9F) {
2926            !!!cp (1011);            !!!cp (1011);
2927            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);            !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2928            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
2929          }          }
2930    
2931          return {type => CHARACTER_TOKEN, data => chr $code,          return {type => CHARACTER_TOKEN, data => chr $code,
2932                  has_reference => 1};                  has_reference => 1,
2933                    line => $l, column => $c,
2934                   };
2935        } # X        } # X
2936      } elsif (0x0030 <= $self->{next_char} and      } elsif (0x0030 <= $self->{next_char} and
2937               $self->{next_char} <= 0x0039) { # 0..9               $self->{next_char} <= 0x0039) { # 0..9
# Line 2295  sub _tokenize_attempt_to_consume_an_enti Line 2952  sub _tokenize_attempt_to_consume_an_enti
2952          !!!next-input-character;          !!!next-input-character;
2953        } else {        } else {
2954          !!!cp (1014);          !!!cp (1014);
2955          !!!parse-error (type => 'no refc');          !!!parse-error (type => 'no refc', line => $l, column => $c);
2956        }        }
2957    
2958        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2959          !!!cp (1015);          !!!cp (1015);
2960          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);          !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2961          $code = 0xFFFD;          $code = 0xFFFD;
2962        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
2963          !!!cp (1016);          !!!cp (1016);
2964          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);          !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2965          $code = 0xFFFD;          $code = 0xFFFD;
2966        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
2967          !!!cp (1017);          !!!cp (1017);
2968          !!!parse-error (type => 'CR character reference');          !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2969          $code = 0x000A;          $code = 0x000A;
2970        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
2971          !!!cp (1018);          !!!cp (1018);
2972          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2973          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
2974        }        }
2975                
2976        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,
2977                  line => $l, column => $c,
2978                 };
2979      } else {      } else {
2980        !!!cp (1019);        !!!cp (1019);
2981        !!!parse-error (type => 'bare nero');        !!!parse-error (type => 'bare nero', line => $l, column => $c);
2982        !!!back-next-input-character ($self->{next_char});        !!!back-next-input-character ($self->{next_char});
2983        $self->{next_char} = 0x0023; # #        $self->{next_char} = 0x0023; # #
2984        return undef;        return undef;
# Line 2336  sub _tokenize_attempt_to_consume_an_enti Line 2995  sub _tokenize_attempt_to_consume_an_enti
2995      require Whatpm::_NamedEntityList;      require Whatpm::_NamedEntityList;
2996      our $EntityChar;      our $EntityChar;
2997    
2998      while (length $entity_name < 10 and      while (length $entity_name < 30 and
2999             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
3000             ((0x0041 <= $self->{next_char} and # a             ((0x0041 <= $self->{next_char} and # a
3001               $self->{next_char} <= 0x005A) or # x               $self->{next_char} <= 0x005A) or # x
# Line 2369  sub _tokenize_attempt_to_consume_an_enti Line 3028  sub _tokenize_attempt_to_consume_an_enti
3028            
3029      if ($match > 0) {      if ($match > 0) {
3030        !!!cp (1023);        !!!cp (1023);
3031        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
3032                  line => $l, column => $c,
3033                 };
3034      } elsif ($match < 0) {      } elsif ($match < 0) {
3035        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc', line => $l, column => $c);
3036        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
3037          !!!cp (1024);          !!!cp (1024);
3038          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};          return {type => CHARACTER_TOKEN, data => '&'.$entity_name,
3039                    line => $l, column => $c,
3040                   };
3041        } else {        } else {
3042          !!!cp (1025);          !!!cp (1025);
3043          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
3044                    line => $l, column => $c,
3045                   };
3046        }        }
3047      } else {      } else {
3048        !!!cp (1026);        !!!cp (1026);
3049        !!!parse-error (type => 'bare ero');        !!!parse-error (type => 'bare ero', line => $l, column => $c);
3050        ## NOTE: "No characters are consumed" in the spec.        ## NOTE: "No characters are consumed" in the spec.
3051        return {type => CHARACTER_TOKEN, data => '&'.$value};        return {type => CHARACTER_TOKEN, data => '&'.$value,
3052                  line => $l, column => $c,
3053                 };
3054      }      }
3055    } else {    } else {
3056      !!!cp (1027);      !!!cp (1027);
3057      ## no characters are consumed      ## no characters are consumed
3058      !!!parse-error (type => 'bare ero');      !!!parse-error (type => 'bare ero', line => $l, column => $c);
3059      return undef;      return undef;
3060    }    }
3061  } # _tokenize_attempt_to_consume_an_entity  } # _tokenize_attempt_to_consume_an_entity
# Line 2459  sub _tree_construction_initial ($) { Line 3126  sub _tree_construction_initial ($) {
3126            defined $token->{public_identifier} or            defined $token->{public_identifier} or
3127            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
3128          !!!cp ('t1');          !!!cp ('t1');
3129          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
3130        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
3131          !!!cp ('t2');          !!!cp ('t2');
3132          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)
3133          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
3134        } else {        } else {
3135          !!!cp ('t3');          !!!cp ('t3');
3136        }        }
3137                
3138        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
3139          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3140          ## NOTE: Default value for both |public_id| and |system_id| attributes
3141          ## are empty strings, so that we don't set any value in missing cases.
3142        $doctype->public_id ($token->{public_identifier})        $doctype->public_id ($token->{public_identifier})
3143            if defined $token->{public_identifier};            if defined $token->{public_identifier};
3144        $doctype->system_id ($token->{system_identifier})        $doctype->system_id ($token->{system_identifier})
# Line 2484  sub _tree_construction_initial ($) { Line 3153  sub _tree_construction_initial ($) {
3153        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{public_identifier}) {
3154          my $pubid = $token->{public_identifier};          my $pubid = $token->{public_identifier};
3155          $pubid =~ tr/a-z/A-z/;          $pubid =~ tr/a-z/A-z/;
3156          if ({          my $prefix = [
3157            "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,            "+//SILMARIL//DTD HTML PRO V0R11 19970101//",
3158            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3159            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3160            "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 1//",
3161            "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 2//",
3162            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//",
3163            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//",
3164            "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT//",
3165            "-//IETF//DTD HTML 2.0//EN" => 1,            "-//IETF//DTD HTML 2.0//",
3166            "-//IETF//DTD HTML 2.1E//EN" => 1,            "-//IETF//DTD HTML 2.1E//",
3167            "-//IETF//DTD HTML 3.0//EN" => 1,            "-//IETF//DTD HTML 3.0//",
3168            "-//IETF//DTD HTML 3.0//EN//" => 1,            "-//IETF//DTD HTML 3.2 FINAL//",
3169            "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,            "-//IETF//DTD HTML 3.2//",
3170            "-//IETF//DTD HTML 3.2//EN" => 1,            "-//IETF//DTD HTML 3//",
3171            "-//IETF//DTD HTML 3//EN" => 1,            "-//IETF//DTD HTML LEVEL 0//",
3172            "-//IETF//DTD HTML LEVEL 0//EN" => 1,            "-//IETF//DTD HTML LEVEL 1//",
3173            "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,            "-//IETF//DTD HTML LEVEL 2//",
3174            "-//IETF//DTD HTML LEVEL 1//EN" => 1,            "-//IETF//DTD HTML LEVEL 3//",
3175            "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 0//",
3176            "-//IETF//DTD HTML LEVEL 2//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 1//",
3177            "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 2//",
3178            "-//IETF//DTD HTML LEVEL 3//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 3//",
3179            "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,            "-//IETF//DTD HTML STRICT//",
3180            "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,            "-//IETF//DTD HTML//",
3181            "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,            "-//METRIUS//DTD METRIUS PRESENTATIONAL//",
3182            "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//",
3183            "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//",
3184            "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//",
3185            "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//",
3186            "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//",
3187            "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//",
3188            "-//IETF//DTD HTML STRICT//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD HTML//",
3189            "-//IETF//DTD HTML STRICT//EN//2.0" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//",
3190            "-//IETF//DTD HTML STRICT//EN//3.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//",
3191            "-//IETF//DTD HTML//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//",
3192            "-//IETF//DTD HTML//EN//2.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//",
3193            "-//IETF//DTD HTML//EN//3.0" => 1,            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//",
3194            "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//",
3195            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//",
3196            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//",
3197            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//",
3198            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//",
3199            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,            "-//W3C//DTD HTML 3 1995-03-24//",
3200            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,            "-//W3C//DTD HTML 3.2 DRAFT//",
3201            "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,            "-//W3C//DTD HTML 3.2 FINAL//",
3202            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//W3C//DTD HTML 3.2//",
3203            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//W3C//DTD HTML 3.2S DRAFT//",
3204            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 FRAMESET//",
3205            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 TRANSITIONAL//",
3206            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMETNAL 19960712//",
3207            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMENTAL 970421//",
3208            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//W3C//DTD W3 HTML//",
3209            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//W3O//DTD W3 HTML 3.0//",
3210            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML 2.0//",
3211            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML//",
3212            "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,          ]; # $prefix
3213            "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,          my $match;
3214            "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,          for (@$prefix) {
3215            "-//W3C//DTD HTML 3.2//EN" => 1,            if (substr ($prefix, 0, length $_) eq $_) {
3216            "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,              $match = 1;
3217            "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,              last;
3218            "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,            }
3219            "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,          }
3220            "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,          if ($match or
3221            "-//W3C//DTD W3 HTML//EN" => 1,              $pubid eq "-//W3O//DTD W3 HTML STRICT 3.0//EN//" or
3222            "-//W3O//DTD W3 HTML 3.0//EN" => 1,              $pubid eq "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" or
3223            "-//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}) {  
3224            !!!cp ('t5');            !!!cp ('t5');
3225            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3226          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD HTML 4.01 FRAMESET//] or
3227                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD HTML 4.01 TRANSITIONAL//]) {
3228            if (defined $token->{system_identifier}) {            if (defined $token->{system_identifier}) {
3229              !!!cp ('t6');              !!!cp ('t6');
3230              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
# Line 2569  sub _tree_construction_initial ($) { Line 3232  sub _tree_construction_initial ($) {
3232              !!!cp ('t7');              !!!cp ('t7');
3233              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
3234            }            }
3235          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD XHTML 1.0 FRAMESET//] or
3236                   $pubid eq "-//W3C//DTD XHTML 1.0 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD XHTML 1.0 TRANSITIONAL//]) {
3237            !!!cp ('t8');            !!!cp ('t8');
3238            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
3239          } else {          } else {
# Line 2583  sub _tree_construction_initial ($) { Line 3246  sub _tree_construction_initial ($) {
3246          my $sysid = $token->{system_identifier};          my $sysid = $token->{system_identifier};
3247          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
3248          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") {
3249            ## TODO: Check the spec: PUBLIC "(limited quirks)" "(quirks)"            ## NOTE: Ensure that |PUBLIC "(limited quirks)" "(quirks)"| is
3250              ## marked as quirks.
3251            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3252            !!!cp ('t11');            !!!cp ('t11');
3253          } else {          } else {
# Line 2602  sub _tree_construction_initial ($) { Line 3266  sub _tree_construction_initial ($) {
3266                END_OF_FILE_TOKEN, 1,                END_OF_FILE_TOKEN, 1,
3267               }->{$token->{type}}) {               }->{$token->{type}}) {
3268        !!!cp ('t14');        !!!cp ('t14');
3269        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3270        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3271        ## Go to the "before html" insertion mode.        ## Go to the "before html" insertion mode.
3272        ## reprocess        ## reprocess
3273          !!!ack-later;
3274        return;        return;
3275      } elsif ($token->{type} == CHARACTER_TOKEN) {      } elsif ($token->{type} == CHARACTER_TOKEN) {
3276        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
# Line 2623  sub _tree_construction_initial ($) { Line 3288  sub _tree_construction_initial ($) {
3288          !!!cp ('t17');          !!!cp ('t17');
3289        }        }
3290    
3291        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3292        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3293        ## Go to the "before html" insertion mode.        ## Go to the "before html" insertion mode.
3294        ## reprocess        ## reprocess
# Line 2652  sub _tree_construction_root_element ($) Line 3317  sub _tree_construction_root_element ($)
3317    B: {    B: {
3318        if ($token->{type} == DOCTYPE_TOKEN) {        if ($token->{type} == DOCTYPE_TOKEN) {
3319          !!!cp ('t19');          !!!cp ('t19');
3320          !!!parse-error (type => 'in html:#DOCTYPE');          !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
3321          ## Ignore the token          ## Ignore the token
3322          ## Stay in the insertion mode.          ## Stay in the insertion mode.
3323          !!!next-token;          !!!next-token;
# Line 2686  sub _tree_construction_root_element ($) Line 3351  sub _tree_construction_root_element ($)
3351        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
3352          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
3353            my $root_element;            my $root_element;
3354            !!!create-element ($root_element, $token->{tag_name}, $token->{attributes});            !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3355            $self->{document}->append_child ($root_element);            $self->{document}->append_child ($root_element);
3356            push @{$self->{open_elements}}, [$root_element, 'html'];            push @{$self->{open_elements}},
3357                  [$root_element, $el_category->{html}];
3358    
3359            if ($token->{attributes}->{manifest}) {            if ($token->{attributes}->{manifest}) {
3360              !!!cp ('t24');              !!!cp ('t24');
3361              $self->{application_cache_selection}              $self->{application_cache_selection}
3362                  ->($token->{attributes}->{manifest}->{value});                  ->($token->{attributes}->{manifest}->{value});
3363              ## ISSUE: No relative reference resolution?              ## ISSUE: Spec is unclear on relative references.
3364                ## According to Hixie (#whatwg 2008-03-19), it should be
3365                ## resolved against the base URI of the document in HTML
3366                ## or xml:base of the element in XHTML.
3367            } else {            } else {
3368              !!!cp ('t25');              !!!cp ('t25');
3369              $self->{application_cache_selection}->(undef);              $self->{application_cache_selection}->(undef);
3370            }            }
3371    
3372              !!!nack ('t25c');
3373    
3374            !!!next-token;            !!!next-token;
3375            return; ## Go to the "before head" insertion mode.            return; ## Go to the "before head" insertion mode.
3376          } else {          } else {
# Line 2716  sub _tree_construction_root_element ($) Line 3387  sub _tree_construction_root_element ($)
3387          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
3388        }        }
3389    
3390      my $root_element; !!!create-element ($root_element, 'html');      my $root_element;
3391        !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3392      $self->{document}->append_child ($root_element);      $self->{document}->append_child ($root_element);
3393      push @{$self->{open_elements}}, [$root_element, 'html'];      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3394    
3395      $self->{application_cache_selection}->(undef);      $self->{application_cache_selection}->(undef);
3396    
3397      ## NOTE: Reprocess the token.      ## NOTE: Reprocess the token.
3398        !!!ack-later;
3399      return; ## Go to the "before head" insertion mode.      return; ## Go to the "before head" insertion mode.
3400    
3401      ## ISSUE: There is an issue in the spec      ## ISSUE: There is an issue in the spec
# Line 2746  sub _reset_insertion_mode ($) { Line 3419  sub _reset_insertion_mode ($) {
3419        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3420          $last = 1;          $last = 1;
3421          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
3422            if ($self->{inner_html_node}->[1] eq 'td' or            !!!cp ('t28');
3423                $self->{inner_html_node}->[1] eq 'th') {            $node = $self->{inner_html_node};
3424              !!!cp ('t27');          } else {
3425              #            die "_reset_insertion_mode: t27";
           } else {  
             !!!cp ('t28');  
             $node = $self->{inner_html_node};  
           }  
3426          }          }
3427        }        }
3428              
3429        ## Step 4..13        ## Step 4..14
3430        my $new_mode = {        my $new_mode;
3431          if ($node->[1] & FOREIGN_EL) {
3432            !!!cp ('t28.1');
3433            ## NOTE: Strictly spaking, the line below only applies to MathML and
3434            ## SVG elements.  Currently the HTML syntax supports only MathML and
3435            ## SVG elements as foreigners.
3436            $new_mode = IN_BODY_IM | IN_FOREIGN_CONTENT_IM;
3437          } elsif ($node->[1] & TABLE_CELL_EL) {
3438            if ($last) {
3439              !!!cp ('t28.2');
3440              #
3441            } else {
3442              !!!cp ('t28.3');
3443              $new_mode = IN_CELL_IM;
3444            }
3445          } else {
3446            !!!cp ('t28.4');
3447            $new_mode = {
3448                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
3449                        ## NOTE: |option| and |optgroup| do not set                        ## NOTE: |option| and |optgroup| do not set
3450                        ## insertion mode to "in select" by themselves.                        ## insertion mode to "in select" by themselves.
                       td => IN_CELL_IM,  
                       th => IN_CELL_IM,  
3451                        tr => IN_ROW_IM,                        tr => IN_ROW_IM,
3452                        tbody => IN_TABLE_BODY_IM,                        tbody => IN_TABLE_BODY_IM,
3453                        thead => IN_TABLE_BODY_IM,                        thead => IN_TABLE_BODY_IM,
# Line 2774  sub _reset_insertion_mode ($) { Line 3458  sub _reset_insertion_mode ($) {
3458                        head => IN_BODY_IM, # not in head!                        head => IN_BODY_IM, # not in head!
3459                        body => IN_BODY_IM,                        body => IN_BODY_IM,
3460                        frameset => IN_FRAMESET_IM,                        frameset => IN_FRAMESET_IM,
3461                       }->{$node->[1]};                       }->{$node->[0]->manakai_local_name};
3462          }
3463        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3464                
3465        ## Step 14        ## Step 15
3466        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3467          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3468            !!!cp ('t29');            !!!cp ('t29');
3469            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
# Line 2792  sub _reset_insertion_mode ($) { Line 3477  sub _reset_insertion_mode ($) {
3477          !!!cp ('t31');          !!!cp ('t31');
3478        }        }
3479                
3480        ## Step 15        ## Step 16
3481        $self->{insertion_mode} = IN_BODY_IM and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
3482                
3483        ## Step 16        ## Step 17
3484        $i--;        $i--;
3485        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3486                
3487        ## Step 17        ## Step 18
3488        redo S3;        redo S3;
3489      } # S3      } # S3
3490    
# Line 2911  sub _tree_construction_main ($) { Line 3596  sub _tree_construction_main ($) {
3596      ## Step 1      ## Step 1
3597      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3598      my $el;      my $el;
3599      !!!create-element ($el, $start_tag_name, $token->{attributes});      !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3600    
3601      ## Step 2      ## Step 2
3602      $insert->($el);      $insert->($el);
# Line 2922  sub _tree_construction_main ($) { Line 3607  sub _tree_construction_main ($) {
3607    
3608      ## Step 4      ## Step 4
3609      my $text = '';      my $text = '';
3610        !!!nack ('t40.1');
3611      !!!next-token;      !!!next-token;
3612      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3613        !!!cp ('t40');        !!!cp ('t40');
# Line 2948  sub _tree_construction_main ($) { Line 3634  sub _tree_construction_main ($) {
3634        ## NOTE: An end-of-file token.        ## NOTE: An end-of-file token.
3635        if ($content_model_flag == CDATA_CONTENT_MODEL) {        if ($content_model_flag == CDATA_CONTENT_MODEL) {
3636          !!!cp ('t43');          !!!cp ('t43');
3637          !!!parse-error (type => 'in CDATA:#'.$token->{type});          !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3638        } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {        } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3639          !!!cp ('t44');          !!!cp ('t44');
3640          !!!parse-error (type => 'in RCDATA:#'.$token->{type});          !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
3641        } else {        } else {
3642          die "$0: $content_model_flag in parse_rcdata";          die "$0: $content_model_flag in parse_rcdata";
3643        }        }
# Line 2961  sub _tree_construction_main ($) { Line 3647  sub _tree_construction_main ($) {
3647    
3648    my $script_start_tag = sub () {    my $script_start_tag = sub () {
3649      my $script_el;      my $script_el;
3650      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
3651      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3652    
3653      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
3654      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
3655            
3656      my $text = '';      my $text = '';
3657        !!!nack ('t45.1');
3658      !!!next-token;      !!!next-token;
3659      while ($token->{type} == CHARACTER_TOKEN) {      while ($token->{type} == CHARACTER_TOKEN) {
3660        !!!cp ('t45');        !!!cp ('t45');
# Line 2987  sub _tree_construction_main ($) { Line 3674  sub _tree_construction_main ($) {
3674        ## Ignore the token        ## Ignore the token
3675      } else {      } else {
3676        !!!cp ('t48');        !!!cp ('t48');
3677        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3678        ## ISSUE: And ignore?        ## ISSUE: And ignore?
3679        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3680      }      }
# Line 3010  sub _tree_construction_main ($) { Line 3697  sub _tree_construction_main ($) {
3697      !!!next-token;      !!!next-token;
3698    }; # $script_start_tag    }; # $script_start_tag
3699    
3700      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
3701      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
3702      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
3703    
3704    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
3705      my $tag_name = shift;      my $end_tag_token = shift;
3706        my $tag_name = $end_tag_token->{tag_name};
3707    
3708        ## NOTE: The adoption agency algorithm (AAA).
3709    
3710      FET: {      FET: {
3711        ## Step 1        ## Step 1
3712        my $formatting_element;        my $formatting_element;
3713        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3714        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3715          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3716              !!!cp ('t52');
3717              last AFE;
3718            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
3719                         eq $tag_name) {
3720            !!!cp ('t51');            !!!cp ('t51');
3721            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3722            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3723            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           !!!cp ('t52');  
           last AFE;  
3724          }          }
3725        } # AFE        } # AFE
3726        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3727          !!!cp ('t53');          !!!cp ('t53');
3728          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!parse-error (type => 'unmatched end tag:'.$tag_name, token => $end_tag_token);
3729          ## Ignore the token          ## Ignore the token
3730          !!!next-token;          !!!next-token;
3731          return;          return;
# Line 3047  sub _tree_construction_main ($) { Line 3742  sub _tree_construction_main ($) {
3742              last INSCOPE;              last INSCOPE;
3743            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3744              !!!cp ('t55');              !!!cp ('t55');
3745              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3746                                token => $end_tag_token);
3747              ## Ignore the token              ## Ignore the token
3748              !!!next-token;              !!!next-token;
3749              return;              return;
3750            }            }
3751          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
                   table => 1, caption => 1, td => 1, th => 1,  
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
3752            !!!cp ('t56');            !!!cp ('t56');
3753            $in_scope = 0;            $in_scope = 0;
3754          }          }
3755        } # INSCOPE        } # INSCOPE
3756        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3757          !!!cp ('t57');          !!!cp ('t57');
3758          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3759                            token => $end_tag_token);
3760          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3761          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
3762          return;          return;
3763        }        }
3764        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3765          !!!cp ('t58');          !!!cp ('t58');
3766          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!parse-error (type => 'not closed',
3767                            value => $self->{open_elements}->[-1]->[0]
3768                                ->manakai_local_name,
3769                            token => $end_tag_token);
3770        }        }
3771                
3772        ## Step 2        ## Step 2
# Line 3077  sub _tree_construction_main ($) { Line 3774  sub _tree_construction_main ($) {
3774        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
3775        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3776          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3777          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
3778              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3779              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
3780               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
3781            !!!cp ('t59');            !!!cp ('t59');
3782            $furthest_block = $node;            $furthest_block = $node;
3783            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
# Line 3166  sub _tree_construction_main ($) { Line 3863  sub _tree_construction_main ($) {
3863        } # S7          } # S7  
3864                
3865        ## Step 8        ## Step 8
3866        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
3867            my $foster_parent_element;
3868            my $next_sibling;
3869            OE: for (reverse 0..$#{$self->{open_elements}}) {
3870              if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3871                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3872                                 if (defined $parent and $parent->node_type == 1) {
3873                                   !!!cp ('t65.1');
3874                                   $foster_parent_element = $parent;
3875                                   $next_sibling = $self->{open_elements}->[$_]->[0];
3876                                 } else {
3877                                   !!!cp ('t65.2');
3878                                   $foster_parent_element
3879                                     = $self->{open_elements}->[$_ - 1]->[0];
3880                                 }
3881                                 last OE;
3882                               }
3883                             } # OE
3884                             $foster_parent_element = $self->{open_elements}->[0]->[0]
3885                               unless defined $foster_parent_element;
3886            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
3887            $open_tables->[-1]->[1] = 1; # tainted
3888          } else {
3889            !!!cp ('t65.3');
3890            $common_ancestor_node->[0]->append_child ($last_node->[0]);
3891          }
3892                
3893        ## Step 9        ## Step 9
3894        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 3212  sub _tree_construction_main ($) { Line 3934  sub _tree_construction_main ($) {
3934      } # FET      } # FET
3935    }; # $formatting_end_tag    }; # $formatting_end_tag
3936    
   ## NOTE: $open_tables->[-1]->[0] is the "current table".  
   ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.  
   my $open_tables = [[$self->{open_elements}->[0]->[0]]];  
   
3937    $insert = my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
3938      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
3939    }; # $insert_to_current    }; # $insert_to_current
3940    
3941    my $insert_to_foster = sub {    my $insert_to_foster = sub {
3942      my $child = shift;      my $child = shift;
3943      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]}) {  
3944        # MUST        # MUST
3945        my $foster_parent_element;        my $foster_parent_element;
3946        my $next_sibling;        my $next_sibling;
3947                           OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3948                             if ($self->{open_elements}->[$_]->[1] eq 'table') {          if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3949                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3950                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3951                                 !!!cp ('t70');                                 !!!cp ('t70');
# Line 3254  sub _tree_construction_main ($) { Line 3970  sub _tree_construction_main ($) {
3970      }      }
3971    }; # $insert_to_foster    }; # $insert_to_foster
3972    
3973    B: {    B: while (1) {
3974      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
3975        !!!cp ('t73');        !!!cp ('t73');
3976        !!!parse-error (type => 'DOCTYPE in the middle');        !!!parse-error (type => 'DOCTYPE in the middle', token => $token);
3977        ## Ignore the token        ## Ignore the token
3978        ## Stay in the phase        ## Stay in the phase
3979        !!!next-token;        !!!next-token;
3980        redo B;        next B;
     } elsif ($token->{type} == END_OF_FILE_TOKEN) {  
       if ($self->{insertion_mode} & AFTER_HTML_IMS) {  
         !!!cp ('t74');  
         #  
       } else {  
         ## Generate implied end tags  
         while ({  
                 dd => 1, dt => 1, li => 1, p => 1,  
                }->{$self->{open_elements}->[-1]->[1]}) {  
           !!!cp ('t75');  
           pop @{$self->{open_elements}};  
         }  
           
         if (@{$self->{open_elements}} > 2 or  
             (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {  
           !!!cp ('t76');  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         } elsif (defined $self->{inner_html_node} and  
                  @{$self->{open_elements}} > 1 and  
                  $self->{open_elements}->[1]->[1] ne 'body') {  
 ## ISSUE: This case is never reached.  
           !!!cp ('t77');  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         } else {  
           !!!cp ('t78');  
         }  
   
         ## ISSUE: There is an issue in the spec.  
       }  
   
       ## Stop parsing  
       last B;  
3981      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
3982               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
3983        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
3984          !!!cp ('t79');          !!!cp ('t79');
3985          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html', token => $token);
3986          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
3987        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
3988          !!!cp ('t80');          !!!cp ('t80');
3989          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html', token => $token);
3990          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
3991        } else {        } else {
3992          !!!cp ('t81');          !!!cp ('t81');
3993        }        }
3994    
3995        !!!cp ('t82');        !!!cp ('t82');
3996        !!!parse-error (type => 'not first start tag');        !!!parse-error (type => 'not first start tag', token => $token);
3997        my $top_el = $self->{open_elements}->[0]->[0];        my $top_el = $self->{open_elements}->[0]->[0];
3998        for my $attr_name (keys %{$token->{attributes}}) {        for my $attr_name (keys %{$token->{attributes}}) {
3999          unless ($top_el->has_attribute_ns (undef, $attr_name)) {          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
# Line 3319  sub _tree_construction_main ($) { Line 4003  sub _tree_construction_main ($) {
4003               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
4004          }          }
4005        }        }
4006          !!!nack ('t84.1');
4007        !!!next-token;        !!!next-token;
4008        redo B;        next B;
4009      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
4010        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
4011        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
# Line 3334  sub _tree_construction_main ($) { Line 4019  sub _tree_construction_main ($) {
4019          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
4020        }        }
4021        !!!next-token;        !!!next-token;
4022        redo B;        next B;
4023      } elsif ($self->{insertion_mode} & HEAD_IMS) {      } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
4024          if ($token->{type} == CHARACTER_TOKEN) {
4025            !!!cp ('t87.1');
4026            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4027            !!!next-token;
4028            next B;
4029          } elsif ($token->{type} == START_TAG_TOKEN) {
4030            if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
4031                 $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
4032                not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
4033                ($token->{tag_name} eq 'svg' and
4034                 $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
4035              ## NOTE: "using the rules for secondary insertion mode"then"continue"
4036              !!!cp ('t87.2');
4037              #
4038            } elsif ({
4039                      b => 1, big => 1, blockquote => 1, body => 1, br => 1,
4040                      center => 1, code => 1, dd => 1, div => 1, dl => 1, dt => 1,
4041                      em => 1, embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1,
4042                      h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, i => 1,
4043                      img => 1, li => 1, listing => 1, menu => 1, meta => 1,
4044                      nobr => 1, ol => 1, p => 1, pre => 1, ruby => 1, s => 1,
4045                      small => 1, span => 1, strong => 1, strike => 1, sub => 1,
4046                      sup => 1, table => 1, tt => 1, u => 1, ul => 1, var => 1,
4047                     }->{$token->{tag_name}}) {
4048              !!!cp ('t87.2');
4049              !!!parse-error (type => 'not closed',
4050                              value => $self->{open_elements}->[-1]->[0]
4051                                  ->manakai_local_name,
4052                              token => $token);
4053    
4054              pop @{$self->{open_elements}}
4055                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4056    
4057              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4058              ## Reprocess.
4059              next B;
4060            } else {
4061              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
4062              my $tag_name = $token->{tag_name};
4063              if ($nsuri eq $SVG_NS) {
4064                $tag_name = {
4065                   altglyph => 'altGlyph',
4066                   altglyphdef => 'altGlyphDef',
4067                   altglyphitem => 'altGlyphItem',
4068                   animatecolor => 'animateColor',
4069                   animatemotion => 'animateMotion',
4070                   animatetransform => 'animateTransform',
4071                   clippath => 'clipPath',
4072                   feblend => 'feBlend',
4073                   fecolormatrix => 'feColorMatrix',
4074                   fecomponenttransfer => 'feComponentTransfer',
4075                   fecomposite => 'feComposite',
4076                   feconvolvematrix => 'feConvolveMatrix',
4077                   fediffuselighting => 'feDiffuseLighting',
4078                   fedisplacementmap => 'feDisplacementMap',
4079                   fedistantlight => 'feDistantLight',
4080                   feflood => 'feFlood',
4081                   fefunca => 'feFuncA',
4082                   fefuncb => 'feFuncB',
4083                   fefuncg => 'feFuncG',
4084                   fefuncr => 'feFuncR',
4085                   fegaussianblur => 'feGaussianBlur',
4086                   feimage => 'feImage',
4087                   femerge => 'feMerge',
4088                   femergenode => 'feMergeNode',
4089                   femorphology => 'feMorphology',
4090                   feoffset => 'feOffset',
4091                   fepointlight => 'fePointLight',
4092                   fespecularlighting => 'feSpecularLighting',
4093                   fespotlight => 'feSpotLight',
4094                   fetile => 'feTile',
4095                   feturbulence => 'feTurbulence',
4096                   foreignobject => 'foreignObject',
4097                   glyphref => 'glyphRef',
4098                   lineargradient => 'linearGradient',
4099                   radialgradient => 'radialGradient',
4100                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4101                   textpath => 'textPath',  
4102                }->{$tag_name} || $tag_name;
4103              }
4104    
4105              ## "adjust SVG attributes" (SVG only) - done in insert-element-f
4106    
4107              ## "adjust foreign attributes" - done in insert-element-f
4108    
4109              !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
4110    
4111              if ($self->{self_closing}) {
4112                pop @{$self->{open_elements}};
4113                !!!ack ('t87.3');
4114              } else {
4115                !!!cp ('t87.4');
4116              }
4117    
4118              !!!next-token;
4119              next B;
4120            }
4121          } elsif ($token->{type} == END_TAG_TOKEN) {
4122            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4123            !!!cp ('t87.5');
4124            #
4125          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4126            !!!cp ('t87.6');
4127            !!!parse-error (type => 'not closed',
4128                            value => $self->{open_elements}->[-1]->[0]
4129                                ->manakai_local_name,
4130                            token => $token);
4131    
4132            pop @{$self->{open_elements}}
4133                while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4134    
4135            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4136            ## Reprocess.
4137            next B;
4138          } else {
4139            die "$0: $token->{type}: Unknown token type";        
4140          }
4141        }
4142    
4143        if ($self->{insertion_mode} & HEAD_IMS) {
4144        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4145          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4146            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
# Line 3345  sub _tree_construction_main ($) { Line 4150  sub _tree_construction_main ($) {
4150              !!!cp ('t88.1');              !!!cp ('t88.1');
4151              ## Ignore the token.              ## Ignore the token.
4152              !!!next-token;              !!!next-token;
4153              redo B;              next B;
4154            }            }
4155            unless (length $token->{data}) {            unless (length $token->{data}) {
4156              !!!cp ('t88');              !!!cp ('t88');
4157              !!!next-token;              !!!next-token;
4158              redo B;              next B;
4159            }            }
4160          }          }
4161    
4162          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4163            !!!cp ('t89');            !!!cp ('t89');
4164            ## As if <head>            ## As if <head>
4165            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4166            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4167            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            push @{$self->{open_elements}},
4168                  [$self->{head_element}, $el_category->{head}];
4169    
4170            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4171            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
# Line 3369  sub _tree_construction_main ($) { Line 4175  sub _tree_construction_main ($) {
4175            !!!cp ('t90');            !!!cp ('t90');
4176            ## As if </noscript>            ## As if </noscript>
4177            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4178            !!!parse-error (type => 'in noscript:#character');            !!!parse-error (type => 'in noscript:#character', token => $token);
4179                        
4180            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4181            ## As if </head>            ## As if </head>
# Line 3385  sub _tree_construction_main ($) { Line 4191  sub _tree_construction_main ($) {
4191            !!!cp ('t92');            !!!cp ('t92');
4192          }          }
4193    
4194              ## "after head" insertion mode          ## "after head" insertion mode
4195              ## As if <body>          ## As if <body>
4196              !!!insert-element ('body');          !!!insert-element ('body',, $token);
4197              $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4198              ## reprocess          ## reprocess
4199              redo B;          next B;
4200            } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4201              if ($token->{tag_name} eq 'head') {          if ($token->{tag_name} eq 'head') {
4202                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4203                  !!!cp ('t93');              !!!cp ('t93');
4204                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4205                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              $self->{open_elements}->[-1]->[0]->append_child
4206                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];                  ($self->{head_element});
4207                  $self->{insertion_mode} = IN_HEAD_IM;              push @{$self->{open_elements}},
4208                  !!!next-token;                  [$self->{head_element}, $el_category->{head}];
4209                  redo B;              $self->{insertion_mode} = IN_HEAD_IM;
4210                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {              !!!nack ('t93.1');
4211                  !!!cp ('t94');              !!!next-token;
4212                  #              next B;
4213                } else {            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4214                  !!!cp ('t95');              !!!cp ('t93.2');
4215                  !!!parse-error (type => 'in head:head'); # or in head noscript              !!!parse-error (type => 'after head:head', token => $token); ## TODO: error type
4216                  ## Ignore the token              ## Ignore the token
4217                  !!!next-token;              !!!nack ('t93.3');
4218                  redo B;              !!!next-token;
4219                }              next B;
4220              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {            } else {
4221                !!!cp ('t96');              !!!cp ('t95');
4222                ## As if <head>              !!!parse-error (type => 'in head:head', token => $token); # or in head noscript
4223                !!!create-element ($self->{head_element}, 'head');              ## Ignore the token
4224                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              !!!nack ('t95.1');
4225                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];              !!!next-token;
4226                next B;
4227              }
4228            } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4229              !!!cp ('t96');
4230              ## As if <head>
4231              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4232              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4233              push @{$self->{open_elements}},
4234                  [$self->{head_element}, $el_category->{head}];
4235    
4236                $self->{insertion_mode} = IN_HEAD_IM;            $self->{insertion_mode} = IN_HEAD_IM;
4237                ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4238              } else {          } else {
4239                !!!cp ('t97');            !!!cp ('t97');
4240              }          }
4241    
4242              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
4243                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4244                  !!!cp ('t98');                  !!!cp ('t98');
4245                  ## As if </noscript>                  ## As if </noscript>
4246                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4247                  !!!parse-error (type => 'in noscript:base');                  !!!parse-error (type => 'in noscript:base', token => $token);
4248                                
4249                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4250                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
# Line 3440  sub _tree_construction_main ($) { Line 4255  sub _tree_construction_main ($) {
4255                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4256                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4257                  !!!cp ('t100');                  !!!cp ('t100');
4258                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4259                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4260                        [$self->{head_element}, $el_category->{head}];
4261                } else {                } else {
4262                  !!!cp ('t101');                  !!!cp ('t101');
4263                }                }
4264                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4265                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4266                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4267                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4268                  !!!nack ('t101.1');
4269                !!!next-token;                !!!next-token;
4270                redo B;                next B;
4271              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
4272                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4273                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4274                  !!!cp ('t102');                  !!!cp ('t102');
4275                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4276                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4277                        [$self->{head_element}, $el_category->{head}];
4278                } else {                } else {
4279                  !!!cp ('t103');                  !!!cp ('t103');
4280                }                }
4281                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4282                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4283                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4284                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4285                  !!!ack ('t103.1');
4286                !!!next-token;                !!!next-token;
4287                redo B;                next B;
4288              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
4289                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4290                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4291                  !!!cp ('t104');                  !!!cp ('t104');
4292                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4293                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4294                        [$self->{head_element}, $el_category->{head}];
4295                } else {                } else {
4296                  !!!cp ('t105');                  !!!cp ('t105');
4297                }                }
4298                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4299                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.
4300    
4301                unless ($self->{confident}) {                unless ($self->{confident}) {
4302                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) {
4303                    !!!cp ('t106');                    !!!cp ('t106');
4304                      ## NOTE: Whether the encoding is supported or not is handled
4305                      ## in the {change_encoding} callback.
4306                    $self->{change_encoding}                    $self->{change_encoding}
4307                        ->($self, $token->{attributes}->{charset}->{value});                        ->($self, $token->{attributes}->{charset}->{value},
4308                             $token);
4309                                        
4310                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4311                        ->set_user_data (manakai_has_reference =>                        ->set_user_data (manakai_has_reference =>
4312                                             $token->{attributes}->{charset}                                             $token->{attributes}->{charset}
4313                                                 ->{has_reference});                                                 ->{has_reference});
4314                  } elsif ($token->{attributes}->{content}) {                  } elsif ($token->{attributes}->{content}) {
                   ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
4315                    if ($token->{attributes}->{content}->{value}                    if ($token->{attributes}->{content}->{value}
4316                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                        =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4317                            [\x09-\x0D\x20]*=                            [\x09-\x0D\x20]*=
4318                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4319                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
4320                      !!!cp ('t107');                      !!!cp ('t107');
4321                        ## NOTE: Whether the encoding is supported or not is handled
4322                        ## in the {change_encoding} callback.
4323                      $self->{change_encoding}                      $self->{change_encoding}
4324                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4325                               $token);
4326                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4327                          ->set_user_data (manakai_has_reference =>                          ->set_user_data (manakai_has_reference =>
4328                                               $token->{attributes}->{content}                                               $token->{attributes}->{content}
# Line 3525  sub _tree_construction_main ($) { Line 4350  sub _tree_construction_main ($) {
4350    
4351                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4352                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4353                  !!!ack ('t110.1');
4354                !!!next-token;                !!!next-token;
4355                redo B;                next B;
4356              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
4357                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4358                  !!!cp ('t111');                  !!!cp ('t111');
4359                  ## As if </noscript>                  ## As if </noscript>
4360                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4361                  !!!parse-error (type => 'in noscript:title');                  !!!parse-error (type => 'in noscript:title', token => $token);
4362                                
4363                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4364                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4365                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4366                  !!!cp ('t112');                  !!!cp ('t112');
4367                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4368                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4369                        [$self->{head_element}, $el_category->{head}];
4370                } else {                } else {
4371                  !!!cp ('t113');                  !!!cp ('t113');
4372                }                }
# Line 3550  sub _tree_construction_main ($) { Line 4377  sub _tree_construction_main ($) {
4377                $parse_rcdata->(RCDATA_CONTENT_MODEL);                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4378                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4379                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4380                redo B;                next B;
4381              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style' or
4382                         $token->{tag_name} eq 'noframes') {
4383                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4384                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
4385                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4386                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4387                  !!!cp ('t114');                  !!!cp ('t114');
4388                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4389                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4390                        [$self->{head_element}, $el_category->{head}];
4391                } else {                } else {
4392                  !!!cp ('t115');                  !!!cp ('t115');
4393                }                }
4394                $parse_rcdata->(CDATA_CONTENT_MODEL);                $parse_rcdata->(CDATA_CONTENT_MODEL);
4395                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4396                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4397                redo B;                next B;
4398              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4399                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
4400                  !!!cp ('t116');                  !!!cp ('t116');
4401                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
4402                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4403                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4404                    !!!nack ('t116.1');
4405                  !!!next-token;                  !!!next-token;
4406                  redo B;                  next B;
4407                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4408                  !!!cp ('t117');                  !!!cp ('t117');
4409                  !!!parse-error (type => 'in noscript:noscript');                  !!!parse-error (type => 'in noscript:noscript', token => $token);
4410                  ## Ignore the token                  ## Ignore the token
4411                    !!!nack ('t117.1');
4412                  !!!next-token;                  !!!next-token;
4413                  redo B;                  next B;
4414                } else {                } else {
4415                  !!!cp ('t118');                  !!!cp ('t118');
4416                  #                  #
# Line 3589  sub _tree_construction_main ($) { Line 4420  sub _tree_construction_main ($) {
4420                  !!!cp ('t119');                  !!!cp ('t119');
4421                  ## As if </noscript>                  ## As if </noscript>
4422                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4423                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:script', token => $token);
4424                                
4425                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4426                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4427                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4428                  !!!cp ('t120');                  !!!cp ('t120');
4429                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4430                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4431                        [$self->{head_element}, $el_category->{head}];
4432                } else {                } else {
4433                  !!!cp ('t121');                  !!!cp ('t121');
4434                }                }
# Line 3605  sub _tree_construction_main ($) { Line 4437  sub _tree_construction_main ($) {
4437                $script_start_tag->();                $script_start_tag->();
4438                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4439                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4440                redo B;                next B;
4441              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
4442                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
4443                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4444                  !!!cp ('t122');                  !!!cp ('t122');
4445                  ## As if </noscript>                  ## As if </noscript>
4446                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4447                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});                  !!!parse-error (type => 'in noscript:'.$token->{tag_name}, token => $token);
4448                                    
4449                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4450                  ## As if </head>                  ## As if </head>
# Line 3629  sub _tree_construction_main ($) { Line 4461  sub _tree_construction_main ($) {
4461                }                }
4462    
4463                ## "after head" insertion mode                ## "after head" insertion mode
4464                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4465                if ($token->{tag_name} eq 'body') {                if ($token->{tag_name} eq 'body') {
4466                  !!!cp ('t126');                  !!!cp ('t126');
4467                  $self->{insertion_mode} = IN_BODY_IM;                  $self->{insertion_mode} = IN_BODY_IM;
# Line 3639  sub _tree_construction_main ($) { Line 4471  sub _tree_construction_main ($) {
4471                } else {                } else {
4472                  die "$0: tag name: $self->{tag_name}";                  die "$0: tag name: $self->{tag_name}";
4473                }                }
4474                  !!!nack ('t127.1');
4475                !!!next-token;                !!!next-token;
4476                redo B;                next B;
4477              } else {              } else {
4478                !!!cp ('t128');                !!!cp ('t128');
4479                #                #
# Line 3650  sub _tree_construction_main ($) { Line 4483  sub _tree_construction_main ($) {
4483                !!!cp ('t129');                !!!cp ('t129');
4484                ## As if </noscript>                ## As if </noscript>
4485                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4486                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
4487                                
4488                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4489                ## As if </head>                ## As if </head>
# Line 3669  sub _tree_construction_main ($) { Line 4502  sub _tree_construction_main ($) {
4502    
4503              ## "after head" insertion mode              ## "after head" insertion mode
4504              ## As if <body>              ## As if <body>
4505              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4506              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4507              ## reprocess              ## reprocess
4508              redo B;              !!!ack-later;
4509                next B;
4510            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
4511              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
4512                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4513                  !!!cp ('t132');                  !!!cp ('t132');
4514                  ## As if <head>                  ## As if <head>
4515                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4516                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4517                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4518                        [$self->{head_element}, $el_category->{head}];
4519    
4520                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4521                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4522                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4523                  !!!next-token;                  !!!next-token;
4524                  redo B;                  next B;
4525                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4526                  !!!cp ('t133');                  !!!cp ('t133');
4527                  ## As if </noscript>                  ## As if </noscript>
4528                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4529                  !!!parse-error (type => 'in noscript:/head');                  !!!parse-error (type => 'in noscript:/head', token => $token);
4530                                    
4531                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4532                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4533                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4534                  !!!next-token;                  !!!next-token;
4535                  redo B;                  next B;
4536                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4537                  !!!cp ('t134');                  !!!cp ('t134');
4538                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4539                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4540                  !!!next-token;                  !!!next-token;
4541                  redo B;                  next B;
4542                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4543                    !!!cp ('t134.1');
4544                    !!!parse-error (type => 'unmatched end tag:head', token => $token);
4545                    ## Ignore the token
4546                    !!!next-token;
4547                    next B;
4548                } else {                } else {
4549                  !!!cp ('t135');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
                 #  
4550                }                }
4551              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4552                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
# Line 3714  sub _tree_construction_main ($) { Line 4554  sub _tree_construction_main ($) {
4554                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4555                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4556                  !!!next-token;                  !!!next-token;
4557                  redo B;                  next B;
4558                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
4559                           $self->{insertion_mode} == AFTER_HEAD_IM) {
4560                  !!!cp ('t137');                  !!!cp ('t137');
4561                  !!!parse-error (type => 'unmatched end tag:noscript');                  !!!parse-error (type => 'unmatched end tag:noscript', token => $token);
4562                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
4563                  !!!next-token;                  !!!next-token;
4564                  redo B;                  next B;
4565                } else {                } else {
4566                  !!!cp ('t138');                  !!!cp ('t138');
4567                  #                  #
# Line 3728  sub _tree_construction_main ($) { Line 4569  sub _tree_construction_main ($) {
4569              } elsif ({              } elsif ({
4570                        body => 1, html => 1,                        body => 1, html => 1,
4571                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4572                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM or
4573                  !!!cp ('t139');                    $self->{insertion_mode} == IN_HEAD_IM or
4574                  ## 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) {  
4575                  !!!cp ('t140');                  !!!cp ('t140');
4576                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4577                  ## Ignore the token                  ## Ignore the token
4578                  !!!next-token;                  !!!next-token;
4579                  redo B;                  next B;
4580                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4581                    !!!cp ('t140.1');
4582                    !!!parse-error (type => 'unmatched end tag:' . $token->{tag_name}, token => $token);
4583                    ## Ignore the token
4584                    !!!next-token;
4585                    next B;
4586                } else {                } else {
4587                  !!!cp ('t141');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
4588                }                }
4589                              } elsif ($token->{tag_name} eq 'p') {
4590                #                !!!cp ('t142');
4591              } elsif ({                !!!parse-error (type => 'unmatched end tag:p', token => $token);
4592                        p => 1, br => 1,                ## Ignore the token
4593                       }->{$token->{tag_name}}) {                !!!next-token;
4594                  next B;
4595                } elsif ($token->{tag_name} eq 'br') {
4596                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4597                  !!!cp ('t142');                  !!!cp ('t142.2');
4598                  ## As if <head>                  ## (before head) as if <head>, (in head) as if </head>
4599                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4600                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4601                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  $self->{insertion_mode} = AFTER_HEAD_IM;
4602      
4603                    ## Reprocess in the "after head" insertion mode...
4604                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4605                    !!!cp ('t143.2');
4606                    ## As if </head>
4607                    pop @{$self->{open_elements}};
4608                    $self->{insertion_mode} = AFTER_HEAD_IM;
4609      
4610                    ## Reprocess in the "after head" insertion mode...
4611                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4612                    !!!cp ('t143.3');
4613                    ## ISSUE: Two parse errors for <head><noscript></br>
4614                    !!!parse-error (type => 'unmatched end tag:br', token => $token);
4615                    ## As if </noscript>
4616                    pop @{$self->{open_elements}};
4617                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4618    
4619                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4620                } else {                  ## As if </head>
4621                  !!!cp ('t143');                  pop @{$self->{open_elements}};
4622                }                  $self->{insertion_mode} = AFTER_HEAD_IM;
4623    
4624                #                  ## Reprocess in the "after head" insertion mode...
4625              } else {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4626                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                  !!!cp ('t143.4');
                 !!!cp ('t144');  
4627                  #                  #
4628                } else {                } else {
4629                  !!!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;  
4630                }                }
4631    
4632                  ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
4633                  !!!parse-error (type => 'unmatched end tag:br', token => $token);
4634                  ## Ignore the token
4635                  !!!next-token;
4636                  next B;
4637                } else {
4638                  !!!cp ('t145');
4639                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4640                  ## Ignore the token
4641                  !!!next-token;
4642                  next B;
4643              }              }
4644    
4645              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4646                !!!cp ('t146');                !!!cp ('t146');
4647                ## As if </noscript>                ## As if </noscript>
4648                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4649                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
4650                                
4651                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4652                ## As if </head>                ## As if </head>
# Line 3798  sub _tree_construction_main ($) { Line 4662  sub _tree_construction_main ($) {
4662              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4663  ## ISSUE: This case cannot be reached?  ## ISSUE: This case cannot be reached?
4664                !!!cp ('t148');                !!!cp ('t148');
4665                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4666                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
4667                !!!next-token;                !!!next-token;
4668                redo B;                next B;
4669              } else {              } else {
4670                !!!cp ('t149');                !!!cp ('t149');
4671              }              }
4672    
4673              ## "after head" insertion mode              ## "after head" insertion mode
4674              ## As if <body>              ## As if <body>
4675              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4676              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4677              ## reprocess              ## reprocess
4678              redo B;              next B;
4679            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4680              die "$0: $token->{type}: Unknown token type";          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4681            }            !!!cp ('t149.1');
4682    
4683              ## NOTE: As if <head>
4684              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4685              $self->{open_elements}->[-1]->[0]->append_child
4686                  ($self->{head_element});
4687              #push @{$self->{open_elements}},
4688              #    [$self->{head_element}, $el_category->{head}];
4689              #$self->{insertion_mode} = IN_HEAD_IM;
4690              ## NOTE: Reprocess.
4691    
4692              ## NOTE: As if </head>
4693              #pop @{$self->{open_elements}};
4694              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4695              ## NOTE: Reprocess.
4696              
4697              #
4698            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4699              !!!cp ('t149.2');
4700    
4701              ## NOTE: As if </head>
4702              pop @{$self->{open_elements}};
4703              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4704              ## NOTE: Reprocess.
4705    
4706              #
4707            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4708              !!!cp ('t149.3');
4709    
4710              !!!parse-error (type => 'in noscript:#eof', token => $token);
4711    
4712              ## As if </noscript>
4713              pop @{$self->{open_elements}};
4714              #$self->{insertion_mode} = IN_HEAD_IM;
4715              ## NOTE: Reprocess.
4716    
4717              ## NOTE: As if </head>
4718              pop @{$self->{open_elements}};
4719              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4720              ## NOTE: Reprocess.
4721    
4722              #
4723            } else {
4724              !!!cp ('t149.4');
4725              #
4726            }
4727    
4728            ## NOTE: As if <body>
4729            !!!insert-element ('body',, $token);
4730            $self->{insertion_mode} = IN_BODY_IM;
4731            ## NOTE: Reprocess.
4732            next B;
4733          } else {
4734            die "$0: $token->{type}: Unknown token type";
4735          }
4736    
4737            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
4738      } elsif ($self->{insertion_mode} & BODY_IMS) {      } elsif ($self->{insertion_mode} & BODY_IMS) {
# Line 3826  sub _tree_construction_main ($) { Line 4744  sub _tree_construction_main ($) {
4744              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4745    
4746              !!!next-token;              !!!next-token;
4747              redo B;              next B;
4748            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
4749              if ({              if ({
4750                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
# Line 3834  sub _tree_construction_main ($) { Line 4752  sub _tree_construction_main ($) {
4752                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4753                if ($self->{insertion_mode} == IN_CELL_IM) {                if ($self->{insertion_mode} == IN_CELL_IM) {
4754                  ## have an element in table scope                  ## have an element in table scope
4755                  my $tn;                  for (reverse 0..$#{$self->{open_elements}}) {
                 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
4756                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4757                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] & TABLE_CELL_EL) {
4758                      !!!cp ('t151');                      !!!cp ('t151');
4759                      $tn = $node->[1];  
4760                      last INSCOPE;                      ## Close the cell
4761                    } elsif ({                      !!!back-token; # <x>
4762                              table => 1, html => 1,                      $token = {type => END_TAG_TOKEN,
4763                             }->{$node->[1]}) {                                tag_name => $node->[0]->manakai_local_name,
4764                                  line => $token->{line},
4765                                  column => $token->{column}};
4766                        next B;
4767                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4768                      !!!cp ('t152');                      !!!cp ('t152');
4769                      last INSCOPE;                      ## ISSUE: This case can never be reached, maybe.
4770                        last;
4771                    }                    }
4772                  } # INSCOPE                  }
4773                    unless (defined $tn) {  
4774                      !!!cp ('t153');                  !!!cp ('t153');
4775  ## TODO: This error type is wrong.                  !!!parse-error (type => 'start tag not allowed',
4776                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      value => $token->{tag_name}, token => $token);
4777                      ## Ignore the token                  ## Ignore the token
4778                      !!!next-token;                  !!!nack ('t153.1');
4779                      redo B;                  !!!next-token;
4780                    }                  next B;
                   
                 !!!cp ('t154');  
                 ## Close the cell  
                 !!!back-token; # <?>  
                 $token = {type => END_TAG_TOKEN, tag_name => $tn};  
                 redo B;  
4781                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4782                  !!!parse-error (type => 'not closed:caption');                  !!!parse-error (type => 'not closed:caption', token => $token);
4783                                    
4784                  ## As if </caption>                  ## NOTE: As if </caption>.
4785                  ## have a table element in table scope                  ## have a table element in table scope
4786                  my $i;                  my $i;
4787                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4788                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4789                    if ($node->[1] eq 'caption') {                      my $node = $self->{open_elements}->[$_];
4790                      !!!cp ('t155');                      if ($node->[1] & CAPTION_EL) {
4791                      $i = $_;                        !!!cp ('t155');
4792                      last INSCOPE;                        $i = $_;
4793                    } elsif ({                        last INSCOPE;
4794                              table => 1, html => 1,                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4795                             }->{$node->[1]}) {                        !!!cp ('t156');
4796                      !!!cp ('t156');                        last;
4797                      last INSCOPE;                      }
4798                    }                    }
4799    
4800                      !!!cp ('t157');
4801                      !!!parse-error (type => 'start tag not allowed',
4802                                      value => $token->{tag_name}, token => $token);
4803                      ## Ignore the token
4804                      !!!nack ('t157.1');
4805                      !!!next-token;
4806                      next B;
4807                  } # 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;  
                   }  
4808                                    
4809                  ## generate implied end tags                  ## generate implied end tags
4810                  while ({                  while ($self->{open_elements}->[-1]->[1]
4811                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4812                    !!!cp ('t158');                    !!!cp ('t158');
4813                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4814                  }                  }
4815    
4816                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4817                    !!!cp ('t159');                    !!!cp ('t159');
4818                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
4819                                      value => $self->{open_elements}->[-1]->[0]
4820                                          ->manakai_local_name,
4821                                      token => $token);
4822                  } else {                  } else {
4823                    !!!cp ('t160');                    !!!cp ('t160');
4824                  }                  }
# Line 3912  sub _tree_construction_main ($) { Line 4830  sub _tree_construction_main ($) {
4830                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4831                                    
4832                  ## reprocess                  ## reprocess
4833                  redo B;                  !!!ack-later;
4834                    next B;
4835                } else {                } else {
4836                  !!!cp ('t161');                  !!!cp ('t161');
4837                  #                  #
# Line 3928  sub _tree_construction_main ($) { Line 4847  sub _tree_construction_main ($) {
4847                  my $i;                  my $i;
4848                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4849                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4850                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4851                      !!!cp ('t163');                      !!!cp ('t163');
4852                      $i = $_;                      $i = $_;
4853                      last INSCOPE;                      last INSCOPE;
4854                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
4855                      !!!cp ('t164');                      !!!cp ('t164');
4856                      last INSCOPE;                      last INSCOPE;
4857                    }                    }
4858                  } # INSCOPE                  } # INSCOPE
4859                    unless (defined $i) {                    unless (defined $i) {
4860                      !!!cp ('t165');                      !!!cp ('t165');
4861                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4862                      ## Ignore the token                      ## Ignore the token
4863                      !!!next-token;                      !!!next-token;
4864                      redo B;                      next B;
4865                    }                    }
4866                                    
4867                  ## generate implied end tags                  ## generate implied end tags
4868                  while ({                  while ($self->{open_elements}->[-1]->[1]
4869                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4870                    !!!cp ('t166');                    !!!cp ('t166');
4871                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4872                  }                  }
4873    
4874                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
4875                            ne $token->{tag_name}) {
4876                    !!!cp ('t167');                    !!!cp ('t167');
4877                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
4878                                      value => $self->{open_elements}->[-1]->[0]
4879                                          ->manakai_local_name,
4880                                      token => $token);
4881                  } else {                  } else {
4882                    !!!cp ('t168');                    !!!cp ('t168');
4883                  }                  }
# Line 3969  sub _tree_construction_main ($) { Line 4889  sub _tree_construction_main ($) {
4889                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
4890                                    
4891                  !!!next-token;                  !!!next-token;
4892                  redo B;                  next B;
4893                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4894                  !!!cp ('t169');                  !!!cp ('t169');
4895                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4896                  ## Ignore the token                  ## Ignore the token
4897                  !!!next-token;                  !!!next-token;
4898                  redo B;                  next B;
4899                } else {                } else {
4900                  !!!cp ('t170');                  !!!cp ('t170');
4901                  #                  #
# Line 3984  sub _tree_construction_main ($) { Line 4904  sub _tree_construction_main ($) {
4904                if ($self->{insertion_mode} == IN_CAPTION_IM) {                if ($self->{insertion_mode} == IN_CAPTION_IM) {
4905                  ## have a table element in table scope                  ## have a table element in table scope
4906                  my $i;                  my $i;
4907                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4908                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4909                    if ($node->[1] eq $token->{tag_name}) {                      my $node = $self->{open_elements}->[$_];
4910                      !!!cp ('t171');                      if ($node->[1] & CAPTION_EL) {
4911                      $i = $_;                        !!!cp ('t171');
4912                      last INSCOPE;                        $i = $_;
4913                    } elsif ({                        last INSCOPE;
4914                              table => 1, html => 1,                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4915                             }->{$node->[1]}) {                        !!!cp ('t172');
4916                      !!!cp ('t172');                        last;
4917                      last INSCOPE;                      }
4918                    }                    }
4919    
4920                      !!!cp ('t173');
4921                      !!!parse-error (type => 'unmatched end tag',
4922                                      value => $token->{tag_name}, token => $token);
4923                      ## Ignore the token
4924                      !!!next-token;
4925                      next B;
4926                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!cp ('t173');  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
4927                                    
4928                  ## generate implied end tags                  ## generate implied end tags
4929                  while ({                  while ($self->{open_elements}->[-1]->[1]
4930                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4931                    !!!cp ('t174');                    !!!cp ('t174');
4932                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4933                  }                  }
4934                                    
4935                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4936                    !!!cp ('t175');                    !!!cp ('t175');
4937                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
4938                                      value => $self->{open_elements}->[-1]->[0]
4939                                          ->manakai_local_name,
4940                                      token => $token);
4941                  } else {                  } else {
4942                    !!!cp ('t176');                    !!!cp ('t176');
4943                  }                  }
# Line 4027  sub _tree_construction_main ($) { Line 4949  sub _tree_construction_main ($) {
4949                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4950                                    
4951                  !!!next-token;                  !!!next-token;
4952                  redo B;                  next B;
4953                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
4954                  !!!cp ('t177');                  !!!cp ('t177');
4955                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4956                  ## Ignore the token                  ## Ignore the token
4957                  !!!next-token;                  !!!next-token;
4958                  redo B;                  next B;
4959                } else {                } else {
4960                  !!!cp ('t178');                  !!!cp ('t178');
4961                  #                  #
# Line 4046  sub _tree_construction_main ($) { Line 4968  sub _tree_construction_main ($) {
4968                ## have an element in table scope                ## have an element in table scope
4969                my $i;                my $i;
4970                my $tn;                my $tn;
4971                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: {
4972                  my $node = $self->{open_elements}->[$_];                  for (reverse 0..$#{$self->{open_elements}}) {
4973                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
4974                    !!!cp ('t179');                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4975                    $i = $_;                      !!!cp ('t179');
4976                    last INSCOPE;                      $i = $_;
4977                  } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {  
4978                    !!!cp ('t180');                      ## Close the cell
4979                    $tn = $node->[1];                      !!!back-token; # </x>
4980                    ## NOTE: There is exactly one |td| or |th| element                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
4981                    ## in scope in the stack of open elements by definition.                                line => $token->{line},
4982                  } elsif ({                                column => $token->{column}};
4983                            table => 1, html => 1,                      next B;
4984                           }->{$node->[1]}) {                    } elsif ($node->[1] & TABLE_CELL_EL) {
4985                    !!!cp ('t181');                      !!!cp ('t180');
4986                    last INSCOPE;                      $tn = $node->[0]->manakai_local_name;
4987                        ## NOTE: There is exactly one |td| or |th| element
4988                        ## in scope in the stack of open elements by definition.
4989                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4990                        ## ISSUE: Can this be reached?
4991                        !!!cp ('t181');
4992                        last;
4993                      }
4994                  }                  }
4995                } # INSCOPE  
               unless (defined $i) {  
4996                  !!!cp ('t182');                  !!!cp ('t182');
4997                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
4998                        value => $token->{tag_name}, token => $token);
4999                  ## Ignore the token                  ## Ignore the token
5000                  !!!next-token;                  !!!next-token;
5001                  redo B;                  next B;
5002                } else {                } # INSCOPE
                 !!!cp ('t183');  
               }  
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => END_TAG_TOKEN, tag_name => $tn};  
               redo B;  
5003              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
5004                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5005                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption', token => $token);
5006    
5007                ## As if </caption>                ## As if </caption>
5008                ## have a table element in table scope                ## have a table element in table scope
5009                my $i;                my $i;
5010                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5011                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5012                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
5013                    !!!cp ('t184');                    !!!cp ('t184');
5014                    $i = $_;                    $i = $_;
5015                    last INSCOPE;                    last INSCOPE;
5016                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5017                    !!!cp ('t185');                    !!!cp ('t185');
5018                    last INSCOPE;                    last INSCOPE;
5019                  }                  }
5020                } # INSCOPE                } # INSCOPE
5021                unless (defined $i) {                unless (defined $i) {
5022                  !!!cp ('t186');                  !!!cp ('t186');
5023                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!parse-error (type => 'unmatched end tag:caption', token => $token);
5024                  ## Ignore the token                  ## Ignore the token
5025                  !!!next-token;                  !!!next-token;
5026                  redo B;                  next B;
5027                }                }
5028                                
5029                ## generate implied end tags                ## generate implied end tags
5030                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
5031                  !!!cp ('t187');                  !!!cp ('t187');
5032                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5033                }                }
5034    
5035                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5036                  !!!cp ('t188');                  !!!cp ('t188');
5037                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed',
5038                                    value => $self->{open_elements}->[-1]->[0]
5039                                        ->manakai_local_name,
5040                                    token => $token);
5041                } else {                } else {
5042                  !!!cp ('t189');                  !!!cp ('t189');
5043                }                }
# Line 4128  sub _tree_construction_main ($) { Line 5049  sub _tree_construction_main ($) {
5049                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5050    
5051                ## reprocess                ## reprocess
5052                redo B;                next B;
5053              } elsif ({              } elsif ({
5054                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
5055                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5056                if ($self->{insertion_mode} & BODY_TABLE_IMS) {                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
5057                  !!!cp ('t190');                  !!!cp ('t190');
5058                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5059                  ## Ignore the token                  ## Ignore the token
5060                  !!!next-token;                  !!!next-token;
5061                  redo B;                  next B;
5062                } else {                } else {
5063                  !!!cp ('t191');                  !!!cp ('t191');
5064                  #                  #
# Line 4148  sub _tree_construction_main ($) { Line 5069  sub _tree_construction_main ($) {
5069                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
5070                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5071                !!!cp ('t192');                !!!cp ('t192');
5072                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5073                ## Ignore the token                ## Ignore the token
5074                !!!next-token;                !!!next-token;
5075                redo B;                next B;
5076              } else {              } else {
5077                !!!cp ('t193');                !!!cp ('t193');
5078                #                #
5079              }              }
5080          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5081            for my $entry (@{$self->{open_elements}}) {
5082              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
5083                !!!cp ('t75');
5084                !!!parse-error (type => 'in body:#eof', token => $token);
5085                last;
5086              }
5087            }
5088    
5089            ## Stop parsing.
5090            last B;
5091        } else {        } else {
5092          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5093        }        }
# Line 4171  sub _tree_construction_main ($) { Line 5103  sub _tree_construction_main ($) {
5103            unless (length $token->{data}) {            unless (length $token->{data}) {
5104              !!!cp ('t194');              !!!cp ('t194');
5105              !!!next-token;              !!!next-token;
5106              redo B;              next B;
5107            } else {            } else {
5108              !!!cp ('t195');              !!!cp ('t195');
5109            }            }
5110          }          }
5111    
5112              !!!parse-error (type => 'in table:#character');              !!!parse-error (type => 'in table:#character', token => $token);
5113    
5114              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
5115              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
# Line 4185  sub _tree_construction_main ($) { Line 5117  sub _tree_construction_main ($) {
5117              ## result in a new Text node.              ## result in a new Text node.
5118              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
5119                            
5120              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]}) {  
5121                # MUST                # MUST
5122                my $foster_parent_element;                my $foster_parent_element;
5123                my $next_sibling;                my $next_sibling;
5124                my $prev_sibling;                my $prev_sibling;
5125                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
5126                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
5127                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
5128                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
5129                      !!!cp ('t196');                      !!!cp ('t196');
# Line 4229  sub _tree_construction_main ($) { Line 5158  sub _tree_construction_main ($) {
5158          }          }
5159                            
5160          !!!next-token;          !!!next-token;
5161          redo B;          next B;
5162        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5163              if ({              if ({
5164                   tr => ($self->{insertion_mode} != IN_ROW_IM),                   tr => ($self->{insertion_mode} != IN_ROW_IM),
# Line 4237  sub _tree_construction_main ($) { Line 5166  sub _tree_construction_main ($) {
5166                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
5167                if ($self->{insertion_mode} == IN_TABLE_IM) {                if ($self->{insertion_mode} == IN_TABLE_IM) {
5168                  ## Clear back to table context                  ## Clear back to table context
5169                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5170                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5171                    !!!cp ('t201');                    !!!cp ('t201');
5172                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5173                  }                  }
5174                                    
5175                  !!!insert-element ('tbody');                  !!!insert-element ('tbody',, $token);
5176                  $self->{insertion_mode} = IN_TABLE_BODY_IM;                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5177                  ## reprocess in the "in table body" insertion mode...                  ## reprocess in the "in table body" insertion mode...
5178                }                }
# Line 4251  sub _tree_construction_main ($) { Line 5180  sub _tree_construction_main ($) {
5180                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5181                  unless ($token->{tag_name} eq 'tr') {                  unless ($token->{tag_name} eq 'tr') {
5182                    !!!cp ('t202');                    !!!cp ('t202');
5183                    !!!parse-error (type => 'missing start tag:tr');                    !!!parse-error (type => 'missing start tag:tr', token => $token);
5184                  }                  }
5185                                    
5186                  ## Clear back to table body context                  ## Clear back to table body context
5187                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5188                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5189                    !!!cp ('t203');                    !!!cp ('t203');
5190                    ## ISSUE: Can this case be reached?                    ## ISSUE: Can this case be reached?
5191                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4266  sub _tree_construction_main ($) { Line 5194  sub _tree_construction_main ($) {
5194                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
5195                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5196                    !!!cp ('t204');                    !!!cp ('t204');
5197                    !!!insert-element ($token->{tag_name}, $token->{attributes});                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5198                      !!!nack ('t204');
5199                    !!!next-token;                    !!!next-token;
5200                    redo B;                    next B;
5201                  } else {                  } else {
5202                    !!!cp ('t205');                    !!!cp ('t205');
5203                    !!!insert-element ('tr');                    !!!insert-element ('tr',, $token);
5204                    ## reprocess in the "in row" insertion mode                    ## reprocess in the "in row" insertion mode
5205                  }                  }
5206                } else {                } else {
# Line 4279  sub _tree_construction_main ($) { Line 5208  sub _tree_construction_main ($) {
5208                }                }
5209    
5210                ## Clear back to table row context                ## Clear back to table row context
5211                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5212                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5213                  !!!cp ('t207');                  !!!cp ('t207');
5214                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5215                }                }
5216                                
5217                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5218                $self->{insertion_mode} = IN_CELL_IM;                $self->{insertion_mode} = IN_CELL_IM;
5219    
5220                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
5221                                
5222                  !!!nack ('t207.1');
5223                !!!next-token;                !!!next-token;
5224                redo B;                next B;
5225              } elsif ({              } elsif ({
5226                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5227                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
# Line 4304  sub _tree_construction_main ($) { Line 5233  sub _tree_construction_main ($) {
5233                  my $i;                  my $i;
5234                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5235                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5236                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5237                      !!!cp ('t208');                      !!!cp ('t208');
5238                      $i = $_;                      $i = $_;
5239                      last INSCOPE;                      last INSCOPE;
5240                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             html => 1,  
   
                             ## NOTE: This element does not appear here, maybe.  
                             table => 1,  
                            }->{$node->[1]}) {  
5241                      !!!cp ('t209');                      !!!cp ('t209');
5242                      last INSCOPE;                      last INSCOPE;
5243                    }                    }
5244                  } # INSCOPE                  } # INSCOPE
5245                  unless (defined $i) {                  unless (defined $i) {
5246                   !!!cp ('t210');                    !!!cp ('t210');
5247  ## TODO: This type is wrong.  ## TODO: This type is wrong.
5248                   !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);
5249                    ## Ignore the token                    ## Ignore the token
5250                      !!!nack ('t210.1');
5251                    !!!next-token;                    !!!next-token;
5252                    redo B;                    next B;
5253                  }                  }
5254                                    
5255                  ## Clear back to table row context                  ## Clear back to table row context
5256                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5257                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5258                    !!!cp ('t211');                    !!!cp ('t211');
5259                    ## ISSUE: Can this case be reached?                    ## ISSUE: Can this case be reached?
5260                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4341  sub _tree_construction_main ($) { Line 5265  sub _tree_construction_main ($) {
5265                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5266                    !!!cp ('t212');                    !!!cp ('t212');
5267                    ## reprocess                    ## reprocess
5268                    redo B;                    !!!ack-later;
5269                      next B;
5270                  } else {                  } else {
5271                    !!!cp ('t213');                    !!!cp ('t213');
5272                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
# Line 4353  sub _tree_construction_main ($) { Line 5278  sub _tree_construction_main ($) {
5278                  my $i;                  my $i;
5279                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5280                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5281                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5282                      !!!cp ('t214');                      !!!cp ('t214');
5283                      $i = $_;                      $i = $_;
5284                      last INSCOPE;                      last INSCOPE;
5285                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5286                      !!!cp ('t215');                      !!!cp ('t215');
5287                      last INSCOPE;                      last INSCOPE;
5288                    }                    }
# Line 4369  sub _tree_construction_main ($) { Line 5290  sub _tree_construction_main ($) {
5290                  unless (defined $i) {                  unless (defined $i) {
5291                    !!!cp ('t216');                    !!!cp ('t216');
5292  ## TODO: This erorr type ios wrong.  ## TODO: This erorr type ios wrong.
5293                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5294                    ## Ignore the token                    ## Ignore the token
5295                      !!!nack ('t216.1');
5296                    !!!next-token;                    !!!next-token;
5297                    redo B;                    next B;
5298                  }                  }
5299    
5300                  ## Clear back to table body context                  ## Clear back to table body context
5301                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5302                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5303                    !!!cp ('t217');                    !!!cp ('t217');
5304                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5305                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4400  sub _tree_construction_main ($) { Line 5321  sub _tree_construction_main ($) {
5321    
5322                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
5323                  ## Clear back to table context                  ## Clear back to table context
5324                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5325                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5326                    !!!cp ('t219');                    !!!cp ('t219');
5327                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5328                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5329                  }                  }
5330                                    
5331                  !!!insert-element ('colgroup');                  !!!insert-element ('colgroup',, $token);
5332                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5333                  ## reprocess                  ## reprocess
5334                  redo B;                  !!!ack-later;
5335                    next B;
5336                } elsif ({                } elsif ({
5337                          caption => 1,                          caption => 1,
5338                          colgroup => 1,                          colgroup => 1,
5339                          tbody => 1, tfoot => 1, thead => 1,                          tbody => 1, tfoot => 1, thead => 1,
5340                         }->{$token->{tag_name}}) {                         }->{$token->{tag_name}}) {
5341                  ## Clear back to table context                  ## Clear back to table context
5342                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5343                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5344                    !!!cp ('t220');                    !!!cp ('t220');
5345                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5346                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4427  sub _tree_construction_main ($) { Line 5349  sub _tree_construction_main ($) {
5349                  push @$active_formatting_elements, ['#marker', '']                  push @$active_formatting_elements, ['#marker', '']
5350                      if $token->{tag_name} eq 'caption';                      if $token->{tag_name} eq 'caption';
5351                                    
5352                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5353                  $self->{insertion_mode} = {                  $self->{insertion_mode} = {
5354                                             caption => IN_CAPTION_IM,                                             caption => IN_CAPTION_IM,
5355                                             colgroup => IN_COLUMN_GROUP_IM,                                             colgroup => IN_COLUMN_GROUP_IM,
# Line 4436  sub _tree_construction_main ($) { Line 5358  sub _tree_construction_main ($) {
5358                                             thead => IN_TABLE_BODY_IM,                                             thead => IN_TABLE_BODY_IM,
5359                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
5360                  !!!next-token;                  !!!next-token;
5361                  redo B;                  !!!nack ('t220.1');
5362                    next B;
5363                } else {                } else {
5364                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
5365                }                }
5366              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5367                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
5368                                  value => $self->{open_elements}->[-1]->[0]
5369                                      ->manakai_local_name,
5370                                  token => $token);
5371    
5372                ## As if </table>                ## As if </table>
5373                ## have a table element in table scope                ## have a table element in table scope
5374                my $i;                my $i;
5375                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5376                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5377                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5378                    !!!cp ('t221');                    !!!cp ('t221');
5379                    $i = $_;                    $i = $_;
5380                    last INSCOPE;                    last INSCOPE;
5381                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           #table => 1,  
                           html => 1,  
                          }->{$node->[1]}) {  
5382                    !!!cp ('t222');                    !!!cp ('t222');
5383                    last INSCOPE;                    last INSCOPE;
5384                  }                  }
# Line 4463  sub _tree_construction_main ($) { Line 5386  sub _tree_construction_main ($) {
5386                unless (defined $i) {                unless (defined $i) {
5387                  !!!cp ('t223');                  !!!cp ('t223');
5388  ## TODO: The following is wrong, maybe.  ## TODO: The following is wrong, maybe.
5389                  !!!parse-error (type => 'unmatched end tag:table');                  !!!parse-error (type => 'unmatched end tag:table', token => $token);
5390                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5391                    !!!nack ('t223.1');
5392                  !!!next-token;                  !!!next-token;
5393                  redo B;                  next B;
5394                }                }
5395                                
5396    ## TODO: Followings are removed from the latest spec.
5397                ## generate implied end tags                ## generate implied end tags
5398                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
5399                  !!!cp ('t224');                  !!!cp ('t224');
5400                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5401                }                }
5402    
5403                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5404                  !!!cp ('t225');                  !!!cp ('t225');
5405  ## ISSUE: Can this case be reached?                  ## NOTE: |<table><tr><table>|
5406                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed',
5407                                    value => $self->{open_elements}->[-1]->[0]
5408                                        ->manakai_local_name,
5409                                    token => $token);
5410                } else {                } else {
5411                  !!!cp ('t226');                  !!!cp ('t226');
5412                }                }
# Line 4490  sub _tree_construction_main ($) { Line 5416  sub _tree_construction_main ($) {
5416    
5417                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5418    
5419                ## reprocess            ## reprocess
5420                redo B;            !!!ack-later;
5421              next B;
5422          } elsif ($token->{tag_name} eq 'style') {          } elsif ($token->{tag_name} eq 'style') {
5423            if (not $open_tables->[-1]->[1]) { # tainted            if (not $open_tables->[-1]->[1]) { # tainted
5424              !!!cp ('t227.8');              !!!cp ('t227.8');
5425              ## NOTE: This is a "as if in head" code clone.              ## NOTE: This is a "as if in head" code clone.
5426              $parse_rcdata->(CDATA_CONTENT_MODEL);              $parse_rcdata->(CDATA_CONTENT_MODEL);
5427              redo B;              next B;
5428            } else {            } else {
5429              !!!cp ('t227.7');              !!!cp ('t227.7');
5430              #              #
# Line 4507  sub _tree_construction_main ($) { Line 5434  sub _tree_construction_main ($) {
5434              !!!cp ('t227.6');              !!!cp ('t227.6');
5435              ## NOTE: This is a "as if in head" code clone.              ## NOTE: This is a "as if in head" code clone.
5436              $script_start_tag->();              $script_start_tag->();
5437              redo B;              next B;
5438            } else {            } else {
5439              !!!cp ('t227.5');              !!!cp ('t227.5');
5440              #              #
# Line 4518  sub _tree_construction_main ($) { Line 5445  sub _tree_construction_main ($) {
5445                my $type = lc $token->{attributes}->{type}->{value};                my $type = lc $token->{attributes}->{type}->{value};
5446                if ($type eq 'hidden') {                if ($type eq 'hidden') {
5447                  !!!cp ('t227.3');                  !!!cp ('t227.3');
5448                  !!!parse-error (type => 'in table:'.$token->{tag_name});                  !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
5449    
5450                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5451    
5452                  ## TODO: form element pointer                  ## TODO: form element pointer
5453    
5454                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5455    
5456                  !!!next-token;                  !!!next-token;
5457                  redo B;                  !!!ack ('t227.2.1');
5458                    next B;
5459                } else {                } else {
5460                  !!!cp ('t227.2');                  !!!cp ('t227.2');
5461                  #                  #
# Line 4545  sub _tree_construction_main ($) { Line 5473  sub _tree_construction_main ($) {
5473            #            #
5474          }          }
5475    
5476          !!!parse-error (type => 'in table:'.$token->{tag_name});          !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
5477    
5478          $insert = $insert_to_foster;          $insert = $insert_to_foster;
5479          #          #
# Line 4556  sub _tree_construction_main ($) { Line 5484  sub _tree_construction_main ($) {
5484                my $i;                my $i;
5485                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5486                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5487                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_ROW_EL) {
5488                    !!!cp ('t228');                    !!!cp ('t228');
5489                    $i = $_;                    $i = $_;
5490                    last INSCOPE;                    last INSCOPE;
5491                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5492                    !!!cp ('t229');                    !!!cp ('t229');
5493                    last INSCOPE;                    last INSCOPE;
5494                  }                  }
5495                } # INSCOPE                } # INSCOPE
5496                unless (defined $i) {                unless (defined $i) {
5497                  !!!cp ('t230');                  !!!cp ('t230');
5498                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5499                  ## Ignore the token                  ## Ignore the token
5500                    !!!nack ('t230.1');
5501                  !!!next-token;                  !!!next-token;
5502                  redo B;                  next B;
5503                } else {                } else {
5504                  !!!cp ('t232');                  !!!cp ('t232');
5505                }                }
5506    
5507                ## Clear back to table row context                ## Clear back to table row context
5508                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5509                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5510                  !!!cp ('t231');                  !!!cp ('t231');
5511  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
5512                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 4589  sub _tree_construction_main ($) { Line 5515  sub _tree_construction_main ($) {
5515                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5516                $self->{insertion_mode} = IN_TABLE_BODY_IM;                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5517                !!!next-token;                !!!next-token;
5518                redo B;                !!!nack ('t231.1');
5519                  next B;
5520              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5521                if ($self->{insertion_mode} == IN_ROW_IM) {                if ($self->{insertion_mode} == IN_ROW_IM) {
5522                  ## As if </tr>                  ## As if </tr>
# Line 4597  sub _tree_construction_main ($) { Line 5524  sub _tree_construction_main ($) {
5524                  my $i;                  my $i;
5525                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5526                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5527                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5528                      !!!cp ('t233');                      !!!cp ('t233');
5529                      $i = $_;                      $i = $_;
5530                      last INSCOPE;                      last INSCOPE;
5531                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5532                      !!!cp ('t234');                      !!!cp ('t234');
5533                      last INSCOPE;                      last INSCOPE;
5534                    }                    }
# Line 4611  sub _tree_construction_main ($) { Line 5536  sub _tree_construction_main ($) {
5536                  unless (defined $i) {                  unless (defined $i) {
5537                    !!!cp ('t235');                    !!!cp ('t235');
5538  ## TODO: The following is wrong.  ## TODO: The following is wrong.
5539                    !!!parse-error (type => 'unmatched end tag:'.$token->{type});                    !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);
5540                    ## Ignore the token                    ## Ignore the token
5541                      !!!nack ('t236.1');
5542                    !!!next-token;                    !!!next-token;
5543                    redo B;                    next B;
5544                  }                  }
5545                                    
5546                  ## Clear back to table row context                  ## Clear back to table row context
5547                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5548                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5549                    !!!cp ('t236');                    !!!cp ('t236');
5550  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
5551                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4636  sub _tree_construction_main ($) { Line 5561  sub _tree_construction_main ($) {
5561                  my $i;                  my $i;
5562                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5563                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5564                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5565                      !!!cp ('t237');                      !!!cp ('t237');
5566                      $i = $_;                      $i = $_;
5567                      last INSCOPE;                      last INSCOPE;
5568                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5569                      !!!cp ('t238');                      !!!cp ('t238');
5570                      last INSCOPE;                      last INSCOPE;
5571                    }                    }
5572                  } # INSCOPE                  } # INSCOPE
5573                  unless (defined $i) {                  unless (defined $i) {
5574                    !!!cp ('t239');                    !!!cp ('t239');
5575                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5576                    ## Ignore the token                    ## Ignore the token
5577                      !!!nack ('t239.1');
5578                    !!!next-token;                    !!!next-token;
5579                    redo B;                    next B;
5580                  }                  }
5581                                    
5582                  ## Clear back to table body context                  ## Clear back to table body context
5583                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5584                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5585                    !!!cp ('t240');                    !!!cp ('t240');
5586                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5587                  }                  }
# Line 4686  sub _tree_construction_main ($) { Line 5607  sub _tree_construction_main ($) {
5607                my $i;                my $i;
5608                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5609                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5610                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_EL) {
5611                    !!!cp ('t241');                    !!!cp ('t241');
5612                    $i = $_;                    $i = $_;
5613                    last INSCOPE;                    last INSCOPE;
5614                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5615                    !!!cp ('t242');                    !!!cp ('t242');
5616                    last INSCOPE;                    last INSCOPE;
5617                  }                  }
5618                } # INSCOPE                } # INSCOPE
5619                unless (defined $i) {                unless (defined $i) {
5620                  !!!cp ('t243');                  !!!cp ('t243');
5621                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5622                  ## Ignore the token                  ## Ignore the token
5623                    !!!nack ('t243.1');
5624                  !!!next-token;                  !!!next-token;
5625                  redo B;                  next B;
5626                }                }
5627                                    
5628                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
# Line 4711  sub _tree_construction_main ($) { Line 5631  sub _tree_construction_main ($) {
5631                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5632                                
5633                !!!next-token;                !!!next-token;
5634                redo B;                next B;
5635              } elsif ({              } elsif ({
5636                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5637                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
# Line 4721  sub _tree_construction_main ($) { Line 5641  sub _tree_construction_main ($) {
5641                  my $i;                  my $i;
5642                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5643                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5644                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5645                      !!!cp ('t247');                      !!!cp ('t247');
5646                      $i = $_;                      $i = $_;
5647                      last INSCOPE;                      last INSCOPE;
5648                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5649                      !!!cp ('t248');                      !!!cp ('t248');
5650                      last INSCOPE;                      last INSCOPE;
5651                    }                    }
5652                  } # INSCOPE                  } # INSCOPE
5653                    unless (defined $i) {                    unless (defined $i) {
5654                      !!!cp ('t249');                      !!!cp ('t249');
5655                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5656                      ## Ignore the token                      ## Ignore the token
5657                        !!!nack ('t249.1');
5658                      !!!next-token;                      !!!next-token;
5659                      redo B;                      next B;
5660                    }                    }
5661                                    
5662                  ## As if </tr>                  ## As if </tr>
# Line 4745  sub _tree_construction_main ($) { Line 5664  sub _tree_construction_main ($) {
5664                  my $i;                  my $i;
5665                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5666                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5667                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5668                      !!!cp ('t250');                      !!!cp ('t250');
5669                      $i = $_;                      $i = $_;
5670                      last INSCOPE;                      last INSCOPE;
5671                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5672                      !!!cp ('t251');                      !!!cp ('t251');
5673                      last INSCOPE;                      last INSCOPE;
5674                    }                    }
5675                  } # INSCOPE                  } # INSCOPE
5676                    unless (defined $i) {                    unless (defined $i) {
5677                      !!!cp ('t252');                      !!!cp ('t252');
5678                      !!!parse-error (type => 'unmatched end tag:tr');                      !!!parse-error (type => 'unmatched end tag:tr', token => $token);
5679                      ## Ignore the token                      ## Ignore the token
5680                        !!!nack ('t252.1');
5681                      !!!next-token;                      !!!next-token;
5682                      redo B;                      next B;
5683                    }                    }
5684                                    
5685                  ## Clear back to table row context                  ## Clear back to table row context
5686                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5687                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5688                    !!!cp ('t253');                    !!!cp ('t253');
5689  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
5690                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4782  sub _tree_construction_main ($) { Line 5699  sub _tree_construction_main ($) {
5699                my $i;                my $i;
5700                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5701                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5702                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5703                    !!!cp ('t254');                    !!!cp ('t254');
5704                    $i = $_;                    $i = $_;
5705                    last INSCOPE;                    last INSCOPE;
5706                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5707                    !!!cp ('t255');                    !!!cp ('t255');
5708                    last INSCOPE;                    last INSCOPE;
5709                  }                  }
5710                } # INSCOPE                } # INSCOPE
5711                unless (defined $i) {                unless (defined $i) {
5712                  !!!cp ('t256');                  !!!cp ('t256');
5713                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5714                  ## Ignore the token                  ## Ignore the token
5715                    !!!nack ('t256.1');
5716                  !!!next-token;                  !!!next-token;
5717                  redo B;                  next B;
5718                }                }
5719    
5720                ## Clear back to table body context                ## Clear back to table body context
5721                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5722                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5723                  !!!cp ('t257');                  !!!cp ('t257');
5724  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
5725                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 4812  sub _tree_construction_main ($) { Line 5727  sub _tree_construction_main ($) {
5727    
5728                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5729                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5730                  !!!nack ('t257.1');
5731                !!!next-token;                !!!next-token;
5732                redo B;                next B;
5733              } elsif ({              } elsif ({
5734                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
5735                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
5736                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5737                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
5738                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5739                !!!cp ('t258');            !!!cp ('t258');
5740                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5741                ## Ignore the token            ## Ignore the token
5742                !!!next-token;            !!!nack ('t258.1');
5743                redo B;             !!!next-token;
5744              next B;
5745          } else {          } else {
5746            !!!cp ('t259');            !!!cp ('t259');
5747            !!!parse-error (type => 'in table:/'.$token->{tag_name});            !!!parse-error (type => 'in table:/'.$token->{tag_name}, token => $token);
5748    
5749            $insert = $insert_to_foster;            $insert = $insert_to_foster;
5750            #            #
5751          }          }
5752          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5753            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5754                    @{$self->{open_elements}} == 1) { # redundant, maybe
5755              !!!parse-error (type => 'in body:#eof', token => $token);
5756              !!!cp ('t259.1');
5757              #
5758            } else {
5759              !!!cp ('t259.2');
5760              #
5761            }
5762    
5763            ## Stop parsing
5764            last B;
5765        } else {        } else {
5766          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5767        }        }
# Line 4842  sub _tree_construction_main ($) { Line 5772  sub _tree_construction_main ($) {
5772                unless (length $token->{data}) {                unless (length $token->{data}) {
5773                  !!!cp ('t260');                  !!!cp ('t260');
5774                  !!!next-token;                  !!!next-token;
5775                  redo B;                  next B;
5776                }                }
5777              }              }
5778                            
# Line 4851  sub _tree_construction_main ($) { Line 5781  sub _tree_construction_main ($) {
5781            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
5782              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
5783                !!!cp ('t262');                !!!cp ('t262');
5784                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5785                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5786                  !!!ack ('t262.1');
5787                !!!next-token;                !!!next-token;
5788                redo B;                next B;
5789              } else {              } else {
5790                !!!cp ('t263');                !!!cp ('t263');
5791                #                #
5792              }              }
5793            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
5794              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
5795                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5796                  !!!cp ('t264');                  !!!cp ('t264');
5797                  !!!parse-error (type => 'unmatched end tag:colgroup');                  !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5798                  ## Ignore the token                  ## Ignore the token
5799                  !!!next-token;                  !!!next-token;
5800                  redo B;                  next B;
5801                } else {                } else {
5802                  !!!cp ('t265');                  !!!cp ('t265');
5803                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
5804                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5805                  !!!next-token;                  !!!next-token;
5806                  redo B;                              next B;            
5807                }                }
5808              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
5809                !!!cp ('t266');                !!!cp ('t266');
5810                !!!parse-error (type => 'unmatched end tag:col');                !!!parse-error (type => 'unmatched end tag:col', token => $token);
5811                ## Ignore the token                ## Ignore the token
5812                !!!next-token;                !!!next-token;
5813                redo B;                next B;
5814              } else {              } else {
5815                !!!cp ('t267');                !!!cp ('t267');
5816                #                #
5817              }              }
5818            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5819              die "$0: $token->{type}: Unknown token type";          if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5820            }              @{$self->{open_elements}} == 1) { # redundant, maybe
5821              !!!cp ('t270.2');
5822              ## Stop parsing.
5823              last B;
5824            } else {
5825              ## NOTE: As if </colgroup>.
5826              !!!cp ('t270.1');
5827              pop @{$self->{open_elements}}; # colgroup
5828              $self->{insertion_mode} = IN_TABLE_IM;
5829              ## Reprocess.
5830              next B;
5831            }
5832          } else {
5833            die "$0: $token->{type}: Unknown token type";
5834          }
5835    
5836            ## As if </colgroup>            ## As if </colgroup>
5837            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5838              !!!cp ('t269');              !!!cp ('t269');
5839              !!!parse-error (type => 'unmatched end tag:colgroup');  ## TODO: Wrong error type?
5840                !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5841              ## Ignore the token              ## Ignore the token
5842                !!!nack ('t269.1');
5843              !!!next-token;              !!!next-token;
5844              redo B;              next B;
5845            } else {            } else {
5846              !!!cp ('t270');              !!!cp ('t270');
5847              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
5848              $self->{insertion_mode} = IN_TABLE_IM;              $self->{insertion_mode} = IN_TABLE_IM;
5849                !!!ack-later;
5850              ## reprocess              ## reprocess
5851              redo B;              next B;
5852            }            }
5853      } elsif ($self->{insertion_mode} == IN_SELECT_IM) {      } elsif ($self->{insertion_mode} & SELECT_IMS) {
5854        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5855          !!!cp ('t271');          !!!cp ('t271');
5856          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5857          !!!next-token;          !!!next-token;
5858          redo B;          next B;
5859        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5860              if ($token->{tag_name} eq 'option') {          if ($token->{tag_name} eq 'option') {
5861                if ($self->{open_elements}->[-1]->[1] eq 'option') {            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5862                  !!!cp ('t272');              !!!cp ('t272');
5863                  ## As if </option>              ## As if </option>
5864                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5865                } else {            } else {
5866                  !!!cp ('t273');              !!!cp ('t273');
5867                }            }
5868    
5869                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5870                !!!next-token;            !!!nack ('t273.1');
5871                redo B;            !!!next-token;
5872              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
5873                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
5874                  !!!cp ('t274');            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5875                  ## As if </option>              !!!cp ('t274');
5876                  pop @{$self->{open_elements}};              ## As if </option>
5877                } else {              pop @{$self->{open_elements}};
5878                  !!!cp ('t275');            } else {
5879                }              !!!cp ('t275');
5880              }
5881    
5882                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5883                  !!!cp ('t276');              !!!cp ('t276');
5884                  ## As if </optgroup>              ## As if </optgroup>
5885                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5886                } else {            } else {
5887                  !!!cp ('t277');              !!!cp ('t277');
5888                }            }
5889    
5890                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5891                !!!next-token;            !!!nack ('t277.1');
5892                redo B;            !!!next-token;
5893              } elsif ($token->{tag_name} eq 'select') {            next B;
5894  ## TODO: The type below is not good - <select> is replaced by </select>          } elsif ({
5895                !!!parse-error (type => 'not closed:select');                     select => 1, input => 1, textarea => 1,
5896                ## As if </select> instead                   }->{$token->{tag_name}} or
5897                ## have an element in table scope                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5898                my $i;                    {
5899                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                     caption => 1, table => 1,
5900                  my $node = $self->{open_elements}->[$_];                     tbody => 1, tfoot => 1, thead => 1,
5901                  if ($node->[1] eq $token->{tag_name}) {                     tr => 1, td => 1, th => 1,
5902                    !!!cp ('t278');                    }->{$token->{tag_name}})) {
5903                    $i = $_;            ## TODO: The type below is not good - <select> is replaced by </select>
5904                    last INSCOPE;            !!!parse-error (type => 'not closed:select', token => $token);
5905                  } elsif ({            ## NOTE: As if the token were </select> (<select> case) or
5906                            table => 1, html => 1,            ## as if there were </select> (otherwise).
5907                           }->{$node->[1]}) {            ## have an element in table scope
5908                    !!!cp ('t279');            my $i;
5909                    last INSCOPE;            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5910                  }              my $node = $self->{open_elements}->[$_];
5911                } # INSCOPE              if ($node->[1] & SELECT_EL) {
5912                unless (defined $i) {                !!!cp ('t278');
5913                  !!!cp ('t280');                $i = $_;
5914                  !!!parse-error (type => 'unmatched end tag:select');                last INSCOPE;
5915                  ## Ignore the token              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5916                  !!!next-token;                !!!cp ('t279');
5917                  redo B;                last INSCOPE;
5918                }              }
5919              } # INSCOPE
5920              unless (defined $i) {
5921                !!!cp ('t280');
5922                !!!parse-error (type => 'unmatched end tag:select', token => $token);
5923                ## Ignore the token
5924                !!!nack ('t280.1');
5925                !!!next-token;
5926                next B;
5927              }
5928                                
5929                !!!cp ('t281');            !!!cp ('t281');
5930                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
5931    
5932                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5933    
5934                !!!next-token;            if ($token->{tag_name} eq 'select') {
5935                redo B;              !!!nack ('t281.2');
5936                !!!next-token;
5937                next B;
5938              } else {
5939                !!!cp ('t281.1');
5940                !!!ack-later;
5941                ## Reprocess the token.
5942                next B;
5943              }
5944          } else {          } else {
5945            !!!cp ('t282');            !!!cp ('t282');
5946            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);
5947            ## Ignore the token            ## Ignore the token
5948              !!!nack ('t282.1');
5949            !!!next-token;            !!!next-token;
5950            redo B;            next B;
5951          }          }
5952        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5953              if ($token->{tag_name} eq 'optgroup') {          if ($token->{tag_name} eq 'optgroup') {
5954                if ($self->{open_elements}->[-1]->[1] eq 'option' and            if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
5955                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
5956                  !!!cp ('t283');              !!!cp ('t283');
5957                  ## As if </option>              ## As if </option>
5958                  splice @{$self->{open_elements}}, -2;              splice @{$self->{open_elements}}, -2;
5959                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5960                  !!!cp ('t284');              !!!cp ('t284');
5961                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5962                } else {            } else {
5963                  !!!cp ('t285');              !!!cp ('t285');
5964                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5965                  ## Ignore the token              ## Ignore the token
5966                }            }
5967                !!!next-token;            !!!nack ('t285.1');
5968                redo B;            !!!next-token;
5969              } elsif ($token->{tag_name} eq 'option') {            next B;
5970                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'option') {
5971                  !!!cp ('t286');            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5972                  pop @{$self->{open_elements}};              !!!cp ('t286');
5973                } else {              pop @{$self->{open_elements}};
5974                  !!!cp ('t287');            } else {
5975                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t287');
5976                  ## Ignore the token              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5977                }              ## Ignore the token
5978                !!!next-token;            }
5979                redo B;            !!!nack ('t287.1');
5980              } elsif ($token->{tag_name} eq 'select') {            !!!next-token;
5981                ## have an element in table scope            next B;
5982                my $i;          } elsif ($token->{tag_name} eq 'select') {
5983                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            ## have an element in table scope
5984                  my $node = $self->{open_elements}->[$_];            my $i;
5985                  if ($node->[1] eq $token->{tag_name}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5986                    !!!cp ('t288');              my $node = $self->{open_elements}->[$_];
5987                    $i = $_;              if ($node->[1] & SELECT_EL) {
5988                    last INSCOPE;                !!!cp ('t288');
5989                  } elsif ({                $i = $_;
5990                            table => 1, html => 1,                last INSCOPE;
5991                           }->{$node->[1]}) {              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5992                    !!!cp ('t289');                !!!cp ('t289');
5993                    last INSCOPE;                last INSCOPE;
5994                  }              }
5995                } # INSCOPE            } # INSCOPE
5996                unless (defined $i) {            unless (defined $i) {
5997                  !!!cp ('t290');              !!!cp ('t290');
5998                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5999                  ## Ignore the token              ## Ignore the token
6000                  !!!next-token;              !!!nack ('t290.1');
6001                  redo B;              !!!next-token;
6002                }              next B;
6003              }
6004                                
6005                !!!cp ('t291');            !!!cp ('t291');
6006                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6007    
6008                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6009    
6010                !!!next-token;            !!!nack ('t291.1');
6011                redo B;            !!!next-token;
6012              } elsif ({            next B;
6013                        caption => 1, table => 1, tbody => 1,          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6014                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                   {
6015                       }->{$token->{tag_name}}) {                    caption => 1, table => 1, tbody => 1,
6016                      tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
6017                     }->{$token->{tag_name}}) {
6018  ## TODO: The following is wrong?  ## TODO: The following is wrong?
6019                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6020                                
6021                ## have an element in table scope            ## have an element in table scope
6022                my $i;            my $i;
6023                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6024                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6025                  if ($node->[1] eq $token->{tag_name}) {              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6026                    !!!cp ('t292');                !!!cp ('t292');
6027                    $i = $_;                $i = $_;
6028                    last INSCOPE;                last INSCOPE;
6029                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6030                            table => 1, html => 1,                !!!cp ('t293');
6031                           }->{$node->[1]}) {                last INSCOPE;
6032                    !!!cp ('t293');              }
6033                    last INSCOPE;            } # INSCOPE
6034                  }            unless (defined $i) {
6035                } # INSCOPE              !!!cp ('t294');
6036                unless (defined $i) {              ## Ignore the token
6037                  !!!cp ('t294');              !!!nack ('t294.1');
6038                  ## Ignore the token              !!!next-token;
6039                  !!!next-token;              next B;
6040                  redo B;            }
               }  
6041                                
6042                ## As if </select>            ## As if </select>
6043                ## have an element in table scope            ## have an element in table scope
6044                undef $i;            undef $i;
6045                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6046                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6047                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
6048                    !!!cp ('t295');                !!!cp ('t295');
6049                    $i = $_;                $i = $_;
6050                    last INSCOPE;                last INSCOPE;
6051                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
6052  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
6053                    !!!cp ('t296');                !!!cp ('t296');
6054                    last INSCOPE;                last INSCOPE;
6055                  }              }
6056                } # INSCOPE            } # INSCOPE
6057                unless (defined $i) {            unless (defined $i) {
6058                  !!!cp ('t297');              !!!cp ('t297');
6059  ## TODO: The following error type is correct?  ## TODO: The following error type is correct?
6060                  !!!parse-error (type => 'unmatched end tag:select');              !!!parse-error (type => 'unmatched end tag:select', token => $token);
6061                  ## Ignore the </select> token              ## Ignore the </select> token
6062                  !!!next-token; ## TODO: ok?              !!!nack ('t297.1');
6063                  redo B;              !!!next-token; ## TODO: ok?
6064                }              next B;
6065              }
6066                                
6067                !!!cp ('t298');            !!!cp ('t298');
6068                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6069    
6070                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6071    
6072                ## reprocess            !!!ack-later;
6073                redo B;            ## reprocess
6074              next B;
6075          } else {          } else {
6076            !!!cp ('t299');            !!!cp ('t299');
6077            !!!parse-error (type => 'in select:/'.$token->{tag_name});            !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);
6078            ## Ignore the token            ## Ignore the token
6079              !!!nack ('t299.3');
6080            !!!next-token;            !!!next-token;
6081            redo B;            next B;
6082            }
6083          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6084            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6085                    @{$self->{open_elements}} == 1) { # redundant, maybe
6086              !!!cp ('t299.1');
6087              !!!parse-error (type => 'in body:#eof', token => $token);
6088            } else {
6089              !!!cp ('t299.2');
6090          }          }
6091    
6092            ## Stop parsing.
6093            last B;
6094        } else {        } else {
6095          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6096        }        }
# Line 5125  sub _tree_construction_main ($) { Line 6106  sub _tree_construction_main ($) {
6106            unless (length $token->{data}) {            unless (length $token->{data}) {
6107              !!!cp ('t300');              !!!cp ('t300');
6108              !!!next-token;              !!!next-token;
6109              redo B;              next B;
6110            }            }
6111          }          }
6112                    
6113          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6114            !!!cp ('t301');            !!!cp ('t301');
6115            !!!parse-error (type => 'after html:#character');            !!!parse-error (type => 'after html:#character', token => $token);
6116    
6117            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
6118          } else {          } else {
# Line 5139  sub _tree_construction_main ($) { Line 6120  sub _tree_construction_main ($) {
6120          }          }
6121                    
6122          ## "after body" insertion mode          ## "after body" insertion mode
6123          !!!parse-error (type => 'after body:#character');          !!!parse-error (type => 'after body:#character', token => $token);
6124    
6125          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6126          ## reprocess          ## reprocess
6127          redo B;          next B;
6128        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6129          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6130            !!!cp ('t303');            !!!cp ('t303');
6131            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
6132                        
6133            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
6134          } else {          } else {
# Line 5155  sub _tree_construction_main ($) { Line 6136  sub _tree_construction_main ($) {
6136          }          }
6137    
6138          ## "after body" insertion mode          ## "after body" insertion mode
6139          !!!parse-error (type => 'after body:'.$token->{tag_name});          !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);
6140    
6141          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6142            !!!ack-later;
6143          ## reprocess          ## reprocess
6144          redo B;          next B;
6145        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6146          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6147            !!!cp ('t305');            !!!cp ('t305');
6148            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
6149                        
6150            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6151            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
# Line 5175  sub _tree_construction_main ($) { Line 6157  sub _tree_construction_main ($) {
6157          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
6158            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
6159              !!!cp ('t307');              !!!cp ('t307');
6160              !!!parse-error (type => 'unmatched end tag:html');              !!!parse-error (type => 'unmatched end tag:html', token => $token);
6161              ## Ignore the token              ## Ignore the token
6162              !!!next-token;              !!!next-token;
6163              redo B;              next B;
6164            } else {            } else {
6165              !!!cp ('t308');              !!!cp ('t308');
6166              $self->{insertion_mode} = AFTER_HTML_BODY_IM;              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6167              !!!next-token;              !!!next-token;
6168              redo B;              next B;
6169            }            }
6170          } else {          } else {
6171            !!!cp ('t309');            !!!cp ('t309');
6172            !!!parse-error (type => 'after body:/'.$token->{tag_name});            !!!parse-error (type => 'after body:/'.$token->{tag_name}, token => $token);
6173    
6174            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
6175            ## reprocess            ## reprocess
6176            redo B;            next B;
6177          }          }
6178          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6179            !!!cp ('t309.2');
6180            ## Stop parsing
6181            last B;
6182        } else {        } else {
6183          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6184        }        }
# Line 5204  sub _tree_construction_main ($) { Line 6190  sub _tree_construction_main ($) {
6190            unless (length $token->{data}) {            unless (length $token->{data}) {
6191              !!!cp ('t310');              !!!cp ('t310');
6192              !!!next-token;              !!!next-token;
6193              redo B;              next B;
6194            }            }
6195          }          }
6196                    
6197          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
6198            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6199              !!!cp ('t311');              !!!cp ('t311');
6200              !!!parse-error (type => 'in frameset:#character');              !!!parse-error (type => 'in frameset:#character', token => $token);
6201            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6202              !!!cp ('t312');              !!!cp ('t312');
6203              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#character', token => $token);
6204            } else { # "after html frameset"            } else { # "after html frameset"
6205              !!!cp ('t313');              !!!cp ('t313');
6206              !!!parse-error (type => 'after html:#character');              !!!parse-error (type => 'after html:#character', token => $token);
6207    
6208              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6209              ## Reprocess in the "after frameset" insertion mode.              ## Reprocess in the "after frameset" insertion mode.
6210              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#character', token => $token);
6211            }            }
6212                        
6213            ## Ignore the token.            ## Ignore the token.
# Line 5232  sub _tree_construction_main ($) { Line 6218  sub _tree_construction_main ($) {
6218              !!!cp ('t315');              !!!cp ('t315');
6219              !!!next-token;              !!!next-token;
6220            }            }
6221            redo B;            next B;
6222          }          }
6223                    
6224          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
6225        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6226          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
6227            !!!cp ('t316');            !!!cp ('t316');
6228            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
6229    
6230            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
6231            ## Process in the "after frameset" insertion mode.            ## Process in the "after frameset" insertion mode.
# Line 5250  sub _tree_construction_main ($) { Line 6236  sub _tree_construction_main ($) {
6236          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6237              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6238            !!!cp ('t318');            !!!cp ('t318');
6239            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6240              !!!nack ('t318.1');
6241            !!!next-token;            !!!next-token;
6242            redo B;            next B;
6243          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
6244                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
6245            !!!cp ('t319');            !!!cp ('t319');
6246            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6247            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6248              !!!ack ('t319.1');
6249            !!!next-token;            !!!next-token;
6250            redo B;            next B;
6251          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
6252            !!!cp ('t320');            !!!cp ('t320');
6253            ## NOTE: As if in body.            ## NOTE: As if in head.
6254            $parse_rcdata->(CDATA_CONTENT_MODEL);            $parse_rcdata->(CDATA_CONTENT_MODEL);
6255            redo B;            next B;
6256          } else {          } else {
6257            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6258              !!!cp ('t321');              !!!cp ('t321');
6259              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!parse-error (type => 'in frameset:'.$token->{tag_name}, token => $token);
6260            } else {            } else {
6261              !!!cp ('t322');              !!!cp ('t322');
6262              !!!parse-error (type => 'after frameset:'.$token->{tag_name});              !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);
6263            }            }
6264            ## Ignore the token            ## Ignore the token
6265              !!!nack ('t322.1');
6266            !!!next-token;            !!!next-token;
6267            redo B;            next B;
6268          }          }
6269        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6270          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
6271            !!!cp ('t323');            !!!cp ('t323');
6272            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
6273    
6274            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
6275            ## Process in the "after frameset" insertion mode.            ## Process in the "after frameset" insertion mode.
# Line 5290  sub _tree_construction_main ($) { Line 6279  sub _tree_construction_main ($) {
6279    
6280          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6281              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6282            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6283                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
6284              !!!cp ('t325');              !!!cp ('t325');
6285              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6286              ## Ignore the token              ## Ignore the token
6287              !!!next-token;              !!!next-token;
6288            } else {            } else {
# Line 5303  sub _tree_construction_main ($) { Line 6292  sub _tree_construction_main ($) {
6292            }            }
6293    
6294            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
6295                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6296              !!!cp ('t327');              !!!cp ('t327');
6297              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6298            } else {            } else {
6299              !!!cp ('t328');              !!!cp ('t328');
6300            }            }
6301            redo B;            next B;
6302          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
6303                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6304            !!!cp ('t329');            !!!cp ('t329');
6305            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6306            !!!next-token;            !!!next-token;
6307            redo B;            next B;
6308          } else {          } else {
6309            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6310              !!!cp ('t330');              !!!cp ('t330');
6311              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});              !!!parse-error (type => 'in frameset:/'.$token->{tag_name}, token => $token);
6312            } else {            } else {
6313              !!!cp ('t331');              !!!cp ('t331');
6314              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});              !!!parse-error (type => 'after frameset:/'.$token->{tag_name}, token => $token);
6315            }            }
6316            ## Ignore the token            ## Ignore the token
6317            !!!next-token;            !!!next-token;
6318            redo B;            next B;
6319            }
6320          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6321            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6322                    @{$self->{open_elements}} == 1) { # redundant, maybe
6323              !!!cp ('t331.1');
6324              !!!parse-error (type => 'in body:#eof', token => $token);
6325            } else {
6326              !!!cp ('t331.2');
6327          }          }
6328            
6329            ## Stop parsing
6330            last B;
6331        } else {        } else {
6332          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6333        }        }
# Line 5343  sub _tree_construction_main ($) { Line 6343  sub _tree_construction_main ($) {
6343          !!!cp ('t332');          !!!cp ('t332');
6344          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6345          $script_start_tag->();          $script_start_tag->();
6346          redo B;          next B;
6347        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
6348          !!!cp ('t333');          !!!cp ('t333');
6349          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6350          $parse_rcdata->(CDATA_CONTENT_MODEL);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6351          redo B;          next B;
6352        } elsif ({        } elsif ({
6353                  base => 1, link => 1,                  base => 1, link => 1,
6354                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6355          !!!cp ('t334');          !!!cp ('t334');
6356          ## 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
6357          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6358          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6359            !!!ack ('t334.1');
6360          !!!next-token;          !!!next-token;
6361          redo B;          next B;
6362        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
6363          ## 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
6364          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6365          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.
6366    
6367          unless ($self->{confident}) {          unless ($self->{confident}) {
6368            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) {
6369              !!!cp ('t335');              !!!cp ('t335');
6370                ## NOTE: Whether the encoding is supported or not is handled
6371                ## in the {change_encoding} callback.
6372              $self->{change_encoding}              $self->{change_encoding}
6373                  ->($self, $token->{attributes}->{charset}->{value});                  ->($self, $token->{attributes}->{charset}->{value}, $token);
6374                            
6375              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6376                  ->set_user_data (manakai_has_reference =>                  ->set_user_data (manakai_has_reference =>
6377                                       $token->{attributes}->{charset}                                       $token->{attributes}->{charset}
6378                                           ->{has_reference});                                           ->{has_reference});
6379            } elsif ($token->{attributes}->{content}) {            } elsif ($token->{attributes}->{content}) {
             ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
6380              if ($token->{attributes}->{content}->{value}              if ($token->{attributes}->{content}->{value}
6381                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                  =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6382                      [\x09-\x0D\x20]*=                      [\x09-\x0D\x20]*=
6383                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6384                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
6385                !!!cp ('t336');                !!!cp ('t336');
6386                  ## NOTE: Whether the encoding is supported or not is handled
6387                  ## in the {change_encoding} callback.
6388                $self->{change_encoding}                $self->{change_encoding}
6389                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6390                $meta_el->[0]->get_attribute_node_ns (undef, 'content')                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6391                    ->set_user_data (manakai_has_reference =>                    ->set_user_data (manakai_has_reference =>
6392                                         $token->{attributes}->{content}                                         $token->{attributes}->{content}
# Line 5406  sub _tree_construction_main ($) { Line 6410  sub _tree_construction_main ($) {
6410            }            }
6411          }          }
6412    
6413            !!!ack ('t338.1');
6414          !!!next-token;          !!!next-token;
6415          redo B;          next B;
6416        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
6417          !!!cp ('t341');          !!!cp ('t341');
6418          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6419          $parse_rcdata->(RCDATA_CONTENT_MODEL);          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6420          redo B;          next B;
6421        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
6422          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body:body', token => $token);
6423                                
6424          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
6425              $self->{open_elements}->[1]->[1] ne 'body') {              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6426            !!!cp ('t342');            !!!cp ('t342');
6427            ## Ignore the token            ## Ignore the token
6428          } else {          } else {
# Line 5431  sub _tree_construction_main ($) { Line 6436  sub _tree_construction_main ($) {
6436              }              }
6437            }            }
6438          }          }
6439            !!!nack ('t343.1');
6440          !!!next-token;          !!!next-token;
6441          redo B;          next B;
6442        } elsif ({        } elsif ({
6443                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6444                  div => 1, dl => 1, fieldset => 1,                  div => 1, dl => 1, fieldset => 1,
6445                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6446                  menu => 1, ol => 1, p => 1, ul => 1,                  menu => 1, ol => 1, p => 1, ul => 1,
6447                  pre => 1, listing => 1,                  pre => 1, listing => 1,
6448                    form => 1,
6449                    table => 1,
6450                    hr => 1,
6451                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6452            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
6453              !!!cp ('t350');
6454              !!!parse-error (type => 'in form:form', token => $token);
6455              ## Ignore the token
6456              !!!nack ('t350.1');
6457              !!!next-token;
6458              next B;
6459            }
6460    
6461          ## has a p element in scope          ## has a p element in scope
6462          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6463            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6464              !!!cp ('t344');              !!!cp ('t344');
6465              !!!back-token;              !!!back-token; # <form>
6466              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6467              redo B;                        line => $token->{line}, column => $token->{column}};
6468            } elsif ({              next B;
6469                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6470              !!!cp ('t345');              !!!cp ('t345');
6471              last INSCOPE;              last INSCOPE;
6472            }            }
6473          } # INSCOPE          } # INSCOPE
6474                        
6475          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6476          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6477              !!!nack ('t346.1');
6478            !!!next-token;            !!!next-token;
6479            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
6480              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
# Line 5470  sub _tree_construction_main ($) { Line 6487  sub _tree_construction_main ($) {
6487            } else {            } else {
6488              !!!cp ('t348');              !!!cp ('t348');
6489            }            }
6490          } else {          } elsif ($token->{tag_name} eq 'form') {
6491            !!!cp ('t347');            !!!cp ('t347.1');
6492              $self->{form_element} = $self->{open_elements}->[-1]->[0];
6493    
6494              !!!nack ('t347.2');
6495            !!!next-token;            !!!next-token;
6496          }          } elsif ($token->{tag_name} eq 'table') {
6497          redo B;            !!!cp ('t382');
6498        } elsif ($token->{tag_name} eq 'form') {            push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
6499          if (defined $self->{form_element}) {            
6500            !!!cp ('t350');            $self->{insertion_mode} = IN_TABLE_IM;
6501            !!!parse-error (type => 'in form:form');  
6502            ## Ignore the token            !!!nack ('t382.1');
6503              !!!next-token;
6504            } elsif ($token->{tag_name} eq 'hr') {
6505              !!!cp ('t386');
6506              pop @{$self->{open_elements}};
6507            
6508              !!!nack ('t386.1');
6509            !!!next-token;            !!!next-token;
           redo B;  
6510          } else {          } else {
6511            ## 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 ({  
                       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];  
6512            !!!next-token;            !!!next-token;
           redo B;  
6513          }          }
6514        } elsif ($token->{tag_name} eq 'li') {          next B;
6515          } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
6516          ## has a p element in scope          ## has a p element in scope
6517          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6518            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6519              !!!cp ('t353');              !!!cp ('t353');
6520              !!!back-token;              !!!back-token; # <x>
6521              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6522              redo B;                        line => $token->{line}, column => $token->{column}};
6523            } elsif ({              next B;
6524                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6525              !!!cp ('t354');              !!!cp ('t354');
6526              last INSCOPE;              last INSCOPE;
6527            }            }
# Line 5524  sub _tree_construction_main ($) { Line 6530  sub _tree_construction_main ($) {
6530          ## Step 1          ## Step 1
6531          my $i = -1;          my $i = -1;
6532          my $node = $self->{open_elements}->[$i];          my $node = $self->{open_elements}->[$i];
6533            my $li_or_dtdd = {li => {li => 1},
6534                              dt => {dt => 1, dd => 1},
6535                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6536          LI: {          LI: {
6537            ## Step 2            ## Step 2
6538            if ($node->[1] eq 'li') {            if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6539              if ($i != -1) {              if ($i != -1) {
6540                !!!cp ('t355');                !!!cp ('t355');
6541                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'not closed',
6542                                $self->{open_elements}->[-1]->[1]);                                value => $self->{open_elements}->[-1]->[0]
6543                                      ->manakai_local_name,
6544                                  token => $token);
6545              } else {              } else {
6546                !!!cp ('t356');                !!!cp ('t356');
6547              }              }
# Line 5541  sub _tree_construction_main ($) { Line 6552  sub _tree_construction_main ($) {
6552            }            }
6553                        
6554            ## Step 3            ## Step 3
6555            if (not $formatting_category->{$node->[1]} and            if (not ($node->[1] & FORMATTING_EL) and
6556                #not $phrasing_category->{$node->[1]} and                #not $phrasing_category->{$node->[1]} and
6557                ($special_category->{$node->[1]} or                ($node->[1] & SPECIAL_EL or
6558                 $scoping_category->{$node->[1]}) and                 $node->[1] & SCOPING_EL) and
6559                $node->[1] ne 'address' and $node->[1] ne 'div') {                not ($node->[1] & ADDRESS_EL) and
6560                  not ($node->[1] & DIV_EL)) {
6561              !!!cp ('t358');              !!!cp ('t358');
6562              last LI;              last LI;
6563            }            }
# Line 5557  sub _tree_construction_main ($) { Line 6569  sub _tree_construction_main ($) {
6569            redo LI;            redo LI;
6570          } # LI          } # LI
6571                        
6572          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6573            !!!nack ('t359.1');
6574          !!!next-token;          !!!next-token;
6575          redo B;          next 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 ({  
                     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});  
         !!!next-token;  
         redo B;  
6576        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
6577          ## has a p element in scope          ## has a p element in scope
6578          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6579            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6580              !!!cp ('t367');              !!!cp ('t367');
6581              !!!back-token;              !!!back-token; # <plaintext>
6582              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6583              redo B;                        line => $token->{line}, column => $token->{column}};
6584            } elsif ({              next B;
6585                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6586              !!!cp ('t368');              !!!cp ('t368');
6587              last INSCOPE;              last INSCOPE;
6588            }            }
6589          } # INSCOPE          } # INSCOPE
6590                        
6591          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6592                        
6593          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
6594                        
6595            !!!nack ('t368.1');
6596          !!!next-token;          !!!next-token;
6597          redo B;          next B;
6598        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
6599          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
6600            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
6601            if ($node->[1] eq 'a') {            if ($node->[1] & A_EL) {
6602              !!!cp ('t371');              !!!cp ('t371');
6603              !!!parse-error (type => 'in a:a');              !!!parse-error (type => 'in a:a', token => $token);
6604                            
6605              !!!back-token;              !!!back-token; # <a>
6606              $token = {type => END_TAG_TOKEN, tag_name => 'a'};              $token = {type => END_TAG_TOKEN, tag_name => 'a',
6607              $formatting_end_tag->($token->{tag_name});                        line => $token->{line}, column => $token->{column}};
6608                $formatting_end_tag->($token);
6609                            
6610              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
6611                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
# Line 5673  sub _tree_construction_main ($) { Line 6630  sub _tree_construction_main ($) {
6630                        
6631          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6632    
6633          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6634          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6635    
6636            !!!nack ('t374.1');
6637          !!!next-token;          !!!next-token;
6638          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;  
6639        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
6640          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6641    
6642          ## has a |nobr| element in scope          ## has a |nobr| element in scope
6643          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6644            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6645            if ($node->[1] eq 'nobr') {            if ($node->[1] & NOBR_EL) {
6646              !!!cp ('t376');              !!!cp ('t376');
6647              !!!parse-error (type => 'in nobr:nobr');              !!!parse-error (type => 'in nobr:nobr', token => $token);
6648              !!!back-token;              !!!back-token; # <nobr>
6649              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
6650              redo B;                        line => $token->{line}, column => $token->{column}};
6651            } elsif ({              next B;
6652                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($node->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6653              !!!cp ('t377');              !!!cp ('t377');
6654              last INSCOPE;              last INSCOPE;
6655            }            }
6656          } # INSCOPE          } # INSCOPE
6657                    
6658          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6659          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6660                    
6661            !!!nack ('t377.1');
6662          !!!next-token;          !!!next-token;
6663          redo B;          next B;
6664        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
6665          ## has a button element in scope          ## has a button element in scope
6666          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6667            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6668            if ($node->[1] eq 'button') {            if ($node->[1] & BUTTON_EL) {
6669              !!!cp ('t378');              !!!cp ('t378');
6670              !!!parse-error (type => 'in button:button');              !!!parse-error (type => 'in button:button', token => $token);
6671              !!!back-token;              !!!back-token; # <button>
6672              $token = {type => END_TAG_TOKEN, tag_name => 'button'};              $token = {type => END_TAG_TOKEN, tag_name => 'button',
6673              redo B;                        line => $token->{line}, column => $token->{column}};
6674            } elsif ({              next B;
6675                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($node->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6676              !!!cp ('t379');              !!!cp ('t379');
6677              last INSCOPE;              last INSCOPE;
6678            }            }
# Line 5738  sub _tree_construction_main ($) { Line 6680  sub _tree_construction_main ($) {
6680                        
6681          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6682                        
6683          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6684    
6685          ## TODO: associate with $self->{form_element} if defined          ## TODO: associate with $self->{form_element} if defined
6686    
6687          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
6688    
6689            !!!nack ('t379.1');
6690          !!!next-token;          !!!next-token;
6691          redo B;          next B;
       } elsif ($token->{tag_name} eq 'marquee' or  
                $token->{tag_name} eq 'object') {  
         !!!cp ('t380');  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!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 ({  
                     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;  
6692        } elsif ({        } elsif ({
6693                  area => 1, basefont => 1, bgsound => 1, br => 1,                  xmp => 1,
6694                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                  iframe => 1,
6695                  image => 1,                  noembed => 1,
6696                    noframes => 1, ## NOTE: This is an "as if in head" code clone.
6697                    noscript => 0, ## TODO: 1 if scripting is enabled
6698                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6699          if ($token->{tag_name} eq 'image') {          if ($token->{tag_name} eq 'xmp') {
6700            !!!cp ('t384');            !!!cp ('t381');
6701            !!!parse-error (type => 'image');            $reconstruct_active_formatting_elements->($insert_to_current);
           $token->{tag_name} = 'img';  
6702          } else {          } else {
6703            !!!cp ('t385');            !!!cp ('t399');
6704          }          }
6705            ## NOTE: There is an "as if in body" code clone.
6706          ## NOTE: There is an "as if <br>" code clone.          $parse_rcdata->(CDATA_CONTENT_MODEL);
6707          $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 ({  
                     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;  
6708        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
6709          !!!parse-error (type => 'isindex');          !!!parse-error (type => 'isindex', token => $token);
6710                    
6711          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
6712            !!!cp ('t389');            !!!cp ('t389');
6713            ## Ignore the token            ## Ignore the token
6714              !!!nack ('t389'); ## NOTE: Not acknowledged.
6715            !!!next-token;            !!!next-token;
6716            redo B;            next B;
6717          } else {          } else {
6718              !!!ack ('t391.1');
6719    
6720            my $at = $token->{attributes};            my $at = $token->{attributes};
6721            my $form_attrs;            my $form_attrs;
6722            $form_attrs->{action} = $at->{action} if $at->{action};            $form_attrs->{action} = $at->{action} if $at->{action};
# Line 5856  sub _tree_construction_main ($) { Line 6726  sub _tree_construction_main ($) {
6726            delete $at->{prompt};            delete $at->{prompt};
6727            my @tokens = (            my @tokens = (
6728                          {type => START_TAG_TOKEN, tag_name => 'form',                          {type => START_TAG_TOKEN, tag_name => 'form',
6729                           attributes => $form_attrs},                           attributes => $form_attrs,
6730                          {type => START_TAG_TOKEN, tag_name => 'hr'},                           line => $token->{line}, column => $token->{column}},
6731                          {type => START_TAG_TOKEN, tag_name => 'p'},                          {type => START_TAG_TOKEN, tag_name => 'hr',
6732                          {type => START_TAG_TOKEN, tag_name => 'label'},                           line => $token->{line}, column => $token->{column}},
6733                            {type => START_TAG_TOKEN, tag_name => 'p',
6734                             line => $token->{line}, column => $token->{column}},
6735                            {type => START_TAG_TOKEN, tag_name => 'label',
6736                             line => $token->{line}, column => $token->{column}},
6737                         );                         );
6738            if ($prompt_attr) {            if ($prompt_attr) {
6739              !!!cp ('t390');              !!!cp ('t390');
6740              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
6741                               #line => $token->{line}, column => $token->{column},
6742                              };
6743            } else {            } else {
6744              !!!cp ('t391');              !!!cp ('t391');
6745              push @tokens, {type => CHARACTER_TOKEN,              push @tokens, {type => CHARACTER_TOKEN,
6746                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD                             data => 'This is a searchable index. Insert your search keywords here: ',
6747                               #line => $token->{line}, column => $token->{column},
6748                              }; # SHOULD
6749              ## TODO: make this configurable              ## TODO: make this configurable
6750            }            }
6751            push @tokens,            push @tokens,
6752                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
6753                             line => $token->{line}, column => $token->{column}},
6754                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
6755                          {type => END_TAG_TOKEN, tag_name => 'label'},                          {type => END_TAG_TOKEN, tag_name => 'label',
6756                          {type => END_TAG_TOKEN, tag_name => 'p'},                           line => $token->{line}, column => $token->{column}},
6757                          {type => START_TAG_TOKEN, tag_name => 'hr'},                          {type => END_TAG_TOKEN, tag_name => 'p',
6758                          {type => END_TAG_TOKEN, tag_name => 'form'};                           line => $token->{line}, column => $token->{column}},
6759            $token = shift @tokens;                          {type => START_TAG_TOKEN, tag_name => 'hr',
6760                             line => $token->{line}, column => $token->{column}},
6761                            {type => END_TAG_TOKEN, tag_name => 'form',
6762                             line => $token->{line}, column => $token->{column}};
6763            !!!back-token (@tokens);            !!!back-token (@tokens);
6764            redo B;            !!!next-token;
6765              next B;
6766          }          }
6767        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
6768          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
6769          my $el;          my $el;
6770          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
6771                    
6772          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
6773          $self->{content_model} = RCDATA_CONTENT_MODEL;          $self->{content_model} = RCDATA_CONTENT_MODEL;
# Line 5893  sub _tree_construction_main ($) { Line 6776  sub _tree_construction_main ($) {
6776          $insert->($el);          $insert->($el);
6777                    
6778          my $text = '';          my $text = '';
6779            !!!nack ('t392.1');
6780          !!!next-token;          !!!next-token;
6781          if ($token->{type} == CHARACTER_TOKEN) {          if ($token->{type} == CHARACTER_TOKEN) {
6782            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
# Line 5923  sub _tree_construction_main ($) { Line 6807  sub _tree_construction_main ($) {
6807            ## Ignore the token            ## Ignore the token
6808          } else {          } else {
6809            !!!cp ('t398');            !!!cp ('t398');
6810            !!!parse-error (type => 'in RCDATA:#'.$token->{type});            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
6811          }          }
6812          !!!next-token;          !!!next-token;
6813            next B;
6814          } elsif ($token->{tag_name} eq 'rt' or
6815                   $token->{tag_name} eq 'rp') {
6816            ## has a |ruby| element in scope
6817            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6818              my $node = $self->{open_elements}->[$_];
6819              if ($node->[1] & RUBY_EL) {
6820                !!!cp ('t398.1');
6821                ## generate implied end tags
6822                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
6823                  !!!cp ('t398.2');
6824                  pop @{$self->{open_elements}};
6825                }
6826                unless ($self->{open_elements}->[-1]->[1] & RUBY_EL) {
6827                  !!!cp ('t398.3');
6828                  !!!parse-error (type => 'not closed',
6829                                  value => $self->{open_elements}->[-1]->[0]
6830                                      ->manakai_local_name,
6831                                  token => $token);
6832                  pop @{$self->{open_elements}}
6833                      while not $self->{open_elements}->[-1]->[1] & RUBY_EL;
6834                }
6835                last INSCOPE;
6836              } elsif ($node->[1] & SCOPING_EL) {
6837                !!!cp ('t398.4');
6838                last INSCOPE;
6839              }
6840            } # INSCOPE
6841    
6842            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6843    
6844            !!!nack ('t398.5');
6845            !!!next-token;
6846          redo B;          redo B;
6847        } elsif ({        } elsif ($token->{tag_name} eq 'math' or
6848                  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');  
6849          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
6850    
6851          ## TODO: associate with $self->{form_element} if defined          ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
6852    
6853            ## "adjust foreign attributes" - done in insert-element-f
6854                    
6855          $self->{insertion_mode} = IN_SELECT_IM;          !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
6856            
6857            if ($self->{self_closing}) {
6858              pop @{$self->{open_elements}};
6859              !!!ack ('t398.1');
6860            } else {
6861              !!!cp ('t398.2');
6862              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
6863              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
6864              ## mode, "in body" (not "in foreign content") secondary insertion
6865              ## mode, maybe.
6866            }
6867    
6868          !!!next-token;          !!!next-token;
6869          redo B;          next B;
6870        } elsif ({        } elsif ({
6871                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
6872                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 5955  sub _tree_construction_main ($) { Line 6874  sub _tree_construction_main ($) {
6874                  thead => 1, tr => 1,                  thead => 1, tr => 1,
6875                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6876          !!!cp ('t401');          !!!cp ('t401');
6877          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);
6878          ## Ignore the token          ## Ignore the token
6879            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
6880          !!!next-token;          !!!next-token;
6881          redo B;          next B;
6882                    
6883          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
6884        } else {        } else {
6885          !!!cp ('t402');          if ($token->{tag_name} eq 'image') {
6886              !!!cp ('t384');
6887              !!!parse-error (type => 'image', token => $token);
6888              $token->{tag_name} = 'img';
6889            } else {
6890              !!!cp ('t385');
6891            }
6892    
6893            ## NOTE: There is an "as if <br>" code clone.
6894          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6895                    
6896          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6897    
6898            if ({
6899                 applet => 1, marquee => 1, object => 1,
6900                }->{$token->{tag_name}}) {
6901              !!!cp ('t380');
6902              push @$active_formatting_elements, ['#marker', ''];
6903              !!!nack ('t380.1');
6904            } elsif ({
6905                      b => 1, big => 1, em => 1, font => 1, i => 1,
6906                      s => 1, small => 1, strile => 1,
6907                      strong => 1, tt => 1, u => 1,
6908                     }->{$token->{tag_name}}) {
6909              !!!cp ('t375');
6910              push @$active_formatting_elements, $self->{open_elements}->[-1];
6911              !!!nack ('t375.1');
6912            } elsif ($token->{tag_name} eq 'input') {
6913              !!!cp ('t388');
6914              ## TODO: associate with $self->{form_element} if defined
6915              pop @{$self->{open_elements}};
6916              !!!ack ('t388.2');
6917            } elsif ({
6918                      area => 1, basefont => 1, bgsound => 1, br => 1,
6919                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
6920                      #image => 1,
6921                     }->{$token->{tag_name}}) {
6922              !!!cp ('t388.1');
6923              pop @{$self->{open_elements}};
6924              !!!ack ('t388.3');
6925            } elsif ($token->{tag_name} eq 'select') {
6926              ## TODO: associate with $self->{form_element} if defined
6927            
6928              if ($self->{insertion_mode} & TABLE_IMS or
6929                  $self->{insertion_mode} & BODY_TABLE_IMS or
6930                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
6931                !!!cp ('t400.1');
6932                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
6933              } else {
6934                !!!cp ('t400.2');
6935                $self->{insertion_mode} = IN_SELECT_IM;
6936              }
6937              !!!nack ('t400.3');
6938            } else {
6939              !!!nack ('t402');
6940            }
6941                    
6942          !!!next-token;          !!!next-token;
6943          redo B;          next B;
6944        }        }
6945      } elsif ($token->{type} == END_TAG_TOKEN) {      } elsif ($token->{type} == END_TAG_TOKEN) {
6946        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
6947          if (@{$self->{open_elements}} > 1 and          ## has a |body| element in scope
6948              $self->{open_elements}->[1]->[1] eq 'body') {          my $i;
6949            for (@{$self->{open_elements}}) {          INSCOPE: {
6950              unless ({            for (reverse @{$self->{open_elements}}) {
6951                         dd => 1, dt => 1, li => 1, p => 1, td => 1,              if ($_->[1] & BODY_EL) {
6952                         th => 1, tr => 1, body => 1, html => 1,                !!!cp ('t405');
6953                       tbody => 1, tfoot => 1, thead => 1,                $i = $_;
6954                      }->{$_->[1]}) {                last INSCOPE;
6955                !!!cp ('t403');              } elsif ($_->[1] & SCOPING_EL) {
6956                !!!parse-error (type => 'not closed:'.$_->[1]);                !!!cp ('t405.1');
6957              } else {                last;
               !!!cp ('t404');  
6958              }              }
6959            }            }
6960    
6961            $self->{insertion_mode} = AFTER_BODY_IM;            !!!parse-error (type => 'start tag not allowed',
6962            !!!next-token;                            value => $token->{tag_name}, token => $token);
6963            redo B;            ## NOTE: Ignore the token.
         } else {  
           !!!cp ('t405');  
           !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           ## Ignore the token  
6964            !!!next-token;            !!!next-token;
6965            redo B;            next B;
6966            } # INSCOPE
6967    
6968            for (@{$self->{open_elements}}) {
6969              unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
6970                !!!cp ('t403');
6971                !!!parse-error (type => 'not closed',
6972                                value => $_->[0]->manakai_local_name,
6973                                token => $token);
6974                last;
6975              } else {
6976                !!!cp ('t404');
6977              }
6978          }          }
6979    
6980            $self->{insertion_mode} = AFTER_BODY_IM;
6981            !!!next-token;
6982            next B;
6983        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
6984          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
6985            ## up-to-date, though it has same effect as speced.
6986            if (@{$self->{open_elements}} > 1 and
6987                $self->{open_elements}->[1]->[1] & BODY_EL) {
6988            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
6989            if ($self->{open_elements}->[-1]->[1] ne 'body') {            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
6990              !!!cp ('t406');              !!!cp ('t406');
6991              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!parse-error (type => 'not closed',
6992                                value => $self->{open_elements}->[1]->[0]
6993                                    ->manakai_local_name,
6994                                token => $token);
6995            } else {            } else {
6996              !!!cp ('t407');              !!!cp ('t407');
6997            }            }
6998            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6999            ## reprocess            ## reprocess
7000            redo B;            next B;
7001          } else {          } else {
7002            !!!cp ('t408');            !!!cp ('t408');
7003            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7004            ## Ignore the token            ## Ignore the token
7005            !!!next-token;            !!!next-token;
7006            redo B;            next B;
7007          }          }
7008        } elsif ({        } elsif ({
7009                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
7010                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1, listing => 1,
7011                  menu => 1, ol => 1, pre => 1, ul => 1,                  menu => 1, ol => 1, pre => 1, ul => 1,
7012                  dd => 1, dt => 1, li => 1,                  dd => 1, dt => 1, li => 1,
7013                  button => 1, marquee => 1, object => 1,                  applet => 1, button => 1, marquee => 1, object => 1,
7014                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7015          ## has an element in scope          ## has an element in scope
7016          my $i;          my $i;
7017          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7018            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7019            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7020              !!!cp ('t410');              !!!cp ('t410');
7021              $i = $_;              $i = $_;
7022              last INSCOPE;              last INSCOPE;
7023            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7024              !!!cp ('t411');              !!!cp ('t411');
7025              last INSCOPE;              last INSCOPE;
7026            }            }
# Line 6042  sub _tree_construction_main ($) { Line 7028  sub _tree_construction_main ($) {
7028    
7029          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
7030            !!!cp ('t413');            !!!cp ('t413');
7031            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7032          } else {          } else {
7033            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7034            while ({            while ({
7035                      ## END_TAG_OPTIONAL_EL
7036                    dd => ($token->{tag_name} ne 'dd'),                    dd => ($token->{tag_name} ne 'dd'),
7037                    dt => ($token->{tag_name} ne 'dt'),                    dt => ($token->{tag_name} ne 'dt'),
7038                    li => ($token->{tag_name} ne 'li'),                    li => ($token->{tag_name} ne 'li'),
7039                    p => 1,                    p => 1,
7040                   }->{$self->{open_elements}->[-1]->[1]}) {                    rt => 1,
7041                      rp => 1,
7042                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
7043              !!!cp ('t409');              !!!cp ('t409');
7044              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
7045            }            }
7046    
7047            ## Step 2.            ## Step 2.
7048            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7049                      ne $token->{tag_name}) {
7050              !!!cp ('t412');              !!!cp ('t412');
7051              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              !!!parse-error (type => 'not closed',
7052                                value => $self->{open_elements}->[-1]->[0]
7053                                    ->manakai_local_name,
7054                                token => $token);
7055            } else {            } else {
7056              !!!cp ('t414');              !!!cp ('t414');
7057            }            }
# Line 6069  sub _tree_construction_main ($) { Line 7062  sub _tree_construction_main ($) {
7062            ## Step 4.            ## Step 4.
7063            $clear_up_to_marker->()            $clear_up_to_marker->()
7064                if {                if {
7065                  button => 1, marquee => 1, object => 1,                  applet => 1, button => 1, marquee => 1, object => 1,
7066                }->{$token->{tag_name}};                }->{$token->{tag_name}};
7067          }          }
7068          !!!next-token;          !!!next-token;
7069          redo B;          next B;
7070        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
7071          undef $self->{form_element};          undef $self->{form_element};
7072    
# Line 6081  sub _tree_construction_main ($) { Line 7074  sub _tree_construction_main ($) {
7074          my $i;          my $i;
7075          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7076            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7077            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & FORM_EL) {
7078              !!!cp ('t418');              !!!cp ('t418');
7079              $i = $_;              $i = $_;
7080              last INSCOPE;              last INSCOPE;
7081            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7082              !!!cp ('t419');              !!!cp ('t419');
7083              last INSCOPE;              last INSCOPE;
7084            }            }
# Line 6096  sub _tree_construction_main ($) { Line 7086  sub _tree_construction_main ($) {
7086    
7087          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
7088            !!!cp ('t421');            !!!cp ('t421');
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            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7092            while ({            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                   dd => 1, dt => 1, li => 1, p => 1,  
                  }->{$self->{open_elements}->[-1]->[1]}) {  
7093              !!!cp ('t417');              !!!cp ('t417');
7094              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
7095            }            }
7096                        
7097            ## Step 2.            ## Step 2.
7098            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7099                      ne $token->{tag_name}) {
7100              !!!cp ('t417.1');              !!!cp ('t417.1');
7101              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              !!!parse-error (type => 'not closed',
7102                                value => $self->{open_elements}->[-1]->[0]
7103                                    ->manakai_local_name,
7104                                token => $token);
7105            } else {            } else {
7106              !!!cp ('t420');              !!!cp ('t420');
7107            }              }  
# Line 6119  sub _tree_construction_main ($) { Line 7111  sub _tree_construction_main ($) {
7111          }          }
7112    
7113          !!!next-token;          !!!next-token;
7114          redo B;          next B;
7115        } elsif ({        } elsif ({
7116                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
7117                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 6127  sub _tree_construction_main ($) { Line 7119  sub _tree_construction_main ($) {
7119          my $i;          my $i;
7120          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7121            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7122            if ({            if ($node->[1] & HEADING_EL) {
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
7123              !!!cp ('t423');              !!!cp ('t423');
7124              $i = $_;              $i = $_;
7125              last INSCOPE;              last INSCOPE;
7126            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7127              !!!cp ('t424');              !!!cp ('t424');
7128              last INSCOPE;              last INSCOPE;
7129            }            }
# Line 6144  sub _tree_construction_main ($) { Line 7131  sub _tree_construction_main ($) {
7131    
7132          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
7133            !!!cp ('t425.1');            !!!cp ('t425.1');
7134            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7135          } else {          } else {
7136            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7137            while ({            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                   dd => 1, dt => 1, li => 1, p => 1,  
                  }->{$self->{open_elements}->[-1]->[1]}) {  
7138              !!!cp ('t422');              !!!cp ('t422');
7139              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
7140            }            }
7141                        
7142            ## Step 2.            ## Step 2.
7143            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7144                      ne $token->{tag_name}) {
7145              !!!cp ('t425');              !!!cp ('t425');
7146              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7147            } else {            } else {
7148              !!!cp ('t426');              !!!cp ('t426');
7149            }            }
# Line 6167  sub _tree_construction_main ($) { Line 7153  sub _tree_construction_main ($) {
7153          }          }
7154                    
7155          !!!next-token;          !!!next-token;
7156          redo B;          next B;
7157        } elsif ($token->{tag_name} eq 'p') {        } elsif ($token->{tag_name} eq 'p') {
7158          ## has an element in scope          ## has an element in scope
7159          my $i;          my $i;
7160          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7161            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7162            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & P_EL) {
7163              !!!cp ('t410.1');              !!!cp ('t410.1');
7164              $i = $_;              $i = $_;
7165              last INSCOPE;              last INSCOPE;
7166            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7167              !!!cp ('t411.1');              !!!cp ('t411.1');
7168              last INSCOPE;              last INSCOPE;
7169            }            }
7170          } # INSCOPE          } # INSCOPE
7171    
7172          if (defined $i) {          if (defined $i) {
7173            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7174                      ne $token->{tag_name}) {
7175              !!!cp ('t412.1');              !!!cp ('t412.1');
7176              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              !!!parse-error (type => 'not closed',
7177                                value => $self->{open_elements}->[-1]->[0]
7178                                    ->manakai_local_name,
7179                                token => $token);
7180            } else {            } else {
7181              !!!cp ('t414.1');              !!!cp ('t414.1');
7182            }            }
# Line 6197  sub _tree_construction_main ($) { Line 7184  sub _tree_construction_main ($) {
7184            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
7185          } else {          } else {
7186            !!!cp ('t413.1');            !!!cp ('t413.1');
7187            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7188    
7189            !!!cp ('t415.1');            !!!cp ('t415.1');
7190            ## As if <p>, then reprocess the current token            ## As if <p>, then reprocess the current token
7191            my $el;            my $el;
7192            !!!create-element ($el, 'p');            !!!create-element ($el, $HTML_NS, 'p',, $token);
7193            $insert->($el);            $insert->($el);
7194            ## NOTE: Not inserted into |$self->{open_elements}|.            ## NOTE: Not inserted into |$self->{open_elements}|.
7195          }          }
7196    
7197          !!!next-token;          !!!next-token;
7198          redo B;          next B;
7199        } elsif ({        } elsif ({
7200                  a => 1,                  a => 1,
7201                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
# Line 6216  sub _tree_construction_main ($) { Line 7203  sub _tree_construction_main ($) {
7203                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
7204                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7205          !!!cp ('t427');          !!!cp ('t427');
7206          $formatting_end_tag->($token->{tag_name});          $formatting_end_tag->($token);
7207          redo B;          next B;
7208        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
7209          !!!cp ('t428');          !!!cp ('t428');
7210          !!!parse-error (type => 'unmatched end tag:br');          !!!parse-error (type => 'unmatched end tag:br', token => $token);
7211    
7212          ## As if <br>          ## As if <br>
7213          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7214                    
7215          my $el;          my $el;
7216          !!!create-element ($el, 'br');          !!!create-element ($el, $HTML_NS, 'br',, $token);
7217          $insert->($el);          $insert->($el);
7218                    
7219          ## Ignore the token.          ## Ignore the token.
7220          !!!next-token;          !!!next-token;
7221          redo B;          next B;
7222        } elsif ({        } elsif ({
7223                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7224                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 6245  sub _tree_construction_main ($) { Line 7232  sub _tree_construction_main ($) {
7232                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
7233                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7234          !!!cp ('t429');          !!!cp ('t429');
7235          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7236          ## Ignore the token          ## Ignore the token
7237          !!!next-token;          !!!next-token;
7238          redo B;          next B;
7239                    
7240          ## ISSUE: Issue on HTML5 new elements in spec          ## ISSUE: Issue on HTML5 new elements in spec
7241                    
# Line 6259  sub _tree_construction_main ($) { Line 7246  sub _tree_construction_main ($) {
7246    
7247          ## Step 2          ## Step 2
7248          S2: {          S2: {
7249            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7250              ## Step 1              ## Step 1
7251              ## generate implied end tags              ## generate implied end tags
7252              while ({              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                     dd => 1, dt => 1, li => 1, p => 1,  
                    }->{$self->{open_elements}->[-1]->[1]}) {  
7253                !!!cp ('t430');                !!!cp ('t430');
7254                ## ISSUE: Can this case be reached?                ## NOTE: |<ruby><rt></ruby>|.
7255                  ## ISSUE: <ruby><rt></rt> will also take this code path,
7256                  ## which seems wrong.
7257                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
7258                  $node_i++;
7259              }              }
7260                    
7261              ## Step 2              ## Step 2
7262              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7263                        ne $token->{tag_name}) {
7264                !!!cp ('t431');                !!!cp ('t431');
7265                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
7266                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
7267                                  value => $self->{open_elements}->[-1]->[0]
7268                                      ->manakai_local_name,
7269                                  token => $token);
7270              } else {              } else {
7271                !!!cp ('t432');                !!!cp ('t432');
7272              }              }
7273                            
7274              ## Step 3              ## Step 3
7275              splice @{$self->{open_elements}}, $node_i;              splice @{$self->{open_elements}}, $node_i if $node_i < 0;
7276    
7277              !!!next-token;              !!!next-token;
7278              last S2;              last S2;
7279            } else {            } else {
7280              ## Step 3              ## Step 3
7281              if (not $formatting_category->{$node->[1]} and              if (not ($node->[1] & FORMATTING_EL) and
7282                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
7283                  ($special_category->{$node->[1]} or                  ($node->[1] & SPECIAL_EL or
7284                   $scoping_category->{$node->[1]})) {                   $node->[1] & SCOPING_EL)) {
7285                !!!cp ('t433');                !!!cp ('t433');
7286                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7287                ## Ignore the token                ## Ignore the token
7288                !!!next-token;                !!!next-token;
7289                last S2;                last S2;
# Line 6307  sub _tree_construction_main ($) { Line 7299  sub _tree_construction_main ($) {
7299            ## Step 5;            ## Step 5;
7300            redo S2;            redo S2;
7301          } # S2          } # S2
7302          redo B;          next B;
7303        }        }
7304      }      }
7305      redo B;      next B;
7306      } continue { # B
7307        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7308          ## NOTE: The code below is executed in cases where it does not have
7309          ## to be, but it it is harmless even in those cases.
7310          ## has an element in scope
7311          INSCOPE: {
7312            for (reverse 0..$#{$self->{open_elements}}) {
7313              my $node = $self->{open_elements}->[$_];
7314              if ($node->[1] & FOREIGN_EL) {
7315                last INSCOPE;
7316              } elsif ($node->[1] & SCOPING_EL) {
7317                last;
7318              }
7319            }
7320            
7321            ## NOTE: No foreign element in scope.
7322            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7323          } # INSCOPE
7324        }
7325    } # B    } # B
7326    
7327    ## Stop parsing # MUST    ## Stop parsing # MUST
# Line 6356  sub set_inner_html ($$$) { Line 7367  sub set_inner_html ($$$) {
7367    
7368      ## Step 8 # MUST      ## Step 8 # MUST
7369      my $i = 0;      my $i = 0;
7370      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7371      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7372      $p->{set_next_char} = sub {      $p->{set_next_char} = sub {
7373        my $self = shift;        my $self = shift;
7374    
# Line 6366  sub set_inner_html ($$$) { Line 7377  sub set_inner_html ($$$) {
7377    
7378        $self->{next_char} = -1 and return if $i >= length $$s;        $self->{next_char} = -1 and return if $i >= length $$s;
7379        $self->{next_char} = ord substr $$s, $i++, 1;        $self->{next_char} = ord substr $$s, $i++, 1;
7380        $column++;  
7381          ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7382          $p->{column}++;
7383    
7384        if ($self->{next_char} == 0x000A) { # LF        if ($self->{next_char} == 0x000A) { # LF
7385          $line++;          $p->{line}++;
7386          $column = 0;          $p->{column} = 0;
7387          !!!cp ('i1');          !!!cp ('i1');
7388        } elsif ($self->{next_char} == 0x000D) { # CR        } elsif ($self->{next_char} == 0x000D) { # CR
7389          $i++ if substr ($$s, $i, 1) eq "\x0A";          $i++ if substr ($$s, $i, 1) eq "\x0A";
7390          $self->{next_char} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
7391          $line++;          $p->{line}++;
7392          $column = 0;          $p->{column} = 0;
7393          !!!cp ('i2');          !!!cp ('i2');
7394        } elsif ($self->{next_char} > 0x10FFFF) {        } elsif ($self->{next_char} > 0x10FFFF) {
7395          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
# Line 6385  sub set_inner_html ($$$) { Line 7398  sub set_inner_html ($$$) {
7398          !!!cp ('i4');          !!!cp ('i4');
7399          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
7400          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7401          } elsif ($self->{next_char} <= 0x0008 or
7402                   (0x000E <= $self->{next_char} and
7403                    $self->{next_char} <= 0x001F) or
7404                   (0x007F <= $self->{next_char} and
7405                    $self->{next_char} <= 0x009F) or
7406                   (0xD800 <= $self->{next_char} and
7407                    $self->{next_char} <= 0xDFFF) or
7408                   (0xFDD0 <= $self->{next_char} and
7409                    $self->{next_char} <= 0xFDDF) or
7410                   {
7411                    0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
7412                    0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
7413                    0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
7414                    0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
7415                    0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
7416                    0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
7417                    0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
7418                    0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
7419                    0x10FFFE => 1, 0x10FFFF => 1,
7420                   }->{$self->{next_char}}) {
7421            !!!cp ('i4.1');
7422            !!!parse-error (type => 'control char', level => $self->{must_level});
7423    ## TODO: error type documentation
7424        }        }
7425      };      };
7426      $p->{prev_char} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];
# Line 6392  sub set_inner_html ($$$) { Line 7428  sub set_inner_html ($$$) {
7428            
7429      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7430        my (%opt) = @_;        my (%opt) = @_;
7431        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7432          my $column = $opt{column};
7433          if (defined $opt{token} and defined $opt{token}->{line}) {
7434            $line = $opt{token}->{line};
7435            $column = $opt{token}->{column};
7436          }
7437          warn "Parse error ($opt{type}) at line $line column $column\n";
7438      };      };
7439      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7440        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7441      };      };
7442            
7443      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
# Line 6419  sub set_inner_html ($$$) { Line 7461  sub set_inner_html ($$$) {
7461          unless defined $p->{content_model};          unless defined $p->{content_model};
7462          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
7463    
7464      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7465          ## TODO: Foreign element OK?
7466    
7467      ## Step 3      ## Step 3
7468      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
# Line 6429  sub set_inner_html ($$$) { Line 7472  sub set_inner_html ($$$) {
7472      $doc->append_child ($root);      $doc->append_child ($root);
7473    
7474      ## Step 5 # MUST      ## Step 5 # MUST
7475      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7476    
7477      undef $p->{head_element};      undef $p->{head_element};
7478    
# Line 6475  sub set_inner_html ($$$) { Line 7518  sub set_inner_html ($$$) {
7518      ## ISSUE: mutation events?      ## ISSUE: mutation events?
7519    
7520      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
7521    
7522        delete $p->{parse_error}; # delete loop
7523    } else {    } else {
7524      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";
7525    }    }

Legend:
Removed from v.1.100  
changed lines
  Added in v.1.151

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24