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

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

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

revision 1.116 by wakaba, Mon Mar 17 13:23:39 2008 UTC revision 1.145 by wakaba, Sat May 24 11:57:47 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    
49    sub TABLE_ROWS_EL () {
50      TABLE_EL |
51      TABLE_ROW_EL |
52      TABLE_ROW_GROUP_EL
53    }
54    
55    sub END_TAG_OPTIONAL_EL () {
56      DD_EL |
57      DT_EL |
58      LI_EL |
59      P_EL
60    }
61    
62    sub ALL_END_TAG_OPTIONAL_EL () {
63      END_TAG_OPTIONAL_EL |
64      BODY_EL |
65      HTML_EL |
66      TABLE_CELL_EL |
67      TABLE_ROW_EL |
68      TABLE_ROW_GROUP_EL
69    }
70    
71    sub SCOPING_EL () {
72      BUTTON_EL |
73      CAPTION_EL |
74      HTML_EL |
75      TABLE_EL |
76      TABLE_CELL_EL |
77      MISC_SCOPING_EL
78    }
79    
80    sub TABLE_SCOPING_EL () {
81      HTML_EL |
82      TABLE_EL
83    }
84    
85    sub TABLE_ROWS_SCOPING_EL () {
86      HTML_EL |
87      TABLE_ROW_GROUP_EL
88    }
89    
90    sub TABLE_ROW_SCOPING_EL () {
91      HTML_EL |
92      TABLE_ROW_EL
93    }
94    
95    sub SPECIAL_EL () {
96      ADDRESS_EL |
97      BODY_EL |
98      DIV_EL |
99      END_TAG_OPTIONAL_EL |
100      FORM_EL |
101      FRAMESET_EL |
102      HEADING_EL |
103      OPTION_EL |
104      OPTGROUP_EL |
105      SELECT_EL |
106      TABLE_ROW_EL |
107      TABLE_ROW_GROUP_EL |
108      MISC_SPECIAL_EL
109    }
110    
111    my $el_category = {
112      a => A_EL | FORMATTING_EL,
113      address => ADDRESS_EL,
114      applet => MISC_SCOPING_EL,
115      area => MISC_SPECIAL_EL,
116      b => FORMATTING_EL,
117      base => MISC_SPECIAL_EL,
118      basefont => MISC_SPECIAL_EL,
119      bgsound => MISC_SPECIAL_EL,
120      big => FORMATTING_EL,
121      blockquote => MISC_SPECIAL_EL,
122      body => BODY_EL,
123      br => MISC_SPECIAL_EL,
124      button => BUTTON_EL,
125      caption => CAPTION_EL,
126      center => MISC_SPECIAL_EL,
127      col => MISC_SPECIAL_EL,
128      colgroup => MISC_SPECIAL_EL,
129      dd => DD_EL,
130      dir => MISC_SPECIAL_EL,
131      div => DIV_EL,
132      dl => MISC_SPECIAL_EL,
133      dt => DT_EL,
134      em => FORMATTING_EL,
135      embed => MISC_SPECIAL_EL,
136      fieldset => MISC_SPECIAL_EL,
137      font => FORMATTING_EL,
138      form => FORM_EL,
139      frame => MISC_SPECIAL_EL,
140      frameset => FRAMESET_EL,
141      h1 => HEADING_EL,
142      h2 => HEADING_EL,
143      h3 => HEADING_EL,
144      h4 => HEADING_EL,
145      h5 => HEADING_EL,
146      h6 => HEADING_EL,
147      head => MISC_SPECIAL_EL,
148      hr => MISC_SPECIAL_EL,
149      html => HTML_EL,
150      i => FORMATTING_EL,
151      iframe => MISC_SPECIAL_EL,
152      img => MISC_SPECIAL_EL,
153      input => MISC_SPECIAL_EL,
154      isindex => MISC_SPECIAL_EL,
155      li => LI_EL,
156      link => MISC_SPECIAL_EL,
157      listing => MISC_SPECIAL_EL,
158      marquee => MISC_SCOPING_EL,
159      menu => MISC_SPECIAL_EL,
160      meta => MISC_SPECIAL_EL,
161      nobr => NOBR_EL | FORMATTING_EL,
162      noembed => MISC_SPECIAL_EL,
163      noframes => MISC_SPECIAL_EL,
164      noscript => MISC_SPECIAL_EL,
165      object => MISC_SCOPING_EL,
166      ol => MISC_SPECIAL_EL,
167      optgroup => OPTGROUP_EL,
168      option => OPTION_EL,
169      p => P_EL,
170      param => MISC_SPECIAL_EL,
171      plaintext => MISC_SPECIAL_EL,
172      pre => MISC_SPECIAL_EL,
173      s => FORMATTING_EL,
174      script => MISC_SPECIAL_EL,
175      select => SELECT_EL,
176      small => FORMATTING_EL,
177      spacer => MISC_SPECIAL_EL,
178      strike => FORMATTING_EL,
179      strong => FORMATTING_EL,
180      style => MISC_SPECIAL_EL,
181      table => TABLE_EL,
182      tbody => TABLE_ROW_GROUP_EL,
183      td => TABLE_CELL_EL,
184      textarea => MISC_SPECIAL_EL,
185      tfoot => TABLE_ROW_GROUP_EL,
186      th => TABLE_CELL_EL,
187      thead => TABLE_ROW_GROUP_EL,
188      title => MISC_SPECIAL_EL,
189      tr => TABLE_ROW_EL,
190      tt => FORMATTING_EL,
191      u => FORMATTING_EL,
192      ul => MISC_SPECIAL_EL,
193      wbr => MISC_SPECIAL_EL,
194    };
195    
196    my $el_category_f = {
197      $MML_NS => {
198        'annotation-xml' => MML_AXML_EL,
199        mi => FOREIGN_FLOW_CONTENT_EL,
200        mo => FOREIGN_FLOW_CONTENT_EL,
201        mn => FOREIGN_FLOW_CONTENT_EL,
202        ms => FOREIGN_FLOW_CONTENT_EL,
203        mtext => FOREIGN_FLOW_CONTENT_EL,
204      },
205      $SVG_NS => {
206        foreignObject => FOREIGN_FLOW_CONTENT_EL,
207        desc => FOREIGN_FLOW_CONTENT_EL,
208        title => FOREIGN_FLOW_CONTENT_EL,
209      },
210      ## NOTE: In addition, FOREIGN_EL is set to non-HTML elements.
211    };
212    
213    my $svg_attr_name = {
214      attributetype => 'attributeType',
215      basefrequency => 'baseFrequency',
216      baseprofile => 'baseProfile',
217      calcmode => 'calcMode',
218      clippathunits => 'clipPathUnits',
219      contentscripttype => 'contentScriptType',
220      contentstyletype => 'contentStyleType',
221      diffuseconstant => 'diffuseConstant',
222      edgemode => 'edgeMode',
223      externalresourcesrequired => 'externalResourcesRequired',
224      fecolormatrix => 'feColorMatrix',
225      fecomposite => 'feComposite',
226      fegaussianblur => 'feGaussianBlur',
227      femorphology => 'feMorphology',
228      fetile => 'feTile',
229      filterres => 'filterRes',
230      filterunits => 'filterUnits',
231      glyphref => 'glyphRef',
232      gradienttransform => 'gradientTransform',
233      gradientunits => 'gradientUnits',
234      kernelmatrix => 'kernelMatrix',
235      kernelunitlength => 'kernelUnitLength',
236      keypoints => 'keyPoints',
237      keysplines => 'keySplines',
238      keytimes => 'keyTimes',
239      lengthadjust => 'lengthAdjust',
240      limitingconeangle => 'limitingConeAngle',
241      markerheight => 'markerHeight',
242      markerunits => 'markerUnits',
243      markerwidth => 'markerWidth',
244      maskcontentunits => 'maskContentUnits',
245      maskunits => 'maskUnits',
246      numoctaves => 'numOctaves',
247      pathlength => 'pathLength',
248      patterncontentunits => 'patternContentUnits',
249      patterntransform => 'patternTransform',
250      patternunits => 'patternUnits',
251      pointsatx => 'pointsAtX',
252      pointsaty => 'pointsAtY',
253      pointsatz => 'pointsAtZ',
254      preservealpha => 'preserveAlpha',
255      preserveaspectratio => 'preserveAspectRatio',
256      primitiveunits => 'primitiveUnits',
257      refx => 'refX',
258      refy => 'refY',
259      repeatcount => 'repeatCount',
260      repeatdur => 'repeatDur',
261      requiredextensions => 'requiredExtensions',
262      specularconstant => 'specularConstant',
263      specularexponent => 'specularExponent',
264      spreadmethod => 'spreadMethod',
265      startoffset => 'startOffset',
266      stddeviation => 'stdDeviation',
267      stitchtiles => 'stitchTiles',
268      surfacescale => 'surfaceScale',
269      systemlanguage => 'systemLanguage',
270      tablevalues => 'tableValues',
271      targetx => 'targetX',
272      targety => 'targetY',
273      textlength => 'textLength',
274      viewbox => 'viewBox',
275      viewtarget => 'viewTarget',
276      xchannelselector => 'xChannelSelector',
277      ychannelselector => 'yChannelSelector',
278      zoomandpan => 'zoomAndPan',
279  };  };
280    
281    my $foreign_attr_xname = {
282      'xlink:actuate' => [$XLINK_NS, ['xlink', 'actuate']],
283      'xlink:arcrole' => [$XLINK_NS, ['xlink', 'arcrole']],
284      'xlink:href' => [$XLINK_NS, ['xlink', 'href']],
285      'xlink:role' => [$XLINK_NS, ['xlink', 'role']],
286      'xlink:show' => [$XLINK_NS, ['xlink', 'show']],
287      'xlink:title' => [$XLINK_NS, ['xlink', 'title']],
288      'xlink:type' => [$XLINK_NS, ['xlink', 'type']],
289      'xml:base' => [$XML_NS, ['xml', 'base']],
290      'xml:lang' => [$XML_NS, ['xml', 'lang']],
291      'xml:space' => [$XML_NS, ['xml', 'space']],
292      'xmlns' => [$XMLNS_NS, [undef, 'xmlns']],
293      'xmlns:xlink' => [$XMLNS_NS, ['xmlns', 'xlink']],
294    };
295    
296    ## ISSUE: xmlns:xlink="non-xlink-ns" is not an error.
297    
298  my $c1_entity_char = {  my $c1_entity_char = {
299    0x80 => 0x20AC,    0x80 => 0x20AC,
300    0x81 => 0xFFFD,    0x81 => 0xFFFD,
# Line 61  my $c1_entity_char = { Line 330  my $c1_entity_char = {
330    0x9F => 0x0178,    0x9F => 0x0178,
331  }; # $c1_entity_char  }; # $c1_entity_char
332    
 my $special_category = {  
   address => 1, area => 1, base => 1, basefont => 1, bgsound => 1,  
   blockquote => 1, body => 1, br => 1, center => 1, col => 1, colgroup => 1,  
   dd => 1, dir => 1, div => 1, dl => 1, dt => 1, embed => 1, fieldset => 1,  
   form => 1, frame => 1, frameset => 1, h1 => 1, h2 => 1, h3 => 1,  
   h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, iframe => 1, image => 1,  
   img => 1, input => 1, isindex => 1, li => 1, link => 1, listing => 1,  
   menu => 1, meta => 1, noembed => 1, noframes => 1, noscript => 1,  
   ol => 1, optgroup => 1, option => 1, p => 1, param => 1, plaintext => 1,  
   pre => 1, script => 1, select => 1, spacer => 1, style => 1, tbody => 1,  
   textarea => 1, tfoot => 1, thead => 1, title => 1, tr => 1, ul => 1, wbr => 1,  
 };  
 my $scoping_category = {  
   applet => 1, button => 1, caption => 1, html => 1, marquee => 1, object => 1,  
   table => 1, td => 1, th => 1,  
 };  
 my $formatting_category = {  
   a => 1, b => 1, big => 1, em => 1, font => 1, i => 1, nobr => 1,  
   s => 1, small => 1, strile => 1, strong => 1, tt => 1, u => 1,  
 };  
 # $phrasing_category: all other elements  
   
333  sub parse_byte_string ($$$$;$) {  sub parse_byte_string ($$$$;$) {
334      my $self = shift;
335      my $charset_name = shift;
336      open my $input, '<', ref $_[0] ? $_[0] : \($_[0]);
337      return $self->parse_byte_stream ($charset_name, $input, @_[1..$#_]);
338    } # parse_byte_string
339    
340    sub parse_byte_stream ($$$$;$) {
341    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
342    my $charset = shift;    my $charset_name = shift;
343    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;  
   }  
344    
345    $self->{change_encoding} = sub {    my $onerror = $_[2] || sub {
346      my $self = shift;      my (%opt) = @_;
347      my $charset = lc shift;      warn "Parse error ($opt{type})\n";
348      my $token = shift;    };
349      ## TODO: if $charset is supported    $self->{parse_error} = $onerror; # updated later by parse_char_string
350      ## TODO: normalize charset name  
351      ## HTML5 encoding sniffing algorithm
352      require Message::Charset::Info;
353      my $charset;
354      my $buffer;
355      my ($char_stream, $e_status);
356    
357      ## "Change the encoding" algorithm:    SNIFFING: {
358    
359      ## Step 1          ## Step 1
360      if ($charset eq 'utf-16') { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?      if (defined $charset_name) {
361        $charset = 'utf-8';        $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
362    
363          ## ISSUE: Unsupported encoding is not ignored according to the spec.
364          ($char_stream, $e_status) = $charset->get_decode_handle
365              ($byte_stream, allow_error_reporting => 1,
366               allow_fallback => 1);
367          if ($char_stream) {
368            $self->{confident} = 1;
369            last SNIFFING;
370          } else {
371            ## TODO: unsupported error
372          }
373      }      }
374    
375      ## Step 2      ## Step 2
376      if (defined $self->{input_encoding} and      my $byte_buffer = '';
377          $self->{input_encoding} eq $charset) {      for (1..1024) {
378          my $char = $byte_stream->getc;
379          last unless defined $char;
380          $byte_buffer .= $char;
381        } ## TODO: timeout
382    
383        ## Step 3
384        if ($byte_buffer =~ /^\xFE\xFF/) {
385          $charset = Message::Charset::Info->get_by_iana_name ('utf-16be');
386          ($char_stream, $e_status) = $charset->get_decode_handle
387              ($byte_stream, allow_error_reporting => 1,
388               allow_fallback => 1, byte_buffer => \$byte_buffer);
389        $self->{confident} = 1;        $self->{confident} = 1;
390        return;        last SNIFFING;
391        } elsif ($byte_buffer =~ /^\xFF\xFE/) {
392          $charset = Message::Charset::Info->get_by_iana_name ('utf-16le');
393          ($char_stream, $e_status) = $charset->get_decode_handle
394              ($byte_stream, allow_error_reporting => 1,
395               allow_fallback => 1, byte_buffer => \$byte_buffer);
396          $self->{confident} = 1;
397          last SNIFFING;
398        } elsif ($byte_buffer =~ /^\xEF\xBB\xBF/) {
399          $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
400          ($char_stream, $e_status) = $charset->get_decode_handle
401              ($byte_stream, allow_error_reporting => 1,
402               allow_fallback => 1, byte_buffer => \$byte_buffer);
403          $self->{confident} = 1;
404          last SNIFFING;
405      }      }
406    
407      !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.      ## Step 4
408          ':'.$charset, level => 'w', token => $token);      ## TODO: <meta charset>
409    
410      ## Step 3      ## Step 5
411      # if (can) {      ## TODO: from history
       ## change the encoding on the fly.  
       #$self->{confident} = 1;  
       #return;  
     # }  
412    
413      ## Step 4      ## Step 6
414      throw Whatpm::HTML::RestartParser (charset => $charset);      require Whatpm::Charset::UniversalCharDet;
415        $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
416            ($byte_buffer);
417        if (defined $charset_name) {
418          $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
419    
420          ## ISSUE: Unsupported encoding is not ignored according to the spec.
421          require Whatpm::Charset::DecodeHandle;
422          $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
423              ($byte_stream);
424          ($char_stream, $e_status) = $charset->get_decode_handle
425              ($buffer, allow_error_reporting => 1,
426               allow_fallback => 1, byte_buffer => \$byte_buffer);
427          if ($char_stream) {
428            $buffer->{buffer} = $byte_buffer;
429            !!!parse-error (type => 'sniffing:chardet', ## TODO: type name
430                            value => $charset_name,
431                            level => $self->{info_level},
432                            line => 1, column => 1);
433            $self->{confident} = 0;
434            last SNIFFING;
435          }
436        }
437    
438        ## Step 7: default
439        ## TODO: Make this configurable.
440        $charset = Message::Charset::Info->get_by_iana_name ('windows-1252');
441            ## NOTE: We choose |windows-1252| here, since |utf-8| should be
442            ## detectable in the step 6.
443        require Whatpm::Charset::DecodeHandle;
444        $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
445            ($byte_stream);
446        ($char_stream, $e_status)
447            = $charset->get_decode_handle ($buffer,
448                                           allow_error_reporting => 1,
449                                           allow_fallback => 1,
450                                           byte_buffer => \$byte_buffer);
451        $buffer->{buffer} = $byte_buffer;
452        !!!parse-error (type => 'sniffing:default', ## TODO: type name
453                        value => 'windows-1252',
454                        level => $self->{info_level},
455                        line => 1, column => 1);
456        $self->{confident} = 0;
457      } # SNIFFING
458    
459      $self->{input_encoding} = $charset->get_iana_name;
460      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
461        !!!parse-error (type => 'chardecode:fallback', ## TODO: type name
462                        value => $self->{input_encoding},
463                        level => $self->{unsupported_level},
464                        line => 1, column => 1);
465      } elsif (not ($e_status &
466                    Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
467        !!!parse-error (type => 'chardecode:no error', ## TODO: type name
468                        value => $self->{input_encoding},
469                        level => $self->{unsupported_level},
470                        line => 1, column => 1);
471      }
472    
473      $self->{change_encoding} = sub {
474        my $self = shift;
475        $charset_name = shift;
476        my $token = shift;
477    
478        $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
479        ($char_stream, $e_status) = $charset->get_decode_handle
480            ($byte_stream, allow_error_reporting => 1, allow_fallback => 1,
481             byte_buffer => \ $buffer->{buffer});
482        
483        if ($char_stream) { # if supported
484          ## "Change the encoding" algorithm:
485    
486          ## Step 1    
487          if ($charset->{iana_names}->{'utf-16'}) { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?
488            $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
489            ($char_stream, $e_status) = $charset->get_decode_handle
490                ($byte_stream,
491                 byte_buffer => \ $buffer->{buffer});
492          }
493          $charset_name = $charset->get_iana_name;
494          
495          ## Step 2
496          if (defined $self->{input_encoding} and
497              $self->{input_encoding} eq $charset_name) {
498            !!!parse-error (type => 'charset label:matching', ## TODO: type
499                            value => $charset_name,
500                            level => $self->{info_level});
501            $self->{confident} = 1;
502            return;
503          }
504    
505          !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.
506              ':'.$charset_name, level => 'w', token => $token);
507          
508          ## Step 3
509          # if (can) {
510            ## change the encoding on the fly.
511            #$self->{confident} = 1;
512            #return;
513          # }
514          
515          ## Step 4
516          throw Whatpm::HTML::RestartParser ();
517        }
518    }; # $self->{change_encoding}    }; # $self->{change_encoding}
519    
520      my $char_onerror = sub {
521        my (undef, $type, %opt) = @_;
522        !!!parse-error (%opt, type => $type,
523                        line => $self->{line}, column => $self->{column} + 1);
524        if ($opt{octets}) {
525          ${$opt{octets}} = "\x{FFFD}"; # relacement character
526        }
527      };
528      $char_stream->onerror ($char_onerror);
529    
530    my @args = @_; shift @args; # $s    my @args = @_; shift @args; # $s
531    my $return;    my $return;
532    try {    try {
533      $return = $self->parse_char_string ($s, @args);        $return = $self->parse_char_stream ($char_stream, @args);  
534    } catch Whatpm::HTML::RestartParser with {    } catch Whatpm::HTML::RestartParser with {
535      my $charset = shift->{charset};      ## NOTE: Invoked after {change_encoding}.
536      $s = \ (Encode::decode ($charset, $$bytes_s));      
537      $self->{input_encoding} = $charset; ## TODO: normalize      $self->{input_encoding} = $charset->get_iana_name;
538        if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
539          !!!parse-error (type => 'chardecode:fallback', ## TODO: type name
540                          value => $self->{input_encoding},
541                          level => $self->{unsupported_level},
542                          line => 1, column => 1);
543        } elsif (not ($e_status &
544                      Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
545          !!!parse-error (type => 'chardecode:no error', ## TODO: type name
546                          value => $self->{input_encoding},
547                          level => $self->{unsupported_level},
548                          line => 1, column => 1);
549        }
550      $self->{confident} = 1;      $self->{confident} = 1;
551      $return = $self->parse_char_string ($s, @args);      $char_stream->onerror ($char_onerror);
552        $return = $self->parse_char_stream ($char_stream, @args);
553    };    };
554    return $return;    return $return;
555  } # parse_byte_string  } # parse_byte_stream
556    
557  ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM  ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
558  ## and the HTML layer MUST ignore it.  However, we does strip BOM in  ## and the HTML layer MUST ignore it.  However, we does strip BOM in
# Line 163  sub parse_byte_string ($$$$;$) { Line 563  sub parse_byte_string ($$$$;$) {
563  ## 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
564  ## strip the BOM and never strip any ZWNBSP.  ## strip the BOM and never strip any ZWNBSP.
565    
566  *parse_char_string = \&parse_string;  sub parse_char_string ($$$;$) {
567      my $self = shift;
568      require utf8;
569      my $s = ref $_[0] ? $_[0] : \($_[0]);
570      open my $input, '<' . (utf8::is_utf8 ($$s) ? ':utf8' : ''), $s;
571      return $self->parse_char_stream ($input, @_[1..$#_]);
572    } # parse_char_string
573    *parse_string = \&parse_char_string;
574    
575  sub parse_string ($$$;$) {  sub parse_char_stream ($$$;$) {
576    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
577    my $s = ref $_[0] ? $_[0] : \($_[0]);    my $input = $_[0];
578    $self->{document} = $_[1];    $self->{document} = $_[1];
579    @{$self->{document}->child_nodes} = ();    @{$self->{document}->child_nodes} = ();
580    
# Line 186  sub parse_string ($$$;$) { Line 593  sub parse_string ($$$;$) {
593      pop @{$self->{prev_char}};      pop @{$self->{prev_char}};
594      unshift @{$self->{prev_char}}, $self->{next_char};      unshift @{$self->{prev_char}}, $self->{next_char};
595    
596      $self->{next_char} = -1 and return if $i >= length $$s;      my $char;
597      $self->{next_char} = ord substr $$s, $i++, 1;      if (defined $self->{next_next_char}) {
598          $char = $self->{next_next_char};
599          delete $self->{next_next_char};
600        } else {
601          $char = $input->getc;
602        }
603        $self->{next_char} = -1 and return unless defined $char;
604        $self->{next_char} = ord $char;
605    
606      ($self->{line_prev}, $self->{column_prev})      ($self->{line_prev}, $self->{column_prev})
607          = ($self->{line}, $self->{column});          = ($self->{line}, $self->{column});
608      $self->{column}++;      $self->{column}++;
609            
610      if ($self->{next_char} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
611          !!!cp ('j1');
612        $self->{line}++;        $self->{line}++;
613        $self->{column} = 0;        $self->{column} = 0;
614      } elsif ($self->{next_char} == 0x000D) { # CR      } elsif ($self->{next_char} == 0x000D) { # CR
615        $i++ if substr ($$s, $i, 1) eq "\x0A";        !!!cp ('j2');
616          my $next = $input->getc;
617          if (defined $next and $next ne "\x0A") {
618            $self->{next_next_char} = $next;
619          }
620        $self->{next_char} = 0x000A; # LF # MUST        $self->{next_char} = 0x000A; # LF # MUST
621        $self->{line}++;        $self->{line}++;
622        $self->{column} = 0;        $self->{column} = 0;
623      } elsif ($self->{next_char} > 0x10FFFF) {      } elsif ($self->{next_char} > 0x10FFFF) {
624          !!!cp ('j3');
625        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
626      } elsif ($self->{next_char} == 0x0000) { # NULL      } elsif ($self->{next_char} == 0x0000) { # NULL
627          !!!cp ('j4');
628        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
629        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
630        } elsif ($self->{next_char} <= 0x0008 or
631                 (0x000E <= $self->{next_char} and $self->{next_char} <= 0x001F) or
632                 (0x007F <= $self->{next_char} and $self->{next_char} <= 0x009F) or
633                 (0xD800 <= $self->{next_char} and $self->{next_char} <= 0xDFFF) or
634                 (0xFDD0 <= $self->{next_char} and $self->{next_char} <= 0xFDDF) or
635                 {
636                  0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
637                  0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
638                  0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
639                  0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
640                  0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
641                  0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
642                  0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
643                  0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
644                  0x10FFFE => 1, 0x10FFFF => 1,
645                 }->{$self->{next_char}}) {
646          !!!cp ('j5');
647          !!!parse-error (type => 'control char', level => $self->{must_level});
648    ## TODO: error type documentation
649      }      }
650    };    };
651    $self->{prev_char} = [-1, -1, -1];    $self->{prev_char} = [-1, -1, -1];
# Line 229  sub parse_string ($$$;$) { Line 669  sub parse_string ($$$;$) {
669    delete $self->{parse_error}; # remove loop    delete $self->{parse_error}; # remove loop
670    
671    return $self->{document};    return $self->{document};
672  } # parse_string  } # parse_char_stream
673    
674  sub new ($) {  sub new ($) {
675    my $class = shift;    my $class = shift;
676    my $self = bless {}, $class;    my $self = bless {
677        must_level => 'm',
678        should_level => 's',
679        good_level => 'w',
680        warn_level => 'w',
681        info_level => 'i',
682        unsupported_level => 'u',
683      }, $class;
684    $self->{set_next_char} = sub {    $self->{set_next_char} = sub {
685      $self->{next_char} = -1;      $self->{next_char} = -1;
686    };    };
# Line 295  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO Line 742  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO
742  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
743  sub BOGUS_DOCTYPE_STATE () { 32 }  sub BOGUS_DOCTYPE_STATE () { 32 }
744  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
745    sub SELF_CLOSING_START_TAG_STATE () { 34 }
746    sub CDATA_BLOCK_STATE () { 35 }
747    
748  sub DOCTYPE_TOKEN () { 1 }  sub DOCTYPE_TOKEN () { 1 }
749  sub COMMENT_TOKEN () { 2 }  sub COMMENT_TOKEN () { 2 }
# Line 312  sub ROW_IMS ()        { 0b10000000 } Line 761  sub ROW_IMS ()        { 0b10000000 }
761  sub BODY_AFTER_IMS () { 0b100000000 }  sub BODY_AFTER_IMS () { 0b100000000 }
762  sub FRAME_IMS ()      { 0b1000000000 }  sub FRAME_IMS ()      { 0b1000000000 }
763  sub SELECT_IMS ()     { 0b10000000000 }  sub SELECT_IMS ()     { 0b10000000000 }
764    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
765        ## NOTE: "in foreign content" insertion mode is special; it is combined
766        ## with the secondary insertion mode.  In this parser, they are stored
767        ## together in the bit-or'ed form.
768    
769  ## NOTE: "initial" and "before html" insertion modes have no constants.  ## NOTE: "initial" and "before html" insertion modes have no constants.
770    
# Line 348  sub _initialize_tokenizer ($) { Line 801  sub _initialize_tokenizer ($) {
801    undef $self->{current_attribute};    undef $self->{current_attribute};
802    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
803    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
804      delete $self->{self_closing};
805    $self->{char} = [];    $self->{char} = [];
806    # $self->{next_char}    # $self->{next_char}
807    !!!next-input-character;    !!!next-input-character;
# Line 368  sub _initialize_tokenizer ($) { Line 822  sub _initialize_tokenizer ($) {
822  ##        ->{value}  ##        ->{value}
823  ##        ->{has_reference} == 1 or 0  ##        ->{has_reference} == 1 or 0
824  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
825    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
826    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
827    ##     while the token is pushed back to the stack.
828    
829    ## ISSUE: "When a DOCTYPE token is created, its
830    ## <i>self-closing flag</i> must be unset (its other state is that it
831    ## be set), and its attributes list must be empty.": Wrong subject?
832    
833  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
834    
# Line 394  sub _initialize_tokenizer ($) { Line 855  sub _initialize_tokenizer ($) {
855    
856  sub _get_next_token ($) {  sub _get_next_token ($) {
857    my $self = shift;    my $self = shift;
858    
859      if ($self->{self_closing}) {
860        !!!parse-error (type => 'nestc', token => $self->{current_token});
861        ## NOTE: The |self_closing| flag is only set by start tag token.
862        ## In addition, when a start tag token is emitted, it is always set to
863        ## |current_token|.
864        delete $self->{self_closing};
865      }
866    
867    if (@{$self->{token}}) {    if (@{$self->{token}}) {
868        $self->{self_closing} = $self->{token}->[0]->{self_closing};
869      return shift @{$self->{token}};      return shift @{$self->{token}};
870    }    }
871    
# Line 466  sub _get_next_token ($) { Line 937  sub _get_next_token ($) {
937        # Anything else        # Anything else
938        my $token = {type => CHARACTER_TOKEN,        my $token = {type => CHARACTER_TOKEN,
939                     data => chr $self->{next_char},                     data => chr $self->{next_char},
940                     line => $self->{line}, column => $self->{column}};                     line => $self->{line}, column => $self->{column},
941                      };
942        ## Stay in the data state        ## Stay in the data state
943        !!!next-input-character;        !!!next-input-character;
944    
# Line 486  sub _get_next_token ($) { Line 958  sub _get_next_token ($) {
958        unless (defined $token) {        unless (defined $token) {
959          !!!cp (13);          !!!cp (13);
960          !!!emit ({type => CHARACTER_TOKEN, data => '&',          !!!emit ({type => CHARACTER_TOKEN, data => '&',
961                    line => $l, column => $c});                    line => $l, column => $c,
962                     });
963        } else {        } else {
964          !!!cp (14);          !!!cp (14);
965          !!!emit ($token);          !!!emit ($token);
# Line 507  sub _get_next_token ($) { Line 980  sub _get_next_token ($) {
980    
981            !!!emit ({type => CHARACTER_TOKEN, data => '<',            !!!emit ({type => CHARACTER_TOKEN, data => '<',
982                      line => $self->{line_prev},                      line => $self->{line_prev},
983                      column => $self->{column_prev}});                      column => $self->{column_prev},
984                       });
985    
986            redo A;            redo A;
987          }          }
# Line 553  sub _get_next_token ($) { Line 1027  sub _get_next_token ($) {
1027    
1028            !!!emit ({type => CHARACTER_TOKEN, data => '<>',            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
1029                      line => $self->{line_prev},                      line => $self->{line_prev},
1030                      column => $self->{column_prev}});                      column => $self->{column_prev},
1031                       });
1032    
1033            redo A;            redo A;
1034          } elsif ($self->{next_char} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
# Line 564  sub _get_next_token ($) { Line 1039  sub _get_next_token ($) {
1039            $self->{state} = BOGUS_COMMENT_STATE;            $self->{state} = BOGUS_COMMENT_STATE;
1040            $self->{current_token} = {type => COMMENT_TOKEN, data => '',            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1041                                      line => $self->{line_prev},                                      line => $self->{line_prev},
1042                                      column => $self->{column_prev}};                                      column => $self->{column_prev},
1043                                       };
1044            ## $self->{next_char} is intentionally left as is            ## $self->{next_char} is intentionally left as is
1045            redo A;            redo A;
1046          } else {          } else {
1047            !!!cp (23);            !!!cp (23);
1048            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago',
1049                              line => $self->{line_prev},
1050                              column => $self->{column_prev});
1051            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1052            ## reconsume            ## reconsume
1053    
1054            !!!emit ({type => CHARACTER_TOKEN, data => '<',            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1055                      line => $self->{line_prev},                      line => $self->{line_prev},
1056                      column => $self->{column_prev}});                      column => $self->{column_prev},
1057                       });
1058    
1059            redo A;            redo A;
1060          }          }
# Line 604  sub _get_next_token ($) { Line 1083  sub _get_next_token ($) {
1083                $self->{state} = DATA_STATE;                $self->{state} = DATA_STATE;
1084    
1085                !!!emit ({type => CHARACTER_TOKEN, data => '</',                !!!emit ({type => CHARACTER_TOKEN, data => '</',
1086                          line => $l, column => $c});                          line => $l, column => $c,
1087                           });
1088        
1089                redo A;                redo A;
1090              }              }
# Line 624  sub _get_next_token ($) { Line 1104  sub _get_next_token ($) {
1104              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
1105              $self->{state} = DATA_STATE;              $self->{state} = DATA_STATE;
1106              !!!emit ({type => CHARACTER_TOKEN, data => '</',              !!!emit ({type => CHARACTER_TOKEN, data => '</',
1107                        line => $l, column => $c});                        line => $l, column => $c,
1108                         });
1109              redo A;              redo A;
1110            } else {            } else {
1111              !!!cp (27);              !!!cp (27);
# Line 638  sub _get_next_token ($) { Line 1119  sub _get_next_token ($) {
1119            # next-input-character is already done            # next-input-character is already done
1120            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1121            !!!emit ({type => CHARACTER_TOKEN, data => '</',            !!!emit ({type => CHARACTER_TOKEN, data => '</',
1122                      line => $l, column => $c});                      line => $l, column => $c,
1123                       });
1124            redo A;            redo A;
1125          }          }
1126        }        }
# Line 677  sub _get_next_token ($) { Line 1159  sub _get_next_token ($) {
1159          # reconsume          # reconsume
1160    
1161          !!!emit ({type => CHARACTER_TOKEN, data => '</',          !!!emit ({type => CHARACTER_TOKEN, data => '</',
1162                    line => $l, column => $c});                    line => $l, column => $c,
1163                     });
1164    
1165          redo A;          redo A;
1166        } else {        } else {
# Line 686  sub _get_next_token ($) { Line 1169  sub _get_next_token ($) {
1169          $self->{state} = BOGUS_COMMENT_STATE;          $self->{state} = BOGUS_COMMENT_STATE;
1170          $self->{current_token} = {type => COMMENT_TOKEN, data => '',          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1171                                    line => $self->{line_prev}, # "<" of "</"                                    line => $self->{line_prev}, # "<" of "</"
1172                                    column => $self->{column_prev} - 1};                                    column => $self->{column_prev} - 1,
1173                                     };
1174          ## $self->{next_char} is intentionally left as is          ## $self->{next_char} is intentionally left as is
1175          redo A;          redo A;
1176        }        }
# Line 703  sub _get_next_token ($) { Line 1187  sub _get_next_token ($) {
1187        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1188          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1189            !!!cp (35);            !!!cp (35);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1190            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1191          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1192            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 736  sub _get_next_token ($) { Line 1218  sub _get_next_token ($) {
1218          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1219          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1220            !!!cp (39);            !!!cp (39);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1221            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1222          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1223            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 758  sub _get_next_token ($) { Line 1238  sub _get_next_token ($) {
1238    
1239          redo A;          redo A;
1240        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1241            !!!cp (42);
1242            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1243          !!!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  
1244          redo A;          redo A;
1245        } else {        } else {
1246          !!!cp (44);          !!!cp (44);
# Line 793  sub _get_next_token ($) { Line 1263  sub _get_next_token ($) {
1263        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1264          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1265            !!!cp (46);            !!!cp (46);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1266            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1267          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1268            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 816  sub _get_next_token ($) { Line 1284  sub _get_next_token ($) {
1284        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{next_char} and
1285                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1286          !!!cp (49);          !!!cp (49);
1287          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{current_attribute}
1288                                value => ''};              = {name => chr ($self->{next_char} + 0x0020),
1289                   value => '',
1290                   line => $self->{line}, column => $self->{column}};
1291          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1292          !!!next-input-character;          !!!next-input-character;
1293          redo A;          redo A;
1294        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1295            !!!cp (50);
1296            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1297          !!!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  
1298          redo A;          redo A;
1299        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1300          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1301          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1302            !!!cp (52);            !!!cp (52);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1303            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1304          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1305            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 871  sub _get_next_token ($) { Line 1329  sub _get_next_token ($) {
1329          } else {          } else {
1330            !!!cp (56);            !!!cp (56);
1331          }          }
1332          $self->{current_attribute} = {name => chr ($self->{next_char}),          $self->{current_attribute}
1333                                value => ''};              = {name => chr ($self->{next_char}),
1334                   value => '',
1335                   line => $self->{line}, column => $self->{column}};
1336          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1337          !!!next-input-character;          !!!next-input-character;
1338          redo A;          redo A;
# Line 882  sub _get_next_token ($) { Line 1342  sub _get_next_token ($) {
1342          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1343              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1344            !!!cp (57);            !!!cp (57);
1345            !!!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});
1346            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1347          } else {          } else {
1348            !!!cp (58);            !!!cp (58);
# Line 911  sub _get_next_token ($) { Line 1371  sub _get_next_token ($) {
1371          $before_leave->();          $before_leave->();
1372          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1373            !!!cp (61);            !!!cp (61);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1374            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1375          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1376            !!!cp (62);            !!!cp (62);
# Line 937  sub _get_next_token ($) { Line 1395  sub _get_next_token ($) {
1395          !!!next-input-character;          !!!next-input-character;
1396          redo A;          redo A;
1397        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1398            !!!cp (64);
1399          $before_leave->();          $before_leave->();
1400            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1401          !!!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  
1402          redo A;          redo A;
1403        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1404          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1405          $before_leave->();          $before_leave->();
1406          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1407            !!!cp (66);            !!!cp (66);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1408            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1409          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1410            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1009  sub _get_next_token ($) { Line 1455  sub _get_next_token ($) {
1455        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1456          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1457            !!!cp (73);            !!!cp (73);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1458            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1459          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1460            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1033  sub _get_next_token ($) { Line 1477  sub _get_next_token ($) {
1477        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{next_char} and
1478                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1479          !!!cp (76);          !!!cp (76);
1480          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{current_attribute}
1481                                value => ''};              = {name => chr ($self->{next_char} + 0x0020),
1482                   value => '',
1483                   line => $self->{line}, column => $self->{column}};
1484          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1485          !!!next-input-character;          !!!next-input-character;
1486          redo A;          redo A;
1487        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1488            !!!cp (77);
1489            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1490          !!!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  
1491          redo A;          redo A;
1492        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1493          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1494          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1495            !!!cp (79);            !!!cp (79);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1496            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1497          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1498            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1081  sub _get_next_token ($) { Line 1514  sub _get_next_token ($) {
1514          redo A;          redo A;
1515        } else {        } else {
1516          !!!cp (82);          !!!cp (82);
1517          $self->{current_attribute} = {name => chr ($self->{next_char}),          $self->{current_attribute}
1518                                value => ''};              = {name => chr ($self->{next_char}),
1519                   value => '',
1520                   line => $self->{line}, column => $self->{column}};
1521          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1522          !!!next-input-character;          !!!next-input-character;
1523          redo A;                  redo A;        
# Line 1115  sub _get_next_token ($) { Line 1550  sub _get_next_token ($) {
1550        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1551          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1552            !!!cp (87);            !!!cp (87);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1553            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1554          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1555            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1140  sub _get_next_token ($) { Line 1573  sub _get_next_token ($) {
1573          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1574          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1575            !!!cp (90);            !!!cp (90);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1576            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1577          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1578            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1189  sub _get_next_token ($) { Line 1620  sub _get_next_token ($) {
1620          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1621          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1622            !!!cp (97);            !!!cp (97);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1623            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1624          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1625            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1233  sub _get_next_token ($) { Line 1662  sub _get_next_token ($) {
1662          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1663          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1664            !!!cp (103);            !!!cp (103);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1665            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1666          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1667            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1280  sub _get_next_token ($) { Line 1707  sub _get_next_token ($) {
1707        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1708          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1709            !!!cp (109);            !!!cp (109);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1710            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1711          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1712            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1305  sub _get_next_token ($) { Line 1730  sub _get_next_token ($) {
1730          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1731          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1732            !!!cp (112);            !!!cp (112);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1733            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1734          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1735            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1377  sub _get_next_token ($) { Line 1800  sub _get_next_token ($) {
1800        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1801          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1802            !!!cp (119);            !!!cp (119);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1803            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1804          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1805            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1399  sub _get_next_token ($) { Line 1820  sub _get_next_token ($) {
1820    
1821          redo A;          redo A;
1822        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1823            !!!cp (122);
1824            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1825          !!!next-input-character;          !!!next-input-character;
1826          if ($self->{next_char} == 0x003E and # >          redo A;
1827              $self->{current_token}->{type} == START_TAG_TOKEN and        } elsif ($self->{next_char} == -1) {
1828              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {          !!!parse-error (type => 'unclosed tag');
1829            # permitted slash          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1830            !!!cp (122);            !!!cp (122.3);
1831            #            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1832            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1833              if ($self->{current_token}->{attributes}) {
1834                !!!cp (122.1);
1835                !!!parse-error (type => 'end tag attribute');
1836              } else {
1837                ## NOTE: This state should never be reached.
1838                !!!cp (122.2);
1839              }
1840          } else {          } else {
1841            !!!cp (123);            die "$0: $self->{current_token}->{type}: Unknown token type";
           !!!parse-error (type => 'nestc');  
1842          }          }
1843          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = DATA_STATE;
1844          # next-input-character is already done          ## Reconsume.
1845            !!!emit ($self->{current_token}); # start tag or end tag
1846          redo A;          redo A;
1847        } else {        } else {
1848          !!!cp (124);          !!!cp ('124.1');
1849          !!!parse-error (type => 'no space between attributes');          !!!parse-error (type => 'no space between attributes');
1850          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1851          ## reconsume          ## reconsume
1852          redo A;          redo A;
1853        }        }
1854        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1855          if ($self->{next_char} == 0x003E) { # >
1856            if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1857              !!!cp ('124.2');
1858              !!!parse-error (type => 'nestc', token => $self->{current_token});
1859              ## TODO: Different type than slash in start tag
1860              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1861              if ($self->{current_token}->{attributes}) {
1862                !!!cp ('124.4');
1863                !!!parse-error (type => 'end tag attribute');
1864              } else {
1865                !!!cp ('124.5');
1866              }
1867              ## TODO: Test |<title></title/>|
1868            } else {
1869              !!!cp ('124.3');
1870              $self->{self_closing} = 1;
1871            }
1872    
1873            $self->{state} = DATA_STATE;
1874            !!!next-input-character;
1875    
1876            !!!emit ($self->{current_token}); # start tag or end tag
1877    
1878            redo A;
1879          } elsif ($self->{next_char} == -1) {
1880            !!!parse-error (type => 'unclosed tag');
1881            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1882              !!!cp (124.7);
1883              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1884            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1885              if ($self->{current_token}->{attributes}) {
1886                !!!cp (124.5);
1887                !!!parse-error (type => 'end tag attribute');
1888              } else {
1889                ## NOTE: This state should never be reached.
1890                !!!cp (124.6);
1891              }
1892            } else {
1893              die "$0: $self->{current_token}->{type}: Unknown token type";
1894            }
1895            $self->{state} = DATA_STATE;
1896            ## Reconsume.
1897            !!!emit ($self->{current_token}); # start tag or end tag
1898            redo A;
1899          } else {
1900            !!!cp ('124.4');
1901            !!!parse-error (type => 'nestc');
1902            ## TODO: This error type is wrong.
1903            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1904            ## Reconsume.
1905            redo A;
1906          }
1907      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1908        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1909                
# Line 1466  sub _get_next_token ($) { Line 1950  sub _get_next_token ($) {
1950          if ($self->{next_char} == 0x002D) { # -          if ($self->{next_char} == 0x002D) { # -
1951            !!!cp (127);            !!!cp (127);
1952            $self->{current_token} = {type => COMMENT_TOKEN, data => '',            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1953                                      line => $l, column => $c};                                      line => $l, column => $c,
1954                                       };
1955            $self->{state} = COMMENT_START_STATE;            $self->{state} = COMMENT_START_STATE;
1956            !!!next-input-character;            !!!next-input-character;
1957            redo A;            redo A;
# Line 1504  sub _get_next_token ($) { Line 1989  sub _get_next_token ($) {
1989                      $self->{state} = DOCTYPE_STATE;                      $self->{state} = DOCTYPE_STATE;
1990                      $self->{current_token} = {type => DOCTYPE_TOKEN,                      $self->{current_token} = {type => DOCTYPE_TOKEN,
1991                                                quirks => 1,                                                quirks => 1,
1992                                                line => $l, column => $c};                                                line => $l, column => $c,
1993                                                 };
1994                      !!!next-input-character;                      !!!next-input-character;
1995                      redo A;                      redo A;
1996                    } else {                    } else {
# Line 1525  sub _get_next_token ($) { Line 2011  sub _get_next_token ($) {
2011          } else {          } else {
2012            !!!cp (135);            !!!cp (135);
2013          }          }
2014          } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
2015                   $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
2016                   $self->{next_char} == 0x005B) { # [
2017            !!!next-input-character;
2018            push @next_char, $self->{next_char};
2019            if ($self->{next_char} == 0x0043) { # C
2020              !!!next-input-character;
2021              push @next_char, $self->{next_char};
2022              if ($self->{next_char} == 0x0044) { # D
2023                !!!next-input-character;
2024                push @next_char, $self->{next_char};
2025                if ($self->{next_char} == 0x0041) { # A
2026                  !!!next-input-character;
2027                  push @next_char, $self->{next_char};
2028                  if ($self->{next_char} == 0x0054) { # T
2029                    !!!next-input-character;
2030                    push @next_char, $self->{next_char};
2031                    if ($self->{next_char} == 0x0041) { # A
2032                      !!!next-input-character;
2033                      push @next_char, $self->{next_char};
2034                      if ($self->{next_char} == 0x005B) { # [
2035                        !!!cp (135.1);
2036                        $self->{state} = CDATA_BLOCK_STATE;
2037                        !!!next-input-character;
2038                        redo A;
2039                      } else {
2040                        !!!cp (135.2);
2041                      }
2042                    } else {
2043                      !!!cp (135.3);
2044                    }
2045                  } else {
2046                    !!!cp (135.4);                
2047                  }
2048                } else {
2049                  !!!cp (135.5);
2050                }
2051              } else {
2052                !!!cp (135.6);
2053              }
2054            } else {
2055              !!!cp (135.7);
2056            }
2057        } else {        } else {
2058          !!!cp (136);          !!!cp (136);
2059        }        }
# Line 1534  sub _get_next_token ($) { Line 2063  sub _get_next_token ($) {
2063        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
2064        $self->{state} = BOGUS_COMMENT_STATE;        $self->{state} = BOGUS_COMMENT_STATE;
2065        $self->{current_token} = {type => COMMENT_TOKEN, data => '',        $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2066                                  line => $l, column => $c};                                  line => $l, column => $c,
2067                                   };
2068        redo A;        redo A;
2069                
2070        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
# Line 2206  sub _get_next_token ($) { Line 2736  sub _get_next_token ($) {
2736          redo A;          redo A;
2737        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
2738          !!!cp (217);          !!!cp (217);
         !!!parse-error (type => 'unclosed DOCTYPE');  
2739    
2740          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2741          ## reconsume          ## reconsume
# Line 2248  sub _get_next_token ($) { Line 2777  sub _get_next_token ($) {
2777          !!!next-input-character;          !!!next-input-character;
2778          redo A;          redo A;
2779        }        }
2780        } elsif ($self->{state} == CDATA_BLOCK_STATE) {
2781          my $s = '';
2782          
2783          my ($l, $c) = ($self->{line}, $self->{column});
2784    
2785          CS: while ($self->{next_char} != -1) {
2786            if ($self->{next_char} == 0x005D) { # ]
2787              !!!next-input-character;
2788              if ($self->{next_char} == 0x005D) { # ]
2789                !!!next-input-character;
2790                MDC: {
2791                  if ($self->{next_char} == 0x003E) { # >
2792                    !!!cp (221.1);
2793                    !!!next-input-character;
2794                    last CS;
2795                  } elsif ($self->{next_char} == 0x005D) { # ]
2796                    !!!cp (221.2);
2797                    $s .= ']';
2798                    !!!next-input-character;
2799                    redo MDC;
2800                  } else {
2801                    !!!cp (221.3);
2802                    $s .= ']]';
2803                    #
2804                  }
2805                } # MDC
2806              } else {
2807                !!!cp (221.4);
2808                $s .= ']';
2809                #
2810              }
2811            } else {
2812              !!!cp (221.5);
2813              #
2814            }
2815            $s .= chr $self->{next_char};
2816            !!!next-input-character;
2817          } # CS
2818    
2819          $self->{state} = DATA_STATE;
2820          ## next-input-character done or EOF, which is reconsumed.
2821    
2822          if (length $s) {
2823            !!!cp (221.6);
2824            !!!emit ({type => CHARACTER_TOKEN, data => $s,
2825                      line => $l, column => $c});
2826          } else {
2827            !!!cp (221.7);
2828          }
2829    
2830          redo A;
2831    
2832          ## ISSUE: "text tokens" in spec.
2833          ## TODO: Streaming support
2834      } else {      } else {
2835        die "$0: $self->{state}: Unknown state";        die "$0: $self->{state}: Unknown state";
2836      }      }
# Line 2332  sub _tokenize_attempt_to_consume_an_enti Line 2915  sub _tokenize_attempt_to_consume_an_enti
2915          }          }
2916    
2917          return {type => CHARACTER_TOKEN, data => chr $code,          return {type => CHARACTER_TOKEN, data => chr $code,
2918                  has_reference => 1, line => $l, column => $c};                  has_reference => 1,
2919                    line => $l, column => $c,
2920                   };
2921        } # X        } # X
2922      } elsif (0x0030 <= $self->{next_char} and      } elsif (0x0030 <= $self->{next_char} and
2923               $self->{next_char} <= 0x0039) { # 0..9               $self->{next_char} <= 0x0039) { # 0..9
# Line 2375  sub _tokenize_attempt_to_consume_an_enti Line 2960  sub _tokenize_attempt_to_consume_an_enti
2960        }        }
2961                
2962        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,
2963                line => $l, column => $c};                line => $l, column => $c,
2964                 };
2965      } else {      } else {
2966        !!!cp (1019);        !!!cp (1019);
2967        !!!parse-error (type => 'bare nero', line => $l, column => $c);        !!!parse-error (type => 'bare nero', line => $l, column => $c);
# Line 2395  sub _tokenize_attempt_to_consume_an_enti Line 2981  sub _tokenize_attempt_to_consume_an_enti
2981      require Whatpm::_NamedEntityList;      require Whatpm::_NamedEntityList;
2982      our $EntityChar;      our $EntityChar;
2983    
2984      while (length $entity_name < 10 and      while (length $entity_name < 30 and
2985             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
2986             ((0x0041 <= $self->{next_char} and # a             ((0x0041 <= $self->{next_char} and # a
2987               $self->{next_char} <= 0x005A) or # x               $self->{next_char} <= 0x005A) or # x
# Line 2429  sub _tokenize_attempt_to_consume_an_enti Line 3015  sub _tokenize_attempt_to_consume_an_enti
3015      if ($match > 0) {      if ($match > 0) {
3016        !!!cp (1023);        !!!cp (1023);
3017        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
3018                line => $l, column => $c};                line => $l, column => $c,
3019                 };
3020      } elsif ($match < 0) {      } elsif ($match < 0) {
3021        !!!parse-error (type => 'no refc', line => $l, column => $c);        !!!parse-error (type => 'no refc', line => $l, column => $c);
3022        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
3023          !!!cp (1024);          !!!cp (1024);
3024          return {type => CHARACTER_TOKEN, data => '&'.$entity_name,          return {type => CHARACTER_TOKEN, data => '&'.$entity_name,
3025                  line => $l, column => $c};                  line => $l, column => $c,
3026                   };
3027        } else {        } else {
3028          !!!cp (1025);          !!!cp (1025);
3029          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
3030                  line => $l, column => $c};                  line => $l, column => $c,
3031                   };
3032        }        }
3033      } else {      } else {
3034        !!!cp (1026);        !!!cp (1026);
3035        !!!parse-error (type => 'bare ero', line => $l, column => $c);        !!!parse-error (type => 'bare ero', line => $l, column => $c);
3036        ## NOTE: "No characters are consumed" in the spec.        ## NOTE: "No characters are consumed" in the spec.
3037        return {type => CHARACTER_TOKEN, data => '&'.$value,        return {type => CHARACTER_TOKEN, data => '&'.$value,
3038                line => $l, column => $c};                line => $l, column => $c,
3039                 };
3040      }      }
3041    } else {    } else {
3042      !!!cp (1027);      !!!cp (1027);
# Line 2533  sub _tree_construction_initial ($) { Line 3123  sub _tree_construction_initial ($) {
3123                
3124        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
3125          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3126          ## NOTE: Default value for both |public_id| and |system_id| attributes
3127          ## are empty strings, so that we don't set any value in missing cases.
3128        $doctype->public_id ($token->{public_identifier})        $doctype->public_id ($token->{public_identifier})
3129            if defined $token->{public_identifier};            if defined $token->{public_identifier};
3130        $doctype->system_id ($token->{system_identifier})        $doctype->system_id ($token->{system_identifier})
# Line 2547  sub _tree_construction_initial ($) { Line 3139  sub _tree_construction_initial ($) {
3139        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{public_identifier}) {
3140          my $pubid = $token->{public_identifier};          my $pubid = $token->{public_identifier};
3141          $pubid =~ tr/a-z/A-z/;          $pubid =~ tr/a-z/A-z/;
3142          if ({          my $prefix = [
3143            "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,            "+//SILMARIL//DTD HTML PRO V0R11 19970101//",
3144            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3145            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3146            "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 1//",
3147            "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 2//",
3148            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//",
3149            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//",
3150            "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT//",
3151            "-//IETF//DTD HTML 2.0//EN" => 1,            "-//IETF//DTD HTML 2.0//",
3152            "-//IETF//DTD HTML 2.1E//EN" => 1,            "-//IETF//DTD HTML 2.1E//",
3153            "-//IETF//DTD HTML 3.0//EN" => 1,            "-//IETF//DTD HTML 3.0//",
3154            "-//IETF//DTD HTML 3.0//EN//" => 1,            "-//IETF//DTD HTML 3.2 FINAL//",
3155            "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,            "-//IETF//DTD HTML 3.2//",
3156            "-//IETF//DTD HTML 3.2//EN" => 1,            "-//IETF//DTD HTML 3//",
3157            "-//IETF//DTD HTML 3//EN" => 1,            "-//IETF//DTD HTML LEVEL 0//",
3158            "-//IETF//DTD HTML LEVEL 0//EN" => 1,            "-//IETF//DTD HTML LEVEL 1//",
3159            "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,            "-//IETF//DTD HTML LEVEL 2//",
3160            "-//IETF//DTD HTML LEVEL 1//EN" => 1,            "-//IETF//DTD HTML LEVEL 3//",
3161            "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 0//",
3162            "-//IETF//DTD HTML LEVEL 2//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 1//",
3163            "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 2//",
3164            "-//IETF//DTD HTML LEVEL 3//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 3//",
3165            "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,            "-//IETF//DTD HTML STRICT//",
3166            "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,            "-//IETF//DTD HTML//",
3167            "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,            "-//METRIUS//DTD METRIUS PRESENTATIONAL//",
3168            "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//",
3169            "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//",
3170            "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//",
3171            "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//",
3172            "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//",
3173            "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//",
3174            "-//IETF//DTD HTML STRICT//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD HTML//",
3175            "-//IETF//DTD HTML STRICT//EN//2.0" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//",
3176            "-//IETF//DTD HTML STRICT//EN//3.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//",
3177            "-//IETF//DTD HTML//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//",
3178            "-//IETF//DTD HTML//EN//2.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//",
3179            "-//IETF//DTD HTML//EN//3.0" => 1,            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//",
3180            "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//",
3181            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//",
3182            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//",
3183            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//",
3184            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//",
3185            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,            "-//W3C//DTD HTML 3 1995-03-24//",
3186            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,            "-//W3C//DTD HTML 3.2 DRAFT//",
3187            "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,            "-//W3C//DTD HTML 3.2 FINAL//",
3188            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//W3C//DTD HTML 3.2//",
3189            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//W3C//DTD HTML 3.2S DRAFT//",
3190            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 FRAMESET//",
3191            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 TRANSITIONAL//",
3192            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMETNAL 19960712//",
3193            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMENTAL 970421//",
3194            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//W3C//DTD W3 HTML//",
3195            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//W3O//DTD W3 HTML 3.0//",
3196            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML 2.0//",
3197            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML//",
3198            "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,          ]; # $prefix
3199            "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,          my $match;
3200            "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,          for (@$prefix) {
3201            "-//W3C//DTD HTML 3.2//EN" => 1,            if (substr ($prefix, 0, length $_) eq $_) {
3202            "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,              $match = 1;
3203            "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,              last;
3204            "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,            }
3205            "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,          }
3206            "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,          if ($match or
3207            "-//W3C//DTD W3 HTML//EN" => 1,              $pubid eq "-//W3O//DTD W3 HTML STRICT 3.0//EN//" or
3208            "-//W3O//DTD W3 HTML 3.0//EN" => 1,              $pubid eq "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" or
3209            "-//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}) {  
3210            !!!cp ('t5');            !!!cp ('t5');
3211            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3212          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD HTML 4.01 FRAMESET//] or
3213                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD HTML 4.01 TRANSITIONAL//]) {
3214            if (defined $token->{system_identifier}) {            if (defined $token->{system_identifier}) {
3215              !!!cp ('t6');              !!!cp ('t6');
3216              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
# Line 2632  sub _tree_construction_initial ($) { Line 3218  sub _tree_construction_initial ($) {
3218              !!!cp ('t7');              !!!cp ('t7');
3219              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
3220            }            }
3221          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD XHTML 1.0 FRAMESET//] or
3222                   $pubid eq "-//W3C//DTD XHTML 1.0 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD XHTML 1.0 TRANSITIONAL//]) {
3223            !!!cp ('t8');            !!!cp ('t8');
3224            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
3225          } else {          } else {
# Line 2646  sub _tree_construction_initial ($) { Line 3232  sub _tree_construction_initial ($) {
3232          my $sysid = $token->{system_identifier};          my $sysid = $token->{system_identifier};
3233          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
3234          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") {
3235            ## TODO: Check the spec: PUBLIC "(limited quirks)" "(quirks)"            ## NOTE: Ensure that |PUBLIC "(limited quirks)" "(quirks)"| is
3236              ## marked as quirks.
3237            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3238            !!!cp ('t11');            !!!cp ('t11');
3239          } else {          } else {
# Line 2669  sub _tree_construction_initial ($) { Line 3256  sub _tree_construction_initial ($) {
3256        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3257        ## Go to the "before html" insertion mode.        ## Go to the "before html" insertion mode.
3258        ## reprocess        ## reprocess
3259          !!!ack-later;
3260        return;        return;
3261      } elsif ($token->{type} == CHARACTER_TOKEN) {      } elsif ($token->{type} == CHARACTER_TOKEN) {
3262        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
# Line 2749  sub _tree_construction_root_element ($) Line 3337  sub _tree_construction_root_element ($)
3337        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
3338          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
3339            my $root_element;            my $root_element;
3340            !!!create-element ($root_element, $token->{tag_name}, $token->{attributes}, $token);            !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3341            $self->{document}->append_child ($root_element);            $self->{document}->append_child ($root_element);
3342            push @{$self->{open_elements}}, [$root_element, 'html'];            push @{$self->{open_elements}},
3343                  [$root_element, $el_category->{html}];
3344    
3345            if ($token->{attributes}->{manifest}) {            if ($token->{attributes}->{manifest}) {
3346              !!!cp ('t24');              !!!cp ('t24');
3347              $self->{application_cache_selection}              $self->{application_cache_selection}
3348                  ->($token->{attributes}->{manifest}->{value});                  ->($token->{attributes}->{manifest}->{value});
3349              ## ISSUE: No relative reference resolution?              ## ISSUE: Spec is unclear on relative references.
3350                ## According to Hixie (#whatwg 2008-03-19), it should be
3351                ## resolved against the base URI of the document in HTML
3352                ## or xml:base of the element in XHTML.
3353            } else {            } else {
3354              !!!cp ('t25');              !!!cp ('t25');
3355              $self->{application_cache_selection}->(undef);              $self->{application_cache_selection}->(undef);
3356            }            }
3357    
3358              !!!nack ('t25c');
3359    
3360            !!!next-token;            !!!next-token;
3361            return; ## Go to the "before head" insertion mode.            return; ## Go to the "before head" insertion mode.
3362          } else {          } else {
# Line 2779  sub _tree_construction_root_element ($) Line 3373  sub _tree_construction_root_element ($)
3373          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
3374        }        }
3375    
3376      my $root_element; !!!create-element ($root_element, 'html',, $token);      my $root_element;
3377        !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3378      $self->{document}->append_child ($root_element);      $self->{document}->append_child ($root_element);
3379      push @{$self->{open_elements}}, [$root_element, 'html'];      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3380    
3381      $self->{application_cache_selection}->(undef);      $self->{application_cache_selection}->(undef);
3382    
3383      ## NOTE: Reprocess the token.      ## NOTE: Reprocess the token.
3384        !!!ack-later;
3385      return; ## Go to the "before head" insertion mode.      return; ## Go to the "before head" insertion mode.
3386    
3387      ## ISSUE: There is an issue in the spec      ## ISSUE: There is an issue in the spec
# Line 2809  sub _reset_insertion_mode ($) { Line 3405  sub _reset_insertion_mode ($) {
3405        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3406          $last = 1;          $last = 1;
3407          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
3408            if ($self->{inner_html_node}->[1] eq 'td' or            !!!cp ('t28');
3409                $self->{inner_html_node}->[1] eq 'th') {            $node = $self->{inner_html_node};
3410              !!!cp ('t27');          } else {
3411              #            die "_reset_insertion_mode: t27";
           } else {  
             !!!cp ('t28');  
             $node = $self->{inner_html_node};  
           }  
3412          }          }
3413        }        }
3414              
3415        ## Step 4..13        ## Step 4..14
3416        my $new_mode = {        my $new_mode;
3417          if ($node->[1] & FOREIGN_EL) {
3418            !!!cp ('t28.1');
3419            ## NOTE: Strictly spaking, the line below only applies to MathML and
3420            ## SVG elements.  Currently the HTML syntax supports only MathML and
3421            ## SVG elements as foreigners.
3422            $new_mode = $self->{insertion_mode} | IN_FOREIGN_CONTENT_IM;
3423            ## ISSUE: What is set as the secondary insertion mode?
3424          } elsif ($node->[1] & TABLE_CELL_EL) {
3425            if ($last) {
3426              !!!cp ('t28.2');
3427              #
3428            } else {
3429              !!!cp ('t28.3');
3430              $new_mode = IN_CELL_IM;
3431            }
3432          } else {
3433            !!!cp ('t28.4');
3434            $new_mode = {
3435                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
3436                        ## NOTE: |option| and |optgroup| do not set                        ## NOTE: |option| and |optgroup| do not set
3437                        ## insertion mode to "in select" by themselves.                        ## insertion mode to "in select" by themselves.
                       td => IN_CELL_IM,  
                       th => IN_CELL_IM,  
3438                        tr => IN_ROW_IM,                        tr => IN_ROW_IM,
3439                        tbody => IN_TABLE_BODY_IM,                        tbody => IN_TABLE_BODY_IM,
3440                        thead => IN_TABLE_BODY_IM,                        thead => IN_TABLE_BODY_IM,
# Line 2837  sub _reset_insertion_mode ($) { Line 3445  sub _reset_insertion_mode ($) {
3445                        head => IN_BODY_IM, # not in head!                        head => IN_BODY_IM, # not in head!
3446                        body => IN_BODY_IM,                        body => IN_BODY_IM,
3447                        frameset => IN_FRAMESET_IM,                        frameset => IN_FRAMESET_IM,
3448                       }->{$node->[1]};                       }->{$node->[0]->manakai_local_name};
3449          }
3450        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3451                
3452        ## Step 14        ## Step 15
3453        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3454          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3455            !!!cp ('t29');            !!!cp ('t29');
3456            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
# Line 2855  sub _reset_insertion_mode ($) { Line 3464  sub _reset_insertion_mode ($) {
3464          !!!cp ('t31');          !!!cp ('t31');
3465        }        }
3466                
3467        ## Step 15        ## Step 16
3468        $self->{insertion_mode} = IN_BODY_IM and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
3469                
3470        ## Step 16        ## Step 17
3471        $i--;        $i--;
3472        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3473                
3474        ## Step 17        ## Step 18
3475        redo S3;        redo S3;
3476      } # S3      } # S3
3477    
# Line 2974  sub _tree_construction_main ($) { Line 3583  sub _tree_construction_main ($) {
3583      ## Step 1      ## Step 1
3584      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3585      my $el;      my $el;
3586      !!!create-element ($el, $start_tag_name, $token->{attributes}, $token);      !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3587    
3588      ## Step 2      ## Step 2
3589      $insert->($el);      $insert->($el);
# Line 2985  sub _tree_construction_main ($) { Line 3594  sub _tree_construction_main ($) {
3594    
3595      ## Step 4      ## Step 4
3596      my $text = '';      my $text = '';
3597        !!!nack ('t40.1');
3598      !!!next-token;      !!!next-token;
3599      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3600        !!!cp ('t40');        !!!cp ('t40');
# Line 3024  sub _tree_construction_main ($) { Line 3634  sub _tree_construction_main ($) {
3634    
3635    my $script_start_tag = sub () {    my $script_start_tag = sub () {
3636      my $script_el;      my $script_el;
3637      !!!create-element ($script_el, 'script', $token->{attributes}, $token);      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
3638      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3639    
3640      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
3641      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
3642            
3643      my $text = '';      my $text = '';
3644        !!!nack ('t45.1');
3645      !!!next-token;      !!!next-token;
3646      while ($token->{type} == CHARACTER_TOKEN) {      while ($token->{type} == CHARACTER_TOKEN) {
3647        !!!cp ('t45');        !!!cp ('t45');
# Line 3088  sub _tree_construction_main ($) { Line 3699  sub _tree_construction_main ($) {
3699        my $formatting_element;        my $formatting_element;
3700        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3701        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3702          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3703              !!!cp ('t52');
3704              last AFE;
3705            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
3706                         eq $tag_name) {
3707            !!!cp ('t51');            !!!cp ('t51');
3708            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3709            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3710            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           !!!cp ('t52');  
           last AFE;  
3711          }          }
3712        } # AFE        } # AFE
3713        unless (defined $formatting_element) {        unless (defined $formatting_element) {
# Line 3123  sub _tree_construction_main ($) { Line 3735  sub _tree_construction_main ($) {
3735              !!!next-token;              !!!next-token;
3736              return;              return;
3737            }            }
3738          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
                   applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
3739            !!!cp ('t56');            !!!cp ('t56');
3740            $in_scope = 0;            $in_scope = 0;
3741          }          }
# Line 3141  sub _tree_construction_main ($) { Line 3750  sub _tree_construction_main ($) {
3750        }        }
3751        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3752          !!!cp ('t58');          !!!cp ('t58');
3753          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1],          !!!parse-error (type => 'not closed',
3754                            value => $self->{open_elements}->[-1]->[0]
3755                                ->manakai_local_name,
3756                          token => $end_tag_token);                          token => $end_tag_token);
3757        }        }
3758                
# Line 3150  sub _tree_construction_main ($) { Line 3761  sub _tree_construction_main ($) {
3761        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
3762        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3763          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3764          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
3765              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3766              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
3767               $scoping_category->{$node->[1]})) { ## Scoping is redundant, maybe               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
3768            !!!cp ('t59');            !!!cp ('t59');
3769            $furthest_block = $node;            $furthest_block = $node;
3770            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
# Line 3239  sub _tree_construction_main ($) { Line 3850  sub _tree_construction_main ($) {
3850        } # S7          } # S7  
3851                
3852        ## Step 8        ## Step 8
3853        if ({        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
            table => 1, tbody => 1, tfoot => 1, thead => 1, tr => 1,  
           }->{$common_ancestor_node->[1]}) {  
3854          my $foster_parent_element;          my $foster_parent_element;
3855          my $next_sibling;          my $next_sibling;
3856                           OE: for (reverse 0..$#{$self->{open_elements}}) {          OE: for (reverse 0..$#{$self->{open_elements}}) {
3857                             if ($self->{open_elements}->[$_]->[1] eq 'table') {            if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3858                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3859                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3860                                 !!!cp ('t65.1');                                 !!!cp ('t65.1');
# Line 3318  sub _tree_construction_main ($) { Line 3927  sub _tree_construction_main ($) {
3927    
3928    my $insert_to_foster = sub {    my $insert_to_foster = sub {
3929      my $child = shift;      my $child = shift;
3930      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]}) {  
3931        # MUST        # MUST
3932        my $foster_parent_element;        my $foster_parent_element;
3933        my $next_sibling;        my $next_sibling;
3934                           OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3935                             if ($self->{open_elements}->[$_]->[1] eq 'table') {          if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3936                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3937                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3938                                 !!!cp ('t70');                                 !!!cp ('t70');
# Line 3350  sub _tree_construction_main ($) { Line 3957  sub _tree_construction_main ($) {
3957      }      }
3958    }; # $insert_to_foster    }; # $insert_to_foster
3959    
3960    B: {    B: while (1) {
3961      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
3962        !!!cp ('t73');        !!!cp ('t73');
3963        !!!parse-error (type => 'DOCTYPE in the middle', token => $token);        !!!parse-error (type => 'DOCTYPE in the middle', token => $token);
3964        ## Ignore the token        ## Ignore the token
3965        ## Stay in the phase        ## Stay in the phase
3966        !!!next-token;        !!!next-token;
3967        redo B;        next B;
3968      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
3969               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
3970        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
# Line 3383  sub _tree_construction_main ($) { Line 3990  sub _tree_construction_main ($) {
3990               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
3991          }          }
3992        }        }
3993          !!!nack ('t84.1');
3994        !!!next-token;        !!!next-token;
3995        redo B;        next B;
3996      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
3997        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
3998        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
# Line 3398  sub _tree_construction_main ($) { Line 4006  sub _tree_construction_main ($) {
4006          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
4007        }        }
4008        !!!next-token;        !!!next-token;
4009        redo B;        next B;
4010      } elsif ($self->{insertion_mode} & HEAD_IMS) {      } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
4011          if ($token->{type} == CHARACTER_TOKEN) {
4012            !!!cp ('t87.1');
4013            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4014            !!!next-token;
4015            next B;
4016          } elsif ($token->{type} == START_TAG_TOKEN) {
4017            if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
4018                 $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
4019                not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
4020                ($token->{tag_name} eq 'svg' and
4021                 $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
4022              ## NOTE: "using the rules for secondary insertion mode"then"continue"
4023              !!!cp ('t87.2');
4024              #
4025            } elsif ({
4026                      b => 1, big => 1, blockquote => 1, body => 1, br => 1,
4027                      center => 1, code => 1, dd => 1, div => 1, dl => 1, em => 1,
4028                      embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1, ## No h4!
4029                      h5 => 1, h6 => 1, head => 1, hr => 1, i => 1, img => 1,
4030                      li => 1, menu => 1, meta => 1, nobr => 1, p => 1, pre => 1,
4031                      ruby => 1, s => 1, small => 1, span => 1, strong => 1,
4032                      sub => 1, sup => 1, table => 1, tt => 1, u => 1, ul => 1,
4033                      var => 1,
4034                     }->{$token->{tag_name}}) {
4035              !!!cp ('t87.2');
4036              !!!parse-error (type => 'not closed',
4037                              value => $self->{open_elements}->[-1]->[0]
4038                                  ->manakai_local_name,
4039                              token => $token);
4040    
4041              pop @{$self->{open_elements}}
4042                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4043    
4044              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4045              ## Reprocess.
4046              next B;
4047            } else {
4048              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
4049              my $tag_name = $token->{tag_name};
4050              if ($nsuri eq $SVG_NS) {
4051                $tag_name = {
4052                   altglyph => 'altGlyph',
4053                   altglyphdef => 'altGlyphDef',
4054                   altglyphitem => 'altGlyphItem',
4055                   animatecolor => 'animateColor',
4056                   animatemotion => 'animateMotion',
4057                   animatetransform => 'animateTransform',
4058                   clippath => 'clipPath',
4059                   feblend => 'feBlend',
4060                   fecolormatrix => 'feColorMatrix',
4061                   fecomponenttransfer => 'feComponentTransfer',
4062                   fecomposite => 'feComposite',
4063                   feconvolvematrix => 'feConvolveMatrix',
4064                   fediffuselighting => 'feDiffuseLighting',
4065                   fedisplacementmap => 'feDisplacementMap',
4066                   fedistantlight => 'feDistantLight',
4067                   feflood => 'feFlood',
4068                   fefunca => 'feFuncA',
4069                   fefuncb => 'feFuncB',
4070                   fefuncg => 'feFuncG',
4071                   fefuncr => 'feFuncR',
4072                   fegaussianblur => 'feGaussianBlur',
4073                   feimage => 'feImage',
4074                   femerge => 'feMerge',
4075                   femergenode => 'feMergeNode',
4076                   femorphology => 'feMorphology',
4077                   feoffset => 'feOffset',
4078                   fepointlight => 'fePointLight',
4079                   fespecularlighting => 'feSpecularLighting',
4080                   fespotlight => 'feSpotLight',
4081                   fetile => 'feTile',
4082                   feturbulence => 'feTurbulence',
4083                   foreignobject => 'foreignObject',
4084                   glyphref => 'glyphRef',
4085                   lineargradient => 'linearGradient',
4086                   radialgradient => 'radialGradient',
4087                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4088                   textpath => 'textPath',  
4089                }->{$tag_name} || $tag_name;
4090              }
4091    
4092              ## "adjust SVG attributes" (SVG only) - done in insert-element-f
4093    
4094              ## "adjust foreign attributes" - done in insert-element-f
4095    
4096              !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
4097    
4098              if ($self->{self_closing}) {
4099                pop @{$self->{open_elements}};
4100                !!!ack ('t87.3');
4101              } else {
4102                !!!cp ('t87.4');
4103              }
4104    
4105              !!!next-token;
4106              next B;
4107            }
4108          } elsif ($token->{type} == END_TAG_TOKEN) {
4109            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4110            !!!cp ('t87.5');
4111            #
4112          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4113            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4114            !!!cp ('t87.6');
4115            #
4116            ## TODO: ...
4117          } else {
4118            die "$0: $token->{type}: Unknown token type";        
4119          }
4120        }
4121    
4122        if ($self->{insertion_mode} & HEAD_IMS) {
4123        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4124          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4125            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
# Line 3409  sub _tree_construction_main ($) { Line 4129  sub _tree_construction_main ($) {
4129              !!!cp ('t88.1');              !!!cp ('t88.1');
4130              ## Ignore the token.              ## Ignore the token.
4131              !!!next-token;              !!!next-token;
4132              redo B;              next B;
4133            }            }
4134            unless (length $token->{data}) {            unless (length $token->{data}) {
4135              !!!cp ('t88');              !!!cp ('t88');
4136              !!!next-token;              !!!next-token;
4137              redo B;              next B;
4138            }            }
4139          }          }
4140    
4141          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4142            !!!cp ('t89');            !!!cp ('t89');
4143            ## As if <head>            ## As if <head>
4144            !!!create-element ($self->{head_element}, 'head',, $token);            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4145            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4146            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            push @{$self->{open_elements}},
4147                  [$self->{head_element}, $el_category->{head}];
4148    
4149            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4150            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
# Line 3449  sub _tree_construction_main ($) { Line 4170  sub _tree_construction_main ($) {
4170            !!!cp ('t92');            !!!cp ('t92');
4171          }          }
4172    
4173              ## "after head" insertion mode          ## "after head" insertion mode
4174              ## As if <body>          ## As if <body>
4175              !!!insert-element ('body',, $token);          !!!insert-element ('body',, $token);
4176              $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4177              ## reprocess          ## reprocess
4178              redo B;          next B;
4179            } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4180              if ($token->{tag_name} eq 'head') {          if ($token->{tag_name} eq 'head') {
4181                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4182                  !!!cp ('t93');              !!!cp ('t93');
4183                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes}, $token);              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4184                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              $self->{open_elements}->[-1]->[0]->append_child
4185                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];                  ($self->{head_element});
4186                  $self->{insertion_mode} = IN_HEAD_IM;              push @{$self->{open_elements}},
4187                  !!!next-token;                  [$self->{head_element}, $el_category->{head}];
4188                  redo B;              $self->{insertion_mode} = IN_HEAD_IM;
4189                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {              !!!nack ('t93.1');
4190                  !!!cp ('t94');              !!!next-token;
4191                  #              next B;
4192                } else {            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4193                  !!!cp ('t95');              !!!cp ('t93.2');
4194                  !!!parse-error (type => 'in head:head', token => $token); # or in head noscript              !!!parse-error (type => 'after head:head', token => $token); ## TODO: error type
4195                  ## Ignore the token              ## Ignore the token
4196                  !!!next-token;              !!!nack ('t93.3');
4197                  redo B;              !!!next-token;
4198                }              next B;
4199              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {            } else {
4200                !!!cp ('t96');              !!!cp ('t95');
4201                ## As if <head>              !!!parse-error (type => 'in head:head', token => $token); # or in head noscript
4202                !!!create-element ($self->{head_element}, 'head',, $token);              ## Ignore the token
4203                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              !!!nack ('t95.1');
4204                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];              !!!next-token;
4205                next B;
4206              }
4207            } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4208              !!!cp ('t96');
4209              ## As if <head>
4210              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4211              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4212              push @{$self->{open_elements}},
4213                  [$self->{head_element}, $el_category->{head}];
4214    
4215                $self->{insertion_mode} = IN_HEAD_IM;            $self->{insertion_mode} = IN_HEAD_IM;
4216                ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4217              } else {          } else {
4218                !!!cp ('t97');            !!!cp ('t97');
4219              }          }
4220    
4221              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
4222                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
# Line 3505  sub _tree_construction_main ($) { Line 4235  sub _tree_construction_main ($) {
4235                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4236                  !!!cp ('t100');                  !!!cp ('t100');
4237                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4238                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4239                        [$self->{head_element}, $el_category->{head}];
4240                } else {                } else {
4241                  !!!cp ('t101');                  !!!cp ('t101');
4242                }                }
# Line 3513  sub _tree_construction_main ($) { Line 4244  sub _tree_construction_main ($) {
4244                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4245                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4246                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4247                  !!!nack ('t101.1');
4248                !!!next-token;                !!!next-token;
4249                redo B;                next B;
4250              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
4251                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4252                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4253                  !!!cp ('t102');                  !!!cp ('t102');
4254                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4255                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4256                        [$self->{head_element}, $el_category->{head}];
4257                } else {                } else {
4258                  !!!cp ('t103');                  !!!cp ('t103');
4259                }                }
# Line 3528  sub _tree_construction_main ($) { Line 4261  sub _tree_construction_main ($) {
4261                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4262                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4263                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4264                  !!!ack ('t103.1');
4265                !!!next-token;                !!!next-token;
4266                redo B;                next B;
4267              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
4268                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4269                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4270                  !!!cp ('t104');                  !!!cp ('t104');
4271                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4272                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4273                        [$self->{head_element}, $el_category->{head}];
4274                } else {                } else {
4275                  !!!cp ('t105');                  !!!cp ('t105');
4276                }                }
# Line 3543  sub _tree_construction_main ($) { Line 4278  sub _tree_construction_main ($) {
4278                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.
4279    
4280                unless ($self->{confident}) {                unless ($self->{confident}) {
4281                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) {
4282                    !!!cp ('t106');                    !!!cp ('t106');
4283                      ## NOTE: Whether the encoding is supported or not is handled
4284                      ## in the {change_encoding} callback.
4285                    $self->{change_encoding}                    $self->{change_encoding}
4286                        ->($self, $token->{attributes}->{charset}->{value},                        ->($self, $token->{attributes}->{charset}->{value},
4287                           $token);                           $token);
# Line 3554  sub _tree_construction_main ($) { Line 4291  sub _tree_construction_main ($) {
4291                                             $token->{attributes}->{charset}                                             $token->{attributes}->{charset}
4292                                                 ->{has_reference});                                                 ->{has_reference});
4293                  } elsif ($token->{attributes}->{content}) {                  } elsif ($token->{attributes}->{content}) {
                   ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
4294                    if ($token->{attributes}->{content}->{value}                    if ($token->{attributes}->{content}->{value}
4295                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                        =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4296                            [\x09-\x0D\x20]*=                            [\x09-\x0D\x20]*=
4297                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4298                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
4299                      !!!cp ('t107');                      !!!cp ('t107');
4300                        ## NOTE: Whether the encoding is supported or not is handled
4301                        ## in the {change_encoding} callback.
4302                      $self->{change_encoding}                      $self->{change_encoding}
4303                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4304                             $token);                             $token);
# Line 3591  sub _tree_construction_main ($) { Line 4329  sub _tree_construction_main ($) {
4329    
4330                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4331                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4332                  !!!ack ('t110.1');
4333                !!!next-token;                !!!next-token;
4334                redo B;                next B;
4335              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
4336                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4337                  !!!cp ('t111');                  !!!cp ('t111');
# Line 3605  sub _tree_construction_main ($) { Line 4344  sub _tree_construction_main ($) {
4344                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4345                  !!!cp ('t112');                  !!!cp ('t112');
4346                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4347                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4348                        [$self->{head_element}, $el_category->{head}];
4349                } else {                } else {
4350                  !!!cp ('t113');                  !!!cp ('t113');
4351                }                }
# Line 3616  sub _tree_construction_main ($) { Line 4356  sub _tree_construction_main ($) {
4356                $parse_rcdata->(RCDATA_CONTENT_MODEL);                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4357                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4358                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4359                redo B;                next B;
4360              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style') {
4361                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4362                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
# Line 3624  sub _tree_construction_main ($) { Line 4364  sub _tree_construction_main ($) {
4364                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4365                  !!!cp ('t114');                  !!!cp ('t114');
4366                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4367                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4368                        [$self->{head_element}, $el_category->{head}];
4369                } else {                } else {
4370                  !!!cp ('t115');                  !!!cp ('t115');
4371                }                }
4372                $parse_rcdata->(CDATA_CONTENT_MODEL);                $parse_rcdata->(CDATA_CONTENT_MODEL);
4373                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4374                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4375                redo B;                next B;
4376              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4377                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
4378                  !!!cp ('t116');                  !!!cp ('t116');
4379                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
4380                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4381                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4382                    !!!nack ('t116.1');
4383                  !!!next-token;                  !!!next-token;
4384                  redo B;                  next B;
4385                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4386                  !!!cp ('t117');                  !!!cp ('t117');
4387                  !!!parse-error (type => 'in noscript:noscript', token => $token);                  !!!parse-error (type => 'in noscript:noscript', token => $token);
4388                  ## Ignore the token                  ## Ignore the token
4389                    !!!nack ('t117.1');
4390                  !!!next-token;                  !!!next-token;
4391                  redo B;                  next B;
4392                } else {                } else {
4393                  !!!cp ('t118');                  !!!cp ('t118');
4394                  #                  #
# Line 3662  sub _tree_construction_main ($) { Line 4405  sub _tree_construction_main ($) {
4405                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4406                  !!!cp ('t120');                  !!!cp ('t120');
4407                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4408                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4409                        [$self->{head_element}, $el_category->{head}];
4410                } else {                } else {
4411                  !!!cp ('t121');                  !!!cp ('t121');
4412                }                }
# Line 3671  sub _tree_construction_main ($) { Line 4415  sub _tree_construction_main ($) {
4415                $script_start_tag->();                $script_start_tag->();
4416                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4417                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4418                redo B;                next B;
4419              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
4420                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
4421                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
# Line 3705  sub _tree_construction_main ($) { Line 4449  sub _tree_construction_main ($) {
4449                } else {                } else {
4450                  die "$0: tag name: $self->{tag_name}";                  die "$0: tag name: $self->{tag_name}";
4451                }                }
4452                  !!!nack ('t127.1');
4453                !!!next-token;                !!!next-token;
4454                redo B;                next B;
4455              } else {              } else {
4456                !!!cp ('t128');                !!!cp ('t128');
4457                #                #
# Line 3738  sub _tree_construction_main ($) { Line 4483  sub _tree_construction_main ($) {
4483              !!!insert-element ('body',, $token);              !!!insert-element ('body',, $token);
4484              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4485              ## reprocess              ## reprocess
4486              redo B;              !!!ack-later;
4487                next B;
4488            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
4489              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
4490                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4491                  !!!cp ('t132');                  !!!cp ('t132');
4492                  ## As if <head>                  ## As if <head>
4493                  !!!create-element ($self->{head_element}, 'head',, $token);                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4494                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4495                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4496                        [$self->{head_element}, $el_category->{head}];
4497    
4498                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4499                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4500                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4501                  !!!next-token;                  !!!next-token;
4502                  redo B;                  next B;
4503                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4504                  !!!cp ('t133');                  !!!cp ('t133');
4505                  ## As if </noscript>                  ## As if </noscript>
# Line 3763  sub _tree_construction_main ($) { Line 4510  sub _tree_construction_main ($) {
4510                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4511                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4512                  !!!next-token;                  !!!next-token;
4513                  redo B;                  next B;
4514                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4515                  !!!cp ('t134');                  !!!cp ('t134');
4516                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4517                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4518                  !!!next-token;                  !!!next-token;
4519                  redo B;                  next B;
4520                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4521                    !!!cp ('t134.1');
4522                    !!!parse-error (type => 'unmatched end tag:head', token => $token);
4523                    ## Ignore the token
4524                    !!!next-token;
4525                    next B;
4526                } else {                } else {
4527                  !!!cp ('t135');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
                 #  
4528                }                }
4529              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4530                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
# Line 3780  sub _tree_construction_main ($) { Line 4532  sub _tree_construction_main ($) {
4532                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4533                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4534                  !!!next-token;                  !!!next-token;
4535                  redo B;                  next B;
4536                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
4537                           $self->{insertion_mode} == AFTER_HEAD_IM) {
4538                  !!!cp ('t137');                  !!!cp ('t137');
4539                  !!!parse-error (type => 'unmatched end tag:noscript', token => $token);                  !!!parse-error (type => 'unmatched end tag:noscript', token => $token);
4540                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
4541                  !!!next-token;                  !!!next-token;
4542                  redo B;                  next B;
4543                } else {                } else {
4544                  !!!cp ('t138');                  !!!cp ('t138');
4545                  #                  #
# Line 3794  sub _tree_construction_main ($) { Line 4547  sub _tree_construction_main ($) {
4547              } elsif ({              } elsif ({
4548                        body => 1, html => 1,                        body => 1, html => 1,
4549                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4550                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM or
4551                  !!!cp ('t139');                    $self->{insertion_mode} == IN_HEAD_IM or
4552                  ## As if <head>                    $self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
                 !!!create-element ($self->{head_element}, 'head',, $token);  
                 $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});  
                 push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
   
                 $self->{insertion_mode} = IN_HEAD_IM;  
                 ## Reprocess in the "in head" insertion mode...  
               } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {  
4553                  !!!cp ('t140');                  !!!cp ('t140');
4554                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4555                  ## Ignore the token                  ## Ignore the token
4556                  !!!next-token;                  !!!next-token;
4557                  redo B;                  next B;
4558                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4559                    !!!cp ('t140.1');
4560                    !!!parse-error (type => 'unmatched end tag:' . $token->{tag_name}, token => $token);
4561                    ## Ignore the token
4562                    !!!next-token;
4563                    next B;
4564                } else {                } else {
4565                  !!!cp ('t141');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
4566                }                }
4567                              } elsif ($token->{tag_name} eq 'p') {
4568                #                !!!cp ('t142');
4569              } elsif ({                !!!parse-error (type => 'unmatched end tag:p', token => $token);
4570                        p => 1, br => 1,                ## Ignore the token
4571                       }->{$token->{tag_name}}) {                !!!next-token;
4572                  next B;
4573                } elsif ($token->{tag_name} eq 'br') {
4574                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4575                  !!!cp ('t142');                  !!!cp ('t142.2');
4576                  ## As if <head>                  ## (before head) as if <head>, (in head) as if </head>
4577                  !!!create-element ($self->{head_element}, 'head',, $token);                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4578                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4579                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  $self->{insertion_mode} = AFTER_HEAD_IM;
4580      
4581                    ## Reprocess in the "after head" insertion mode...
4582                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4583                    !!!cp ('t143.2');
4584                    ## As if </head>
4585                    pop @{$self->{open_elements}};
4586                    $self->{insertion_mode} = AFTER_HEAD_IM;
4587      
4588                    ## Reprocess in the "after head" insertion mode...
4589                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4590                    !!!cp ('t143.3');
4591                    ## ISSUE: Two parse errors for <head><noscript></br>
4592                    !!!parse-error (type => 'unmatched end tag:br', token => $token);
4593                    ## As if </noscript>
4594                    pop @{$self->{open_elements}};
4595                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4596    
4597                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4598                } else {                  ## As if </head>
4599                  !!!cp ('t143');                  pop @{$self->{open_elements}};
4600                }                  $self->{insertion_mode} = AFTER_HEAD_IM;
4601    
4602                #                  ## Reprocess in the "after head" insertion mode...
4603              } else {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4604                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                  !!!cp ('t143.4');
                 !!!cp ('t144');  
4605                  #                  #
4606                } else {                } else {
4607                  !!!cp ('t145');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
4608                }                }
4609    
4610                  ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
4611                  !!!parse-error (type => 'unmatched end tag:br', token => $token);
4612                  ## Ignore the token
4613                  !!!next-token;
4614                  next B;
4615                } else {
4616                  !!!cp ('t145');
4617                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4618                  ## Ignore the token
4619                  !!!next-token;
4620                  next B;
4621              }              }
4622    
4623              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
# Line 3867  sub _tree_construction_main ($) { Line 4643  sub _tree_construction_main ($) {
4643                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4644                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
4645                !!!next-token;                !!!next-token;
4646                redo B;                next B;
4647              } else {              } else {
4648                !!!cp ('t149');                !!!cp ('t149');
4649              }              }
# Line 3877  sub _tree_construction_main ($) { Line 4653  sub _tree_construction_main ($) {
4653              !!!insert-element ('body',, $token);              !!!insert-element ('body',, $token);
4654              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4655              ## reprocess              ## reprocess
4656              redo B;              next B;
4657        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4658          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4659            !!!cp ('t149.1');            !!!cp ('t149.1');
4660    
4661            ## NOTE: As if <head>            ## NOTE: As if <head>
4662            !!!create-element ($self->{head_element}, 'head',, $token);            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4663            $self->{open_elements}->[-1]->[0]->append_child            $self->{open_elements}->[-1]->[0]->append_child
4664                ($self->{head_element});                ($self->{head_element});
4665            #push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            #push @{$self->{open_elements}},
4666              #    [$self->{head_element}, $el_category->{head}];
4667            #$self->{insertion_mode} = IN_HEAD_IM;            #$self->{insertion_mode} = IN_HEAD_IM;
4668            ## NOTE: Reprocess.            ## NOTE: Reprocess.
4669    
# Line 3930  sub _tree_construction_main ($) { Line 4707  sub _tree_construction_main ($) {
4707          !!!insert-element ('body',, $token);          !!!insert-element ('body',, $token);
4708          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4709          ## NOTE: Reprocess.          ## NOTE: Reprocess.
4710          redo B;          next B;
4711        } else {        } else {
4712          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
4713        }        }
# Line 3945  sub _tree_construction_main ($) { Line 4722  sub _tree_construction_main ($) {
4722              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4723    
4724              !!!next-token;              !!!next-token;
4725              redo B;              next B;
4726            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
4727              if ({              if ({
4728                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
# Line 3955  sub _tree_construction_main ($) { Line 4732  sub _tree_construction_main ($) {
4732                  ## have an element in table scope                  ## have an element in table scope
4733                  for (reverse 0..$#{$self->{open_elements}}) {                  for (reverse 0..$#{$self->{open_elements}}) {
4734                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4735                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] & TABLE_CELL_EL) {
4736                      !!!cp ('t151');                      !!!cp ('t151');
4737    
4738                      ## Close the cell                      ## Close the cell
4739                      !!!back-token; # <?>                      !!!back-token; # <x>
4740                      $token = {type => END_TAG_TOKEN, tag_name => $node->[1],                      $token = {type => END_TAG_TOKEN,
4741                                  tag_name => $node->[0]->manakai_local_name,
4742                                line => $token->{line},                                line => $token->{line},
4743                                column => $token->{column}};                                column => $token->{column}};
4744                      redo B;                      next B;
4745                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
4746                      !!!cp ('t152');                      !!!cp ('t152');
4747                      ## ISSUE: This case can never be reached, maybe.                      ## ISSUE: This case can never be reached, maybe.
4748                      last;                      last;
# Line 3977  sub _tree_construction_main ($) { Line 4753  sub _tree_construction_main ($) {
4753                  !!!parse-error (type => 'start tag not allowed',                  !!!parse-error (type => 'start tag not allowed',
4754                      value => $token->{tag_name}, token => $token);                      value => $token->{tag_name}, token => $token);
4755                  ## Ignore the token                  ## Ignore the token
4756                    !!!nack ('t153.1');
4757                  !!!next-token;                  !!!next-token;
4758                  redo B;                  next B;
4759                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4760                  !!!parse-error (type => 'not closed:caption', token => $token);                  !!!parse-error (type => 'not closed:caption', token => $token);
4761                                    
# Line 3988  sub _tree_construction_main ($) { Line 4765  sub _tree_construction_main ($) {
4765                  INSCOPE: {                  INSCOPE: {
4766                    for (reverse 0..$#{$self->{open_elements}}) {                    for (reverse 0..$#{$self->{open_elements}}) {
4767                      my $node = $self->{open_elements}->[$_];                      my $node = $self->{open_elements}->[$_];
4768                      if ($node->[1] eq 'caption') {                      if ($node->[1] & CAPTION_EL) {
4769                        !!!cp ('t155');                        !!!cp ('t155');
4770                        $i = $_;                        $i = $_;
4771                        last INSCOPE;                        last INSCOPE;
4772                      } elsif ({                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
                               table => 1, html => 1,  
                              }->{$node->[1]}) {  
4773                        !!!cp ('t156');                        !!!cp ('t156');
4774                        last;                        last;
4775                      }                      }
# Line 4004  sub _tree_construction_main ($) { Line 4779  sub _tree_construction_main ($) {
4779                    !!!parse-error (type => 'start tag not allowed',                    !!!parse-error (type => 'start tag not allowed',
4780                                    value => $token->{tag_name}, token => $token);                                    value => $token->{tag_name}, token => $token);
4781                    ## Ignore the token                    ## Ignore the token
4782                      !!!nack ('t157.1');
4783                    !!!next-token;                    !!!next-token;
4784                    redo B;                    next B;
4785                  } # INSCOPE                  } # INSCOPE
4786                                    
4787                  ## generate implied end tags                  ## generate implied end tags
4788                  while ({                  while ($self->{open_elements}->[-1]->[1]
4789                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4790                    !!!cp ('t158');                    !!!cp ('t158');
4791                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4792                  }                  }
4793    
4794                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4795                    !!!cp ('t159');                    !!!cp ('t159');
4796                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                    !!!parse-error (type => 'not closed',
4797                                      value => $self->{open_elements}->[-1]->[0]
4798                                          ->manakai_local_name,
4799                                      token => $token);
4800                  } else {                  } else {
4801                    !!!cp ('t160');                    !!!cp ('t160');
4802                  }                  }
# Line 4030  sub _tree_construction_main ($) { Line 4808  sub _tree_construction_main ($) {
4808                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4809                                    
4810                  ## reprocess                  ## reprocess
4811                  redo B;                  !!!ack-later;
4812                    next B;
4813                } else {                } else {
4814                  !!!cp ('t161');                  !!!cp ('t161');
4815                  #                  #
# Line 4046  sub _tree_construction_main ($) { Line 4825  sub _tree_construction_main ($) {
4825                  my $i;                  my $i;
4826                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4827                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4828                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4829                      !!!cp ('t163');                      !!!cp ('t163');
4830                      $i = $_;                      $i = $_;
4831                      last INSCOPE;                      last INSCOPE;
4832                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
4833                      !!!cp ('t164');                      !!!cp ('t164');
4834                      last INSCOPE;                      last INSCOPE;
4835                    }                    }
# Line 4062  sub _tree_construction_main ($) { Line 4839  sub _tree_construction_main ($) {
4839                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4840                      ## Ignore the token                      ## Ignore the token
4841                      !!!next-token;                      !!!next-token;
4842                      redo B;                      next B;
4843                    }                    }
4844                                    
4845                  ## generate implied end tags                  ## generate implied end tags
4846                  while ({                  while ($self->{open_elements}->[-1]->[1]
4847                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4848                    !!!cp ('t166');                    !!!cp ('t166');
4849                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4850                  }                  }
4851    
4852                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
4853                            ne $token->{tag_name}) {
4854                    !!!cp ('t167');                    !!!cp ('t167');
4855                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                    !!!parse-error (type => 'not closed',
4856                                      value => $self->{open_elements}->[-1]->[0]
4857                                          ->manakai_local_name,
4858                                      token => $token);
4859                  } else {                  } else {
4860                    !!!cp ('t168');                    !!!cp ('t168');
4861                  }                  }
# Line 4087  sub _tree_construction_main ($) { Line 4867  sub _tree_construction_main ($) {
4867                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
4868                                    
4869                  !!!next-token;                  !!!next-token;
4870                  redo B;                  next B;
4871                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4872                  !!!cp ('t169');                  !!!cp ('t169');
4873                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4874                  ## Ignore the token                  ## Ignore the token
4875                  !!!next-token;                  !!!next-token;
4876                  redo B;                  next B;
4877                } else {                } else {
4878                  !!!cp ('t170');                  !!!cp ('t170');
4879                  #                  #
# Line 4105  sub _tree_construction_main ($) { Line 4885  sub _tree_construction_main ($) {
4885                  INSCOPE: {                  INSCOPE: {
4886                    for (reverse 0..$#{$self->{open_elements}}) {                    for (reverse 0..$#{$self->{open_elements}}) {
4887                      my $node = $self->{open_elements}->[$_];                      my $node = $self->{open_elements}->[$_];
4888                      if ($node->[1] eq $token->{tag_name}) {                      if ($node->[1] & CAPTION_EL) {
4889                        !!!cp ('t171');                        !!!cp ('t171');
4890                        $i = $_;                        $i = $_;
4891                        last INSCOPE;                        last INSCOPE;
4892                      } elsif ({                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
                               table => 1, html => 1,  
                              }->{$node->[1]}) {  
4893                        !!!cp ('t172');                        !!!cp ('t172');
4894                        last;                        last;
4895                      }                      }
# Line 4122  sub _tree_construction_main ($) { Line 4900  sub _tree_construction_main ($) {
4900                                    value => $token->{tag_name}, token => $token);                                    value => $token->{tag_name}, token => $token);
4901                    ## Ignore the token                    ## Ignore the token
4902                    !!!next-token;                    !!!next-token;
4903                    redo B;                    next B;
4904                  } # INSCOPE                  } # INSCOPE
4905                                    
4906                  ## generate implied end tags                  ## generate implied end tags
4907                  while ({                  while ($self->{open_elements}->[-1]->[1]
4908                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4909                    !!!cp ('t174');                    !!!cp ('t174');
4910                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4911                  }                  }
4912                                    
4913                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4914                    !!!cp ('t175');                    !!!cp ('t175');
4915                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                    !!!parse-error (type => 'not closed',
4916                                      value => $self->{open_elements}->[-1]->[0]
4917                                          ->manakai_local_name,
4918                                      token => $token);
4919                  } else {                  } else {
4920                    !!!cp ('t176');                    !!!cp ('t176');
4921                  }                  }
# Line 4147  sub _tree_construction_main ($) { Line 4927  sub _tree_construction_main ($) {
4927                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4928                                    
4929                  !!!next-token;                  !!!next-token;
4930                  redo B;                  next B;
4931                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
4932                  !!!cp ('t177');                  !!!cp ('t177');
4933                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4934                  ## Ignore the token                  ## Ignore the token
4935                  !!!next-token;                  !!!next-token;
4936                  redo B;                  next B;
4937                } else {                } else {
4938                  !!!cp ('t178');                  !!!cp ('t178');
4939                  #                  #
# Line 4169  sub _tree_construction_main ($) { Line 4949  sub _tree_construction_main ($) {
4949                INSCOPE: {                INSCOPE: {
4950                  for (reverse 0..$#{$self->{open_elements}}) {                  for (reverse 0..$#{$self->{open_elements}}) {
4951                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4952                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4953                      !!!cp ('t179');                      !!!cp ('t179');
4954                      $i = $_;                      $i = $_;
4955    
4956                      ## Close the cell                      ## Close the cell
4957                      !!!back-token; # </?>                      !!!back-token; # </x>
4958                      $token = {type => END_TAG_TOKEN, tag_name => $tn,                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
4959                                line => $token->{line},                                line => $token->{line},
4960                                column => $token->{column}};                                column => $token->{column}};
4961                      redo B;                      next B;
4962                    } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {                    } elsif ($node->[1] & TABLE_CELL_EL) {
4963                      !!!cp ('t180');                      !!!cp ('t180');
4964                      $tn = $node->[1];                      $tn = $node->[0]->manakai_local_name;
4965                      ## NOTE: There is exactly one |td| or |th| element                      ## NOTE: There is exactly one |td| or |th| element
4966                      ## in scope in the stack of open elements by definition.                      ## in scope in the stack of open elements by definition.
4967                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
4968                      ## ISSUE: Can this be reached?                      ## ISSUE: Can this be reached?
4969                      !!!cp ('t181');                      !!!cp ('t181');
4970                      last;                      last;
# Line 4198  sub _tree_construction_main ($) { Line 4976  sub _tree_construction_main ($) {
4976                      value => $token->{tag_name}, token => $token);                      value => $token->{tag_name}, token => $token);
4977                  ## Ignore the token                  ## Ignore the token
4978                  !!!next-token;                  !!!next-token;
4979                  redo B;                  next B;
4980                } # INSCOPE                } # INSCOPE
4981              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
4982                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
# Line 4209  sub _tree_construction_main ($) { Line 4987  sub _tree_construction_main ($) {
4987                my $i;                my $i;
4988                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4989                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4990                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
4991                    !!!cp ('t184');                    !!!cp ('t184');
4992                    $i = $_;                    $i = $_;
4993                    last INSCOPE;                    last INSCOPE;
4994                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
4995                    !!!cp ('t185');                    !!!cp ('t185');
4996                    last INSCOPE;                    last INSCOPE;
4997                  }                  }
# Line 4225  sub _tree_construction_main ($) { Line 5001  sub _tree_construction_main ($) {
5001                  !!!parse-error (type => 'unmatched end tag:caption', token => $token);                  !!!parse-error (type => 'unmatched end tag:caption', token => $token);
5002                  ## Ignore the token                  ## Ignore the token
5003                  !!!next-token;                  !!!next-token;
5004                  redo B;                  next B;
5005                }                }
5006                                
5007                ## generate implied end tags                ## generate implied end tags
5008                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
5009                  !!!cp ('t187');                  !!!cp ('t187');
5010                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5011                }                }
5012    
5013                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5014                  !!!cp ('t188');                  !!!cp ('t188');
5015                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                  !!!parse-error (type => 'not closed',
5016                                    value => $self->{open_elements}->[-1]->[0]
5017                                        ->manakai_local_name,
5018                                    token => $token);
5019                } else {                } else {
5020                  !!!cp ('t189');                  !!!cp ('t189');
5021                }                }
# Line 4250  sub _tree_construction_main ($) { Line 5027  sub _tree_construction_main ($) {
5027                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5028    
5029                ## reprocess                ## reprocess
5030                redo B;                next B;
5031              } elsif ({              } elsif ({
5032                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
5033                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
# Line 4259  sub _tree_construction_main ($) { Line 5036  sub _tree_construction_main ($) {
5036                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5037                  ## Ignore the token                  ## Ignore the token
5038                  !!!next-token;                  !!!next-token;
5039                  redo B;                  next B;
5040                } else {                } else {
5041                  !!!cp ('t191');                  !!!cp ('t191');
5042                  #                  #
# Line 4273  sub _tree_construction_main ($) { Line 5050  sub _tree_construction_main ($) {
5050                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5051                ## Ignore the token                ## Ignore the token
5052                !!!next-token;                !!!next-token;
5053                redo B;                next B;
5054              } else {              } else {
5055                !!!cp ('t193');                !!!cp ('t193');
5056                #                #
5057              }              }
5058        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5059          for my $entry (@{$self->{open_elements}}) {          for my $entry (@{$self->{open_elements}}) {
5060            if (not {            unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
             dd => 1, dt => 1, li => 1, p => 1, tbody => 1, td => 1, tfoot => 1,  
             th => 1, thead => 1, tr => 1, body => 1, html => 1,  
           }->{$entry->[1]}) {  
5061              !!!cp ('t75');              !!!cp ('t75');
5062              !!!parse-error (type => 'in body:#eof', token => $token);              !!!parse-error (type => 'in body:#eof', token => $token);
5063              last;              last;
# Line 4307  sub _tree_construction_main ($) { Line 5081  sub _tree_construction_main ($) {
5081            unless (length $token->{data}) {            unless (length $token->{data}) {
5082              !!!cp ('t194');              !!!cp ('t194');
5083              !!!next-token;              !!!next-token;
5084              redo B;              next B;
5085            } else {            } else {
5086              !!!cp ('t195');              !!!cp ('t195');
5087            }            }
# Line 4321  sub _tree_construction_main ($) { Line 5095  sub _tree_construction_main ($) {
5095              ## result in a new Text node.              ## result in a new Text node.
5096              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
5097                            
5098              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]}) {  
5099                # MUST                # MUST
5100                my $foster_parent_element;                my $foster_parent_element;
5101                my $next_sibling;                my $next_sibling;
5102                my $prev_sibling;                my $prev_sibling;
5103                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
5104                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
5105                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
5106                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
5107                      !!!cp ('t196');                      !!!cp ('t196');
# Line 4365  sub _tree_construction_main ($) { Line 5136  sub _tree_construction_main ($) {
5136          }          }
5137                            
5138          !!!next-token;          !!!next-token;
5139          redo B;          next B;
5140        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5141              if ({              if ({
5142                   tr => ($self->{insertion_mode} != IN_ROW_IM),                   tr => ($self->{insertion_mode} != IN_ROW_IM),
# Line 4373  sub _tree_construction_main ($) { Line 5144  sub _tree_construction_main ($) {
5144                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
5145                if ($self->{insertion_mode} == IN_TABLE_IM) {                if ($self->{insertion_mode} == IN_TABLE_IM) {
5146                  ## Clear back to table context                  ## Clear back to table context
5147                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5148                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5149                    !!!cp ('t201');                    !!!cp ('t201');
5150                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5151                  }                  }
# Line 4391  sub _tree_construction_main ($) { Line 5162  sub _tree_construction_main ($) {
5162                  }                  }
5163                                    
5164                  ## Clear back to table body context                  ## Clear back to table body context
5165                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5166                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5167                    !!!cp ('t203');                    !!!cp ('t203');
5168                    ## ISSUE: Can this case be reached?                    ## ISSUE: Can this case be reached?
5169                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4403  sub _tree_construction_main ($) { Line 5173  sub _tree_construction_main ($) {
5173                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5174                    !!!cp ('t204');                    !!!cp ('t204');
5175                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5176                      !!!nack ('t204');
5177                    !!!next-token;                    !!!next-token;
5178                    redo B;                    next B;
5179                  } else {                  } else {
5180                    !!!cp ('t205');                    !!!cp ('t205');
5181                    !!!insert-element ('tr',, $token);                    !!!insert-element ('tr',, $token);
# Line 4415  sub _tree_construction_main ($) { Line 5186  sub _tree_construction_main ($) {
5186                }                }
5187    
5188                ## Clear back to table row context                ## Clear back to table row context
5189                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5190                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5191                  !!!cp ('t207');                  !!!cp ('t207');
5192                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5193                }                }
# Line 4427  sub _tree_construction_main ($) { Line 5197  sub _tree_construction_main ($) {
5197    
5198                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
5199                                
5200                  !!!nack ('t207.1');
5201                !!!next-token;                !!!next-token;
5202                redo B;                next B;
5203              } elsif ({              } elsif ({
5204                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5205                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
# Line 4440  sub _tree_construction_main ($) { Line 5211  sub _tree_construction_main ($) {
5211                  my $i;                  my $i;
5212                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5213                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5214                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5215                      !!!cp ('t208');                      !!!cp ('t208');
5216                      $i = $_;                      $i = $_;
5217                      last INSCOPE;                      last INSCOPE;
5218                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             html => 1,  
   
                             ## NOTE: This element does not appear here, maybe.  
                             table => 1,  
                            }->{$node->[1]}) {  
5219                      !!!cp ('t209');                      !!!cp ('t209');
5220                      last INSCOPE;                      last INSCOPE;
5221                    }                    }
5222                  } # INSCOPE                  } # INSCOPE
5223                  unless (defined $i) {                  unless (defined $i) {
5224                   !!!cp ('t210');                    !!!cp ('t210');
5225  ## TODO: This type is wrong.  ## TODO: This type is wrong.
5226                   !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);                    !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);
5227                    ## Ignore the token                    ## Ignore the token
5228                      !!!nack ('t210.1');
5229                    !!!next-token;                    !!!next-token;
5230                    redo B;                    next B;
5231                  }                  }
5232                                    
5233                  ## Clear back to table row context                  ## Clear back to table row context
5234                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5235                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5236                    !!!cp ('t211');                    !!!cp ('t211');
5237                    ## ISSUE: Can this case be reached?                    ## ISSUE: Can this case be reached?
5238                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4477  sub _tree_construction_main ($) { Line 5243  sub _tree_construction_main ($) {
5243                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5244                    !!!cp ('t212');                    !!!cp ('t212');
5245                    ## reprocess                    ## reprocess
5246                    redo B;                    !!!ack-later;
5247                      next B;
5248                  } else {                  } else {
5249                    !!!cp ('t213');                    !!!cp ('t213');
5250                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
# Line 4489  sub _tree_construction_main ($) { Line 5256  sub _tree_construction_main ($) {
5256                  my $i;                  my $i;
5257                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5258                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5259                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5260                      !!!cp ('t214');                      !!!cp ('t214');
5261                      $i = $_;                      $i = $_;
5262                      last INSCOPE;                      last INSCOPE;
5263                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5264                      !!!cp ('t215');                      !!!cp ('t215');
5265                      last INSCOPE;                      last INSCOPE;
5266                    }                    }
# Line 4507  sub _tree_construction_main ($) { Line 5270  sub _tree_construction_main ($) {
5270  ## TODO: This erorr type ios wrong.  ## TODO: This erorr type ios wrong.
5271                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5272                    ## Ignore the token                    ## Ignore the token
5273                      !!!nack ('t216.1');
5274                    !!!next-token;                    !!!next-token;
5275                    redo B;                    next B;
5276                  }                  }
5277    
5278                  ## Clear back to table body context                  ## Clear back to table body context
5279                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5280                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5281                    !!!cp ('t217');                    !!!cp ('t217');
5282                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5283                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4536  sub _tree_construction_main ($) { Line 5299  sub _tree_construction_main ($) {
5299    
5300                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
5301                  ## Clear back to table context                  ## Clear back to table context
5302                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5303                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5304                    !!!cp ('t219');                    !!!cp ('t219');
5305                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5306                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4546  sub _tree_construction_main ($) { Line 5309  sub _tree_construction_main ($) {
5309                  !!!insert-element ('colgroup',, $token);                  !!!insert-element ('colgroup',, $token);
5310                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5311                  ## reprocess                  ## reprocess
5312                  redo B;                  !!!ack-later;
5313                    next B;
5314                } elsif ({                } elsif ({
5315                          caption => 1,                          caption => 1,
5316                          colgroup => 1,                          colgroup => 1,
5317                          tbody => 1, tfoot => 1, thead => 1,                          tbody => 1, tfoot => 1, thead => 1,
5318                         }->{$token->{tag_name}}) {                         }->{$token->{tag_name}}) {
5319                  ## Clear back to table context                  ## Clear back to table context
5320                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5321                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5322                    !!!cp ('t220');                    !!!cp ('t220');
5323                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5324                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4572  sub _tree_construction_main ($) { Line 5336  sub _tree_construction_main ($) {
5336                                             thead => IN_TABLE_BODY_IM,                                             thead => IN_TABLE_BODY_IM,
5337                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
5338                  !!!next-token;                  !!!next-token;
5339                  redo B;                  !!!nack ('t220.1');
5340                    next B;
5341                } else {                } else {
5342                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
5343                }                }
5344              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5345                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                !!!parse-error (type => 'not closed',
5346                                  value => $self->{open_elements}->[-1]->[0]
5347                                      ->manakai_local_name,
5348                                  token => $token);
5349    
5350                ## As if </table>                ## As if </table>
5351                ## have a table element in table scope                ## have a table element in table scope
5352                my $i;                my $i;
5353                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5354                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5355                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5356                    !!!cp ('t221');                    !!!cp ('t221');
5357                    $i = $_;                    $i = $_;
5358                    last INSCOPE;                    last INSCOPE;
5359                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           #table => 1,  
                           html => 1,  
                          }->{$node->[1]}) {  
5360                    !!!cp ('t222');                    !!!cp ('t222');
5361                    last INSCOPE;                    last INSCOPE;
5362                  }                  }
# Line 4601  sub _tree_construction_main ($) { Line 5366  sub _tree_construction_main ($) {
5366  ## TODO: The following is wrong, maybe.  ## TODO: The following is wrong, maybe.
5367                  !!!parse-error (type => 'unmatched end tag:table', token => $token);                  !!!parse-error (type => 'unmatched end tag:table', token => $token);
5368                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5369                    !!!nack ('t223.1');
5370                  !!!next-token;                  !!!next-token;
5371                  redo B;                  next B;
5372                }                }
5373                                
5374  ## TODO: Followings are removed from the latest spec.  ## TODO: Followings are removed from the latest spec.
5375                ## generate implied end tags                ## generate implied end tags
5376                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
5377                  !!!cp ('t224');                  !!!cp ('t224');
5378                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5379                }                }
5380    
5381                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5382                  !!!cp ('t225');                  !!!cp ('t225');
5383  ## ISSUE: Can this case be reached?                  ## NOTE: |<table><tr><table>|
5384                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                  !!!parse-error (type => 'not closed',
5385                                    value => $self->{open_elements}->[-1]->[0]
5386                                        ->manakai_local_name,
5387                                    token => $token);
5388                } else {                } else {
5389                  !!!cp ('t226');                  !!!cp ('t226');
5390                }                }
# Line 4627  sub _tree_construction_main ($) { Line 5394  sub _tree_construction_main ($) {
5394    
5395                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5396    
5397                ## reprocess            ## reprocess
5398                redo B;            !!!ack-later;
5399              next B;
5400          } elsif ($token->{tag_name} eq 'style') {          } elsif ($token->{tag_name} eq 'style') {
5401            if (not $open_tables->[-1]->[1]) { # tainted            if (not $open_tables->[-1]->[1]) { # tainted
5402              !!!cp ('t227.8');              !!!cp ('t227.8');
5403              ## NOTE: This is a "as if in head" code clone.              ## NOTE: This is a "as if in head" code clone.
5404              $parse_rcdata->(CDATA_CONTENT_MODEL);              $parse_rcdata->(CDATA_CONTENT_MODEL);
5405              redo B;              next B;
5406            } else {            } else {
5407              !!!cp ('t227.7');              !!!cp ('t227.7');
5408              #              #
# Line 4644  sub _tree_construction_main ($) { Line 5412  sub _tree_construction_main ($) {
5412              !!!cp ('t227.6');              !!!cp ('t227.6');
5413              ## NOTE: This is a "as if in head" code clone.              ## NOTE: This is a "as if in head" code clone.
5414              $script_start_tag->();              $script_start_tag->();
5415              redo B;              next B;
5416            } else {            } else {
5417              !!!cp ('t227.5');              !!!cp ('t227.5');
5418              #              #
# Line 4664  sub _tree_construction_main ($) { Line 5432  sub _tree_construction_main ($) {
5432                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5433    
5434                  !!!next-token;                  !!!next-token;
5435                  redo B;                  !!!ack ('t227.2.1');
5436                    next B;
5437                } else {                } else {
5438                  !!!cp ('t227.2');                  !!!cp ('t227.2');
5439                  #                  #
# Line 4693  sub _tree_construction_main ($) { Line 5462  sub _tree_construction_main ($) {
5462                my $i;                my $i;
5463                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5464                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5465                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_ROW_EL) {
5466                    !!!cp ('t228');                    !!!cp ('t228');
5467                    $i = $_;                    $i = $_;
5468                    last INSCOPE;                    last INSCOPE;
5469                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5470                    !!!cp ('t229');                    !!!cp ('t229');
5471                    last INSCOPE;                    last INSCOPE;
5472                  }                  }
# Line 4708  sub _tree_construction_main ($) { Line 5475  sub _tree_construction_main ($) {
5475                  !!!cp ('t230');                  !!!cp ('t230');
5476                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5477                  ## Ignore the token                  ## Ignore the token
5478                    !!!nack ('t230.1');
5479                  !!!next-token;                  !!!next-token;
5480                  redo B;                  next B;
5481                } else {                } else {
5482                  !!!cp ('t232');                  !!!cp ('t232');
5483                }                }
5484    
5485                ## Clear back to table row context                ## Clear back to table row context
5486                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5487                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5488                  !!!cp ('t231');                  !!!cp ('t231');
5489  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
5490                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 4726  sub _tree_construction_main ($) { Line 5493  sub _tree_construction_main ($) {
5493                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5494                $self->{insertion_mode} = IN_TABLE_BODY_IM;                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5495                !!!next-token;                !!!next-token;
5496                redo B;                !!!nack ('t231.1');
5497                  next B;
5498              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5499                if ($self->{insertion_mode} == IN_ROW_IM) {                if ($self->{insertion_mode} == IN_ROW_IM) {
5500                  ## As if </tr>                  ## As if </tr>
# Line 4734  sub _tree_construction_main ($) { Line 5502  sub _tree_construction_main ($) {
5502                  my $i;                  my $i;
5503                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5504                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5505                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5506                      !!!cp ('t233');                      !!!cp ('t233');
5507                      $i = $_;                      $i = $_;
5508                      last INSCOPE;                      last INSCOPE;
5509                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5510                      !!!cp ('t234');                      !!!cp ('t234');
5511                      last INSCOPE;                      last INSCOPE;
5512                    }                    }
# Line 4750  sub _tree_construction_main ($) { Line 5516  sub _tree_construction_main ($) {
5516  ## TODO: The following is wrong.  ## TODO: The following is wrong.
5517                    !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);                    !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);
5518                    ## Ignore the token                    ## Ignore the token
5519                      !!!nack ('t236.1');
5520                    !!!next-token;                    !!!next-token;
5521                    redo B;                    next B;
5522                  }                  }
5523                                    
5524                  ## Clear back to table row context                  ## Clear back to table row context
5525                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5526                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5527                    !!!cp ('t236');                    !!!cp ('t236');
5528  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
5529                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4773  sub _tree_construction_main ($) { Line 5539  sub _tree_construction_main ($) {
5539                  my $i;                  my $i;
5540                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5541                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5542                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5543                      !!!cp ('t237');                      !!!cp ('t237');
5544                      $i = $_;                      $i = $_;
5545                      last INSCOPE;                      last INSCOPE;
5546                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5547                      !!!cp ('t238');                      !!!cp ('t238');
5548                      last INSCOPE;                      last INSCOPE;
5549                    }                    }
# Line 4790  sub _tree_construction_main ($) { Line 5552  sub _tree_construction_main ($) {
5552                    !!!cp ('t239');                    !!!cp ('t239');
5553                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5554                    ## Ignore the token                    ## Ignore the token
5555                      !!!nack ('t239.1');
5556                    !!!next-token;                    !!!next-token;
5557                    redo B;                    next B;
5558                  }                  }
5559                                    
5560                  ## Clear back to table body context                  ## Clear back to table body context
5561                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5562                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5563                    !!!cp ('t240');                    !!!cp ('t240');
5564                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5565                  }                  }
# Line 4823  sub _tree_construction_main ($) { Line 5585  sub _tree_construction_main ($) {
5585                my $i;                my $i;
5586                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5587                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5588                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_EL) {
5589                    !!!cp ('t241');                    !!!cp ('t241');
5590                    $i = $_;                    $i = $_;
5591                    last INSCOPE;                    last INSCOPE;
5592                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5593                    !!!cp ('t242');                    !!!cp ('t242');
5594                    last INSCOPE;                    last INSCOPE;
5595                  }                  }
# Line 4838  sub _tree_construction_main ($) { Line 5598  sub _tree_construction_main ($) {
5598                  !!!cp ('t243');                  !!!cp ('t243');
5599                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5600                  ## Ignore the token                  ## Ignore the token
5601                    !!!nack ('t243.1');
5602                  !!!next-token;                  !!!next-token;
5603                  redo B;                  next B;
5604                }                }
5605                                    
5606                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
# Line 4848  sub _tree_construction_main ($) { Line 5609  sub _tree_construction_main ($) {
5609                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5610                                
5611                !!!next-token;                !!!next-token;
5612                redo B;                next B;
5613              } elsif ({              } elsif ({
5614                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5615                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
# Line 4858  sub _tree_construction_main ($) { Line 5619  sub _tree_construction_main ($) {
5619                  my $i;                  my $i;
5620                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5621                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5622                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5623                      !!!cp ('t247');                      !!!cp ('t247');
5624                      $i = $_;                      $i = $_;
5625                      last INSCOPE;                      last INSCOPE;
5626                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5627                      !!!cp ('t248');                      !!!cp ('t248');
5628                      last INSCOPE;                      last INSCOPE;
5629                    }                    }
# Line 4873  sub _tree_construction_main ($) { Line 5632  sub _tree_construction_main ($) {
5632                      !!!cp ('t249');                      !!!cp ('t249');
5633                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5634                      ## Ignore the token                      ## Ignore the token
5635                        !!!nack ('t249.1');
5636                      !!!next-token;                      !!!next-token;
5637                      redo B;                      next B;
5638                    }                    }
5639                                    
5640                  ## As if </tr>                  ## As if </tr>
# Line 4882  sub _tree_construction_main ($) { Line 5642  sub _tree_construction_main ($) {
5642                  my $i;                  my $i;
5643                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5644                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5645                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5646                      !!!cp ('t250');                      !!!cp ('t250');
5647                      $i = $_;                      $i = $_;
5648                      last INSCOPE;                      last INSCOPE;
5649                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5650                      !!!cp ('t251');                      !!!cp ('t251');
5651                      last INSCOPE;                      last INSCOPE;
5652                    }                    }
# Line 4897  sub _tree_construction_main ($) { Line 5655  sub _tree_construction_main ($) {
5655                      !!!cp ('t252');                      !!!cp ('t252');
5656                      !!!parse-error (type => 'unmatched end tag:tr', token => $token);                      !!!parse-error (type => 'unmatched end tag:tr', token => $token);
5657                      ## Ignore the token                      ## Ignore the token
5658                        !!!nack ('t252.1');
5659                      !!!next-token;                      !!!next-token;
5660                      redo B;                      next B;
5661                    }                    }
5662                                    
5663                  ## Clear back to table row context                  ## Clear back to table row context
5664                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5665                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5666                    !!!cp ('t253');                    !!!cp ('t253');
5667  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
5668                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4919  sub _tree_construction_main ($) { Line 5677  sub _tree_construction_main ($) {
5677                my $i;                my $i;
5678                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5679                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5680                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5681                    !!!cp ('t254');                    !!!cp ('t254');
5682                    $i = $_;                    $i = $_;
5683                    last INSCOPE;                    last INSCOPE;
5684                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5685                    !!!cp ('t255');                    !!!cp ('t255');
5686                    last INSCOPE;                    last INSCOPE;
5687                  }                  }
# Line 4934  sub _tree_construction_main ($) { Line 5690  sub _tree_construction_main ($) {
5690                  !!!cp ('t256');                  !!!cp ('t256');
5691                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5692                  ## Ignore the token                  ## Ignore the token
5693                    !!!nack ('t256.1');
5694                  !!!next-token;                  !!!next-token;
5695                  redo B;                  next B;
5696                }                }
5697    
5698                ## Clear back to table body context                ## Clear back to table body context
5699                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5700                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5701                  !!!cp ('t257');                  !!!cp ('t257');
5702  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
5703                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 4949  sub _tree_construction_main ($) { Line 5705  sub _tree_construction_main ($) {
5705    
5706                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5707                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5708                  !!!nack ('t257.1');
5709                !!!next-token;                !!!next-token;
5710                redo B;                next B;
5711              } elsif ({              } elsif ({
5712                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
5713                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
5714                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5715                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
5716                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5717                !!!cp ('t258');            !!!cp ('t258');
5718                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5719                ## Ignore the token            ## Ignore the token
5720                !!!next-token;            !!!nack ('t258.1');
5721                redo B;             !!!next-token;
5722              next B;
5723          } else {          } else {
5724            !!!cp ('t259');            !!!cp ('t259');
5725            !!!parse-error (type => 'in table:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'in table:/'.$token->{tag_name}, token => $token);
# Line 4970  sub _tree_construction_main ($) { Line 5728  sub _tree_construction_main ($) {
5728            #            #
5729          }          }
5730        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5731          unless ($self->{open_elements}->[-1]->[1] eq 'html' and          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5732                  @{$self->{open_elements}} == 1) { # redundant, maybe                  @{$self->{open_elements}} == 1) { # redundant, maybe
5733            !!!parse-error (type => 'in body:#eof', token => $token);            !!!parse-error (type => 'in body:#eof', token => $token);
5734            !!!cp ('t259.1');            !!!cp ('t259.1');
# Line 4992  sub _tree_construction_main ($) { Line 5750  sub _tree_construction_main ($) {
5750                unless (length $token->{data}) {                unless (length $token->{data}) {
5751                  !!!cp ('t260');                  !!!cp ('t260');
5752                  !!!next-token;                  !!!next-token;
5753                  redo B;                  next B;
5754                }                }
5755              }              }
5756                            
# Line 5003  sub _tree_construction_main ($) { Line 5761  sub _tree_construction_main ($) {
5761                !!!cp ('t262');                !!!cp ('t262');
5762                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5763                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5764                  !!!ack ('t262.1');
5765                !!!next-token;                !!!next-token;
5766                redo B;                next B;
5767              } else {              } else {
5768                !!!cp ('t263');                !!!cp ('t263');
5769                #                #
5770              }              }
5771            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
5772              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
5773                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5774                  !!!cp ('t264');                  !!!cp ('t264');
5775                  !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);                  !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5776                  ## Ignore the token                  ## Ignore the token
5777                  !!!next-token;                  !!!next-token;
5778                  redo B;                  next B;
5779                } else {                } else {
5780                  !!!cp ('t265');                  !!!cp ('t265');
5781                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
5782                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5783                  !!!next-token;                  !!!next-token;
5784                  redo B;                              next B;            
5785                }                }
5786              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
5787                !!!cp ('t266');                !!!cp ('t266');
5788                !!!parse-error (type => 'unmatched end tag:col', token => $token);                !!!parse-error (type => 'unmatched end tag:col', token => $token);
5789                ## Ignore the token                ## Ignore the token
5790                !!!next-token;                !!!next-token;
5791                redo B;                next B;
5792              } else {              } else {
5793                !!!cp ('t267');                !!!cp ('t267');
5794                #                #
5795              }              }
5796        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5797          if ($self->{open_elements}->[-1]->[1] eq 'html' or          if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5798              @{$self->{open_elements}} == 1) { # redundant, maybe              @{$self->{open_elements}} == 1) { # redundant, maybe
5799            !!!cp ('t270.2');            !!!cp ('t270.2');
5800            ## Stop parsing.            ## Stop parsing.
# Line 5046  sub _tree_construction_main ($) { Line 5805  sub _tree_construction_main ($) {
5805            pop @{$self->{open_elements}}; # colgroup            pop @{$self->{open_elements}}; # colgroup
5806            $self->{insertion_mode} = IN_TABLE_IM;            $self->{insertion_mode} = IN_TABLE_IM;
5807            ## Reprocess.            ## Reprocess.
5808            redo B;            next B;
5809          }          }
5810        } else {        } else {
5811          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5812        }        }
5813    
5814            ## As if </colgroup>            ## As if </colgroup>
5815            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5816              !!!cp ('t269');              !!!cp ('t269');
5817  ## TODO: Wrong error type?  ## TODO: Wrong error type?
5818              !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);              !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5819              ## Ignore the token              ## Ignore the token
5820                !!!nack ('t269.1');
5821              !!!next-token;              !!!next-token;
5822              redo B;              next B;
5823            } else {            } else {
5824              !!!cp ('t270');              !!!cp ('t270');
5825              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
5826              $self->{insertion_mode} = IN_TABLE_IM;              $self->{insertion_mode} = IN_TABLE_IM;
5827                !!!ack-later;
5828              ## reprocess              ## reprocess
5829              redo B;              next B;
5830            }            }
5831      } elsif ($self->{insertion_mode} & SELECT_IMS) {      } elsif ($self->{insertion_mode} & SELECT_IMS) {
5832        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5833          !!!cp ('t271');          !!!cp ('t271');
5834          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5835          !!!next-token;          !!!next-token;
5836          redo B;          next B;
5837        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5838              if ($token->{tag_name} eq 'option') {          if ($token->{tag_name} eq 'option') {
5839                if ($self->{open_elements}->[-1]->[1] eq 'option') {            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5840                  !!!cp ('t272');              !!!cp ('t272');
5841                  ## As if </option>              ## As if </option>
5842                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5843                } else {            } else {
5844                  !!!cp ('t273');              !!!cp ('t273');
5845                }            }
5846    
5847                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5848                !!!next-token;            !!!nack ('t273.1');
5849                redo B;            !!!next-token;
5850              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
5851                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
5852                  !!!cp ('t274');            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5853                  ## As if </option>              !!!cp ('t274');
5854                  pop @{$self->{open_elements}};              ## As if </option>
5855                } else {              pop @{$self->{open_elements}};
5856                  !!!cp ('t275');            } else {
5857                }              !!!cp ('t275');
5858              }
5859    
5860                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5861                  !!!cp ('t276');              !!!cp ('t276');
5862                  ## As if </optgroup>              ## As if </optgroup>
5863                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5864                } else {            } else {
5865                  !!!cp ('t277');              !!!cp ('t277');
5866                }            }
5867    
5868                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5869                !!!next-token;            !!!nack ('t277.1');
5870                redo B;            !!!next-token;
5871              next B;
5872          } elsif ($token->{tag_name} eq 'select' or          } elsif ($token->{tag_name} eq 'select' or
5873                   $token->{tag_name} eq 'input' or                   $token->{tag_name} eq 'input' or
5874                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
# Line 5118  sub _tree_construction_main ($) { Line 5881  sub _tree_construction_main ($) {
5881            !!!parse-error (type => 'not closed:select', token => $token);            !!!parse-error (type => 'not closed:select', token => $token);
5882            ## NOTE: As if the token were </select> (<select> case) or            ## NOTE: As if the token were </select> (<select> case) or
5883            ## as if there were </select> (otherwise).            ## as if there were </select> (otherwise).
5884                ## have an element in table scope            ## have an element in table scope
5885                my $i;            my $i;
5886                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5887                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
5888                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
5889                    !!!cp ('t278');                !!!cp ('t278');
5890                    $i = $_;                $i = $_;
5891                    last INSCOPE;                last INSCOPE;
5892                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5893                            table => 1, html => 1,                !!!cp ('t279');
5894                           }->{$node->[1]}) {                last INSCOPE;
5895                    !!!cp ('t279');              }
5896                    last INSCOPE;            } # INSCOPE
5897                  }            unless (defined $i) {
5898                } # INSCOPE              !!!cp ('t280');
5899                unless (defined $i) {              !!!parse-error (type => 'unmatched end tag:select', token => $token);
5900                  !!!cp ('t280');              ## Ignore the token
5901                  !!!parse-error (type => 'unmatched end tag:select', token => $token);              !!!nack ('t280.1');
5902                  ## Ignore the token              !!!next-token;
5903                  !!!next-token;              next B;
5904                  redo B;            }
               }  
5905                                
5906                !!!cp ('t281');            !!!cp ('t281');
5907                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
5908    
5909                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5910    
5911            if ($token->{tag_name} eq 'select') {            if ($token->{tag_name} eq 'select') {
5912              !!!cp ('t281.2');              !!!nack ('t281.2');
5913              !!!next-token;              !!!next-token;
5914              redo B;              next B;
5915            } else {            } else {
5916              !!!cp ('t281.1');              !!!cp ('t281.1');
5917                !!!ack-later;
5918              ## Reprocess the token.              ## Reprocess the token.
5919              redo B;              next B;
5920            }            }
5921          } else {          } else {
5922            !!!cp ('t282');            !!!cp ('t282');
5923            !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);
5924            ## Ignore the token            ## Ignore the token
5925              !!!nack ('t282.1');
5926            !!!next-token;            !!!next-token;
5927            redo B;            next B;
5928          }          }
5929        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5930              if ($token->{tag_name} eq 'optgroup') {          if ($token->{tag_name} eq 'optgroup') {
5931                if ($self->{open_elements}->[-1]->[1] eq 'option' and            if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
5932                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
5933                  !!!cp ('t283');              !!!cp ('t283');
5934                  ## As if </option>              ## As if </option>
5935                  splice @{$self->{open_elements}}, -2;              splice @{$self->{open_elements}}, -2;
5936                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5937                  !!!cp ('t284');              !!!cp ('t284');
5938                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5939                } else {            } else {
5940                  !!!cp ('t285');              !!!cp ('t285');
5941                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5942                  ## Ignore the token              ## Ignore the token
5943                }            }
5944                !!!next-token;            !!!nack ('t285.1');
5945                redo B;            !!!next-token;
5946              } elsif ($token->{tag_name} eq 'option') {            next B;
5947                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'option') {
5948                  !!!cp ('t286');            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5949                  pop @{$self->{open_elements}};              !!!cp ('t286');
5950                } else {              pop @{$self->{open_elements}};
5951                  !!!cp ('t287');            } else {
5952                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!cp ('t287');
5953                  ## Ignore the token              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5954                }              ## Ignore the token
5955                !!!next-token;            }
5956                redo B;            !!!nack ('t287.1');
5957              } elsif ($token->{tag_name} eq 'select') {            !!!next-token;
5958                ## have an element in table scope            next B;
5959                my $i;          } elsif ($token->{tag_name} eq 'select') {
5960                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            ## have an element in table scope
5961                  my $node = $self->{open_elements}->[$_];            my $i;
5962                  if ($node->[1] eq $token->{tag_name}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5963                    !!!cp ('t288');              my $node = $self->{open_elements}->[$_];
5964                    $i = $_;              if ($node->[1] & SELECT_EL) {
5965                    last INSCOPE;                !!!cp ('t288');
5966                  } elsif ({                $i = $_;
5967                            table => 1, html => 1,                last INSCOPE;
5968                           }->{$node->[1]}) {              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5969                    !!!cp ('t289');                !!!cp ('t289');
5970                    last INSCOPE;                last INSCOPE;
5971                  }              }
5972                } # INSCOPE            } # INSCOPE
5973                unless (defined $i) {            unless (defined $i) {
5974                  !!!cp ('t290');              !!!cp ('t290');
5975                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5976                  ## Ignore the token              ## Ignore the token
5977                  !!!next-token;              !!!nack ('t290.1');
5978                  redo B;              !!!next-token;
5979                }              next B;
5980              }
5981                                
5982                !!!cp ('t291');            !!!cp ('t291');
5983                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
5984    
5985                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5986    
5987                !!!next-token;            !!!nack ('t291.1');
5988                redo B;            !!!next-token;
5989              next B;
5990          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5991                   {                   {
5992                    caption => 1, table => 1, tbody => 1,                    caption => 1, table => 1, tbody => 1,
5993                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
5994                   }->{$token->{tag_name}}) {                   }->{$token->{tag_name}}) {
5995  ## TODO: The following is wrong?  ## TODO: The following is wrong?
5996                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5997                                
5998                ## have an element in table scope            ## have an element in table scope
5999                my $i;            my $i;
6000                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6001                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6002                  if ($node->[1] eq $token->{tag_name}) {              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6003                    !!!cp ('t292');                !!!cp ('t292');
6004                    $i = $_;                $i = $_;
6005                    last INSCOPE;                last INSCOPE;
6006                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6007                            table => 1, html => 1,                !!!cp ('t293');
6008                           }->{$node->[1]}) {                last INSCOPE;
6009                    !!!cp ('t293');              }
6010                    last INSCOPE;            } # INSCOPE
6011                  }            unless (defined $i) {
6012                } # INSCOPE              !!!cp ('t294');
6013                unless (defined $i) {              ## Ignore the token
6014                  !!!cp ('t294');              !!!nack ('t294.1');
6015                  ## Ignore the token              !!!next-token;
6016                  !!!next-token;              next B;
6017                  redo B;            }
               }  
6018                                
6019                ## As if </select>            ## As if </select>
6020                ## have an element in table scope            ## have an element in table scope
6021                undef $i;            undef $i;
6022                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6023                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6024                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
6025                    !!!cp ('t295');                !!!cp ('t295');
6026                    $i = $_;                $i = $_;
6027                    last INSCOPE;                last INSCOPE;
6028                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
6029  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
6030                    !!!cp ('t296');                !!!cp ('t296');
6031                    last INSCOPE;                last INSCOPE;
6032                  }              }
6033                } # INSCOPE            } # INSCOPE
6034                unless (defined $i) {            unless (defined $i) {
6035                  !!!cp ('t297');              !!!cp ('t297');
6036  ## TODO: The following error type is correct?  ## TODO: The following error type is correct?
6037                  !!!parse-error (type => 'unmatched end tag:select', token => $token);              !!!parse-error (type => 'unmatched end tag:select', token => $token);
6038                  ## Ignore the </select> token              ## Ignore the </select> token
6039                  !!!next-token; ## TODO: ok?              !!!nack ('t297.1');
6040                  redo B;              !!!next-token; ## TODO: ok?
6041                }              next B;
6042              }
6043                                
6044                !!!cp ('t298');            !!!cp ('t298');
6045                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6046    
6047                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6048    
6049                ## reprocess            !!!ack-later;
6050                redo B;            ## reprocess
6051              next B;
6052          } else {          } else {
6053            !!!cp ('t299');            !!!cp ('t299');
6054            !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);
6055            ## Ignore the token            ## Ignore the token
6056              !!!nack ('t299.3');
6057            !!!next-token;            !!!next-token;
6058            redo B;            next B;
6059          }          }
6060        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6061          unless ($self->{open_elements}->[-1]->[1] eq 'html' and          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6062                  @{$self->{open_elements}} == 1) { # redundant, maybe                  @{$self->{open_elements}} == 1) { # redundant, maybe
6063            !!!cp ('t299.1');            !!!cp ('t299.1');
6064            !!!parse-error (type => 'in body:#eof', token => $token);            !!!parse-error (type => 'in body:#eof', token => $token);
# Line 5317  sub _tree_construction_main ($) { Line 6083  sub _tree_construction_main ($) {
6083            unless (length $token->{data}) {            unless (length $token->{data}) {
6084              !!!cp ('t300');              !!!cp ('t300');
6085              !!!next-token;              !!!next-token;
6086              redo B;              next B;
6087            }            }
6088          }          }
6089                    
# Line 5335  sub _tree_construction_main ($) { Line 6101  sub _tree_construction_main ($) {
6101    
6102          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6103          ## reprocess          ## reprocess
6104          redo B;          next B;
6105        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6106          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6107            !!!cp ('t303');            !!!cp ('t303');
# Line 5350  sub _tree_construction_main ($) { Line 6116  sub _tree_construction_main ($) {
6116          !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);
6117    
6118          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6119            !!!ack-later;
6120          ## reprocess          ## reprocess
6121          redo B;          next B;
6122        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6123          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6124            !!!cp ('t305');            !!!cp ('t305');
# Line 5370  sub _tree_construction_main ($) { Line 6137  sub _tree_construction_main ($) {
6137              !!!parse-error (type => 'unmatched end tag:html', token => $token);              !!!parse-error (type => 'unmatched end tag:html', token => $token);
6138              ## Ignore the token              ## Ignore the token
6139              !!!next-token;              !!!next-token;
6140              redo B;              next B;
6141            } else {            } else {
6142              !!!cp ('t308');              !!!cp ('t308');
6143              $self->{insertion_mode} = AFTER_HTML_BODY_IM;              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6144              !!!next-token;              !!!next-token;
6145              redo B;              next B;
6146            }            }
6147          } else {          } else {
6148            !!!cp ('t309');            !!!cp ('t309');
# Line 5383  sub _tree_construction_main ($) { Line 6150  sub _tree_construction_main ($) {
6150    
6151            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
6152            ## reprocess            ## reprocess
6153            redo B;            next B;
6154          }          }
6155        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6156          !!!cp ('t309.2');          !!!cp ('t309.2');
# Line 5400  sub _tree_construction_main ($) { Line 6167  sub _tree_construction_main ($) {
6167            unless (length $token->{data}) {            unless (length $token->{data}) {
6168              !!!cp ('t310');              !!!cp ('t310');
6169              !!!next-token;              !!!next-token;
6170              redo B;              next B;
6171            }            }
6172          }          }
6173                    
# Line 5428  sub _tree_construction_main ($) { Line 6195  sub _tree_construction_main ($) {
6195              !!!cp ('t315');              !!!cp ('t315');
6196              !!!next-token;              !!!next-token;
6197            }            }
6198            redo B;            next B;
6199          }          }
6200                    
6201          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
# Line 5447  sub _tree_construction_main ($) { Line 6214  sub _tree_construction_main ($) {
6214              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6215            !!!cp ('t318');            !!!cp ('t318');
6216            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6217              !!!nack ('t318.1');
6218            !!!next-token;            !!!next-token;
6219            redo B;            next B;
6220          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
6221                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
6222            !!!cp ('t319');            !!!cp ('t319');
6223            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6224            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6225              !!!ack ('t319.1');
6226            !!!next-token;            !!!next-token;
6227            redo B;            next B;
6228          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
6229            !!!cp ('t320');            !!!cp ('t320');
6230            ## NOTE: As if in body.            ## NOTE: As if in body.
6231            $parse_rcdata->(CDATA_CONTENT_MODEL);            $parse_rcdata->(CDATA_CONTENT_MODEL);
6232            redo B;            next B;
6233          } else {          } else {
6234            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6235              !!!cp ('t321');              !!!cp ('t321');
# Line 5470  sub _tree_construction_main ($) { Line 6239  sub _tree_construction_main ($) {
6239              !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);
6240            }            }
6241            ## Ignore the token            ## Ignore the token
6242              !!!nack ('t322.1');
6243            !!!next-token;            !!!next-token;
6244            redo B;            next B;
6245          }          }
6246        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6247          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
# Line 5486  sub _tree_construction_main ($) { Line 6256  sub _tree_construction_main ($) {
6256    
6257          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6258              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6259            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6260                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
6261              !!!cp ('t325');              !!!cp ('t325');
6262              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
# Line 5499  sub _tree_construction_main ($) { Line 6269  sub _tree_construction_main ($) {
6269            }            }
6270    
6271            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
6272                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6273              !!!cp ('t327');              !!!cp ('t327');
6274              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6275            } else {            } else {
6276              !!!cp ('t328');              !!!cp ('t328');
6277            }            }
6278            redo B;            next B;
6279          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
6280                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6281            !!!cp ('t329');            !!!cp ('t329');
6282            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6283            !!!next-token;            !!!next-token;
6284            redo B;            next B;
6285          } else {          } else {
6286            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6287              !!!cp ('t330');              !!!cp ('t330');
# Line 5522  sub _tree_construction_main ($) { Line 6292  sub _tree_construction_main ($) {
6292            }            }
6293            ## Ignore the token            ## Ignore the token
6294            !!!next-token;            !!!next-token;
6295            redo B;            next B;
6296          }          }
6297        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6298          unless ($self->{open_elements}->[-1]->[1] eq 'html' and          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6299                  @{$self->{open_elements}} == 1) { # redundant, maybe                  @{$self->{open_elements}} == 1) { # redundant, maybe
6300            !!!cp ('t331.1');            !!!cp ('t331.1');
6301            !!!parse-error (type => 'in body:#eof', token => $token);            !!!parse-error (type => 'in body:#eof', token => $token);
# Line 5550  sub _tree_construction_main ($) { Line 6320  sub _tree_construction_main ($) {
6320          !!!cp ('t332');          !!!cp ('t332');
6321          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6322          $script_start_tag->();          $script_start_tag->();
6323          redo B;          next B;
6324        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
6325          !!!cp ('t333');          !!!cp ('t333');
6326          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6327          $parse_rcdata->(CDATA_CONTENT_MODEL);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6328          redo B;          next B;
6329        } elsif ({        } elsif ({
6330                  base => 1, link => 1,                  base => 1, link => 1,
6331                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 5563  sub _tree_construction_main ($) { Line 6333  sub _tree_construction_main ($) {
6333          ## 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
6334          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6335          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6336            !!!ack ('t334.1');
6337          !!!next-token;          !!!next-token;
6338          redo B;          next B;
6339        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
6340          ## 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
6341          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6342          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.
6343    
6344          unless ($self->{confident}) {          unless ($self->{confident}) {
6345            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) {
6346              !!!cp ('t335');              !!!cp ('t335');
6347                ## NOTE: Whether the encoding is supported or not is handled
6348                ## in the {change_encoding} callback.
6349              $self->{change_encoding}              $self->{change_encoding}
6350                  ->($self, $token->{attributes}->{charset}->{value}, $token);                  ->($self, $token->{attributes}->{charset}->{value}, $token);
6351                            
# Line 5581  sub _tree_construction_main ($) { Line 6354  sub _tree_construction_main ($) {
6354                                       $token->{attributes}->{charset}                                       $token->{attributes}->{charset}
6355                                           ->{has_reference});                                           ->{has_reference});
6356            } elsif ($token->{attributes}->{content}) {            } elsif ($token->{attributes}->{content}) {
             ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
6357              if ($token->{attributes}->{content}->{value}              if ($token->{attributes}->{content}->{value}
6358                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                  =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6359                      [\x09-\x0D\x20]*=                      [\x09-\x0D\x20]*=
6360                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6361                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
6362                !!!cp ('t336');                !!!cp ('t336');
6363                  ## NOTE: Whether the encoding is supported or not is handled
6364                  ## in the {change_encoding} callback.
6365                $self->{change_encoding}                $self->{change_encoding}
6366                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6367                $meta_el->[0]->get_attribute_node_ns (undef, 'content')                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
# Line 5613  sub _tree_construction_main ($) { Line 6387  sub _tree_construction_main ($) {
6387            }            }
6388          }          }
6389    
6390            !!!ack ('t338.1');
6391          !!!next-token;          !!!next-token;
6392          redo B;          next B;
6393        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
6394          !!!cp ('t341');          !!!cp ('t341');
6395          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6396          $parse_rcdata->(RCDATA_CONTENT_MODEL);          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6397          redo B;          next B;
6398        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
6399          !!!parse-error (type => 'in body:body', token => $token);          !!!parse-error (type => 'in body:body', token => $token);
6400                                
6401          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
6402              $self->{open_elements}->[1]->[1] ne 'body') {              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6403            !!!cp ('t342');            !!!cp ('t342');
6404            ## Ignore the token            ## Ignore the token
6405          } else {          } else {
# Line 5638  sub _tree_construction_main ($) { Line 6413  sub _tree_construction_main ($) {
6413              }              }
6414            }            }
6415          }          }
6416            !!!nack ('t343.1');
6417          !!!next-token;          !!!next-token;
6418          redo B;          next B;
6419        } elsif ({        } elsif ({
6420                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6421                  div => 1, dl => 1, fieldset => 1,                  div => 1, dl => 1, fieldset => 1,
# Line 5654  sub _tree_construction_main ($) { Line 6430  sub _tree_construction_main ($) {
6430            !!!cp ('t350');            !!!cp ('t350');
6431            !!!parse-error (type => 'in form:form', token => $token);            !!!parse-error (type => 'in form:form', token => $token);
6432            ## Ignore the token            ## Ignore the token
6433              !!!nack ('t350.1');
6434            !!!next-token;            !!!next-token;
6435            redo B;            next B;
6436          }          }
6437    
6438          ## has a p element in scope          ## has a p element in scope
6439          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6440            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6441              !!!cp ('t344');              !!!cp ('t344');
6442              !!!back-token;              !!!back-token; # <form>
6443              $token = {type => END_TAG_TOKEN, tag_name => 'p',              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6444                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6445              redo B;              next B;
6446            } elsif ({            } elsif ($_->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6447              !!!cp ('t345');              !!!cp ('t345');
6448              last INSCOPE;              last INSCOPE;
6449            }            }
# Line 5677  sub _tree_construction_main ($) { Line 6451  sub _tree_construction_main ($) {
6451                        
6452          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6453          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6454              !!!nack ('t346.1');
6455            !!!next-token;            !!!next-token;
6456            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
6457              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
# Line 5693  sub _tree_construction_main ($) { Line 6468  sub _tree_construction_main ($) {
6468            !!!cp ('t347.1');            !!!cp ('t347.1');
6469            $self->{form_element} = $self->{open_elements}->[-1]->[0];            $self->{form_element} = $self->{open_elements}->[-1]->[0];
6470    
6471              !!!nack ('t347.2');
6472            !!!next-token;            !!!next-token;
6473          } elsif ($token->{tag_name} eq 'table') {          } elsif ($token->{tag_name} eq 'table') {
6474            !!!cp ('t382');            !!!cp ('t382');
# Line 5700  sub _tree_construction_main ($) { Line 6476  sub _tree_construction_main ($) {
6476                        
6477            $self->{insertion_mode} = IN_TABLE_IM;            $self->{insertion_mode} = IN_TABLE_IM;
6478    
6479              !!!nack ('t382.1');
6480            !!!next-token;            !!!next-token;
6481          } elsif ($token->{tag_name} eq 'hr') {          } elsif ($token->{tag_name} eq 'hr') {
6482            !!!cp ('t386');            !!!cp ('t386');
6483            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6484                    
6485              !!!nack ('t386.1');
6486            !!!next-token;            !!!next-token;
6487          } else {          } else {
6488            !!!cp ('t347');            !!!nack ('t347.1');
6489            !!!next-token;            !!!next-token;
6490          }          }
6491          redo B;          next B;
6492        } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {        } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
6493          ## has a p element in scope          ## has a p element in scope
6494          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6495            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6496              !!!cp ('t353');              !!!cp ('t353');
6497              !!!back-token;              !!!back-token; # <x>
6498              $token = {type => END_TAG_TOKEN, tag_name => 'p',              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6499                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6500              redo B;              next B;
6501            } elsif ({            } elsif ($_->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6502              !!!cp ('t354');              !!!cp ('t354');
6503              last INSCOPE;              last INSCOPE;
6504            }            }
# Line 5737  sub _tree_construction_main ($) { Line 6512  sub _tree_construction_main ($) {
6512                            dd => {dt => 1, dd => 1}}->{$token->{tag_name}};                            dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6513          LI: {          LI: {
6514            ## Step 2            ## Step 2
6515            if ($li_or_dtdd->{$node->[1]}) {            if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6516              if ($i != -1) {              if ($i != -1) {
6517                !!!cp ('t355');                !!!cp ('t355');
6518                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'not closed',
6519                                $self->{open_elements}->[-1]->[1], token => $token);                                value => $self->{open_elements}->[-1]->[0]
6520                                      ->manakai_local_name,
6521                                  token => $token);
6522              } else {              } else {
6523                !!!cp ('t356');                !!!cp ('t356');
6524              }              }
# Line 5752  sub _tree_construction_main ($) { Line 6529  sub _tree_construction_main ($) {
6529            }            }
6530                        
6531            ## Step 3            ## Step 3
6532            if (not $formatting_category->{$node->[1]} and            if (not ($node->[1] & FORMATTING_EL) and
6533                #not $phrasing_category->{$node->[1]} and                #not $phrasing_category->{$node->[1]} and
6534                ($special_category->{$node->[1]} or                ($node->[1] & SPECIAL_EL or
6535                 $scoping_category->{$node->[1]}) and                 $node->[1] & SCOPING_EL) and
6536                $node->[1] ne 'address' and $node->[1] ne 'div') {                not ($node->[1] & ADDRESS_EL) and
6537                  not ($node->[1] & DIV_EL)) {
6538              !!!cp ('t358');              !!!cp ('t358');
6539              last LI;              last LI;
6540            }            }
# Line 5769  sub _tree_construction_main ($) { Line 6547  sub _tree_construction_main ($) {
6547          } # LI          } # LI
6548                        
6549          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6550            !!!nack ('t359.1');
6551          !!!next-token;          !!!next-token;
6552          redo B;          next B;
6553        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
6554          ## has a p element in scope          ## has a p element in scope
6555          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6556            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6557              !!!cp ('t367');              !!!cp ('t367');
6558              !!!back-token;              !!!back-token; # <plaintext>
6559              $token = {type => END_TAG_TOKEN, tag_name => 'p',              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6560                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6561              redo B;              next B;
6562            } elsif ({            } elsif ($_->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6563              !!!cp ('t368');              !!!cp ('t368');
6564              last INSCOPE;              last INSCOPE;
6565            }            }
# Line 5793  sub _tree_construction_main ($) { Line 6569  sub _tree_construction_main ($) {
6569                        
6570          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
6571                        
6572            !!!nack ('t368.1');
6573          !!!next-token;          !!!next-token;
6574          redo B;          next B;
6575        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
6576          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
6577            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
6578            if ($node->[1] eq 'a') {            if ($node->[1] & A_EL) {
6579              !!!cp ('t371');              !!!cp ('t371');
6580              !!!parse-error (type => 'in a:a', token => $token);              !!!parse-error (type => 'in a:a', token => $token);
6581                            
6582              !!!back-token;              !!!back-token; # <a>
6583              $token = {type => END_TAG_TOKEN, tag_name => 'a',              $token = {type => END_TAG_TOKEN, tag_name => 'a',
6584                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6585              $formatting_end_tag->($token);              $formatting_end_tag->($token);
# Line 5833  sub _tree_construction_main ($) { Line 6610  sub _tree_construction_main ($) {
6610          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6611          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6612    
6613            !!!nack ('t374.1');
6614          !!!next-token;          !!!next-token;
6615          redo B;          next B;
6616        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
6617          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6618    
6619          ## has a |nobr| element in scope          ## has a |nobr| element in scope
6620          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6621            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6622            if ($node->[1] eq 'nobr') {            if ($node->[1] & NOBR_EL) {
6623              !!!cp ('t376');              !!!cp ('t376');
6624              !!!parse-error (type => 'in nobr:nobr', token => $token);              !!!parse-error (type => 'in nobr:nobr', token => $token);
6625              !!!back-token;              !!!back-token; # <nobr>
6626              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
6627                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6628              redo B;              next B;
6629            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6630              !!!cp ('t377');              !!!cp ('t377');
6631              last INSCOPE;              last INSCOPE;
6632            }            }
# Line 5860  sub _tree_construction_main ($) { Line 6635  sub _tree_construction_main ($) {
6635          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6636          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6637                    
6638            !!!nack ('t377.1');
6639          !!!next-token;          !!!next-token;
6640          redo B;          next B;
6641        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
6642          ## has a button element in scope          ## has a button 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 'button') {            if ($node->[1] & BUTTON_EL) {
6646              !!!cp ('t378');              !!!cp ('t378');
6647              !!!parse-error (type => 'in button:button', token => $token);              !!!parse-error (type => 'in button:button', token => $token);
6648              !!!back-token;              !!!back-token; # <button>
6649              $token = {type => END_TAG_TOKEN, tag_name => 'button',              $token = {type => END_TAG_TOKEN, tag_name => 'button',
6650                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6651              redo B;              next B;
6652            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6653              !!!cp ('t379');              !!!cp ('t379');
6654              last INSCOPE;              last INSCOPE;
6655            }            }
# Line 5890  sub _tree_construction_main ($) { Line 6663  sub _tree_construction_main ($) {
6663    
6664          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
6665    
6666            !!!nack ('t379.1');
6667          !!!next-token;          !!!next-token;
6668          redo B;          next B;
6669        } elsif ({        } elsif ({
6670                  xmp => 1,                  xmp => 1,
6671                  iframe => 1,                  iframe => 1,
# Line 5907  sub _tree_construction_main ($) { Line 6681  sub _tree_construction_main ($) {
6681          }          }
6682          ## NOTE: There is an "as if in body" code clone.          ## NOTE: There is an "as if in body" code clone.
6683          $parse_rcdata->(CDATA_CONTENT_MODEL);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6684          redo B;          next B;
6685        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
6686          !!!parse-error (type => 'isindex', token => $token);          !!!parse-error (type => 'isindex', token => $token);
6687                    
6688          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
6689            !!!cp ('t389');            !!!cp ('t389');
6690            ## Ignore the token            ## Ignore the token
6691              !!!nack ('t389'); ## NOTE: Not acknowledged.
6692            !!!next-token;            !!!next-token;
6693            redo B;            next B;
6694          } else {          } else {
6695            my $at = $token->{attributes};            my $at = $token->{attributes};
6696            my $form_attrs;            my $form_attrs;
# Line 5938  sub _tree_construction_main ($) { Line 6713  sub _tree_construction_main ($) {
6713            if ($prompt_attr) {            if ($prompt_attr) {
6714              !!!cp ('t390');              !!!cp ('t390');
6715              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
6716                             line => $token->{line}, column => $token->{column}};                             #line => $token->{line}, column => $token->{column},
6717                              };
6718            } else {            } else {
6719              !!!cp ('t391');              !!!cp ('t391');
6720              push @tokens, {type => CHARACTER_TOKEN,              push @tokens, {type => CHARACTER_TOKEN,
6721                             data => 'This is a searchable index. Insert your search keywords here: ',                             data => 'This is a searchable index. Insert your search keywords here: ',
6722                             line => $token->{line}, column => $token->{column}}; # SHOULD                             #line => $token->{line}, column => $token->{column},
6723                              }; # SHOULD
6724              ## TODO: make this configurable              ## TODO: make this configurable
6725            }            }
6726            push @tokens,            push @tokens,
# Line 5958  sub _tree_construction_main ($) { Line 6735  sub _tree_construction_main ($) {
6735                           line => $token->{line}, column => $token->{column}},                           line => $token->{line}, column => $token->{column}},
6736                          {type => END_TAG_TOKEN, tag_name => 'form',                          {type => END_TAG_TOKEN, tag_name => 'form',
6737                           line => $token->{line}, column => $token->{column}};                           line => $token->{line}, column => $token->{column}};
6738            $token = shift @tokens;            !!!nack ('t391.1'); ## NOTE: Not acknowledged.
6739            !!!back-token (@tokens);            !!!back-token (@tokens);
6740            redo B;            !!!next-token;
6741              next B;
6742          }          }
6743        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
6744          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
6745          my $el;          my $el;
6746          !!!create-element ($el, $token->{tag_name}, $token->{attributes}, $token);          !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
6747                    
6748          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
6749          $self->{content_model} = RCDATA_CONTENT_MODEL;          $self->{content_model} = RCDATA_CONTENT_MODEL;
# Line 5974  sub _tree_construction_main ($) { Line 6752  sub _tree_construction_main ($) {
6752          $insert->($el);          $insert->($el);
6753                    
6754          my $text = '';          my $text = '';
6755            !!!nack ('t392.1');
6756          !!!next-token;          !!!next-token;
6757          if ($token->{type} == CHARACTER_TOKEN) {          if ($token->{type} == CHARACTER_TOKEN) {
6758            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
# Line 6007  sub _tree_construction_main ($) { Line 6786  sub _tree_construction_main ($) {
6786            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
6787          }          }
6788          !!!next-token;          !!!next-token;
6789          redo B;          next B;
6790          } elsif ($token->{tag_name} eq 'math' or
6791                   $token->{tag_name} eq 'svg') {
6792            $reconstruct_active_formatting_elements->($insert_to_current);
6793    
6794            ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
6795    
6796            ## "adjust foreign attributes" - done in insert-element-f
6797            
6798            !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
6799            
6800            if ($self->{self_closing}) {
6801              pop @{$self->{open_elements}};
6802              !!!ack ('t398.1');
6803            } else {
6804              !!!cp ('t398.2');
6805              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
6806              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
6807              ## mode, "in body" (not "in foreign content") secondary insertion
6808              ## mode, maybe.
6809            }
6810    
6811            !!!next-token;
6812            next B;
6813        } elsif ({        } elsif ({
6814                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
6815                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 6017  sub _tree_construction_main ($) { Line 6819  sub _tree_construction_main ($) {
6819          !!!cp ('t401');          !!!cp ('t401');
6820          !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);
6821          ## Ignore the token          ## Ignore the token
6822            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
6823          !!!next-token;          !!!next-token;
6824          redo B;          next B;
6825                    
6826          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
6827        } else {        } else {
# Line 6040  sub _tree_construction_main ($) { Line 6843  sub _tree_construction_main ($) {
6843              }->{$token->{tag_name}}) {              }->{$token->{tag_name}}) {
6844            !!!cp ('t380');            !!!cp ('t380');
6845            push @$active_formatting_elements, ['#marker', ''];            push @$active_formatting_elements, ['#marker', ''];
6846              !!!nack ('t380.1');
6847          } elsif ({          } elsif ({
6848                    b => 1, big => 1, em => 1, font => 1, i => 1,                    b => 1, big => 1, em => 1, font => 1, i => 1,
6849                    s => 1, small => 1, strile => 1,                    s => 1, small => 1, strile => 1,
# Line 6047  sub _tree_construction_main ($) { Line 6851  sub _tree_construction_main ($) {
6851                   }->{$token->{tag_name}}) {                   }->{$token->{tag_name}}) {
6852            !!!cp ('t375');            !!!cp ('t375');
6853            push @$active_formatting_elements, $self->{open_elements}->[-1];            push @$active_formatting_elements, $self->{open_elements}->[-1];
6854              !!!nack ('t375.1');
6855          } elsif ($token->{tag_name} eq 'input') {          } elsif ($token->{tag_name} eq 'input') {
6856            !!!cp ('t388');            !!!cp ('t388');
6857            ## TODO: associate with $self->{form_element} if defined            ## TODO: associate with $self->{form_element} if defined
6858            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6859              !!!ack ('t388.2');
6860          } elsif ({          } elsif ({
6861                    area => 1, basefont => 1, bgsound => 1, br => 1,                    area => 1, basefont => 1, bgsound => 1, br => 1,
6862                    embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                    embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
# Line 6058  sub _tree_construction_main ($) { Line 6864  sub _tree_construction_main ($) {
6864                   }->{$token->{tag_name}}) {                   }->{$token->{tag_name}}) {
6865            !!!cp ('t388.1');            !!!cp ('t388.1');
6866            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6867              !!!ack ('t388.3');
6868          } elsif ($token->{tag_name} eq 'select') {          } elsif ($token->{tag_name} eq 'select') {
6869            ## TODO: associate with $self->{form_element} if defined            ## TODO: associate with $self->{form_element} if defined
6870                    
# Line 6070  sub _tree_construction_main ($) { Line 6877  sub _tree_construction_main ($) {
6877              !!!cp ('t400.2');              !!!cp ('t400.2');
6878              $self->{insertion_mode} = IN_SELECT_IM;              $self->{insertion_mode} = IN_SELECT_IM;
6879            }            }
6880              !!!nack ('t400.3');
6881          } else {          } else {
6882            !!!cp ('t402');            !!!nack ('t402');
6883          }          }
6884                    
6885          !!!next-token;          !!!next-token;
6886          redo B;          next B;
6887        }        }
6888      } elsif ($token->{type} == END_TAG_TOKEN) {      } elsif ($token->{type} == END_TAG_TOKEN) {
6889        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
# Line 6083  sub _tree_construction_main ($) { Line 6891  sub _tree_construction_main ($) {
6891          my $i;          my $i;
6892          INSCOPE: {          INSCOPE: {
6893            for (reverse @{$self->{open_elements}}) {            for (reverse @{$self->{open_elements}}) {
6894              if ($_->[1] eq 'body') {              if ($_->[1] & BODY_EL) {
6895                !!!cp ('t405');                !!!cp ('t405');
6896                $i = $_;                $i = $_;
6897                last INSCOPE;                last INSCOPE;
6898              } elsif ({              } elsif ($_->[1] & SCOPING_EL) {
                       applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                       button => 1, marquee => 1, object => 1, html => 1,  
                      }->{$_->[1]}) {  
6899                !!!cp ('t405.1');                !!!cp ('t405.1');
6900                last;                last;
6901              }              }
# Line 6100  sub _tree_construction_main ($) { Line 6905  sub _tree_construction_main ($) {
6905                            value => $token->{tag_name}, token => $token);                            value => $token->{tag_name}, token => $token);
6906            ## NOTE: Ignore the token.            ## NOTE: Ignore the token.
6907            !!!next-token;            !!!next-token;
6908            redo B;            next B;
6909          } # INSCOPE          } # INSCOPE
6910    
6911          for (@{$self->{open_elements}}) {          for (@{$self->{open_elements}}) {
6912            unless ({            unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
                    dd => 1, dt => 1, li => 1, p => 1, td => 1,  
                    th => 1, tr => 1, body => 1, html => 1,  
                    tbody => 1, tfoot => 1, thead => 1,  
                   }->{$_->[1]}) {  
6913              !!!cp ('t403');              !!!cp ('t403');
6914              !!!parse-error (type => 'not closed:'.$_->[1], token => $token);              !!!parse-error (type => 'not closed',
6915                                value => $_->[0]->manakai_local_name,
6916                                token => $token);
6917              last;              last;
6918            } else {            } else {
6919              !!!cp ('t404');              !!!cp ('t404');
# Line 6119  sub _tree_construction_main ($) { Line 6922  sub _tree_construction_main ($) {
6922    
6923          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
6924          !!!next-token;          !!!next-token;
6925          redo B;          next B;
6926        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
6927          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
6928            ## up-to-date, though it has same effect as speced.
6929            if (@{$self->{open_elements}} > 1 and
6930                $self->{open_elements}->[1]->[1] & BODY_EL) {
6931            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
6932            if ($self->{open_elements}->[-1]->[1] ne 'body') {            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
6933              !!!cp ('t406');              !!!cp ('t406');
6934              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1], token => $token);              !!!parse-error (type => 'not closed',
6935                                value => $self->{open_elements}->[1]->[0]
6936                                    ->manakai_local_name,
6937                                token => $token);
6938            } else {            } else {
6939              !!!cp ('t407');              !!!cp ('t407');
6940            }            }
6941            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6942            ## reprocess            ## reprocess
6943            redo B;            next B;
6944          } else {          } else {
6945            !!!cp ('t408');            !!!cp ('t408');
6946            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6947            ## Ignore the token            ## Ignore the token
6948            !!!next-token;            !!!next-token;
6949            redo B;            next B;
6950          }          }
6951        } elsif ({        } elsif ({
6952                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
# Line 6150  sub _tree_construction_main ($) { Line 6959  sub _tree_construction_main ($) {
6959          my $i;          my $i;
6960          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6961            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6962            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6963              !!!cp ('t410');              !!!cp ('t410');
6964              $i = $_;              $i = $_;
6965              last INSCOPE;              last INSCOPE;
6966            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6967              !!!cp ('t411');              !!!cp ('t411');
6968              last INSCOPE;              last INSCOPE;
6969            }            }
# Line 6173  sub _tree_construction_main ($) { Line 6979  sub _tree_construction_main ($) {
6979                    dt => ($token->{tag_name} ne 'dt'),                    dt => ($token->{tag_name} ne 'dt'),
6980                    li => ($token->{tag_name} ne 'li'),                    li => ($token->{tag_name} ne 'li'),
6981                    p => 1,                    p => 1,
6982                   }->{$self->{open_elements}->[-1]->[1]}) {                   }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
6983              !!!cp ('t409');              !!!cp ('t409');
6984              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6985            }            }
6986    
6987            ## Step 2.            ## Step 2.
6988            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6989                      ne $token->{tag_name}) {
6990              !!!cp ('t412');              !!!cp ('t412');
6991              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);              !!!parse-error (type => 'not closed',
6992                                value => $self->{open_elements}->[-1]->[0]
6993                                    ->manakai_local_name,
6994                                token => $token);
6995            } else {            } else {
6996              !!!cp ('t414');              !!!cp ('t414');
6997            }            }
# Line 6196  sub _tree_construction_main ($) { Line 7006  sub _tree_construction_main ($) {
7006                }->{$token->{tag_name}};                }->{$token->{tag_name}};
7007          }          }
7008          !!!next-token;          !!!next-token;
7009          redo B;          next B;
7010        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
7011          undef $self->{form_element};          undef $self->{form_element};
7012    
# Line 6204  sub _tree_construction_main ($) { Line 7014  sub _tree_construction_main ($) {
7014          my $i;          my $i;
7015          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7016            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7017            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & FORM_EL) {
7018              !!!cp ('t418');              !!!cp ('t418');
7019              $i = $_;              $i = $_;
7020              last INSCOPE;              last INSCOPE;
7021            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7022              !!!cp ('t419');              !!!cp ('t419');
7023              last INSCOPE;              last INSCOPE;
7024            }            }
# Line 6222  sub _tree_construction_main ($) { Line 7029  sub _tree_construction_main ($) {
7029            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7030          } else {          } else {
7031            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7032            while ({            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                   dd => 1, dt => 1, li => 1, p => 1,  
                  }->{$self->{open_elements}->[-1]->[1]}) {  
7033              !!!cp ('t417');              !!!cp ('t417');
7034              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
7035            }            }
7036                        
7037            ## Step 2.            ## Step 2.
7038            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7039                      ne $token->{tag_name}) {
7040              !!!cp ('t417.1');              !!!cp ('t417.1');
7041              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);              !!!parse-error (type => 'not closed',
7042                                value => $self->{open_elements}->[-1]->[0]
7043                                    ->manakai_local_name,
7044                                token => $token);
7045            } else {            } else {
7046              !!!cp ('t420');              !!!cp ('t420');
7047            }              }  
# Line 6242  sub _tree_construction_main ($) { Line 7051  sub _tree_construction_main ($) {
7051          }          }
7052    
7053          !!!next-token;          !!!next-token;
7054          redo B;          next B;
7055        } elsif ({        } elsif ({
7056                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
7057                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 6250  sub _tree_construction_main ($) { Line 7059  sub _tree_construction_main ($) {
7059          my $i;          my $i;
7060          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7061            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7062            if ({            if ($node->[1] & HEADING_EL) {
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
7063              !!!cp ('t423');              !!!cp ('t423');
7064              $i = $_;              $i = $_;
7065              last INSCOPE;              last INSCOPE;
7066            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7067              !!!cp ('t424');              !!!cp ('t424');
7068              last INSCOPE;              last INSCOPE;
7069            }            }
# Line 6270  sub _tree_construction_main ($) { Line 7074  sub _tree_construction_main ($) {
7074            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7075          } else {          } else {
7076            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7077            while ({            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                   dd => 1, dt => 1, li => 1, p => 1,  
                  }->{$self->{open_elements}->[-1]->[1]}) {  
7078              !!!cp ('t422');              !!!cp ('t422');
7079              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
7080            }            }
7081                        
7082            ## Step 2.            ## Step 2.
7083            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7084                      ne $token->{tag_name}) {
7085              !!!cp ('t425');              !!!cp ('t425');
7086              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7087            } else {            } else {
# Line 6290  sub _tree_construction_main ($) { Line 7093  sub _tree_construction_main ($) {
7093          }          }
7094                    
7095          !!!next-token;          !!!next-token;
7096          redo B;          next B;
7097        } elsif ($token->{tag_name} eq 'p') {        } elsif ($token->{tag_name} eq 'p') {
7098          ## has an element in scope          ## has an element in scope
7099          my $i;          my $i;
7100          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7101            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7102            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & P_EL) {
7103              !!!cp ('t410.1');              !!!cp ('t410.1');
7104              $i = $_;              $i = $_;
7105              last INSCOPE;              last INSCOPE;
7106            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7107              !!!cp ('t411.1');              !!!cp ('t411.1');
7108              last INSCOPE;              last INSCOPE;
7109            }            }
7110          } # INSCOPE          } # INSCOPE
7111    
7112          if (defined $i) {          if (defined $i) {
7113            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7114                      ne $token->{tag_name}) {
7115              !!!cp ('t412.1');              !!!cp ('t412.1');
7116              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);              !!!parse-error (type => 'not closed',
7117                                value => $self->{open_elements}->[-1]->[0]
7118                                    ->manakai_local_name,
7119                                token => $token);
7120            } else {            } else {
7121              !!!cp ('t414.1');              !!!cp ('t414.1');
7122            }            }
# Line 6325  sub _tree_construction_main ($) { Line 7129  sub _tree_construction_main ($) {
7129            !!!cp ('t415.1');            !!!cp ('t415.1');
7130            ## As if <p>, then reprocess the current token            ## As if <p>, then reprocess the current token
7131            my $el;            my $el;
7132            !!!create-element ($el, 'p',, $token);            !!!create-element ($el, $HTML_NS, 'p',, $token);
7133            $insert->($el);            $insert->($el);
7134            ## NOTE: Not inserted into |$self->{open_elements}|.            ## NOTE: Not inserted into |$self->{open_elements}|.
7135          }          }
7136    
7137          !!!next-token;          !!!next-token;
7138          redo B;          next B;
7139        } elsif ({        } elsif ({
7140                  a => 1,                  a => 1,
7141                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
# Line 6340  sub _tree_construction_main ($) { Line 7144  sub _tree_construction_main ($) {
7144                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7145          !!!cp ('t427');          !!!cp ('t427');
7146          $formatting_end_tag->($token);          $formatting_end_tag->($token);
7147          redo B;          next B;
7148        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
7149          !!!cp ('t428');          !!!cp ('t428');
7150          !!!parse-error (type => 'unmatched end tag:br', token => $token);          !!!parse-error (type => 'unmatched end tag:br', token => $token);
# Line 6349  sub _tree_construction_main ($) { Line 7153  sub _tree_construction_main ($) {
7153          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7154                    
7155          my $el;          my $el;
7156          !!!create-element ($el, 'br',, $token);          !!!create-element ($el, $HTML_NS, 'br',, $token);
7157          $insert->($el);          $insert->($el);
7158                    
7159          ## Ignore the token.          ## Ignore the token.
7160          !!!next-token;          !!!next-token;
7161          redo B;          next B;
7162        } elsif ({        } elsif ({
7163                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7164                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 6371  sub _tree_construction_main ($) { Line 7175  sub _tree_construction_main ($) {
7175          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7176          ## Ignore the token          ## Ignore the token
7177          !!!next-token;          !!!next-token;
7178          redo B;          next B;
7179                    
7180          ## ISSUE: Issue on HTML5 new elements in spec          ## ISSUE: Issue on HTML5 new elements in spec
7181                    
# Line 6382  sub _tree_construction_main ($) { Line 7186  sub _tree_construction_main ($) {
7186    
7187          ## Step 2          ## Step 2
7188          S2: {          S2: {
7189            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7190              ## Step 1              ## Step 1
7191              ## generate implied end tags              ## generate implied end tags
7192              while ({              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                     dd => 1, dt => 1, li => 1, p => 1,  
                    }->{$self->{open_elements}->[-1]->[1]}) {  
7193                !!!cp ('t430');                !!!cp ('t430');
7194                ## ISSUE: Can this case be reached?                ## ISSUE: Can this case be reached?
7195                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
7196              }              }
7197                    
7198              ## Step 2              ## Step 2
7199              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7200                        ne $token->{tag_name}) {
7201                !!!cp ('t431');                !!!cp ('t431');
7202                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
7203                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                !!!parse-error (type => 'not closed',
7204                                  value => $self->{open_elements}->[-1]->[0]
7205                                      ->manakai_local_name,
7206                                  token => $token);
7207              } else {              } else {
7208                !!!cp ('t432');                !!!cp ('t432');
7209              }              }
# Line 6409  sub _tree_construction_main ($) { Line 7215  sub _tree_construction_main ($) {
7215              last S2;              last S2;
7216            } else {            } else {
7217              ## Step 3              ## Step 3
7218              if (not $formatting_category->{$node->[1]} and              if (not ($node->[1] & FORMATTING_EL) and
7219                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
7220                  ($special_category->{$node->[1]} or                  ($node->[1] & SPECIAL_EL or
7221                   $scoping_category->{$node->[1]})) {                   $node->[1] & SCOPING_EL)) {
7222                !!!cp ('t433');                !!!cp ('t433');
7223                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7224                ## Ignore the token                ## Ignore the token
# Line 6430  sub _tree_construction_main ($) { Line 7236  sub _tree_construction_main ($) {
7236            ## Step 5;            ## Step 5;
7237            redo S2;            redo S2;
7238          } # S2          } # S2
7239          redo B;          next B;
7240        }        }
7241      }      }
7242      redo B;      next B;
7243      } continue { # B
7244        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7245          ## NOTE: The code below is executed in cases where it does not have
7246          ## to be, but it it is harmless even in those cases.
7247          ## has an element in scope
7248          INSCOPE: {
7249            for (reverse 0..$#{$self->{open_elements}}) {
7250              my $node = $self->{open_elements}->[$_];
7251              if ($node->[1] & FOREIGN_EL) {
7252                last INSCOPE;
7253              } elsif ($node->[1] & SCOPING_EL) {
7254                last;
7255              }
7256            }
7257            
7258            ## NOTE: No foreign element in scope.
7259            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7260          } # INSCOPE
7261        }
7262    } # B    } # B
7263    
7264    ## Stop parsing # MUST    ## Stop parsing # MUST
# Line 6479  sub set_inner_html ($$$) { Line 7304  sub set_inner_html ($$$) {
7304    
7305      ## Step 8 # MUST      ## Step 8 # MUST
7306      my $i = 0;      my $i = 0;
7307      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7308      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7309      $p->{set_next_char} = sub {      $p->{set_next_char} = sub {
7310        my $self = shift;        my $self = shift;
7311    
# Line 6489  sub set_inner_html ($$$) { Line 7314  sub set_inner_html ($$$) {
7314    
7315        $self->{next_char} = -1 and return if $i >= length $$s;        $self->{next_char} = -1 and return if $i >= length $$s;
7316        $self->{next_char} = ord substr $$s, $i++, 1;        $self->{next_char} = ord substr $$s, $i++, 1;
7317        $column++;  
7318          ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7319          $p->{column}++;
7320    
7321        if ($self->{next_char} == 0x000A) { # LF        if ($self->{next_char} == 0x000A) { # LF
7322          $line++;          $p->{line}++;
7323          $column = 0;          $p->{column} = 0;
7324          !!!cp ('i1');          !!!cp ('i1');
7325        } elsif ($self->{next_char} == 0x000D) { # CR        } elsif ($self->{next_char} == 0x000D) { # CR
7326          $i++ if substr ($$s, $i, 1) eq "\x0A";          $i++ if substr ($$s, $i, 1) eq "\x0A";
7327          $self->{next_char} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
7328          $line++;          $p->{line}++;
7329          $column = 0;          $p->{column} = 0;
7330          !!!cp ('i2');          !!!cp ('i2');
7331        } elsif ($self->{next_char} > 0x10FFFF) {        } elsif ($self->{next_char} > 0x10FFFF) {
7332          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
# Line 6508  sub set_inner_html ($$$) { Line 7335  sub set_inner_html ($$$) {
7335          !!!cp ('i4');          !!!cp ('i4');
7336          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
7337          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7338          } elsif ($self->{next_char} <= 0x0008 or
7339                   (0x000E <= $self->{next_char} and
7340                    $self->{next_char} <= 0x001F) or
7341                   (0x007F <= $self->{next_char} and
7342                    $self->{next_char} <= 0x009F) or
7343                   (0xD800 <= $self->{next_char} and
7344                    $self->{next_char} <= 0xDFFF) or
7345                   (0xFDD0 <= $self->{next_char} and
7346                    $self->{next_char} <= 0xFDDF) or
7347                   {
7348                    0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
7349                    0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
7350                    0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
7351                    0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
7352                    0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
7353                    0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
7354                    0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
7355                    0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
7356                    0x10FFFE => 1, 0x10FFFF => 1,
7357                   }->{$self->{next_char}}) {
7358            !!!cp ('i4.1');
7359            !!!parse-error (type => 'control char', level => $self->{must_level});
7360    ## TODO: error type documentation
7361        }        }
7362      };      };
7363      $p->{prev_char} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];
# Line 6515  sub set_inner_html ($$$) { Line 7365  sub set_inner_html ($$$) {
7365            
7366      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7367        my (%opt) = @_;        my (%opt) = @_;
7368        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7369          my $column = $opt{column};
7370          if (defined $opt{token} and defined $opt{token}->{line}) {
7371            $line = $opt{token}->{line};
7372            $column = $opt{token}->{column};
7373          }
7374          warn "Parse error ($opt{type}) at line $line column $column\n";
7375      };      };
7376      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7377        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7378      };      };
7379            
7380      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
# Line 6542  sub set_inner_html ($$$) { Line 7398  sub set_inner_html ($$$) {
7398          unless defined $p->{content_model};          unless defined $p->{content_model};
7399          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
7400    
7401      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7402          ## TODO: Foreign element OK?
7403    
7404      ## Step 3      ## Step 3
7405      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
# Line 6552  sub set_inner_html ($$$) { Line 7409  sub set_inner_html ($$$) {
7409      $doc->append_child ($root);      $doc->append_child ($root);
7410    
7411      ## Step 5 # MUST      ## Step 5 # MUST
7412      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7413    
7414      undef $p->{head_element};      undef $p->{head_element};
7415    
# Line 6598  sub set_inner_html ($$$) { Line 7455  sub set_inner_html ($$$) {
7455      ## ISSUE: mutation events?      ## ISSUE: mutation events?
7456    
7457      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
7458    
7459        delete $p->{parse_error}; # delete loop
7460    } else {    } else {
7461      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";
7462    }    }

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24